drv_wlan.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-7-10 tyx the first version
  9. */
  10. #include "wifi_structures.h"
  11. #include "wifi_constants.h"
  12. #include <wifi/wifi_util.h>
  13. #include <wifi/wifi_conf.h>
  14. #include "board.h"
  15. #include "drv_wlan.h"
  16. // #define SCAN_WAIT_TIME (10000)
  17. struct scan_user_data
  18. {
  19. struct rt_completion done;
  20. scan_callback_fn fun;
  21. void *data;
  22. };
  23. extern rthw_mode_t wifi_mode;
  24. rthw_mode_t rthw_wifi_mode_get(void)
  25. {
  26. return wifi_mode;
  27. }
  28. int rthw_wifi_stop(void)
  29. {
  30. return wifi_off();
  31. }
  32. int rthw_wifi_start(rthw_mode_t mode)
  33. {
  34. if(wifi_on(mode) < 0)
  35. {
  36. rt_kprintf("ERROR: wifi_on failed\n");
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. int rthw_wifi_connect(char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type)
  42. {
  43. int mode;
  44. rtw_wifi_setting_t setting;
  45. mode = rthw_wifi_mode_get();
  46. if ((mode != RTHW_MODE_STA) && (mode != RTHW_MODE_STA_AP))
  47. {
  48. return -1;
  49. }
  50. if(wext_get_mode(WLAN0_NAME, &mode) < 0)
  51. {
  52. rt_kprintf("L:%d wifi get mode err\n", __LINE__);
  53. return -1;
  54. }
  55. if(wifi_connect(ssid, security_type, password, ssid_len, pass_len, -1, NULL) != RTW_SUCCESS)
  56. {
  57. rt_kprintf("wifi connect fail\n");
  58. return -1;
  59. }
  60. rt_kprintf("wifi connect success\n");
  61. rt_kprintf("Show Wi-Fi information\n");
  62. wifi_get_setting(WLAN0_NAME,&setting);
  63. wifi_show_setting(WLAN0_NAME,&setting);
  64. return 0;
  65. }
  66. int rthw_wifi_connect_bssid(char *bssid, char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type)
  67. {
  68. int mode;
  69. rtw_wifi_setting_t setting;
  70. mode = rthw_wifi_mode_get();
  71. if ((mode != RTHW_MODE_STA) && (mode != RTHW_MODE_STA_AP))
  72. {
  73. return -1;
  74. }
  75. if(wext_get_mode(WLAN0_NAME, &mode) < 0)
  76. {
  77. rt_kprintf("L:%d wifi get mode err\n", __LINE__);
  78. return -1;
  79. }
  80. if(wifi_connect_bssid(bssid, ssid, security_type, password, 6, ssid_len, pass_len, -1, NULL) != RTW_SUCCESS)
  81. {
  82. rt_kprintf("wifi connect fail\n");
  83. return -1;
  84. }
  85. rt_kprintf("wifi connect success\n");
  86. rt_kprintf("Show Wi-Fi information\n");
  87. wifi_get_setting(WLAN0_NAME,&setting);
  88. wifi_show_setting(WLAN0_NAME,&setting);
  89. return 0;
  90. }
  91. int rthw_wifi_ap_start(char *ssid, char *password, int channel)
  92. {
  93. int mode = 0, timeout = 20;
  94. rtw_security_t security_type = RTW_SECURITY_WPA2_AES_PSK;
  95. char *name = RT_NULL;
  96. mode = rthw_wifi_mode_get();
  97. if (mode == RTHW_MODE_AP)
  98. {
  99. name = WLAN0_NAME;
  100. }
  101. else if (mode == RTHW_MODE_STA_AP)
  102. {
  103. name = WLAN1_NAME;
  104. }
  105. else
  106. {
  107. return -1;
  108. }
  109. if(wext_get_mode(name, &mode) < 0)
  110. {
  111. rt_kprintf("L:%d wifi get mode err\n", __LINE__);
  112. return -1;
  113. }
  114. if (password == RT_NULL)
  115. {
  116. security_type = RTW_SECURITY_OPEN;
  117. }
  118. if(wifi_start_ap(ssid, security_type, password, rt_strlen(ssid), rt_strlen(password), 6) != 0)
  119. {
  120. rt_kprintf("ERROR: wifi_start_ap failed\n");
  121. return -1;
  122. }
  123. while(1)
  124. {
  125. char essid[33];
  126. if(wext_get_ssid(name, (unsigned char *) essid) > 0)
  127. {
  128. if(strcmp((const char *) essid, (const char *)ssid) == 0)
  129. {
  130. rt_kprintf("%s started\n", ssid);
  131. break;
  132. }
  133. }
  134. if(timeout == 0)
  135. {
  136. rt_kprintf("Start AP timeout\n");
  137. return -1;
  138. }
  139. rt_thread_delay(1 * RT_TICK_PER_SECOND);
  140. timeout --;
  141. }
  142. return 0;
  143. }
  144. static int rthw_wifi_disconnect(char *name)
  145. {
  146. char essid[33];
  147. int timeout = 20;
  148. const rt_uint8_t null_bssid[ETH_ALEN + 2] = {0, 0, 0, 0, 0, 1, 0, 0};
  149. if (name == RT_NULL)
  150. return -1;
  151. if (wext_get_ssid(name, (unsigned char *) essid) < 0)
  152. {
  153. rt_kprintf("\nWIFI disconnected!\n");
  154. return -1;
  155. }
  156. if (wext_set_bssid(name, null_bssid) < 0)
  157. {
  158. rt_kprintf("Failed to set bogus BSSID to disconnect");
  159. return -1;
  160. }
  161. while (1)
  162. {
  163. if(wext_get_ssid(name, (unsigned char *) essid) < 0)
  164. {
  165. rt_kprintf("WIFI disconnected!\n");
  166. break;
  167. }
  168. if(timeout == 0)
  169. {
  170. rt_kprintf("ERROR: Deassoc timeout!\n");
  171. return -1;
  172. }
  173. rt_thread_delay(10);
  174. timeout --;
  175. }
  176. return 0;
  177. }
  178. int rthw_wifi_sta_disconnect(void)
  179. {
  180. int mode = 0;
  181. char *name = RT_NULL;
  182. mode = rthw_wifi_mode_get();
  183. if (mode == RTHW_MODE_STA)
  184. {
  185. name = WLAN0_NAME;
  186. }
  187. else if (mode == RTHW_MODE_STA_AP)
  188. {
  189. name = WLAN0_NAME;
  190. }
  191. else
  192. {
  193. return -1;
  194. }
  195. return rthw_wifi_disconnect(name);
  196. }
  197. int rthw_wifi_ap_disconnect(void)
  198. {
  199. int mode = 0;
  200. char *name = RT_NULL;
  201. mode = rthw_wifi_mode_get();
  202. if (mode == RTHW_MODE_AP)
  203. {
  204. name = WLAN0_NAME;
  205. }
  206. else if (mode == RTHW_MODE_STA_AP)
  207. {
  208. name = WLAN1_NAME;
  209. }
  210. else
  211. {
  212. return -1;
  213. }
  214. return rthw_wifi_disconnect(name);
  215. }
  216. int rthw_wifi_rssi_get(void)
  217. {
  218. int rssi = 0;
  219. wifi_get_rssi(&rssi);
  220. return rssi;
  221. }
  222. void rthw_wifi_channel_set(int channel)
  223. {
  224. wifi_set_channel(channel);
  225. }
  226. int rthw_wifi_channel_get(void)
  227. {
  228. int channel;
  229. wifi_get_channel(&channel);
  230. return channel;
  231. }
  232. static rtw_result_t rthw_wifi_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result)
  233. {
  234. struct scan_user_data* user_data = malloced_scan_result->user_data;
  235. struct rthw_wlan_info info = { 0 };
  236. if (malloced_scan_result->scan_complete != RTW_TRUE)
  237. {
  238. rtw_scan_result_t* record = &malloced_scan_result->ap_details;
  239. if (user_data->fun != RT_NULL)
  240. {
  241. record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
  242. info.ssid = record->SSID.val;
  243. info.bssid = record->BSSID.octet;
  244. info.band = record->band;
  245. info.datarate = 0;
  246. info.channel = record->channel;
  247. info.rssi = record->signal_strength;
  248. info.security = record->security;
  249. user_data->fun(&info, user_data->data);
  250. }
  251. }
  252. else
  253. {
  254. user_data->fun(RT_NULL, user_data->data);
  255. rt_free(user_data);
  256. rt_kprintf("scan ap down\n");
  257. }
  258. return RTW_SUCCESS;
  259. }
  260. int rthw_wifi_scan(scan_callback_fn fun, void *data)
  261. {
  262. struct scan_user_data *user_data;
  263. if (fun == RT_NULL)
  264. {
  265. rt_kprintf("scan callback fun is null\n");
  266. return -1;
  267. }
  268. user_data = rt_malloc(sizeof(struct scan_user_data));
  269. if (user_data == RT_NULL)
  270. {
  271. rt_kprintf("wifi scan malloc fail\n");
  272. return -1;
  273. }
  274. user_data->fun = fun;
  275. user_data->data = data;
  276. if (wifi_scan_networks(rthw_wifi_scan_result_handler, user_data) != RTW_SUCCESS)
  277. {
  278. rt_kprintf("ERROR: wifi scan failed\n\r");
  279. return -1;
  280. }
  281. return 0;
  282. }
  283. static rthw_wlan_monitor_callback_t monitor_callback;
  284. static void rthw_wifi_promisc_callback(unsigned char *buf, unsigned int len, void* userdata)
  285. {
  286. if (monitor_callback)
  287. {
  288. monitor_callback(userdata, len, RT_NULL);
  289. }
  290. }
  291. void rthw_wifi_monitor_callback_set(rthw_wlan_monitor_callback_t callback)
  292. {
  293. monitor_callback = callback;
  294. }
  295. void rthw_wifi_monitor_enable(int enable)
  296. {
  297. if (enable)
  298. {
  299. wifi_enter_promisc_mode();
  300. wifi_set_promisc(RTW_PROMISC_ENABLE, rthw_wifi_promisc_callback, 1);
  301. rt_kprintf("%s run \n", __FUNCTION__);
  302. }
  303. else
  304. {
  305. wifi_set_promisc(RTW_PROMISC_DISABLE, RT_NULL, 0);
  306. rthw_wifi_stop();
  307. }
  308. }