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 #include "wifi_device.h"
17 #include "cmsis_os2.h"
18
19 #include "lwip/netifapi.h"
20 #include "lwip/api_shell.h"
21 #include "lwip/sockets.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 printf("OK");
40 }
41 printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\r\n",
42 macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
43 }
44
45 static volatile int g_connected = 0;
46
OnWifiConnectionChanged(int state,WifiLinkedInfo * info)47 static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
48 {
49 if (!info) return;
50
51 printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
52 PrintLinkedInfo(info);
53
54 if (state == WIFI_STATE_AVAILABLE) {
55 g_connected = 1;
56 } else {
57 g_connected = 0;
58 }
59 }
60
OnWifiScanStateChanged(int state,int size)61 static void OnWifiScanStateChanged(int state, int size)
62 {
63 printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
64 }
65
66 static WifiEvent g_defaultWifiEventListener = {
67 .OnWifiConnectionChanged = OnWifiConnectionChanged,
68 .OnWifiScanStateChanged = OnWifiScanStateChanged
69 };
70
ConnectToHotspot(WifiDeviceConfig * apConfig)71 int ConnectToHotspot(WifiDeviceConfig* apConfig)
72 {
73 WifiErrorCode errCode;
74 int netId = -1;
75
76 errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
77 printf("RegisterWifiEvent: %d\r\n", errCode);
78
79 errCode = EnableWifi();
80 printf("EnableWifi: %d\r\n", errCode);
81
82 errCode = AddDeviceConfig(apConfig, &netId);
83 printf("AddDeviceConfig: %d\r\n", errCode);
84
85 g_connected = 0;
86 errCode = ConnectTo(netId);
87 printf("ConnectTo(%d): %d\r\n", netId, errCode);
88
89 while (!g_connected) { // wait until connect to AP
90 osDelay(TEN);
91 }
92 printf("g_connected: %d\r\n", g_connected);
93
94 struct netif* iface = netifapi_netif_find("wlan0");
95 if (iface) {
96 err_t ret = netifapi_dhcp_start(iface);
97 printf("netifapi_dhcp_start: %d\r\n", ret);
98
99 osDelay(ONE_HUNDRED); // wait DHCP server give me IP
100 ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL);
101 printf("netifapi_netif_common: %d\r\n", ret);
102 }
103 return netId;
104 }
105
DisconnectWithHotspot(int netId)106 void DisconnectWithHotspot(int netId)
107 {
108 WifiErrorCode errCode;
109 errCode = Disconnect(); // disconnect with your AP
110 printf("Disconnect: %d\r\n", errCode);
111
112 errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
113 printf("UnRegisterWifiEvent: %d\r\n", errCode);
114
115 RemoveDevice(netId); // remove AP config
116 printf("RemoveDevice: %d\r\n", errCode);
117
118 errCode = DisableWifi();
119 printf("DisableWifi: %d\r\n", errCode);
120 }
121
122