wlan_prot.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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-14 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_PROT_DEBUG
  16. #define DBG_LEVEL DBG_LOG
  17. #else
  18. #define DBG_LEVEL DBG_INFO
  19. #endif
  20. #define DBG_SECTION_NAME "WLAN.prot"
  21. #define DBG_COLOR
  22. #include <rtdbg.h>
  23. #if RT_WLAN_PROT_NAME_LEN < 4
  24. #error "The name is too short"
  25. #endif
  26. struct rt_wlan_prot_event_des
  27. {
  28. rt_wlan_prot_event_handler handler;
  29. struct rt_wlan_prot *prot;
  30. };
  31. static struct rt_wlan_prot *_prot[RT_WLAN_PROT_MAX];
  32. static struct rt_wlan_prot_event_des prot_event_tab[RT_WLAN_PROT_EVT_MAX][RT_WLAN_PROT_MAX];
  33. static void rt_wlan_prot_event_handle(struct rt_wlan_device *wlan, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff, void *parameter)
  34. {
  35. int i;
  36. struct rt_wlan_prot *wlan_prot;
  37. struct rt_wlan_prot *prot;
  38. rt_wlan_prot_event_handler handler;
  39. rt_wlan_prot_event_t prot_event;
  40. LOG_D("F:%s L:%d event:%d", __FUNCTION__, __LINE__, event);
  41. wlan_prot = wlan->prot;
  42. handler = RT_NULL;
  43. prot = RT_NULL;
  44. switch (event)
  45. {
  46. case RT_WLAN_DEV_EVT_INIT_DONE:
  47. {
  48. LOG_D("L%d event: INIT_DONE", __LINE__);
  49. prot_event = RT_WLAN_PROT_EVT_INIT_DONE;
  50. break;
  51. }
  52. case RT_WLAN_DEV_EVT_CONNECT:
  53. {
  54. LOG_D("L%d event: CONNECT", __LINE__);
  55. prot_event = RT_WLAN_PROT_EVT_CONNECT;
  56. break;
  57. }
  58. case RT_WLAN_DEV_EVT_DISCONNECT:
  59. {
  60. LOG_D("L%d event: DISCONNECT", __LINE__);
  61. prot_event = RT_WLAN_PROT_EVT_DISCONNECT;
  62. break;
  63. }
  64. case RT_WLAN_DEV_EVT_AP_START:
  65. {
  66. LOG_D("L%d event: AP_START", __LINE__);
  67. prot_event = RT_WLAN_PROT_EVT_AP_START;
  68. break;
  69. }
  70. case RT_WLAN_DEV_EVT_AP_STOP:
  71. {
  72. LOG_D("L%d event: AP_STOP", __LINE__);
  73. prot_event = RT_WLAN_PROT_EVT_AP_STOP;
  74. break;
  75. }
  76. case RT_WLAN_DEV_EVT_AP_ASSOCIATED:
  77. {
  78. LOG_D("L%d event: AP_ASSOCIATED", __LINE__);
  79. prot_event = RT_WLAN_PROT_EVT_AP_ASSOCIATED;
  80. break;
  81. }
  82. case RT_WLAN_DEV_EVT_AP_DISASSOCIATED:
  83. {
  84. LOG_D("L%d event: AP_DISASSOCIATED", __LINE__);
  85. prot_event = RT_WLAN_PROT_EVT_AP_DISASSOCIATED;
  86. break;
  87. }
  88. default:
  89. {
  90. return;
  91. }
  92. }
  93. for (i = 0; i < RT_WLAN_PROT_MAX; i++)
  94. {
  95. if ((prot_event_tab[prot_event][i].handler != RT_NULL) &&
  96. (prot_event_tab[prot_event][i].prot->id == wlan_prot->id))
  97. {
  98. handler = prot_event_tab[prot_event][i].handler;
  99. prot = prot_event_tab[prot_event][i].prot;
  100. break;
  101. }
  102. }
  103. if (handler != RT_NULL)
  104. {
  105. handler(prot, wlan, prot_event);
  106. }
  107. }
  108. static struct rt_wlan_device *rt_wlan_prot_find_by_name(const char *name)
  109. {
  110. rt_device_t device;
  111. if (name == RT_NULL)
  112. {
  113. LOG_E("F:%s L:%d Parameter Wrongful", __FUNCTION__, __LINE__);
  114. return RT_NULL;
  115. }
  116. device = rt_device_find(name);
  117. if (device == RT_NULL)
  118. {
  119. LOG_E("F:%s L:%d not find wlan dev!! name:%s", __FUNCTION__, __LINE__, name);
  120. return RT_NULL;
  121. }
  122. return (struct rt_wlan_device *)device;
  123. }
  124. rt_err_t rt_wlan_prot_attach(const char *dev_name, const char *prot_name)
  125. {
  126. struct rt_wlan_device *wlan;
  127. wlan = rt_wlan_prot_find_by_name(dev_name);
  128. if (wlan == RT_NULL)
  129. {
  130. return -RT_ERROR;
  131. }
  132. return rt_wlan_prot_attach_dev(wlan, prot_name);
  133. }
  134. rt_err_t rt_wlan_prot_detach(const char *name)
  135. {
  136. struct rt_wlan_device *wlan;
  137. wlan = rt_wlan_prot_find_by_name(name);
  138. if (wlan == RT_NULL)
  139. {
  140. return -RT_ERROR;
  141. }
  142. return rt_wlan_prot_detach_dev(wlan);
  143. }
  144. rt_err_t rt_wlan_prot_attach_dev(struct rt_wlan_device *wlan, const char *prot_name)
  145. {
  146. int i = 0;
  147. struct rt_wlan_prot *prot = wlan->prot;
  148. rt_wlan_dev_event_t event;
  149. if (wlan == RT_NULL)
  150. {
  151. LOG_E("F:%s L:%d wlan is null", __FUNCTION__, __LINE__);
  152. return -RT_ERROR;
  153. }
  154. if (prot != RT_NULL &&
  155. (rt_strcmp(prot->name, prot_name) == 0))
  156. {
  157. LOG_D("prot is register");
  158. return RT_EOK;
  159. }
  160. /* if prot not NULL */
  161. if (prot != RT_NULL)
  162. rt_wlan_prot_detach_dev(wlan);
  163. #ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE
  164. if (rt_strcmp(RT_WLAN_PROT_LWIP, prot_name) != 0)
  165. {
  166. return -RT_ERROR;
  167. }
  168. #endif
  169. /* find prot */
  170. for (i = 0; i < RT_WLAN_PROT_MAX; i++)
  171. {
  172. if ((_prot[i] != RT_NULL) && (rt_strcmp(_prot[i]->name, prot_name) == 0))
  173. {
  174. /* attach prot */
  175. wlan->prot = _prot[i]->ops->dev_reg_callback(_prot[i], wlan);
  176. break;
  177. }
  178. }
  179. if (i >= RT_WLAN_PROT_MAX)
  180. {
  181. LOG_E("F:%s L:%d not find wlan protocol", __FUNCTION__, __LINE__);
  182. return -RT_ERROR;
  183. }
  184. for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event ++)
  185. {
  186. if (rt_wlan_dev_register_event_handler(wlan, event, rt_wlan_prot_event_handle, RT_NULL) != RT_EOK)
  187. {
  188. LOG_E("prot register event filed:%d", event);
  189. }
  190. }
  191. return RT_EOK;
  192. }
  193. rt_err_t rt_wlan_prot_detach_dev(struct rt_wlan_device *wlan)
  194. {
  195. struct rt_wlan_prot *prot = wlan->prot;
  196. rt_wlan_dev_event_t event;
  197. if (prot == RT_NULL)
  198. return RT_EOK;
  199. for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event ++)
  200. {
  201. rt_wlan_dev_unregister_event_handler(wlan, event, rt_wlan_prot_event_handle);
  202. }
  203. /* detach prot */
  204. prot->ops->dev_unreg_callback(prot, wlan);
  205. wlan->prot = RT_NULL;
  206. return RT_EOK;
  207. }
  208. rt_err_t rt_wlan_prot_regisetr(struct rt_wlan_prot *prot)
  209. {
  210. int i;
  211. rt_uint32_t id;
  212. static rt_uint8_t num;
  213. /* Parameter checking */
  214. if ((prot == RT_NULL) ||
  215. (prot->ops->prot_recv == RT_NULL) ||
  216. (prot->ops->dev_reg_callback == RT_NULL))
  217. {
  218. LOG_E("F:%s L:%d Parameter Wrongful", __FUNCTION__, __LINE__);
  219. return -RT_EINVAL;
  220. }
  221. /* save prot */
  222. for (i = 0; i < RT_WLAN_PROT_MAX; i++)
  223. {
  224. if (_prot[i] == RT_NULL)
  225. {
  226. id = (RT_LWAN_ID_PREFIX << 16) | num;
  227. prot->id = id;
  228. _prot[i] = prot;
  229. num ++;
  230. break;
  231. }
  232. else if (rt_strcmp(_prot[i]->name, prot->name) == 0)
  233. {
  234. break;
  235. }
  236. }
  237. /* is full */
  238. if (i >= RT_WLAN_PROT_MAX)
  239. {
  240. LOG_E("F:%s L:%d Space full", __FUNCTION__, __LINE__);
  241. return -RT_ERROR;
  242. }
  243. return RT_EOK;
  244. }
  245. rt_err_t rt_wlan_prot_event_register(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event, rt_wlan_prot_event_handler handler)
  246. {
  247. int i;
  248. if ((prot == RT_NULL) || (handler == RT_NULL))
  249. {
  250. return -RT_EINVAL;
  251. }
  252. for (i = 0; i < RT_WLAN_PROT_MAX; i++)
  253. {
  254. if (prot_event_tab[event][i].handler == RT_NULL)
  255. {
  256. prot_event_tab[event][i].handler = handler;
  257. prot_event_tab[event][i].prot = prot;
  258. return RT_EOK;
  259. }
  260. }
  261. return -RT_ERROR;
  262. }
  263. rt_err_t rt_wlan_prot_event_unregister(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event)
  264. {
  265. int i;
  266. if (prot == RT_NULL)
  267. {
  268. return -RT_EINVAL;
  269. }
  270. for (i = 0; i < RT_WLAN_PROT_MAX; i++)
  271. {
  272. if ((prot_event_tab[event][i].handler != RT_NULL) &&
  273. (prot_event_tab[event][i].prot == prot))
  274. {
  275. rt_memset(&prot_event_tab[event][i], 0, sizeof(struct rt_wlan_prot_event_des));
  276. return RT_EOK;
  277. }
  278. }
  279. return -RT_ERROR;
  280. }
  281. rt_err_t rt_wlan_prot_transfer_dev(struct rt_wlan_device *wlan, void *buff, int len)
  282. {
  283. if (wlan->ops->wlan_send != RT_NULL)
  284. {
  285. return wlan->ops->wlan_send(wlan, buff, len);
  286. }
  287. return -RT_ERROR;
  288. }
  289. rt_err_t rt_wlan_dev_transfer_prot(struct rt_wlan_device *wlan, void *buff, int len)
  290. {
  291. struct rt_wlan_prot *prot = wlan->prot;
  292. if (prot != RT_NULL)
  293. {
  294. return prot->ops->prot_recv(wlan, buff, len);
  295. }
  296. return -RT_ERROR;
  297. }
  298. extern int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff);
  299. int rt_wlan_prot_ready(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff)
  300. {
  301. return rt_wlan_prot_ready_event(wlan, buff);
  302. }
  303. void rt_wlan_prot_dump(void)
  304. {
  305. int i;
  306. rt_kprintf(" name id \n");
  307. rt_kprintf("-------- --------\n");
  308. for (i = 0; i < RT_WLAN_PROT_MAX; i++)
  309. {
  310. if (_prot[i] != RT_NULL)
  311. {
  312. rt_kprintf("%-8.8s ", _prot[i]->name);
  313. rt_kprintf("%08x\n", _prot[i]->id);
  314. }
  315. }
  316. }