Skip to content

Commit 8289f49

Browse files
cleaning CNetIf classes definition
1 parent 49048cf commit 8289f49

File tree

1 file changed

+88
-70
lines changed

1 file changed

+88
-70
lines changed

libraries/lwIpWrapper/src/CNetIf.h

+88-70
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef _ARDUINO_LWIP_NETIF_H_
2-
#define _ARDUINO_LWIP_NETIF_H_
1+
#pragma once
32

43
// #define LWIP_USE_TIMER
54
#define UNUSED(x) (void)(x)
@@ -12,6 +11,8 @@
1211
#include "IPAddress.h"
1312
#include "EthernetDriver.h"
1413
#include <string>
14+
#include <interface.h>
15+
1516
#ifdef USE_LWIP_AS_LIBRARY
1617
#include "lwip/include/lwip/dhcp.h"
1718
#include "lwip/include/lwip/dns.h"
@@ -27,7 +28,7 @@
2728
#include "lwIP_Arduino.h"
2829
#endif
2930

30-
#define LWIP_USE_TIMER
31+
// #define LWIP_USE_TIMER
3132

3233
#ifdef LWIP_USE_TIMER
3334
#include "FspTimer.h"
@@ -95,32 +96,18 @@ typedef enum {
9596
#define TRUNCATED -3
9697
#define INVALID_RESPONSE -4
9798

98-
99-
ip_addr_t* u8_to_ip_addr(uint8_t* ipu8, ip_addr_t* ipaddr);
100-
101-
uint32_t ip_addr_to_u32(ip_addr_t* ipaddr);
99+
class CLwipIf;
102100

103101
/* Base class implements DHCP, derived class will switch it on or off */
104-
class CNetIf {
105-
protected:
106-
struct netif ni;
107-
108-
#ifdef LWIP_DHCP
109-
bool dhcp_acquired;
110-
#endif
111-
112-
// IPAddress _dnsServerAddress;
113-
114-
// Driver interface pointer
115-
NetworkDriver *driver = nullptr;
102+
class CNetIf: public NetworkInterface {
116103
public:
117-
CNetIf();
118-
virtual ~CNetIf();
104+
CNetIf(NetworkDriver *driver=nullptr);
105+
virtual ~CNetIf() {}
119106
/*
120107
* The begin function is called by the user in the sketch to initialize the network interface
121108
* that he is planning on using in the sketch.
122109
*/
123-
virtual void begin(
110+
virtual int begin(
124111
const IPAddress &ip = INADDR_NONE,
125112
const IPAddress &nm = INADDR_NONE,
126113
const IPAddress &gw = INADDR_NONE);
@@ -162,46 +149,72 @@ class CNetIf {
162149
uint32_t getNmAdd() { return ip4_addr_get_u32(&(ni.netmask)); }
163150
uint32_t getGwAdd() { return ip4_addr_get_u32(&(ni.gw)); }
164151

165-
void setHostname(const char* name)
166-
{
167-
memset(hostname, 0x00, MAX_HOSTNAME_DIM);
168-
memcpy(hostname, name, strlen(name) < MAX_HOSTNAME_DIM ? strlen(name) : MAX_HOSTNAME_DIM);
169-
}
152+
// void setHostname(const char* name)
153+
// {
154+
// memset(hostname, 0x00, MAX_HOSTNAME_DIM);
155+
// memcpy(hostname, name, strlen(name) < MAX_HOSTNAME_DIM ? strlen(name) : MAX_HOSTNAME_DIM);
156+
// }
170157

171158

172159
virtual int getMacAddress(uint8_t* mac) = 0;
173-
};
174160

175-
/* -------------------------------------------------------------------------- */
176-
class CEth : public CNetIf {
177-
/* -------------------------------------------------------------------------- */
161+
friend CLwipIf;
178162
protected:
163+
struct netif ni;
164+
165+
#ifdef LWIP_DHCP
166+
bool dhcp_acquired;
167+
#endif
168+
179169
/*
180170
* this function is used to initialize the netif structure of lwip
181171
*/
182-
static err_t init(struct netif* ni) override;
172+
virtual err_t init(struct netif* ni) = 0;
183173

184174
/*
185175
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
186176
*/
187-
static err_t output(struct netif* ni, struct pbuf* p) override;
188-
static const char eth_ifname_prefix = 'e';
189-
static uint8_t eth_id;
177+
virtual err_t output(struct netif* ni, struct pbuf* p) = 0;
178+
179+
// the following functions are used to call init and output from lwip in the object context in the C code
180+
friend err_t _netif_init(struct netif* ni);
181+
friend err_t _netif_output(struct netif* ni, struct pbuf* p);
182+
183+
// IPAddress _dnsServerAddress;
184+
185+
// Driver interface pointer
186+
NetworkDriver *driver = nullptr;
187+
188+
void linkDownCallback();
189+
void linkUpCallback();
190+
};
191+
192+
class CEth : public CNetIf {
190193
public:
191-
CEth();
192-
virtual ~CEth();
193-
virtual void begin(
194+
CEth(NetworkDriver *driver=nullptr);
195+
// virtual ~CEth();
196+
virtual int begin(
194197
const IPAddress &ip = INADDR_NONE,
195198
const IPAddress &nm = INADDR_NONE,
196199
const IPAddress &gw = INADDR_NONE) override;
197200

198-
virtual void task() override;
199-
200201
virtual int getMacAddress(uint8_t* mac) override {
201202
UNUSED(mac); // FIXME not implemented
202203
return 1;
203204
}
205+
protected:
206+
/*
207+
* this function is used to initialize the netif structure of lwip
208+
*/
209+
err_t init(struct netif* ni) override;
210+
211+
/*
212+
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
213+
*/
214+
err_t output(struct netif* ni, struct pbuf* p) override;
204215

216+
static const char eth_ifname_prefix = 'e';
217+
static uint8_t eth_id;
205218
private:
206219
/*
207220
* This function is passed to the driver class and it is meant to
@@ -210,28 +223,11 @@ class CEth : public CNetIf {
210223
void consume_callback(uint8_t* buffer, uint32_t len);
211224
};
212225

213-
/* -------------------------------------------------------------------------- */
214226
class CWifiStation : public CNetIf {
215-
/* -------------------------------------------------------------------------- */
216-
protected:
217-
static const char wifistation_ifname_prefix = 'w';
218-
static uint8_t wifistation_id;
219-
220-
/*
221-
* this function is used to initialize the netif structure of lwip
222-
*/
223-
static err_t init(struct netif* ni) override;
224-
225-
/*
226-
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
227-
*/
228-
static err_t output(struct netif* ni, struct pbuf* p) override;
229-
230-
WifiApCfg_t access_point_cfg;
231227
public:
232228
CWifiStation();
233229
virtual ~CWifiStation();
234-
virtual void begin(
230+
virtual int begin(
235231
const IPAddress &ip = INADDR_NONE,
236232
const IPAddress &nm = INADDR_NONE,
237233
const IPAddress &gw = INADDR_NONE) override;
@@ -248,40 +244,62 @@ class CWifiStation : public CNetIf {
248244
virtual uint8_t* getBSSID(uint8_t* bssid);
249245
virtual int32_t getRSSI();
250246
virtual uint8_t getEncryptionType();
251-
};
252-
253-
/* -------------------------------------------------------------------------- */
254-
class CWifiSoftAp : public CNetIf {
255-
/* -------------------------------------------------------------------------- */
256247
protected:
257248
static const char wifistation_ifname_prefix = 'w';
258249
static uint8_t wifistation_id;
250+
259251
/*
260252
* this function is used to initialize the netif structure of lwip
261253
*/
262-
static err_t init(struct netif* ni);
254+
err_t init(struct netif* ni) override;
263255

264256
/*
265257
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
266258
*/
267-
static err_t output(struct netif* ni, struct pbuf* p);
259+
err_t output(struct netif* ni, struct pbuf* p) override;
268260

269-
SoftApCfg_t soft_ap_cfg;
261+
private:
262+
std::vector<AccessPoint_t> access_points;
263+
WifiApCfg_t access_point_cfg;
264+
bool hw_init; // TODO this should be moved to the wifi driver class
265+
};
266+
267+
class CWifiSoftAp : public CNetIf {
270268
public:
271269
CWifiSoftAp();
272270
virtual ~CWifiSoftAp();
273-
virtual void begin(
271+
virtual int begin(
274272
const IPAddress &ip = INADDR_NONE,
275273
const IPAddress &nm = INADDR_NONE,
276274
const IPAddress &gw = INADDR_NONE) override;
277275
virtual void task() override;
278276

277+
int startSoftAp(const char* ssid, const char* passphrase=nullptr, uint8_t channel=0);
278+
int stopSoftAp();
279+
279280
virtual int getMacAddress(uint8_t* mac) override;
280281

281282
virtual const char* getSSID();
282283
virtual uint8_t* getBSSID(uint8_t* bssid);
283284
virtual int32_t getRSSI();
284285
virtual uint8_t getEncryptionType();
286+
protected:
287+
static const char softap_ifname_prefix = 's';
288+
static uint8_t softap_id;
289+
/*
290+
* this function is used to initialize the netif structure of lwip
291+
*/
292+
err_t init(struct netif* ni);
293+
294+
/*
295+
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
296+
*/
297+
err_t output(struct netif* ni, struct pbuf* p);
298+
299+
private:
300+
std::vector<AccessPoint_t> access_points;
301+
SoftApCfg_t soft_ap_cfg;
302+
bool hw_init; // TODO this should be moved to the wifi driver class
285303
};
286304

287305
class CLwipIf {
@@ -307,6 +325,7 @@ class CLwipIf {
307325

308326
// function for setting an iface as default
309327
void setDefaultIface(CNetIf* iface);
328+
// TODO get iface method
310329

311330
// functions that handle DNS resolution
312331
// DNS servers are also set by dhcp
@@ -321,6 +340,7 @@ class CLwipIf {
321340
#endif
322341
private:
323342
CLwipIf();
343+
~CLwipIf();
324344

325345
// TODO define a Timer for calling tasks
326346

@@ -333,9 +353,7 @@ class CLwipIf {
333353

334354
friend class CNetIf;
335355

336-
#ifdef NETWORKSTACK_USE_TIMER
356+
#ifdef LWIP_USE_TIMER
337357
FspTimer timer;
338358
#endif
339359
};
340-
341-
#endif

0 commit comments

Comments
 (0)