drv_wifi.c 20 KB

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