• 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 #include "net_params.h"
22 
PrintLinkedInfo(WifiLinkedInfo * info)23 static void PrintLinkedInfo(WifiLinkedInfo* info)
24 {
25     int ret = 0;
26 
27     if (!info) {
28         return;
29     }
30 
31     static char macAddress[32] = {0};
32     unsigned char* mac = info->bssid;
33     if (snprintf_s(macAddress, sizeof(macAddress) + 1, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
34         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) < 0) { /* mac地址从0,1,2,3,4,5位 */
35             return;
36     }
37 }
38 
39 static volatile int g_connected = 0;
40 
OnWifiConnectionChanged(int state,WifiLinkedInfo * info)41 static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
42 {
43     if (!info) return;
44 
45     printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
46     PrintLinkedInfo(info);
47 
48     if (state == WIFI_STATE_AVAILABLE) {
49         g_connected = 1;
50     } else {
51         g_connected = 0;
52     }
53 }
54 
OnWifiScanStateChanged(int state,int size)55 static void OnWifiScanStateChanged(int state, int size)
56 {
57     printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
58 }
59 
60 static WifiEvent g_defaultWifiEventListener = {
61     .OnWifiConnectionChanged = OnWifiConnectionChanged,
62     .OnWifiScanStateChanged = OnWifiScanStateChanged
63 };
64 
65 static struct netif* g_iface = NULL;
66 
67 err_t netifapi_set_hostname(struct netif *netif, char *hostname, u8_t namelen);
68 #define SSID_LEN (2)
69 #define PSK_LEN (9)
ConnectToHotspot(void)70 int ConnectToHotspot(void)
71 {
72     WifiDeviceConfig config = {0};
73 
74     // 准备AP的配置参数
75     strcpy_s(config.ssid, SSID_LEN, PARAM_HOTSPOT_SSID);
76     strcpy_s(config.preSharedKey, PSK_LEN, PARAM_HOTSPOT_PSK);
77     config.securityType = PARAM_HOTSPOT_TYPE;
78 
79     osDelay(10); /* 延时10ms */
80     WifiErrorCode errCode;
81     int netId = -1;
82 
83     errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
84     printf("RegisterWifiEvent: %d\r\n", errCode);
85 
86     errCode = EnableWifi();
87     printf("EnableWifi: %d\r\n", errCode);
88 
89     errCode = AddDeviceConfig(&config, &netId);
90     printf("AddDeviceConfig: %d\r\n", errCode);
91 
92     g_connected = 0;
93     errCode = ConnectTo(netId);
94     printf("ConnectTo(%d): %d\r\n", netId, errCode);
95 
96     while (!g_connected) { // wait until connect to AP
97         osDelay(10); /* 持续10ms去连接AP */
98     }
99     printf("g_connected: %d\r\n", g_connected);
100 
101     g_iface = netifapi_netif_find("wlan0");
102     if (g_iface) {
103         char* hostname = "hispark";
104         err_t ret = netifapi_set_hostname(g_iface, hostname, strlen(hostname));
105         printf("netifapi_set_hostname: %d\r\n", ret);
106 
107         ret = netifapi_dhcp_start(g_iface);
108         printf("netifapi_dhcp_start: %d\r\n", ret);
109 
110         osDelay(100); // wait DHCP server give me IP 100
111 #if 1
112         ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
113         printf("netifapi_netif_common: %d\r\n", ret);
114 #else
115 #endif
116     }
117     return netId;
118 }
119 
DisconnectWithHotspot(int netId)120 void DisconnectWithHotspot(int netId)
121 {
122     if (g_iface) {
123         err_t ret = netifapi_dhcp_stop(g_iface);
124         printf("netifapi_dhcp_stop: %d\r\n", ret);
125     }
126 
127     WifiErrorCode errCode = Disconnect(); // disconnect with your AP
128     printf("Disconnect: %d\r\n", errCode);
129 
130     errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
131     printf("UnRegisterWifiEvent: %d\r\n", errCode);
132 
133     RemoveDevice(netId); // remove AP config
134     printf("RemoveDevice: %d\r\n", errCode);
135 
136     errCode = DisableWifi();
137     printf("DisableWifi: %d\r\n", errCode);
138 }