Skip to content

Commit 0f430c5

Browse files
Making it possible to call multiple time the begin function on an interfare
1 parent 88f5091 commit 0f430c5

File tree

2 files changed

+63
-29
lines changed

2 files changed

+63
-29
lines changed

libraries/lwIpWrapper/src/CNetIf.cpp

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,32 @@ int CNetIf::begin(const IPAddress &ip, const IPAddress &nm, const IPAddress &gw)
302302
ip_addr_t _nm = fromArduinoIP(nm);
303303
ip_addr_t _gw = fromArduinoIP(gw);
304304

305-
// netif add copies the ip addresses into the netif, no need to store them also in the object
306-
struct netif *_ni = netif_add(
307-
&this->ni,
308-
&_ip, &_nm, &_gw, // ip addresses are being copied and not taken as reference, use a local defined variable
309-
this,
310-
_netif_init,
311-
ethernet_input
312-
);
313-
if(_ni == nullptr) {
314-
return -1;
315-
}
305+
char name[3] = {
306+
ni.name[0],
307+
ni.name[1],
308+
ni.num + '0',
309+
};
310+
if(netif_find(name) == nullptr) {
311+
312+
// netif add copies the ip addresses into the netif, no need to store them also in the object
313+
struct netif *_ni = netif_add(
314+
&this->ni,
315+
&_ip, &_nm, &_gw, // ip addresses are being copied and not taken as reference, use a local defined variable
316+
this,
317+
_netif_init,
318+
ethernet_input
319+
);
320+
321+
if(_ni == nullptr) {
322+
return -1;
323+
}
324+
} else {
325+
if (netif_is_link_up(&this->ni)) {
326+
netif_set_down(&this->ni);
327+
}
316328

329+
// TODO check for any changes detected and update the iface
330+
}
317331
netif_set_up(&this->ni);
318332

