35
35
36
36
#include "emac_api.h"
37
37
38
+ #if DEVICE_EMAC
39
+ #define NETIF_INIT_FN emac_lwip_if_init
40
+ #else
41
+ #define NETIF_INIT_FN eth_arch_enetif_init
42
+ #endif
43
+
44
+ #define DHCP_TIMEOUT 15000
45
+
38
46
/* Static arena of sockets */
39
47
static struct lwip_socket {
40
48
bool in_use ;
@@ -352,6 +360,47 @@ char *lwip_get_gateway(char *buf, int buflen)
352
360
#endif
353
361
}
354
362
363
+ int lwip_start_dhcp (unsigned int timeout )
364
+ {
365
+ err_t err = 0 ;
366
+ #if LWIP_IPV4
367
+ err = dhcp_start (& lwip_netif );
368
+ if (err ) {
369
+ return NSAPI_ERROR_DHCP_FAILURE ;
370
+ }
371
+ #endif
372
+
373
+ #if DEVICE_EMAC
374
+ // If doesn't have address
375
+ if (!lwip_get_ip_addr (true, & lwip_netif )) {
376
+ err = sys_arch_sem_wait (& lwip_netif_has_addr , timeout );
377
+ if (err == SYS_ARCH_TIMEOUT ) {
378
+ return NSAPI_ERROR_DHCP_FAILURE ;
379
+ }
380
+ lwip_connected = true;
381
+ }
382
+ #endif /* DEVICE_EMAC */
383
+
384
+ return err ;
385
+ }
386
+
387
+ int lwip_start_static_ip (const char * ip , const char * netmask , const char * gw )
388
+ {
389
+
390
+ #if LWIP_IPV4
391
+ ip4_addr_t ip_addr ;
392
+ ip4_addr_t netmask_addr ;
393
+ ip4_addr_t gw_addr ;
394
+
395
+ if (!inet_aton (ip , & ip_addr ) ||
396
+ !inet_aton (netmask , & netmask_addr ) ||
397
+ !inet_aton (gw , & gw_addr )) {
398
+ return NSAPI_ERROR_PARAMETER ;
399
+ }
400
+
401
+ netif_set_addr (& lwip_netif , & ip_addr , & netmask_addr , & gw_addr );
402
+ #endif
403
+ }
355
404
356
405
int lwip_bringup (emac_interface_t * emac , bool dhcp , const char * ip , const char * netmask , const char * gw )
357
406
{
@@ -373,23 +422,14 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *
373
422
sys_arch_sem_wait (& lwip_tcpip_inited , 0 );
374
423
375
424
memset (& lwip_netif , 0 , sizeof lwip_netif );
376
- #if DEVICE_EMAC
377
425
if (!netif_add (& lwip_netif ,
378
426
#if LWIP_IPV4
379
427
0 , 0 , 0 ,
380
428
#endif
381
- emac , emac_lwip_if_init , tcpip_input )) {
429
+ emac , NETIF_INIT_FN , tcpip_input )) {
382
430
return -1 ;
383
431
}
384
- #else /* DEVICE_EMAC */
385
- if (!netif_add (& lwip_netif ,
386
- #if LWIP_IPV4
387
- 0 , 0 , 0 ,
388
- #endif
389
- NULL , eth_arch_enetif_init , tcpip_input )) {
390
- return -1 ;
391
- }
392
- #endif /* DEVICE_EMAC */
432
+
393
433
netif_set_default (& lwip_netif );
394
434
395
435
netif_set_link_callback (& lwip_netif , lwip_netif_link_irq );
@@ -435,47 +475,26 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *
435
475
}
436
476
}
437
477
438
- #if LWIP_IPV4
478
+ #if ! DEVICE_EMAC
439
479
if (!dhcp ) {
440
- ip4_addr_t ip_addr ;
441
- ip4_addr_t netmask_addr ;
442
- ip4_addr_t gw_addr ;
443
-
444
- if (!inet_aton (ip , & ip_addr ) ||
445
- !inet_aton (netmask , & netmask_addr ) ||
446
- !inet_aton (gw , & gw_addr )) {
447
- return NSAPI_ERROR_PARAMETER ;
448
- }
449
-
450
- netif_set_addr (& lwip_netif , & ip_addr , & netmask_addr , & gw_addr );
480
+ lwip_start_static_ip (ip , netmask , gw );
451
481
}
452
- #endif
453
482
454
483
netif_set_up (& lwip_netif );
455
484
456
- #if !DEVICE_EMAC
457
- #if LWIP_IPV4
458
- // Connect to the network
459
485
lwip_dhcp = dhcp ;
460
-
461
- if (lwip_dhcp ) {
462
- err_t err = dhcp_start (& lwip_netif );
463
- if (err ) {
464
- return NSAPI_ERROR_DHCP_FAILURE ;
465
- }
486
+ if (dhcp ) {
487
+ lwip_start_dhcp (DHCP_TIMEOUT );
466
488
}
467
- #endif
468
489
469
490
// If doesn't have address
470
491
if (!lwip_get_ip_addr (true, & lwip_netif )) {
471
- //ret = sys_arch_sem_wait(&lwip_netif_has_addr, 15000);
472
- ret = sys_arch_sem_wait (& lwip_netif_has_addr , 30000 );
492
+ ret = sys_arch_sem_wait (& lwip_netif_has_addr , DHCP_TIMEOUT );
473
493
if (ret == SYS_ARCH_TIMEOUT ) {
474
494
return NSAPI_ERROR_DHCP_FAILURE ;
475
495
}
476
496
lwip_connected = true;
477
497
}
478
- #endif /* DEVICE_EMAC */
479
498
480
499
#if ADDR_TIMEOUT
481
500
// If address is not for preferred stack waits a while to see
@@ -485,6 +504,8 @@ int lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *
485
504
}
486
505
#endif
487
506
507
+ #endif /* DEVICE_EMAC */
508
+
488
509
#if LWIP_IPV6
489
510
add_dns_addr (& lwip_netif );
490
511
#endif
@@ -537,46 +558,6 @@ static int lwip_err_remap(err_t err) {
537
558
}
538
559
}
539
560
540
- int lwip_start_dhcp (unsigned int timeout )
541
- {
542
- err_t err = NSAPI_ERROR_DNS_FAILURE ;
543
- #if LWIP_IPV4
544
- err = dhcp_start (& lwip_netif );
545
- if (err ) {
546
- return NSAPI_ERROR_DHCP_FAILURE ;
547
- }
548
- #endif
549
-
550
- // If doesn't have address
551
- if (!lwip_get_ip_addr (true, & lwip_netif )) {
552
- err = sys_arch_sem_wait (& lwip_netif_has_addr , timeout );
553
- if (err == SYS_ARCH_TIMEOUT ) {
554
- return NSAPI_ERROR_DHCP_FAILURE ;
555
- }
556
- lwip_connected = true;
557
- }
558
- return err ;
559
- }
560
-
561
- int lwip_start_static_ip (const char * ip , const char * netmask , const char * gw )
562
- {
563
-
564
- #if LWIP_IPV4
565
- ip4_addr_t ip_addr ;
566
- ip4_addr_t netmask_addr ;
567
- ip4_addr_t gw_addr ;
568
-
569
- if (!inet_aton (ip , & ip_addr ) ||
570
- !inet_aton (netmask , & netmask_addr ) ||
571
- !inet_aton (gw , & gw_addr )) {
572
- return NSAPI_ERROR_PARAMETER ;
573
- }
574
-
575
- netif_set_addr (& lwip_netif , & ip_addr , & netmask_addr , & gw_addr );
576
- #endif
577
-
578
- }
579
-
580
561
/* LWIP network stack implementation */
581
562
static int lwip_gethostbyname (nsapi_stack_t * stack , const char * host , nsapi_addr_t * addr , nsapi_version_t version )
582
563
{
0 commit comments