-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
156 lines (138 loc) · 6.66 KB
/
config.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
//config.cpp
#include "config.h"
Config::Config(){
}
void Config::init() {
safe_strncpy (app_id, APP_ID, MAX_TINY_STRING_LENGTH);
safe_strncpy (firmware_primary_prefix, FW_PRIMARY_PREFIX, MAX_LONG_STRING_LENGTH);
safe_strncpy (firmware_secondary_prefix, FW_BACKUP_PREFIX, MAX_LONG_STRING_LENGTH);
safe_strncpy (certificate_primary_prefix, CERTIFICATE_PRIMARY_PREFIX, MAX_LONG_STRING_LENGTH);
safe_strncpy (certificate_secondary_prefix, CERTIFICATE_BACKUP_PREFIX, MAX_LONG_STRING_LENGTH);
dump();
load_config(); // read overriding params from Flash file
}
bool Config::load_config() {
SERIAL_PRINTLN("Loading config from Flash...");
if (!SPIFFS.begin()) {
Serial.println("--- Failed to mount file system. ---");
return false;
}
File configFile = SPIFFS.open(CONFIG_FILE_NAME, "r");
if (!configFile) {
Serial.println("--- Failed to open config file. ---");
SPIFFS.end();
return false;
}
Serial.println ("Config file opened.");
size_t size = configFile.size();
if (size > CONFIG_FILE_SIZE) {
Serial.println("--- Config file size is too large. ---");
SPIFFS.end();
return false;
}
// Allocate a buffer to store file contents ***
// ArduinoJson library requires the input buffer to be mutable
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
// Consider this number carefully; json needs extra space for metadata:
StaticJsonDocument<JSON_CONFIG_FILE_SIZE> doc; // including extra space for Json
auto error = deserializeJson(doc, buf.get());
Serial.print ("Deserialization result: ");
Serial.println (error.c_str());
if (error) {
Serial.println("--- Failed to parse config file. ---");
return false;
SPIFFS.end();
}
char *tmp;
tmp = (char *)(doc["APP"] | APP_ID);
safe_strncpy (app_id, tmp, MAX_TINY_STRING_LENGTH);
tmp = (char *)(doc["OTA1"] | FW_PRIMARY_PREFIX);
safe_strncpy (firmware_primary_prefix, tmp, MAX_LONG_STRING_LENGTH);
tmp = (char *)(doc["OTA2"] | FW_BACKUP_PREFIX);
safe_strncpy (firmware_secondary_prefix, tmp, MAX_LONG_STRING_LENGTH);
tmp = (char *)(doc["CERT1"] | CERTIFICATE_PRIMARY_PREFIX);
safe_strncpy (certificate_primary_prefix, tmp, MAX_LONG_STRING_LENGTH);
tmp = (char *)(doc["CERT2"] | CERTIFICATE_BACKUP_PREFIX);
safe_strncpy (certificate_secondary_prefix, tmp, MAX_LONG_STRING_LENGTH);
SPIFFS.end(); // unmount file system
return true;
}
void Config::dump() {
SERIAL_PRINTLN("\n-----------------------------------------");
SERIAL_PRINTLN(" configuration ");
SERIAL_PRINTLN("-----------------------------------------");
print_heap();
SERIAL_PRINT ("Firmware version: ");
SERIAL_PRINTLN (current_firmware_version);
SERIAL_PRINT ("TLS certificate version: ");
SERIAL_PRINTLN (current_certificate_version);
SERIAL_PRINT ("app id: ");
SERIAL_PRINTLN (app_id);
SERIAL_PRINTLN();
SERIAL_PRINTLN ("primary OTA server: ");
SERIAL_PRINTLN (get_primary_OTA_url());
SERIAL_PRINTLN ("primary OTA version check file: ");
SERIAL_PRINTLN (get_primary_version_url());
SERIAL_PRINTLN ("secondary OTA server: ");
SERIAL_PRINTLN (get_secondary_OTA_url());
SERIAL_PRINTLN ("secondary OTA version check file: ");
SERIAL_PRINTLN (get_secondary_version_url());
SERIAL_PRINTLN();
SERIAL_PRINTLN ("primary certificate version check file: ");
SERIAL_PRINTLN (get_primary_certificate_version_url());
SERIAL_PRINTLN ("secondary certificate version check file: ");
SERIAL_PRINTLN (get_secondary_certificate_version_url());
SERIAL_PRINTLN ("primary certificate files: ");
for (int i=0; i<NUM_CERTIFICATE_FILES; i++)
SERIAL_PRINTLN (get_primary_certificate_url(i));
SERIAL_PRINTLN ("secondary certificate files: ");
for (int i=0; i<NUM_CERTIFICATE_FILES; i++)
SERIAL_PRINTLN (get_secondary_certificate_url(i));
yield();
delay(1000); // stabilize heap ?
print_heap();
SERIAL_PRINTLN("-----------------------------------------\n");
}
short Config::get_num_files() {
return NUM_CERTIFICATE_FILES;
}
// NOTE: reusable_string is a scratch pad string; it holds one URL at a time, to save memory!
// So don't call any of these functions while the previous string is still in use
// NOTE: the following two functions introduce a slash between the prefix and the file name; so this is not
// to be added in the prefix string
const char* Config::get_primary_certificate_version_url() {
snprintf (reusable_string, MAX_LONG_STRING_LENGTH-1, "%s/%s", certificate_primary_prefix, CERT_VERSION_FILE);
return ((const char*) reusable_string);
}
const char* Config::get_secondary_certificate_version_url(){
snprintf (reusable_string, MAX_LONG_STRING_LENGTH-1, "%s/%s", certificate_secondary_prefix, CERT_VERSION_FILE);
return ((const char*) reusable_string);
}
// NOTE: the following two functions do not add a slash between the prefix and the file name, since the SPIFF file name
// should mandatorily have a leading slash.
const char* Config::get_primary_certificate_url (short file_number){
snprintf (reusable_string, MAX_LONG_STRING_LENGTH-1, "%s%s", certificate_primary_prefix, file_names[file_number]);
return ((const char*) reusable_string);
}
const char* Config::get_secondary_certificate_url (short file_number){
snprintf (reusable_string, MAX_LONG_STRING_LENGTH-1, "%s%s", certificate_secondary_prefix, file_names[file_number]);
return ((const char*) reusable_string);
}
// NOTE: these functions introduce the slash again
const char* Config::get_primary_OTA_url() {
snprintf (reusable_string, MAX_LONG_STRING_LENGTH-1, "%s/%s.bin", firmware_primary_prefix, app_id);
return ((const char*) reusable_string);
}
const char* Config::get_primary_version_url() {
snprintf (reusable_string, MAX_LONG_STRING_LENGTH-1, "%s/%s.txt", firmware_primary_prefix, app_id);
return ((const char*) reusable_string);
}
const char* Config::get_secondary_OTA_url() {
snprintf (reusable_string, MAX_LONG_STRING_LENGTH-1, "%s/%s.bin", firmware_secondary_prefix, app_id);
return ((const char*) reusable_string);
}
const char* Config::get_secondary_version_url() {
snprintf (reusable_string, MAX_LONG_STRING_LENGTH-1, "%s/%s.txt", firmware_secondary_prefix, app_id);
return ((const char*) reusable_string);
}