大家好、我在 LwIP 中遇到了一些问题。 我使具有用户界面的应用程序可以在静态 IP 和 DHCP 之间切换。 但 DHCP 未正确初始化, lwIPInit()不返回 IP 地址(在初始化期间返回零作为默认值)。 在某些情况下、它工作正常、我从 DHCP 接收 IP、但这种情况绝对是随机发生的。 在任何请求时正确初始化静态 IP。 您对此问题有什么看法吗? 这是我来自 lwIPInit()的代码
/* This is a peace of code from where I make a choise between static IP and DHCP*/
case MODULES_NETWORK_IP_STATIC:
/* IPaddr NetMask Gate */
//uint32_t htonl_aaddr = lwip_htonl(&network.sv_ip_addr);
ipAddr = lwIPInit(0, macAddress, network.sv_ip_addr, network.sv_ip_mask, network.sv_ip_gate, IPADDR_USE_STATIC, lwip_init_seq);
break;
case MODULES_NETWORK_IP_DHCP:
/* Initialze the lwIP library, using DHCP.*/
ipAddr = lwIPInit(0, macAddress, 0, 0, 0, IPADDR_USE_DHCP, lwip_init_seq);
break;
//////////////////////////////////////////////////////////////////////////////////////////
extern main_database_t main_database;
unsigned int lwIPInit(unsigned int instNum, unsigned char *macArray,
unsigned int ipAddr, unsigned int netMask,
unsigned int gwAddr, unsigned int ipMode, unsigned int inited)
{
struct ip_addr ip_addr;
struct ip_addr net_mask;
struct ip_addr gw_addr;
volatile unsigned char *state;
unsigned int *ipAddrPtr;
volatile unsigned int cnt = 0x3FFFFFFF;
lwip_init();
/* Setup the network address values. */
if(ipMode == IPADDR_USE_STATIC)
{
ip_addr.addr = htonl(ipAddr);
net_mask.addr = htonl(netMask);
gw_addr.addr = htonl(gwAddr);
}
else
{
ip_addr.addr = 0;
net_mask.addr = 0;
gw_addr.addr = 0;
}
hdkif_macaddrset(instNum, macArray);
/*
** Create, configure and add the Ethernet controller interface with
** default settings. ip_input should be used to send packets directly to
** the stack. The lwIP will internaly call the hdkif_init function.
*/
if(inited){
if(NULL ==
netif_add(&hdkNetIF[instNum], &ip_addr, &net_mask, &gw_addr, &instNum,
hdkif_init, ip_input))
{
return 0;
};
}else{
dhcp_stop(&hdkNetIF[instNum]);
netif_set_down(&hdkNetIF[instNum]);
netif_set_addr(&hdkNetIF[instNum], &ip_addr, &net_mask, &gw_addr);
}
netif_set_default(&hdkNetIF[instNum]);
/* Start DHCP, if enabled. */
#if LWIP_DHCP
if(ipMode == IPADDR_USE_DHCP)
{
dhcp_start(&hdkNetIF[instNum]);
}
#endif
/* Start AutoIP, if enabled and DHCP is not. */
#if LWIP_AUTOIP
if(ipMode == IPADDR_USE_AUTOIP)
{
autoip_start(&hdkNetIF[instNum]);
}
#endif
if(ipMode != IPADDR_USE_STATIC)
{
/* wait till the dynamic IP address is properly assigned */
state = &(hdkNetIF[instNum].dhcp->state);
cnt = 20;
while(*state != DHCP_BOUND){
vTaskDelay(1000);
cnt--;
if(cnt == 0)
return 0;
}
}
else
{
/* Bring the interface up */
netif_set_up(&hdkNetIF[instNum]);
}
ipAddrPtr = (unsigned int*)&(hdkNetIF[instNum].ip_addr);
main_database.sv_ip_addr = *(unsigned int*)&(hdkNetIF[instNum].ip_addr);
main_database.sv_ip_mask = *(unsigned int*)&(hdkNetIF[instNum].netmask);
main_database.sv_ip_gate = *(unsigned int*)&(hdkNetIF[instNum].gw);
return (*ipAddrPtr);
}