Skip to content

if DHCP fails the static IP can be applied without problem #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/EthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,18 @@ int EthernetClient::connect(IPAddress ip, uint16_t port)
_tcp_client->data.available = 0;
_tcp_client->state = TCP_NONE;

uint32_t startTime = millis();
ip_addr_t ipaddr;
tcp_arg(_tcp_client->pcb, _tcp_client);
if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) {
stop();
return 0;
}

uint32_t startTime = millis();
startTime = millis();
while (_tcp_client->state == TCP_NONE) {
stm32_eth_scheduler();
if ((_tcp_client->state == TCP_CLOSING) || ((millis() - startTime) >= 10000)) {
if ((_tcp_client->state == TCP_CLOSING) || ((millis() - startTime) >= _timeout)) {
stop();
return 0;
}
Expand Down
5 changes: 5 additions & 0 deletions src/EthernetClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ class EthernetClient : public Client {
{
return (_tcp_client->pcb->remote_port);
};
void setTimeout(uint16_t timeout)
{
_timeout = timeout;
}

friend class EthernetServer;

using Print::write;

private:
struct tcp_struct *_tcp_client;
uint16_t _timeout = 10000;
};

#endif
51 changes: 27 additions & 24 deletions src/utility/stm32_eth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ static void TIM_scheduler_Config(void);
*/
static void Netif_Config(void)
{
netif_remove(&gnetif);
/* Add the network interface */
netif_add(&gnetif, &(gconfig.ipaddr), &(gconfig.netmask), &(gconfig.gw), NULL, &ethernetif_init, &ethernet_input);

Expand Down Expand Up @@ -176,6 +177,7 @@ static void TIM_scheduler_Config(void)
{
/* Configure HardwareTimer */
HardwareTimer *EthTim = new HardwareTimer(DEFAULT_ETHERNET_TIMER);
EthTim->setMode(1, TIMER_OUTPUT_COMPARE);

/* Timer set to 1ms */
EthTim->setOverflow(1000, MICROSEC_FORMAT);
Expand All @@ -191,47 +193,48 @@ void stm32_eth_init(const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, co
if (!initDone) {
/* Initialize the LwIP stack */
lwip_init();
}

if (mac != NULL) {
ethernetif_set_mac_addr(mac);
} // else default value is used: MAC_ADDR0 ... MAC_ADDR5
if (mac != NULL) {
ethernetif_set_mac_addr(mac);
} // else default value is used: MAC_ADDR0 ... MAC_ADDR5

if (ip != NULL) {
IP_ADDR4(&(gconfig.ipaddr), ip[0], ip[1], ip[2], ip[3]);
} else {
if (ip != NULL) {
IP_ADDR4(&(gconfig.ipaddr), ip[0], ip[1], ip[2], ip[3]);
} else {
#if LWIP_DHCP
ip_addr_set_zero_ip4(&(gconfig.ipaddr));
ip_addr_set_zero_ip4(&(gconfig.ipaddr));
#else
IP_ADDR4(&(gconfig.ipaddr), IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP_ADDR4(&(gconfig.ipaddr), IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
#endif /* LWIP_DHCP */
}
}

if (gw != NULL) {
IP_ADDR4(&(gconfig.gw), gw[0], gw[1], gw[2], gw[3]);
} else {
if (gw != NULL) {
IP_ADDR4(&(gconfig.gw), gw[0], gw[1], gw[2], gw[3]);
} else {
#if LWIP_DHCP
ip_addr_set_zero_ip4(&(gconfig.gw));
ip_addr_set_zero_ip4(&(gconfig.gw));
#else
IP_ADDR4(&(gconfig.gw), GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
IP_ADDR4(&(gconfig.gw), GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
#endif /* LWIP_DHCP */
}
}

if (netmask != NULL) {
IP_ADDR4(&(gconfig.netmask), netmask[0], netmask[1], netmask[2], netmask[3]);
} else {
if (netmask != NULL) {
IP_ADDR4(&(gconfig.netmask), netmask[0], netmask[1], netmask[2], netmask[3]);
} else {
#if LWIP_DHCP
ip_addr_set_zero_ip4(&(gconfig.netmask));
ip_addr_set_zero_ip4(&(gconfig.netmask));
#else
IP_ADDR4(&(gconfig.netmask), NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP_ADDR4(&(gconfig.netmask), NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
#endif /* LWIP_DHCP */
}
}

/* Configure the Network interface */
Netif_Config();
/* Configure the Network interface */
Netif_Config();

if (!initDone) {
// stm32_eth_scheduler() will be called every 1ms.
TIM_scheduler_Config();

initDone = 1;
}

Expand Down