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