Alios things 3.3.0 Wi Fi networking

 

 

1. Alios things Wi Fi Networking Background

Accessing Wi Fi network is the first step of networking most IoT devices.

Access to Wi Fi generally requires two stages: distribution and networking.

1.1 introduction to Netmgr

The Netmgr module introduced in this paper abstracts the distribution network and networking capabilities driven by Wi Fi, which is convenient for IoT devices to quickly access the network and connect to the cloud.

Wi Fi devices need to be connected to Wi Fi hotspot (Wi Fi AP) before they can communicate with other devices based on IP. We call the step of obtaining the SSID / password of Wi Fi hotspot by Wi Fi device as Wi Fi distribution network. For mobile phones / computers / tablets, users can enter the SSID / password of Wi Fi hotspot through keyboard or touch screen. However, for IoT devices without keyboard and touch screen, how to obtain the SSID / password of Wi Fi hotspot is the first key step to realize device network management.

1.2 distribution network

The following two distribution modes are briefly introduced:

1. Zero configuration: a distribution network scheme that does not require users to input hotspot information. It is to let a device connected to the internet hotspot send the SSID / password of the hotspot to the device to be distributed.

2. One click distribution network: the mobile APP packages the corresponding information to the specific area of 802.11 data packet and sends it to the surrounding environment; The Wi Fi module of the intelligent device is in promiscuous mode and monitors all messages in the network until the required information is parsed (the data format was agreed by both parties before).

For more information about distribution network, please refer to the article:

N ways of Wi Fi Internet of things device distribution network

1.3 networking

In either way, the ultimate goal is to get SSID/PASSWORD, connect to AP and access the network.

The terminal device needs to go through three stages to successfully connect to Wi Fi:

1) Scanning phase (SCAN);

2) Authentication phase;

3) Association.

After the association is successful, the terminal device initiates a DHCP request or uses a static IP address, which indicates that the network connection is successful.

 

2. Important Netmgr} API

Netmgr provides a set of API support to facilitate users to quickly access AP.

 

2.1,netmgr_service_init

Initialize netmgr module

 

Function prototype

int netmgr_service_init(utask_t *task);

parameter list

Parameter name

Parameter description

Parameter example

task

At present, only NULL needs to be passed in

NULL

Return parameters

0, successful

Less than 0, failed

 

2.2,netmgr_service_deinit

De initialize netmgr

Function prototype

void netmgr_service_deinit(void);

parameter list

Parameter name

Parameter description

Parameter example

nothing

nothing

nothing

Return parameters

nothing

 

2.3,netmgr_add_dev

Add netmgr device

Function prototype

int netmgr_add_dev(const char* name);

parameter list

Parameter name

Parameter description

Parameter example

name

Add full device pathname

"/dev/wifi0"

 

Return parameters

0, successful

Less than 0, failed

 

2.4,netmgr_get_dev

Get the netmgr handle of the device according to the complete device pathname

Function prototype

netmgr_hdl_t netmgr_get_dev(const char* name);

parameter list

Parameter name

Parameter description

Parameter example

name

Added full device pathname

"/dev/wifi0"

Return parameters

Greater than or equal to 0, successful

Less than 0, failed

 

2.5,netmgr_set_ifconfig

Set network card related information

Function prototype

int netmgr_set_ifconfig(netmgr_hdl_t hdl, netmgr_ifconfig_info_t* info);

parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

info

Set network card configuration information

nothing

Return parameters

0, successful

Less than 0, failed

remarks:

The following is netmgr_ifconfig_info_t, where rssi does not support setting, but only reading.

#define IPADDR_STR_LEN 16
/** @brief  this struct defines netmgr ifconfig info */
typedef struct netmgr_ifconfig_info {
   bool dhcp_en;                         /**< dhcp is enabled */
   char ip_addr[IPADDR_STR_LEN];         /**< ip address */
   char mask[IPADDR_STR_LEN];            /**< ip address mask */
   char gw[IPADDR_STR_LEN];              /**< gateway ip address */
   char dns_server[IPADDR_STR_LEN];      /**< dns server address */
   char mac[6];                          /**< mac address */
   int  rssi;                            /**< rssi */
} netmgr_ifconfig_info_t;

 

