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

Connecting an nRF9160 to AWS

Hello,

I'm attempting to connect the nRF9160 DK to our AWS IoT account. I've been following the information in this question: https://devzone.nordicsemi.com/f/nordic-q-a/44528/switch-cloud-endpoint-from-nordic-aws-to-our-own-aws-account but I'm having an issue establishing the connection.

So far I have been able to call the certificates from a replica of 'certificates.h' and flash these onto the nRF91 using the nrf_inbuilt_key commands and have been able to establish a broker connection but receive a time out error when connecting to AWS with MQTT. I am currently modifying mqtt_simple to acheve this connection.

So far I have updated 'pj.conf' with:

# General config
CONFIG_TEST_RANDOM_GENERATOR=y

# Networking
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y

# LTE link control
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
CONFIG_NRF_CLOUD_PROVISION_CERTIFICATES=y

# BSD library
CONFIG_BSD_LIBRARY=y

# AT Host
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_AT_HOST_LIBRARY=y

# MQTT
CONFIG_MQTT_LIB=y
CONFIG_MQTT_LIB_TLS=y

# Appliaction
CONFIG_MQTT_PUB_TOPIC="my/publish/topic"
CONFIG_MQTT_SUB_TOPIC="my/subscribe/topic"
CONFIG_MQTT_CLIENT_ID="MQTT_TEST"
CONFIG_MQTT_BROKER_HOSTNAME="xxxxxxxxxxx.iot.eu-west-1.amazonaws.com" (redacted)
CONFIG_MQTT_BROKER_PORT=8883
CONFIG_NRF_CLOUD_SEC_TAG=1234

# Main thread
CONFIG_MAIN_THREAD_PRIORITY=7
CONFIG_MAIN_STACK_SIZE=4096

CONFIG_HEAP_MEM_POOL_SIZE=1024

I have added the following additional functions to mqtt_simple:

#include "certificates.h"

#define NRF_CLOUD_HOSTNAME CONFIG_MQTT_BROKER_HOSTNAME
#define NRF_CLOUD_SEC_TAG CONFIG_NRF_CLOUD_SEC_TAG

static struct nct {
	struct mqtt_sec_config tls_config;
	struct mqtt_client client;
	struct sockaddr_storage broker;
	struct mqtt_utf8 dc_tx_endp;
	struct mqtt_utf8 dc_rx_endp;
	u32_t message_id;
} nct;

static void client_init(struct mqtt_client *client)
{
	mqtt_client_init(client);

	broker_init();

	/* MQTT client configuration */
	client->broker = &broker;
	client->evt_cb = mqtt_evt_handler;
	client->client_id.utf8 = (u8_t *)CONFIG_MQTT_CLIENT_ID;
	client->client_id.size = strlen(CONFIG_MQTT_CLIENT_ID);
	client->password = NULL;
	client->user_name = NULL;
	client->protocol_version = MQTT_VERSION_3_1_1;

	/* MQTT buffers configuration */
	client->rx_buf = rx_buffer;
	client->rx_buf_size = sizeof(rx_buffer);
	client->tx_buf = tx_buffer;
	client->tx_buf_size = sizeof(tx_buffer);

	nct.client.transport.type = MQTT_TRANSPORT_SECURE;

	struct mqtt_sec_config *tls_config = &nct.client.transport.tls.config;

	memcpy(tls_config, &nct.tls_config, sizeof(struct mqtt_sec_config));
}

static int nct_provision(void)
{
    printk("nct provisions active\n");
    
    static sec_tag_t sec_tag_list[] = {NRF_CLOUD_SEC_TAG};
    
    nct.tls_config.peer_verify = 2;
    nct.tls_config.cipher_count = 0;
    nct.tls_config.cipher_list = NULL;
    nct.tls_config.sec_tag_count = ARRAY_SIZE(sec_tag_list);
    nct.tls_config.sec_tag_list = sec_tag_list;
    nct.tls_config.hostname = CONFIG_MQTT_BROKER_HOSTNAME;
    
    
    int err;
    
    printk("BSD and Cloud provisions defined\n");
    /* Delete certificates */
    nrf_sec_tag_t sec_tag = NRF_CLOUD_SEC_TAG;
    
    for (nrf_key_mgnt_cred_type_t type = 0; type < 5; type++) {
    	err = nrf_inbuilt_key_delete(sec_tag, type);
    	printk("nrf_inbuilt_key_delete(%d, %d) => result=%d\n",
    		sec_tag, type, err);
    }
    err = nrf_inbuilt_key_delete(0,0);
    printk("nrf_inbuilt_key_delete(0, 0) => result=%d\n",
    	err);
    /* Provision CA Certificate. */
    err = nrf_inbuilt_key_write(NRF_CLOUD_SEC_TAG,
    			NRF_KEY_MGMT_CRED_TYPE_CA_CHAIN,
    			NRF_CLOUD_CA_CERTIFICATE,
    			strlen(NRF_CLOUD_CA_CERTIFICATE));
    printk("nrf_inbuilt_key_write(%d, root-CA => result=%d)\n",
    	NRF_CLOUD_SEC_TAG, err);
    if (err) {
    	printk("NRF_CLOUD_CA_CERTIFICATE err: %d", err);
    	return err;
    }
    
    /* Provision Private Certificate. */
    err = nrf_inbuilt_key_write(
    	NRF_CLOUD_SEC_TAG,
    	NRF_KEY_MGMT_CRED_TYPE_PRIVATE_CERT,
    	NRF_CLOUD_CLIENT_PRIVATE_KEY,
    	strlen(NRF_CLOUD_CLIENT_PRIVATE_KEY));
    	printk("nrf_inbuilt_key_write(%d, Private Key => result=%d)\n",
    		NRF_CLOUD_SEC_TAG, err);
    if (err) {
    	printk("NRF_CLOUD_CLIENT_PRIVATE_KEY err: %d", err);
    	return err;
    }
    
    /* Provision Public Certificate. */
    err = nrf_inbuilt_key_write(
    	NRF_CLOUD_SEC_TAG,
    	NRF_KEY_MGMT_CRED_TYPE_PUBLIC_CERT,
    	NRF_CLOUD_CLIENT_PUBLIC_CERTIFICATE,
    	strlen(NRF_CLOUD_CLIENT_PUBLIC_CERTIFICATE));
    	printk("nrf_inbuilt_key_write(%d, Public Cert => result=%d)\n",
    		NRF_CLOUD_SEC_TAG, err);
    if (err) {
    	printk("NRF_CLOUD_CLIENT_PUBLIC_CERTIFICATE err: %d",
    		err);
    	return err;
    }
    return 0;
}

