drv_wifi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * File : drv_wifi.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. * 2017-5-30 Bernard the first version
  23. * 2018-5-30 flyingcys add amebaz wifi driver
  24. */
  25. #include <skbuff.h>
  26. #include "board.h"
  27. #include <string.h>
  28. #include <drv_wlan.h>
  29. #include <wlan_dev.h>
  30. #include "drv_wlan.h"
  31. #include "drv_wifi.h"
  32. #define DBG_ENABLE
  33. #define DBG_LEVEL DBG_INFO
  34. #define DBG_SECTION_NAME "WIFI"
  35. #define DBG_COLOR
  36. #include <rtdbg.h>
  37. #define MAX_ADDR_LEN (6)
  38. #define RT_WLAN_SSID_MAX_LEN (32)
  39. #define WIFI_INIT_FLAG (0x1 << 0)
  40. #define WIFI_MAC_FLAG (0x1 << 1)
  41. #define WIFI_TYPE_STA (0)
  42. #define WIFI_TYPE_AP (1)
  43. struct ameba_wifi
  44. {
  45. struct rt_wlan_device *wlan;
  46. rt_uint8_t dev_addr[MAX_ADDR_LEN];
  47. rt_uint8_t flag;
  48. int connected;
  49. int type;
  50. };
  51. extern unsigned char rltk_wlan_running(unsigned char idx);
  52. extern struct sk_buff * rltk_wlan_get_recv_skb(int idx);
  53. extern unsigned char rltk_wlan_check_isup(int idx);
  54. extern void rltk_wlan_tx_inc(int idx);
  55. extern struct sk_buff * rltk_wlan_alloc_skb(unsigned int total_len);
  56. extern void rltk_wlan_send_skb(int idx, struct sk_buff *skb);
  57. extern void rltk_wlan_tx_dec(int idx);
  58. extern void rtw_event_register(int event, void (*fun)(char *buf, int len, int flags, void *user_data) , void *user_data);
  59. static const struct rt_wlan_dev_ops ops;
  60. static struct ameba_wifi wifi_sta;
  61. static struct ameba_wifi wifi_ap;
  62. rt_inline struct ameba_wifi *rthw_wifi_get_dev(int idx)
  63. {
  64. int mode = rthw_wifi_mode_get();
  65. if (mode == 1) return &wifi_sta;
  66. if (mode == 2) return &wifi_ap;
  67. if (idx == 0) return &wifi_sta;
  68. if (idx == 1) return &wifi_ap;
  69. return RT_NULL;
  70. }
  71. rt_inline int rthw_wifi_get_idx(struct ameba_wifi *wifi)
  72. {
  73. int mode = rthw_wifi_mode_get();
  74. if (mode == 1) return 0;
  75. if (mode == 2) return 0;
  76. return wifi->type;
  77. }
  78. int rthw_wifi_register(struct ameba_wifi *wifi)
  79. {
  80. struct rt_wlan_device *wlan = RT_NULL;
  81. if ((wifi->flag & WIFI_INIT_FLAG) == 0)
  82. {
  83. if (wifi->type == WIFI_TYPE_STA)
  84. wlan = rt_wlan_dev_register(RT_WLAN_DEVICE_STA_NAME, &ops, 0, wifi);
  85. if (wifi->type == WIFI_TYPE_AP)
  86. wlan = rt_wlan_dev_register(RT_WLAN_DEVICE_AP_NAME, &ops, 0, wifi);
  87. wifi->flag |= WIFI_INIT_FLAG;
  88. wifi->wlan = wlan;
  89. LOG_D("F:%s L:%d wifi:0x%08x wlan:0x%08x\n", __FUNCTION__, __LINE__, wifi, wlan);
  90. }
  91. return RT_EOK;
  92. }
  93. void netif_post_sleep_processing(void)
  94. {
  95. }
  96. void netif_pre_sleep_processing(void)
  97. {
  98. }
  99. unsigned char *rltk_wlan_get_ip(int idx)
  100. {
  101. struct ameba_wifi *wifi;
  102. wifi = rthw_wifi_get_dev(idx);
  103. if (wifi == RT_NULL)
  104. return RT_NULL;
  105. LOG_D("F:%s L:%d is run", __FUNCTION__, __LINE__);
  106. /* 这里留空了,会不会炸未知 */
  107. return RT_NULL;
  108. }
  109. int netif_is_valid_IP(int idx, unsigned char *ip_dest)
  110. {
  111. LOG_D("F:%s L:%d is run ip: %d:%d:%d:%d", __FUNCTION__, __LINE__,
  112. ip_dest[0], ip_dest[1], ip_dest[2], ip_dest[3]);
  113. return 1;
  114. }
  115. void rltk_wlan_set_netif_info(int idx, void *dev, rt_uint8_t *dev_addr)
  116. {
  117. struct ameba_wifi *wifi = RT_NULL;
  118. wifi = rthw_wifi_get_dev(idx);
  119. if (wifi == RT_NULL)
  120. return;
  121. LOG_D("F:%s L:%d wifi:0x%08x type:0x%x", __FUNCTION__, __LINE__, wifi, wifi->flag);
  122. rt_memcpy(wifi->dev_addr, dev_addr, 6);
  123. wifi->flag |= WIFI_MAC_FLAG;
  124. rthw_wifi_register(wifi);
  125. LOG_D("wifi type:%d", wifi->type);
  126. LOG_D("idx:%d MAC %02x:%02x:%02x:%02x:%02x:%02x", idx, dev_addr[0], dev_addr[1], dev_addr[2], dev_addr[3], dev_addr[4], dev_addr[5]);
  127. }
  128. static void rtw_connect_callbackfn(char *buf, int len, int flags, void *user_data)
  129. {
  130. struct ameba_wifi *wifi = user_data;
  131. LOG_D("L:%d wifi connect callback flags:%d user_data:%08x", __LINE__, flags, user_data);
  132. wifi->connected = 1;
  133. rt_wlan_dev_indicate_event_handle(wifi->wlan, RT_WLAN_DEV_EVT_CONNECT, RT_NULL);
  134. }
  135. static void rtw_connect_fail_callbackfn(char *buf, int len, int flags, void *user_data)
  136. {
  137. struct ameba_wifi *wifi = user_data;
  138. LOG_D("L:%d wifi connect callback flags:%d", __LINE__, flags);
  139. wifi->connected = 0;
  140. rt_wlan_dev_indicate_event_handle(wifi->wlan, RT_WLAN_DEV_EVT_CONNECT_FAIL, RT_NULL);
  141. }
  142. static void rtw_disconnect_callbackfn(char *buf, int len, int flags, void *user_data)
  143. {
  144. struct ameba_wifi *wifi = user_data;
  145. LOG_D("L:%d wifi disconnect callback flags:%d", __LINE__, flags);
  146. wifi->connected = 0;
  147. rt_wlan_dev_indicate_event_handle(wifi->wlan, RT_WLAN_DEV_EVT_DISCONNECT, RT_NULL);
  148. }
  149. static void rtw_sta_assoc_callbackfn(char *buf, int len, int flags, void *user_data)
  150. {
  151. LOG_D("L:%d wifi sta assoc callback flags:%d", __LINE__, flags);
  152. }
  153. static void rtw_sta_disassoc_callbackfn(char *buf, int len, int flags, void *user_data)
  154. {
  155. LOG_D("L:%d wifi sta assoc callback flags:%d buf:%08x %08x", __LINE__, flags, *((rt_uint32_t*)buf), *((rt_uint32_t*)buf + 4));
  156. }
  157. void netif_rx(int idx, unsigned int len)
  158. {
  159. struct ameba_wifi *wifi = RT_NULL;
  160. struct sk_buff *skb = RT_NULL;
  161. wifi = rthw_wifi_get_dev(idx);
  162. if (wifi == RT_NULL)
  163. return;
  164. LOG_D("F:%s L:%d idx:%d len:%d", __FUNCTION__, __LINE__, idx, len);
  165. if((!wifi->connected) || (!rltk_wlan_running(idx)))
  166. return;
  167. skb = (struct sk_buff *)rltk_wlan_get_recv_skb(idx);
  168. if(!skb)
  169. {
  170. LOG_D("netif_rx rltk_wlan_get_recv_skb NULL.");
  171. return;
  172. }
  173. rt_wlan_dev_report_data(wifi->wlan, skb->data, len);
  174. }
  175. static rt_wlan_security_t security_map_from_ameba(rthw_security_t security)
  176. {
  177. rt_wlan_security_t result = SECURITY_OPEN;
  178. switch (security)
  179. {
  180. case RTHW_SECURITY_OPEN: result = SECURITY_OPEN; break;
  181. case RTHW_SECURITY_WEP_PSK: result = SECURITY_WEP_PSK; break;
  182. case RTHW_SECURITY_WEP_SHARED: result = SECURITY_WEP_SHARED; break;
  183. case RTHW_SECURITY_WPA_TKIP_PSK: result = SECURITY_WPA_TKIP_PSK; break;
  184. case RTHW_SECURITY_WPA_AES_PSK: result = SECURITY_WPA_AES_PSK; break;
  185. case RTHW_SECURITY_WPA2_AES_PSK: result = SECURITY_WPA2_AES_PSK; break;
  186. case RTHW_SECURITY_WPA2_TKIP_PSK: result = SECURITY_WPA2_TKIP_PSK; break;
  187. case RTHW_SECURITY_WPA2_MIXED_PSK: result = SECURITY_WPA2_MIXED_PSK; break;
  188. case RTHW_SECURITY_WPA_WPA2_MIXED: result = SECURITY_WPA2_AES_PSK; break;
  189. case RTHW_SECURITY_WPS_OPEN: result = SECURITY_WPS_OPEN; break;
  190. case RTHW_SECURITY_WPS_SECURE: result = SECURITY_WPS_SECURE; break;
  191. default: result = -1; break;
  192. }
  193. return result;
  194. }
  195. static rthw_security_t security_map_from_rtthread(rt_wlan_security_t security)
  196. {
  197. rt_wlan_security_t result = RTHW_SECURITY_OPEN;
  198. switch (security)
  199. {
  200. case SECURITY_OPEN: result = RTHW_SECURITY_OPEN; break;
  201. case SECURITY_WEP_PSK: result = RTHW_SECURITY_WEP_PSK; break;
  202. case SECURITY_WEP_SHARED: result = RTHW_SECURITY_WEP_SHARED; break;
  203. case SECURITY_WPA_TKIP_PSK: result = RTHW_SECURITY_WPA_TKIP_PSK; break;
  204. case SECURITY_WPA_AES_PSK: result = RTHW_SECURITY_WPA_AES_PSK; break;
  205. case SECURITY_WPA2_AES_PSK: result = RTHW_SECURITY_WPA2_AES_PSK; break;
  206. case SECURITY_WPA2_TKIP_PSK: result = RTHW_SECURITY_WPA2_TKIP_PSK; break;
  207. case SECURITY_WPA2_MIXED_PSK: result = RTHW_SECURITY_WPA2_MIXED_PSK; break;
  208. case SECURITY_WPS_OPEN: result = RTHW_SECURITY_WPS_OPEN; break;
  209. case SECURITY_WPS_SECURE: result = RTHW_SECURITY_WPS_SECURE; break;
  210. default: result = -1; break;
  211. }
  212. return result;
  213. }
  214. static void rt_ameba_wifi_scan_callback(struct rthw_wlan_info *info, void *user_data)
  215. {
  216. struct rt_wlan_info wlan_info = { 0 };
  217. struct rt_wlan_buff buff;
  218. struct ameba_wifi *wifi = user_data;
  219. if (info == RT_NULL)
  220. {
  221. rt_wlan_dev_indicate_event_handle(wifi->wlan, RT_WLAN_DEV_EVT_SCAN_DONE, RT_NULL);
  222. return;
  223. }
  224. memcpy(&wlan_info.bssid[0], info->bssid, 6);
  225. strncpy(&wlan_info.ssid.val[0], info->ssid, RT_WLAN_SSID_MAX_LEN);
  226. wlan_info.ssid.len = strlen(&wlan_info.ssid.val[0]);
  227. wlan_info.band = info->band == RTHW_802_11_BAND_2_4GHZ ? RTHW_802_11_BAND_2_4GHZ : RTHW_802_11_BAND_5GHZ;
  228. wlan_info.channel = info->channel;
  229. wlan_info.datarate = info->datarate * 1000;
  230. wlan_info.security = security_map_from_ameba(info->security);
  231. wlan_info.rssi = info->rssi;
  232. buff.data = &wlan_info;
  233. buff.len = sizeof(wlan_info);
  234. rt_wlan_dev_indicate_event_handle(wifi->wlan, RT_WLAN_DEV_EVT_SCAN_REPORT, &buff);
  235. }
  236. static void rthw_wlan_monitor_callback(rt_uint8_t *data, int len, void *user_data)
  237. {
  238. rt_wlan_dev_promisc_handler(wifi_sta.wlan, data, len);
  239. }
  240. static rt_err_t rthw_wlan_init (struct rt_wlan_device *wlan)
  241. {
  242. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  243. return RT_EOK;
  244. }
  245. static rt_err_t rthw_wlan_mode (struct rt_wlan_device *wlan, rt_wlan_mode_t mode)
  246. {
  247. struct ameba_wifi *wifi = (struct ameba_wifi *)(wlan->user_data);
  248. LOG_D("F:%s L:%d mode:%d", __FUNCTION__, __LINE__, mode);
  249. if (mode == RT_WLAN_STATION)
  250. {
  251. if (wifi->type != WIFI_TYPE_STA)
  252. {
  253. LOG_D("this wlan not support sta mode");
  254. return -RT_ERROR;
  255. }
  256. }
  257. else if (mode == RT_WLAN_AP)
  258. {
  259. if (wifi->type != WIFI_TYPE_AP)
  260. {
  261. LOG_D("this wlan not support ap mode");
  262. return -RT_ERROR;
  263. }
  264. }
  265. return RT_EOK;
  266. }
  267. static rt_err_t rthw_wlan_scan (struct rt_wlan_device *wlan, struct rt_scan_info *scan_info)
  268. {
  269. struct ameba_wifi *wifi = (struct ameba_wifi *)(wlan->user_data);
  270. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  271. LOG_D("F:%s L:%d wifi:0x%08x type:0x%x", __FUNCTION__, __LINE__, wifi, wifi->type);
  272. if (wifi->type != WIFI_TYPE_STA)
  273. {
  274. LOG_D("this wlan not support scan mode");
  275. return -RT_ERROR;
  276. }
  277. if (rthw_wifi_mode_get() == RTHW_MODE_NONE)
  278. {
  279. if(rthw_wifi_start(RTHW_MODE_STA_AP) != RT_EOK)
  280. {
  281. LOG_D("L:%d wifistart failed...", __LINE__);
  282. return -1;
  283. }
  284. }
  285. rthw_wifi_scan(rt_ameba_wifi_scan_callback, wifi);
  286. return RT_EOK;
  287. }
  288. static rt_err_t rthw_wlan_join (struct rt_wlan_device *wlan, struct rt_sta_info *sta_info)
  289. {
  290. struct ameba_wifi *wifi = (struct ameba_wifi *)(wlan->user_data);
  291. int result = 0, i;
  292. char *ssid = RT_NULL, *key = RT_NULL;
  293. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  294. if (wifi->type != WIFI_TYPE_STA)
  295. {
  296. LOG_E("this wlan not support sta mode");
  297. return -RT_ERROR;
  298. }
  299. if ((rthw_wifi_mode_get() != RTHW_MODE_STA) && (rthw_wifi_mode_get() != RTHW_MODE_STA_AP))
  300. {
  301. rthw_wifi_stop();
  302. rt_thread_delay(RT_TICK_PER_SECOND / 10);
  303. if (rthw_wifi_start(RTHW_MODE_STA_AP) != RT_EOK)
  304. {
  305. LOG_E("wifi on failed, join fail");
  306. return -RT_ERROR;
  307. }
  308. }
  309. for (i = 0; i < RT_WLAN_BSSID_MAX_LENGTH; i++)
  310. {
  311. if (sta_info->bssid[i] != 0xff || sta_info->bssid[i] != 0x00)
  312. break;
  313. }
  314. if (i < RT_WLAN_BSSID_MAX_LENGTH)
  315. {
  316. if (sta_info->ssid.len > 0)
  317. ssid = &sta_info->ssid.val[0];
  318. if (sta_info->key.len > 0)
  319. key = &sta_info->key.val[0];
  320. LOG_D("bssid connect bssid: %02x:%02x:%02x:%02x:%02x:%02x ssid:%s ssid_len:%d key:%s key_len%d",
  321. sta_info->bssid[0],sta_info->bssid[1],sta_info->bssid[2],sta_info->bssid[3],sta_info->bssid[4],sta_info->bssid[5],
  322. ssid,
  323. sta_info->ssid.len,
  324. key,
  325. sta_info->key.len
  326. );
  327. result = rthw_wifi_connect_bssid(sta_info->bssid, ssid, sta_info->ssid.len, key, sta_info->key.len, security_map_from_rtthread(sta_info->security));
  328. }
  329. else
  330. {
  331. result = rthw_wifi_connect(sta_info->ssid.val, sta_info->ssid.len, sta_info->key.val, sta_info->key.len, security_map_from_rtthread(sta_info->security));
  332. }
  333. if (result != 0)
  334. {
  335. LOG_E("amebaz_wifi_connect failed...");
  336. return -RT_ERROR;
  337. }
  338. // netif_set_connected((struct ameba_wifi *)wlan, 1);
  339. LOG_D("amebaz_wifi_connect do");
  340. return RT_EOK;
  341. }
  342. static rt_err_t rthw_wlan_softap (struct rt_wlan_device *wlan, struct rt_ap_info *ap_info)
  343. {
  344. struct ameba_wifi *wifi = (struct ameba_wifi *)(wlan->user_data);
  345. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  346. if (wifi->type != WIFI_TYPE_AP)
  347. {
  348. LOG_E("this wlan not support ap mode");
  349. return -RT_ERROR;
  350. }
  351. if (rthw_wifi_ap_start(&ap_info->ssid.val[0], &ap_info->key.val[0], ap_info->channel) != 0)
  352. {
  353. rt_wlan_dev_indicate_event_handle(wifi->wlan, RT_WLAN_DEV_EVT_AP_STOP, RT_NULL);
  354. wifi->connected = 0;
  355. return -RT_ERROR;
  356. }
  357. rt_wlan_dev_indicate_event_handle(wifi->wlan, RT_WLAN_DEV_EVT_AP_START, RT_NULL);
  358. wifi->connected = 1;
  359. return RT_EOK;
  360. }
  361. static rt_err_t rthw_wlan_disconnect (struct rt_wlan_device *wlan)
  362. {
  363. struct ameba_wifi *wifi = (struct ameba_wifi *)(wlan->user_data);
  364. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  365. if (wifi->type != WIFI_TYPE_STA)
  366. {
  367. LOG_E("this wlan not support sta mode");
  368. return -RT_ERROR;
  369. }
  370. wifi->connected = 0;
  371. rthw_wifi_sta_disconnect();
  372. rt_wlan_dev_indicate_event_handle(wifi->wlan, RT_WLAN_DEV_EVT_AP_STOP, RT_NULL);
  373. return RT_EOK;
  374. }
  375. static rt_err_t rthw_wlan_ap_stop (struct rt_wlan_device *wlan)
  376. {
  377. struct ameba_wifi *wifi = (struct ameba_wifi *)(wlan->user_data);
  378. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  379. if (wifi->type != WIFI_TYPE_AP)
  380. {
  381. LOG_E("this wlan not support ap mode");
  382. return -RT_ERROR;
  383. }
  384. rthw_wifi_ap_disconnect();
  385. return RT_EOK;
  386. }
  387. static rt_err_t rthw_wlan_ap_deauth (struct rt_wlan_device *wlan, rt_uint8_t mac[])
  388. {
  389. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  390. return RT_EOK;
  391. }
  392. static rt_err_t rthw_wlan_scan_stop (struct rt_wlan_device *wlan)
  393. {
  394. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  395. return RT_EOK;
  396. }
  397. static int rthw_wlan_get_rssi (struct rt_wlan_device *wlan)
  398. {
  399. struct ameba_wifi *wifi = (struct ameba_wifi *)(wlan->user_data);
  400. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  401. if (wifi->type != WIFI_TYPE_STA)
  402. {
  403. LOG_E("this wlan not support sta mode");
  404. return -RT_ERROR;
  405. }
  406. return rthw_wifi_rssi_get();
  407. }
  408. static rt_err_t rthw_wlan_set_powersave (struct rt_wlan_device *wlan, int level)
  409. {
  410. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  411. return RT_EOK;
  412. }
  413. static int rthw_wlan_get_powersave (struct rt_wlan_device *wlan)
  414. {
  415. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  416. return 0;
  417. }
  418. static rt_err_t rthw_wlan_cfg_promisc (struct rt_wlan_device *wlan, rt_bool_t start)
  419. {
  420. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  421. if(start)
  422. {
  423. rthw_wifi_monitor_callback_set(rthw_wlan_monitor_callback);
  424. rthw_wifi_monitor_enable(1);
  425. }
  426. else
  427. {
  428. rthw_wifi_monitor_callback_set(RT_NULL);
  429. rthw_wifi_monitor_enable(0);
  430. }
  431. return RT_EOK;
  432. }
  433. static rt_err_t rthw_wlan_cfg_filter (struct rt_wlan_device *wlan, struct rt_wlan_filter *filter)
  434. {
  435. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  436. return RT_EOK;
  437. }
  438. static rt_err_t rthw_wlan_set_channel (struct rt_wlan_device *wlan, int channel)
  439. {
  440. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  441. rthw_wifi_channel_set(channel);
  442. return RT_EOK;
  443. }
  444. static int rthw_wlan_get_channel (struct rt_wlan_device *wlan)
  445. {
  446. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  447. return rthw_wifi_channel_get();
  448. }
  449. static rt_err_t rthw_wlan_set_country (struct rt_wlan_device *wlan, rt_country_code_t country_code)
  450. {
  451. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  452. return RT_EOK;
  453. }
  454. static rt_country_code_t rthw_wlan_get_country (struct rt_wlan_device *wlan)
  455. {
  456. LOG_D("F:%s L:%d\n", __FUNCTION__, __LINE__);
  457. return RT_EOK;
  458. }
  459. static rt_err_t rthw_wlan_set_mac (struct rt_wlan_device *wlan, rt_uint8_t mac[])
  460. {
  461. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  462. return -RT_ERROR;
  463. }
  464. static rt_err_t rthw_wlan_get_mac (struct rt_wlan_device *wlan, rt_uint8_t mac[])
  465. {
  466. struct ameba_wifi *wifi = (struct ameba_wifi *)wlan->user_data;
  467. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  468. if(mac == RT_NULL)
  469. {
  470. return -RT_ERROR;
  471. }
  472. memcpy(mac, wifi->dev_addr, MAX_ADDR_LEN);
  473. return RT_EOK;
  474. }
  475. static int rthw_wlan_recv (struct rt_wlan_device *wlan, void *buff, int len)
  476. {
  477. LOG_D("F:%s L:%d", __FUNCTION__, __LINE__);
  478. return RT_EOK;
  479. }
  480. static int rthw_wlan_send (struct rt_wlan_device *wlan, void *buff, int len)
  481. {
  482. struct ameba_wifi *wifi = (struct ameba_wifi *)wlan->user_data;
  483. int idx = rthw_wifi_get_idx(wifi);
  484. rt_base_t level;
  485. struct sk_buff *skb = RT_NULL;
  486. LOG_D("F:%s L:%d len:%d", __FUNCTION__, __LINE__, len);
  487. level = rt_hw_interrupt_disable();
  488. if(!wifi->connected || !rltk_wlan_check_isup(idx))
  489. {
  490. rt_hw_interrupt_enable(level);
  491. return -RT_ERROR;
  492. }
  493. rltk_wlan_tx_inc(idx);
  494. rt_hw_interrupt_enable(level);
  495. skb = (struct sk_buff *)rltk_wlan_alloc_skb(len);
  496. if (skb == RT_NULL)
  497. {
  498. LOG_W("rltk_wlan_alloc_skb NULL for WIFI TX.");
  499. goto exit;
  500. }
  501. /* copy buff to a whole ETH frame */
  502. memcpy(skb->tail, buff, len);
  503. skb_put(skb, len);
  504. rltk_wlan_send_skb(idx, skb);
  505. exit:
  506. level = rt_hw_interrupt_disable();
  507. rltk_wlan_tx_dec(idx);
  508. rt_hw_interrupt_enable(level);
  509. LOG_D("F:%s L:%d end", __FUNCTION__, __LINE__);
  510. return RT_EOK;
  511. }
  512. static const struct rt_wlan_dev_ops ops =
  513. {
  514. .wlan_init = rthw_wlan_init ,
  515. .wlan_mode = rthw_wlan_mode ,
  516. .wlan_scan = rthw_wlan_scan ,
  517. .wlan_join = rthw_wlan_join ,
  518. .wlan_softap = rthw_wlan_softap ,
  519. .wlan_disconnect = rthw_wlan_disconnect ,
  520. .wlan_ap_stop = rthw_wlan_ap_stop ,
  521. .wlan_ap_deauth = rthw_wlan_ap_deauth ,
  522. .wlan_scan_stop = rthw_wlan_scan_stop ,
  523. .wlan_get_rssi = rthw_wlan_get_rssi ,
  524. .wlan_set_powersave = rthw_wlan_set_powersave ,
  525. .wlan_get_powersave = rthw_wlan_get_powersave ,
  526. .wlan_cfg_promisc = rthw_wlan_cfg_promisc ,
  527. .wlan_cfg_filter = rthw_wlan_cfg_filter ,
  528. .wlan_set_channel = rthw_wlan_set_channel ,
  529. .wlan_get_channel = rthw_wlan_get_channel ,
  530. .wlan_set_country = rthw_wlan_set_country ,
  531. .wlan_get_country = rthw_wlan_get_country ,
  532. .wlan_set_mac = rthw_wlan_set_mac ,
  533. .wlan_get_mac = rthw_wlan_get_mac ,
  534. .wlan_recv = rthw_wlan_recv ,
  535. .wlan_send = rthw_wlan_send ,
  536. };
  537. int rthw_wifi_low_init(void)
  538. {
  539. static rt_int8_t _init_flag = 0;
  540. if (_init_flag)
  541. {
  542. return 1;
  543. }
  544. rt_memset(&wifi_sta, 0, sizeof(wifi_sta));
  545. rt_memset(&wifi_ap, 0, sizeof(wifi_ap));
  546. wifi_sta.type = WIFI_TYPE_STA;
  547. wifi_ap.type = WIFI_TYPE_AP;
  548. if(rthw_wifi_start(RTHW_MODE_STA_AP) != RT_EOK)
  549. {
  550. LOG_E("amebaz_wifi_start failed...");
  551. return -1;
  552. }
  553. LOG_I("amebaz_wifi_start success");
  554. LOG_D("F:%s L:%d wifi_sta:0x%08x wifi_ap:0x%08x", __FUNCTION__, __LINE__, &wifi_sta, &wifi_ap);
  555. wifi_reg_event_handler(0, rtw_connect_callbackfn, &wifi_sta);
  556. wifi_reg_event_handler(1, rtw_disconnect_callbackfn, &wifi_sta);
  557. wifi_reg_event_handler(8, rtw_sta_assoc_callbackfn, &wifi_ap);
  558. wifi_reg_event_handler(9, rtw_sta_disassoc_callbackfn, &wifi_ap);
  559. _init_flag = 1;
  560. return 0;
  561. }
  562. INIT_DEVICE_EXPORT(rthw_wifi_low_init);