-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiFiService.cpp
567 lines (511 loc) · 14.4 KB
/
WiFiService.cpp
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
WiFiService
Arduino class to encapsulate UDP data service over WiFi
Author: (c) M. Naylor 2022
History:
Ver 1.0 Initial version
*/
#include <WiFiNINA.h>
#include <time.h>
#include "WiFiService.h"
#include "DoorState.h"
const char *WiFiStatus [] = { "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", "WL_NO_MODULE" }; // = 255
/*
UDP config
*/
constexpr auto WIFI_FLASHTIME = 10; // every 1/2 second
// Valid message parts
constexpr char cMsgVersion1 [] = "V001"; // message version
constexpr char TempHumidityReqMsg [] = "M001"; // Req temp / humidity
constexpr char RestartReqMsg [] = "M002"; // Req restart
constexpr char DoorStatusReqMsg [] = "M003"; // Req Door status
constexpr char DoorOpenReqMsg [] = "M004"; // Req Door Open
constexpr char DoorCloseReqMsg [] = "M005"; // Req Door Close
constexpr char DoorStopReqMsg [] = "M006"; // Req Door Stop
constexpr char DoorLightOnReqMsg [] = "M007"; // Req Light On
constexpr char DoorLightOffReqMsg [] = "M008"; // Req Light off
constexpr char PartSeparator [] = ":";
constexpr auto MAX_INCOMING_UDP_MSG = 255;
constexpr auto WIFI_CONNECT_TIMEOUT_MS = 10000;
constexpr uint16_t MulticastSendPort = 0xCE5C;
enum eResponseMessage { TEMPDATA, DOORDATA };
#define CALL_MEMBER_FN_BY_PTR( object, ptrToMember ) ( ( object )->*( ptrToMember ) )
extern void Error ( String s, bool bInISR = false );
extern void Info ( String s, bool bInISR = false );
void TerminateProgram ( const __FlashStringHelper *pErrMsg )
{
Error ( pErrMsg );
while ( true )
;
}
WiFiService::WiFiService ()
{
// Set the timezone to GMT with daylight saving time adjustments
setenv ( "TZ", "GMTGMT-1,M3.4.0/01,M10.4.0/02", 1 );
}
/// @brief Destructor to clean up the WiFiService
WiFiService::~WiFiService()
{
WiFiDisconnect();
}
const char *WiFiService::WiFiStatusToString ( uint8_t iState ) const
{
if ( iState == 255 )
{
return WiFiStatus [ 9 ];
}
else
{
if ( iState > sizeof ( WiFiStatus ) / sizeof ( WiFiStatus [ 0 ] ) - 1 )
{
return "Unknown";
}
else
{
return WiFiStatus [ iState ];
}
}
}
uint32_t WiFiService::GetBeginTimeOutCount () const
{
return m_beginTimeouts;
}
const char *WiFiService::GetHostName () const
{
return m_HostName;
}
unsigned long WiFiService::GetTime () const
{
return WiFi.getTime ();
}
WiFiService::Status WiFiService::GetState () const
{
return m_State;
}
void WiFiService::SetLED ( RGBType theColour, uint8_t flashTime )
{
if ( m_pLED != nullptr )
{
m_pLED->SetLEDColour ( theColour, flashTime );
}
}
void WiFiService::SetState ( WiFiService::Status state )
{
m_State = state;
switch ( state )
{
case WiFiService::Status::CONNECTED:
SetLED ( CONNECTED_COLOUR );
break;
case WiFiService::Status::UNCONNECTED:
SetLED ( UNCONNECTED_COLOUR, WIFI_FLASHTIME );
break;
}
}
/**
* @brief Initializes the WiFi service with the provided parameters.
*
* @param HostName The hostname to set for the WiFi connection.
* @param WiFissid The SSID of the WiFi network to connect to.
* @param WiFipwd The password of the WiFi network.
* @param pLED Pointer to an LED object for indicating status.
*/
void WiFiService::Begin ( const char *HostName, const char *WiFissid, const char *WiFipwd, MNRGBLEDBaseLib *pLED )
{
m_SSID = WiFissid;
m_Pwd = WiFipwd;
m_HostName = HostName;
m_pLED = pLED;
WiFi.setHostname ( m_HostName );
String fv = WiFi.firmwareVersion ();
if ( fv < WIFI_FIRMWARE_LATEST_VERSION )
{
SetLED ( OLD_WIFI_FIRMWARE_COLOUR );
Error ( "Please upgrade the firmware. Latest is " + String ( WIFI_FIRMWARE_LATEST_VERSION ) + ", board has " + String ( fv ) );
}
else
{
// Set the initial state to UNCONNECTED as the WiFi connection has not been established yet
SetState ( Status::UNCONNECTED );
}
}
void WiFiService::CalcMyMulticastAddress ( IPAddress &result ) const
{
CalcMulticastAddress ( WiFi.localIP (), result );
}
void WiFiService::CalcMulticastAddress ( IPAddress ip, IPAddress &subnetMask ) const
{
subnetMask = IPAddress ( 0UL ); // WiFi.subnetMask();
uint8_t firstOctet = ( ip & 0xff );
if ( firstOctet > 0 && firstOctet <= 127 )
{
// class A
subnetMask = IPAddress ( 255, 0, 0, 0 );
}
else if ( firstOctet > 127 && firstOctet <= 191 )
{
// Class B
subnetMask = IPAddress ( 255, 255, 0, 0 );
}
else if ( firstOctet > 191 && firstOctet <= 223 )
{
// Class C
subnetMask = IPAddress ( 255, 255, 255, 0 );
}
subnetMask = ( ip & subnetMask ) | ( ~subnetMask );
}
IPAddress WiFiService::GetMulticastAddress () const
{
return m_multicastAddr;
}
bool WiFiService::WiFiConnect ()
{
bool bResult = true;
static uint32_t iStartCount = 0UL;
if ( !IsConnected () )
{
Info ( "Starting WiFi, attempt " + String ( iStartCount ) );
uint8_t status;
uint32_t ulStart = millis ();
WiFi.begin ( m_SSID, m_Pwd );
String msg = "connecting ";
do
{
status = WiFi.status ();
delay ( 500 );
Info ( msg );
msg += ".";
}
while ( status != WL_CONNECTED && ( millis () - ulStart ) < WIFI_CONNECT_TIMEOUT_MS );
if ( status != WL_CONNECTED )
{
bResult = false;
SetState ( WiFiService::Status::UNCONNECTED );
Error ( "Connect failed, status is " + String ( WiFiStatusToString ( status ) ) );
iStartCount++;
m_beginTimeouts++;
}
else
{
CalcMyMulticastAddress ( m_multicastAddr );
Info ( "Connected to " + String ( m_SSID ) );
SetState ( WiFiService::Status::CONNECTED );
iStartCount = 0UL;
m_beginConnects++;
}
}
return bResult;
}
void WiFiService::WiFiDisconnect ()
{
WiFi.disconnect ();
Info ( "Disconnecting wifi" );
SetState ( WiFiService::Status::UNCONNECTED );
}
String WiFiService::ToIPString ( const IPAddress &address )
{
return String ( address [ 0 ] ) + "." + address [ 1 ] + "." + address [ 2 ] + "." + address [ 3 ];
}
bool WiFiService::IsConnected () const
{
return WiFi.status () == WL_CONNECTED;
}
uint32_t WiFiService::GetBeginCount ()
{
return m_beginConnects;
}
/***************************************************************************************************************************************/
/*
*
*
*
*
*
* UDPWiFiService Class
*
*
*
*
*
*/
/***************************************************************************************************************************************/
UDPWiFiService::UDPWiFiService ()
{
m_pMulticastDestList = new FixedIPList ( 4 );
}
bool UDPWiFiService::Begin ( UDPWiFiServiceCallback pHandleReqData, const char *WiFissid, const char *WiFipwd, const char *HostName, const uint16_t portUDP, MNRGBLEDBaseLib *pLED )
{
bool bResult = false;
WiFiService::Begin ( HostName, WiFissid, WiFipwd, pLED );
m_Port = portUDP;
m_MsgHandlerCallback = pHandleReqData;
if ( m_sUDPReceivedMsg.reserve ( MAX_INCOMING_UDP_MSG ) )
{
Start ();
bResult = true;
}
return bResult;
}
bool UDPWiFiService::Begin ( UDPWiFiServiceCallback pHandleReqData, const char *WiFissid, const char *WiFipwd, const char *HostName, MNRGBLEDBaseLib *pLED, const uint16_t portUDP )
{
return Begin ( pHandleReqData, WiFissid, WiFipwd, HostName, portUDP, pLED );
}
void UDPWiFiService::CheckUDP ()
{
String Msg = "?";
if ( GetUDPMessage ( Msg ) )
{
ProcessUDPMessage ( Msg );
}
}
/// Appends local time to provided String
void UDPWiFiService::GetLocalTime ( String &result, time_t timeError )
{
if ( timeError == 0 )
{
timeError = GetTime ();
}
if ( timeError != 0 )
{
tm localtm;
localtime_r ( &timeError, &localtm );
char sTime [ 20 ];
// Format: DD/MM/YY HH:MM:SS
sprintf ( sTime, "%02d/%02d/%02d %02d:%02d:%02d", localtm.tm_mday, localtm.tm_mon + 1, ( localtm.tm_year - 100 ), localtm.tm_hour, localtm.tm_min, localtm.tm_sec );
result += sTime;
}
}
bool UDPWiFiService::GetUDPMessage ( String &RecvMessage )
{
if ( WiFiConnect () )
{
m_pMulticastDestList->Add ( GetMulticastAddress () );
return ReadUDPMessage ( RecvMessage );
}
else
{
SetState ( WiFiService::Status::UNCONNECTED );
return false;
}
}
bool UDPWiFiService::ReadUDPMessage ( String &sRecvMessage )
{
bool bResult = false;
char sBuffer [ MAX_INCOMING_UDP_MSG ];
// if there's data available, read a packet
unsigned int packetSize = m_myUDP.parsePacket ();
if ( packetSize > 0 )
{
SetLED ( PROCESSING_MSG_COLOUR );
delay ( 500 );
String logMessage = "Received packet of size " + String ( packetSize ) + " From " + ToIPString ( m_myUDP.remoteIP () ) + ", port " + String ( m_myUDP.remotePort () );
// Info ( logMessage ) ;
if ( packetSize < sizeof ( sBuffer ) - 1 )
{
// read the packet into packetBufffer
int len = m_myUDP.read ( sBuffer, sizeof ( sBuffer ) - 1 );
if ( len >= 0 )
{
sBuffer [ len ] = 0;
sRecvMessage = sBuffer;
bResult = true;
m_ulReqCount++;
}
else
{
Error("Failed to read UDP packet");
}
// create multicast address from send ip and add to list of subnets to send multicasts to and add to list
IPAddress result;
CalcMulticastAddress ( m_myUDP.remoteIP (), result );
m_pMulticastDestList->Add ( result );
}
else
{
m_ulBadRequests++;
}
}
return bResult;
}
bool UDPWiFiService::Start ()
{
bool bResult = false;
if ( m_myUDP.begin ( m_Port ) == 1 )
{
bResult = true;
// Error ( "Started UDP" );
IPAddress localSubnet = GetMulticastAddress ();
if ( (long unsigned int)localSubnet != 0UL )
{
m_pMulticastDestList->Add ( localSubnet );
}
}
else
{
Error ( "Unable to allocate UDP Port, restarting" );
delay ( 1000 * 20 );
MN::Utils::ResetBoard ( F ( "" ) );
}
return bResult;
}
uint32_t UDPWiFiService::GetMCastSentCount ()
{
return m_ulMCastSentCount;
}
uint32_t UDPWiFiService::GetRequestsReceivedCount ()
{
return m_ulReqCount;
}
bool UDPWiFiService::SendReply ( String sMsg )
{
bool bResult = false;
if ( WiFiConnect () )
{
if ( sMsg.length () > 0 )
{
if ( m_myUDP.beginPacket ( m_myUDP.remoteIP (), m_myUDP.remotePort () ) == 1 )
{
m_myUDP.write ( sMsg.c_str () );
if ( m_myUDP.endPacket () == 0 )
{
Error ( "Message Response failed" );
WiFiDisconnect ();
}
else
{
m_ulReplyCount++;
// Error ( "Sent " + String ( pMsg->substring(0, pMsg->length()-1) ) + " to " + ToIPString ( m_myUDP.remoteIP () ) + ":" + m_myUDP.remotePort() );
SetState ( WiFiService::Status::CONNECTED );
bResult = true;
}
}
else
{
Error ( "Unable to send UDP message, beginpacket() failed sending to : " + ToIPString ( m_myUDP.remoteIP () ) + " : " + m_myUDP.remotePort () );
}
}
else
{
Error ( "Empty reply to be sent" );
}
}
return bResult;
}
/// @brief Checks WiFi connected and attempts to connect if not. If connected then will look for a message and store in parameter
/// @param paramPtr pointer to String to receive any available message
/// @return
bool UDPWiFiService::SendAll ( String sMsg )
{
bool bResult = false;
if ( WiFiConnect () )
{
if ( sMsg.length () > 0 )
{
uint8_t iterator = m_pMulticastDestList->GetIterator ();
IPAddress nextIP;
while ( (long unsigned int)( nextIP = m_pMulticastDestList->GetNext ( iterator ) ) != 0UL )
{
delay ( 200 );
if ( m_myUDP.beginPacket ( nextIP, MulticastSendPort ) == 1 )
{
m_myUDP.write ( sMsg.c_str () );
if ( m_myUDP.endPacket () == 0 )
{
Error ( "Multicast Message failed" );
WiFiDisconnect ();
}
else
{
SetLED ( PROCESSING_MSG_COLOUR );
delay ( 500 );
SetState ( WiFiService::Status::CONNECTED );
bResult = true;
m_ulMCastSentCount++;
}
}
}
}
else
{
Error ( "Error: Empty message to be sent" );
}
}
return bResult;
}
uint32_t UDPWiFiService::GetReplySentCount ()
{
return m_ulReplyCount;
}
FixedIPList *UDPWiFiService::GetMulticastList ()
{
return m_pMulticastDestList;
}
/// @brief Releases UDP port and disconnects from WiFi
void UDPWiFiService::Stop ()
{
Info ( "Stopping WiFI" );
m_myUDP.stop ();
WiFiDisconnect ();
}
/// @brief Processes the UDP message that has been received
/// @param sRecvMessage String containing the messade received
void UDPWiFiService::ProcessUDPMessage ( const String &sRecvMessage )
{
if ( sRecvMessage.startsWith ( cMsgVersion1 ) )
{
// Version 1 message received
if ( sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 2 ).startsWith ( TempHumidityReqMsg ) )
{
// Got a data request
// Error ( "Temp Data request" );
m_MsgHandlerCallback ( UDPWiFiService::ReqMsgType::TEMPDATA );
}
else if ( sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 2 ).startsWith ( RestartReqMsg ) )
{
// Got a reset request
MN::Utils::ResetBoard ( F ( "Reset request" ) );
}
else if ( sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 2 ).startsWith ( DoorStatusReqMsg ) )
{
// Got a door status request
// Error ( "Door Data request" );
m_MsgHandlerCallback ( UDPWiFiService::ReqMsgType::DOORDATA );
}
else if ( sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 2 ).startsWith ( DoorOpenReqMsg ) )
{
// Error ( "Door Open request" );
m_MsgHandlerCallback ( UDPWiFiService::ReqMsgType::DOOROPEN );
}
else if ( sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 2 ).startsWith ( DoorCloseReqMsg ) )
{
// Error ( "Door Close request" );
m_MsgHandlerCallback ( UDPWiFiService::ReqMsgType::DOORCLOSE );
}
else if ( sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 2 ).startsWith ( DoorStopReqMsg ) )
{
// Error ( "Door Stop request" );
m_MsgHandlerCallback ( UDPWiFiService::ReqMsgType::DOORSTOP );
}
else if ( sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 2 ).startsWith ( DoorLightOnReqMsg ) )
{
// Error ( "Light On request" );
m_MsgHandlerCallback ( UDPWiFiService::ReqMsgType::LIGHTON );
}
else if ( sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 2 ).startsWith ( DoorLightOffReqMsg ) )
{
// Info ( "Light Off request" );
m_MsgHandlerCallback ( UDPWiFiService::ReqMsgType::LIGHTOFF );
}
else
{
m_ulBadRequests++;
Error ( "Unknown request : " + sRecvMessage.substring ( sizeof ( cMsgVersion1 ) + sizeof ( PartSeparator ) - 1 ) );
}
}
else
{
m_ulBadMgsVersion++;
Error ( "Unknown message version : " + sRecvMessage );
}
}