Skip to content

Commit d946593

Browse files
authored
allow retries of if_config (#99)
1 parent 3d123eb commit d946593

1 file changed

Lines changed: 76 additions & 63 deletions

File tree

lwip/network.c

Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,90 +1462,103 @@ s32 if_configex(struct in_addr *local_ip,struct in_addr *netmask,struct in_addr
14621462
struct timespec tb;
14631463
dev_s hbba = NULL;
14641464

1465-
if(g_netinitiated) return 0;
1466-
g_netinitiated = 1;
1465+
if(g_netinitiated >= 4) return 0;
14671466

14681467
// AddDevice(&dotab_stdnet);
14691468
#ifdef STATS
14701469
stats_init();
14711470
#endif /* STATS */
14721471

1473-
sys_init();
1474-
mem_init();
1475-
memp_init();
1476-
pbuf_init();
1477-
netif_init();
1472+
if (g_netinitiated < 1) {
1473+
sys_init();
1474+
mem_init();
1475+
memp_init();
1476+
pbuf_init();
1477+
netif_init();
1478+
++g_netinitiated;
1479+
}
14781480

1479-
// init tcpip thread message box
1480-
if(MQ_Init(&netthread_mbox,MQBOX_SIZE)!=MQ_ERROR_SUCCESSFUL) return -1;
1481+
if (g_netinitiated < 2) {
1482+
// init tcpip thread message box
1483+
if(MQ_Init(&netthread_mbox,MQBOX_SIZE)!=MQ_ERROR_SUCCESSFUL) return -1;
1484+
++g_netinitiated;
1485+
}
14811486

14821487
// create & setup interface
14831488
loc_ip.addr = 0;
14841489
mask.addr = 0;
14851490
gw.addr = 0;
14861491

1487-
1488-
if(use_dhcp==FALSE) {
1489-
if( !gateway || gateway->s_addr==0
1490-
|| !local_ip || local_ip->s_addr==0
1491-
|| !netmask || netmask->s_addr==0 ) return -1;
1492-
loc_ip.addr = local_ip->s_addr;
1493-
mask.addr = netmask->s_addr;
1494-
gw.addr = gateway->s_addr;
1495-
}
1496-
hbba = bba_create(&g_hNetIF);
1497-
pnet = netif_add(&g_hNetIF,&loc_ip, &mask, &gw, hbba, bba_init, net_input);
1498-
if(pnet) {
1499-
netif_set_up(pnet);
1500-
netif_set_default(pnet);
1501-
#if (LWIP_DHCP)
1502-
if(use_dhcp==TRUE) {
1503-
//setup coarse timer
1504-
tb.tv_sec = DHCP_COARSE_TIMER_SECS;
1505-
tb.tv_nsec = 0;
1506-
net_dhcpcoarse_ticks = __lwp_wd_calc_ticks(&tb);
1507-
__lwp_wd_initialize(&dhcp_coarsetimer_cntrl, __dhcpcoarse_timer, DHCPCOARSE_TIMER_ID, NULL);
1508-
__lwp_wd_insert_ticks(&dhcp_coarsetimer_cntrl, net_dhcpcoarse_ticks);
1509-
1510-
//setup fine timer
1511-
tb.tv_sec = 0;
1512-
tb.tv_nsec = DHCP_FINE_TIMER_MSECS * TB_NSPERMS;
1513-
net_dhcpfine_ticks = __lwp_wd_calc_ticks(&tb);
1514-
__lwp_wd_initialize(&dhcp_finetimer_cntrl, __dhcpfine_timer, DHCPFINE_TIMER_ID, NULL);
1515-
__lwp_wd_insert_ticks(&dhcp_finetimer_cntrl, net_dhcpfine_ticks);
1516-
1517-
//now start dhcp client
1518-
dhcp_start(pnet);
1492+
if (g_netinitiated < 3) {
1493+
if(use_dhcp==FALSE) {
1494+
if( !gateway || gateway->s_addr==0
1495+
|| !local_ip || local_ip->s_addr==0
1496+
|| !netmask || netmask->s_addr==0 ) return -1;
1497+
loc_ip.addr = local_ip->s_addr;
1498+
mask.addr = netmask->s_addr;
1499+
gw.addr = gateway->s_addr;
15191500
}
1520-
#endif
1521-
} else
1522-
return -1;
1501+
hbba = bba_create(&g_hNetIF);
1502+
pnet = netif_add(&g_hNetIF,&loc_ip, &mask, &gw, hbba, bba_init, net_input);
1503+
if(pnet) {
1504+
netif_set_up(pnet);
1505+
netif_set_default(pnet);
1506+
#if (LWIP_DHCP)
1507+
if(use_dhcp==TRUE) {
1508+
//setup coarse timer
1509+
tb.tv_sec = DHCP_COARSE_TIMER_SECS;
1510+
tb.tv_nsec = 0;
1511+
net_dhcpcoarse_ticks = __lwp_wd_calc_ticks(&tb);
1512+
__lwp_wd_initialize(&dhcp_coarsetimer_cntrl, __dhcpcoarse_timer, DHCPCOARSE_TIMER_ID, NULL);
1513+
__lwp_wd_insert_ticks(&dhcp_coarsetimer_cntrl, net_dhcpcoarse_ticks);
1514+
1515+
//setup fine timer
1516+
tb.tv_sec = 0;
1517+
tb.tv_nsec = DHCP_FINE_TIMER_MSECS * TB_NSPERMS;
1518+
net_dhcpfine_ticks = __lwp_wd_calc_ticks(&tb);
1519+
__lwp_wd_initialize(&dhcp_finetimer_cntrl, __dhcpfine_timer, DHCPFINE_TIMER_ID, NULL);
1520+
__lwp_wd_insert_ticks(&dhcp_finetimer_cntrl, net_dhcpfine_ticks);
1521+
1522+
//now start dhcp client
1523+
dhcp_start(pnet);
1524+
}
1525+
#endif
1526+
} else
1527+
return -1;
1528+
++g_netinitiated;
1529+
}
15231530

15241531
// setup loopinterface
15251532
IP4_ADDR(&loc_ip, 127, 0, 0, 1);
15261533
IP4_ADDR(&mask, 255, 0, 0, 0);
15271534
IP4_ADDR(&gw, 127, 0, 0, 1);
15281535
pnet = netif_add(&g_hLoopIF, &loc_ip, &mask, &gw, NULL, loopif_init, net_input);
15291536

1530-
//last and least start the tcpip layer
1531-
ret = net_init();
1532-
1533-
if ( ret == 0 && use_dhcp == TRUE ) {
1534-
1535-
int retries = max_retries;
1536-
// wait for dhcp to bind
1537-
while ( g_hNetIF.dhcp->state != DHCP_BOUND && retries > 0 ) {
1538-
retries--;
1539-
usleep(500000);
1540-
}
1541-
1542-
if ( retries > 0 ) {
1543-
//copy back network addresses
1544-
if ( local_ip != NULL ) local_ip->s_addr = g_hNetIF.ip_addr.addr;
1545-
if ( gateway != NULL ) gateway->s_addr = g_hNetIF.gw.addr;
1546-
if ( netmask != NULL ) netmask->s_addr = g_hNetIF.netmask.addr;
1547-
} else {
1548-
ret = -2;
1537+
if (g_netinitiated < 4) {
1538+
//last and least start the tcpip layer
1539+
ret = net_init();
1540+
1541+
if (ret >= 0) {
1542+
if (use_dhcp == TRUE) {
1543+
int retries = max_retries;
1544+
// wait for dhcp to bind
1545+
while ( g_hNetIF.dhcp->state != DHCP_BOUND && retries > 0 ) {
1546+
retries--;
1547+
usleep(500000);
1548+
}
1549+
1550+
if ( retries > 0 ) {
1551+
//copy back network addresses
1552+
if ( local_ip != NULL ) local_ip->s_addr = g_hNetIF.ip_addr.addr;
1553+
if ( gateway != NULL ) gateway->s_addr = g_hNetIF.gw.addr;
1554+
if ( netmask != NULL ) netmask->s_addr = g_hNetIF.netmask.addr;
1555+
++g_netinitiated;
1556+
} else {
1557+
ret = -2;
1558+
}
1559+
} else {
1560+
++g_netinitiated;
1561+
}
15491562
}
15501563
}
15511564

0 commit comments

Comments
 (0)