wlan_lwip.c 14 KB

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