SNTP query on nRF7002-DK failed to send over UDP socket

Hello everyone,

I received my nRF7002-DK board and I wanted to get the time using sntp client.
I wrote a small code (which is mostly a fusion between this code and the sntp_client sample) that connects to my network, setup a sntp context and sends a query to get the time but I keep getting this error : <err> net_sntp: Failed to send over UDP socket -1.

I suspect that something is wrong in my prj.conf but as I am just discovering Zephyr OS, I can't figure out what I am missing...
Any piece of help or advice would be greatly appreciated.

The SNTP part of my code :

	//SNTP Test
	memset(&addr,  0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(SNTP_PORT);
	zsock_inet_pton(AF_INET, SNTP_SERVER, &addr.sin_addr);

	sntp_status = sntp_init(&ctx, (struct sockaddr *) &addr, sizeof(addr));
	if(sntp_status < 0){
		printk("Init SNTP socket : Failure - Error %d\n", sntp_status);
	}
	else{
		printk("Init SNTP socket : Success\n");
		sntp_status = sntp_query(&ctx, 4*MSEC_PER_SEC, &s_time);
		if(sntp_status < 0){
			printk("Send SNTP query : Failure - Error %d\n", sntp_status);
		}
		else{
			printk("Send SNTP query : Success %d\n");
		}
	}
	sntp_close(&ctx);

My complete code :

#include <zephyr/kernel.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/wifi_mgmt.h>
#include <zephyr/net/net_event.h>
#include <errno.h>

#include <zephyr/net/sntp.h>
#include <arpa/inet.h>

#define SSID MYSSID
#define PSK MYPSK

#define SNTP_SERVER "ntp.midway.ovh"
#define SNTP_PORT 123

static K_SEM_DEFINE(wifi_connected, 0, 1);
static K_SEM_DEFINE(ipv4_address_obtained, 0, 1);

static struct net_mgmt_event_callback wifi_cb;
static struct net_mgmt_event_callback ipv4_cb;

//SNTP Test
static struct sntp_ctx ctx;
static struct sockaddr_in addr;
static struct sntp_time s_time;

static void handle_wifi_connect_result(struct net_mgmt_event_callback *cb)
{
    const struct wifi_status *status = (const struct wifi_status *)cb->info;

    if (status->status)
    {
        printk("Connection request failed (%d)\n", status->status);
    }
    else
    {
        printk("Connected\n");
        k_sem_give(&wifi_connected);
    }
}

static void handle_wifi_disconnect_result(struct net_mgmt_event_callback *cb)
{
    const struct wifi_status *status = (const struct wifi_status *)cb->info;

    if (status->status)
    {
        printk("Disconnection request (%d)\n", status->status);
    }
    else
    {
        printk("Disconnected\n");
        k_sem_take(&wifi_connected, K_NO_WAIT);
    }
}

static void handle_ipv4_result(struct net_if *iface)
{
    int i = 0;

    for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {

        char buf[NET_IPV4_ADDR_LEN];

        if (iface->config.ip.ipv4->unicast[i].addr_type != NET_ADDR_DHCP) {
            continue;
        }

        printk("IPv4 address: %s\n",
                net_addr_ntop(AF_INET,
                                &iface->config.ip.ipv4->unicast[i].address.in_addr,
                                buf, sizeof(buf)));
        printk("Subnet: %s\n",
                net_addr_ntop(AF_INET,
                                &iface->config.ip.ipv4->netmask,
                                buf, sizeof(buf)));
        printk("Router: %s\n",
                net_addr_ntop(AF_INET,
                                &iface->config.ip.ipv4->gw,
                                buf, sizeof(buf)));
        }

        k_sem_give(&ipv4_address_obtained);
}

static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event, struct net_if *iface)
{
    switch (mgmt_event)
    {

        case NET_EVENT_WIFI_CONNECT_RESULT:
            handle_wifi_connect_result(cb);
            break;

        case NET_EVENT_WIFI_DISCONNECT_RESULT:
            handle_wifi_disconnect_result(cb);
            break;

        case NET_EVENT_IPV4_ADDR_ADD:
            handle_ipv4_result(iface);
            break;

        default:
            break;
    }
}

