• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "wifi_device.h"
17 #include "cmsis_os2.h"
18 
19 #include "lwip/netifapi.h"
20 #include "lwip/api_shell.h"
21 
22 #define OS_SLEEP_10MS (10)
23 #define OS_SLEEP_100MS (100)
24 
PrintLinkedInfo(const WifiLinkedInfo * info)25 static void PrintLinkedInfo(const WifiLinkedInfo* info)
26 {
27     if (!info) return;
28 
29     static char macAddress[32] = {0};
30     unsigned char* mac = info->bssid;
31     if (snprintf_s(macAddress, sizeof(macAddress) + 1, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
32         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) < 0) { /* mac地址从0,1,2,3,4,5位 */
33             return;
34     }
35 }
36 
37 static volatile int g_connected = 0;
38 
OnWifiConnectionChanged(int state,const WifiLinkedInfo * info)39 static void OnWifiConnectionChanged(int state, const WifiLinkedInfo* info)
40 {
41     if (!info) return;
42 
43     printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
44     PrintLinkedInfo(info);
45 
46     if (state == WIFI_STATE_AVAILABLE) {
47         g_connected = 1;
48     } else {
49         g_connected = 0;
50     }
51 }
52 
OnWifiScanStateChanged(int state,int size)53 static void OnWifiScanStateChanged(int state, int size)
54 {
55     printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
56 }
57 
58 static WifiEvent g_defaultWifiEventListener = {
59     .OnWifiConnectionChanged = OnWifiConnectionChanged,
60     .OnWifiScanStateChanged = OnWifiScanStateChanged
61 };
62 
63 static struct netif* g_iface = NULL;
64 
65 err_t netifapi_set_hostname(struct netif *netif, char *hostname, u8_t namelen);
66 
ConnectToHotspot(const WifiDeviceConfig * apConfig)67 int ConnectToHotspot(const WifiDeviceConfig*  apConfig)
68 {
69     WifiErrorCode errCode;
70     int netId = -1;
71 
72     errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
73     printf("RegisterWifiEvent: %d\r\n", errCode);
74 
75     errCode = EnableWifi();
76     printf("EnableWifi: %d\r\n", errCode);
77 
78     errCode = AddDeviceConfig(apConfig, &netId);
79     printf("AddDeviceConfig: %d\r\n", errCode);
80 
81     g_connected = 0;
82     errCode = ConnectTo(netId);
83     printf("ConnectTo(%d): %d\r\n", netId, errCode);
84 
85     while (!g_connected) { // wait until connect to AP
86         osDelay(OS_SLEEP_10MS);
87     }
88     printf("g_connected: %d\r\n", g_connected);
89 
90     g_iface = netifapi_netif_find("wlan0");
91     if (g_iface) {
92         err_t ret;
93         char* hostname = "hispark";
94         ret = netifapi_set_hostname(g_iface, hostname, strlen(hostname));
95         printf("netifapi_set_hostname: %d\r\n", ret);
96 
97         ret = netifapi_dhcp_start(g_iface);
98         printf("netifapi_dhcp_start: %d\r\n", ret);
99 
100         osDelay(OS_SLEEP_100MS); // wait DHCP server give me IP
101 #if 1
102         ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
103         printf("netifapi_netif_common: %d\r\n", ret);
104 #else
105         // 下面这种方式也可以打印 IP、网关、子网掩码信息
106         ip4_addr_t ip = {0};
107         ip4_addr_t netmask = {0};
108         ip4_addr_t gw = {0};
109         ret = netifapi_netif_get_addr(g_iface, &ip, &netmask, &gw);
110         if (ret == ERR_OK) {
111             printf("ip = %s\r\n", ip4addr_ntoa(&ip));
112             printf("netmask = %s\r\n", ip4addr_ntoa(&netmask));
113             printf("gw = %s\r\n", ip4addr_ntoa(&gw));
114         }
115         printf("netifapi_netif_get_addr: %d\r\n", ret);
116 #endif
117     }
118     return netId;
119 }
120 
DisconnectWithHotspot(int netId)121 void DisconnectWithHotspot(int netId)
122 {
123     if (g_iface) {
124         err_t ret = netifapi_dhcp_stop(g_iface);
125         printf("netifapi_dhcp_stop: %d\r\n", ret);
126     }
127 
128     WifiErrorCode errCode = Disconnect(); // disconnect with your AP
129     printf("Disconnect: %d\r\n", errCode);
130 
131     errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
132     printf("UnRegisterWifiEvent: %d\r\n", errCode);
133 
134     RemoveDevice(netId); // remove AP config
135     printf("RemoveDevice: %d\r\n", errCode);
136 
137     errCode = DisableWifi();
138     printf("DisableWifi: %d\r\n", errCode);
139 }
140 #define OS_SLEEP_1000MS (1000)
WifiStaReadyWait(void)141 void WifiStaReadyWait(void)
142 {
143     ip4_addr_t ipAddr;
144     ip4_addr_t ipAny;
145     IP4_ADDR(&ipAny, 0, 0, 0, 0);
146     IP4_ADDR(&ipAddr, 0, 0, 0, 0);
147     ConnectToHotspot();
148 
149     while (memcmp(&ipAddr, &ipAny, sizeof(ip4_addr_t)) == 0) {
150         IOT_LOG_DEBUG("Wait the DHCP READY");
151         hi_sleep(OS_SLEEP_1000MS);
152         netifapi_netif_get_addr(gLwipNetif, &ipAddr, NULL, NULL);
153     }
154     wifi_first_connecting = WIFI_CONNECT_STATUS;
155     printf("wifi first connecting status = %d\r\n", wifi_first_connecting);
156     IOT_LOG_DEBUG("wifi sta dhcp done");
157 
158     return;
159 }
160