2.6,netmgr_get_ifconfig

Set network card related information

Function prototype

int netmgr_get_ifconfig(netmgr_hdl_t hdl, netmgr_ifconfig_info_t* info);

parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

info

Get network card configuration information

nothing

Return parameters

0, successful

Less than 0, failed

 

2.7,netmgr_set_auto_reconnect

Start netmgr

Function prototype

void netmgr_set_auto_reconnect(netmgr_hdl_t hdl, bool enable);

parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

enable

Whether to automatically initiate Wi Fi networking

nothing

Return parameters

nothing

 

remarks

When the parameter autoconfig is true, the Wi Fi function can be automatically connected. If the device has a successful AP connection record, it will automatically disconnect the SSID and Password in the successfully linked AP record. If the device has no record of successfully connecting to the AP, it will not automatically initiate the networking action. When the parameter autoconfig is false, the function of automatically connecting to the AP will be turned off, and the AP will not be connected.

 

2.8,netmgr_connect

Connect network

Function prototype

int netmgr_connect(netmgr_hdl_t hdl, netmgr_connect_params_t* params);

parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

params

Network connection parameters

nothing

 

Return parameters

0, successful

Others, failed

remarks:

netmgr_connect_params_t structure only supports Wi Fi at present, and we will supplement other connection methods in the future.

/** @brief network type */
typedef enum {
    NETMGR_TYPE_WIFI,
    NETMGR_TYPE_GPRS,
    NETMGR_TYPE_NBIOT,
    NETMGR_TYPE_ETH,
    NETMGR_TYPE_MAX
} netmgr_type_t;

/** @brief  netmgr wifi connect params */
typedef struct netmgr_wifi_conenct_params {
    char        ssid[NETMGR_SSID_MAX_LEN+1];   /**< wifi ssid to connect*/
    char        pwd[NETMGR_PWD_MAX_LEN+1];     /**< wifi password to connect */
    char        bssid[NETMGR_BSSID_MAX_LEN];   /**< wifi bssid to connect */
    int         timeout;                       /**< wifi connect timeout */
} netmgr_wifi_connect_params_t;

/** @brief  netmgr connect params */
typedef struct netmgr_connect_params {
    netmgr_type_t type;
    union {
        netmgr_wifi_connect_params_t wifi_params;     /**< wifi connect params */
    } params;
} netmgr_connect_params_t;

 

2.9,netmgr_disconnect

Disconnect the network

Function prototype

int netmgr_disconnect(netmgr_hdl_t hdl);

parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

 

Return parameters

0, successful

Others, failed

 

2.10,netmgr_get_state

Gets the status of the current network

Function prototype

int netmgr_get_state(netmgr_hdl_t hdl);

parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

Return parameters

typedef enum netmgr_conn_state{                                                                     
    CONN_STATE_DISCONNECTING,                                                                       
    CONN_STATE_DISCONNECTED,                                                                        
    CONN_STATE_CONNECTING,                                                                          
    CONN_STATE_CONNECTED,                                                                           
    CONN_STATE_OBTAINING_IP,                                                                        
    CONN_STATE_NETWORK_CONNECTED,                                                                   
    CONN_STATE_FAILED,                                                                              
    CONN_STATE_UNKNOWN                                                                              
} netmgr_conn_state_t;

 

2.11,netmgr_save_config

Save network configuration

Function prototype

int netmgr_save_config(netmgr_hdl_t hdl);
parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

 

Return parameters

0, successful

Others, failed

remarks:

Generally, the configuration file is saved after the network connection is successful.

 

2.12,netmgr_get_config

Get netmgr configuration file

 

Prototype function

int netmgr_get_config(netmgr_hdl_t hdl, netmgr_config_t* config);

parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

config

netmgr configuration information

nothing

