SSD_WLAN Reference


1. Kernel Config Description

1.1. Configuring WEXT

Wext is Wireless Extension, which is the standard interface of WIFI driver and user mode process in the kernel. The iwpriv command depends on this config.


1.2. Configuring CFG80211

CFG80211 is the standard interface of WIFI driver and user mode process in the kernel. Before CFG80211 appeared, WEXT was used, and now more and more CFG80211 is used.


2. Tool usage

2.1. Use of wap_supplicant

wpa_supplicant is an independently running daemon that starts wireless network background services and processes WPA state machines, control commands, driver events, configuration information, etc. in the message loop.

Common command parameters are as follows:

-I <ifname> // network interface name

-c <conf> // configuration file name

-C <ctrl_intf> // control interface name

-D<driver> // driver type name

-p <driver_param> // driver parameter

-b <br_ifname> // bridge interface name

-d // add debugging information

Example of use:

/config/wifi/wpa_supplicant -Dnl80211 -i wlan0 -c /customer/wpa_supplicant.conf -d &

Start the wireless network service, the driver type is nl80211, the network port name is wlan0, and the configuration file path is /customer/wpa_supplicant.conf. If the hotspot information is saved in the configuration file, the connection is initiated.


2.2. wpa_cli use

wpa_cli is the client program that communicates with wpa_supplicant to search, setup and connect to the network.

Common command parameters are as follows (for example, the current network interface is wlan0):

wpa_cli –i wlan0 scan // scan for nearby hotspots

wpa_cli –i wlan0 scan_result // Display scan result

wpa_cli –I wlan0 status // View current connection information

wpa_cli –I wlan0 add_network // Create a new link and return the network id

wpa_cli –I wlan0 set_network <network id> <variable> <value> // Set network parameters, such as ssid, psk, keymgmt

wpa_cli –I wlan0 select_network <network id> // select the specified network (other connections will be disconnected)

wpa_cli –I wlan0 enable_network <network id> // Enable the specified network

wpa_cli –I wlan0 disable_network <network id> // disable the specified network

wpa_cli –I wlan0 save_config // keep connection info to wpa_supplicant.conf

2.3. iwlist usage

iwlist is used to analyze the /proc/net/wireless file to obtain information about the wireless network card.

Common command parameters are as follows (for example, the current network interface is wlan0):

iwlist wlan0 scanning // scan the current wireless network

iwlist wlan0 frequen // display channel information

iwlist wlan0 rate // show connection speed

iwlist wlan0 power // show power mode

2.4. Use of udhcpc

As a dhcp client, udhcpc dynamically obtains the IP.

Common command parameters are as follows:

-i <ifname> // Network interface name

-s <script> // udhcpc script

-a // use arping to verify the provided address

-t <count> // Number of times to send packets

-T <time> // time interval between packets (default is 3s)

-f // foreground execution

-b // background execution

-n // exit if lease acquisition fails

-q // Exit after getting the lease

Example of use:

udhcpc -q -i wlan0 -s /etc/init.d/udhcpc.script &

2.5. hostapd usage

hostapd is used to use the current device as a hotspot, allowing other network devices to access.

Example of use:

hostapd -B /config/wifi/hostapd.conf // -B: Execute the daemon in the background

2.6. dnsmasq usage

dnsmasq is a lightweight, easy-to-configure DNS forwarder and DHCP server. Its purpose is to provide DNS and optionally DHCP to a small network.

Common command parameters are as follows:

-i <ifname> // network interface name

-C <path> // Specify the configuration file path (default is /etc/dnsmasq.conf)

--no-daemon // non-background execution

Example of use:

dnsmasq -i p2p0 --no-daemon -C /config/wifi/dnsmasq.conf &

2.7. hostapd_cli use

Hostapd_cli is used as the client of hostapd. When using it, you need to start the main program of hostapd first. After hostapd is started, when running the cli client, it will automatically connect to the currently working hostapd process. After the connection is successful, the cli client can obtain and control the parameters of the hostapd application.

Common command parameters are as follows:

-i <ifname> // network interface name

-p <path> // control socket path (default is /var/run/hostapd)

-B // Execute the daemon in the background

Example of use:

/config/wifi/hostapd_cli -I p2p0 –p /var/run/hostapd all_sta &

3. WIFI Function Test

3.1. Load WIFI driver

First insmod loads the WIFI driver, take ssw101b as an example, the corresponding driver is ssw101b_wifi_HT40_usb.ko. Run insmod ssw101b_wifi_HT40_usb.ko, use ifconfig -a to view, you can see the newly generated wlan0 node and p2p0 node:


3.2. STA Mode Test

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/lib:/config/wifi
mkdir -p /tmp/wifi/run
chmod 777 /tmp/wifi/run
ifconfig wlan0 up

Create the wpa_supplicant.conf file, edit it as follows, use the test hotspot as "WiFi_test" and the password as "12345678".

Test the connection:

/config/wifi/wpa_supplicant -Dnl80211 -i wlan0 -c /appconfigs/wpa_supplicant.conf -d &

Get IP:

udhcpc -q -i wlan0 -s /etc/init.d/udhcpc.script &

View wlan0 information:

Scan for hotspots:

/config/wifi/iwlist wlan0 scan | grep Cell -A 5

It can be seen that the currently connected hotspot "WiFi_test" channel is 1.


3.3. AP Mode Test

mkdir -p /var/run/hostapd

mkdir -p /var/wifi/misc

mkdir -p /var/lib/misc

ifconfig p2p0 up

ifconfig p2p0 192.168.1.100 netmask 255.255.255.0

The hostapd configuration file is in /config/wifi/hostapd.conf, where ssid is the hotspot name of the current device, and wpa_passphrase is the connection password of the device. This file can be modified to change the device name and password.

Enable hotspot:

/config/wifi/hostapd -B /config/wifi/hostapd.conf

/config/wifi/dnsmasq -i p2p0 --no-daemon -C /config/wifi/dnsmasq.conf &

The network device can search for the hotspot "ssw101bap" and use the mobile phone to connect to the hotspot:

View device connection information:

/config/wifi/hostapd_cli -i p2p0 -p /var/run/hostapd all_sta &

You can see that the number of accesses is 1, and the access device mac is the same as the mobile phone mac.


3.4. Bridge Mode Test

When WIFI is enabled with STA and AP at the same time, a bridge is established between wlan0 and p2p0.

Kernel configuration adds network bridging support, and compiling the kernel in the kernel/modules directory will generate llc.ko, stp.ko, and bridge.ko. These three drivers are loaded in sequence before the network bridge. Note: Kernel network related configuration changes, the wifi driver may need to be recompiled.

To establish a bridge, you also need to edit /config/wifi/hostapd.conf, add the bridge field, and modify the channel so that it is the same as the channel for accessing the hotspot in STA mode.

After compiling the kernel and wifi driver, load the wifi driver according to section 3.1, and then follow the steps in sections 3.2 and 3.3 to enable STA mode to access the test hotspot "WiFi_test", and enable AP mode to open the hotspot.

Create a bridge:

brctl addbr br0

brctl addif br0 wlan0

brctl addif br0 p2p0

ifconfig br0 up

Use the test phone to access the hotspot "ssw101bap". When "WiFi_test" is connected to the external network, the test mobile phone can access the external network normally.

View br0 information:


4. MI_WLAN usage example

4.1. MI_WLAN connects to hotspot in STA mode

An example of a local device connecting to an AP:

1.      static WLAN_HANDLE g_wlanHdl = -1;  
2.  static MI_WLAN_InitParams_t g_stParam = {"/config/wifi/wlan.json"};  
3.  static MI_WLAN_OpenParams_t g_stOpenParam = {E_MI_WLAN_NETWORKTYPE_INFRA};  
4.  static MI_WLAN_ConnectParam_t g_stConnectParam = {  
5.      E_MI_WLAN_SECURITY_WPA,  
6.      "WiFi_test",  
7.      "12345678",  
8.      5000  
9.  };  
10.   
11. static bool g_bConnected = false;  
12. static pthread_t g_ptConn = NULL;  
13. static bool g_bThreadRun = false;  
14.   
15. void *_ConnectWorkThread(void *args)  
16. {  
17.     int nTimeoutCnt = 50;               // timeout 10s  
18.     MI_WLAN_Status_t status;  
19.     MI_WLAN_ConnectParam_t *pstConnParam = (MI_WLAN_ConnectParam_t*)args;  
20.     MI_WLAN_Connect(&g_wlanHdl, pstConnParam);  
21.     printf("current conn info: handle=%d, ssid=%s, passwd=%s\n", g_wlanHdl, (char*)pstConnParam->au8SSId, (char*)pstConnParam->au8Password);  
22.   
23.     while (1)  
24.     {  
25.         if (!g_bThreadRun)  
26.             break;  
27.           
28.         if (nTimeoutCnt-- <= 0)  
29.         {  
30.             printf("connect failed, timeout\n");  
31.             break;  
32.         }  
33.   
34.         static int i = 0;  
35.         MI_WLAN_GetStatus(g_wlanHdl, &status);  
36.         printf("MI_WLAN_GetStatus: count %d\n", i++);  
37.   
38.         if (status.stStaStatus.state == WPA_COMPLETED)  
39.         {  
40.             g_bConnected = true;  
41.             printf("connect success: ssid=%s, ip=%s\n", (char*)status.stStaStatus.ssid, (char*)status.stStaStatus.ip_address);  
42.             break;  
43.         }  
44.   
45.         usleep(100000);  
46.     }  
47.       
48.     return NULL;  
49. }  
50.   
51. int main(int argc, char **argv)  
52. {     
53.     MI_WLAN_ScanResult_t scanResult;  
54.     char ch = 0;  
55.       
56.     // init wlan module  
57.     if (MI_WLAN_Init(&g_stParam))  
58.     {  
59.         printf("Wlan init failed, please ensure wlan module is enabled.\n");  
60.         return -1;  
61.     }  
62.   
63.     if (MI_WLAN_Open(&g_stOpenParam))  
64.     {  
65.         printf("wlan open failed\n");  
66.         break;  
67.     }  
68.       
69.     g_bThreadRun = true;  
70.     pthread_create(&g_ptConn, NULL, _ConnectWorkThread, &g_stConnectParam);  
71.       
72.     while ((ch = getchar()) != 'q')  
73.     {  
74.         if (ch == 's')  
75.         {  
76.             memset(&scanResult, 0, sizeof(MI_WLAN_ScanResult_t));  
77.             MI_WLAN_Scan(NULL, &scanResult);  
78.               
79.             if (scanResult.u8APNumber > 0)  
80.             {  
81.                 printf("Scan result:\n");  
82.                   
83.                 for (int i = 0; i < scanResult.u8APNumber; i++)  
84.                 {  
85.                     char *pSsid = (char*)scanResult.stAPInfo[i].au8SSId;  
86.                     if (pSsid && strcmp(pSsid, "\"\""))  
87.                     {  
88.                         char trimSsid[36];  
89.                         memset(trimSsid, 0, sizeof(trimSsid));  
90.                         strncpy(trimSsid, pSsid+1, strlen(pSsid));  
91.                         printf("SSID: %s\n", trimSsid);  
92.                         printf("MAC: %s\n", (char*)scanResult.stAPInfo[i].au8Mac);  
93.                         printf("encrypt: %s\n", scanResult.stAPInfo[i].bEncryptKey?"true":"false");  
94.                         printf("signalSTR: %d db\n", scanResult.stAPInfo[i].stQuality.signalSTR);  
95.                         printf("frequency: %f GHz\n", scanResult.stAPInfo[i].fFrequency);  
96.                         printf("bitrate: %f Mb/s\n", scanResult.stAPInfo[i].fBitRate);  
97.                         printf("channel: %d\n", scanResult.stAPInfo[i].u8Channel);  
98.                         printf("channel: %d\n", scanResult.stAPInfo[i].u16CellId);  
99.                         printf("\n");  
100.                        }  
101.                    }  
102.                }  
103.            }  
104.        }  
105.          
106.        g_bThreadRun = false;  
107.        pthread_join(g_ptConn, NULL);  
108.          
109.        if (g_bConnected)  
110.        {  
111.            MI_WLAN_Disconnect(g_wlanHdl);  
112.            g_bConnected = false;  
113.        }  
114.          
115.        MI_WLAN_Close(&g_stOpenParam);  
116.        MI_WLAN_DeInit();  
117.          
118.        return 0;  
119.    }

