-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandsHandler.cpp
327 lines (239 loc) · 6.08 KB
/
CommandsHandler.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
#include <EEPROM.h>
#include "CRC.hpp"
#include "CommandsHandler.hpp"
#include "Logger.hpp"
namespace Commands {
namespace Handler {
/***********/
/* Private */
/***********/
unsigned long m_nLastBlinkTime = 0; // 0 = Blinking disabled
String m_sDeviceID = "";
RawDeviceID m_szTmpDeviceID = ""; // Declared here to avoid stack overrun
LOG_DECLARE_BUFFER();
bool GetLED()
{
return (HIGH == digitalRead(PIN_LED));
}
void SetLED(bool bState)
{
digitalWrite(PIN_LED, (bState ? HIGH : LOW));
}
void ToggleLED()
{
SetLED(!GetLED());
}
void FlashLED()
{
bool bOldState = GetLED();
for (int nIndex = 0; nIndex < (2 * LED_FLASH_COUNT); nIndex++)
{
ToggleLED();
delay(LED_FLASH_TIMEOUT);
}
SetLED(bOldState);
}
void BlinkLEDStart()
{
m_nLastBlinkTime = micros();
}
void BlinkLEDStop()
{
m_nLastBlinkTime = 0;
}
bool CheckPairingButtonPressed()
{
bool bPressed = (LOW == digitalRead(PIN_BUTTON));
if (!bPressed)
{
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_ERRORS
LOG("Command: pairing button not pressed.");
#endif
}
return bPressed;
}
void ReadDeviceID()
{
// Reset device ID
m_sDeviceID = "";
BlinkLEDStart();
// Read device ID
memset(m_szTmpDeviceID, 0, sizeof(m_szTmpDeviceID));
EEPROM.get(0, m_szTmpDeviceID);
size_t nLength = strlen(m_szTmpDeviceID);
if ((0 == nLength) || (nLength > MAX_DEVICE_ID_LENGTH))
{
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_ERRORS
LOG_FMT(
"Command: invalid stored ID (size %d).",
nLength
);
#endif
return;
}
// Check CRC
uint16_t nStoredCRC;
EEPROM.get(sizeof(m_szTmpDeviceID), nStoredCRC);
uint16_t nCalculatedCRC = CRC16(
(const unsigned char*)m_szTmpDeviceID,
nLength
);
if (nStoredCRC != nCalculatedCRC)
{
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_ERRORS
LOG_FMT(
"Command: bad CRC (stored = 0x%04X, calc = 0x%04X).",
nStoredCRC,
nCalculatedCRC
);
#endif
return;
}
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_INFO
LOG_FMT(
"Command: read ID \"%s\", CRC = 0x%04X.",
m_szTmpDeviceID,
nStoredCRC
);
#endif
// Store valid device ID
m_sDeviceID = m_szTmpDeviceID;
// Final check
if (IsPaired())
{
BlinkLEDStop();
SetLED(false);
}
}
void ExecCommand_Pair(String sDeviceID)
{
if (CheckPairingButtonPressed())
{
memset(m_szTmpDeviceID, 0, sizeof(m_szTmpDeviceID));
sDeviceID.toCharArray(m_szTmpDeviceID, sizeof(m_szTmpDeviceID));
EEPROM.put(0, m_szTmpDeviceID);
uint16_t nCRC = CRC16(
(const unsigned char*)m_szTmpDeviceID,
sDeviceID.length()
);
EEPROM.put(sizeof(m_szTmpDeviceID), nCRC);
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_INFO
LOG_FMT(
"Command: ID \"%s\", CRC = 0x%04X written.",
m_szTmpDeviceID,
nCRC
);
#endif
m_sDeviceID = sDeviceID;
BlinkLEDStop();
SetLED(false);
}
}
void ExecCommand_Switch(bool bStatus)
{
digitalWrite(PIN_RELAY, (bStatus ? LOW : HIGH));
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_INFO
LOG_FMT(
"Command: relay switched %s.",
(bStatus ? "on" : "off")
);
#endif
}
void ExecCommand_Unpair()
{
if (CheckPairingButtonPressed())
{
for (unsigned int nIndex = 0 ; nIndex < EEPROM.length(); nIndex++)
{
EEPROM.write(nIndex, 0);
}
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_INFO
LOG_FMT(
"Command: EEPROM (size %d) cleared.",
EEPROM.length()
);
#endif
m_sDeviceID = "";
ExecCommand_Switch(false);
BlinkLEDStart();
}
}
/**********/
/* Public */
/**********/
void Initialize()
{
// LED
pinMode(PIN_LED, OUTPUT);
// Relay
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_RELAY, HIGH);
// Button
pinMode(PIN_BUTTON, INPUT_PULLUP);
// Device ID
ReadDeviceID();
}
bool IsPaired()
{
return m_sDeviceID.length();
}
void ProcessCommand(String sCommand)
{
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_INFO
LOG_FMT(
"Command: handling command \"%s\"...",
sCommand.c_str()
);
#endif
Command eCommand;
String sDeviceID = m_sDeviceID;
if (!Parse(sCommand, sDeviceID, eCommand))
{
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_ERRORS
LOG_FMT(
"Command: command \"%s\" not parsed.",
sCommand.c_str()
);
#endif
}
else
{
switch(eCommand)
{
case Command::SwitchOn :
ExecCommand_Switch(true);
break;
case Command::SwitchOff :
ExecCommand_Switch(false);
break;
case Command::Pair :
ExecCommand_Pair(sDeviceID);
break;
case Command::Unpair :
ExecCommand_Unpair();
break;
default:
#if HANDLER_LOG_LEVEL & HANDLER_LOG_MASK_ERRORS
LOG_FMT(
"Command: unknown command ID \"%d\".",
(int)eCommand
);
#endif
}
FlashLED();
}
}
void BlinkLED()
{
if (0 != m_nLastBlinkTime)
{
unsigned long nTimeDiff = (micros() - m_nLastBlinkTime);
if (nTimeDiff >= (unsigned long)LED_BLINK_TIMEOUT)
{
ToggleLED();
m_nLastBlinkTime = micros();
}
}
}
}
}