Return parameters

0, successful

Others, failed

remarks:

/** @brief  this struct defines wifi ap info */
typedef struct {
    char     ssid[NETMGR_SSID_MAX_LEN+1];       /**< ssid of wifi ap */
    uint8_t  pwd[NETMGR_PWD_MAX_LEN+1];         /**< password of wifi ap */
    uint8_t  bssid[NETMGR_BSSID_MAX_LEN];       /**< bssid of wifi ap */
    int8_t   ap_power;                          /**< signal strength of wifi ap */
    uint8_t  channel;                           /**< signal channel of wifi ap */
    uint8_t  sec_type;                          /**< details see netmgr_wifi_sec_type */
    bool     contain_chinese;                   /**< true:contain chinese false:no chinese */
    netmgr_ssid_format_e  ssid_format;          /**< ssid string format */
    char     gbk_ssid[NETMGR_SSID_MAX_LEN+1];   /**< gbk ssid string */
} netmgr_wifi_ap_info_t;

/** @brief  this struct defines wifi ap config */
typedef struct {
    int ap_num;                                          /**< ap number of array */
    int used_ap;                                         /**< ap that is used in the array */
    netmgr_wifi_ap_info_t config[MAX_AP_CONFIG_NUM];     /**< The ap information array */
} netmgr_wifi_ap_config_t, netmgr_wifi_config_t;

/** @brief  netmgr config struct */
typedef struct netmgr_config {
    netmgr_type_t type;
    union {
        netmgr_wifi_config_t wifi_config;          /**< wifi config struct */
        //netmgr_gprs_config_t gprs_config;
        //netmgr_nbiot_config_t nbiot_config;
        //netmgr_eth_config_t eth_config;
    } config;
} netmgr_config_t;

 

2.13,netmgr_del_config

Delete netmgr profile

Function prototype

int netmgr_del_config(netmgr_hdl_t hdl, netmgr_del_config_t* config);

parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

config

netmgr configuration information

nothing

Return parameters

0, successful

Others, failed

remarks:

/** @brief  netmgr delete config */
typedef struct netmgr_del_config {
    netmgr_type_t type;
    union {
        char ssid[NETMGR_SSID_MAX_LEN+1];      /**< wifi ssid to delete */
    } config;
} netmgr_del_config_t;

 

2.14,netmgr_set_msg_cb

Set message callback function

Function prototype

int netmgr_set_msg_cb(netmgr_hdl_t hdl, netmgr_msg_cb_t cb);
parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

cb

Message callback function

nothing

 

Return parameters

0, successful

Others, failed

remarks:

/** @brief  netmgr message type */
typedef enum {
    NETMGR_MSGID_MIN = 0,
    NETMGR_MSGID_WIFI_STATUS = NETMGR_MSGID_MIN,
    NETMGR_MSGID_WIFI_STATUS_FROM_IMPL,
    NETMGR_MSGID_WIFI_TRACE_FROM_IMPL,
    NETMGR_MSGID_NETWORK_STATUS,
    NETMGR_MSGID_ETH_STATUS_FROM_IMPL,
    NETMGR_MSGID_MAX
} netmgr_msgid_t;

/** @brief  this struct defines netmgr message */
typedef struct {
    netmgr_msgid_t id;                        /**< netmgr msg id */
    union {
        int   status;                         /**< reason of status change */
        void *network_status_change;          /**< msg content of status change */
        void *trace;
    } data;
} netmgr_msg_t;

/** @brief  this struct defines netmgr message callback function */
typedef void (*netmgr_msg_cb_t)(netmgr_msg_t* msg);
When netmgr msg id be equal to NETMGR_MSGID_WIFI_STATUS_FROM_IMPL, Reported data yes status,Its value is defined in eventid.h, Details are as follows:

#define EVENT_NETMGR_BASE                       0x01000
#define EVENT_NETMGR_WIFI_DISCONNECTED          (EVENT_NETMGR_BASE + 1)  // Connection disconected
#define EVENT_NETMGR_WIFI_SCAN_STARTED          (EVENT_NETMGR_BASE + 2)  // Scan start
#define EVENT_NETMGR_WIFI_SCAN_FAILED           (EVENT_NETMGR_BASE + 3)  // Scan failed
#define EVENT_NETMGR_WIFI_SCAN_DONE             (EVENT_NETMGR_BASE + 4)  // Scan failed
#define EVENT_NETMGR_WIFI_NETWORK_NOT_FOUND     (EVENT_NETMGR_BASE + 5)  // no AP found
#define EVENT_NETMGR_WIFI_AUTHENTICATING        (EVENT_NETMGR_BASE + 6)  // Authentication start
#define EVENT_NETMGR_WIFI_AUTH_REJECT           (EVENT_NETMGR_BASE + 7)  // Authentication rejected by AP
#define EVENT_NETMGR_WIFI_AUTH_TIMEOUT          (EVENT_NETMGR_BASE + 8)  // Authentication timeout with AP
#define EVENT_NETMGR_WIFI_ASSOCIATING           (EVENT_NETMGR_BASE + 9)  // Association starts
#define EVENT_NETMGR_WIFI_ASSOC_REJECT          (EVENT_NETMGR_BASE + 10) // Association rejected by AP
#define EVENT_NETMGR_WIFI_ASSOC_TIMEOUT         (EVENT_NETMGR_BASE + 11) // Association timeout with AP
#define EVENT_NETMGR_WIFI_ASSOCIATED            (EVENT_NETMGR_BASE + 12) // Authentication succeed
#define EVENT_NETMGR_WIFI_4WAY_HANDSHAKE        (EVENT_NETMGR_BASE + 13) // 4way-handshark start
#define EVENT_NETMGR_WIFI_HANDSHAKE_FAILED      (EVENT_NETMGR_BASE + 14) // 4way-handshake fails
#define EVENT_NETMGR_WIFI_4WAY_HANDSHAKE_DONE   (EVENT_NETMGR_BASE + 15) // 4way-handshark done
#define EVENT_NETMGR_WIFI_GROUP_HANDSHAKE       (EVENT_NETMGR_BASE + 16) // group-handshark start
#define EVENT_NETMGR_WIFI_GROUP_HANDSHAKE_DONE  (EVENT_NETMGR_BASE + 17) // group-handshark done = completed
#define EVENT_NETMGR_WIFI_CONNECTED             (EVENT_NETMGR_BASE + 18) // Connection to AP done
#define EVENT_NETMGR_WIFI_CONN_TIMEOUT          (EVENT_NETMGR_BASE + 19) // Connection timeout
#define EVENT_NETMGR_WIFI_DEAUTH                (EVENT_NETMGR_BASE + 20) // Deauth received from AP
#define EVENT_NETMGR_WIFI_MAX                   (EVENT_NETMGR_WIFI_DEAUTH)

#define EVENT_NETMGR_DHCP_BASE                  (EVENT_NETMGR_WIFI_MAX)
#define EVENT_NETMGR_DHCP_START_FAILED          (EVENT_NETMGR_DHCP_BASE + 1)  // DHCP start fails
#define EVENT_NETMGR_DHCP_TIMEOUT               (EVENT_NETMGR_DHCP_BASE + 2)  // DHCP timeout
#define EVENT_NETMGR_DHCP_SUCCESS               (EVENT_NETMGR_DHCP_BASE + 3)  // DHCP success
#define EVENT_NETMGR_DHCP_MAX                   (EVENT_NETMGR_DHCP_SUCCESS)

#define EVENT_NETMGR_SNTP_BASE                  (EVENT_NETMGR_DHCP_MAX)
#define EVENT_NETMGR_SNTP_SUCCESS               (EVENT_NETMGR_SNTP_BASE + 1)  // SNTP success
#define EVENT_NETMGR_SNTP_FAILED                (EVENT_NETMGR_SNTP_BASE + 2 ) // SNTP failure
#define EVENT_NETMGR_SNTP_MAX                   (EVENT_NETMGR_SNTP_FAILED)

