This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

HTTP Example. Secure Error or Stuck

Hi! 
Found in this post https://devzone.nordicsemi.com/f/nordic-q-a/43514/nrf9160-http-and-other-examples
about http client example, and link to repo to examles.
All libs is up to date. (zephyr, nrf, nrfxlib, mcuboot)

I have two error types:
1. Hardfault error. After getaddr return -1.
2. Forever stuck in this stage

06 NRF_IPC		Non-Secure	OK
07 NRF_VMC		Non-Secure	OK
08 NRF_FPU		Non-Secure	ERROR
09 NRF_EGU1		Non-Secure	OK
10 NRF_EGU2		Non-Secure	OK
11 NRF_TWIM2		Non-Secure	OK
12 NRF_SPIM3		Non-Secure	OK
13 NRF_TIMER0		Non-Secure	OK
14 NRF_TIMER1		Non-Secure	OK
15 NRF_TIMER2		Non-Secure	OK
16 NRF_GPIOTE1		Non-Secure	OK

SPM: MSP_NS 20024e00
SPM: prepare to jump to Non-Secure image
***** Booting Zephyr OS v1.13.99-ncs1-5891-gd70aa052bc4b *****

  • Hi Moshe,
    There is an issue with the samples in that repo, mainy that they are missing a config in the prj.conf file.

    It should work as normal if you add:

    CONFIG_MAIN_STACK_SIZE=4096
    in the prj.conf file to the sample and then build/flash it.

    I have added a suggestion for the prj.conf file here:
    CONFIG_BSD_LIBRARY=y
    CONFIG_GPIO=n
    CONFIG_SERIAL=y
    CONFIG_STDOUT_CONSOLE=y
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_TEST_RANDOM_GENERATOR=y
    CONFIG_NETWORKING=y
    CONFIG_NET_BUF_USER_DATA_SIZE=1
    CONFIG_NET_SOCKETS_OFFLOAD=y
    CONFIG_NET_SOCKETS=y
    CONFIG_NET_SOCKETS_POSIX_NAMES=y
    CONFIG_NET_RAW_MODE=y
    CONFIG_TRUSTED_EXECUTION_NONSECURE=y
    CONFIG_LOG=n
    CONFIG_LOG_DEFAULT_LEVEL=4
    CONFIG_HEAP_MEM_POOL_SIZE=1024
    # LTE link control
    CONFIG_LTE_LINK_CONTROL=y
    CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
    
    # Main thread
    CONFIG_MAIN_THREAD_PRIORITY=7
    CONFIG_MAIN_STACK_SIZE=4096
    And added that them modem starts in the main application, to make it function a little bit more like the official samples:
    /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <net/socket.h>
    #include <stdio.h>
    #include <uart.h>
    #include <string.h>
    #include <lte_lc.h>
    
    #define HTTP_HOST "google.com"
    #define HTTP_PORT 80
    #define RECV_BUF_SIZE 8192
    
    char recv_buf[RECV_BUF_SIZE + 1];
    
    int blocking_recv(int fd, u8_t *buf, u32_t size, u32_t flags)
    {
    	int err;
    
    	do {
    		err = recv(fd, buf, size, flags);
    	} while (err < 0 && errno == EAGAIN);
    
    	return err;
    }
    
    int blocking_send(int fd, u8_t *buf, u32_t size, u32_t flags)
    {
    	int err;
    
    	do {
    		err = send(fd, buf, size, flags);
    	} while (err < 0 && errno == EAGAIN);
    
    	return err;
    }
    
    int blocking_connect(int fd, struct sockaddr *local_addr, socklen_t len)
    {
    	int err;
    
    	do {
    		err = connect(fd, local_addr, len);
    	} while (err < 0 && errno == EAGAIN);
    
    	return err;
    }
    
    void app_http_start(void)
    {
    	struct sockaddr_in local_addr;
    	struct addrinfo *res;
    
    	local_addr.sin_family = AF_INET;
    	local_addr.sin_port = htons(0);
    	local_addr.sin_addr.s_addr = 0;
    
    	printk("HTTP example\n\r");
    
    	int err = getaddrinfo(HTTP_HOST, NULL, NULL, &res);
    
    	printk("getaddrinfo err: %d\n\r", err);
    	((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTP_PORT);
    
    	char send_buf[] =
    		"GET / HTTP/1.1\r\nHost: www.google.com:80\r\nConnection: close\r\n\r\n";
    	int send_data_len = strlen(send_buf);
    
    	int client_fd = socket(AF_INET, SOCK_STREAM, 0);
    
    	printk("client_fd: %d\n\r", client_fd);
    	err = bind(client_fd, (struct sockaddr *)&local_addr,
    		   sizeof(local_addr));
    	printk("bind err: %d\n\r", err);
    	err = blocking_connect(client_fd, (struct sockaddr *)res->ai_addr,
    			       sizeof(struct sockaddr_in));
    	printk("connect err: %d\n\r", err);
    
    	int num_bytes = send(client_fd, send_buf, send_data_len, 0);
    
    	printk("send err: %d\n\r", num_bytes);
    
    	int tot_num_bytes = 0;
    
    	do {
    		/* TODO: make a proper timeout *
    		 * Current solution will just hang 
    		 * until remote side closes connection */
    		num_bytes =
    			blocking_recv(client_fd, recv_buf, RECV_BUF_SIZE, 0);
    		tot_num_bytes += num_bytes;
    
    		if (num_bytes <= 0) {
    			break;
    		}
    		printk("%s", recv_buf);
    	} while (num_bytes > 0);
    
    	printk("\n\rFinished. Closing socket");
    	err = close(client_fd);
    }
    
    
    /**@brief Configures modem to provide LTE link. Blocks until link is
     * successfully established.
     */
    static void modem_configure(void)
    {
    #if defined(CONFIG_LTE_LINK_CONTROL)
    	if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
    		/* Do nothing, modem is already turned on
    		 * and connected.
    		 */
    	} else {
    		int err;
    
    		printk("LTE Link Connecting ...\n");
    		err = lte_lc_init_and_connect();
    		__ASSERT(err == 0, "LTE link could not be established.");
    		printk("LTE Link Connected!\n");
    	}
    #endif
    }
    
    int main(void)
    {
    	printk("HTTP Sample Started\n");
    	modem_configure();
    	app_http_start();
    	while (1) {
    		;
    	}
    }
    
    Best Regards,
    Martin L.
  • Hey, I paste everything you wrote, but there is still error.


    Peripheral		Domain		Status
    00 NRF_P0		Non-Secure	OK
    01 NRF_CLOCK		Non-Secure	OK
    02 NRF_RTC1		Non-Secure	OK
    03 NRF_NVMC		Non-Secure	OK
    04 NRF_UARTE1		Non-Secure	OK
    05 NRF_UARTE2		Secure		SKIP
    06 NRF_IPC		Non-Secure	OK
    07 NRF_VMC		Non-Secure	OK
    08 NRF_FPU		Non-Secure	ERROR
    09 NRF_EGU1		Non-Secure	OK
    10 NRF_EGU2		Non-Secure	OK
    11 NRF_TWIM2		Non-Secure	OK
    12 NRF_SPIM3		Non-Secure	OK
    13 NRF_TIMER0		Non-Secure	OK
    14 NRF_TIMER1		Non-Secure	OK
    15 NRF_TIMER2		Non-Secure	OK
    16 NRF_GPIOTE1		Non-Secure	OK
    
    SPM: MSP_NS 20024e00
    SPM: prepare to jump to Non-Secure image
    ***** Booting Zephyr OS v1.13.99-ncs1-5891-gd70aa052bc4b *****
    HTTP Sample Started
    LTE Link Connecting ...
    LTE Link Connected!
    HTTP example
    
    getaddrinfo err: -1
    
    Exception occurred in Secure State
    ***** HARD FAULT *****
      Fault escalation (see below)
    ***** BUS FAULT *****
      Precise data bus error
      BFAR Address: 0x50008120
    ***** Hardware exception *****
    Current thread ID = 0x20020130
    Faulting instruction address = 0x41376
    Fatal fault in ISR! Spinning...

  • Hi Moshe,

     I assume that you have tested some other samples and know that you have coverage and made connection with the network.


    Since the "nrf9160_samples" is it's own branch in a forked repository(rallare/nrf) of the nrf repo, it is behind of a lot of commits which is in the master branch.
    This means that if you are going to work with those samples, you need to be in sync. with the same stage as the rallare/nrf repository.

    • So if you are working in the nrf master branch, you need to revert to the correct stage which syncs up with the forked repository.

    cd ncs/nrf
    git checkout b95410c398d334050ef029eebf159e43905423d1
    west update

    Then you should be able to paste in the http sample and build/run it correctly.


    • If you have cloned the forked "rallare/nrf" repository and checked out the "nrf9160_samples" branch you should do a "west update" inside that branch to sync up the rest of the repos (zephyr, nrfxlib..)

    This is shown in detail in this guide: : 

    How_to_add_a_remote_branch_into_your_own_local_repository_v1.2.pdf


    by the way this is the correct output of the sample:

    Best Regards,

    Martin L.

Related