drv_wlan.c 8.4 KB

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