#define EVENT_NETMGR_CONN_BASE                  (EVENT_NETMGR_SNTP_MAX)
#define EVENT_NETMGR_CONN_RECONNECT             (EVENT_NETMGR_CONN_BASE + 1)  // Reconnect AP
#define EVENT_NETMGR_CONN_MAX                   (EVENT_NETMGR_CONN_RECONNECT)

#define EVENT_NETMGR_GOT_IP                     (EVENT_NETMGR_DHCP_SUCCESS)

#define EVENT_NETMGR_NET_BASE                   (EVENT_NETMGR_CONN_MAX)
#define EVENT_NETMGR_NET_DISCON                 (EVENT_NETMGR_WIFI_DISCONNECTED)
#define EVENT_NETMGR_NET_CONFIG                 (EVENT_NETMGR_NET_BASE + 1)
#define EVENT_NETMGR_MAX                        (EVENT_NETMGR_NET_CONFIG)
When netmgr msg id be equal to NETMGR_MSGID_NETWORK_STATUS,Reported data yes network_status_change,The corresponding structure is as follows:

#define NETMGR_WIFI_METHOD_MAX_LENGTH       (32)
#define NETMGR_WIFI_STATUS_MAX_LENGTH       (32)
#define NETMGR_WIFI_SSID_MAX_LENGTH         (32)
#define NETMGR_WIFI_PASSWORD_MAX_LENGTH     (64)

/** @brief  this struct defines netmgr wifi status change info */
typedef struct {
   char method[NETMGR_WIFI_METHOD_MAX_LENGTH+1];       /**< status change method */
   int  quantity;                                      /**< signal quantity */
   char status[NETMGR_WIFI_STATUS_MAX_LENGTH+1];       /**< current status */
   char ssid[NETMGR_WIFI_SSID_MAX_LENGTH+1];           /**< ssid of wifi */
   char password[NETMGR_WIFI_PASSWORD_MAX_LENGTH+1];   /**< password of wifi */
   uint8_t reason_code;                                /**< reason of status change */
} netmgr_wifi_network_status_change_t;

 

2.15,netmgr_del_msg_cb

Delete message callback function

Function prototype

int netmgr_del_msg_cb(netmgr_hdl_t hdl, netmgr_msg_cb_t cb);
parameter list

Parameter name

Parameter description

Parameter example

hdl

netmgr handle

nothing

cb

Message callback function

nothing

Return parameters

0, successful

Others, failed

 

3. API usage examples

3.1. Direct networking

Note: netmgr needs the support of event service internally, so it needs to call netmgr_ service_ The init function calls event while initializing netmgr_service_init initialization function.

Using event_subscribe registers to listen for specific events, while netmgr_set_msg_cb can listen to all events supported by netmgr.

#include "netmgr.h"
#include <uservice/uservice.h>
#include <uservice/eventid.h>
const char* ssid = "aos";
const char* passwd = "123456";


static void wifi_event_cb(uint32_t event_id, const void *param, void *context)
{
    printf("Got IP\r\n");
}


static void netmgr_comp_example()
{
    netmgr_connect_params_t netmgr_params;
    netmgr_hdl_t netmgr_handle;
    printf("netmgr test \r\n");

    event_service_init(NULL);

    netmgr_service_init(NULL);

    event_subscribe(EVENT_NETMGR_DHCP_SUCCESS, wifi_event_cb, NULL);

    memset(&netmgr_params, 0, sizeof(netmgr_connect_params_t));

    strncpy(netmgr_params.params.wifi_params.ssid, ssid, NETMGR_SSID_MAX_LEN);

    strncpy(netmgr_params.params.wifi_params.pwd, passwd, NETMGR_PWD_MAX_LEN);

    netmgr_params.params.wifi_params.timeout = 5000;


    netmgr_handle = netmgr_get_dev("/dev/wifi0");

    if(netmgr_handle < 0) {

        printf("get netmgr handle failed\n");

        return;

    }

    if(netmgr_connect(netmgr_handle, &netmgr_params) != 0) {

        printf("netmgr connect failed");

        return;

    }

}

 

