wlan_lwip.c 13 KB

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