wlan_prot.c 8.6 KB

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