wlan_lwip.c 14 KB

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