1
- #ifndef _ARDUINO_LWIP_NETIF_H_
2
- #define _ARDUINO_LWIP_NETIF_H_
1
+ #pragma once
3
2
4
3
// #define LWIP_USE_TIMER
5
4
#define UNUSED (x ) (void )(x)
12
11
#include " IPAddress.h"
13
12
#include " EthernetDriver.h"
14
13
#include < string>
14
+ #include < interface.h>
15
+
15
16
#ifdef USE_LWIP_AS_LIBRARY
16
17
#include " lwip/include/lwip/dhcp.h"
17
18
#include " lwip/include/lwip/dns.h"
27
28
#include " lwIP_Arduino.h"
28
29
#endif
29
30
30
- #define LWIP_USE_TIMER
31
+ // #define LWIP_USE_TIMER
31
32
32
33
#ifdef LWIP_USE_TIMER
33
34
#include " FspTimer.h"
@@ -95,32 +96,18 @@ typedef enum {
95
96
#define TRUNCATED -3
96
97
#define INVALID_RESPONSE -4
97
98
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 ;
102
100
103
101
/* 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 {
116
103
public:
117
- CNetIf ();
118
- virtual ~CNetIf ();
104
+ CNetIf (NetworkDriver *driver= nullptr );
105
+ virtual ~CNetIf () {}
119
106
/*
120
107
* The begin function is called by the user in the sketch to initialize the network interface
121
108
* that he is planning on using in the sketch.
122
109
*/
123
- virtual void begin (
110
+ virtual int begin (
124
111
const IPAddress &ip = INADDR_NONE,
125
112
const IPAddress &nm = INADDR_NONE,
126
113
const IPAddress &gw = INADDR_NONE);
@@ -162,46 +149,72 @@ class CNetIf {
162
149
uint32_t getNmAdd () { return ip4_addr_get_u32 (&(ni.netmask )); }
163
150
uint32_t getGwAdd () { return ip4_addr_get_u32 (&(ni.gw )); }
164
151
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
+ // }
170
157
171
158
172
159
virtual int getMacAddress (uint8_t * mac) = 0;
173
- };
174
160
175
- /* -------------------------------------------------------------------------- */
176
- class CEth : public CNetIf {
177
- /* -------------------------------------------------------------------------- */
161
+ friend CLwipIf;
178
162
protected:
163
+ struct netif ni;
164
+
165
+ #ifdef LWIP_DHCP
166
+ bool dhcp_acquired;
167
+ #endif
168
+
179
169
/*
180
170
* this function is used to initialize the netif structure of lwip
181
171
*/
182
- static err_t init (struct netif * ni) override ;
172
+ virtual err_t init (struct netif * ni) = 0 ;
183
173
184
174
/*
185
175
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
186
176
*/
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 {
190
193
public:
191
- CEth ();
192
- virtual ~CEth ();
193
- virtual void begin (
194
+ CEth (NetworkDriver *driver= nullptr );
195
+ // virtual ~CEth();
196
+ virtual int begin (
194
197
const IPAddress &ip = INADDR_NONE,
195
198
const IPAddress &nm = INADDR_NONE,
196
199
const IPAddress &gw = INADDR_NONE) override ;
197
200
198
- virtual void task () override ;
199
-
200
201
virtual int getMacAddress (uint8_t * mac) override {
201
202
UNUSED (mac); // FIXME not implemented
202
203
return 1 ;
203
204
}
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 ;
204
215
216
+ static const char eth_ifname_prefix = ' e' ;
217
+ static uint8_t eth_id;
205
218
private:
206
219
/*
207
220
* This function is passed to the driver class and it is meant to
@@ -210,28 +223,11 @@ class CEth : public CNetIf {
210
223
void consume_callback (uint8_t * buffer, uint32_t len);
211
224
};
212
225
213
- /* -------------------------------------------------------------------------- */
214
226
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;
231
227
public:
232
228
CWifiStation ();
233
229
virtual ~CWifiStation ();
234
- virtual void begin (
230
+ virtual int begin (
235
231
const IPAddress &ip = INADDR_NONE,
236
232
const IPAddress &nm = INADDR_NONE,
237
233
const IPAddress &gw = INADDR_NONE) override ;
@@ -248,40 +244,62 @@ class CWifiStation : public CNetIf {
248
244
virtual uint8_t * getBSSID (uint8_t * bssid);
249
245
virtual int32_t getRSSI ();
250
246
virtual uint8_t getEncryptionType ();
251
- };
252
-
253
- /* -------------------------------------------------------------------------- */
254
- class CWifiSoftAp : public CNetIf {
255
- /* -------------------------------------------------------------------------- */
256
247
protected:
257
248
static const char wifistation_ifname_prefix = ' w' ;
258
249
static uint8_t wifistation_id;
250
+
259
251
/*
260
252
* this function is used to initialize the netif structure of lwip
261
253
*/
262
- static err_t init (struct netif * ni);
254
+ err_t init (struct netif * ni) override ;
263
255
264
256
/*
265
257
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
266
258
*/
267
- static err_t output (struct netif * ni, struct pbuf * p);
259
+ err_t output (struct netif * ni, struct pbuf * p) override ;
268
260
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 {
270
268
public:
271
269
CWifiSoftAp ();
272
270
virtual ~CWifiSoftAp ();
273
- virtual void begin (
271
+ virtual int begin (
274
272
const IPAddress &ip = INADDR_NONE,
275
273
const IPAddress &nm = INADDR_NONE,
276
274
const IPAddress &gw = INADDR_NONE) override ;
277
275
virtual void task () override ;
278
276
277
+ int startSoftAp (const char * ssid, const char * passphrase=nullptr , uint8_t channel=0 );
278
+ int stopSoftAp ();
279
+
279
280
virtual int getMacAddress (uint8_t * mac) override ;
280
281
281
282
virtual const char * getSSID ();
282
283
virtual uint8_t * getBSSID (uint8_t * bssid);
283
284
virtual int32_t getRSSI ();
284
285
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
285
303
};
286
304
287
305
class CLwipIf {
@@ -307,6 +325,7 @@ class CLwipIf {
307
325
308
326
// function for setting an iface as default
309
327
void setDefaultIface (CNetIf* iface);
328
+ // TODO get iface method
310
329
311
330
// functions that handle DNS resolution
312
331
// DNS servers are also set by dhcp
@@ -321,6 +340,7 @@ class CLwipIf {
321
340
#endif
322
341
private:
323
342
CLwipIf ();
343
+ ~CLwipIf ();
324
344
325
345
// TODO define a Timer for calling tasks
326
346
@@ -333,9 +353,7 @@ class CLwipIf {
333
353
334
354
friend class CNetIf ;
335
355
336
- #ifdef NETWORKSTACK_USE_TIMER
356
+ #ifdef LWIP_USE_TIMER
337
357
FspTimer timer;
338
358
#endif
339
359
};
340
-
341
- #endif
0 commit comments