void wifi_connect(void)
{
    struct net_if *iface = net_if_get_default();

    struct wifi_connect_req_params wifi_params = {0};

    wifi_params.ssid = SSID;
    wifi_params.psk = PSK;
    wifi_params.ssid_length = strlen(SSID);
    wifi_params.psk_length = strlen(PSK);
    wifi_params.channel = WIFI_CHANNEL_ANY;
    wifi_params.security = WIFI_SECURITY_TYPE_PSK;
    wifi_params.band = WIFI_FREQ_BAND_2_4_GHZ; 
    wifi_params.mfp = WIFI_MFP_OPTIONAL;

    printk("Connecting to SSID: %s\n", wifi_params.ssid);

	int8_t status = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &wifi_params, sizeof(struct wifi_connect_req_params)); 
    if (status)
    {
        printk("WiFi Connection Request Failed\n");
    }
}

void wifi_status(void)
{
    struct net_if *iface = net_if_get_default();
    
    struct wifi_iface_status status = {0};

    if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, iface, &status,	sizeof(struct wifi_iface_status)))
    {
        printk("WiFi Status Request Failed\n");
    }

    printk("\n");

    if (status.state >= WIFI_STATE_ASSOCIATED) {
        printk("SSID: %-32s\n", status.ssid);
        printk("Band: %s\n", wifi_band_txt(status.band));
        printk("Channel: %d\n", status.channel);
        printk("Security: %s\n", wifi_security_txt(status.security));
        printk("RSSI: %d\n", status.rssi);
    }
}

void wifi_disconnect(void)
{
    struct net_if *iface = net_if_get_default();

    if (net_mgmt(NET_REQUEST_WIFI_DISCONNECT, iface, NULL, 0))
    {
        printk("WiFi Disconnection Request Failed\n");
    }
}

