-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathConfigurationMode.cpp
More file actions
415 lines (347 loc) · 11.4 KB
/
ConfigurationMode.cpp
File metadata and controls
415 lines (347 loc) · 11.4 KB
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
#include <Arduino.h>
#include "ConfigurationMode.h"
#include "Storage.h"
#include <Network/Signature.h>
#define END_CHAR ('\r')
#define TAB_CHAR ('\t')
#define SPACE_CHAR (' ')
#define BACKSPACE_CHAR (0x08)
#define DEL_CHAR (0x7f)
#define MAX_CMD_ARG (4)
#define DLM "\r\n"
#define PROMPT DLM "# "
#define INBUF_SIZE (1024)
static Storage* Storage_;
struct console_command
{
const char *name;
const char *help;
void (*function) (int argc, char **argv);
};
static void help_command(int argc, char** argv);
static void burn_rtl8720_command(int argc, char** argv);
static void reset_factory_settings_command(int argc, char** argv);
static void display_settings_command(int argc, char** argv);
static void wifissid_command(int argc, char** argv);
static void wifipwd_command(int argc, char** argv);
static void az_idscope_command(int argc, char** argv);
static void az_regid_command(int argc, char** argv);
static void az_symkey_command(int argc, char** argv);
static void az_iotc_command(int argc, char** argv);
static void ei_hmackey_command(int argc, char** argv); // New command function declaration
static const struct console_command cmds[] =
{
{"help" , "Help document" , help_command },
{"burn_rtl8720" , "Enter the Burn RTL8720 Firmware mode" , burn_rtl8720_command },
{"reset_factory_settings", "Reset factory settings" , reset_factory_settings_command },
{"show_settings" , "Display settings" , display_settings_command },
{"set_wifissid" , "Set Wi-Fi SSID" , wifissid_command },
{"set_wifipwd" , "Set Wi-Fi password" , wifipwd_command },
{"set_az_idscope" , "Set id scope of Azure IoT DPS" , az_idscope_command },
{"set_az_regid" , "Set registration id of Azure IoT DPS" , az_regid_command },
{"set_az_symkey" , "Set symmetric key of Azure IoT DPS" , az_symkey_command },
{"set_az_iotc" , "Set connection information of Azure IoT Central", az_iotc_command },
{"set_ei_hmackey" , "Set Edge Impulse HMAC Key" , ei_hmackey_command } // New command entry
};
static const int cmd_count = sizeof(cmds) / sizeof(cmds[0]);
static void EnterBurnRTL8720Mode()
{
// Switch mode of RTL8720
pinMode(PIN_SERIAL2_RX, OUTPUT);
pinMode(RTL8720D_CHIP_PU, OUTPUT);
digitalWrite(PIN_SERIAL2_RX, LOW);
digitalWrite(RTL8720D_CHIP_PU, LOW);
delay(500);
pinMode(RTL8720D_CHIP_PU, INPUT);
delay(500);
pinMode(PIN_SERIAL2_RX, INPUT);
// Initialize UART
Serial.beginWithoutDTR(115200);
auto oldBaud = Serial.baud();
RTL8720D.begin(oldBaud);
delay(500);
while (true)
{
// Change baud
const auto baud = Serial.baud();
if (baud != oldBaud)
{
RTL8720D.begin(baud);
oldBaud = baud;
}
// USB -> RTL
while (Serial.available()) RTL8720D.write(Serial.read());
// RTL -> USB
while (RTL8720D.available()) Serial.write(RTL8720D.read());
}
}
static void print_help()
{
Serial.print("Configuration console:" DLM);
for (int i = 0; i < cmd_count; i++)
{
Serial.print(String::format(" - %s: %s." DLM, cmds[i].name, cmds[i].help));
}
}
static void help_command(int argc, char** argv)
{
print_help();
}
static void burn_rtl8720_command(int argc, char** argv)
{
Serial.print("Enter the Burn RTL8720 Firmware mode." DLM);
Serial.print("[Windows]" DLM);
Serial.print(" ambd_flash_tool.exe erase" DLM);
Serial.print(" ambd_flash_tool.exe flash -d [RTL8720-firmware-path]" DLM);
Serial.print("[macOS/Linux]" DLM);
Serial.print(" python3 ambd_flash_tool.py erase" DLM);
Serial.print(" python3 ambd_flash_tool.py flash -d [RTL8720-firmware-path]" DLM);
delay(1000);
EnterBurnRTL8720Mode();
}
static void reset_factory_settings_command(int argc, char** argv)
{
Storage_->Erase();
Storage_->Load();
Serial.print("Reset factory settings successfully." DLM);
}
static void display_settings_command(int argc, char** argv)
{
Serial.print(String::format("Wi-Fi SSID = %s" DLM, Storage_->WiFiSSID.c_str()));
Serial.print(String::format("Wi-Fi password = %s" DLM, Storage_->WiFiPassword.c_str()));
Serial.print(String::format("Id scope of Azure IoT DPS = %s" DLM, Storage_->IdScope.c_str()));
Serial.print(String::format("Registration id of Azure IoT DPS = %s" DLM, Storage_->RegistrationId.c_str()));
Serial.print(String::format("Symmetric key of Azure IoT DPS = %s" DLM, Storage_->SymmetricKey.c_str()));
Serial.print(String::format("Edge Impulse HMAC Key = %s" DLM, Storage_->EdgeImpulseHmacKey.c_str())); // Display HMAC key
}
static void wifissid_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.print(String::format("ERROR: Usage: %s <SSID>. Please provide the SSID of the Wi-Fi." DLM, argv[0]));
return;
}
Storage_->WiFiSSID = argv[1];
Storage_->Save();
Serial.print("Set Wi-Fi SSID successfully." DLM);
}
static void wifipwd_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.print(String::format("ERROR: Usage: %s <Password>. Please provide the password of the Wi-Fi." DLM, argv[0]));
return;
}
Storage_->WiFiPassword = argv[1];
Storage_->Save();
Serial.print("Set Wi-Fi password successfully." DLM);
}
static void az_idscope_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.print(String::format("ERROR: Usage: %s <Id scope>. Please provide the id scope of the Azure IoT DPS." DLM, argv[0]));
return;
}
Storage_->IdScope = argv[1];
Storage_->Save();
Serial.print("Set id scope successfully." DLM);
}
static void az_regid_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.print(String::format("ERROR: Usage: %s <Registration id>. Please provide the registraion id of the Azure IoT DPS." DLM, argv[0]));
return;
}
Storage_->RegistrationId = argv[1];
Storage_->Save();
Serial.print("Set registration id successfully." DLM);
}
static void az_symkey_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.print(String::format("ERROR: Usage: %s <Symmetric key>. Please provide the symmetric key of the Azure IoT DPS." DLM, argv[0]));
return;
}
Storage_->SymmetricKey = argv[1];
Storage_->Save();
Serial.print("Set symmetric key successfully." DLM);
}
static void az_iotc_command(int argc, char** argv)
{
if (argc != 4)
{
Serial.print(String::format("ERROR: Usage: %s <Id scope> <SAS key> <Device id>." DLM, argv[0]));
return;
}
Storage_->IdScope = argv[1];
Storage_->RegistrationId = argv[3];
Storage_->SymmetricKey = ComputeDerivedSymmetricKey(argv[2], argv[3]);
Storage_->Save();
Serial.print("Set connection information of Azure IoT Central successfully." DLM);
}
// New command function implementation
static void ei_hmackey_command(int argc, char** argv)
{
if (argc != 2)
{
Serial.print(String::format("ERROR: Usage: %s <HMAC Key>. Please provide the Edge Impulse HMAC Key." DLM, argv[0]));
return;
}
Storage_->EdgeImpulseHmacKey = argv[1];
Storage_->Save();
Serial.print("Set Edge Impulse HMAC Key successfully." DLM);
}
static bool CliGetInput(char* inbuf, int* bp)
{
if (inbuf == nullptr)
{
return false;
}
while (Serial.available() >= 1)
{
inbuf[*bp] = (char)Serial.read();
if (inbuf[*bp] == END_CHAR)
{
/* end of input line */
inbuf[*bp] = '\0';
*bp = 0;
return true;
}
else if (inbuf[*bp] == TAB_CHAR)
{
inbuf[*bp] = SPACE_CHAR;
}
else if (inbuf[*bp] == BACKSPACE_CHAR || inbuf[*bp] == DEL_CHAR)
{
// Delete
if (*bp > 0)
{
(*bp)--;
Serial.write(BACKSPACE_CHAR);
Serial.write(SPACE_CHAR);
Serial.write(BACKSPACE_CHAR);
}
continue;
}
else if (inbuf[*bp] < SPACE_CHAR)
{
continue;
}
// Echo
Serial.write(inbuf[*bp]);
(*bp)++;
if (*bp >= INBUF_SIZE)
{
Serial.print(DLM "ERROR: Input buffer overflow." DLM);
Serial.print(PROMPT);
*bp = 0;
continue;
}
}
return false;
}
static bool CliHandleInput(char* inbuf)
{
struct
{
unsigned inArg:1;
unsigned inQuote:1;
unsigned done:1;
} stat;
char* argv[MAX_CMD_ARG];
int argc = 0;
int i = 0;
memset((void*)&argv, 0, sizeof(argv));
memset(&stat, 0, sizeof(stat));
do
{
switch (inbuf[i])
{
case '\0':
if (stat.inQuote)
{
return false;
}
stat.done = 1;
break;
case '"':
if (i > 0 && inbuf[i - 1] == '\\' && stat.inArg)
{
memcpy(&inbuf[i - 1], &inbuf[i], strlen(&inbuf[i]) + 1);
--i;
break;
}
if (!stat.inQuote && stat.inArg) break;
if (stat.inQuote && !stat.inArg) return false;
if (!stat.inQuote && !stat.inArg)
{
stat.inArg = 1;
stat.inQuote = 1;
argc++;
argv[argc - 1] = &inbuf[i + 1];
}
else if (stat.inQuote && stat.inArg)
{
stat.inArg = 0;
stat.inQuote = 0;
inbuf[i] = '\0';
}
break;
case ' ':
if (i > 0 && inbuf[i - 1] == '\\' && stat.inArg)
{
memcpy(&inbuf[i - 1], &inbuf[i], strlen(&inbuf[i]) + 1);
--i;
break;
}
if (!stat.inQuote && stat.inArg)
{
stat.inArg = 0;
inbuf[i] = '\0';
}
break;
default:
if (!stat.inArg)
{
stat.inArg = 1;
argc++;
argv[argc - 1] = &inbuf[i];
}
break;
}
}
while (!stat.done && ++i < INBUF_SIZE && argc <= MAX_CMD_ARG);
if (stat.inQuote) return false;
if (argc < 1) return true;
Serial.print(DLM);
for(int i = 0; i < cmd_count; i++)
{
if(strcmp(cmds[i].name, argv[0]) == 0)
{
cmds[i].function(argc, argv);
return true;
}
}
Serial.print(String::format("ERROR: Invalid command: %s" DLM, argv[0]));
return true;
}
[[noreturn]] void ConfigurationMode(Storage& storage)
{
Storage_ = &storage;
print_help();
Serial.print(PROMPT);
char inbuf[INBUF_SIZE];
int bp = 0;
while (true)
{
if (!CliGetInput(inbuf, &bp)) continue;
if (!CliHandleInput(inbuf))
{
Serial.print("ERROR: Syntax error." DLM);
}
Serial.print(PROMPT);
}
}