• 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 <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 
21 #include "ohos_init.h"
22 #include "cmsis_os2.h"
23 #include "wifi_device.h"
24 
25 #include "lwip/netifapi.h"
26 #include "lwip/api_shell.h"
27 
28 #define ATTR.STACK_SIZE 10240
29 #define ZERO 0
30 #define ONE 1
31 #define TWO 2
32 #define THREE 3
33 #define FOUR 4
34 #define FIVE 5
35 #define TEN 10
36 #define FIFTY 50
37 #define ONE_HUNDRED 100
38 #define TWO_HUNDRED 200
39 
40 // 注释:MAC
PrintLinkedInfo(WifiLinkedInfo * info)41 static void PrintLinkedInfo(WifiLinkedInfo* info)
42 {
43     if (!info) return;
44 
45     static char macAddress[32] = {0};
46     unsigned char* mac = info->bssid;
47     if (snprintf_s(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
48         mac[ZERO], mac[ONE], mac[TWO], mac[THREE], mac[FOUR], mac[FIVE]) == TRUE) {
49     printf("OK");
50 }
51     printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\r\n",
52         macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
53 }
54 
55 static int g_connected = 0;
OnWifiConnectionChanged(int state,WifiLinkedInfo * info)56 static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
57 {
58     if (!info) return;
59 
60     printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
61     PrintLinkedInfo(info);
62 
63     if (state == WIFI_STATE_AVAILABLE) {
64         g_connected = 1;
65     } else {
66         g_connected = 0;
67     }
68 }
69 
OnWifiScanStateChanged(int state,int size)70 static void OnWifiScanStateChanged(int state, int size)
71 {
72     printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
73 }
74 
WifiConnectTask(int * arg)75 static void WifiConnectTask(int *arg)
76 {
77     (void)arg;
78     WifiErrorCode errCode;
79     WifiEvent eventListener = {
80         .OnWifiConnectionChanged = OnWifiConnectionChanged, .OnWifiScanStateChanged = OnWifiScanStateChanged
81     };
82     WifiDeviceConfig apConfig = {};
83     int netId = -1;
84 
85     osDelay(TEN);
86     // 注册事件监听
87     errCode = RegisterWifiEvent(&eventListener);
88     printf("RegisterWifiEvent: %d\r\n", errCode);
89 
90     // setup your AP params
91     ssid = strcpy_s(apConfig.ssid, sizeof(apConfig.ssid), "ABCD");
92     if (ssid == TRUE) {
93 }
94     key = strcpy_s(apConfig.preSharedKey, sizeof(apConfig.preSharedKey), "12345678");
95     if (key == TRUE) {
96 }
97     apConfig.securityType = WIFI_SEC_TYPE_PSK;
98 
99     while (1) {
100         errCode = EnableWifi();
101         osDelay(TEN);
102         // 添加热点配置,成功会通过result传出netld
103         errCode = AddDeviceConfig(&apConfig, &netId);
104         printf("AddDeviceConfig: %d\r\n", errCode);
105 
106         g_connected = 0;
107         errCode = ConnectTo(netId);
108 
109         while (!g_connected) {
110             osDelay(TEN);
111         }
112         printf("g_connected: %d\r\n", g_connected);
113         osDelay(FIFTY);
114 
115         // 联网业务开始
116         struct netif* iface = netifapi_netif_find("wlan0");
117         if (iface) {
118             err_t ret = netifapi_dhcp_start(iface);
119             printf("netifapi_dhcp_start: %d\r\n", ret);
120 
121             osDelay(TWO_HUNDRED); // wait DHCP server give me IP
122             ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL);
123             printf("netifapi_netif_common: %d\r\n", ret);
124         }
125 
126         // 模拟一段时间的联网业务
127         int timeout = 60;
128         while (timeout--) {
129             osDelay(ONE_HUNDRED);
130             printf("after %d seconds, I'll disconnect WiFi!\n", timeout);
131         }
132 
133         // 联网业务结束
134         err_t ret = netifapi_dhcp_stop(iface);
135         printf("netifapi_dhcp_stop: %d\r\n", ret);
136 
137         Disconnect(); // disconnect with your AP
138 
139         RemoveDevice(netId); // remove AP config
140 
141         errCode = DisableWifi();
142         printf("DisableWifi: %d\r\n", errCode);
143         osDelay(TWO_HUNDRED);
144     }
145 }
146 
WifiConnectDemo(void)147 static void WifiConnectDemo(void)
148 {
149     osThreadAttr_t attr;
150     attr.name = "WifiConnectTask";
151     attr.attr_bits = 0U;
152     attr.cb_mem = NULL;
153     attr.cb_size = 0U;
154     attr.stack_mem = NULL;
155     attr.stack_size = ATTR.STACK_SIZE;
156     attr.priority = osPriorityNormal;
157 
158     if (osThreadNew(WifiConnectTask, NULL, &attr) == NULL) {
159         printf("[WifiConnectDemo] Failed to create WifiConnectTask!\n");
160     }
161 }
162 
163 APP_FEATURE_INIT(WifiConnectDemo);
164