wlan_dev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. if (password != RT_NULL)
  127. {
  128. rt_memcpy(ap_info.key.val, password, password_len);
  129. }
  130. ap_info.key.len = password_len;
  131. ap_info.hidden = info->hidden;
  132. ap_info.channel = info->channel;
  133. ap_info.security = info->security;
  134. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SOFTAP, &ap_info);
  135. return result;
  136. }
  137. rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device)
  138. {
  139. rt_err_t result = RT_EOK;
  140. if (device == RT_NULL)
  141. {
  142. return -RT_EIO;
  143. }
  144. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_STOP, RT_NULL);
  145. return result;
  146. }
  147. rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6])
  148. {
  149. rt_err_t result = RT_EOK;
  150. if (device == RT_NULL)
  151. {
  152. return -RT_EIO;
  153. }
  154. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_DEAUTH, mac);
  155. return result;
  156. }
  157. int rt_wlan_dev_get_rssi(struct rt_wlan_device *device)
  158. {
  159. int rssi = 0;
  160. rt_err_t result = RT_EOK;
  161. if (device == RT_NULL)
  162. {
  163. rt_set_errno(-RT_EIO);
  164. return 0;
  165. }
  166. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_RSSI, &rssi);
  167. if (result != RT_EOK)
  168. {
  169. rt_set_errno(result);
  170. return 0;
  171. }
  172. return rssi;
  173. }
  174. rt_err_t rt_wlan_dev_get_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_GET_MAC, &mac[0]);
  182. return result;
  183. }
  184. rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6])
  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_MAC, &mac[0]);
  192. return result;
  193. }
  194. rt_err_t rt_wlan_dev_set_powersave(struct rt_wlan_device *device, int level)
  195. {
  196. rt_err_t result = RT_EOK;
  197. if (device == RT_NULL)
  198. {
  199. return -RT_EIO;
  200. }
  201. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_POWERSAVE, &level);
  202. return result;
  203. }
  204. int rt_wlan_dev_get_powersave(struct rt_wlan_device *device)
  205. {
  206. int level = -1;
  207. rt_err_t result = RT_EOK;
  208. if (device == RT_NULL)
  209. {
  210. rt_set_errno(-RT_EIO);
  211. return -1;
  212. }
  213. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_POWERSAVE, &level);
  214. if (result != RT_EOK)
  215. {
  216. rt_set_errno(result);
  217. }
  218. return level;
  219. }
  220. 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)
  221. {
  222. int i = 0;
  223. rt_base_t level;
  224. if (device == RT_NULL)
  225. {
  226. return -RT_EIO;
  227. }
  228. if (event >= RT_WLAN_DEV_EVT_MAX)
  229. {
  230. return -RT_EINVAL;
  231. }
  232. level = rt_hw_interrupt_disable();
  233. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  234. {
  235. if (device->handler_table[event][i].handler == RT_NULL)
  236. {
  237. device->handler_table[event][i].handler = handler;
  238. device->handler_table[event][i].parameter = parameter;
  239. rt_hw_interrupt_enable(level);
  240. return RT_EOK;
  241. }
  242. }
  243. rt_hw_interrupt_enable(level);
  244. /* No space found */
  245. return -RT_ERROR;
  246. }
  247. 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)
  248. {
  249. int i = 0;
  250. rt_base_t level;
  251. if (device == RT_NULL)
  252. {
  253. return -RT_EIO;
  254. }
  255. if (event >= RT_WLAN_DEV_EVT_MAX)
  256. {
  257. return -RT_EINVAL;
  258. }
  259. level = rt_hw_interrupt_disable();
  260. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  261. {
  262. if (device->handler_table[event][i].handler == handler)
  263. {
  264. rt_memset(&device->handler_table[event][i], 0, sizeof(struct rt_wlan_dev_event_desc));
  265. rt_hw_interrupt_enable(level);
  266. return RT_EOK;
  267. }
  268. }
  269. rt_hw_interrupt_enable(level);
  270. /* not find iteam */
  271. return -RT_ERROR;
  272. }
  273. void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff)
  274. {
  275. void *parameter[RT_WLAN_DEV_EVENT_NUM];
  276. rt_wlan_dev_event_handler handler[RT_WLAN_DEV_EVENT_NUM];
  277. int i;
  278. rt_base_t level;
  279. if (device == RT_NULL)
  280. {
  281. return;
  282. }
  283. if (event >= RT_WLAN_DEV_EVT_MAX)
  284. {
  285. return;
  286. }
  287. /* get callback handle */
  288. level = rt_hw_interrupt_disable();
  289. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  290. {
  291. handler[i] = device->handler_table[event][i].handler;
  292. parameter[i] = device->handler_table[event][i].parameter;
  293. }
  294. rt_hw_interrupt_enable(level);
  295. /* run callback */
  296. for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++)
  297. {
  298. if (handler[i] != RT_NULL)
  299. {
  300. handler[i](device, event, buff, parameter[i]);
  301. }
  302. }
  303. }
  304. rt_err_t rt_wlan_dev_enter_promisc(struct rt_wlan_device *device)
  305. {
  306. rt_err_t result = RT_EOK;
  307. int enable = 1;
  308. if (device == RT_NULL)
  309. {
  310. return -RT_EIO;
  311. }
  312. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
  313. return result;
  314. }
  315. rt_err_t rt_wlan_dev_exit_promisc(struct rt_wlan_device *device)
  316. {
  317. rt_err_t result = RT_EOK;
  318. int enable = 0;
  319. if (device == RT_NULL)
  320. {
  321. return -RT_EIO;
  322. }
  323. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable);
  324. return result;
  325. }
  326. rt_err_t rt_wlan_dev_set_promisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback)
  327. {
  328. if (device == RT_NULL)
  329. {
  330. return -RT_EIO;
  331. }
  332. device->pormisc_callback = callback;
  333. return RT_EOK;
  334. }
  335. void rt_wlan_dev_promisc_handler(struct rt_wlan_device *device, void *data, int len)
  336. {
  337. rt_wlan_pormisc_callback_t callback;
  338. if (device == RT_NULL)
  339. {
  340. return;
  341. }
  342. callback = device->pormisc_callback;
  343. if (callback != RT_NULL)
  344. {
  345. callback(device, data, len);
  346. }
  347. }
  348. rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter)
  349. {
  350. rt_err_t result = RT_EOK;
  351. if (device == RT_NULL)
  352. {
  353. return -RT_EIO;
  354. }
  355. if (filter == RT_NULL)
  356. {
  357. return -RT_ERROR;
  358. }
  359. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_FILTER, filter);
  360. return result;
  361. }
  362. rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel)
  363. {
  364. rt_err_t result = RT_EOK;
  365. if (device == RT_NULL)
  366. {
  367. return -RT_EIO;
  368. }
  369. if (channel < 0)
  370. {
  371. return -RT_ERROR;
  372. }
  373. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_CHANNEL, &channel);
  374. return result;
  375. }
  376. int rt_wlan_dev_get_channel(struct rt_wlan_device *device)
  377. {
  378. rt_err_t result = RT_EOK;
  379. int channel = -1;
  380. if (device == RT_NULL)
  381. {
  382. rt_set_errno(-RT_EIO);
  383. return -1;
  384. }
  385. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_CHANNEL, &channel);
  386. if (result != RT_EOK)
  387. {
  388. rt_set_errno(result);
  389. return -1;
  390. }
  391. return channel;
  392. }
  393. rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code)
  394. {
  395. int result = RT_EOK;
  396. if (device == RT_NULL)
  397. {
  398. return -RT_EIO;
  399. }
  400. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_COUNTRY, &country_code);
  401. return result;
  402. }
  403. rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device)
  404. {
  405. int result = RT_EOK;
  406. rt_country_code_t country_code = RT_COUNTRY_UNKNOWN;
  407. if (device == RT_NULL)
  408. {
  409. rt_set_errno(-RT_EIO);
  410. return RT_COUNTRY_UNKNOWN;
  411. }
  412. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_COUNTRY, &country_code);
  413. if (result != RT_EOK)
  414. {
  415. rt_set_errno(result);
  416. return RT_COUNTRY_UNKNOWN;
  417. }
  418. return country_code;
  419. }
  420. rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info)
  421. {
  422. struct rt_scan_info scan_info = { 0 };
  423. struct rt_scan_info *p_scan_info = RT_NULL;
  424. rt_err_t result = 0;
  425. if (device == RT_NULL)
  426. {
  427. return -RT_EIO;
  428. }
  429. if (info != RT_NULL)
  430. {
  431. if (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH)
  432. {
  433. LOG_E("L:%d ssid is to long", __LINE__);
  434. return -RT_EINVAL;
  435. }
  436. rt_memcpy(&scan_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t));
  437. rt_memcpy(scan_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH);
  438. scan_info.channel_min = -1;
  439. scan_info.channel_max = -1;
  440. p_scan_info = &scan_info;
  441. }
  442. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN, p_scan_info);
  443. return result;
  444. }
  445. rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device)
  446. {
  447. rt_err_t result = 0;
  448. if (device == RT_NULL)
  449. {
  450. return -RT_EIO;
  451. }
  452. result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN_STOP, RT_NULL);
  453. return result;
  454. }
  455. rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len)
  456. {
  457. return rt_wlan_dev_transfer_prot(device, buff, len);
  458. }
  459. static rt_err_t _rt_wlan_dev_init(rt_device_t dev)
  460. {
  461. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  462. rt_err_t result = RT_EOK;
  463. rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_FIFO);
  464. if (wlan->ops->wlan_init)
  465. result = wlan->ops->wlan_init(wlan);
  466. if (result == RT_EOK)
  467. {
  468. LOG_I("wlan init success");
  469. }
  470. else
  471. {
  472. LOG_I("wlan init failed");
  473. }
  474. return result;
  475. }
  476. static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args)
  477. {
  478. struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
  479. rt_err_t err = RT_EOK;
  480. RT_ASSERT(dev != RT_NULL);
  481. WLAN_DEV_LOCK(wlan);
  482. switch (cmd)
  483. {
  484. case RT_WLAN_CMD_MODE:
  485. {
  486. rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args);
  487. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE");
  488. if (wlan->ops->wlan_mode)
  489. err = wlan->ops->wlan_mode(wlan, mode);
  490. break;
  491. }
  492. case RT_WLAN_CMD_SCAN:
  493. {
  494. struct rt_scan_info *scan_info = args;
  495. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN");
  496. if (wlan->ops->wlan_scan)
  497. err = wlan->ops->wlan_scan(wlan, scan_info);
  498. break;
  499. }
  500. case RT_WLAN_CMD_JOIN:
  501. {
  502. struct rt_sta_info *sta_info = args;
  503. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN");
  504. if (wlan->ops->wlan_join)
  505. err = wlan->ops->wlan_join(wlan, sta_info);
  506. break;
  507. }
  508. case RT_WLAN_CMD_SOFTAP:
  509. {
  510. struct rt_ap_info *ap_info = args;
  511. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP");
  512. if (wlan->ops->wlan_softap)
  513. err = wlan->ops->wlan_softap(wlan, ap_info);
  514. break;
  515. }
  516. case RT_WLAN_CMD_DISCONNECT:
  517. {
  518. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT");
  519. if (wlan->ops->wlan_disconnect)
  520. err = wlan->ops->wlan_disconnect(wlan);
  521. break;
  522. }
  523. case RT_WLAN_CMD_AP_STOP:
  524. {
  525. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP");
  526. if (wlan->ops->wlan_ap_stop)
  527. err = wlan->ops->wlan_ap_stop(wlan);
  528. break;
  529. }
  530. case RT_WLAN_CMD_AP_DEAUTH:
  531. {
  532. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH");
  533. if (wlan->ops->wlan_ap_deauth)
  534. err = wlan->ops->wlan_ap_deauth(wlan, args);
  535. break;
  536. }
  537. case RT_WLAN_CMD_SCAN_STOP:
  538. {
  539. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP");
  540. if (wlan->ops->wlan_scan_stop)
  541. err = wlan->ops->wlan_scan_stop(wlan);
  542. break;
  543. }
  544. case RT_WLAN_CMD_GET_RSSI:
  545. {
  546. int *rssi = args;
  547. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI");
  548. if (wlan->ops->wlan_get_rssi)
  549. *rssi = wlan->ops->wlan_get_rssi(wlan);
  550. break;
  551. }
  552. case RT_WLAN_CMD_SET_POWERSAVE:
  553. {
  554. int level = *((int *)args);
  555. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_POWERSAVE, "RT_WLAN_CMD_SET_POWERSAVE");
  556. if (wlan->ops->wlan_set_powersave)
  557. err = wlan->ops->wlan_set_powersave(wlan, level);
  558. break;
  559. }
  560. case RT_WLAN_CMD_GET_POWERSAVE:
  561. {
  562. int *level = args;
  563. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_POWERSAVE, "RT_WLAN_CMD_GET_POWERSAVE");
  564. if (wlan->ops->wlan_get_powersave)
  565. *level = wlan->ops->wlan_get_powersave(wlan);
  566. break;
  567. }
  568. case RT_WLAN_CMD_CFG_PROMISC:
  569. {
  570. rt_bool_t start = *((rt_bool_t *)args);
  571. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC");
  572. if (wlan->ops->wlan_cfg_promisc)
  573. err = wlan->ops->wlan_cfg_promisc(wlan, start);
  574. break;
  575. }
  576. case RT_WLAN_CMD_CFG_FILTER:
  577. {
  578. struct rt_wlan_filter *filter = args;
  579. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER");
  580. if (wlan->ops->wlan_cfg_filter)
  581. err = wlan->ops->wlan_cfg_filter(wlan, filter);
  582. break;
  583. }
  584. case RT_WLAN_CMD_SET_CHANNEL:
  585. {
  586. int channel = *(int *)args;
  587. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL");
  588. if (wlan->ops->wlan_set_channel)
  589. err = wlan->ops->wlan_set_channel(wlan, channel);
  590. break;
  591. }
  592. case RT_WLAN_CMD_GET_CHANNEL:
  593. {
  594. int *channel = args;
  595. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL");
  596. if (wlan->ops->wlan_get_channel)
  597. *channel = wlan->ops->wlan_get_channel(wlan);
  598. break;
  599. }
  600. case RT_WLAN_CMD_SET_COUNTRY:
  601. {
  602. rt_country_code_t country = *(rt_country_code_t *)args;
  603. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY");
  604. if (wlan->ops->wlan_set_country)
  605. err = wlan->ops->wlan_set_country(wlan, country);
  606. break;
  607. }
  608. case RT_WLAN_CMD_GET_COUNTRY:
  609. {
  610. rt_country_code_t *country = args;
  611. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY");
  612. if (wlan->ops->wlan_get_country)
  613. *country = wlan->ops->wlan_get_country(wlan);
  614. break;
  615. }
  616. case RT_WLAN_CMD_SET_MAC:
  617. {
  618. rt_uint8_t *mac = args;
  619. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC");
  620. if (wlan->ops->wlan_set_mac)
  621. err = wlan->ops->wlan_set_mac(wlan, mac);
  622. break;
  623. }
  624. case RT_WLAN_CMD_GET_MAC:
  625. {
  626. rt_uint8_t *mac = args;
  627. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC");
  628. if (wlan->ops->wlan_get_mac)
  629. err = wlan->ops->wlan_get_mac(wlan, mac);
  630. break;
  631. }
  632. default:
  633. LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN");
  634. break;
  635. }
  636. WLAN_DEV_UNLOCK(wlan);
  637. return err;
  638. }
  639. #ifdef RT_USING_DEVICE_OPS
  640. const static struct rt_device_ops wlan_ops =
  641. {
  642. _rt_wlan_dev_init,
  643. RT_NULL,
  644. RT_NULL,
  645. RT_NULL,
  646. RT_NULL,
  647. _rt_wlan_dev_control
  648. };
  649. #endif
  650. rt_err_t rt_wlan_dev_register(struct rt_wlan_device *wlan, const char *name, const struct rt_wlan_dev_ops *ops, rt_uint32_t flag, void *user_data)
  651. {
  652. rt_err_t err = RT_EOK;
  653. if ((wlan == RT_NULL) || (name == RT_NULL) || (ops == RT_NULL))
  654. {
  655. LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__);
  656. return RT_NULL;
  657. }
  658. rt_memset(wlan, 0, sizeof(struct rt_wlan_device));
  659. #ifdef RT_USING_DEVICE_OPS
  660. wlan->device.ops = &wlan_ops;
  661. #else
  662. wlan->device.init = _rt_wlan_dev_init;
  663. wlan->device.open = RT_NULL;
  664. wlan->device.close = RT_NULL;
  665. wlan->device.read = RT_NULL;
  666. wlan->device.write = RT_NULL;
  667. wlan->device.control = _rt_wlan_dev_control;
  668. #endif
  669. wlan->device.user_data = RT_NULL;
  670. wlan->device.type = RT_Device_Class_NetIf;
  671. wlan->ops = ops;
  672. wlan->user_data = user_data;
  673. wlan->flags = flag;
  674. err = rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR);
  675. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  676. return err;
  677. }