Skip to content

Commit ec3cba5

Browse files
making netif compatible with connection handler usage
1 parent 0f430c5 commit ec3cba5

File tree

3 files changed

+76
-31
lines changed

3 files changed

+76
-31
lines changed

libraries/WiFi/src/WiFi.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ const char* CWifi::firmwareVersion() {
2323
/* -------------------------------------------------------------------------- */
2424
int CWifi::begin(const char* ssid) {
2525
/* -------------------------------------------------------------------------- */
26-
WiFiStation.begin();
27-
return WiFiStation.connectToAP(ssid, nullptr);
26+
WiFiStation.connectToAP(ssid, nullptr);
27+
return WiFiStation.begin();
2828
}
2929

3030

3131
/* -------------------------------------------------------------------------- */
3232
int CWifi::begin(const char* ssid, const char *passphrase) {
3333
/* -------------------------------------------------------------------------- */
34-
WiFiStation.begin();
35-
return WiFiStation.connectToAP(ssid, passphrase);
34+
WiFiStation.connectToAP(ssid, passphrase);
35+
return WiFiStation.begin();;
3636
}
3737

3838
/* passphrase is needed so a default one will be set */
@@ -242,6 +242,7 @@ uint8_t CWifi::channel(uint8_t networkItem) {
242242
uint8_t CWifi::status() { // FIXME
243243
/* -------------------------------------------------------------------------- */
244244
// return CLwipIf::getInstance().getWifiStatus();
245+
return WiFiStation.status();
245246
}
246247

247248
/* -------------------------------------------------------------------------- */

libraries/lwIpWrapper/src/CNetIf.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,19 @@ CWifiStation::~CWifiStation() {
597597
}
598598

599599
int CWifiStation::begin(const IPAddress &ip, const IPAddress &nm, const IPAddress &gw) {
600-
int res = 0;
600+
return CNetIf::begin(ip, nm, gw);
601+
}
602+
603+
int CWifiStation::connectToAP(const char* ssid, const char *passphrase) {
604+
WifiApCfg_t ap;
605+
int rv = ESP_CONTROL_CTRL_ERROR; // FIXME this should be set with an error meaning AP not found
606+
bool found = false;
607+
int8_t best_index = -1; // this index is used to find the ap with the best rssi
601608
int time_num = 0;
602609

603610
CEspControl::getInstance().listenForStationDisconnectEvent([this] (CCtrlMsgWrapper *resp) -> int {
604611
netif_set_link_down(&this->ni);
612+
wifi_status = WL_DISCONNECTED;
605613
return ESP_CONTROL_OK;
606614
});
607615
CEspControl::getInstance().listenForInitEvent([this] (CCtrlMsgWrapper *resp) -> int {
@@ -610,10 +618,11 @@ int CWifiStation::begin(const IPAddress &ip, const IPAddress &nm, const IPAddres
610618
return ESP_CONTROL_OK;
611619
});
612620

613-
if ((res=CEspControl::getInstance().initSpiDriver()) != 0) {
614-
res = -1; // FIXME put a proper error code
621+
if ((rv=CEspControl::getInstance().initSpiDriver()) != 0 && !hw_init) {
622+
rv = -1; // FIXME put a proper error code
615623
goto exit;
616624
}
625+
wifi_status = WL_NO_SSID_AVAIL;
617626

618627
while (time_num < 100 && !hw_init) { // TODO #define WIFI_INIT_TIMEOUT_MS 10000
619628
CEspControl::getInstance().communicateWithEsp();
@@ -622,21 +631,11 @@ int CWifiStation::begin(const IPAddress &ip, const IPAddress &nm, const IPAddres
622631
}
623632

624633
CLwipIf::getInstance().sync_timer();
625-
res = CEspControl::getInstance().setWifiMode(WIFI_MODE_STA);
634+
rv = CEspControl::getInstance().setWifiMode(WIFI_MODE_STA);
626635
CLwipIf::getInstance().enable_timer();
627636

628-
return CNetIf::begin(ip, nm, gw);
629-
exit:
630-
return res;
631-
}
632-
633-
int CWifiStation::connectToAP(const char* ssid, const char *passphrase) {
634-
WifiApCfg_t ap;
635-
int rv = ESP_CONTROL_CTRL_ERROR; // FIXME this should be set with an error meaning AP not found
636-
bool found = false;
637-
int8_t best_index = -1; // this index is used to find the ap with the best rssi
638-
639637
if((rv=this->scanForAp()) != WL_SCAN_COMPLETED) {
638+
rv = -2;
640639
goto exit;
641640
}
642641

@@ -666,8 +665,12 @@ int CWifiStation::connectToAP(const char* ssid, const char *passphrase) {
666665

667666
if (rv == ESP_CONTROL_OK) {
668667
CEspControl::getInstance().getAccessPointConfig(access_point_cfg);
668+
wifi_status = WL_CONNECTED;
669+
669670

670671
netif_set_link_up(&this->ni);
672+
} else {
673+
wifi_status = WL_CONNECT_FAILED;
671674
}
672675
CLwipIf::getInstance().enable_timer();
673676
}
@@ -685,13 +688,13 @@ int CWifiStation::scanForAp() {
685688
CLwipIf::getInstance().enable_timer();
686689

687690
if (res == ESP_CONTROL_OK) {
688-
res = WL_SCAN_COMPLETED;
691+
wifi_status = WL_SCAN_COMPLETED;
689692
} else {
690-
res = WL_NO_SSID_AVAIL;
693+
wifi_status = WL_NO_SSID_AVAIL;
691694
}
692695

693696

694-
return res;
697+
return wifi_status;
695698
}
696699

697700
// disconnect

libraries/lwIpWrapper/src/CNetIf.h

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,17 @@ typedef enum {
7979
NI_ETHERNET
8080
} NetIfType_t;
8181

82-
enum EthernetLinkStatus {
82+
enum LinkStatus {
8383
Unknown,
8484
LinkON,
8585
LinkOFF
8686
};
8787

88+
enum EthernetHardwareStatus {
89+
EthernetNoHardware,
90+
EthernetLwip = 7
91+
};
92+
8893
#define MAX_CLIENT MEMP_NUM_TCP_PCB
8994
#define MAX_DHCP_TRIES 4
9095
#define TIMEOUT_DNS_REQUEST 10000U
@@ -148,7 +153,7 @@ class CNetIf: public NetworkInterface {
148153

149154
inline int disconnect() { this->down(); return 0; }
150155

151-
inline EthernetLinkStatus linkStatus() { return netif_is_link_up(&ni) ? LinkON : LinkOFF; }
156+
inline LinkStatus linkStatus() { return netif_is_link_up(&ni) ? LinkON : LinkOFF; }
152157

153158
bool isLinkUp() { return (bool)netif_is_link_up(&ni); }
154159

@@ -164,16 +169,10 @@ class CNetIf: public NetworkInterface {
164169
IPAddress gatewayIP() { return IPAddress(this->getGwAdd()); }
165170
IPAddress dnsServerIP() { /* FIXME understand where dns should be managed */}
166171

167-
// FIXME hostname should be defined in the NetworkStack singleton
168-
// void setHostname(const char* name)
169-
// {
170-
// memset(hostname, 0x00, MAX_HOSTNAME_DIM);
171-
// memcpy(hostname, name, strlen(name) < MAX_HOSTNAME_DIM ? strlen(name) : MAX_HOSTNAME_DIM);
172-
// }
173172
void config(IPAddress _ip, IPAddress _gw, IPAddress _nm);
174173

175-
176174
virtual int getMacAddress(uint8_t* mac) = 0;
175+
virtual int setMacAddress(uint8_t* mac) = 0;
177176

178177
friend CLwipIf;
179178
protected:
@@ -216,13 +215,40 @@ class CEth : public CNetIf {
216215
const IPAddress &gw = INADDR_NONE,
217216
const IPAddress &dns = INADDR_NONE);
218217

218+
// The following are overloaded begin methods kept for retrocompatibility with other Arduino cores
219+
// Initialise the Ethernet shield to use the provided MAC address and gain the rest of the
220+
// configuration through DHCP.
221+
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
222+
virtual int begin(
223+
uint8_t *mac_address,
224+
const IPAddress &local_ip = INADDR_NONE,
225+
const IPAddress &dns_server = INADDR_NONE,
226+
const IPAddress &gateway = INADDR_NONE,
227+
const IPAddress &subnet = INADDR_NONE,
228+
const unsigned long timeout = 60000,
229+
const unsigned long responseTimeout = 4000);
230+
231+
virtual int begin(
232+
uint8_t *mac_address,
233+
const unsigned long timeout = 60000,
234+
const unsigned long responseTimeout = 4000);
235+
236+
219237
virtual int getMacAddress(uint8_t* mac) override {
220238
UNUSED(mac); // FIXME not implemented
221239
return 1;
222240
}
223241

242+
virtual int setMacAddress(uint8_t* mac) override {
243+
UNUSED(mac); // FIXME not implemented
244+
return 1;
245+
}
246+
247+
224248
int maintain() {} // Deprecated method for retrocompatibility
225249
void schedule(void) {} // Deprecated method for retrocompatibility
250+
251+
inline EthernetHardwareStatus hardwareStatus() { return EthernetLwip; }
226252
protected:
227253
/*
228254
* this function is used to initialize the netif structure of lwip
@@ -262,6 +288,11 @@ class CWifiStation : public CNetIf {
262288
// FIXME not implemented
263289
}
264290

291+
virtual int setMacAddress(uint8_t* mac) override {
292+
UNUSED(mac); // FIXME not implemented
293+
return 1;
294+
}
295+
265296
virtual const char* getSSID();
266297
virtual uint8_t* getBSSID(uint8_t* bssid);
267298
virtual int32_t getRSSI();
@@ -276,6 +307,10 @@ class CWifiStation : public CNetIf {
276307

277308
int setLowPowerMode();
278309
int resetLowPowerMode();
310+
311+
inline WifiStatus_t status() {
312+
return wifi_status;
313+
}
279314
protected:
280315
static const char wifistation_ifname[];
281316

@@ -293,6 +328,7 @@ class CWifiStation : public CNetIf {
293328
std::vector<AccessPoint_t> access_points;
294329
WifiApCfg_t access_point_cfg;
295330
bool hw_init; // TODO this should be moved to the wifi driver class
331+
WifiStatus_t wifi_status = WL_IDLE_STATUS; // TODO this should be moved to the wifi driver class
296332
};
297333

298334
class CWifiSoftAp : public CNetIf {
@@ -312,6 +348,11 @@ class CWifiSoftAp : public CNetIf {
312348
// FIXME not implemented
313349
}
314350

351+
virtual int setMacAddress(uint8_t* mac) override {
352+
UNUSED(mac); // FIXME not implemented
353+
return 1;
354+
}
355+
315356
virtual const char* getSSID();
316357
virtual uint8_t* getBSSID(uint8_t* bssid);
317358
virtual uint8_t getEncryptionType();

0 commit comments

Comments
 (0)