-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathWipperSnapper_LittleFS.cpp
172 lines (155 loc) · 6.52 KB
/
WipperSnapper_LittleFS.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
/*!
* @file Wippersnapper_LittleFS.cpp
*
* Interfaces with LittleFS filesystem for ESP32, ESP8266 platforms.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Brent Rubell 2021-2022 for Adafruit Industries.
*
* BSD license, all text here must be included in any redistribution.
*
*/
#if defined(ARDUINO_FEATHER_ESP32) || \
defined(ARDUINO_ESP8266_ADAFRUIT_HUZZAH) || \
defined(ARDUINO_ADAFRUIT_ITSYBITSY_ESP32) || \
defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) || \
defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO) || \
defined(ARDUINO_SPARKLEMOTIONMINI_ESP32) || \
defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3) || \
defined(ARDUINO_ADAFRUIT_FEATHER_ESP32C6)
#include "WipperSnapper_LittleFS.h"
/**************************************************************************/
/*!
@brief Attempts to set up and initialize a pre-existing LittleFS
filesystem.
*/
/**************************************************************************/
WipperSnapper_LittleFS::WipperSnapper_LittleFS() {
#if PRINT_DEPENDENCIES
// Print project build dependencies
WS_DEBUG_PRINTLN("Build Dependencies:");
WS_DEBUG_PRINTLN("*********************");
WS_DEBUG_PRINTLN(project_dependencies);
WS_DEBUG_PRINTLN("*********************");
#endif
// Attempt to initialize filesystem
if (!LittleFS.begin()) {
setStatusLEDColor(RED);
fsHalt("ERROR: Failure initializing LittleFS!");
}
}
/**************************************************************************/
/*!
@brief Destructor for LittleFS
*/
/**************************************************************************/
WipperSnapper_LittleFS::~WipperSnapper_LittleFS() { LittleFS.end(); }
/**************************************************************************/
/*!
@brief Locates, opens and parses the WipperSnapper secrets file
on the LittleFS filesystem.
*/
/**************************************************************************/
void WipperSnapper_LittleFS::parseSecrets() {
// Check if `secrets.json` file exists on FS
if (!LittleFS.exists("/secrets.json")) {
fsHalt("ERROR: No secrets.json found on filesystem - did you upload "
"credentials?");
}
// Attempt to open secrets.json file for reading
File secretsFile = LittleFS.open("/secrets.json", "r");
if (!secretsFile) {
fsHalt("ERROR: Could not open secrets.json file for reading!");
}
// Attempt to deserialize the file's JSON document
JsonDocument doc;
DeserializationError error = deserializeJson(doc, secretsFile);
if (error) {
fsHalt(String("ERROR: deserializeJson() failed with code ") +
error.c_str());
}
if (doc.containsKey("network_type_wifi")) {
// set default network config
convertFromJson(doc["network_type_wifi"], WS._config.network);
if (!doc["network_type_wifi"].containsKey("alternative_networks")) {
// do nothing extra, we already have the only network
WS_DEBUG_PRINTLN("Found single wifi network in secrets.json");
} else if (doc["network_type_wifi"]["alternative_networks"]
.is<JsonArray>()) {
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
// Parse network credentials from array in secrets
JsonArray altnetworks = doc["network_type_wifi"]["alternative_networks"];
int8_t altNetworkCount = (int8_t)altnetworks.size();
WS_DEBUG_PRINT("Network count: ");
WS_DEBUG_PRINTLN(altNetworkCount);
if (altNetworkCount == 0) {
fsHalt("ERROR: No alternative network entries found under "
"network_type_wifi.alternative_networks in secrets.json!");
}
// check if over 3, warn user and take first three
for (int i = 0; i < altNetworkCount; i++) {
if (i >= 3) {
WS_DEBUG_PRINT("WARNING: More than 3 networks in secrets.json, "
"only the first 3 will be used. Not using ");
WS_DEBUG_PRINTLN(altnetworks[i]["network_ssid"].as<const char *>());
break;
}
convertFromJson(altnetworks[i], WS._multiNetworks[i]);
WS_DEBUG_PRINT("Added SSID: ");
WS_DEBUG_PRINTLN(WS._multiNetworks[i].ssid);
WS_DEBUG_PRINT("PASS: ");
WS_DEBUG_PRINTLN(WS._multiNetworks[i].pass);
}
WS._isWiFiMulti = true;
} else {
fsHalt("ERROR: Unrecognised value type for "
"network_type_wifi.alternative_networks in secrets.json!");
}
} else {
fsHalt("ERROR: Could not find network_type_wifi in secrets.json!");
}
// Extract a config struct from the JSON document
WS._config = doc.as<secretsConfig>();
// Validate the config struct is not filled with default values
if (strcmp(WS._config.aio_user, "YOUR_IO_USERNAME_HERE") == 0 ||
strcmp(WS._config.aio_key, "YOUR_IO_KEY_HERE") == 0) {
fsHalt(
"ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
"io_username and io_key to match your Adafruit IO credentials!\n");
}
if (strcmp(WS._config.network.ssid, "YOUR_WIFI_SSID_HERE") == 0 ||
strcmp(WS._config.network.pass, "YOUR_WIFI_PASS_HERE") == 0) {
fsHalt("ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
"change network_ssid and network_password to match your Adafruit IO "
"credentials!\n");
}
// specify type of value for json key, by using the |operator to include
// a typed default value equivalent of with .as<float> w/ default value
// https://arduinojson.org/v7/api/jsonvariant/or/
WS._config.status_pixel_brightness =
doc["status_pixel_brightness"] | (float)STATUS_PIXEL_BRIGHTNESS_DEFAULT;
// Close the file
secretsFile.close();
// Stop LittleFS, we no longer need it
LittleFS.end();
}
/**************************************************************************/
/*!
@brief Halts execution and blinks the status LEDs yellow.
@param msg
Error message to print to serial console.
*/
/**************************************************************************/
void WipperSnapper_LittleFS::fsHalt(String msg) {
statusLEDSolid(WS_LED_STATUS_FS_WRITE);
while (1) {
WS_DEBUG_PRINTLN("Fatal Error: Halted execution!");
WS_DEBUG_PRINTLN(msg.c_str());
delay(1000);
yield();
}
}
#endif