Issues with the Wi-Fi driver interface

Hi,

I'm currently trying to write a simple Wi-Fi driver code that can connect to my Wi-Fi network. I'm building for the nrf7002dk_nrf5340_cpuapp and am using NCS v2.5.3.

For this, I based my code on Exercise 2 of the Wi-Fi fundementals lesson 2.

However, when I try to run my wifi_connect function, it returns an error -134.

int8_t wifi_connect(char* ssid, char* passkey)
{
    struct net_if *iface = net_if_get_first_wifi();
    if (iface == NULL) {
        LOG_ERR("No Wi-Fi network interface found!");
        return -ENODEV;
    }

    struct wifi_connect_req_params params = {
        .ssid = ssid,
        .ssid_length = strlen(ssid),
        .psk = passkey,
        .psk_length = strlen(passkey),
        .channel = WIFI_CHANNEL_ANY,
        .security = WIFI_SECURITY_TYPE_PSK,
        .mfp = WIFI_MFP_OPTIONAL,
        .timeout = SYS_FOREVER_MS,
    };

    int err = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &params, sizeof(params));
    if (err) {
        LOG_ERR("Wi-Fi Connection Failed (Error: %d)", err);
        return err;
    }

    LOG_INF("Connecting to Wi-Fi: %s", ssid);
    return 0;
}

When stepping into the 

net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &params, sizeof(params)) while debugging i see that the error comes from this part in the wifi_mgmt.c
const struct wifi_mgmt_ops *const wifi_mgmt_api = get_wifi_api(iface);

if (wifi_mgmt_api == NULL || wifi_mgmt_api->connect == NULL) {
	return -ENOTSUP;
}

When I look at wifi_mgmt_api, I see that there is data in it, but not in wifi_mgmt_api->connect. It does, however, contain data in the scan variable.

I think it has something to do with the iface, but I can't find anything about it online.

Could someone please explain to me how the iface works,
what determines whether wifi_mgmt_api->connect is populated,
and what might cause it to not want to connect to Wi-Fi?

these are my configs
#wifi settings
CONFIG_WIFI=y
CONFIG_WIFI_NRF700X=y
CONFIG_NET_L2_WIFI_MGMT=y
CONFIG_NET_CONFIG_SETTINGS=y

# Networking
CONFIG_NETWORKING=y
CONFIG_NET_L2_ETHERNET=y
CONFIG_NET_NATIVE=n
CONFIG_NET_OFFLOAD=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
CONFIG_NET_UDP=y
CONFIG_NET_TCP=y

# Enable network management
CONFIG_NET_MGMT=y
CONFIG_NET_MGMT_EVENT=y

CONFIG_INIT_STACKS=y

# Memory and Stack Settings
CONFIG_HEAP_MEM_POOL_SIZE=25000
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_INIT_STACKS=y
CONFIG_STACK_SENTINEL=y

# Debugging and Logging
CONFIG_DEBUG_COREDUMP=y
CONFIG_DEBUG_COREDUMP_BACKEND_LOGGING=y
CONFIG_DEBUG_COREDUMP_MEMORY_DUMP_MIN=y
CONFIG_LOG=y

# Bluetooth Configuration
CONFIG_BT=y
CONFIG_BT_BROADCASTER=y
CONFIG_BT_OBSERVER=y
CONFIG_BT_SMP=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_BUF_ACL_RX_SIZE=151
CONFIG_BT_L2CAP_TX_MTU=147
CONFIG_BT_BUF_ACL_TX_SIZE=151
CONFIG_BT_RX_STACK_SIZE=4096
CONFIG_BT_BONDABLE=n
CONFIG_BT_DEVICE_NAME_DYNAMIC=y

# NFC Configuration
CONFIG_NFC_T4T_NRFXLIB=y
CONFIG_NFC_NDEF=y
CONFIG_NFC_NDEF_MSG=y
CONFIG_NFC_NDEF_RECORD=y
CONFIG_NFC_NDEF_LE_OOB_REC=y
CONFIG_NFC_NDEF_CH_MSG=y
CONFIG_NFC_NDEF_TNEP_RECORD=y
CONFIG_NFC_TNEP_TAG=y
CONFIG_NFC_NDEF_PARSER=y
CONFIG_NFC_NDEF_CH_PARSER=y
CONFIG_NFC_NDEF_LE_OOB_REC_PARSER=y
CONFIG_NFC_TNEP_CH=y

# System Settings
CONFIG_NEWLIB_LIBC=y
CONFIG_REBOOT=y