319333
// add the interface to the network stack
@@ -448,7 +462,7 @@ bool CNetIf::dhcpRenew() {
448462
/* ##########################################################################
449463
* ETHERNET NETWORK INTERFACE CLASS
450464
* ########################################################################## */
451-
uint8_t CEth::eth_id = 0;
465+
const char CEth::eth_ifname[] = "en";
452466

453467
CEth::CEth(NetworkDriver *driver)
454468
: CNetIf(driver) {
@@ -470,19 +484,42 @@ int CEth::begin(const IPAddress &ip, const IPAddress &nm, const IPAddress &gw, c
470484
return res;
471485
}
472486

487+
int CEth::begin(
488+
uint8_t *mac_address,
489+
const IPAddress &local_ip,
490+
const IPAddress &dns_server,
491+
const IPAddress &gateway,
492+
const IPAddress &subnet,
493+
const unsigned long timeout,
494+
const unsigned long responseTimeout) {
495+
496+
this->setMacAddress(mac_address);
497+
498+
return this->begin(local_ip, subnet, gateway, dns_server);
499+
}
500+
501+
int CEth::begin(
502+
uint8_t *mac_address,
503+
const unsigned long timeout,
504+
const unsigned long responseTimeout) {
505+
506+
this->setMacAddress(mac_address);
507+
508+
return this->begin();
509+
}
510+
511+
473512
err_t CEth::init(struct netif* ni) {
474513
// Setting up netif
475514
#if LWIP_NETIF_HOSTNAME
476515
ni->hostname = "C33_eth";
477516
#endif
478-
ni->name[0] = CEth::eth_ifname_prefix;
479-
ni->name[1] = '0' + CEth::eth_id++;
517+
ni->name[0] = CEth::eth_ifname[0];
518+
ni->name[1] = CEth::eth_ifname[1];
480519
ni->mtu = 1500; // TODO get this from the network
481520
ni->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
482521

483522
memcpy(ni->hwaddr, this->driver->getMacAddress(), 6); // FIXME handle this using a constant
484-
// ni->hwaddr = C33EthernetDriver.getMacAddress();
485-
// ni->hwaddr_len = sizeof(macaddress);
486523
ni->hwaddr_len = 6;
487524

488525
ni->output = etharp_output;
@@ -549,7 +586,7 @@ void CEth::consume_callback(uint8_t* buffer, uint32_t len) {
549586
/* ########################################################################## */
550587
/* CWifiStation NETWORK INTERFACE CLASS */
551588
/* ########################################################################## */
552-
uint8_t CWifiStation::wifistation_id = 0;
589+
const char CWifiStation::wifistation_ifname[] = "ws";
553590

554591
CWifiStation::CWifiStation()
555592
: hw_init(false) {
@@ -673,8 +710,8 @@ err_t CWifiStation::init(struct netif* ni) {
673710
#if LWIP_NETIF_HOSTNAME
674711
ni->hostname = "C33-WifiSta";
675712
#endif
676-
ni->name[0] = CWifiStation::wifistation_ifname_prefix;
677-
ni->name[1] = '0' + CWifiStation::wifistation_id++;
713+
ni->name[0] = CWifiStation::wifistation_ifname[0];
714+
ni->name[1] = CWifiStation::wifistation_ifname[1];
678715
ni->mtu = 1500; // FIXME get this from the network
679716
ni->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
680717

@@ -859,7 +896,7 @@ int CWifiStation::resetLowPowerMode() {
859896
/* ########################################################################## */
860897
/* CWifiSoftAp NETWORK INTERFACE CLASS */
861898
/* ########################################################################## */
862-
uint8_t CWifiSoftAp::softap_id = 0;
899+
const char CWifiSoftAp::softap_ifname[] = "sa";
863900

864901
// This is required for dhcp server to assign ip addresses to AP clients
865902
IPAddress default_nm("255.255.255.0");
@@ -883,7 +920,7 @@ int CWifiSoftAp::begin(const IPAddress &ip, const IPAddress &nm, const IPAddress
883920
return ESP_CONTROL_OK;
884921
});
885922

886-
if ((res=CEspControl::getInstance().initSpiDriver()) != 0) {
923+
if ((res=CEspControl::getInstance().initSpiDriver()) != 0 && !hw_init) {
887924
// res = -1; // FIXME put a proper error code
888925
goto exit;
889926
}
@@ -953,8 +990,8 @@ err_t CWifiSoftAp::init(struct netif* ni) {
953990
// TODO pass the hostname in the constructor os with a setter
954991
ni->hostname = "C33-SoftAP";
955992
#endif
956-
ni->name[0] = CWifiSoftAp::softap_ifname_prefix;
957-
ni->name[1] = '0' + CWifiSoftAp::softap_id++;
993+
ni->name[0] = CWifiSoftAp::softap_ifname[0];
994+
ni->name[1] = CWifiSoftAp::softap_ifname[1];
958995
ni->mtu = 1500; // FIXME get this from the network
959996
ni->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
960997

libraries/lwIpWrapper/src/CNetIf.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ class CEth : public CNetIf {
234234
*/
235235
err_t output(struct netif* ni, struct pbuf* p) override;
236236

237-
static const char eth_ifname_prefix = 'e';
238-
static uint8_t eth_id;
237+
static const char eth_ifname[];
239238
private:
240239
/*
241240
* This function is passed to the driver class and it is meant to
@@ -278,8 +277,7 @@ class CWifiStation : public CNetIf {
278277
int setLowPowerMode();
279278
int resetLowPowerMode();
280279
protected:
281-
static const char wifistation_ifname_prefix = 'w';
282-
static uint8_t wifistation_id;
280+
static const char wifistation_ifname[];
283281

284282
/*
285283
* this function is used to initialize the netif structure of lwip
@@ -322,8 +320,7 @@ class CWifiSoftAp : public CNetIf {
322320
int setLowPowerMode();
323321
int resetLowPowerMode();
324322
protected:
325-
static const char softap_ifname_prefix = 's';
326-
static uint8_t softap_id;
323+
static const char softap_ifname[];
327324
/*
328325
* this function is used to initialize the netif structure of lwip
329326
*/

0 commit comments

Comments
 (0)