1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 "cmsis_os2.h"
17 #include "ohos_init.h"
18 #include "wifi_device.h"
19 #include "wifi_error_code.h"
20 #include <stdlib.h>
21 #include <string.h>
22
23 #define SELECT_WIFI_SSID "test_wifi"
24 #define SELECT_WIFI_PASSWORD "12345678"
25 #define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK
26
27 #define SCAN_OPTION 1
28 #if SCAN_OPTION
29 #define SCAN_SUCCESS_FLAGS 1U
30 osEventFlagsId_t eventId;
31 #endif
32 WifiEvent g_wifiEventHandler = {0};
33 WifiErrorCode error;
34
OnWifiScanStateChangedHandler(int state,int size)35 static void OnWifiScanStateChangedHandler(int state, int size)
36 {
37 (void)state;
38 if (state > 0) {
39 #if SCAN_OPTION
40 osEventFlagsSet(eventId, SCAN_SUCCESS_FLAGS);
41 #endif
42 printf("wifi scan result %d\r\n", size);
43 } else {
44 printf("wifi scan failed\r\n");
45 }
46 }
47
OnWifiConnectionChangedHandler(int state,WifiLinkedInfo * info)48 static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
49 {
50 (void)info;
51 if (state > 0) {
52 printf("callback function for wifi connect\r\n");
53 } else {
54 printf("connect error,please check password\r\n");
55 }
56 }
57
OnHotspotStaJoinHandler(StationInfo * info)58 static void OnHotspotStaJoinHandler(StationInfo *info)
59 {
60 (void)info;
61 printf("STA join AP\n");
62 }
63
OnHotspotStaLeaveHandler(StationInfo * info)64 static void OnHotspotStaLeaveHandler(StationInfo *info)
65 {
66 (void)info;
67 printf("HotspotStaLeave:info is null.\n");
68 }
69
OnHotspotStateChangedHandler(int state)70 static void OnHotspotStateChangedHandler(int state)
71 {
72 printf("HotspotStateChanged:state is %d.\n", state);
73 }
74
WiFiInit(void)75 static WifiErrorCode WiFiInit(void)
76 {
77 g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
78 g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
79 g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
80 g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
81 g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
82 return RegisterWifiEvent(&g_wifiEventHandler);
83 }
84
85 #if SCAN_OPTION
WifiScan(uint32_t timeoutMs)86 static int WifiScan(uint32_t timeoutMs)
87 {
88 uint32_t flags = 0;
89 if (Scan() == ERROR_WIFI_IFACE_INVALID)
90 return -1;
91
92 flags = osEventFlagsWait(eventId, SCAN_SUCCESS_FLAGS, osFlagsWaitAny, timeoutMs);
93 return (flags == SCAN_SUCCESS_FLAGS) ? 0 : -1;
94 }
95 #endif
96
WifiSTATask(void)97 static void WifiSTATask(void)
98 {
99 WifiDeviceConfig select_ap_config = {0};
100 strcpy(select_ap_config.ssid, SELECT_WIFI_SSID);
101 strcpy(select_ap_config.preSharedKey, SELECT_WIFI_PASSWORD);
102 select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE;
103 osDelay(2000);
104 printf("<--WifiSTATask Init-->\r\n");
105 if (WiFiInit() != WIFI_SUCCESS) {
106 printf("WiFiInit failed!\r\n");
107 return;
108 }
109 if (EnableWifi() != WIFI_SUCCESS) {
110 printf("EnableWifi failed\r\n");
111 return;
112 }
113 if (IsWifiActive() == WIFI_STA_NOT_ACTIVE) {
114 printf("Wifi station is not activated.\n");
115 return;
116 }
117 #if SCAN_OPTION
118 eventId = osEventFlagsNew(NULL);
119 if (eventId == NULL) {
120 printf("Failed to create EventFlags!\n");
121 return;
122 }
123 if (WifiScan(10000) < 0) {
124 printf("WifiScan failed\r\n");
125 return;
126 }
127 unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
128 WifiScanInfo *info = (WifiScanInfo *)malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
129 if (info == NULL) {
130 printf("malloc failed\r\n");
131 return;
132 }
133 if (GetScanInfoList(info, &size) != WIFI_SUCCESS) {
134 printf("GetScanInfoList failed\r\n");
135 free(info);
136 return;
137 }
138 for (unsigned int i = 0; i < size; i++) {
139 printf("no:%03u, ssid:%-30s, rssi:%5d\r\n", i + 1, info[i].ssid, info[i].rssi);
140 }
141 free(info);
142 #endif
143 int result;
144 if (AddDeviceConfig(&select_ap_config, &result) != WIFI_SUCCESS) {
145 printf("AddDeviceConfig failed!\r\n");
146 return;
147 }
148 printf("Connecting to %s...\r\n", SELECT_WIFI_SSID);
149 error = ConnectTo(result); ///< block and retry
150 printf("WiFi connect %s!\r\n", (error == WIFI_SUCCESS) ? "succeed" : "failed");
151 for (;;) {
152 osDelay(100);
153 }
154 }
155
WifiClientSTA(void)156 static void WifiClientSTA(void)
157 {
158 printf("[%s:%d]: %s\n", __FILE__, __LINE__, __func__);
159
160 osThreadAttr_t attr;
161 attr.name = "WifiSTATask";
162 attr.attr_bits = 0U;
163 attr.cb_mem = NULL;
164 attr.cb_size = 0U;
165 attr.stack_mem = NULL;
166 attr.stack_size = 10240;
167 attr.priority = 24;
168
169 if (osThreadNew((osThreadFunc_t)WifiSTATask, NULL, &attr) == NULL) {
170 printf("Failed to create WifiSTATask!\n");
171 }
172 }
173
174 APP_FEATURE_INIT(WifiClientSTA);
175