-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathCNetIf.h
429 lines (349 loc) · 11.4 KB
/
CNetIf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#ifndef _ARDUINO_LWIP_NETIF_H_
#define _ARDUINO_LWIP_NETIF_H_
// #define LWIP_USE_TIMER
#define UNUSED(x) (void)(x)
#define USE_LWIP_AS_LIBRARY
#include "Arduino.h"
#include "CCtrlWrapper.h"
#include "CEspControl.h"
#include "IPAddress.h"
#include "EthernetDriver.h"
#include <string>
#ifdef USE_LWIP_AS_LIBRARY
#include "lwip/include/lwip/dhcp.h"
#include "lwip/include/lwip/dns.h"
#include "lwip/include/lwip/init.h"
#include "lwip/include/lwip/ip_addr.h"
#include "lwip/include/lwip/opt.h"
#include "lwip/include/lwip/prot/dhcp.h"
#include "lwip/include/lwip/tcp.h"
#include "lwip/include/lwip/timeouts.h"
#include "lwip/include/lwip/udp.h"
#include "lwip/include/netif/ethernet.h"
#else
#include "lwIP_Arduino.h"
#endif
#define LWIP_USE_TIMER
#ifdef LWIP_USE_TIMER
#include "FspTimer.h"
#endif
#define MAX_SOFAT_CONNECTION_DEF 5
#define MAC_ADDRESS_DIM 6
#define NETWORK_INTERFACES_MAX_NUM 3
#define MAX_HOSTNAME_DIM 253
#define WIFI_INIT_TIMEOUT_MS 10000
#define WL_MAC_ADDR_LENGTH 6
/* DEFAULT ADDRESS FOR ETHERNET CONFIGURATION */
#define ETH_IFNAME0 'e'
#define ETH_IFNAME1 't'
#define WST_IFNAME0 'w'
#define WST_IFNAME1 'f'
#define WSA_IFNAME0 'w'
#define WSA_IFNAME1 'a'
typedef enum {
WL_NO_SHIELD = 255,
WL_NO_MODULE = WL_NO_SHIELD,
WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL,
WL_SCAN_COMPLETED,
WL_CONNECTED,
WL_CONNECT_FAILED,
WL_CONNECTION_LOST,
WL_DISCONNECTED,
WL_AP_LISTENING,
WL_AP_CONNECTED,
WL_AP_FAILED
} WifiStatus_t;
/* Encryption modes */
enum wl_enc_type {
ENC_TYPE_WEP,
ENC_TYPE_WPA,
ENC_TYPE_WPA2,
ENC_TYPE_WPA2_ENTERPRISE,
ENC_TYPE_WPA3,
ENC_TYPE_NONE,
ENC_TYPE_AUTO,
ENC_TYPE_UNKNOWN = 255
};
typedef enum {
NI_WIFI_STATION,
NI_WIFI_SOFTAP,
NI_ETHERNET
} NetIfType_t;
#define MAX_CLIENT 32
#define MAX_DHCP_TRIES 4
#define TIMEOUT_DNS_REQUEST 10000U
class CNetIf;
using NetIfRxCb_f = int (*)(CNetIf*);
using LwipInit_f = err_t (*)(struct netif* netif);
using LwipInput_f = err_t (*)(struct pbuf* p, struct netif* inp);
#define DHCP_CHECK_NONE (0)
#define DHCP_CHECK_RENEW_FAIL (1)
#define DHCP_CHECK_RENEW_OK (2)
#define DHCP_CHECK_REBIND_FAIL (3)
#define DHCP_CHECK_REBIND_OK (4)
#define TIMED_OUT -1
#define INVALID_SERVER -2
#define TRUNCATED -3
#define INVALID_RESPONSE -4
typedef enum {
DHCP_IDLE_STATUS,
DHCP_START_STATUS,
DHCP_WAIT_STATUS,
DHCP_GOT_STATUS,
DHCP_RELEASE_STATUS,
DHCP_STOP_STATUS
} DhcpSt_t;
ip_addr_t* u8_to_ip_addr(uint8_t* ipu8, ip_addr_t* ipaddr);
uint32_t ip_addr_to_u32(ip_addr_t* ipaddr);
/* Base class implements DHCP, derived class will switch it on or off */
/* -------------------------------------------------------------------------- */
class CNetIf {
/* -------------------------------------------------------------------------- */
protected:
int id;
struct netif ni;
#if LWIP_NETIF_HOSTNAME
char hostname[MAX_HOSTNAME_DIM];
#endif
ip_addr_t ip;
ip_addr_t nm;
ip_addr_t gw;
/* these can be overridden by a config() function called before begin() */
static IPAddress default_ip;
static IPAddress default_nm;
static IPAddress default_gw;
static IPAddress default_dhcp_server_ip;
unsigned long dhcp_timeout;
DhcpSt_t dhcp_st;
bool dhcp_started;
bool dhcp_acquired;
uint8_t _dhcp_lease_state;
void dhcp_task();
void dhcp_reset();
bool dhcp_request();
uint8_t dhcp_get_lease_state();
IPAddress _dnsServerAddress;
public:
CNetIf();
virtual ~CNetIf();
/* --------------
* DHCP functions
* -------------- */
bool DhcpIsStarted() { return dhcp_started; }
void DhcpSetTimeout(unsigned long t);
/* stops DHCP */
void DhcpStop();
/* tells DHCP is not used on that interface */
void DhcpNotUsed();
/* starts DHCP and tries to acquire addresses, return true if acquired, false otherwise */
bool DhcpStart();
/* tells if DHCP has acquired addresses or not */
bool isDhcpAcquired();
int checkLease();
virtual void setLinkUp();
virtual void setLinkDown();
bool isLinkUp() { return (bool)netif_is_link_up(&ni); }
/* getters / setters */
void setId(int _id) { id = _id; }
int getId() { return id; }
struct netif* getNi() { return ∋ }
uint32_t getIpAdd() { return ip4_addr_get_u32(&(ni.ip_addr)); }
uint32_t getNmAdd() { return ip4_addr_get_u32(&(ni.netmask)); }
uint32_t getGwAdd() { return ip4_addr_get_u32(&(ni.gw)); }
void setHostname(const char* name)
{
memset(hostname, 0x00, MAX_HOSTNAME_DIM);
memcpy(hostname, name, strlen(name) < MAX_HOSTNAME_DIM ? strlen(name) : MAX_HOSTNAME_DIM);
}
/* add */
virtual void begin(IPAddress _ip,
IPAddress _gw,
IPAddress _nm)
= 0;
virtual void task() = 0;
virtual int getMacAddress(uint8_t* mac) = 0;
/* default dummy implementation because ethernet does not have that */
virtual const char* getSSID() { return nullptr; }
virtual uint8_t* getBSSID(uint8_t* bssid) { return nullptr; }
virtual int32_t getRSSI() { return 0; }
virtual uint8_t getEncryptionType() { return 0; }
friend class CWifi;
};
/* -------------------------------------------------------------------------- */
class CEth : public CNetIf {
/* -------------------------------------------------------------------------- */
protected:
/*
* this function is used to initialize the netif structure of lwip
*/
static err_t init(struct netif* ni);
/*
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
*/
static err_t output(struct netif* ni, struct pbuf* p);
public:
CEth();
virtual ~CEth();
virtual void begin(IPAddress _ip,
IPAddress _gw,
IPAddress _nm) override;
virtual void task() override;
virtual int getMacAddress(uint8_t* mac)
{
UNUSED(mac);
return 1;
}
virtual void handleEthRx();
};
/* -------------------------------------------------------------------------- */
class CWifiStation : public CNetIf {
/* -------------------------------------------------------------------------- */
protected:
/*
* this function is used to initialize the netif structure of lwip
*/
static err_t init(struct netif* ni);
/*
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
*/
static err_t output(struct netif* ni, struct pbuf* p);
public:
CWifiStation();
virtual ~CWifiStation();
virtual void begin(IPAddress _ip,
IPAddress _gw,
IPAddress _nm) override;
virtual void task() override;
virtual int getMacAddress(uint8_t* mac) override;
virtual const char* getSSID() override;
virtual uint8_t* getBSSID(uint8_t* bssid) override;
virtual int32_t getRSSI() override;
virtual uint8_t getEncryptionType() override;
};
/* -------------------------------------------------------------------------- */
class CWifiSoftAp : public CNetIf {
/* -------------------------------------------------------------------------- */
protected:
/*
* this function is used to initialize the netif structure of lwip
*/
static err_t init(struct netif* ni);
/*
* This function is passed to lwip and used to send a buffer to the driver in order to transmit it
*/
static err_t output(struct netif* ni, struct pbuf* p);
public:
CWifiSoftAp();
virtual ~CWifiSoftAp();
virtual void begin(IPAddress _ip,
IPAddress _gw,
IPAddress _nm) override;
virtual void task() override;
virtual int getMacAddress(uint8_t* mac) override;
virtual const char* getSSID() override;
virtual uint8_t* getBSSID(uint8_t* bssid) override;
virtual int32_t getRSSI() override;
virtual uint8_t getEncryptionType() override;
};
/* -------------------------------------------------------------------------- */
class CLwipIf {
/* -------------------------------------------------------------------------- */
private:
bool eth_initialized;
bool willing_to_start_sync_req;
bool async_requests_ongoing;
friend CWifiStation;
friend CWifiSoftAp;
static CNetIf* net_ifs[NETWORK_INTERFACES_MAX_NUM];
static WifiStatus_t wifi_status;
/* initialize lwIP and timer */
CLwipIf();
/* timer */
#ifdef LWIP_USE_TIMER
static FspTimer timer;
static void timer_cb(timer_callback_args_t* arg);
#endif
std::vector<AccessPoint_t> access_points;
WifiApCfg_t access_point_cfg;
SoftApCfg_t soft_ap_cfg;
static bool wifi_hw_initialized;
static bool connected_to_access_point;
static int initEventCb(CCtrlMsgWrapper* resp);
static bool initWifiHw(bool asStation);
static bool pending_eth_rx;
static int disconnectEventcb(CCtrlMsgWrapper* resp);
static void dns_callback(const char* name, const ip_addr_t* ipaddr, void* callback_arg);
int8_t get_ip_address_from_hostname(const char* hostname, uint32_t* ipaddr);
int inet2aton(const char* aIPAddrString, IPAddress& aResult);
public:
static CLwipIf& getInstance();
CLwipIf(CLwipIf const&) = delete;
void operator=(CLwipIf const&) = delete;
~CLwipIf();
bool isEthInitialized() { return eth_initialized; }
void startSyncRequest()
{
if (async_requests_ongoing) {
synchronized
{
willing_to_start_sync_req = true;
}
while (willing_to_start_sync_req) {
delay(1);
}
}
}
void restartAsyncRequest()
{
async_requests_ongoing = true;
delay(10);
timer.enable_overflow_irq();
}
/* --------------
* DNS functions
* -------------- */
int getHostByName(const char* aHostname, IPAddress& aResult);
void beginDns(IPAddress aDNSServer);
void setDns(uint8_t num, IPAddress aDNSServer);
IPAddress getDns(int _num = 0);
/* when you 'get' a network interface, you get a pointer to one of the pointers
held by net_ifs array
if the array element then an attempt to set up the network interface is made
this function actually calls the private function setUp... and that ones
call the private _get */
CNetIf* get(NetIfType_t type,
IPAddress _ip = INADDR_NONE,
IPAddress _gw = INADDR_NONE,
IPAddress _nm = INADDR_NONE);
static void ethLinkUp();
static void ethLinkDown();
/* this function set the mac address of the corresponding interface to mac
and set this value for lwip */
bool setMacAddress(NetIfType_t type, uint8_t* mac = nullptr);
int getMacAddress(NetIfType_t type, uint8_t* mac);
int scanForAp();
int getApNum();
const char* getSSID(uint8_t i);
int32_t getRSSI(uint8_t i);
uint8_t getEncrType(uint8_t i);
uint8_t* getBSSID(uint8_t i, uint8_t* bssid);
uint8_t getChannel(uint8_t i);
int connectToAp(const char* ssid, const char* pwd);
int disconnectFromAp();
const char* getSSID();
uint8_t* getBSSID(uint8_t* bssid);
uint32_t getRSSI();
uint8_t getEncrType();
WifiStatus_t getWifiStatus() { return wifi_status; }
int startSoftAp(const char* ssid, const char* passphrase, uint8_t channel);
int setLowPowerMode();
int resetLowPowerMode();
const char* getSSID(NetIfType_t type);
uint8_t* getBSSID(NetIfType_t type, uint8_t* bssid);
int32_t getRSSI(NetIfType_t type);
uint8_t getEncryptionType(NetIfType_t type);
int setWifiMode(WifiMode_t mode);
void lwip_task();
};
#endif