wlan_lwip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. #include <wlan_workqueue.h>
  15. #ifdef RT_USING_LWIP
  16. #include <netif/ethernetif.h>
  17. #include <lwip/netifapi.h>
  18. #ifdef LWIP_USING_DHCPD
  19. #include <dhcp_server.h>
  20. #endif
  21. #define DBG_ENABLE
  22. #ifdef RT_WLAN_LWIP_DEBUG
  23. #define DBG_LEVEL DBG_LOG
  24. #else
  25. #define DBG_LEVEL DBG_INFO
  26. #endif
  27. #define DBG_SECTION_NAME "WLAN.lwip"
  28. #define DBG_COLOR
  29. #include <rtdbg.h>
  30. struct lwip_prot_des
  31. {
  32. struct rt_wlan_prot prot;
  33. struct eth_device eth;
  34. rt_int8_t connected_flag;
  35. struct rt_timer timer;
  36. struct rt_work work;
  37. };
  38. static void netif_is_ready(struct rt_work *work, void *parameter)
  39. {
  40. ip_addr_t ip_addr_zero = { 0 };
  41. struct rt_wlan_device *wlan = parameter;
  42. struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot;
  43. struct eth_device *eth_dev = &lwip_prot->eth;
  44. char str[IP4ADDR_STRLEN_MAX];
  45. rt_base_t level;
  46. struct rt_wlan_buff buff;
  47. rt_uint32_t ip_addr[4];
  48. rt_timer_stop(&lwip_prot->timer);
  49. if (ip_addr_cmp(&(eth_dev->netif->ip_addr), &ip_addr_zero) != 0)
  50. {
  51. rt_timer_start(&lwip_prot->timer);
  52. goto exit;
  53. }
  54. rt_memset(&ip_addr, 0, sizeof(ip_addr));
  55. #if LWIP_IPV4 && LWIP_IPV6
  56. if (eth_dev->netif->ip_addr.type == IPADDR_TYPE_V4)
  57. {
  58. ip_addr[0] = ip4_addr_get_u32(&eth_dev->netif->ip_addr.u_addr.ip4);
  59. buff.data = &ip_addr[0];
  60. buff.len = sizeof(ip_addr[0]);
  61. }
  62. else if (eth_dev->netif->ip_addr.type == IPADDR_TYPE_V6)
  63. {
  64. *(ip6_addr_t *)(&ip_addr[0]) = eth_dev->netif->ip_addr.u_addr.ip6;
  65. buff.data = ip_addr;
  66. buff.len = sizeof(ip_addr);
  67. }
  68. else
  69. {
  70. LOG_W("F:%s L:%d ip addr type not support", __FUNCTION__, __LINE__);
  71. }
  72. #else
  73. #if LWIP_IPV4
  74. ip_addr[0] = ip4_addr_get_u32(&eth_dev->netif->ip_addr);
  75. buff.data = &ip_addr[0];
  76. buff.len = sizeof(ip_addr[0]);
  77. #else
  78. *(ip_addr_t *)(&ip_addr[0]) = eth_dev->netif->ip_addr;
  79. buff.data = ip_addr;
  80. buff.len = sizeof(ip_addr);
  81. #endif
  82. #endif
  83. if (rt_wlan_prot_ready(wlan, &buff) != 0)
  84. {
  85. rt_timer_start(&lwip_prot->timer);
  86. goto exit;
  87. }
  88. rt_memset(str, 0, IP4ADDR_STRLEN_MAX);
  89. rt_enter_critical();
  90. rt_memcpy(str, ipaddr_ntoa(&(eth_dev->netif->ip_addr)), IP4ADDR_STRLEN_MAX);
  91. rt_exit_critical();
  92. LOG_I("Got IP address : %s", str);
  93. exit:
  94. level = rt_hw_interrupt_disable();
  95. rt_memset(work, 0, sizeof(struct rt_work));
  96. rt_hw_interrupt_enable(level);
  97. }
  98. static void timer_callback(void *parameter)
  99. {
  100. struct rt_workqueue *workqueue;
  101. struct rt_wlan_device *wlan = parameter;
  102. struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot;
  103. struct rt_work *work = &lwip_prot->work;
  104. rt_base_t level;
  105. workqueue = rt_wlan_get_workqueue();
  106. if (workqueue != RT_NULL)
  107. {
  108. level = rt_hw_interrupt_disable();
  109. rt_work_init(work, netif_is_ready, parameter);
  110. rt_hw_interrupt_enable(level);
  111. if (rt_workqueue_dowork(workqueue, work) != RT_EOK)
  112. {
  113. level = rt_hw_interrupt_disable();
  114. rt_memset(work, 0, sizeof(struct rt_work));
  115. rt_hw_interrupt_enable(level);
  116. }
  117. }
  118. }
  119. static void netif_set_connected(void *parameter)
  120. {
  121. struct rt_wlan_device *wlan = parameter;
  122. struct lwip_prot_des *lwip_prot = wlan->prot;
  123. struct eth_device *eth_dev = &lwip_prot->eth;
  124. if (lwip_prot->connected_flag)
  125. {
  126. if (wlan->mode == RT_WLAN_STATION)
  127. {
  128. LOG_D("F:%s L:%d dhcp start run", __FUNCTION__, __LINE__);
  129. netifapi_netif_set_link_up(eth_dev->netif);
  130. #ifdef RT_LWIP_DHCP
  131. dhcp_start(eth_dev->netif);
  132. #endif
  133. rt_timer_start(&lwip_prot->timer);
  134. }
  135. else if (wlan->mode == RT_WLAN_AP)
  136. {
  137. LOG_D("F:%s L:%d dhcpd start run", __FUNCTION__, __LINE__);
  138. netifapi_netif_set_link_up(eth_dev->netif);
  139. #ifdef LWIP_USING_DHCPD
  140. {
  141. char netif_name[8];
  142. int i;
  143. rt_memset(netif_name, 0, sizeof(netif_name));
  144. for (i = 0; i < sizeof(eth_dev->netif->name); i++)
  145. {
  146. netif_name[i] = eth_dev->netif->name[i];
  147. }
  148. dhcpd_start(netif_name);
  149. }
  150. #endif
  151. }
  152. }
  153. else
  154. {
  155. if (wlan->mode == RT_WLAN_STATION)
  156. {
  157. LOG_D("F:%s L:%d dhcp stop run", __FUNCTION__, __LINE__);
  158. netifapi_netif_set_link_down(eth_dev->netif);
  159. #ifdef RT_LWIP_DHCP
  160. {
  161. ip4_addr_t ip_addr = { 0 };
  162. dhcp_stop(eth_dev->netif);
  163. netif_set_addr(eth_dev->netif, &ip_addr, &ip_addr, &ip_addr);
  164. }
  165. #endif
  166. rt_timer_stop(&lwip_prot->timer);
  167. }
  168. else if (wlan->mode == RT_WLAN_AP)
  169. {
  170. LOG_D("F:%s L:%d dhcpd stop run", __FUNCTION__, __LINE__);
  171. netifapi_netif_set_link_down(eth_dev->netif);
  172. }
  173. }
  174. }
  175. static void rt_wlan_lwip_event_handle(struct rt_wlan_prot *port, struct rt_wlan_device *wlan, int event)
  176. {
  177. struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot;
  178. rt_bool_t flag_old;
  179. flag_old = lwip_prot->connected_flag;
  180. switch (event)
  181. {
  182. case RT_WLAN_PROT_EVT_CONNECT:
  183. {
  184. LOG_D("event: CONNECT");
  185. lwip_prot->connected_flag = RT_TRUE;
  186. break;
  187. }
  188. case RT_WLAN_PROT_EVT_DISCONNECT:
  189. {
  190. LOG_D("event: DISCONNECT");
  191. lwip_prot->connected_flag = RT_FALSE;
  192. break;
  193. }
  194. case RT_WLAN_PROT_EVT_AP_START:
  195. {
  196. LOG_D("event: AP_START");
  197. lwip_prot->connected_flag = RT_TRUE;
  198. break;
  199. }
  200. case RT_WLAN_PROT_EVT_AP_STOP:
  201. {
  202. LOG_D("event: AP_STOP");
  203. lwip_prot->connected_flag = RT_FALSE;
  204. break;
  205. }
  206. case RT_WLAN_PROT_EVT_AP_ASSOCIATED:
  207. {
  208. LOG_D("event: ASSOCIATED");
  209. break;
  210. }
  211. case RT_WLAN_PROT_EVT_AP_DISASSOCIATED:
  212. {
  213. LOG_D("event: DISASSOCIATED");
  214. break;
  215. }
  216. default :
  217. {
  218. LOG_D("event: UNKNOWN");
  219. break;
  220. }
  221. }
  222. if (flag_old != lwip_prot->connected_flag)
  223. {
  224. rt_wlan_workqueue_dowork(netif_set_connected, wlan);
  225. // netif_set_connected(wlan);
  226. }
  227. }
  228. static rt_err_t rt_wlan_lwip_protocol_control(rt_device_t device, int cmd, void *args)
  229. {
  230. struct eth_device *eth_dev = (struct eth_device *)device;
  231. struct rt_wlan_device *wlan;
  232. rt_err_t err = RT_EOK;
  233. RT_ASSERT(eth_dev != RT_NULL);
  234. LOG_D("F:%s L:%d device:0x%08x user_data:0x%08x", __FUNCTION__, __LINE__, eth_dev, eth_dev->parent.user_data);
  235. switch (cmd)
  236. {
  237. case NIOCTL_GADDR:
  238. /* get MAC address */
  239. wlan = eth_dev->parent.user_data;
  240. err = rt_device_control((rt_device_t)wlan, RT_WLAN_CMD_GET_MAC, args);
  241. break;
  242. default :
  243. break;
  244. }
  245. return err;
  246. }
  247. static rt_err_t rt_wlan_lwip_protocol_recv(struct rt_wlan_device *wlan, void *buff, int len)
  248. {
  249. struct eth_device *eth_dev = &((struct lwip_prot_des *)wlan->prot)->eth;
  250. struct pbuf *p = RT_NULL;
  251. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  252. if (eth_dev == RT_NULL)
  253. {
  254. return -RT_ERROR;
  255. }
  256. #ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE
  257. {
  258. p = buff;
  259. if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK)
  260. {
  261. return -RT_ERROR;
  262. }
  263. return RT_EOK;
  264. }
  265. #else
  266. {
  267. int count = 0;
  268. while (p == RT_NULL)
  269. {
  270. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  271. if (p != RT_NULL)
  272. break;
  273. p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
  274. if (p != RT_NULL)
  275. break;
  276. LOG_D("F:%s L:%d wait for pbuf_alloc!", __FUNCTION__, __LINE__);
  277. rt_thread_delay(1);
  278. count++;
  279. //wait for 10ms or give up!!
  280. if (count >= 10)
  281. {
  282. LOG_W("F:%s L:%d pbuf allocate fail!!!", __FUNCTION__, __LINE__);
  283. return -RT_ENOMEM;
  284. }
  285. }
  286. /*copy data dat -> pbuf*/
  287. pbuf_take(p, buff, len);
  288. if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK)
  289. {
  290. LOG_D("F:%s L:%d IP input error", __FUNCTION__, __LINE__);
  291. pbuf_free(p);
  292. p = RT_NULL;
  293. }
  294. LOG_D("F:%s L:%d netif iput success! len:%d", __FUNCTION__, __LINE__, len);
  295. return RT_EOK;
  296. }
  297. #endif
  298. }
  299. static rt_err_t rt_wlan_lwip_protocol_send(rt_device_t device, struct pbuf *p)
  300. {
  301. struct rt_wlan_device *wlan = ((struct eth_device *)device)->parent.user_data;
  302. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  303. if (wlan == RT_NULL)
  304. {
  305. return RT_EOK;
  306. }
  307. #ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE
  308. {
  309. rt_wlan_prot_transfer_dev(wlan, p, p->tot_len);
  310. return RT_EOK;
  311. }
  312. #else
  313. {
  314. rt_uint8_t *frame;
  315. /* sending data directly */
  316. if (p->len == p->tot_len)
  317. {
  318. frame = (rt_uint8_t *)p->payload;
  319. rt_wlan_prot_transfer_dev(wlan, frame, p->tot_len);
  320. LOG_D("F:%s L:%d run len:%d", __FUNCTION__, __LINE__, p->tot_len);
  321. return RT_EOK;
  322. }
  323. frame = rt_malloc(p->tot_len);
  324. if (frame == RT_NULL)
  325. {
  326. LOG_E("F:%s L:%d malloc out_buf fail\n", __FUNCTION__, __LINE__);
  327. return -RT_ENOMEM;
  328. }
  329. /*copy pbuf -> data dat*/
  330. pbuf_copy_partial(p, frame, p->tot_len, 0);
  331. /* send data */
  332. rt_wlan_prot_transfer_dev(wlan, frame, p->tot_len);
  333. LOG_D("F:%s L:%d run len:%d", __FUNCTION__, __LINE__, p->tot_len);
  334. rt_free(frame);
  335. return RT_EOK;
  336. }
  337. #endif
  338. }
  339. static struct rt_wlan_prot *rt_wlan_lwip_protocol_register(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan)
  340. {
  341. struct eth_device *eth = RT_NULL;
  342. static rt_uint8_t id = 0;
  343. char eth_name[4], timer_name[16];
  344. rt_device_t device = RT_NULL;
  345. struct lwip_prot_des *lwip_prot;
  346. if (wlan == RT_NULL || prot == RT_NULL)
  347. return RT_NULL;;
  348. LOG_D("F:%s L:%d is run wlan:0x%08x", __FUNCTION__, __LINE__, wlan);
  349. do
  350. {
  351. /* find ETH device name */
  352. eth_name[0] = 'w';
  353. eth_name[1] = '0' + id++;
  354. eth_name[2] = '\0';
  355. device = rt_device_find(eth_name);
  356. }
  357. while (device);
  358. if (id > 9)
  359. {
  360. LOG_E("F:%s L:%d not find Empty name", __FUNCTION__, __LINE__, eth_name);
  361. return RT_NULL;
  362. }
  363. if (rt_device_open((rt_device_t)wlan, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  364. {
  365. LOG_E("F:%s L:%d open wlan failed", __FUNCTION__, __LINE__);
  366. return RT_NULL;
  367. }
  368. lwip_prot = rt_malloc(sizeof(struct lwip_prot_des));
  369. if (lwip_prot == RT_NULL)
  370. {
  371. LOG_E("F:%s L:%d malloc mem failed", __FUNCTION__, __LINE__);
  372. rt_device_close((rt_device_t)wlan);
  373. return RT_NULL;
  374. }
  375. rt_memset(lwip_prot, 0, sizeof(struct lwip_prot_des));
  376. eth = &lwip_prot->eth;
  377. eth->parent.init = RT_NULL;
  378. eth->parent.open = RT_NULL;
  379. eth->parent.close = RT_NULL;
  380. eth->parent.read = RT_NULL;
  381. eth->parent.write = RT_NULL;
  382. eth->parent.control = rt_wlan_lwip_protocol_control;
  383. eth->parent.user_data = wlan;
  384. eth->eth_rx = RT_NULL;
  385. eth->eth_tx = rt_wlan_lwip_protocol_send;
  386. /* register ETH device */
  387. if (eth_device_init(eth, eth_name) != RT_EOK)
  388. {
  389. LOG_E("eth device init failed");
  390. rt_device_close((rt_device_t)wlan);
  391. rt_free(lwip_prot);
  392. return RT_NULL;
  393. }
  394. rt_memcpy(&lwip_prot->prot, prot, sizeof(struct rt_wlan_prot));
  395. if (wlan->mode == RT_WLAN_STATION)
  396. {
  397. rt_sprintf(timer_name, "timer_%s", eth_name);
  398. rt_timer_init(&lwip_prot->timer, timer_name, timer_callback, wlan, rt_tick_from_millisecond(1000),
  399. RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT);
  400. }
  401. LOG_I("eth device init ok name:%s", eth_name);
  402. return &lwip_prot->prot;
  403. }
  404. static void rt_wlan_lwip_protocol_unregister(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan)
  405. {
  406. /*TODO*/
  407. LOG_D("F:%s L:%d is run wlan:0x%08x", __FUNCTION__, __LINE__, wlan);
  408. }
  409. static struct rt_wlan_prot_ops ops =
  410. {
  411. rt_wlan_lwip_protocol_recv,
  412. rt_wlan_lwip_protocol_register,
  413. rt_wlan_lwip_protocol_unregister
  414. };
  415. int rt_wlan_lwip_init(void)
  416. {
  417. static struct rt_wlan_prot prot;
  418. rt_wlan_prot_event_t event;
  419. rt_memset(&prot, 0, sizeof(prot));
  420. rt_strncpy(&prot.name[0], RT_WLAN_PROT_LWIP, RT_WLAN_PROT_NAME_LEN);
  421. prot.ops = &ops;
  422. if (rt_wlan_prot_regisetr(&prot) != RT_EOK)
  423. {
  424. LOG_E("F:%s L:%d protocol regisetr failed", __FUNCTION__, __LINE__);
  425. return -1;
  426. }
  427. for (event = RT_WLAN_PROT_EVT_INIT_DONE; event < RT_WLAN_PROT_EVT_MAX; event++)
  428. {
  429. rt_wlan_prot_event_register(&prot, event, rt_wlan_lwip_event_handle);
  430. }
  431. return 0;
  432. }
  433. INIT_PREV_EXPORT(rt_wlan_lwip_init);
  434. #endif