• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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 
71 static struct netif* g_iface = NULL;
72 
ConnectToHotspot(WifiDeviceConfig * apConfig)73 int ConnectToHotspot(WifiDeviceConfig* apConfig)
74 {
75     WifiErrorCode errCode;
76     int netId = -1;
77 
78     errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
79     printf("RegisterWifiEvent: %d\r\n", errCode);
80 
81     errCode = EnableWifi();
82     printf("EnableWifi: %d\r\n", errCode);
83 
84     errCode = AddDeviceConfig(apConfig, &netId);
85     printf("AddDeviceConfig: %d\r\n", errCode);
86 
87     g_connected = 0;
88     errCode = ConnectTo(netId);
89     printf("ConnectTo(%d): %d\r\n", netId, errCode);
90 
91     while (!g_connected) { // wait until connect to AP
92         osDelay(TEN);
93     }
94     printf("g_connected: %d\r\n", g_connected);
95 
96     g_iface = netifapi_netif_find("wlan0");
97     if (g_iface) {
98         err_t ret = netifapi_dhcp_start(g_iface);
99         printf("netifapi_dhcp_start: %d\r\n", ret);
100 
101         osDelay(ONE_HUNDRED); // wait DHCP server give me IP
102         ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
103         printf("netifapi_netif_common: %d\r\n", ret);
104     }
105     return netId;
106 }
107 
DisconnectWithHotspot(int netId)108 void DisconnectWithHotspot(int netId)
109 {
110     if (g_iface) {
111         err_t ret = netifapi_dhcp_stop(g_iface);
112         printf("netifapi_dhcp_stop: %d\r\n", ret);
113     }
114 
115     WifiErrorCode errCode = Disconnect(); // disconnect with your AP
116     printf("Disconnect: %d\r\n", errCode);
117 
118     errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
119     printf("UnRegisterWifiEvent: %d\r\n", errCode);
120 
121     RemoveDevice(netId); // remove AP config
122     printf("RemoveDevice: %d\r\n", errCode);
123 
124     errCode = DisableWifi();
125     printf("DisableWifi: %d\r\n", errCode);
126 }
127 
128