iot_wifi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  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. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "lwip/netif.h"
  19. #include "lwip/netifapi.h"
  20. #include "lwip/ip4_addr.h"
  21. #include "lwip/api_shell.h"
  22. #include "cmsis_os2.h"
  23. #include "wifi_device.h"
  24. #include "wifi_error_code.h"
  25. #include "ohos_init.h"
  26. #include "iot_wifi.h"
  27. #define DEF_TIMEOUT 15
  28. #define ONE_SECOND 1
  29. #define SELECT_WIFI_SECURITYTYPE WIFI_SEC_TYPE_PSK
  30. static void WiFiInit(void);
  31. static void WaitSacnResult(void);
  32. static int WaitConnectResult(void);
  33. static void OnWifiScanStateChangedHandler(int state, int size);
  34. static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info);
  35. static void OnHotspotStaJoinHandler(StationInfo *info);
  36. static void OnHotspotStateChangedHandler(int state);
  37. static void OnHotspotStaLeaveHandler(StationInfo *info);
  38. static int g_staScanSuccess = 0;
  39. static int g_ConnectSuccess = 0;
  40. static int ssid_count = 0;
  41. WifiEvent g_wifiEventHandler = {0};
  42. WifiErrorCode error;
  43. #define SELECT_WLAN_PORT "wlan0"
  44. int WifiConnect(const char *ssid, const char *psk)
  45. {
  46. WifiScanInfo *info = NULL;
  47. unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
  48. static struct netif *g_lwip_netif = NULL;
  49. osDelay(200);
  50. printf("<--System Init-->\r\n");
  51. // 初始化WIFI
  52. WiFiInit();
  53. // 使能WIFI
  54. if (EnableWifi() != WIFI_SUCCESS) {
  55. printf("EnableWifi failed, error = %d\r\n", error);
  56. return -1;
  57. }
  58. // 判断WIFI是否激活
  59. if (IsWifiActive() == 0)
  60. {
  61. printf("Wifi station is not actived.\r\n");
  62. return -1;
  63. }
  64. // 分配空间,保存WiFi信息
  65. info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
  66. if (info == NULL) {
  67. return -1;
  68. }
  69. // 轮询查找WiFi列表
  70. do {
  71. // 重置标志位
  72. ssid_count = 0;
  73. g_staScanSuccess = 0;
  74. // 开始扫描
  75. Scan();
  76. // 等待扫描结果
  77. WaitSacnResult();
  78. // 获取扫描列表
  79. error = GetScanInfoList(info, &size);
  80. } while (g_staScanSuccess != 1);
  81. for (uint8_t i = 0; i < ssid_count; i++) {
  82. printf("no:%03d, ssid:%-30s, rssi:%5d\r\n", i+1, info[i].ssid, info[i].rssi/100);
  83. }
  84. // 连接指定的WiFi热点
  85. for (uint8_t i = 0; i < ssid_count; i++) {
  86. if (strcmp(ssid, info[i].ssid) == 0) {
  87. int result;
  88. printf("Select:%3d wireless, Waiting...\r\n", i+1);
  89. // 拷贝要连接的热点信息
  90. WifiDeviceConfig select_ap_config = {0};
  91. strcpy(select_ap_config.ssid, info[i].ssid);
  92. strcpy(select_ap_config.preSharedKey, psk);
  93. select_ap_config.securityType = SELECT_WIFI_SECURITYTYPE;
  94. if (AddDeviceConfig(&select_ap_config, &result) == WIFI_SUCCESS) {
  95. if (ConnectTo(result) == WIFI_SUCCESS && WaitConnectResult() == 1) {
  96. printf("WiFi connect succeed!\r\n");
  97. g_lwip_netif = netifapi_netif_find(SELECT_WLAN_PORT);
  98. break;
  99. }
  100. }
  101. }
  102. if (i == ssid_count - 1) {
  103. printf("ERROR: No wifi as expected\r\n");
  104. while(1) osDelay(100);
  105. }
  106. }
  107. // 启动DHCP
  108. if (g_lwip_netif) {
  109. dhcp_start(g_lwip_netif);
  110. printf("begain to dhcp\r\n");
  111. }
  112. // 等待DHCP
  113. for(;;) {
  114. if(dhcp_is_bound(g_lwip_netif) == ERR_OK) {
  115. printf("<-- DHCP state:OK -->\r\n");
  116. // 打印获取到的IP信息
  117. netifapi_netif_common(g_lwip_netif, dhcp_clients_info_show, NULL);
  118. break;
  119. }
  120. printf("<-- DHCP state:Inprogress -->\r\n");
  121. osDelay(100);
  122. }
  123. osDelay(100);
  124. return 0;
  125. }
  126. /****************************************************
  127. * 初始化WIFI
  128. ****************************************************/
  129. static void WiFiInit(void)
  130. {
  131. printf("<--Wifi Init-->\r\n");
  132. g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
  133. g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
  134. g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
  135. g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
  136. g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
  137. error = RegisterWifiEvent(&g_wifiEventHandler);
  138. if (error != WIFI_SUCCESS) {
  139. printf("register wifi event fail!\r\n");
  140. } else {
  141. printf("register wifi event succeed!\r\n");
  142. }
  143. }
  144. static void OnWifiScanStateChangedHandler(int state, int size)
  145. {
  146. if (size > 0) {
  147. ssid_count = size;
  148. g_staScanSuccess = 1;
  149. }
  150. printf("callback function for wifi scan:%d, %d\r\n", state, size);
  151. return;
  152. }
  153. static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo *info)
  154. {
  155. if (info == NULL) {
  156. printf("WifiConnectionChanged:info is null, stat is %d.\n", state);
  157. } else {
  158. if (state == WIFI_STATE_AVALIABLE) {
  159. g_ConnectSuccess = 1;
  160. } else {
  161. g_ConnectSuccess = 0;
  162. }
  163. }
  164. }
  165. static void OnHotspotStaJoinHandler(StationInfo *info)
  166. {
  167. (void)info;
  168. printf("STA join AP\n");
  169. return;
  170. }
  171. static void OnHotspotStaLeaveHandler(StationInfo *info)
  172. {
  173. (void)info;
  174. printf("HotspotStaLeave:info is null.\n");
  175. return;
  176. }
  177. static void OnHotspotStateChangedHandler(int state)
  178. {
  179. printf("HotspotStateChanged:state is %d.\n", state);
  180. return;
  181. }
  182. static void WaitSacnResult(void)
  183. {
  184. int scanTimeout = DEF_TIMEOUT;
  185. while (scanTimeout > 0) {
  186. sleep(ONE_SECOND);
  187. scanTimeout--;
  188. if (g_staScanSuccess == 1) {
  189. printf("WaitSacnResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout));
  190. break;
  191. }
  192. }
  193. if (scanTimeout <= 0) {
  194. printf("WaitSacnResult:timeout!\n");
  195. }
  196. }
  197. static int WaitConnectResult(void)
  198. {
  199. int ConnectTimeout = DEF_TIMEOUT;
  200. while (ConnectTimeout > 0) {
  201. sleep(ONE_SECOND);
  202. ConnectTimeout--;
  203. if (g_ConnectSuccess == 1) {
  204. printf("WaitConnectResult:wait success[%d]s\n", (DEF_TIMEOUT - ConnectTimeout));
  205. break;
  206. }
  207. }
  208. if (ConnectTimeout <= 0) {
  209. printf("WaitConnectResult:timeout!\n");
  210. return 0;
  211. }
  212. return 1;
  213. }
  214. int BOARD_InitWifi(void)
  215. {
  216. return 0;
  217. }
  218. int BOARD_ConnectWifi(const char *wifiSSID, const char *wifiPWD)
  219. {
  220. return WifiConnect(wifiSSID, wifiPWD);
  221. }
  222. int BOARD_DisconnectWifi()
  223. {
  224. Disconnect();
  225. return 0;
  226. }