3.2. Use history networking

#include "netmgr.h"


void start_netmgr(void) {

    printf("netmgr test \r\n");


    event_service_init(NULL);


    netmgr_service_init(NULL);
 

    netmgr_set_auto_reconnect(true);

}

 

3.3 example of using netmgr

A WiFi is registered in the above example_ event_ CB function to listen for events, EVENT_NETMGR_DHCP_SUCCESS indicates that the IP address is successfully obtained.

Netmgr in the above example_ comp_ The example function is added to the file solutions/helloworld_demo/helloworld.c.

int application_start(int argc, char *argv[])

     int count = 0;

     printf("nano entry here!\r\n");


     netmgr_comp_example();


     while(1) {
        printf("hello world! count %d \r\n", count++);

        aos_msleep(1000);
    };
}

 

3.4. Add netmgr module

solutions/helloworld_ demo/package. The second part of yaml file: add netmgr component to dependency information

## Part II: dependent information

#Specify the component and version that the component depends on. The version supports condition comparison. Support: > = v1 0, >v1. 0, ==v1. 0, <=v1. 0, <v1. 0, v1. 0

#If no condition is specified, the default is = =, such as v1 0 and = = v1 0

# depends:                                 # < optional > this component depends on other components. Only reasonable dependence can ensure that the component can be compiled and used

#   - minilibc: v7.2.0

#   - aos: >=v7.2.0

depends:

  - init: dev_aos

  - cli: dev_aos

  - osal_aos: dev_aos

  - haas100: dev_aos

  - netmgr: dev_aos

 

4. Netmgr command

4.1 introduction

Support operations related to Wi Fi networking by using commands, such as reading / writing / deleting the stored connection information, printing the information of all APS in the current network, connecting AP, disconnecting AP, querying network status, etc.

command line

explain

netmgr -t wifi -i

initialization

netmgr -t wifi -a [0/1]

Set whether to automatically reconnect. 0, no automatic reconnection; 1. Automatic reconnection.

netmgr -t wifi -b [0/1]

Whether to save the connection history. 0, do not save connection history. 1. Save the connection history.

netmgr -t wifi -c [ssid] [password]

Networking with ssid password

netmgr -t wifi -e

Disconnect Wi Fi

netmgr -t wifi -m

Set MAC address

netmgr -t wifi -s

Print information about AP S on the current network

netmgr -t wifi -p

Print current network status

netmgr -t wifi -r

Read Wi Fi profile

netmgr -t wifi -w [wifi_config]

Write Wi Fi profile. wifi_config format, for example, network={\nssid=\"apple\"\npassword=\"aos123456\"\nchannel=\"1\"\n}\n

netmgr -t wifi -d

Delete Wi Fi profile

netmgr -t wifi -n

1. Enable sntp; 2. Turn off sntp. At present, there is no parameter to query sntp status. By default, sntp function is turned on.

 

4.2. Command example

Use the following command to quickly connect the AP whose SSID is "aos" and password is "123456".

netmgr -t wifi -i

netmgr -t wifi -b 1

netmgr -t wifi -a 1

netmgr -t wifi -c aos 123456

You can also write the configuration command manually and use the following command to connect to the network. If the channel information of the AP cannot be determined, use 0 to scan the whole network segment.

netmgr -t wifi -i

netmgr -t wifi -w network={\\nssid=\"apple\"\\npassword=\"aos123456\"\\nchannel=\"1\"\\n}\\n

netmgr -t a 1

 

Developer Support

HaaS Solution Center: https://haas.iot.aliyun.com/
HaaS technology community: https://blog.csdn.net/HaaSTech


The developers' nail group and official account are shown below, and developers' staple groups have technical support students on duty every day.

 

Keywords: network Operating System wifi

Added by tomkure on Tue, 01 Feb 2022 01:26:25 +0200