4.2. MI_WLAN enables hotspot in AP mode

Local device as host example:

1.  static WLAN_HANDLE g_apHdl = AP_HANDLE;  
2.  static MI_WLAN_OpenParams_t g_stOpenParam = {E_MI_WLAN_NETWORKTYPE_AP};   
3.  static MI_WLAN_InitParams_t g_stParam = {"/config/wifi/wlan.json"};  
4.  static MI_WLAN_Status_t  g_stStatus;  
5.  static bool g_bConnected = false;  
6.    
7.  int main(int argc, char **argv)  
8.  {  
9.      // init wlan module  
10.     if (MI_WLAN_Init(&g_stParam))  
11.     {  
12.         printf("Wlan init failed, please ensure wlan module is enabled.\n");  
13.         return -1;  
14.     }  
15.   
16.     if (MI_WLAN_Open(&g_stOpenParam))  
17.     {  
18.         printf("wlan open failed\n");  
19.         break;  
20.     }  
21.       
22.     if (MI_SUCCESS == MI_WLAN_Connect(&g_apHdl, NULL))  
23.         g_bConnected = true;  
24.       
25.     while ((ch = getchar()) != 'q')  
26.     {  
27.         if (ch == 's')  
28.         {  
29.             MI_WLAN_GetStatus(AP_HANDLE, &g_stStatus);  
30.             if (g_stStatus.stApStatus.u16HostNum > 0)  
31.             {  
32.                 printf("Access port:\n");  
33.                   
34.                 for (int i = 0; i < g_stStatus.stApStatus.u16HostNum; i++)  
35.                 {  
36.                     printf("HostName: %s\n", g_stStatus.stApStatus.astHosts[i].hostname);  
37.                     printf("IP: %s\n", (char*)g_stStatus.stApStatus.astHosts[i].ipaddr);  
38.                     printf("MAC: %s\n", (char*)g_stStatus.stApStatus.astHosts[i].macaddr);  
39.                     printf("Connected time: %lld\n", g_stStatus.stApStatus.astHosts[i].connectedtime);  
40.                     printf("\n");  
41.                 }  
42.             }  
43.         }  
44.     }  
45.       
46.     if (g_bConnected)  
47.     {  
48.         MI_WLAN_Disconnect(g_apHdl);   
49.         g_bConnected = false;  
50.     }  
51.       
52.     MI_WLAN_Close(&g_stOpenParam);  
53.     MI_WLAN_DeInit();  
54.       
55.     return 0;  
56.       
57. }

5. DEMO Reference

MI_WLAN DemoCode reference:

wifi.tar.gz

wlan.json