Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files.

Network Management API

The Network Management API is used to send network requests or receive notifications on network events. The API allows applications, as well as network layer code itself, to call defined network routines at any level in the IP stack, or receive notifications on relevant network events. Procedure requests are done through a single net_mgmt() API that invokes the registered handler for the corresponding request.

Enabling the API

To enable the Network Management API in your application, we can enable the following Kconfigs

In addition, you need to include the following header files

  • wifi.h: Header file for Wi-Fi Management API
  • wifi_mgmt.h: Header file for Wi-Fi specific Network Management API’s
  • net_mgmt.h: Header file for the Network Management API

In this course, we will be using the network connection manager, an experimental feature of nRF Connect SDK, to listen to the network interface and IP events to verify whether an interface is connected or not.

The Kconfig CONFIG_NET_CONNECTION_MANAGER will add and start the connection manager in the application, and will raise L4 events “connected” and “disconnected”,

Enabling Wi-Fi

To be able to use the Network Management API to connect to Wi-Fi, we also need to enable Wi-Fi in the application.

This is done by enabling the following Kconfigs

  • CONFIG_WIFI: Enables the general Wi-Fi drivers
  • CONFIG_WIFI_NRF700X: Enables the Nordic Wi-Fi Driver
  • CONFIG_WPA_SUPP: Enables WPA (Wi-Fi Protected Access) supplicant support, primarily responsible for implementing the authentication phase of WPA, as well as network scanning, key exchange and roaming support.

Configure the Wi-Fi parameters

When connecting to Wi-Fi, the parameters are passed in the structure struct wifi_connect_req_params, which has the following signature

  • SSID – SSID of your Wi-Fi network
  • PSK – PSK, typically a passphrase or password
  • SAE password (sae_password) – SAE password, if using WPA3
  • Band (band) – Wi-Fi band you want to connect to
  • Channel (channel) – Wi-Fi channel to use
  • Security – The security type to use (NONE, PSK, PSK_SHA256, SAW, WAPI, EAP, WEP, WPA_SPK)
  • MFP – Management frame protection options
  • Timeout – Connection timeout in seconds

Here is an example of populating the Wi-Fi parameters

Request the Wi-Fi connection

All network management requests are called using net_mgmt(), which has the following signature

  • mgmt_request: a bitmask to indicate which stack layer is targeted, if a net_if object is implied and the specific management procedure being requested
  • iface: network interface being used, use the helper function net_if_get_first_wifi() or net_if_get_default().
  • data: the Wi-Fi configuration parameters, stored in struct wifi_connect_req_params
  • len: the length of data, i.e sizeof(struct wifi_connect_req_params)

To request the Wi-Fi connection, we will use the command NET_REQUEST_WIFI_CONNECT, as shown below

Listen to network events

After we have requested a Wi-Fi connection, we want to register a callback function to listen to network events so the application is notified when the Wi-Fi connection has been established.

The callback handler function net_mgmt_event_handler_t must have the following signature

The Wi-Fi-related network events are defined in the Wi-Fi Management API, such as NET_EVENT_WIFI_CONNECT_RESULT and NET_EVENT_WIFI_DISCONNECT_RESULT.

However, we will instead be using the events defined in the network connection manager: NET_EVENT_L4_CONNECTED and NET_EVENT_L4_DISCONNECTED. The network connection manager adds a layer of abstraction to your application to easily be able to port an application between different connection protocols, for example, Wi-Fi and cellular.

It is important to note that the NET_EVENT_L4_CONNECTED event is only triggered once your device has received an IP address, so CONFIG_DHCPV4 must be enabled in the application.

Define the callback function

Define the callback function net_mgmt_event_handler()

Initialize and add the callback structure

First, define the Network Management event callback structure, struct net_mgmt_event_callback.

static struct net_mgmt_event_callback mgmt_cb;

To initialize the callback structure, we will use the helper function net_mgmt_init_event_callback() which has the following signature

Then, we need to add the callback structure using net_mgmt_add_event_callback() which has the following signature

net_mgmt_init_event_callback(&mgmt_cb, net_mgmt_event_handler, EVENT_MASK);
net_mgmt_add_event_callback(&mgmt_cb);

Automatic connection feature

The Wi-Fi management extension library adds an automatic connect feature to the Wi-Fi stack, by using the NET_REQUEST_WIFI_CONNECT_STORED command in net_mgmt().

Add the library to your application through the following Kconfig

Include the header file for the library in the main.c file of your application

struct net_if *iface = net_if_get_first_wifi();
int err = net_mgmt(NET_REQUEST_WIFI_CONNECT_STORED, iface, NULL, 0);
if (err) {
	LOG_ERR("Auto-connecting to Wi-Fi failed, err: %d", err);
	return ENOEXEC;
}

Important

Please note that although this is the most configurable way to request a Wi-Fi connection, since you can configure struct wifi_connect_req_params, it requires inputting Wi-Fi credentials into the application firmware and is therefore not secure and not recommended in production, but rather for development purposes. We will take a look at a more secure way to provision the network credentials in Exercise 2 of this lesson.

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.