-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory_setup.cpp
63 lines (59 loc) · 2.25 KB
/
factory_setup.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
#include "factory_setup.h"
bool isFactorySetupDone() {
return setupComplete == SETUP_COMPLETE_MAGIC_NUMBER;
}
void handleReceivedSetupData() {
if (Serial.read() == '#') {
// Read the serial number until ';' is encountered and convert to int
String input = Serial.readStringUntil(';');
if (input == "SETUP") {
input = Serial.readStringUntil(';');
sscanf(input.c_str(), "%d", &serialNumber);
// Read the starting code until ';' is encountered and convert to
// int
input = Serial.readStringUntil(';');
sscanf(input.c_str(), "%d", &startingCode);
// Read the next part (hexadecimal string)
char hexString[33];
input = Serial.readStringUntil('\n');
input.toCharArray(hexString, sizeof(hexString));
// Convert hexadecimal string to byte array
for (int i = 0; i < 32 / 2; i++) {
char hexByte[3];
hexByte[0] = hexString[i * 2];
hexByte[1] = hexString[i * 2 + 1];
hexByte[2] = '\0';
secretKey[i] = strtol(hexByte, NULL, 16);
}
// We mark the setup as complete
setupComplete = SETUP_COMPLETE_MAGIC_NUMBER;
} else {
Serial.println("#INVALID");
}
}
}
void factorySetupLoop() {
if (Serial.available() > 0) { // send data only when you receive data; condition in
// setupComplete in case the eeprom was badly initialized
handleReceivedSetupData();
}
if (setupComplete == SETUP_COMPLETE_MAGIC_NUMBER) {
// Print the received data
debugPrintln("INFO: Setup Data Received");
debugPrint("INFO: SN: ");
debugPrint(serialNumber);
debugPrint(", SC: ");
debugPrint(startingCode);
debugPrint(", KEY: ");
for (int i = 0; i < 32 / 2; i++) {
debugPrint(secretKey[i], HEX);
}
debugPrintln();
// We save the data
saveSetupData();
debugPrintln("INFO: Setup data saved");
// We load it right away to ensure all variables are set
loadAllData();
debugPrintln("INFO: Setup complete. RESTART DEVICE.");
}
}