Skip to content

Commit d5993db

Browse files
SolairseirABOSTMfpistm
authored
fix: if DHCP fails the static IP can be applied without problem (#50)
and adding timeout to client connection Co-authored-by: Alexandre Bourdiol <[email protected]> Co-authored-by: Frederic Pillon <[email protected]>
1 parent c191ead commit d5993db

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

src/EthernetClient.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ int EthernetClient::connect(IPAddress ip, uint16_t port)
6565
_tcp_client->data.available = 0;
6666
_tcp_client->state = TCP_NONE;
6767

68+
uint32_t startTime = millis();
6869
ip_addr_t ipaddr;
6970
tcp_arg(_tcp_client->pcb, _tcp_client);
7071
if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) {
7172
stop();
7273
return 0;
7374
}
7475

75-
uint32_t startTime = millis();
76+
startTime = millis();
7677
while (_tcp_client->state == TCP_NONE) {
7778
stm32_eth_scheduler();
78-
if ((_tcp_client->state == TCP_CLOSING) || ((millis() - startTime) >= 10000)) {
79+
if ((_tcp_client->state == TCP_CLOSING) || ((millis() - startTime) >= _timeout)) {
7980
stop();
8081
return 0;
8182
}

src/EthernetClient.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ class EthernetClient : public Client {
5252
{
5353
return (_tcp_client->pcb->remote_port);
5454
};
55+
void setTimeout(uint16_t timeout)
56+
{
57+
_timeout = timeout;
58+
}
5559

5660
friend class EthernetServer;
5761

5862
using Print::write;
5963

6064
private:
6165
struct tcp_struct *_tcp_client;
66+
uint16_t _timeout = 10000;
6267
};
6368

6469
#endif

src/utility/stm32_eth.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ static void TIM_scheduler_Config(void);
111111
*/
112112
static void Netif_Config(void)
113113
{
114+
netif_remove(&gnetif);
114115
/* Add the network interface */
115116
netif_add(&gnetif, &(gconfig.ipaddr), &(gconfig.netmask), &(gconfig.gw), NULL, &ethernetif_init, &ethernet_input);
116117

@@ -176,6 +177,7 @@ static void TIM_scheduler_Config(void)
176177
{
177178
/* Configure HardwareTimer */
178179
HardwareTimer *EthTim = new HardwareTimer(DEFAULT_ETHERNET_TIMER);
180+
EthTim->setMode(1, TIMER_OUTPUT_COMPARE);
179181

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

195-
if (mac != NULL) {
196-
ethernetif_set_mac_addr(mac);
197-
} // else default value is used: MAC_ADDR0 ... MAC_ADDR5
198+
if (mac != NULL) {
199+
ethernetif_set_mac_addr(mac);
200+
} // else default value is used: MAC_ADDR0 ... MAC_ADDR5
198201

199-
if (ip != NULL) {
200-
IP_ADDR4(&(gconfig.ipaddr), ip[0], ip[1], ip[2], ip[3]);
201-
} else {
202+
if (ip != NULL) {
203+
IP_ADDR4(&(gconfig.ipaddr), ip[0], ip[1], ip[2], ip[3]);
204+
} else {
202205
#if LWIP_DHCP
203-
ip_addr_set_zero_ip4(&(gconfig.ipaddr));
206+
ip_addr_set_zero_ip4(&(gconfig.ipaddr));
204207
#else
205-
IP_ADDR4(&(gconfig.ipaddr), IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
208+
IP_ADDR4(&(gconfig.ipaddr), IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
206209
#endif /* LWIP_DHCP */
207-
}
210+
}
208211

209-
if (gw != NULL) {
210-
IP_ADDR4(&(gconfig.gw), gw[0], gw[1], gw[2], gw[3]);
211-
} else {
212+
if (gw != NULL) {
213+
IP_ADDR4(&(gconfig.gw), gw[0], gw[1], gw[2], gw[3]);
214+
} else {
212215
#if LWIP_DHCP
213-
ip_addr_set_zero_ip4(&(gconfig.gw));
216+
ip_addr_set_zero_ip4(&(gconfig.gw));
214217
#else
215-
IP_ADDR4(&(gconfig.gw), GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
218+
IP_ADDR4(&(gconfig.gw), GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
216219
#endif /* LWIP_DHCP */
217-
}
220+
}
218221

219-
if (netmask != NULL) {
220-
IP_ADDR4(&(gconfig.netmask), netmask[0], netmask[1], netmask[2], netmask[3]);
221-
} else {
222+
if (netmask != NULL) {
223+
IP_ADDR4(&(gconfig.netmask), netmask[0], netmask[1], netmask[2], netmask[3]);
224+
} else {
222225
#if LWIP_DHCP
223-
ip_addr_set_zero_ip4(&(gconfig.netmask));
226+
ip_addr_set_zero_ip4(&(gconfig.netmask));
224227
#else
225-
IP_ADDR4(&(gconfig.netmask), NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
228+
IP_ADDR4(&(gconfig.netmask), NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
226229
#endif /* LWIP_DHCP */
227-
}
230+
}
228231

229-
/* Configure the Network interface */
230-
Netif_Config();
232+
/* Configure the Network interface */
233+
Netif_Config();
231234

235+
if (!initDone) {
232236
// stm32_eth_scheduler() will be called every 1ms.
233237
TIM_scheduler_Config();
234-
235238
initDone = 1;
236239
}
237240

0 commit comments

Comments
 (0)