wlan_dev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-03 tyx the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <wlan_dev.h>
  13. #include <wlan_prot.h>
  14. #define DBG_ENABLE
  15. #ifdef RT_WLAN_DEV_DEBUG
  16. #define DBG_LEVEL DBG_LOG
  17. #else
  18. #define DBG_LEVEL DBG_INFO
  19. #endif
  20. #define DBG_SECTION_NAME "WLAN.dev"
  21. #define DBG_COLOR
  22. #include <rtdbg.h>
  23. #ifndef RT_DEVICE
  24. #define RT_DEVICE(__device) ((rt_device_t)__device)
  25. #endif
  26. #define WLAN_DEV_LOCK(_wlan) (rt_mutex_take(&(_wlan)->lock, RT_WAITING_FOREVER))
  27. #define WLAN_DEV_UNLOCK(_wlan) (rt_mutex_release(&(_wlan)->lock))
  28. #if RT_WLAN_SSID_MAX_LENGTH < 1
  29. #error "SSID length is too short"
  30. #endif
  31. #if RT_WLAN_BSSID_MAX_LENGTH < 1
  32. #error "BSSID length is too short"
  33. #endif
  34. #if RT_WLAN_PASSWORD_MAX_LENGTH < 1
  35. #error "password length is too short"
  36. #endif
  37. #if RT_WLAN_DEV_EVENT_NUM < 2
  38. #error "dev num Too little"
  39. #endif
  40. rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode)
  41. {
  42. rt_err_t result = RT_EOK;
  43. /* init wlan device */
  44. LOG_D("F:%s L:%d is run device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
  45. if ((device == RT_NULL) || (mode >= RT_WLAN_MODE_MAX))
  46. {
  47. LOG_E("F:%s L:%d Parameter Wrongful device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode);
  48. return -RT_ERROR;
  49. }
  50. result = rt_device_init(RT_DEVICE(device));
  51. if (result != RT_EOK)
  52. {
  53. LOG_E("L:%d wlan init failed", __LINE__);
  54. return -RT_ERROR;
  55. }
  56. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_MODE, (void *)&mode);
  57. if (result != RT_EOK)
  58. {
  59. LOG_E("L:%d wlan config mode failed", __LINE__);
  60. return -RT_ERROR;
  61. }
  62. device->mode = mode;
  63. return result;
  64. }
  65. rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
  66. {
  67. rt_err_t result = RT_EOK;
  68. struct rt_sta_info sta_info;
  69. if (device == RT_NULL)
  70. {
  71. return -RT_EIO;
  72. }
  73. if (info == RT_NULL)
  74. {
  75. return -RT_ERROR;
  76. }
  77. if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) ||
  78. (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH))
  79. {
  80. LOG_E("L:%d password or ssid is to long", __LINE__);
  81. return -RT_ERROR;
  82. }
  83. rt_memset(&sta_info, 0, sizeof(struct rt_sta_info));
  84. rt_memcpy(&sta_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
  85. rt_memcpy(sta_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
  86. if (password != RT_NULL)
  87. {
  88. rt_memcpy(sta_info.key.val, password, password_len);
  89. sta_info.key.len = password_len;
  90. }
  91. sta_info.channel = info->channel;
  92. sta_info.security = info->security;
  93. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_JOIN, &sta_info);
  94. return result;
  95. }
  96. rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device)
  97. {
  98. rt_err_t result = RT_EOK;
  99. if (device == RT_NULL)
  100. {
  101. return -RT_EIO;
  102. }
  103. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_DISCONNECT, RT_NULL);
  104. return result;
  105. }
  106. rt_err_t rt_wlan_dev_ap_start(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len)
  107. {
  108. rt_err_t result = RT_EOK;
  109. struct rt_ap_info ap_info;
  110. if (device == RT_NULL)
  111. {
  112. return -RT_EIO;
  113. }
  114. if (info == RT_NULL)
  115. {
  116. return -RT_ERROR;
  117. }
  118. if ((password_len >= RT_WLAN_PASSWORD_MAX_LENGTH) ||
  119. (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH))
  120. {
  121. LOG_E("L:%d password or ssid is to long", __LINE__);
  122. return -RT_ERROR;
  123. }
  124. rt_memset(&ap_info, 0, sizeof(struct rt_ap_info));
  125. rt_memcpy(&ap_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
  126. rt_memcpy(ap_info.key.val, password, password_len);
  127. ap_info.key.len = password_len;
  128. ap_info.hidden = info->hidden;
  129. ap_info.channel = info->channel;
  130. ap_info.security = info->security;
  131. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SOFTAP, &ap_info);
  132. return result;
  133. }
  134. rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device)
  135. {
  136. rt_err_t result = RT_EOK;
  137. if (device == RT_NULL)
  138. {
  139. return -RT_EIO;
  140. }
  141. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_STOP, RT_NULL);
  142. return result;
  143. }
  144. rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6])
  145. {
  146. rt_err_t result = RT_EOK;
  147. if (device == RT_NULL)
  148. {
  149. return -RT_EIO;
  150. }
  151. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_DEAUTH, mac);
  152. return result;
  153. }
  154. int rt_wlan_dev_get_rssi(struct rt_wlan_device *device)
  155. {
  156. int rssi = 0;
  157. if (device == RT_NULL)
  158. {
  159. return -RT_EIO;
  160. }
  161. rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_RSSI, &rssi);
  162. return rssi;
  163. }
  164. rt_err_t rt_wlan_dev_get_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
  165. {
  166. rt_err_t result = RT_EOK;
  167. if (device == RT_NULL)
  168. {
  169. return -RT_EIO;
  170. }
  171. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_MAC, &mac[0]);
  172. return result;
  173. }
  174. rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
  175. {
  176. rt_err_t result = RT_EOK;
  177. if (device == RT_NULL)
  178. {
  179. return -RT_EIO;
  180. }
  181. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_MAC, &mac[0]);
  182. return result;
  183. }
  184. rt_err_t rt_wlan_dev_set_powersave(struct rt_wlan_device *device, int level)
  185. {
  186. rt_err_t result = RT_EOK;
  187. if (device == RT_NULL)
  188. {
  189. return -RT_EIO;
  190. }
  191. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_POWERSAVE, &level);
  192. return result;
  193. }
  194. int rt_wlan_dev_get_powersave(struct rt_wlan_device *device)
  195. {
  196. int level = 0;
  197. if (device == RT_NULL)
  198. {
  199. return -1;
  200. }
  201. rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_POWERSAVE, &level);
  202. return level;
  203. }
  204. rt_err_t rt_wlan_dev_register_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler, void *parameter)
  205. {
  206. int i = 0;
  207. rt_base_t level;
  208. if (device == RT_NULL)
  209. {
  210. return -RT_EIO;
  211. }
  212. if (event >= RT_WLAN_DEV_EVT_MAX)
  213. {
  214. return -RT_EINVAL;
  215. }
  216. level = rt_hw_interrupt_disable();
  217. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  218. {
  219. if (device->handler_table[event][i].handler == RT_NULL)
  220. {
  221. device->handler_table[event][i].handler = handler;
  222. device->handler_table[event][i].parameter = parameter;
  223. rt_hw_interrupt_enable(level);
  224. return RT_EOK;
  225. }
  226. }
  227. rt_hw_interrupt_enable(level);
  228. /* No space found */
  229. return -RT_ERROR;
  230. }
  231. rt_err_t rt_wlan_dev_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler)
  232. {
  233. int i = 0;
  234. rt_base_t level;
  235. if (device == RT_NULL)
  236. {
  237. return -RT_EIO;
  238. }
  239. if (event >= RT_WLAN_DEV_EVT_MAX)
  240. {
  241. return -RT_EINVAL;
  242. }
  243. level = rt_hw_interrupt_disable();
  244. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  245. {
  246. if (device->handler_table[event][i].handler == handler)
  247. {
  248. rt_memset(&device->handler_table[event][i], 0, sizeof(struct rt_wlan_dev_event_desc));
  249. rt_hw_interrupt_enable(level);
  250. return RT_EOK;
  251. }
  252. }
  253. rt_hw_interrupt_enable(level);
  254. /* not find iteam */
  255. return -RT_ERROR;
  256. }
  257. void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff)
  258. {
  259. void *parameter[RT_WLAN_DEV_EVENT_NUM];
  260. rt_wlan_dev_event_handler handler[RT_WLAN_DEV_EVENT_NUM];
  261. int i;
  262. rt_base_t level;
  263. if (device == RT_NULL)
  264. {
  265. return;
  266. }
  267. if (event >= RT_WLAN_DEV_EVT_MAX)
  268. {
  269. return;
  270. }
  271. /* get callback handle */
  272. level = rt_hw_interrupt_disable();
  273. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  274. {
  275. handler[i] = device->handler_table[event][i].handler;
  276. parameter[i] = device->handler_table[event][i].parameter;
  277. }
  278. rt_hw_interrupt_enable(level);
  279. /* run callback */
  280. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  281. {
  282. if (handler[i] != RT_NULL)
  283. {
  284. handler[i](device, event, buff, parameter[i]);
  285. }
  286. }
  287. }
  288. rt_err_t rt_wlan_dev_enter_promisc(struct rt_wlan_device *device)
  289. {
  290. rt_err_t result = RT_EOK;
  291. int enable = 1;
  292. if (device == RT_NULL)
  293. {
  294. return -RT_EIO;
  295. }
  296. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
  297. return result;
  298. }
  299. rt_err_t rt_wlan_dev_exit_promisc(struct rt_wlan_device *device)
  300. {
  301. rt_err_t result = RT_EOK;
  302. int enable = 0;
  303. if (device == RT_NULL)
  304. {
  305. return -RT_EIO;
  306. }
  307. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
  308. return result;
  309. }
  310. rt_err_t rt_wlan_dev_set_promisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback)
  311. {
  312. if (device == RT_NULL)
  313. {
  314. return -RT_EIO;
  315. }
  316. device->pormisc_callback = callback;
  317. return RT_EOK;
  318. }
  319. void rt_wlan_dev_promisc_handler(struct rt_wlan_device *device, void *data, int len)
  320. {
  321. rt_wlan_pormisc_callback_t callback;
  322. if (device == RT_NULL)
  323. {
  324. return;
  325. }
  326. callback = device->pormisc_callback;
  327. if (callback != RT_NULL)
  328. {
  329. callback(device, data, len);
  330. }
  331. }
  332. rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter)
  333. {
  334. rt_err_t result = RT_EOK;
  335. if (device == RT_NULL)
  336. {
  337. return -RT_EIO;
  338. }
  339. if (filter == RT_NULL)
  340. {
  341. return -RT_ERROR;
  342. }
  343. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_FILTER, filter);
  344. return result;
  345. }
  346. rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel)
  347. {
  348. rt_err_t result = RT_EOK;
  349. if (device == RT_NULL)
  350. {
  351. return -RT_EIO;
  352. }
  353. if (channel < 0)
  354. {
  355. return -RT_ERROR;
  356. }
  357. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_CHANNEL, &channel);
  358. return result;
  359. }
  360. rt_err_t rt_wlan_dev_get_channel(struct rt_wlan_device *device)
  361. {
  362. rt_err_t result = RT_EOK;
  363. int channel;
  364. if (device == RT_NULL)
  365. {
  366. return -RT_EIO;
  367. }
  368. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_CHANNEL, &channel);
  369. if (result != RT_EOK)
  370. {
  371. return -1;
  372. }
  373. return channel;
  374. }
  375. rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code)
  376. {
  377. int result = RT_EOK;
  378. if (device == RT_NULL)
  379. {
  380. return -RT_EIO;
  381. }
  382. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_COUNTRY, &country_code);
  383. return result;
  384. }
  385. rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device)
  386. {
  387. int result = 0;
  388. rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
  389. if (device == RT_NULL)
  390. {
  391. return country_code;
  392. }
  393. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_COUNTRY, &country_code);
  394. if (result != RT_EOK)
  395. {
  396. return RT_COUNTRY_UNKNOWN;
  397. }
  398. return country_code;
  399. }
  400. rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info)
  401. {
  402. struct rt_scan_info scan_info = { 0 };
  403. struct rt_scan_info *p_scan_info = RT_NULL;
  404. rt_err_t result = 0;
  405. if (device == RT_NULL)
  406. {
  407. return -RT_EIO;
  408. }
  409. if (info != RT_NULL)
  410. {
  411. if (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH)
  412. {
  413. LOG_E("L:%d ssid is to long", __LINE__);
  414. return -RT_EINVAL;
  415. }
  416. rt_memcpy(&scan_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
  417. rt_memcpy(scan_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
  418. scan_info.channel_min = -1;
  419. scan_info.channel_max = -1;
  420. p_scan_info = &scan_info;
  421. }
  422. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN, p_scan_info);
  423. return result;
  424. }
  425. rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device)
  426. {
  427. rt_err_t result = 0;
  428. if (device == RT_NULL)
  429. {
  430. return -RT_EIO;
  431. }
  432. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN_STOP, RT_NULL);
  433. return result;
  434. }
  435. rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len)
  436. {
  437. return rt_wlan_dev_transfer_prot(device, buff, len);
  438. }
  439. static rt_err_t _rt_wlan_dev_init(rt_device_t dev)
  440. {
  441. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  442. rt_err_t result = RT_EOK;
  443. rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_FIFO);
  444. if (wlan->ops->wlan_init)
  445. result = wlan->ops->wlan_init(wlan);
  446. if (result == RT_EOK)
  447. {
  448. LOG_I("wlan init success");
  449. }
  450. else
  451. {
  452. LOG_I("wlan init failed");
  453. }
  454. return result;
  455. }
  456. static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args)
  457. {
  458. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  459. rt_err_t err = RT_EOK;
  460. RT_ASSERT(dev != RT_NULL);
  461. WLAN_DEV_LOCK(wlan);
  462. switch (cmd)
  463. {
  464. case RT_WLAN_CMD_MODE:
  465. {
  466. rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args);
  467. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE");
  468. if (wlan->ops->wlan_mode)
  469. err = wlan->ops->wlan_mode(wlan, mode);
  470. break;
  471. }
  472. case RT_WLAN_CMD_SCAN:
  473. {
  474. struct rt_scan_info *scan_info = args;
  475. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN");
  476. if (wlan->ops->wlan_scan)
  477. err = wlan->ops->wlan_scan(wlan, scan_info);
  478. break;
  479. }
  480. case RT_WLAN_CMD_JOIN:
  481. {
  482. struct rt_sta_info *sta_info = args;
  483. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN");
  484. if (wlan->ops->wlan_join)
  485. err = wlan->ops->wlan_join(wlan, sta_info);
  486. break;
  487. }
  488. case RT_WLAN_CMD_SOFTAP:
  489. {
  490. struct rt_ap_info *ap_info = args;
  491. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP");
  492. if (wlan->ops->wlan_softap)
  493. err = wlan->ops->wlan_softap(wlan, ap_info);
  494. break;
  495. }
  496. case RT_WLAN_CMD_DISCONNECT:
  497. {
  498. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT");
  499. if (wlan->ops->wlan_disconnect)
  500. err = wlan->ops->wlan_disconnect(wlan);
  501. break;
  502. }
  503. case RT_WLAN_CMD_AP_STOP:
  504. {
  505. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP");
  506. if (wlan->ops->wlan_ap_stop)
  507. err = wlan->ops->wlan_ap_stop(wlan);
  508. break;
  509. }
  510. case RT_WLAN_CMD_AP_DEAUTH:
  511. {
  512. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH");
  513. if (wlan->ops->wlan_ap_deauth)
  514. err = wlan->ops->wlan_ap_deauth(wlan, args);
  515. break;
  516. }
  517. case RT_WLAN_CMD_SCAN_STOP:
  518. {
  519. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP");
  520. if (wlan->ops->wlan_scan_stop)
  521. err = wlan->ops->wlan_scan_stop(wlan);
  522. break;
  523. }
  524. case RT_WLAN_CMD_GET_RSSI:
  525. {
  526. int *rssi = args;
  527. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI");
  528. if (wlan->ops->wlan_get_rssi)
  529. *rssi = wlan->ops->wlan_get_rssi(wlan);
  530. break;
  531. }
  532. case RT_WLAN_CMD_SET_POWERSAVE:
  533. {
  534. int level = *((int *)args);
  535. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_POWERSAVE, "RT_WLAN_CMD_SET_POWERSAVE");
  536. if (wlan->ops->wlan_set_powersave)
  537. wlan->ops->wlan_set_powersave(wlan, level);
  538. break;
  539. }
  540. case RT_WLAN_CMD_GET_POWERSAVE:
  541. {
  542. int *level = args;
  543. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_POWERSAVE, "RT_WLAN_CMD_GET_POWERSAVE");
  544. if (wlan->ops->wlan_get_powersave)
  545. *level = wlan->ops->wlan_get_powersave(wlan);
  546. break;
  547. }
  548. case RT_WLAN_CMD_CFG_PROMISC:
  549. {
  550. rt_bool_t start = *((rt_bool_t *)args);
  551. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC");
  552. if (wlan->ops->wlan_cfg_promisc)
  553. wlan->ops->wlan_cfg_promisc(wlan, start);
  554. break;
  555. }
  556. case RT_WLAN_CMD_CFG_FILTER:
  557. {
  558. struct rt_wlan_filter *filter = args;
  559. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER");
  560. if (wlan->ops->wlan_cfg_filter)
  561. wlan->ops->wlan_cfg_filter(wlan, filter);
  562. break;
  563. }
  564. case RT_WLAN_CMD_SET_CHANNEL:
  565. {
  566. int channel = *(int *)args;
  567. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL");
  568. if (wlan->ops->wlan_set_channel)
  569. wlan->ops->wlan_set_channel(wlan, channel);
  570. break;
  571. }
  572. case RT_WLAN_CMD_GET_CHANNEL:
  573. {
  574. int *channel = args;
  575. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL");
  576. if (wlan->ops->wlan_get_channel)
  577. *channel = wlan->ops->wlan_get_channel(wlan);
  578. break;
  579. }
  580. case RT_WLAN_CMD_SET_COUNTRY:
  581. {
  582. rt_country_code_t country = *(rt_country_code_t *)args;
  583. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY");
  584. if (wlan->ops->wlan_set_country)
  585. wlan->ops->wlan_set_country(wlan, country);
  586. break;
  587. }
  588. case RT_WLAN_CMD_GET_COUNTRY:
  589. {
  590. rt_country_code_t *country = args;
  591. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY");
  592. if (wlan->ops->wlan_get_country)
  593. *country = wlan->ops->wlan_get_country(wlan);
  594. break;
  595. }
  596. case RT_WLAN_CMD_SET_MAC:
  597. {
  598. rt_uint8_t *mac = args;
  599. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC");
  600. if (wlan->ops->wlan_set_mac)
  601. wlan->ops->wlan_set_mac(wlan, mac);
  602. break;
  603. }
  604. case RT_WLAN_CMD_GET_MAC:
  605. {
  606. rt_uint8_t *mac = args;
  607. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC");
  608. if (wlan->ops->wlan_get_mac)
  609. wlan->ops->wlan_get_mac(wlan, mac);
  610. break;
  611. }
  612. default:
  613. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN");
  614. break;
  615. }
  616. WLAN_DEV_UNLOCK(wlan);
  617. return err;
  618. }
  619. struct rt_wlan_device *rt_wlan_dev_register(const char *name, const struct rt_wlan_dev_ops *ops, rt_uint32_t flag, void *user_data)
  620. {
  621. struct rt_wlan_device *wlan;
  622. if (name == RT_NULL || ops == RT_NULL)
  623. {
  624. LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__);
  625. return RT_NULL;
  626. }
  627. wlan = rt_malloc(sizeof(struct rt_wlan_device));
  628. if (wlan == RT_NULL)
  629. {
  630. LOG_E("F:%s L:%d", __FUNCTION__, __LINE__);
  631. return RT_NULL;
  632. }
  633. rt_memset(wlan, 0, sizeof(struct rt_wlan_device));
  634. wlan->device.init = _rt_wlan_dev_init;
  635. wlan->device.open = RT_NULL;
  636. wlan->device.close = RT_NULL;
  637. wlan->device.read = RT_NULL;
  638. wlan->device.write = RT_NULL;
  639. wlan->device.control = _rt_wlan_dev_control;
  640. wlan->device.user_data = RT_NULL;
  641. wlan->device.type = RT_Device_Class_NetIf;
  642. wlan->ops = ops;
  643. wlan->user_data = user_data;
  644. wlan->flags = flag;
  645. rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR);
  646. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  647. return wlan;
  648. }