wlan_dev.c 20 KB

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