wlan_lwip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. #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. 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[8];
  166. int i;
  167. rt_memset(netif_name, 0, sizeof(netif_name));
  168. for (i = 0; i < sizeof(eth_dev->netif->name); i++)
  169. {
  170. netif_name[i] = eth_dev->netif->name[i];
  171. }
  172. dhcpd_start(netif_name);
  173. }
  174. #endif
  175. }
  176. }
  177. else
  178. {
  179. if (wlan->mode == RT_WLAN_STATION)
  180. {
  181. LOG_D("F:%s L:%d dhcp stop run", __FUNCTION__, __LINE__);
  182. netifapi_netif_common(eth_dev->netif, netif_set_link_down, NULL);
  183. #ifdef RT_LWIP_DHCP
  184. {
  185. ip_addr_t ip_addr = { 0 };
  186. dhcp_stop(eth_dev->netif);
  187. netif_set_addr(eth_dev->netif, &ip_addr, &ip_addr, &ip_addr);
  188. }
  189. #endif
  190. rt_timer_stop(&lwip_prot->timer);
  191. }
  192. else if (wlan->mode == RT_WLAN_AP)
  193. {
  194. LOG_D("F:%s L:%d dhcpd stop run", __FUNCTION__, __LINE__);
  195. netifapi_netif_common(eth_dev->netif, netif_set_link_down, NULL);
  196. }
  197. }
  198. }
  199. static void rt_wlan_lwip_event_handle(struct rt_wlan_prot *port, struct rt_wlan_device *wlan, int event)
  200. {
  201. struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot;
  202. rt_bool_t flag_old;
  203. if (lwip_prot == RT_NULL)
  204. return;
  205. flag_old = lwip_prot->connected_flag;
  206. switch (event)
  207. {
  208. case RT_WLAN_PROT_EVT_CONNECT:
  209. {
  210. LOG_D("event: CONNECT");
  211. lwip_prot->connected_flag = RT_TRUE;
  212. break;
  213. }
  214. case RT_WLAN_PROT_EVT_DISCONNECT:
  215. {
  216. LOG_D("event: DISCONNECT");
  217. lwip_prot->connected_flag = RT_FALSE;
  218. break;
  219. }
  220. case RT_WLAN_PROT_EVT_AP_START:
  221. {
  222. LOG_D("event: AP_START");
  223. lwip_prot->connected_flag = RT_TRUE;
  224. break;
  225. }
  226. case RT_WLAN_PROT_EVT_AP_STOP:
  227. {
  228. LOG_D("event: AP_STOP");
  229. lwip_prot->connected_flag = RT_FALSE;
  230. break;
  231. }
  232. case RT_WLAN_PROT_EVT_AP_ASSOCIATED:
  233. {
  234. LOG_D("event: ASSOCIATED");
  235. break;
  236. }
  237. case RT_WLAN_PROT_EVT_AP_DISASSOCIATED:
  238. {
  239. LOG_D("event: DISASSOCIATED");
  240. break;
  241. }
  242. default :
  243. {
  244. LOG_D("event: UNKNOWN");
  245. break;
  246. }
  247. }
  248. if (flag_old != lwip_prot->connected_flag)
  249. {
  250. #ifdef RT_WLAN_WORK_THREAD_ENABLE
  251. rt_wlan_workqueue_dowork(netif_set_connected, wlan);
  252. #else
  253. netif_set_connected(wlan);
  254. #endif
  255. }
  256. }
  257. static rt_err_t rt_wlan_lwip_protocol_control(rt_device_t device, int cmd, void *args)
  258. {
  259. struct eth_device *eth_dev = (struct eth_device *)device;
  260. struct rt_wlan_device *wlan;
  261. rt_err_t err = RT_EOK;
  262. RT_ASSERT(eth_dev != RT_NULL);
  263. LOG_D("F:%s L:%d device:0x%08x user_data:0x%08x", __FUNCTION__, __LINE__, eth_dev, eth_dev->parent.user_data);
  264. switch (cmd)
  265. {
  266. case NIOCTL_GADDR:
  267. /* get MAC address */
  268. wlan = eth_dev->parent.user_data;
  269. err = rt_device_control((rt_device_t)wlan, RT_WLAN_CMD_GET_MAC, args);
  270. break;
  271. default :
  272. break;
  273. }
  274. return err;
  275. }
  276. static rt_err_t rt_wlan_lwip_protocol_recv(struct rt_wlan_device *wlan, void *buff, int len)
  277. {
  278. struct eth_device *eth_dev = &((struct lwip_prot_des *)wlan->prot)->eth;
  279. struct pbuf *p = RT_NULL;
  280. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  281. if (eth_dev == RT_NULL)
  282. {
  283. return -RT_ERROR;
  284. }
  285. #ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE
  286. {
  287. p = buff;
  288. if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK)
  289. {
  290. return -RT_ERROR;
  291. }
  292. return RT_EOK;
  293. }
  294. #else
  295. {
  296. int count = 0;
  297. while (p == RT_NULL)
  298. {
  299. p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
  300. if (p != RT_NULL)
  301. break;
  302. p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
  303. if (p != RT_NULL)
  304. break;
  305. LOG_D("F:%s L:%d wait for pbuf_alloc!", __FUNCTION__, __LINE__);
  306. rt_thread_delay(1);
  307. count++;
  308. //wait for 10ms or give up!!
  309. if (count >= 10)
  310. {
  311. LOG_W("F:%s L:%d pbuf allocate fail!!!", __FUNCTION__, __LINE__);
  312. return -RT_ENOMEM;
  313. }
  314. }
  315. /*copy data dat -> pbuf*/
  316. pbuf_take(p, buff, len);
  317. if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK)
  318. {
  319. LOG_D("F:%s L:%d IP input error", __FUNCTION__, __LINE__);
  320. pbuf_free(p);
  321. p = RT_NULL;
  322. }
  323. LOG_D("F:%s L:%d netif iput success! len:%d", __FUNCTION__, __LINE__, len);
  324. return RT_EOK;
  325. }
  326. #endif
  327. }
  328. static rt_err_t rt_wlan_lwip_protocol_send(rt_device_t device, struct pbuf *p)
  329. {
  330. struct rt_wlan_device *wlan = ((struct eth_device *)device)->parent.user_data;
  331. LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__);
  332. if (wlan == RT_NULL)
  333. {
  334. return RT_EOK;
  335. }
  336. #ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE
  337. {
  338. rt_wlan_prot_transfer_dev(wlan, p, p->tot_len);
  339. return RT_EOK;
  340. }
  341. #else
  342. {
  343. rt_uint8_t *frame;
  344. /* sending data directly */
  345. if (p->len == p->tot_len)
  346. {
  347. frame = (rt_uint8_t *)p->payload;
  348. rt_wlan_prot_transfer_dev(wlan, frame, p->tot_len);
  349. LOG_D("F:%s L:%d run len:%d", __FUNCTION__, __LINE__, p->tot_len);
  350. return RT_EOK;
  351. }
  352. frame = rt_malloc(p->tot_len);
  353. if (frame == RT_NULL)
  354. {
  355. LOG_E("F:%s L:%d malloc out_buf fail\n", __FUNCTION__, __LINE__);
  356. return -RT_ENOMEM;
  357. }
  358. /*copy pbuf -> data dat*/
  359. pbuf_copy_partial(p, frame, p->tot_len, 0);
  360. /* send data */
  361. rt_wlan_prot_transfer_dev(wlan, frame, p->tot_len);
  362. LOG_D("F:%s L:%d run len:%d", __FUNCTION__, __LINE__, p->tot_len);
  363. rt_free(frame);
  364. return RT_EOK;
  365. }
  366. #endif
  367. }
  368. #ifdef RT_USING_DEVICE_OPS
  369. const static struct rt_device_ops wlan_lwip_ops =
  370. {
  371. RT_NULL,
  372. RT_NULL,
  373. RT_NULL,
  374. RT_NULL,
  375. RT_NULL,
  376. rt_wlan_lwip_protocol_control
  377. };
  378. #endif
  379. static struct rt_wlan_prot *rt_wlan_lwip_protocol_register(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan)
  380. {
  381. struct eth_device *eth = RT_NULL;
  382. static rt_uint8_t id = 0;
  383. char eth_name[4], timer_name[16];
  384. rt_device_t device = RT_NULL;
  385. struct lwip_prot_des *lwip_prot;
  386. if (wlan == RT_NULL || prot == RT_NULL)
  387. return RT_NULL;;
  388. LOG_D("F:%s L:%d is run wlan:0x%08x", __FUNCTION__, __LINE__, wlan);
  389. do
  390. {
  391. /* find ETH device name */
  392. eth_name[0] = 'w';
  393. eth_name[1] = '0' + id++;
  394. eth_name[2] = '\0';
  395. device = rt_device_find(eth_name);
  396. }
  397. while (device);
  398. if (id > 9)
  399. {
  400. LOG_E("F:%s L:%d not find Empty name", __FUNCTION__, __LINE__, eth_name);
  401. return RT_NULL;
  402. }
  403. if (rt_device_open((rt_device_t)wlan, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  404. {
  405. LOG_E("F:%s L:%d open wlan failed", __FUNCTION__, __LINE__);
  406. return RT_NULL;
  407. }
  408. lwip_prot = rt_malloc(sizeof(struct lwip_prot_des));
  409. if (lwip_prot == RT_NULL)
  410. {
  411. LOG_E("F:%s L:%d malloc mem failed", __FUNCTION__, __LINE__);
  412. rt_device_close((rt_device_t)wlan);
  413. return RT_NULL;
  414. }
  415. rt_memset(lwip_prot, 0, sizeof(struct lwip_prot_des));
  416. eth = &lwip_prot->eth;
  417. #ifdef RT_USING_DEVICE_OPS
  418. eth->parent.ops = &wlan_lwip_ops;
  419. #else
  420. eth->parent.init = RT_NULL;
  421. eth->parent.open = RT_NULL;
  422. eth->parent.close = RT_NULL;
  423. eth->parent.read = RT_NULL;
  424. eth->parent.write = RT_NULL;
  425. eth->parent.control = rt_wlan_lwip_protocol_control;
  426. #endif
  427. eth->parent.user_data = wlan;
  428. eth->eth_rx = RT_NULL;
  429. eth->eth_tx = rt_wlan_lwip_protocol_send;
  430. /* register ETH device */
  431. if (eth_device_init(eth, eth_name) != RT_EOK)
  432. {
  433. LOG_E("eth device init failed");
  434. rt_device_close((rt_device_t)wlan);
  435. rt_free(lwip_prot);
  436. return RT_NULL;
  437. }
  438. rt_memcpy(&lwip_prot->prot, prot, sizeof(struct rt_wlan_prot));
  439. rt_sprintf(timer_name, "timer_%s", eth_name);
  440. rt_timer_init(&lwip_prot->timer, timer_name, timer_callback, wlan, rt_tick_from_millisecond(1000),
  441. RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT);
  442. netif_set_up(eth->netif);
  443. LOG_I("eth device init ok name:%s", eth_name);
  444. #ifdef RT_USING_NETDEV
  445. wlan->netdev = netdev_get_by_name(eth_name);
  446. #endif
  447. return &lwip_prot->prot;
  448. }
  449. static void rt_wlan_lwip_protocol_unregister(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan)
  450. {
  451. /*TODO*/
  452. LOG_D("F:%s L:%d is run wlan:0x%08x", __FUNCTION__, __LINE__, wlan);
  453. }
  454. static struct rt_wlan_prot_ops ops =
  455. {
  456. rt_wlan_lwip_protocol_recv,
  457. rt_wlan_lwip_protocol_register,
  458. rt_wlan_lwip_protocol_unregister
  459. };
  460. int rt_wlan_lwip_init(void)
  461. {
  462. static struct rt_wlan_prot prot;
  463. rt_wlan_prot_event_t event;
  464. rt_memset(&prot, 0, sizeof(prot));
  465. rt_strncpy(&prot.name[0], RT_WLAN_PROT_LWIP_NAME, RT_WLAN_PROT_NAME_LEN);
  466. prot.ops = &ops;
  467. if (rt_wlan_prot_regisetr(&prot) != RT_EOK)
  468. {
  469. LOG_E("F:%s L:%d protocol regisetr failed", __FUNCTION__, __LINE__);
  470. return -1;
  471. }
  472. for (event = RT_WLAN_PROT_EVT_INIT_DONE; event < RT_WLAN_PROT_EVT_MAX; event++)
  473. {
  474. rt_wlan_prot_event_register(&prot, event, rt_wlan_lwip_event_handle);
  475. }
  476. return 0;
  477. }
  478. INIT_PREV_EXPORT(rt_wlan_lwip_init);
  479. #endif
  480. #endif