• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
104     osDelay(2000);
105     printf("<--WifiSTATask Init-->\r\n");
106 
107     if (WiFiInit() != WIFI_SUCCESS) {
108         printf("WiFiInit failed!\r\n");
109         return;
110     }
111 
112     if (EnableWifi() != WIFI_SUCCESS) {
113         printf("EnableWifi failed\r\n");
114         return;
115     }
116 
117     if (IsWifiActive() == WIFI_STA_NOT_ACTIVE) {
118         printf("Wifi station is not activated.\n");
119         return;
120     }
121 
122 #if SCAN_OPTION
123     eventId = osEventFlagsNew(NULL);
124     if (eventId == NULL) {
125         printf("Failed to create EventFlags!\n");
126         return;
127     }
128     if (WifiScan(10000) < 0) {
129         printf("WifiScan failed\r\n");
130         return;
131     }
132     unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
133     WifiScanInfo *info = (WifiScanInfo *)malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
134     if (info == NULL) {
135         printf("malloc failed\r\n");
136         return;
137     }
138     if (GetScanInfoList(info, &size) != WIFI_SUCCESS) ///< size = scan_result_len
139     {
140         printf("GetScanInfoList failed\r\n");
141         free(info);
142         return;
143     }
144     printf("********************\r\n");
145     for (unsigned int i = 0; i < size; i++) {
146         printf("no:%03u, ssid:%-30s, rssi:%5d\r\n", i + 1, info[i].ssid, info[i].rssi);
147     }
148     printf("********************\r\n");
149     free(info);
150 #endif
151 
152     int result;
153     if (AddDeviceConfig(&select_ap_config, &result) != WIFI_SUCCESS) {
154         printf("AddDeviceConfig failed!\r\n");
155         return;
156     }
157     printf("Connecting to %s...\r\n", SELECT_WIFI_SSID);
158     error = ConnectTo(result); ///< block and retry
159     printf("WiFi connect %s!\r\n", (error == WIFI_SUCCESS) ? "succeed" : "failed");
160 
161     for (;;) {
162         osDelay(100);
163     }
164 }
165 
WifiClientSTA(void)166 static void WifiClientSTA(void)
167 {
168     printf("[%s:%d]: %s\n", __FILE__, __LINE__, __func__);
169 
170     osThreadAttr_t attr;
171     attr.name = "WifiSTATask";
172     attr.attr_bits = 0U;
173     attr.cb_mem = NULL;
174     attr.cb_size = 0U;
175     attr.stack_mem = NULL;
176     attr.stack_size = 10240;
177     attr.priority = 24;
178 
179     if (osThreadNew((osThreadFunc_t)WifiSTATask, NULL, &attr) == NULL) {
180         printf("Failed to create WifiSTATask!\n");
181     }
182 }
183 
184 APP_FEATURE_INIT(WifiClientSTA);
185