Skip to content

Commit ae11b51

Browse files
committed
Split lwip initialisation and interface bringup
Split IP stack initialisation from mbed_lwip_bringup to mbed_lwip_init.
1 parent 98eb50f commit ae11b51

File tree

3 files changed

+43
-63
lines changed

3 files changed

+43
-63
lines changed

features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int EthernetInterface::set_dhcp(bool dhcp)
4141

4242
int EthernetInterface::connect()
4343
{
44-
return mbed_lwip_bringup(NULL, _dhcp,
44+
return mbed_lwip_bringup(_dhcp,
4545
_ip_address[0] ? _ip_address : 0,
4646
_netmask[0] ? _netmask : 0,
4747
_gateway[0] ? _gateway : 0);

features/FEATURE_LWIP/lwip-interface/lwip_stack.c

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -360,55 +360,8 @@ char *mbed_lwip_get_gateway(char *buf, int buflen)
360360
#endif
361361
}
362362

363-
int mbed_lwip_start_dhcp(unsigned int timeout)
363+
int mbed_lwip_init(emac_interface_t *emac)
364364
{
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 (!mbed_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 mbed_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-
}
404-
405-
int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw)
406-
{
407-
// Check if we've already connected
408-
if (lwip_connected) {
409-
return NSAPI_ERROR_PARAMETER;
410-
}
411-
412365
// Check if we've already brought up lwip
413366
if (!mbed_lwip_get_mac_address()) {
414367
// Set up network
@@ -427,19 +380,33 @@ int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const c
427380
0, 0, 0,
428381
#endif
429382
emac, MBED_NETIF_INIT_FN, tcpip_input)) {
430-
return -1;
383+
return NSAPI_ERROR_DEVICE_ERROR;
431384
}
432385

433386
netif_set_default(&lwip_netif);
434387

435-
netif_set_link_callback (&lwip_netif, mbed_lwip_netif_link_irq);
388+
netif_set_link_callback(&lwip_netif, mbed_lwip_netif_link_irq);
436389
netif_set_status_callback(&lwip_netif, mbed_lwip_netif_status_irq);
437390

438391
#if !DEVICE_EMAC
439392
eth_arch_enable_interrupts();
440393
#endif
441394
}
442395

396+
return NSAPI_ERROR_OK;
397+
}
398+
399+
int mbed_lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw)
400+
{
401+
// Check if we've already connected
402+
if (lwip_connected) {
403+
return NSAPI_ERROR_PARAMETER;
404+
}
405+
406+
if(mbed_lwip_init(NULL) != NSAPI_ERROR_OK) {
407+
return NSAPI_ERROR_DEVICE_ERROR;
408+
}
409+
443410
// Zero out socket set
444411
mbed_lwip_arena_init();
445412

@@ -475,21 +442,39 @@ int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const c
475442
}
476443
}
477444

478-
#if !DEVICE_EMAC
445+
#if LWIP_IPV4
479446
if (!dhcp) {
480-
mbed_lwip_start_static_ip(ip, netmask, gw);
447+
ip4_addr_t ip_addr;
448+
ip4_addr_t netmask_addr;
449+
ip4_addr_t gw_addr;
450+
451+
if (!inet_aton(ip, &ip_addr) ||
452+
!inet_aton(netmask, &netmask_addr) ||
453+
!inet_aton(gw, &gw_addr)) {
454+
return NSAPI_ERROR_PARAMETER;
455+
}
456+
457+
netif_set_addr(&lwip_netif, &ip_addr, &netmask_addr, &gw_addr);
481458
}
459+
#endif
482460

483461
netif_set_up(&lwip_netif);
484462

463+
#if LWIP_IPV4
464+
// Connect to the network
485465
lwip_dhcp = dhcp;
486-
if (dhcp) {
487-
mbed_lwip_start_dhcp(DHCP_TIMEOUT);
466+
467+
if (lwip_dhcp) {
468+
err_t err = dhcp_start(&lwip_netif);
469+
if (err) {
470+
return NSAPI_ERROR_DHCP_FAILURE;
471+
}
488472
}
473+
#endif
489474

490475
// If doesn't have address
491476
if (!mbed_lwip_get_ip_addr(true, &lwip_netif)) {
492-
ret = sys_arch_sem_wait(&lwip_netif_has_addr, DHCP_TIMEOUT);
477+
ret = sys_arch_sem_wait(&lwip_netif_has_addr, 15000);
493478
if (ret == SYS_ARCH_TIMEOUT) {
494479
return NSAPI_ERROR_DHCP_FAILURE;
495480
}
@@ -504,16 +489,13 @@ int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const c
504489
}
505490
#endif
506491

507-
#endif /* DEVICE_EMAC */
508-
509492
#if LWIP_IPV6
510493
add_dns_addr(&lwip_netif);
511494
#endif
512495

513496
return 0;
514497
}
515498

516-
517499
int mbed_lwip_bringdown(void)
518500
{
519501
// Check if we've connected

features/FEATURE_LWIP/lwip-interface/lwip_stack.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ extern "C" {
2525
#endif
2626

2727
// Access to lwip through the nsapi
28-
int mbed_lwip_bringup(emac_interface_t *emac, bool dhcp, const char *ip, const char *netmask, const char *gw);
28+
int mbed_lwip_init(emac_interface_t *emac);
29+
int mbed_lwip_bringup(bool dhcp, const char *ip, const char *netmask, const char *gw);
2930
int mbed_lwip_bringdown(void);
3031

3132
const char *mbed_lwip_get_mac_address(void);
3233
char *mbed_lwip_get_ip_address(char *buf, int buflen);
3334
char *mbed_lwip_get_netmask(char *buf, int buflen);
3435
char *mbed_lwip_get_gateway(char *buf, int buflen);
3536

36-
int mbed_lwip_start_dhcp(unsigned int timeout);
37-
int mbed_lwip_start_static_ip(const char *ip, const char *netmask, const char *gw);
38-
3937
extern nsapi_stack_t lwip_stack;
4038

4139

0 commit comments

Comments
 (0)