# Enable UART driver
CONFIG_SERIAL=y
CONFIG_UART_ASYNC_API=y
CONFIG_UART_CONSOLE=y  # Enable UART console
Kind regards, JonasR
  • Hi,

    You are missing the band parameter in the connection request parameters struct. I tested your code in lesson 2 exercise 2, and I was able to connect after adding band like this:

    int8_t wifi_connect(char* ssid, char* passkey)
    {
        struct net_if *iface = net_if_get_first_wifi();
        if (iface == NULL) {
            LOG_ERR("No Wi-Fi network interface found!");
            return -ENODEV;
        }
    
        struct wifi_connect_req_params params = {
            .ssid = ssid,
            .ssid_length = strlen(ssid),
            .psk = passkey,
            .psk_length = strlen(passkey),
            .channel = WIFI_CHANNEL_ANY,
            .security = WIFI_SECURITY_TYPE_PSK,
            .mfp = WIFI_MFP_OPTIONAL,
            .timeout = SYS_FOREVER_MS,
            .band = WIFI_FREQ_BAND_UNKNOWN,
        };
    
        int err = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &params, sizeof(params));
        if (err) {
            LOG_ERR("Wi-Fi Connection Failed (Error: %d)", err);
            return err;
        }
    
        LOG_INF("Connecting to Wi-Fi: %s", ssid);
        return 0;
    }

    Best regards,
    Marte

  • Hi Marte,
    thanks for the quick response!
    I added .band to the params but i still get the same return. i have added my source code if that helps to recreate my problem.

    #include "drivers/wifi/wifi_driver.h"
    // #include "drivers/nfc/nfc_functions.h"
    // #include "drivers/ble/ble_driver.h"
    
    #include <stdio.h>
    #include <stddef.h>
    #include <stdbool.h>
    #include <string.h>
    #include <errno.h>
    #include <zephyr/net/net_if.h>
    #include <zephyr/logging/log.h>
    #include <zephyr/kernel.h>
    
    LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
    
    //extern struct k_msgq nfc_msgq;  // Externe NFC message queue
    
    int main(void)
    {
        LOG_INF("Starting main application");
    
        // Initialize Wi-Fi, NFC, and Bluetooth with error handling
        wifi_init();
        // nfc_init();
        // init_bt(BLE_CENTRAL, BT_LE_SCAN_TYPE_ACTIVE, BT_LE_SCAN_OPT_FILTER_DUPLICATE, 
        //         BT_GAP_SCAN_FAST_INTERVAL, BT_GAP_SCAN_FAST_WINDOW);
    
        //nfc_wifi_data_t nfc_wifi_data;
    
        struct net_if *iface = net_if_get_first_wifi();
        if (!iface) {
            LOG_ERR("No Wi-Fi interface found!");
            return -1;
        }
    
        while (1) 
        {
            if (!net_if_is_up(iface)) {
                LOG_INF("Wi-Fi is down. Attempting to connect...");
    
                int err = wifi_connect("MUG_test", "mug_test");
                if (err == 0) {
                    LOG_INF("Wi-Fi connection attempt started");
                } else {
                    LOG_ERR("Failed to initiate Wi-Fi connection (Error: %d)", err);
                }
            } else {
                LOG_INF("Wi-Fi is already up");
            }
    
            k_sleep(K_SECONDS(5));  // Avoid constant retries, wait 5 seconds before checking again
        }
    }
    wifi_driver.h
    #include <stdint.h>
    
    #include <zephyr/kernel.h>
    #include <zephyr/logging/log.h>
    
    #include <zephyr/net/wifi.h>
    #include <zephyr/net/wifi_mgmt.h>
    #include <zephyr/net/net_mgmt.h>
    
    LOG_MODULE_REGISTER(wifi_driver, LOG_LEVEL_INF);
    
    static struct net_mgmt_event_callback wifi_mgmt_cb;
    
    static void wifi_event_handler(struct net_mgmt_event_callback *cb,
                                   uint32_t event, struct net_if *iface)
    {
        if (event == NET_EVENT_WIFI_CONNECT_RESULT) {
            struct wifi_status *status = (struct wifi_status *)cb->info;
            if (status->status) {
                LOG_ERR("Wi-Fi Connection Failed (Status: %d)", status->status);
            } else {
                LOG_INF("Wi-Fi Connected Successfully!");
            }
        } else if (event == NET_EVENT_WIFI_DISCONNECT_RESULT) {
            LOG_WRN("Wi-Fi Disconnected!");
        }
    }
    
    uint8_t wifi_init(void)
    {
        struct net_if *iface = net_if_get_default();
        if (!iface) {
            LOG_ERR("No default network interface found!");
            return -ENODEV;
        }
    
        net_mgmt_init_event_callback(&wifi_mgmt_cb, wifi_event_handler,
                                     NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT);
        net_mgmt_add_event_callback(&wifi_mgmt_cb);
    
        LOG_INF("Wi-Fi Initialized");
        return 0;
    }
    
    int8_t wifi_connect(char* ssid, char* passkey)
    {
        struct net_if *iface = net_if_get_first_wifi();
        if (iface == NULL) {
            LOG_ERR("No Wi-Fi network interface found!");
            return -ENODEV;
        }
    
        struct wifi_connect_req_params params = {
            .ssid = ssid,
            .ssid_length = strlen(ssid),
            .psk = passkey,
            .psk_length = strlen(passkey),
            .channel = WIFI_CHANNEL_ANY,
            .security = WIFI_SECURITY_TYPE_PSK,
            .mfp = WIFI_MFP_OPTIONAL,
            .timeout = SYS_FOREVER_MS,
            .band = WIFI_FREQ_BAND_UNKNOWN,
        };
    
        int err = net_mgmt(NET_REQUEST_WIFI_CONNECT, iface, &params, sizeof(params));
        if (err) {
            LOG_ERR("Wi-Fi Connection Failed (Error: %d)", err);
            return err;
        }
    
        LOG_INF("Connecting to Wi-Fi: %s", ssid);
        return 0;
    }
    

    thanks in advance.
    Kind regards,

    JonasR

  • Hi,

    There are some incorrect configurations in your prj.conf. You should remove the following Kconfigs:

    CONFIG_NET_L2_WIFI_MGMT=y
    CONFIG_NET_CONFIG_SETTINGS=y

    You must also change CONFIG_NET_NATIVE from n to y and increase the stack sizes of CONFIG_NET_MGMT_EVENT_STACK_SIZE and CONFIG_HEAP_MEM_POOL_SIZE. Lastly, you must enable the WPA supplicant with CONFIG_WPA_SUPP.

    Here is the modified prj.conf:

    #wifi settings
    CONFIG_WIFI=y
    CONFIG_WIFI_NRF700X=y
    CONFIG_WPA_SUPP=y
    
    # Networking
    CONFIG_NETWORKING=y
    CONFIG_NET_L2_ETHERNET=y
    CONFIG_NET_NATIVE=y
    CONFIG_NET_OFFLOAD=y
    CONFIG_NET_SOCKETS=y
    CONFIG_NET_IPV6=y
    CONFIG_NET_IPV4=y
    CONFIG_NET_UDP=y
    CONFIG_NET_TCP=y
    
    # Enable network management
    CONFIG_NET_MGMT=y
    CONFIG_NET_MGMT_EVENT=y
    CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096
    
    CONFIG_INIT_STACKS=y
    
    # Memory and Stack Settings
    CONFIG_HEAP_MEM_POOL_SIZE=153600
    CONFIG_MAIN_STACK_SIZE=4096
    CONFIG_INIT_STACKS=y
    CONFIG_STACK_SENTINEL=y
    
    # Debugging and Logging
    CONFIG_DEBUG_COREDUMP=y
    CONFIG_DEBUG_COREDUMP_BACKEND_LOGGING=y
    CONFIG_DEBUG_COREDUMP_MEMORY_DUMP_MIN=y
    CONFIG_LOG=y
    
    # Bluetooth Configuration
    CONFIG_BT=y
    CONFIG_BT_BROADCASTER=y
    CONFIG_BT_OBSERVER=y
    CONFIG_BT_SMP=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_BUF_ACL_RX_SIZE=151
    CONFIG_BT_L2CAP_TX_MTU=147
    CONFIG_BT_BUF_ACL_TX_SIZE=151
    CONFIG_BT_RX_STACK_SIZE=4096
    CONFIG_BT_BONDABLE=n
    CONFIG_BT_DEVICE_NAME_DYNAMIC=y
    
    # NFC Configuration
    CONFIG_NFC_T4T_NRFXLIB=y
    CONFIG_NFC_NDEF=y
    CONFIG_NFC_NDEF_MSG=y
    CONFIG_NFC_NDEF_RECORD=y
    CONFIG_NFC_NDEF_LE_OOB_REC=y
    CONFIG_NFC_NDEF_CH_MSG=y
    CONFIG_NFC_NDEF_TNEP_RECORD=y
    CONFIG_NFC_TNEP_TAG=y
    CONFIG_NFC_NDEF_PARSER=y
    CONFIG_NFC_NDEF_CH_PARSER=y
    CONFIG_NFC_NDEF_LE_OOB_REC_PARSER=y
    CONFIG_NFC_TNEP_CH=y
    
    # System Settings
    CONFIG_NEWLIB_LIBC=y
    CONFIG_REBOOT=y
    
    # Enable UART driver
    CONFIG_SERIAL=y
    CONFIG_UART_ASYNC_API=y
    CONFIG_UART_CONSOLE=y  # Enable UART console
    

    Best regards,
    Marte

Related