Skip to content

Commit 3594be9

Browse files
committed
LwipIntfDev - method end() to enable repeated begin
1 parent 31c1592 commit 3594be9

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

cores/esp8266/LwipIntfDev.h

+30-7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class LwipIntfDev: public LwipIntf, public RawDev
6868

6969
// default mac-address is inferred from esp8266's STA interface
7070
boolean begin(const uint8_t* macAddress = nullptr, const uint16_t mtu = DEFAULT_MTU);
71+
void end();
7172

7273
const netif* getNetIf() const
7374
{
@@ -138,6 +139,7 @@ class LwipIntfDev: public LwipIntf, public RawDev
138139
int8_t _intrPin;
139140
uint8_t _macAddress[6];
140141
bool _started;
142+
bool _scheduled;
141143
bool _default;
142144
};
143145

@@ -229,6 +231,7 @@ boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu
229231
if (!netif_add(&_netif, ip_2_ip4(&ip_addr), ip_2_ip4(&netmask), ip_2_ip4(&gw), this,
230232
netif_init_s, ethernet_input))
231233
{
234+
RawDev::end();
232235
return false;
233236
}
234237

@@ -242,10 +245,11 @@ boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu
242245
break;
243246

244247
case ERR_IF:
248+
RawDev::end();
245249
return false;
246250

247251
default:
248-
netif_remove(&_netif);
252+
end();
249253
return false;
250254
}
251255
}
@@ -272,22 +276,41 @@ boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu
272276
}
273277
}
274278

275-
if (_intrPin < 0
276-
&& !schedule_recurrent_function_us(
279+
if (_intrPin < 0 && !_scheduled)
280+
{
281+
_scheduled = schedule_recurrent_function_us(
277282
[&]()
278283
{
284+
if (!_started)
285+
{
286+
_scheduled = false;
287+
return false;
288+
}
279289
this->handlePackets();
280290
return true;
281291
},
282-
100))
283-
{
284-
netif_remove(&_netif);
285-
return false;
292+
100);
293+
if (!_scheduled)
294+
{
295+
end();
296+
return false;
297+
}
286298
}
287299

288300
return true;
289301
}
290302

303+
template<class RawDev>
304+
void LwipIntfDev<RawDev>::end()
305+
{
306+
netif_remove(&_netif);
307+
ip_addr_copy(_netif.ip_addr, ip_addr_any); // to allow DHCP at next begin
308+
ip_addr_copy(_netif.netmask, ip_addr_any);
309+
ip_addr_copy(_netif.gw, ip_addr_any);
310+
_started = false;
311+
RawDev::end();
312+
}
313+
291314
template<class RawDev>
292315
wl_status_t LwipIntfDev<RawDev>::status()
293316
{

0 commit comments

Comments
 (0)