void main(void)
{
	int err;
	int err_provision;
	printk("The MQTT simple sample started\n");

 	err_provision = nct_provision();
	if (err_provision != 0) {
		printk("ERROR: nct_provision failure %d\n", err_provision);
		return;
	}
	printk("err_provision = %d\n", err_provision);
	modem_configure();

	client_init(&client);

I have changed the following in Kconfig:

menu "MQTT simple sample"
config MQTT_PUB_TOPIC
	string "MQTT publish topic"
	default "my/publish/topic"

config MQTT_SUB_TOPIC
	string "MQTT subscribe topic"
	default "my/subscribe/topic"

config MQTT_CLIENT_ID
	string "MQTT Client ID"
	default "MQTT_TEST"

config MQTT_BROKER_HOSTNAME
	string "MQTT broker hostname"
	default "xxxxxxxxx.iot.eu-west-1.amazonaws.com" (redacted)

config MQTT_BROKER_PORT
	int "MQTT broker port"
	default 8883

config MQTT_MESSAGE_BUFFER_SIZE
	int ""
	default 128

config MQTT_PAYLOAD_BUFFER_SIZE
	int ""
	default 128

config NRF_CLOUD_SEC_TAG
	int ""
	default 1234

Finally, the certs are inside certificates.h:

/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: BSD-5-Clause-Nordic
 */

#define NRF_CLOUD_CLIENT_ID "MQTT_TEST"

#define NRF_CLOUD_CLIENT_PRIVATE_KEY \
	"-----BEGIN RSA PRIVATE KEY-----\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"-----END RSA PRIVATE KEY-----\r\n"

#define NRF_CLOUD_CLIENT_PUBLIC_CERTIFICATE \
	"-----BEGIN CERTIFICATE-----\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"-----END CERTIFICATE-----\r\n"

#define NRF_CLOUD_CA_CERTIFICATE \
	"-----BEGIN CERTIFICATE-----\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"xxxxxxx\r\n" \
	"-----END CERTIFICATE-----\r\n"

I'm not sure what is still left to change in order to make the connection to AWS.

Parents
  • Hi,

     

    You are using \r\n as the line ending (normal is \n only), but both shall be accepted by the nrf_inbuilt API.

    I do not see any problems specific to your implementation, but what you could do is to verify that you have the latest modem installed, which can be found here:

    https://www.nordicsemi.com/Products/Low-power-cellular-IoT/nRF9160/Download#infotabs

     

    Do you always get the same error message, even if you try several times? if you enter debug mode, and debug the function mqtt_transport_socket_tls.c::mqtt_client_tls_connect(), which function returns the err?

     

    Kind regards,

    Håkon

  • Apologises for the delayed response. I have identified the issue with the connection. Turns out the network provider requires a whitelisted IP address that is incompatible with the dynamic IPs of AWS IoT Core.

    As the device I am using is deployed in another area of the UK, I can't manually update the modem to the latest version without risking something going wrong. I'd rather avoid updating the modem at this moment if I can get away with it. The device is hooked up to a Raspberry Pi with internet access which can faciliate re-flashing with JLinkExe but hasn't been set up with the modem update tools.

    I've been busy figuring out a counter to this issue and have decided to take a different path and use the BSD library to create a UDP connection to a static IP bounded EC2 instance on AWS. I have encountered a different problem with setting up this kind of connection however.

    The code compiles but the nRF9160 reports back a hard fault when the code is loaded onto the device. It will run the code until the UDP IPv4 socket information structure (struct nrf_sockaddr_in) is populated. I've tried to follow what examples I have found of this structure in the ncs repo but I seem to be missing something.

    This is were it appears the code gets to before hard fault, I placed printks to figure out where the code reaches before faulting:

    /**@brief Configures modem to test connection. Blocks until link is
     * successfully established. Creates a UDP socket and connects to
     * AWS EC2 instance
     */
    static int UDP_Socket_Config_and_Connect(void)
    {
    	int err;
    	int UDP_Socket;
    
    	// Sets up UDP Socket information
    	struct nrf_sockaddr_in *UDP_sockaddr;
    			printk("code checkpoint 0\n");
    	UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    			printk("code checkpoint 1\n");
    	UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    			printk("code checkpoint 2\n");
    	UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    			printk("code checkpoint 3\n");
    	UDP_sockaddr->sin_addr.s_addr	    	= 	    0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    			printk("code checkpoint 4\n");
    
    	UDP_Socket = nrf_socket(NRF_AF_LTE, NRF_SOCK_DGRAM, NRF_IPPROTO_UDP); //Opens UDP socket using UDP protocols
    	if (UDP_Socket == -1) {
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	printk("Socket created for init\n");
    
    	err = nrf_connect(UDP_Socket, UDP_sockaddr, UDP_sockaddr->sin_len); //Tests connection to UDP IPv4 address
    	if(err)
    	{
    		printk("Failure to establish connection via nrf_connect, closing socket\n");
    		close(UDP_Socket);
    	}
    	close(UDP_Socket);
    	printk("Config and Connect Successful\n");
    	return err;
    }

    This is the UART information when the device resets until faulting:

    Flash region		Domain		Permissions
    00 0x00000 0x02000 	Secure		rwxl
    01 0x02000 0x04000 	Non-Secure	rwxl
    02 0x04000 0x06000 	Non-Secure	rwxl
    03 0x06000 0x08000 	Non-Secure	rwxl
    04 0x08000 0x0a000 	Non-Secure	rwxl
    05 0x0a000 0x0c000 	Non-Secure	rwxl
    06 0x0c000 0x0e000 	Non-Secure	rwxl
    07 0x0e000 0x10000 	Non-Secure	rwxl
    08 0x10000 0x12000 	Non-Secure	rwxl
    09 0x12000 0x14000 	Non-Secure	rwxl
    10 0x14000 0x16000 	Non-Secure	rwxl
    11 0x16000 0x18000 	Non-Secure	rwxl
    12 0x18000 0x1a000 	Non-Secure	rwxl
    13 0x1a00ecure	rwxl
    14 0x1c000 0x1e000 	Non-Secure	rwxl
    15 0x1e000 0x20000 	Non-Secure	rwxl
    16 0x20000 0x22000 	Non-Secure	rwxl
    17 0x22000 0x24000 	Non-Secure	rwxl
    18 0x24000 0x26000 	Non-Secure	rwxl
    19 0x26000 0x28000 	Non-Secure	rwxl
    20 0x28000 0x2a000 	Non-Secure	rwxl
    21 0x2a000 0x2c000 	Non-Secure	rwxl
    22 0x2c000 0x2e000 	Non-Secure	rwxl
    23 0x2e000 0x30000 	Non-Secure	rwxl
    24 0x30000 0x32000 	Non-Secure	rwxl
    25 0x32000 0x34000 	Non-Secure	rwxl
    26 0x34000 0x36000 	Non-Secure	rwxl
    27 0x36000 0x38000 	Non-Secure	rwxl
    28 0x38000 0x3a000 	Non-Secure	rwxl
    29 0x3a000 0x3c000 	Non-Secure	rwxl
    30 0x3c000 0x3e000 	Non-Secure	rwxl
    31 0x3e000 0x40000 	Non-Secure	rwxl
    
    SRAM region		Domain		Permissions
    00 0x00000 0x01000	Secure		rwxl
    01 0x01000 0x02000	Secure		rwxl
    02 0x02000 0x03000	Secure		rwxl
    03 0x03000 0x04000	Secure		rwxl
    04 0x04000 0x05000	Secure		rwxl
    05 0x05000 0x06000	Secure		rwxl
    06 0x06000 0x07000	Secure		rwxl
    07 0x07000 0x08000	Secure		rwxl
    08 0x08000 0x09000	Non-Secure	rwxl
    09 0x09000 0x0a000	Non-Secure	rwxl
    10 0x0a000 0x0b000	Non-Secure	rwxl
    11 0x0b000 0x0c000	Non-Secure	rwxl
    12 0x0c000 0x0d000	Non-Secure	rwxl
    13 0x0d000 0x0e000	Non-Secure	rwxl
    14 0x0e000 0x0f000	Non-Secure	rwxl
    15 0x0f000 0x10000	Non-Secure	rwxl
    16 0x10000 0x11000	Non-Secure	rwxl
    17 0x11000 0x12000	Non-Secure	rwxl
    18 0x12000 0x13000	Non-Secure	rwxl
    19 0x13000 0x14000	Non-Secure	rwxl
    20 0x14000 0x15000	Non-Secure	rwxl
    21 0x15000 0x16000	Non-Secure	rwxl
    22 0x16000 0x17000	Non-Secure	rwxl
    23 0x17000 0x18000	Non-Secure	rwxl
    24 0x18000 0x19000	Non-Secure	rwxl
    25 0x19000 0x1a000	Non-Secure	rwxl
    26 0x1a000 0x1b000	Non-Secure	rwxl
    27 0x1b000 0x1c000	Non-Secure	rwxl
    28 0x1c000 0x1d000	Non-Secure	rwxl
    29 0x1d000 0x1e000	Non-Secure	rwxl
    30 0x1e000 0x1f000	Non-Secure	rwxl
    31 0x1f000 0x20000	Non-Secure	rwxl
    
    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_SAADC		Non-Secure	OK
    17 NRF_GPIOTE1		Non-Secure	OK
    
    SPM: MSP_NS 20022e00
    SPM: prepare to jump to Non-Secure image
    ***** Booting Zephyr OS v1.13.99-ncs1-6044-g4eaddb50e90b *****
    UDP_Test Application started
    code checkpoint 0
    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 = 0x20020150
    Faulting instruction address = 0x93da
    Fatal fault in ISR! Spinning...

    This is main.c

    /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <stdio.h>
    #include <uart.h>
    #include <string.h>
    
    //#include "certificates.h"
    //#include "provisions/provision.h"
    #include "../../../../../nrfxlib/bsdlib/include/nrf_socket.h"
    
    #if defined(CONFIG_BSD_LIBRARY)
    
    /**@brief Recoverable BSD library error. */
    void bsd_recoverable_error_handler(uint32_t err)
    {
    	printk("bsdlib recoverable error: %u\n", err);
    }
    
    /**@brief Irrecoverable BSD library error. */
    void bsd_irrecoverable_error_handler(uint32_t err)
    {
    	printk("bsdlib irrecoverable error: %u\n", err);
    
    	__ASSERT_NO_MSG(false);
    }
    
    #endif /* defined(CONFIG_BSD_LIBRARY) */
    
    // #if defined(CONFIG_BSD_LIBRARY) //Includes nrf secure key manager code, not impliment yet
    // #include "nrf_inbuilt_key.h"
    // #endif
    
    /**@brief Configures modem to test connection. Blocks until link is
     * successfully established. Creates a UDP socket and connects to
     * AWS EC2 instance
     */
    static int UDP_Socket_Config_and_Connect(void)
    {
    	int err;
    	int UDP_Socket;
    
    	// Sets up UDP Socket information
    	struct nrf_sockaddr_in *UDP_sockaddr;
    			printk("code checkpoint 0\n");
    	UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);						/**< Length of this data structure */
    			printk("code checkpoint 1\n");
    	UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	/**< Port, in network byte order */
    			printk("code checkpoint 2\n");
    	UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    			printk("code checkpoint 3\n");
    	UDP_sockaddr->sin_addr.s_addr		= 	0xXXXXXXXX;											/**< IPv4 address, in hex format */
    			printk("code checkpoint 4\n");
    
    	UDP_Socket = nrf_socket(NRF_AF_LTE, NRF_SOCK_DGRAM, NRF_IPPROTO_UDP); //Opens UDP socket using UDP protocols
    	if (UDP_Socket == -1) {
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	printk("Socket created for init\n");
    
    	err = nrf_connect(UDP_Socket, UDP_sockaddr, UDP_sockaddr->sin_len); //Tests connection to UDP IPv4 address
    	if(err)
    	{
    		printk("Failure to establish connection via nrf_connect, closing socket\n");
    		close(UDP_Socket);
    	}
    	close(UDP_Socket);
    	printk("Config and Connect Successful\n");
    	return err;
    }
    
    /**@brief Sends a test UDP packet to AWS. Uses non-blocking mode. */
    
    static int UDP_Send(const char *UDP_buff)
    {
    	int err;
    	int UDP_Socket;
    
    	struct nrf_sockaddr_in *UDP_sockaddr;
    	UDP_sockaddr->sin_family =		NRF_AF_INET;            				/**< Socket family. */
    	UDP_sockaddr->sin_port		=		NRF_HTONS(xxxxx);              	/**< Port, in network byte order. */
    	UDP_sockaddr->sin_addr.s_addr		= 	0xXXXXXXXX;								/**< IPv4 address. */
    	UDP_sockaddr->sin_len 		=		sizeof(struct nrf_sockaddr_in);						/**< Length of this data structure. */
    
    	UDP_Socket = nrf_socket(NRF_AF_LTE, NRF_SOCK_DGRAM, NRF_IPPROTO_UDP);
    	if (UDP_Socket == -1) {
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	printk("Socket created for transfer\n");
    
    	err = nrf_connect(UDP_Socket, UDP_sockaddr, UDP_sockaddr->sin_len);
    	if(err)
    	{
    		printk("Failure to establish connection via nrf_connect, closing socket\n");
    		close(UDP_Socket);
    	}
    	printk("Send socket connected\n");
    	return err;
    
    	err = nrf_send(UDP_Socket, UDP_buff, strlen(UDP_buff), NRF_MSG_DONTWAIT); //Sends UDP Buffer to AWS EC2 with non-blocking flag
    	if (err)
    	{
    		printk("Error sending %s to IP %08x\n", UDP_buff, UDP_sockaddr->sin_addr.s_addr);
    		close(UDP_Socket);
    	}
    	printk("Sent %s to IP %08x\n", UDP_buff, UDP_sockaddr->sin_addr.s_addr);
    }
    
    void main(void)
    {
    	printk("UDP_Test Application started\n");
    
    	const char Buff[] = "Hello World\n";
    
    	//Set up Zephyr 10 second timer that will call UDP_Send
    	struct k_timer my_timer;
    	void my_work_handler(struct k_timer *timer_1)
    	{
    		UDP_Send(Buff);
    		printk("timer triggered!\n");
    	}
    
    	UDP_Socket_Config_and_Connect(); //Test UDP connection
    
    	//Initialize and activate timer
    	k_timer_init(&my_timer, my_work_handler, NULL);
    	k_timer_start(&my_timer, K_SECONDS(10), K_SECONDS(10));
    
    	while(true)
    	{
    		k_sleep(K_MSEC(10));
    		/* Put CPU to idle to save power */
    		k_cpu_idle();
    	}
    }
    

    EDIT: I should clarify, I have one DK deployed in an area of NB-IoT coverage that can be re-flashed remotely. I have a second DK beside me that I can use to test flashes operate as expected, but without any NB-IoT coverage in this area, before taking the merged zephyr hex file and remotely flashing using JLinkExe.

  • Hi,

     

    This code looks like trouble:

    struct nrf_sockaddr_in *UDP_sockaddr;
    printk("code checkpoint 0\n");
    UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    printk("code checkpoint 1\n");
    UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    printk("code checkpoint 2\n");
    UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    printk("code checkpoint 3\n");
    UDP_sockaddr->sin_addr.s_addr	    	= 	    0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    printk("code checkpoint 4\n");

     

    Could you try this instead?

    struct nrf_sockaddr_in my_sockaddr; // memset if not populating everything
    struct nrf_sockaddr_in *UDP_sockaddr = &my_sockaddr;
    printk("code checkpoint 0\n");
    UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    printk("code checkpoint 1\n");
    UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    printk("code checkpoint 2\n");
    UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    printk("code checkpoint 3\n");
    UDP_sockaddr->sin_addr.s_addr	    	= 	    0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    printk("code checkpoint 4\n");

     

    Kind regards,

    Håkon

  • Hi,

    Thank you, that allowed the code to pass through the struct without faulting.

    I have tweeked the code slightly as the timer was causing an instruction fault (first time I've seen that as I use that timer code in other Zephyr nrf91 samples, I have removed it for now as it wasn't really needed). I have edited nrf_connect as even though there was no network for it to connect to, it was still returning 0 on err.

    Is this the correct way for nrf_connect to be called? It now fails out as expected for no network connection.

    /**@brief Configures modem to test connection. Blocks until link is
     * successfully established. Creates a UDP socket and connects to
     * AWS EC2 instance
     */
    static int UDP_Socket_Config_and_Connect(void)
    {
    	int err;
    	int UDP_Socket;
    
    	// Sets up UDP Socket information
    	struct nrf_sockaddr_in my_sockaddr; // memset if not populating everything
    	struct nrf_sockaddr_in *UDP_sockaddr = &my_sockaddr;
    
    	UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    	UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    	UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    	UDP_sockaddr->sin_addr.s_addr	  = 	0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    
    	UDP_Socket = nrf_socket(NRF_AF_INET, NRF_SOCK_DGRAM, NRF_IPPROTO_UDP); //Opens UDP socket using UDP protocols
    	if (UDP_Socket == -1) {
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	printk("Socket created for init\n");
    
    	err = nrf_connect(UDP_Socket, (struct sockaddr *)&UDP_sockaddr, sizeof(UDP_sockaddr)); //Tests connection to UDP IPv4 address
    	if(err < 0)
    	{
    		printk("Failure to establish connection via nrf_connect, closing socket\n");
    		nrf_close(UDP_Socket);
    		return err;
    	}
    	nrf_close(UDP_Socket);
    	printk("Config and Connect Successful\n");
    	return err;
    }

  • I see. Thank you, I'll have a look through the udp-example. It will be a few days before the EC2 IP is whitelisted.

    I will update when the network is available to test the code.

    Many thanks

  • Hello again,

    I haven't been able to test the network as of yet but have been noting a few issues with the program. It has expanded over the past week to include some commented out section of the GPS sample code, (the goal is to design a device that will swap between LTE and GPS as needed) but this is all unneeded at the moment as the deployed DK is using old modem FW and can't use the AT command AT%XSYSTEMMODE. So all it really can do for now is turn itself off and on to test the AT command socket.

    I have noticed that the code just skips past the blocking do-while loop that I copied from the UDP example you linked to in your last reply. Rather than blocking while the errno is -EAGAIN, it auto fails after one attempt connection attempt. errno is coming back with error number 134.

    The code also for some reason stops being able to open sockets. I orginally thought this was due to me not closing sockets correctly when erroring out of the connect attempts thus running out of sockets but it actually doesn't appear to be the issue. Adding a print out for UDP_Socket shows that the Socket ID is always 2 (not sure why socket 1 isn't the first socket).

    /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <stdio.h>
    #include <uart.h>
    #include <string.h>
    #include <bsd.h>
    #include <net/socket.h>
    
    //#include "certificates.h"
    //#include "provisions/provision.h"
    #include "../../../../../nrfxlib/bsdlib/include/nrf_socket.h"
    
    #define AT_XSYSTEMMODE_GPS 			"AT\%XSYSTEMMODE=0,0,1,0"
    #define AT_MAGPIO_GPS      			"AT\%XMAGPIO=1,0,0,1,1,1574,1577"
    #define AT_CFUN_ON        			"AT+CFUN=1"
    #define AT_XSYSTEMMODE_LTE			"AT\%XSYSTEMMODE=0,1,0,0"
    #define AT_CFUN_OFF							"AT+CFUN=4"
    #define AT_MAGPIO_CLEAR					"AT\%XMAGPIO"
    
    //static const char     at_commands_GPS[][31]  = { AT_CFUN_OFF, AT_XSYSTEMMODE_GPS, AT_MAGPIO_GPS, AT_CFUN_ON };
    static const char			at_commands_Init[][31] = { AT_CFUN_OFF, AT_CFUN_ON };
    //static const char			at_commands_LTE[][31]  = { AT_CFUN_OFF, AT_XSYSTEMMODE_GPS, AT_MAGPIO_CLEAR, AT_CFUN_ON };
    //static int            fd;
    
    // static char           nmea_strings[10][NRF_GNSS_NMEA_MAX_LEN];
    // static u32_t          nmea_string_cnt;
    //
    // static bool           got_first_fix;
    // static bool           update_terminal;
    // static u64_t          fix_timestamp;
    // nrf_gnss_data_frame_t last_fix;
    static int 						modem_init_flag;
    static int 						data_to_send = 0;
    
    #if defined(CONFIG_BSD_LIBRARY)
    
    /**@brief Recoverable BSD library error. */
    void bsd_recoverable_error_handler(uint32_t err)
    {
    	printk("bsdlib recoverable error: %lu\n", err);
    }
    
    /**@brief Irrecoverable BSD library error. */
    void bsd_irrecoverable_error_handler(uint32_t err)
    {
    	printk("bsdlib irrecoverable error: %lu\n", err);
    
    	__ASSERT_NO_MSG(false);
    }
    
    #endif /* defined(CONFIG_BSD_LIBRARY) */
    
    // #if defined(CONFIG_BSD_LIBRARY) //Includes nrf secure key manager code, not impliment yet
    // #include "nrf_inbuilt_key.h"
    // #endif
    
    int blocking_connect(int fd, struct sockaddr *local_addr, socklen_t len)
    {
    	int err;
    
    	printk("Attempting to establish connection to EC2\n");
    	do {
    		err = connect(fd, local_addr, len);
    	} while (err < 0 && errno == EAGAIN);
    
    	return err;
    }
    
    static int enable_modem(void)
    {
    	int  at_sock;
    	int  bytes_sent;
    	int  bytes_received;
    	char buf[2];
    
    	at_sock = socket(AF_LTE, 0, NPROTO_AT);
    
    	if (at_sock < 0) {
    		return -1;
    	}
    
    	for (int i = 0; i < ARRAY_SIZE(at_commands_Init); i++) {
    		bytes_sent = send(at_sock, at_commands_Init[i],
    				  strlen(at_commands_Init[i]), 0);
    
    		if (bytes_sent < 0) {
    			close(at_sock);
    			return -1;
    		}
    
    		do {
    			bytes_received = recv(at_sock, buf, 2, 0);
    		} while (bytes_received == 0);
    
    		if (memcmp(buf, "OK", 2) != 0) {
    			close(at_sock);
    			return -1;
    		}
    	}
    	printk("Modem enabled");
    	close(at_sock);
    
    	return 0;
    }
    
    // static int swap_to_lte(void)
    // {
    // 	int  at_sock;
    // 	int  bytes_sent;
    // 	int  bytes_received;
    // 	char buf[2];
    //
    // 	at_sock = nrf_socket(AF_LTE, 0, NPROTO_AT);
    // 	if (at_sock < 0) {
    // 		return -1;
    // 	}
    //
    // 	for (int i = 0; i < ARRAY_SIZE(at_commands_LTE); i++) {
    // 		bytes_sent = nrf_send(at_sock, at_commands_LTE[i],
    // 				  strlen(at_commands_LTE[i]), 0);
    //
    // 		if (bytes_sent < 0) {
    // 			nrf_close(at_sock);
    // 			return -1;
    // 		}
    //
    // 		do {
    // 			bytes_received = nrf_recv(at_sock, buf, 2, 0);
    // 		} while (bytes_received == 0);
    //
    // 		if (memcmp(buf, "OK", 2) != 0) {
    // 			nrf_close(at_sock);
    // 			return -1;
    // 		}
    // 	}
    //
    // 	nrf_close(at_sock);
    //
    // 	return 0;
    // }
    //
    // static int swap_to_gps(void)
    // {
    // 	int  at_sock;
    // 	int  bytes_sent;
    // 	int  bytes_received;
    // 	char buf[2];
    //
    // 	at_sock = nrf_socket(AF_LTE, 0, NPROTO_AT);
    // 	if (at_sock < 0) {
    // 		return -1;
    // 	}
    //
    // 	for (int i = 0; i < ARRAY_SIZE(at_commands_GPS); i++) {
    // 		bytes_sent = nrf_send(at_sock, at_commands_GPS[i],
    // 				  strlen(at_commands_GPS[i]), 0);
    //
    // 		if (bytes_sent < 0) {
    // 			nrf_close(at_sock);
    // 			return -1;
    // 		}
    //
    // 		do {
    // 			bytes_received = nrf_recv(at_sock, buf, 2, 0);
    // 		} while (bytes_received == 0);
    //
    // 		if (memcmp(buf, "OK", 2) != 0) {
    // 			nrf_close(at_sock);
    // 			return -1;
    // 		}
    // 	}
    //
    // 	nrf_close(at_sock);
    //
    // 	return 0;
    // }
    
    // static int init_app(void)
    // {
    // 	u16_t fix_retry     = 0;
    // 	u16_t fix_interval  = 1;
    // 	u16_t nmea_mask     = NRF_CONFIG_NMEA_GSV_MASK |
    // 			      NRF_CONFIG_NMEA_GSA_MASK |
    // 			      NRF_CONFIG_NMEA_GLL_MASK |
    // 			      NRF_CONFIG_NMEA_GGA_MASK |
    // 			      NRF_CONFIG_NMEA_RMC_MASK;
    // 	int   retval;
    //
    // 	if (swap_to_gps() != 0) {
    // 		printk("Failed to enable GPS\n");
    // 		return -1;
    // 	}
    //
    // 	fd = nrf_socket(NRF_AF_LOCAL, NRF_SOCK_DGRAM, NRF_PROTO_GNSS);
    //
    // 	if (fd >= 0) {
    // 		printk("Socket created\n");
    // 	} else {
    // 		printk("Could not init socket (err: %d)\n", fd);
    // 		return -1;
    // 	}
    //
    // 	retval = nrf_setsockopt(fd,
    // 				NRF_SOL_GNSS,
    // 				NRF_SO_GNSS_FIX_RETRY,
    // 				&fix_retry,
    // 				sizeof(uint16_t));
    //
    // 	if (retval != 0) {
    // 		printk("Failed to set fix retry value\n");
    // 		return -1;
    // 	}
    //
    // 	retval = nrf_setsockopt(fd,
    // 				NRF_SOL_GNSS,
    // 				NRF_SO_GNSS_FIX_INTERVAL,
    // 				&fix_interval,
    // 				sizeof(uint16_t));
    //
    // 	if (retval != 0) {
    // 		printk("Failed to set fix interval value\n");
    // 		return -1;
    // 	}
    //
    // 	retval = nrf_setsockopt(fd,
    // 				NRF_SOL_GNSS,
    // 				NRF_SO_GNSS_NMEA_MASK,
    // 				&nmea_mask,
    // 				sizeof(uint16_t));
    //
    // 	if (retval != 0) {
    // 		printk("Failed to set nmea mask\n");
    // 		return -1;
    // 	}
    //
    // 	retval = nrf_setsockopt(fd,
    // 				NRF_SOL_GNSS,
    // 				NRF_SO_GNSS_START,
    // 				NULL,
    // 				0);
    //
    // 	if (retval != 0) {
    // 		printk("Failed to start GPS\n");
    // 		return -1;
    // 	}
    //
    // 	return 0;
    // }
    
    /**@brief Configures modem to test connection. Blocks until link is
     * successfully established. Creates a UDP socket and connects to
     * AWS EC2 instance
     */
    static int UDP_Socket_Config_and_Connect(void)
    {
    	int err;
    	int UDP_Socket;
    
    	// Sets up UDP Socket information
    	struct nrf_sockaddr_in my_sockaddr; // memset if not populating everything
    	struct nrf_sockaddr_in *UDP_sockaddr = &my_sockaddr;
    
    	UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    	UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    	UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    	UDP_sockaddr->sin_addr.s_addr	  = 	0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    
    	UDP_Socket = socket(NRF_AF_INET, NRF_SOCK_DGRAM, 0); //Opens UDP socket using UDP protocols
    	if (UDP_Socket == -1) {
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	printk("Socket created for init\n");
    
    	err = blocking_connect(UDP_Socket, (struct sockaddr *)&UDP_sockaddr, sizeof(UDP_sockaddr)); //Tests connection to UDP IPv4 address
    	if(err < 0)
    	{
    		printk("Failure to establish connection via nrf_connect, closing socket\n");
    		close(UDP_Socket);
    		return err;
    	}
    	close(UDP_Socket);
    	printk("Config and Connect Successful\n");
    	return err;
    }
    
    /**@brief Sends a test UDP packet to AWS. Uses non-blocking mode. */
    
    static int UDP_Send(char *UDP_buff)
    {
    	int err;
    	int UDP_Socket;
    	ssize_t bytes_sent;
    
    	struct nrf_sockaddr_in my_sockaddr; // memset if not populating everything
    	struct nrf_sockaddr_in *UDP_sockaddr = &my_sockaddr;
    	UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    	UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    	UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    	UDP_sockaddr->sin_addr.s_addr	  = 	0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    
    	UDP_Socket = socket(NRF_AF_INET, NRF_SOCK_DGRAM, 0);
    	printk("Socket ID: %d\n", UDP_Socket);
    	if (UDP_Socket == -1)
    	{
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	else
    	{
    		printk("Socket created for transfer\n");
    	}
    
    	err = blocking_connect(UDP_Socket, (struct sockaddr *)&UDP_sockaddr, sizeof(UDP_sockaddr));
    
    	if (err < 0)
    	{
    		printk("Failure to establish connection via nrf_connect, closing socket\n");
    		close(UDP_Socket);
    		return err;
    	}
    	else
    	{
    		printk("Send socket connected\n");
    	}
    
    	bytes_sent = send(UDP_Socket, UDP_buff, strlen(UDP_buff), NRF_MSG_WAITALL); //Sends UDP Buffer to AWS EC2 with blocking flag
    
    	if (bytes_sent < 0)
    	{
    		printk("Error sending %s to IP %lu\n", UDP_buff, UDP_sockaddr->sin_addr.s_addr);
    		close(UDP_Socket);
    		err = -1;
    		return err;
    	}
    	else
    	{
    		printk("Sent %08x bytes to IP %lu\n", bytes_sent, UDP_sockaddr->sin_addr.s_addr);
    	}
    	close(UDP_Socket);
    	return err;
    }
    
    // int process_gps_data(nrf_gnss_data_frame_t *gps_data)
    // {
    // 	int retval;
    //
    // 	retval = nrf_recv(fd, gps_data, sizeof(nrf_gnss_data_frame_t), NRF_MSG_DONTWAIT);
    //
    // 	if (retval > 0) {
    //
    // 		switch (gps_data->data_id) {
    // 		case NRF_GNSS_PVT_DATA_ID:
    //
    // 			if ((gps_data->pvt.flags &
    // 				NRF_GNSS_PVT_FLAG_FIX_VALID_BIT)
    // 				== NRF_GNSS_PVT_FLAG_FIX_VALID_BIT) {
    //
    // 				if (!got_first_fix) {
    // 					got_first_fix = true;
    // 				}
    //
    // 				fix_timestamp = k_uptime_get();
    // 				memcpy(&last_fix, gps_data, sizeof(nrf_gnss_data_frame_t));
    //
    // 				nmea_string_cnt = 0;
    // 				update_terminal = true;
    // 			}
    // 			break;
    //
    // 		case NRF_GNSS_NMEA_DATA_ID:
    // 			if (nmea_string_cnt < 10) {
    // 				memcpy(nmea_strings[nmea_string_cnt++],
    // 				       gps_data->nmea,
    // 				       retval);
    // 			}
    // 			break;
    //
    // 		default:
    // 			break;
    // 		}
    // 	}
    //
    // 	return retval;
    // }
    
    //Set up Zephyr 10 second timer that will call UDP_Send
    struct k_timer modemretry_timer;
    void modem_retry(struct k_timer *timer_1)
    {
    	int err;
    	printk("Retrying the modem!\n");
    	err = enable_modem();
    	if(err!=0)
    	{
    		modem_init_flag=0;
    	}
    	else
    	{
    		modem_init_flag=1;
    	}
    }
    
    //Set up Zephyr 10 second timer that will call UDP_Send
    struct k_timer UDPSend_timer;
    void UDP_Resend(struct k_timer *timer_2)
    {
    	data_to_send = 1;
    	printk("Timer trigger for UDPSend!\n");
    }
    
    void main(void)
    {
    	int error;
    
    	printk("UDP_Test Application started\n");
    
    	char *Buff = "Hello AWS\n";
    	char *Buff_Timer = "Timer Trigger\n";
    
    	error=enable_modem();
    	if(error != 0)
    	{
    		printk("Failed to initialise the modem\n");
    		modem_init_flag=0;
    		//Initialize and activate timer
    		while(modem_init_flag==0)
    		{
    			k_timer_init(&modemretry_timer, modem_retry, NULL);
    			k_timer_start(&modemretry_timer, K_SECONDS(10), K_SECONDS(10));
    		}
    		k_timer_stop(&modemretry_timer);
    	}
    	UDP_Socket_Config_and_Connect(); //Test UDP connection
    	UDP_Send(Buff);
    
    	//swap_to_gps();
    	//init_app();
    	//swap_to_lte();
    	printk("Dropping into main while loop\n");
    	k_timer_init(&UDPSend_timer, UDP_Resend, NULL);
    	k_timer_start(&UDPSend_timer, K_SECONDS(10), K_SECONDS(10));
    
    	while(true)
    	{
    		if (data_to_send == 1)
    		{
    			//UDP_Socket_Config_and_Connect(); //Test UDP connection
    			UDP_Send(Buff_Timer);
    			data_to_send = 0;
    		}
    		k_sleep(K_MSEC(10));
    		/* Put CPU to idle to save power */
    		k_cpu_idle();
    	}
    }
    

    ***** Booting Zephyr OS v1.14.99-ncs1-3-g3c4f27282002 *****
    Flash region		Domain		Permissions
    00 0x00000 0x08000 	Secure		rwxl
    01 0x08000 0x10000 	Non-Secure	rwxl
    02 0x10000 0x18000 	Non-Secure	rwxl
    03 0x18000 0x20000 	Non-Secure	rwxl
    04 0x20000 0x28000 	Non-Secure	rwxl
    05 0x28000 0x30000 	Non-Secure	rwxl
    06 0x30000 0x38000 	Non-Secure	rwxl
    07 0x38000 0x40000 	Non-Secure	rwxl
    08 0x40000 0x48000 	Non-Secure	rwxl
    09 0x48000 0x50000 	Non-Secure	rwxl
    10 0x50000 0x58000 	Non-Secure	rwxl
    11 0x58000 0x60000 	Non-Secure	rwxl
    12 0x60000 0x68000 	Non-Secure	rwxl
    13 0x68000 0x70000 	Non-Secure	rwxl
    14 0x70000 0x78000 	Non-Secure	rwxl
    15 0x78000 0x80000 	Non-Secure	rwxl
    16 0x80000 0x88000 	Non-Secure	rwxl
    17 0x88000 0x90000 	Non-Secure	rwxl
    18 0x90000 0x98000 	Non-Secure	rwxl
    19 0x98000 0xa0000 	Non-Secure	rwxl
    20 0xa0000 0xa8000 	Non-Secure	rwxl
    21 0xa8000 0xb0000 	Non-Secure	rwxl
    22 0xb0000 0xb8000 	Non-Secure	rwxl
    23 0xb8000 0xc0000 	Non-Secure	rwxl
    24 0xc0000 0xc8000 	Non-Secure	rwxl
    25 0xc8000 0xd0000 	Non-Secure	rwxl
    26 0xd0000 0xd8000 	Non-Secure	rwxl
    27 0xd8000 0xe0000 	Non-Secure	rwxl
    28 0xe0000 0xe8000 	Non-Secure	rwxl
    29 0xe8000 0xf0000 	Non-Secure	rwxl
    30 0xf0000 0xf8000 	Non-Secure	rwxl
    31 0xf8000 0x100000 	Non-Secure	rwxl
    
    SRAM region		Domain		Permissions
    00 0x00000 0x02000	Secure		rwxl
    01 0x02000 0x04000	Secure		rwxl
    02 0x04000 0x06000	Secure		rwxl
    03 0x06000 0x08000	Secure		rwxl
    04 0x08000 0x0a000	Secure		rwxl
    05 0x0a000 0x0c000	Secure		rwxl
    06 0x0c000 0x0e000	Secure		rwxl
    07 0x0e000 0x10000	Secure		rwxl
    08 0x10000 0x12000	Non-Secure	rwxl
    09 0x12000 0x14000	Non-Secure	rwxl
    10 0x14000 0x16000	Non-Secure	rwxl
    11 0x16000 0x18000	Non-Secure	rwxl
    12 0x18000 0x1a000	Non-Secure	rwxl
    13 0x1a000 0x1c000	Non-Secure	rwxl
    14 0x1c000 0x1e000	Non-Secure	rwxl
    15 0x1e000 0x20000	Non-Secure	rwxl
    16 0x20000 0x22000	Non-Secure	rwxl
    17 0x22000 0x24000	Non-Secure	rwxl
    18 0x24000 0x26000	Non-Secure	rwxl
    19 0x26000 0x28000	Non-Secure	rwxl
    20 0x28000 0x2a000	Non-Secure	rwxl
    21 0x2a000 0x2c000	Non-Secure	rwxl
    22 0x2c000 0x2e000	Non-Secure	rwxl
    23 0x2e000 0x30000	Non-Secure	rwxl
    24 0x30000 0x32000	Non-Secure	rwxl
    25 0x32000 0x34000	Non-Secure	rwxl
    26 0x34000 0x36000	Non-Secure	rwxl
    27 0x36000 0x38000	Non-Secure	rwxl
    28 0x38000 0x3a000	Non-Secure	rwxl
    29 0x3a000 0x3c000	Non-Secure	rwxl
    30 0x3c000 0x3e000	Non-Secure	rwxl
    31 0x3e000 0x40000	Non-Secure	rwxl
    
    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	OK
    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_SAADC		Non-Secure	OK
    17 NRF_GPIOTE1		Non-Secure	OK
    
    SPM: NS image at 0x8000
    SPM: NS MSP at 0x20023308
    SPM: NS reset vector at 0xd915
    SPM: prepare to jump to Non-Secure image.
    ***** Booting Zephyr OS v1.14.99-ncs1-3-g3c4f27282002 *****
    UDP_Test Application started
    Modem enabledSocket created for init
    Failure to establish connection via nrf_connect, closing socket
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Dropping into main while loop
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: -1
    Socket failed to open
    Timer trigger for UDPSend!
    Socket ID: -1
    Socket failed to open
    Timer trigger for UDPSend!
    Socket ID: -1
    Socket failed to open
    Timer trigger for UDPSend!
    

Reply
  • Hello again,

    I haven't been able to test the network as of yet but have been noting a few issues with the program. It has expanded over the past week to include some commented out section of the GPS sample code, (the goal is to design a device that will swap between LTE and GPS as needed) but this is all unneeded at the moment as the deployed DK is using old modem FW and can't use the AT command AT%XSYSTEMMODE. So all it really can do for now is turn itself off and on to test the AT command socket.

    I have noticed that the code just skips past the blocking do-while loop that I copied from the UDP example you linked to in your last reply. Rather than blocking while the errno is -EAGAIN, it auto fails after one attempt connection attempt. errno is coming back with error number 134.

    The code also for some reason stops being able to open sockets. I orginally thought this was due to me not closing sockets correctly when erroring out of the connect attempts thus running out of sockets but it actually doesn't appear to be the issue. Adding a print out for UDP_Socket shows that the Socket ID is always 2 (not sure why socket 1 isn't the first socket).

    /*
     * Copyright (c) 2018 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
     */
    
    #include <zephyr.h>
    #include <stdio.h>
    #include <uart.h>
    #include <string.h>
    #include <bsd.h>
    #include <net/socket.h>
    
    //#include "certificates.h"
    //#include "provisions/provision.h"
    #include "../../../../../nrfxlib/bsdlib/include/nrf_socket.h"
    
    #define AT_XSYSTEMMODE_GPS 			"AT\%XSYSTEMMODE=0,0,1,0"
    #define AT_MAGPIO_GPS      			"AT\%XMAGPIO=1,0,0,1,1,1574,1577"
    #define AT_CFUN_ON        			"AT+CFUN=1"
    #define AT_XSYSTEMMODE_LTE			"AT\%XSYSTEMMODE=0,1,0,0"
    #define AT_CFUN_OFF							"AT+CFUN=4"
    #define AT_MAGPIO_CLEAR					"AT\%XMAGPIO"
    
    //static const char     at_commands_GPS[][31]  = { AT_CFUN_OFF, AT_XSYSTEMMODE_GPS, AT_MAGPIO_GPS, AT_CFUN_ON };
    static const char			at_commands_Init[][31] = { AT_CFUN_OFF, AT_CFUN_ON };
    //static const char			at_commands_LTE[][31]  = { AT_CFUN_OFF, AT_XSYSTEMMODE_GPS, AT_MAGPIO_CLEAR, AT_CFUN_ON };
    //static int            fd;
    
    // static char           nmea_strings[10][NRF_GNSS_NMEA_MAX_LEN];
    // static u32_t          nmea_string_cnt;
    //
    // static bool           got_first_fix;
    // static bool           update_terminal;
    // static u64_t          fix_timestamp;
    // nrf_gnss_data_frame_t last_fix;
    static int 						modem_init_flag;
    static int 						data_to_send = 0;
    
    #if defined(CONFIG_BSD_LIBRARY)
    
    /**@brief Recoverable BSD library error. */
    void bsd_recoverable_error_handler(uint32_t err)
    {
    	printk("bsdlib recoverable error: %lu\n", err);
    }
    
    /**@brief Irrecoverable BSD library error. */
    void bsd_irrecoverable_error_handler(uint32_t err)
    {
    	printk("bsdlib irrecoverable error: %lu\n", err);
    
    	__ASSERT_NO_MSG(false);
    }
    
    #endif /* defined(CONFIG_BSD_LIBRARY) */
    
    // #if defined(CONFIG_BSD_LIBRARY) //Includes nrf secure key manager code, not impliment yet
    // #include "nrf_inbuilt_key.h"
    // #endif
    
    int blocking_connect(int fd, struct sockaddr *local_addr, socklen_t len)
    {
    	int err;
    
    	printk("Attempting to establish connection to EC2\n");
    	do {
    		err = connect(fd, local_addr, len);
    	} while (err < 0 && errno == EAGAIN);
    
    	return err;
    }
    
    static int enable_modem(void)
    {
    	int  at_sock;
    	int  bytes_sent;
    	int  bytes_received;
    	char buf[2];
    
    	at_sock = socket(AF_LTE, 0, NPROTO_AT);
    
    	if (at_sock < 0) {
    		return -1;
    	}
    
    	for (int i = 0; i < ARRAY_SIZE(at_commands_Init); i++) {
    		bytes_sent = send(at_sock, at_commands_Init[i],
    				  strlen(at_commands_Init[i]), 0);
    
    		if (bytes_sent < 0) {
    			close(at_sock);
    			return -1;
    		}
    
    		do {
    			bytes_received = recv(at_sock, buf, 2, 0);
    		} while (bytes_received == 0);
    
    		if (memcmp(buf, "OK", 2) != 0) {
    			close(at_sock);
    			return -1;
    		}
    	}
    	printk("Modem enabled");
    	close(at_sock);
    
    	return 0;
    }
    
    // static int swap_to_lte(void)
    // {
    // 	int  at_sock;
    // 	int  bytes_sent;
    // 	int  bytes_received;
    // 	char buf[2];
    //
    // 	at_sock = nrf_socket(AF_LTE, 0, NPROTO_AT);
    // 	if (at_sock < 0) {
    // 		return -1;
    // 	}
    //
    // 	for (int i = 0; i < ARRAY_SIZE(at_commands_LTE); i++) {
    // 		bytes_sent = nrf_send(at_sock, at_commands_LTE[i],
    // 				  strlen(at_commands_LTE[i]), 0);
    //
    // 		if (bytes_sent < 0) {
    // 			nrf_close(at_sock);
    // 			return -1;
    // 		}
    //
    // 		do {
    // 			bytes_received = nrf_recv(at_sock, buf, 2, 0);
    // 		} while (bytes_received == 0);
    //
    // 		if (memcmp(buf, "OK", 2) != 0) {
    // 			nrf_close(at_sock);
    // 			return -1;
    // 		}
    // 	}
    //
    // 	nrf_close(at_sock);
    //
    // 	return 0;
    // }
    //
    // static int swap_to_gps(void)
    // {
    // 	int  at_sock;
    // 	int  bytes_sent;
    // 	int  bytes_received;
    // 	char buf[2];
    //
    // 	at_sock = nrf_socket(AF_LTE, 0, NPROTO_AT);
    // 	if (at_sock < 0) {
    // 		return -1;
    // 	}
    //
    // 	for (int i = 0; i < ARRAY_SIZE(at_commands_GPS); i++) {
    // 		bytes_sent = nrf_send(at_sock, at_commands_GPS[i],
    // 				  strlen(at_commands_GPS[i]), 0);
    //
    // 		if (bytes_sent < 0) {
    // 			nrf_close(at_sock);
    // 			return -1;
    // 		}
    //
    // 		do {
    // 			bytes_received = nrf_recv(at_sock, buf, 2, 0);
    // 		} while (bytes_received == 0);
    //
    // 		if (memcmp(buf, "OK", 2) != 0) {
    // 			nrf_close(at_sock);
    // 			return -1;
    // 		}
    // 	}
    //
    // 	nrf_close(at_sock);
    //
    // 	return 0;
    // }
    
    // static int init_app(void)
    // {
    // 	u16_t fix_retry     = 0;
    // 	u16_t fix_interval  = 1;
    // 	u16_t nmea_mask     = NRF_CONFIG_NMEA_GSV_MASK |
    // 			      NRF_CONFIG_NMEA_GSA_MASK |
    // 			      NRF_CONFIG_NMEA_GLL_MASK |
    // 			      NRF_CONFIG_NMEA_GGA_MASK |
    // 			      NRF_CONFIG_NMEA_RMC_MASK;
    // 	int   retval;
    //
    // 	if (swap_to_gps() != 0) {
    // 		printk("Failed to enable GPS\n");
    // 		return -1;
    // 	}
    //
    // 	fd = nrf_socket(NRF_AF_LOCAL, NRF_SOCK_DGRAM, NRF_PROTO_GNSS);
    //
    // 	if (fd >= 0) {
    // 		printk("Socket created\n");
    // 	} else {
    // 		printk("Could not init socket (err: %d)\n", fd);
    // 		return -1;
    // 	}
    //
    // 	retval = nrf_setsockopt(fd,
    // 				NRF_SOL_GNSS,
    // 				NRF_SO_GNSS_FIX_RETRY,
    // 				&fix_retry,
    // 				sizeof(uint16_t));
    //
    // 	if (retval != 0) {
    // 		printk("Failed to set fix retry value\n");
    // 		return -1;
    // 	}
    //
    // 	retval = nrf_setsockopt(fd,
    // 				NRF_SOL_GNSS,
    // 				NRF_SO_GNSS_FIX_INTERVAL,
    // 				&fix_interval,
    // 				sizeof(uint16_t));
    //
    // 	if (retval != 0) {
    // 		printk("Failed to set fix interval value\n");
    // 		return -1;
    // 	}
    //
    // 	retval = nrf_setsockopt(fd,
    // 				NRF_SOL_GNSS,
    // 				NRF_SO_GNSS_NMEA_MASK,
    // 				&nmea_mask,
    // 				sizeof(uint16_t));
    //
    // 	if (retval != 0) {
    // 		printk("Failed to set nmea mask\n");
    // 		return -1;
    // 	}
    //
    // 	retval = nrf_setsockopt(fd,
    // 				NRF_SOL_GNSS,
    // 				NRF_SO_GNSS_START,
    // 				NULL,
    // 				0);
    //
    // 	if (retval != 0) {
    // 		printk("Failed to start GPS\n");
    // 		return -1;
    // 	}
    //
    // 	return 0;
    // }
    
    /**@brief Configures modem to test connection. Blocks until link is
     * successfully established. Creates a UDP socket and connects to
     * AWS EC2 instance
     */
    static int UDP_Socket_Config_and_Connect(void)
    {
    	int err;
    	int UDP_Socket;
    
    	// Sets up UDP Socket information
    	struct nrf_sockaddr_in my_sockaddr; // memset if not populating everything
    	struct nrf_sockaddr_in *UDP_sockaddr = &my_sockaddr;
    
    	UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    	UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    	UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    	UDP_sockaddr->sin_addr.s_addr	  = 	0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    
    	UDP_Socket = socket(NRF_AF_INET, NRF_SOCK_DGRAM, 0); //Opens UDP socket using UDP protocols
    	if (UDP_Socket == -1) {
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	printk("Socket created for init\n");
    
    	err = blocking_connect(UDP_Socket, (struct sockaddr *)&UDP_sockaddr, sizeof(UDP_sockaddr)); //Tests connection to UDP IPv4 address
    	if(err < 0)
    	{
    		printk("Failure to establish connection via nrf_connect, closing socket\n");
    		close(UDP_Socket);
    		return err;
    	}
    	close(UDP_Socket);
    	printk("Config and Connect Successful\n");
    	return err;
    }
    
    /**@brief Sends a test UDP packet to AWS. Uses non-blocking mode. */
    
    static int UDP_Send(char *UDP_buff)
    {
    	int err;
    	int UDP_Socket;
    	ssize_t bytes_sent;
    
    	struct nrf_sockaddr_in my_sockaddr; // memset if not populating everything
    	struct nrf_sockaddr_in *UDP_sockaddr = &my_sockaddr;
    	UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    	UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    	UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    	UDP_sockaddr->sin_addr.s_addr	  = 	0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    
    	UDP_Socket = socket(NRF_AF_INET, NRF_SOCK_DGRAM, 0);
    	printk("Socket ID: %d\n", UDP_Socket);
    	if (UDP_Socket == -1)
    	{
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	else
    	{
    		printk("Socket created for transfer\n");
    	}
    
    	err = blocking_connect(UDP_Socket, (struct sockaddr *)&UDP_sockaddr, sizeof(UDP_sockaddr));
    
    	if (err < 0)
    	{
    		printk("Failure to establish connection via nrf_connect, closing socket\n");
    		close(UDP_Socket);
    		return err;
    	}
    	else
    	{
    		printk("Send socket connected\n");
    	}
    
    	bytes_sent = send(UDP_Socket, UDP_buff, strlen(UDP_buff), NRF_MSG_WAITALL); //Sends UDP Buffer to AWS EC2 with blocking flag
    
    	if (bytes_sent < 0)
    	{
    		printk("Error sending %s to IP %lu\n", UDP_buff, UDP_sockaddr->sin_addr.s_addr);
    		close(UDP_Socket);
    		err = -1;
    		return err;
    	}
    	else
    	{
    		printk("Sent %08x bytes to IP %lu\n", bytes_sent, UDP_sockaddr->sin_addr.s_addr);
    	}
    	close(UDP_Socket);
    	return err;
    }
    
    // int process_gps_data(nrf_gnss_data_frame_t *gps_data)
    // {
    // 	int retval;
    //
    // 	retval = nrf_recv(fd, gps_data, sizeof(nrf_gnss_data_frame_t), NRF_MSG_DONTWAIT);
    //
    // 	if (retval > 0) {
    //
    // 		switch (gps_data->data_id) {
    // 		case NRF_GNSS_PVT_DATA_ID:
    //
    // 			if ((gps_data->pvt.flags &
    // 				NRF_GNSS_PVT_FLAG_FIX_VALID_BIT)
    // 				== NRF_GNSS_PVT_FLAG_FIX_VALID_BIT) {
    //
    // 				if (!got_first_fix) {
    // 					got_first_fix = true;
    // 				}
    //
    // 				fix_timestamp = k_uptime_get();
    // 				memcpy(&last_fix, gps_data, sizeof(nrf_gnss_data_frame_t));
    //
    // 				nmea_string_cnt = 0;
    // 				update_terminal = true;
    // 			}
    // 			break;
    //
    // 		case NRF_GNSS_NMEA_DATA_ID:
    // 			if (nmea_string_cnt < 10) {
    // 				memcpy(nmea_strings[nmea_string_cnt++],
    // 				       gps_data->nmea,
    // 				       retval);
    // 			}
    // 			break;
    //
    // 		default:
    // 			break;
    // 		}
    // 	}
    //
    // 	return retval;
    // }
    
    //Set up Zephyr 10 second timer that will call UDP_Send
    struct k_timer modemretry_timer;
    void modem_retry(struct k_timer *timer_1)
    {
    	int err;
    	printk("Retrying the modem!\n");
    	err = enable_modem();
    	if(err!=0)
    	{
    		modem_init_flag=0;
    	}
    	else
    	{
    		modem_init_flag=1;
    	}
    }
    
    //Set up Zephyr 10 second timer that will call UDP_Send
    struct k_timer UDPSend_timer;
    void UDP_Resend(struct k_timer *timer_2)
    {
    	data_to_send = 1;
    	printk("Timer trigger for UDPSend!\n");
    }
    
    void main(void)
    {
    	int error;
    
    	printk("UDP_Test Application started\n");
    
    	char *Buff = "Hello AWS\n";
    	char *Buff_Timer = "Timer Trigger\n";
    
    	error=enable_modem();
    	if(error != 0)
    	{
    		printk("Failed to initialise the modem\n");
    		modem_init_flag=0;
    		//Initialize and activate timer
    		while(modem_init_flag==0)
    		{
    			k_timer_init(&modemretry_timer, modem_retry, NULL);
    			k_timer_start(&modemretry_timer, K_SECONDS(10), K_SECONDS(10));
    		}
    		k_timer_stop(&modemretry_timer);
    	}
    	UDP_Socket_Config_and_Connect(); //Test UDP connection
    	UDP_Send(Buff);
    
    	//swap_to_gps();
    	//init_app();
    	//swap_to_lte();
    	printk("Dropping into main while loop\n");
    	k_timer_init(&UDPSend_timer, UDP_Resend, NULL);
    	k_timer_start(&UDPSend_timer, K_SECONDS(10), K_SECONDS(10));
    
    	while(true)
    	{
    		if (data_to_send == 1)
    		{
    			//UDP_Socket_Config_and_Connect(); //Test UDP connection
    			UDP_Send(Buff_Timer);
    			data_to_send = 0;
    		}
    		k_sleep(K_MSEC(10));
    		/* Put CPU to idle to save power */
    		k_cpu_idle();
    	}
    }
    

    ***** Booting Zephyr OS v1.14.99-ncs1-3-g3c4f27282002 *****
    Flash region		Domain		Permissions
    00 0x00000 0x08000 	Secure		rwxl
    01 0x08000 0x10000 	Non-Secure	rwxl
    02 0x10000 0x18000 	Non-Secure	rwxl
    03 0x18000 0x20000 	Non-Secure	rwxl
    04 0x20000 0x28000 	Non-Secure	rwxl
    05 0x28000 0x30000 	Non-Secure	rwxl
    06 0x30000 0x38000 	Non-Secure	rwxl
    07 0x38000 0x40000 	Non-Secure	rwxl
    08 0x40000 0x48000 	Non-Secure	rwxl
    09 0x48000 0x50000 	Non-Secure	rwxl
    10 0x50000 0x58000 	Non-Secure	rwxl
    11 0x58000 0x60000 	Non-Secure	rwxl
    12 0x60000 0x68000 	Non-Secure	rwxl
    13 0x68000 0x70000 	Non-Secure	rwxl
    14 0x70000 0x78000 	Non-Secure	rwxl
    15 0x78000 0x80000 	Non-Secure	rwxl
    16 0x80000 0x88000 	Non-Secure	rwxl
    17 0x88000 0x90000 	Non-Secure	rwxl
    18 0x90000 0x98000 	Non-Secure	rwxl
    19 0x98000 0xa0000 	Non-Secure	rwxl
    20 0xa0000 0xa8000 	Non-Secure	rwxl
    21 0xa8000 0xb0000 	Non-Secure	rwxl
    22 0xb0000 0xb8000 	Non-Secure	rwxl
    23 0xb8000 0xc0000 	Non-Secure	rwxl
    24 0xc0000 0xc8000 	Non-Secure	rwxl
    25 0xc8000 0xd0000 	Non-Secure	rwxl
    26 0xd0000 0xd8000 	Non-Secure	rwxl
    27 0xd8000 0xe0000 	Non-Secure	rwxl
    28 0xe0000 0xe8000 	Non-Secure	rwxl
    29 0xe8000 0xf0000 	Non-Secure	rwxl
    30 0xf0000 0xf8000 	Non-Secure	rwxl
    31 0xf8000 0x100000 	Non-Secure	rwxl
    
    SRAM region		Domain		Permissions
    00 0x00000 0x02000	Secure		rwxl
    01 0x02000 0x04000	Secure		rwxl
    02 0x04000 0x06000	Secure		rwxl
    03 0x06000 0x08000	Secure		rwxl
    04 0x08000 0x0a000	Secure		rwxl
    05 0x0a000 0x0c000	Secure		rwxl
    06 0x0c000 0x0e000	Secure		rwxl
    07 0x0e000 0x10000	Secure		rwxl
    08 0x10000 0x12000	Non-Secure	rwxl
    09 0x12000 0x14000	Non-Secure	rwxl
    10 0x14000 0x16000	Non-Secure	rwxl
    11 0x16000 0x18000	Non-Secure	rwxl
    12 0x18000 0x1a000	Non-Secure	rwxl
    13 0x1a000 0x1c000	Non-Secure	rwxl
    14 0x1c000 0x1e000	Non-Secure	rwxl
    15 0x1e000 0x20000	Non-Secure	rwxl
    16 0x20000 0x22000	Non-Secure	rwxl
    17 0x22000 0x24000	Non-Secure	rwxl
    18 0x24000 0x26000	Non-Secure	rwxl
    19 0x26000 0x28000	Non-Secure	rwxl
    20 0x28000 0x2a000	Non-Secure	rwxl
    21 0x2a000 0x2c000	Non-Secure	rwxl
    22 0x2c000 0x2e000	Non-Secure	rwxl
    23 0x2e000 0x30000	Non-Secure	rwxl
    24 0x30000 0x32000	Non-Secure	rwxl
    25 0x32000 0x34000	Non-Secure	rwxl
    26 0x34000 0x36000	Non-Secure	rwxl
    27 0x36000 0x38000	Non-Secure	rwxl
    28 0x38000 0x3a000	Non-Secure	rwxl
    29 0x3a000 0x3c000	Non-Secure	rwxl
    30 0x3c000 0x3e000	Non-Secure	rwxl
    31 0x3e000 0x40000	Non-Secure	rwxl
    
    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	OK
    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_SAADC		Non-Secure	OK
    17 NRF_GPIOTE1		Non-Secure	OK
    
    SPM: NS image at 0x8000
    SPM: NS MSP at 0x20023308
    SPM: NS reset vector at 0xd915
    SPM: prepare to jump to Non-Secure image.
    ***** Booting Zephyr OS v1.14.99-ncs1-3-g3c4f27282002 *****
    UDP_Test Application started
    Modem enabledSocket created for init
    Failure to establish connection via nrf_connect, closing socket
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Dropping into main while loop
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: 2
    Socket created for transfer
    Attempting to establish connection to EC2
    Failure to establish connection via nrf_connect, closing socket
    Timer trigger for UDPSend!
    Socket ID: -1
    Socket failed to open
    Timer trigger for UDPSend!
    Socket ID: -1
    Socket failed to open
    Timer trigger for UDPSend!
    Socket ID: -1
    Socket failed to open
    Timer trigger for UDPSend!
    

Children
  • Hi,

     

    Sorry for not catching this before, but you are mixing the nrf_socket.h and the zephyr socket API. They do not play well together and needs translation (see nrf91_sockets.c for more detailed info).

    The include "net/socket.h" is also a bit suspicious, as you want to use the socket offloading, thus the translation between nrf and zephyr API. When you are calling socket() (and other calls), I am unsure which function you are calling in the background. This could be the network stacks socket implementation, which will not work with the nRF9160, or it could be the socket offloading (thus going into nrf91_sockets.c) which would have a mismatch of the structs.

    Could you instead try the ntp example and see if that works?

     

    Kind regards,

    Håkon

  • Hi,

    I copied across the files for the ntp example and ran it as it is in the repo.

    Did it require a specific checkout of the supporting repos (Zephyr, MCUBoot, nrfxlib)?

    Running it as is ended with a bus fault after getaddrinfo. Never made it to socket creation.

    ***** Booting Zephyr OS v1.14.99-ncs1-3-g3c4f27282002 *****
    Flash region		Domain		Permissions
    00 0x00000 0x08000 	Secure		rwxl
    01 0x08000 0x10000 	Non-Secure	rwxl
    02 0x10000 0x18000 	Non-Secure	rwxl
    03 0x18000 0x20000 	Non-Secure	rwxl
    04 0x20000 0x28000 	Non-Secure	rwxl
    05 0x28000 0x30000 	Non-Secure	rwxl
    06 0x30000 0x38000 	Non-Secure	rwxl
    07 0x38000 0x40000 	Non-Secure	rwxl
    08 0x40000 0x48000 	Non-Secure	rwxl
    09 0x48000 0x50000 	Non-Secure	rwxl
    10 0x50000 0x58000 	Non-Secure	rwxl
    11 0x58000 0x60000 	Non-Secure00 	Non-Secure	rwxl
    13 0x68000 0x70000 	Non-Secure	rwxl
    14 0x70000 0x78000 	Non-Secure	rwxl
    15 0x78000 0x80000 	Non-Secure	rwxl
    16 0x80000 0x88000 	Non-Secure	rwxl
    17 0x88000 0x90000 	Non-Secure	rwxl
    18 0x90000 0x98000 	Non-Secure	rwxl
    19 0x98000 0xa0000 	Non-Secure	rwxl
    20 0xa0000 0xa8000 	Non-Secure	rwxl
    21 0xa8000 0xb0000 	Non-Secure	rwxl
    22 0xb0000 0xb8000 	Non-Secure	rwxl
    23 0xb8000 0xc0000 	Non-Secure	rwxl
    24 0xc0000 0xc8000 	Non-Secure	rwxl
    25 0xc8000 0xd0000 	Non-Secure	rwxl
    26 0xd0000 0xd8000 	Non-Secure	rwxl
    27 0xd8000 0xe0000 	Non-Secure	rwxl
    28 0xe0000 0xe8000 	Non-Secure	rwxl
    29 0xe8000 0xf0000 	Non-Secure	rwxl
    30 0xf0000 0xf8000 	Non-Secure	rwxl
    31 0xf8000 0x100000 	Non-Secure	rwxl
    
    SRAM region		Domain		Permissions
    00 0x00000 0x02000	Secure		rwxl
    01 0x02000 0x04000	Secure		rwxl
    02 0x04000 0x06000	Secure		rwxl
    03 0x06000 0x08000	Secure		rwxl
    04 0x08000 0x0a000	Secure		rwxl
    05 0x0a000 0x0c000	Secure		rwxl
    06 0x0c000 0x0e000	Secure		rwxl
    07 0x0e000 0x10000	Secure		rwxl
    08 0x10000 0x12000	Non-Secure	rwxl
    09 0x12000 0x14000	Non-Secure	rwxl
    10 0x14000 0x16000	Non-Secure	rwxl
    11 0x16000 0x18000	Non-Secure	rwxl
    12 0x18000 0x1a000	Non-Secure	rwxl
    13 0x1a000 0x1c000	Non-Secure	rwxl
    14 0x1c000 0x1e000	Non-Secure	rwxl
    15 0x1e000 0x20000	Non-Secure	rwxl
    16 0x20000 0x22000	Non-Secure	rwxl
    17 0x22000 0x24000	Non-Secure	rwxl
    18 0x24000 0x26000	Non-Secure	rwxl
    19 0x26000 0x28000	Non-Secure	rwxl
    20 0x28000 0x2a000	Non-Secure	rwxl
    21 0x2a000 0x2c000	Non-Secure	rwxl
    22 0x2c000 0x2e000	Non-Secure	rwxl
    23 0x2e000 0x30000	Non-Secure	rwxl
    24 0x30000 0x32000	Non-Secure	rwxl
    25 0x32000 0x34000	Non-Secure	rwxl
    26 0x34000 0x36000	Non-Secure	rwxl
    27 0x36000 0x38000	Non-Secure	rwxl
    28 0x38000 0x3a000	Non-Secure	rwxl
    29 0x3a000 0x3c000	Non-Secure	rwxl
    30 0x3c000 0x3e000	Non-Secure	rwxl
    31 0x3e000 0x40000	Non-Secure	rwxl
    
    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	OK
    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_SAADC		Non-Secure	OK
    17 NRF_GPIOTE1		Non-Secure	OK
    
    SPM: NS image at 0x8000
    SPM: NS MSP at 0x200232d0
    SPM: NS reset vector at 0xedd9
    SPM: prepare to jump to Non-Secure image.
    ***** Booting Zephyr OS v1.14.99-ncs1-3-g3c4f27282002 *****
    getaddrinfo err: 22
    
    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 = 0x2002026c
    Faulting instruction address = 0xd904
    Fatal fault in ISR! Spinning...

  • the getaddrinfo fails, indicating that your telecom provider might not provide DNS.

    You could try the ftp example in the same repository, as it hardcodes the IP to ftp.uninett.no.

     

    Kind regards,

    Håkon

  • Hi,

    I tried the ftp example, both as it is in the repo and edited. The code doesn't make it past boot up. The printk() line for the application start does not get sent to the link monitor, program just hard faults again. Error fault address at 0x0

    You are correct, my provider does not support anything other than a whitelisted IP address given to them in advance.

    I have gone back to my original program that was giving back the errno 134. Taking what you said, I have removed all <net/socket> commands and used only the nrf_socket library. The code now reports an errno of 22 which I believe is EINVAL (invalid argument) so at least it's not erroring an unknown value for errno.

    ***** Booting Zephyr OS v1.14.99-ncs1-3-g3c4f27282002 *****
    UDP_Test Application started
    Socket ID: 2
    Modem enabled
    Socket created for init
    Socket ID: 2
    Attempting to establish connection to EC2
    Connect Error: -1:22
    

    Arguments passed to blocking_connect():

    	int err;
    	int UDP_Socket;
    	ssize_t bytes_sent;
    
    	struct nrf_sockaddr_in my_sockaddr; // memset if not populating everything
    	struct nrf_sockaddr_in *UDP_sockaddr = &my_sockaddr;
    	UDP_sockaddr->sin_len 					=		sizeof(struct nrf_sockaddr_in);			/**< Length of this data structure */
    	UDP_sockaddr->sin_port					=		NRF_HTONS(xxxxx);              	        /**< Port, in network byte order */
    	UDP_sockaddr->sin_family 				=		NRF_AF_INET;            				/**< Socket family, IPv4 socket */
    	UDP_sockaddr->sin_addr.s_addr	  = 	0xXXXXXXXX;							    /**< IPv4 address, in hex format */
    
    	UDP_Socket = nrf_socket(NRF_AF_INET, NRF_SOCK_DGRAM, 0);
    	printk("Socket ID: %d\n", UDP_Socket);
    	if (UDP_Socket == -1)
    	{
    		printk("Socket failed to open\n");
    		return -EFAULT;
    	}
    	else
    	{
    		printk("Socket created for transfer\n");
    	}
    
    	err = blocking_connect(UDP_Socket, (struct nrf_sockaddr *)&UDP_sockaddr, sizeof(struct nrf_sockaddr_in));

    I also want to check the required prj.conf in case this is also causing issues; apologises if this is all wrong, never used this kind of file structure before and I'm piecing it together with what I learn from the other examples. Unlike the other examples, I'm not calling Link Control as the code should manually init the modem (it needs to be able to do this to change between NB-IoT and GPS later). The one I'm currently using is:

    # Use the development test RNG (not for production)
    CONFIG_TEST_RANDOM_GENERATOR=y
    
    # Networking, enable networking APIs
    CONFIG_NETWORKING=y
    CONFIG_NET_SOCKETS_OFFLOAD=y
    CONFIG_NET_SOCKETS=y
    
    # Allow Socket APIs to use POSIX names like socket(), send(), etc
    # Careful with close(), close() only works on sockets when enabled
    CONFIG_NET_SOCKETS_POSIX_NAMES=y
    
    # BSD library
    CONFIG_BSD_LIBRARY=y
    
    # Main thread, reuse init stack
    CONFIG_MAIN_THREAD_PRIORITY=7
    CONFIG_MAIN_STACK_SIZE=4096
    
    # Available memory for k_malloc
    CONFIG_HEAP_MEM_POOL_SIZE=1024
    
    # Enable stdout for printfs
    CONFIG_STDOUT_CONSOLE=y
    
    # Logging
    CONFIG_LOG=y
    CONFIG_LOG_DEFAULT_LEVEL=4
    
    # Build with newlib c library
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    
    # Enable non-secure firmware as Normal mode
    CONFIG_TRUSTED_EXECUTION_NONSECURE=y
    

    Also, a question about the ftp code. In that code there is a converter for IP to Hex, inet_addr(). I implimented this into my code and printed the result to find that the IP address was backwards from what I expected it to be using manual conversion or something like https://www.browserling.com/tools/ip-to-hex . I have amended the function to put the bytes in the order I would expect but just want to check, is it supposed to be that way or is that an oversight in this checkout?

    Michael

    EDIT:

    I made a seperate version of the code that uses only the net/sockets.h library (like all examples of the nRF9160's data transfer capablities), it is doing the same as before, errno is 134 again. I'm not sure what else needs to be done to make this code block until connection.

  • Hi,

     

     

    MJD093 said:
    I tried the ftp example, both as it is in the repo and edited. The code doesn't make it past boot up. The printk() line for the application start does not get sent to the link monitor, program just hard faults again. Error fault address at 0x0

     Sorry, this is an issue with the newer code base, where it requires a higher stack size for the main thread. Could you try to add this line to the prj.conf and see if it helps?

    CONFIG_MAIN_STACK_SIZE=4096

     

    MJD093 said:
    I have gone back to my original program that was giving back the errno 134. Taking what you said, I have removed all <net/socket> commands and used only the nrf_socket library. The code now reports an errno of 22 which I believe is EINVAL (invalid argument) so at least it's not erroring an unknown value for errno.

    Have you removed all the socket APIs, and used the nrf_* prefixed function calls?

     

    MJD093 said:
    Also, a question about the ftp code. In that code there is a converter for IP to Hex, inet_addr(). I implimented this into my code and printed the result to find that the IP address was backwards from what I expected it to be using manual conversion or something like https://www.browserling.com/tools/ip-to-hex . I have amended the function to put the bytes in the order I would expect but just want to check, is it supposed to be that way or is that an oversight in this checkout?

     This is due to byte ordering on ARM vs. x86. The function in firmware is correct for ARM LE arch.

     

    MJD093 said:
    I made a seperate version of the code that uses only the net/sockets.h library (like all examples of the nRF9160's data transfer capablities), it is doing the same as before, errno is 134 again. I'm not sure what else needs to be done to make this code block until connection.

     No nrf_* prefixed structs or function calls? I am very unsure what this errno is, as there's nothing that should set it to this value. How are you printing the errno?

     

    Kind regards,

    Håkon

Related