wlan_lwip.c 13 KB

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