void main(void)
{
	int sntp_status;

    printk("WiFi Example\nBoard: %s\n", CONFIG_BOARD);

    net_mgmt_init_event_callback(&wifi_cb, wifi_mgmt_event_handler,
                                 NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT);

    net_mgmt_init_event_callback(&ipv4_cb, wifi_mgmt_event_handler, NET_EVENT_IPV4_ADDR_ADD);

    net_mgmt_add_event_callback(&wifi_cb);
    net_mgmt_add_event_callback(&ipv4_cb);

#ifdef CONFIG_BOARD_NRF7002DK_NRF5340_CPUAPP
    // Delay to prevent "Unable to get wpa_s handle for wlan0" on nRF Connect SDK 2.3.0.?
    printk("Sleeping for 1 second while wlan0 comes up\n");
    k_sleep(K_SECONDS(1));
#endif

    wifi_connect();
    k_sem_take(&wifi_connected, K_FOREVER);
    wifi_status();
    k_sem_take(&ipv4_address_obtained, K_FOREVER);
    printk("Ready...\n\n");

	//SNTP Test
	memset(&addr,  0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(SNTP_PORT);
	zsock_inet_pton(AF_INET, SNTP_SERVER, &addr.sin_addr);

	sntp_status = sntp_init(&ctx, (struct sockaddr *) &addr, sizeof(addr));
	if(sntp_status < 0){
		printk("Init SNTP socket : Failure - Error %d\n", sntp_status);
	}
	else{
		printk("Init SNTP socket : Success\n");
		sntp_status = sntp_query(&ctx, 4*MSEC_PER_SEC, &s_time);
		if(sntp_status < 0){
			printk("Send SNTP query : Failure - Error %d\n", sntp_status);
		}
		else{
			printk("Send SNTP query : Success %d\n");
		}
	}
	sntp_close(&ctx);
    
}

And my prj.conf file :

#
# Copyright (c) 2022 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
CONFIG_WIFI=y
CONFIG_WIFI_NRF700X=y

# WPA supplicant
CONFIG_WPA_SUPP=y
CONFIG_NET_L2_WIFI_SHELL=y

# System settings
CONFIG_NEWLIB_LIBC=y
#CONFIG_NEWLIB_LIBC_NANO=n

# Networking
CONFIG_NETWORKING=y
CONFIG_NET_SOCKETS=y
CONFIG_POSIX_API=y
CONFIG_NET_LOG=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
CONFIG_NET_UDP=y
CONFIG_NET_TCP=y
CONFIG_NET_DHCPV4=y
CONFIG_DNS_RESOLVER=y

CONFIG_NET_STATISTICS=y
CONFIG_NET_STATISTICS_WIFI=y
CONFIG_NET_STATISTICS_USER_API=y

CONFIG_NET_PKT_RX_COUNT=8
CONFIG_NET_PKT_TX_COUNT=8

# Below section is the primary contributor to SRAM and is currently
# tuned for performance, but this will be revisited in the future.
CONFIG_NET_BUF_RX_COUNT=16
CONFIG_NET_BUF_TX_COUNT=16
CONFIG_NET_BUF_DATA_SIZE=128
CONFIG_HEAP_MEM_POOL_SIZE=153600
CONFIG_NET_TC_TX_COUNT=1

CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=4
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5
CONFIG_NET_MAX_CONTEXTS=5
CONFIG_NET_CONTEXT_SYNC_RECV=y

CONFIG_INIT_STACKS=y

CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_SHELL=y

# SNTP
CONFIG_SNTP=y

# Memories
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_SHELL_STACK_SIZE=4096
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
CONFIG_NET_TX_STACK_SIZE=4096
CONFIG_NET_RX_STACK_SIZE=4096

# Debugging
CONFIG_STACK_SENTINEL=y
CONFIG_DEBUG_COREDUMP=y
CONFIG_DEBUG_COREDUMP_BACKEND_LOGGING=y
CONFIG_DEBUG_COREDUMP_MEMORY_DUMP_MIN=y
CONFIG_SHELL_CMDS_RESIZE=n
#CONFIG_DEBUG=y
CONFIG_WPA_SUPP_LOG_LEVEL_INF=y

# Kernel options
CONFIG_ENTROPY_GENERATOR=y

# Logging
CONFIG_LOG=y
CONFIG_PRINTK=y
CONFIG_SHELL=y
CONFIG_SHELL_GETOPT=y
CONFIG_DEVICE_SHELL=y
CONFIG_POSIX_CLOCK=y
CONFIG_DATE_SHELL=y
CONFIG_NET_CONFIG_AUTO_INIT=n
CONFIG_NET_CONFIG_SETTINGS=y

CONFIG_WIFI_MGMT_EXT=y
CONFIG_WIFI_CREDENTIALS=y
CONFIG_WIFI_CREDENTIALS_BACKEND_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_NVS=y

#CONFIG_NET_SOCKETS_POSIX_NAMES = y
#CONFIG_NET_SOCKETS_POLL_MAX = 4

Thanks

  • Hey BENBF, I'm having the same problem too (especially when you posted this 1 hour ago lol), in my JlinkRTTViewer I got an earlier error where net_sntp: Failed to create UDP socket 23. Looking into this problem too, please let me know if you ever find a solution!

  • Hi

    I've taken a look at your project files and configs, but am unfortunately not able to see what the issue just from this information. The error seems to originate from sntp_query() and the zsock_send() function that's documented here: https://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html From what I can see it will return -1 and an errno  to indicate what the error is exactly. Do you have a full log file so we can get a better picture at the application?

    For the "Failed to create UDP socket 23" message, I think this is due to a file table overflow, indicating that you have too many open files in your system...

    Best regards,

    Simon

  • Hello,

    Thank you for your message.

    Here is the full log when I launch the application :

    [00:00:00.278,411] <inf> wifi_nrf: QSPI freq = 24 MHz
    [00:00:00.278,442] <inf> wifi_nrf: QSPI latency = 1
    [00:00:00.287,231] <inf> wifi_nrf: wifi_nrf_fmac_fw_load: LMAC patches loaded
    [00:00:00.298,095] <inf> wifi_nrf: wifi_nrf_fmac_fw_load: LMAC boot check passed
    [00:00:00.302,520] <inf> wifi_nrf: wifi_nrf_fmac_fw_load: UMAC patches loaded
    [00:00:00.313,323] <inf> wifi_nrf: wifi_nrf_fmac_fw_load: UMAC boot check passed
    [00:00:00.344,085] <inf> wifi_nrf: RPU LPM type: HW
    [00:00:00.466,308] <inf> fs_nvs: 6 Sectors of 4096 bytes
    [00:00:00.466,308] <inf> fs_nvs: alloc wra: 0, fe8
    [00:00:00.466,339] <inf> fs_nvs: data wra: 0, 0
    *** Booting Zephyr OS build v3.2.99-ncs2 ***
    WiFi Example
    Board: nrf7002dk_nrf5340_cpuapp
    Sleeping for 1 second while wlan0 comes up
    [00:00:00.467,864] <inf> wpa_supp: z_wpas_start: 385 Starting wpa_supplicant thread with debug level: 3
    [00:00:00.468,841] <inf> wpa_supp: Successfully initialized wpa_supplicant
    Connecting to SSID: MySSID
    [00:00:07.404,357] <inf> wpa_supp: MBO: Disable MBO/OCE due to misbehaving AP not having enabled PMF
    [00:00:07.405,914] <inf> wpa_supp: wlan0: SME: Trying to authenticate with 24:4b:fe:ac:44:64 (SSID='Blackfoot' freq=5300 MHz)
    [00:00:07.411,651] <inf> wifi_nrf: wifi_nrf_wpa_supp_authenticate:Authentication request sent successfully
    [00:00:07.662,109] <inf> wpa_supp: wlan0: Trying to associate with 24:4b:fe:ac:44:64 (SSID='Blackfoot' freq=5300 MHz)
    [00:00:07.672,882] <inf> wifi_nrf: wifi_nrf_wpa_supp_associate: Association request sent successfully
    [00:00:07.686,981] <inf> wpa_supp: wlan0: Associated with 24:4b:fe:ac:44:64
    [00:00:07.687,194] <inf> wpa_supp: MBO: Disable MBO/OCE due to misbehaving AP not having enabled PMF
    [00:00:07.687,347] <inf> wpa_supp: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
    [00:00:07.716,949] <inf> wpa_supp: wlan0: WPA: Key negotiation completed with 24:4b:fe:ac:44:64 [PTK=CCMP GTK=CCMP]
    [00:00:07.717,315] <inf> wpa_supp: wlan0: CTRL-EVENT-CONNECTED - Connection to 24:4b:fe:ac:44:64 completed [id=0 id_str=]
    Connected
    Connected
    SSID: MySSID                       
    Band: 5GHz
    Channel: 60
    Security: WPA2-PSK
    RSSI: -44
    [00:00:12.043,121] <inf> net_dhcpv4: Received: 192.168.50.21
    IPv4 address: 192.168.50.21
    Subnet: 255.255.255.0
    Router: 192.168.50.1
    Ready...
    Init SNTP socket : Success
    [00:00:12.045,043] <err> net_sntp: Failed to send over UDP socket -1
    Send SNTP query : Failure - Error -1

    This is only what the board sends in serial. Is there a better way to obtain log ?

    Best regards,

    Benjamin

  • Hi

    Unfortunately I'm not too familiar with the SNTP client sample, as it is a Zephyr specific sample that is managed by them, it could be worth asking in the Zephyr discord if they know what could cause this issue. 

    To get more debug info it could be worth adding CONFIG_DEBUG=y which I see you have commented out in your prj.conf file. 

    Best regards,

    Simon

  • Hello,

    Thank you for your answer.
    I added CONFIG_DEBUG=y to the prj.conf file but it doesn't seem to add much information...

    Since then I managed to create a UDP socket on another project, I'll try to implement the sntp request from this one instead.

    Thank for pointing out the Zephyr Discord.

    Best regards,
    Benjamin

Related