1 /*
2 * Copyright (C) 2022 HiHope Open Source Organization .
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 *
14 * limitations under the License.
15 */
16
17 #include "wifi_device.h"
18 #include "cmsis_os2.h"
19
20 #include "lwip/netifapi.h"
21 #include "lwip/api_shell.h"
22 #define ZERO 0
23 #define ONE 1
24 #define TWO 2
25 #define THREE 3
26 #define FOUR 4
27 #define FIVE 5
28 #define TEN 10
29 #define ONE_HUNDRED 100
30
PrintLinkedInfo(WifiLinkedInfo * info)31 static void PrintLinkedInfo(WifiLinkedInfo* info)
32 {
33 if (!info) return;
34
35 static char macAddress[32] = {0};
36 unsigned char* mac = info->bssid;
37 if (snprintf_s(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
38 mac[ZERO], mac[ONE], mac[TWO], mac[THREE], mac[FOUR], mac[FIVE]) == TRUE) {
39 }
40 printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\r\n",
41 macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
42 }
43
44 static volatile int g_connected = 0;
45
OnWifiConnectionChanged(int state,WifiLinkedInfo * info)46 static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
47 {
48 if (!info) return;
49
50 printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
51 PrintLinkedInfo(info);
52
53 if (state == WIFI_STATE_AVAILABLE) {
54 g_connected = 1;
55 } else {
56 g_connected = 0;
57 }
58 }
59
OnWifiScanStateChanged(int state,int size)60 static void OnWifiScanStateChanged(int state, int size)
61 {
62 printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
63 }
64
65 static WifiEvent g_defaultWifiEventListener = {
66 .OnWifiConnectionChanged = OnWifiConnectionChanged,
67 .OnWifiScanStateChanged = OnWifiScanStateChanged
68 };
69
70 static struct netif* g_iface = NULL;
71
72 err_t netifapi_set_hostname(struct netif *netif, char *hostname, u8_t namelen);
73
ConnectToHotspot(WifiDeviceConfig * apConfig)74 int ConnectToHotspot(WifiDeviceConfig* apConfig)
75 {
76 WifiErrorCode errCode;
77 int netId = -1;
78
79 errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
80 printf("RegisterWifiEvent: %d\r\n", errCode);
81
82 errCode = EnableWifi();
83 printf("EnableWifi: %d\r\n", errCode);
84
85 errCode = AddDeviceConfig(apConfig, &netId);
86 printf("AddDeviceConfig: %d\r\n", errCode);
87
88 g_connected = 0;
89 errCode = ConnectTo(netId);
90 printf("ConnectTo(%d): %d\r\n", netId, errCode);
91
92 while (!g_connected) { // wait until connect to AP
93 osDelay(TEN);
94 }
95 printf("g_connected: %d\r\n", g_connected);
96
97 g_iface = netifapi_netif_find("wlan0");
98 if (g_iface) {
99 err_t ret = 0;
100 char* hostname = "hispark";
101 ret = netifapi_set_hostname(g_iface, hostname, strlen(hostname));
102 printf("netifapi_set_hostname: %d\r\n", ret);
103
104 ret = netifapi_dhcp_start(g_iface);
105 printf("netifapi_dhcp_start: %d\r\n", ret);
106
107 osDelay(ONE_HUNDRED); // wait DHCP server give me IP
108
109 ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
110 printf("netifapi_netif_common: %d\r\n", ret);
111
112 // 下面这种方式也可以打印 IP、网关、子网掩码信息
113 ip4_addr_t ip = {0};
114 ip4_addr_t netmask = {0};
115 ip4_addr_t gw = {0};
116 ret = netifapi_netif_get_addr(g_iface, &ip, &netmask, &gw);
117 if (ret == ERR_OK) {
118 printf("ip = %s\r\n", ip4addr_ntoa(&ip));
119 printf("netmask = %s\r\n", ip4addr_ntoa(&netmask));
120 printf("gw = %s\r\n", ip4addr_ntoa(&gw));
121 }
122 printf("netifapi_netif_get_addr: %d\r\n", ret);
123 }
124 return netId;
125 }
126
DisconnectWithHotspot(int netId)127 void DisconnectWithHotspot(int netId)
128 {
129 if (g_iface) {
130 err_t ret = netifapi_dhcp_stop(g_iface);
131 printf("netifapi_dhcp_stop: %d\r\n", ret);
132 }
133
134 WifiErrorCode errCode = Disconnect(); // disconnect with your AP
135 printf("Disconnect: %d\r\n", errCode);
136
137 errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
138 printf("UnRegisterWifiEvent: %d\r\n", errCode);
139
140 RemoveDevice(netId); // remove AP config
141 printf("RemoveDevice: %d\r\n", errCode);
142
143 errCode = DisableWifi();
144 printf("DisableWifi: %d\r\n", errCode);
145 }
146
147