Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve incomplete config saved to FS after upload from flash utility #383

Merged
merged 12 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 49 additions & 26 deletions ESPixelStick/ESPixelStick.ino
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ config_t config; // Current configuration
bool reboot = false; // Reboot flag
uint32_t lastUpdate; // Update timeout tracker
bool ResetWiFi = false;
bool InitializeConfig = false; // Configuration initialization flag
bool IsBooting = true; // Configuration initialization flag
bool ConfigLoadNeeded = false;
bool ConfigSaveNeeded = false;

/////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -167,7 +168,8 @@ void setup()
// DEBUG_END;

// Done with initialization
InitializeConfig = false;
IsBooting = false;

} // setup

/////////////////////////////////////////////////////////
Expand All @@ -190,12 +192,6 @@ bool validateConfig()
String chipId = int64String (ESP.getEfuseMac (), HEX);
#endif

// Initialization - Force save
if (InitializeConfig) {
logcon (CN_stars + String (F (" Configuration Initialization ")) + CN_stars);
configValid = false;
}

// Device defaults
if (!config.id.length ())
{
Expand Down Expand Up @@ -231,7 +227,6 @@ bool dsDevice(JsonObject & json)
JsonObject JsonDeviceConfig = json[CN_device];

//TODO: Add configuration upgrade handling - cfgver moved to root level

ConfigChanged |= setFromJSON (config.id, JsonDeviceConfig, CN_id);
ConfigChanged |= setFromJSON (config.BlankDelay, JsonDeviceConfig, CN_blanktime);
}
Expand Down Expand Up @@ -293,15 +288,13 @@ bool dsNetwork(JsonObject & json)
} // dsNetwork

// Save the config and schedule a load operation
void SetConfig (JsonObject & json, const char * DataString)
void SetConfig (const char * DataString)
{
// DEBUG_START;

//TODO: This is being called from c_WebMgr::processCmdSet() with no validation
// of the data. Chance for 3rd party software to muck up the configuraton
// if they send bad json data.
// Set config version
json[CN_cfgver] = CurrentConfigVersion;

FileMgr.SaveConfigFile (ConfigFileName, DataString);
ConfigLoadNeeded = true;
Expand All @@ -321,20 +314,33 @@ bool deserializeCore (JsonObject & json)

do // once
{
uint8_t TempVersion = 0;
setFromJSON (TempVersion, json, CN_cfgver);
if (TempVersion != CurrentConfigVersion)
if (json.containsKey(CN_cfgver))
{
//TODO: Add configuration update handler
logcon (String (F ("Incorrect Config Version ID")));
uint8_t TempVersion = uint8_t(-1);
setFromJSON (TempVersion, json, CN_cfgver);
if (TempVersion != CurrentConfigVersion)
{
//TODO: Add configuration update handler
logcon (String (F ("Incorrect Config Version ID")));
}
}
else
{
logcon (String (F ("Missing Config Version ID")));
}

setFromJSON (InitializeConfig, json, CN_init);
// is this an initial config from the flash tool?
if (json.containsKey (CN_init))
{
// trigger a save operation
ConfigSaveNeeded = true;
}

dsDevice (json);
FileMgr.SetConfig (json);
ResetWiFi = dsNetwork (json);
DataHasBeenAccepted = true;

} while (false);

// DEBUG_END;
Expand All @@ -357,6 +363,8 @@ void SaveConfig()
{
// DEBUG_START;

ConfigSaveNeeded = false;

// Save Config
String DataToSave = serializeCore (false);
// DEBUG_V ("ConfigFileName: " + ConfigFileName);
Expand All @@ -379,10 +387,8 @@ void loadConfig()
String temp;
// DEBUG_V ("");
FileMgr.LoadConfigFile (ConfigFileName, &deserializeCoreHandler);
if (!validateConfig())
{
SaveConfig();
}

ConfigSaveNeeded |= !validateConfig ();

// DEBUG_START;
} // loadConfig
Expand All @@ -404,9 +410,9 @@ void GetConfig (JsonObject & json)
json[CN_cfgver] = CurrentConfigVersion;

// Device
JsonObject device = json.createNestedObject(CN_device);
device[CN_id] = config.id;
device[CN_blanktime] = config.BlankDelay;
JsonObject device = json.createNestedObject(CN_device);
device[CN_id] = config.id;
device[CN_blanktime] = config.BlankDelay;

FileMgr.GetConfig (device);

Expand Down Expand Up @@ -460,6 +466,8 @@ String serializeCore(bool pretty)
serializeJson (JsonConfig, jsonConfigString);
}

// DEBUG_V (String ("jsonConfigString: ") + jsonConfigString);

// DEBUG_END;

return jsonConfigString;
Expand Down Expand Up @@ -518,6 +526,11 @@ void loop()
loadConfig ();
}

if (ConfigSaveNeeded)
{
SaveConfig ();
}

if (true == ResetWiFi)
{
ResetWiFi = false;
Expand All @@ -528,6 +541,16 @@ void loop()

void _logcon (String & DriverName, String Message)
{
LOG_PORT.println ("[" + DriverName + "] " + Message);
char Spaces[] = { " " };
if (DriverName.length() < (sizeof(Spaces)-1))
{
Spaces[(sizeof (Spaces) - 1) - DriverName.length ()] = '\0';
}
else
{
Spaces[0] = '\0';
}

LOG_PORT.println ("[" + String (Spaces) + DriverName + "] " + Message);
LOG_PORT.flush ();
} // logcon
1 change: 1 addition & 0 deletions ESPixelStick/src/ConstNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const char CN_mode_name [] = "mode_name";
const char CN_mosi_pin [] = "mosi_pin";
const char CN_multicast [] = "multicast";
const char CN_name [] = "name";
const char CN_NeedAutoConfig [] = "NeedAutoConfig";
const char CN_netmask [] = "netmask";
const char CN_network [] = "network";
const char CN_num_chan [] = "num_chan";
Expand Down
1 change: 1 addition & 0 deletions ESPixelStick/src/ConstNames.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ extern const char CN_mode_name [];
extern const char CN_mosi_pin[];
extern const char CN_multicast [];
extern const char CN_name [];
extern const char CN_NeedAutoConfig [];
extern const char CN_netmask [];
extern const char CN_network [];
extern const char CN_num_chan[];
Expand Down
2 changes: 1 addition & 1 deletion ESPixelStick/src/ESPixelStick.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool deserializeCore (JsonObject & json);
bool dsDevice (JsonObject & json);
bool dsNetwork (JsonObject & json);
extern bool reboot;
extern bool InitializeConfig;
extern bool IsBooting;
extern bool ResetWiFi;
static const String ConfigFileName = "/config.json";

Expand Down
2 changes: 1 addition & 1 deletion ESPixelStick/src/FileMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ bool c_FileMgr::LoadConfigFile (const String& FileName, DeserializationHandler H
fs::File file = LittleFS.open (FileName.c_str (), "r");
if (!file)
{
if (!InitializeConfig) {
if (!IsBooting) {
logcon (String (CN_stars) + CfgFileMessagePrefix + String (F (" Could not open file for reading ")) + CN_stars);
}
break;
Expand Down
15 changes: 7 additions & 8 deletions ESPixelStick/src/WebMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ void c_WebMgr::ProcessGseriesRequests (AsyncWebSocketClient* client)
void c_WebMgr::ProcessReceivedJsonMessage (DynamicJsonDocument & webJsonDoc, AsyncWebSocketClient * client)
{
// DEBUG_START;
//LOG_PORT.printf_P( PSTR("ProcessReceivedJsonMessage heap / stack Stats: %u:%u:%u:%u\n"), ESP.getFreeHeap(), ESP.getHeapFragmentation(), ESP.getMaxFreeBlockSize(), ESP.getFreeContStack());
// LOG_PORT.printf_P( PSTR("ProcessReceivedJsonMessage heap / stack Stats: %u:%u:%u:%u\n"), ESP.getFreeHeap(), ESP.getHeapFragmentation(), ESP.getMaxFreeBlockSize(), ESP.getFreeContStack());

do // once
{
Expand Down Expand Up @@ -871,10 +871,9 @@ void c_WebMgr::processCmdGet (JsonObject & jsonCmd)
size_t bufferoffset = strlen(WebSocketFrameCollectionBuffer);
size_t BufferFreeSize = sizeof (WebSocketFrameCollectionBuffer) - bufferoffset;

if ((jsonCmd[CN_get] == CN_device) ||
(jsonCmd[CN_get] == CN_network) )
if (jsonCmd[CN_get] == CN_system)
{
// DEBUG_V ("device/network");
// DEBUG_V ("system");
FileMgr.ReadConfigFile (ConfigFileName,
(byte*)&WebSocketFrameCollectionBuffer[bufferoffset],
BufferFreeSize);
Expand Down Expand Up @@ -942,12 +941,12 @@ bool c_WebMgr::processCmdSet (JsonObject & jsonCmd)

do // once
{
if ((jsonCmd.containsKey (CN_device)) || (jsonCmd.containsKey (CN_network)))
if (jsonCmd.containsKey (CN_device))
{
// DEBUG_V ("device/network");
extern void SetConfig (JsonObject &, const char* DataString);
extern void SetConfig (const char* DataString);
serializeJson (jsonCmd, WebSocketFrameCollectionBuffer, sizeof (WebSocketFrameCollectionBuffer) - 1);
SetConfig (jsonCmd, WebSocketFrameCollectionBuffer);
SetConfig (WebSocketFrameCollectionBuffer);
pAlexaDevice->setName (config.id);

// DEBUG_V ("device/network: Done");
Expand Down Expand Up @@ -987,7 +986,7 @@ bool c_WebMgr::processCmdSet (JsonObject & jsonCmd)
break;
}

logcon (" ");
// logcon (" ");
PrettyPrint (jsonCmd, String(CN_stars) + F (" ERROR: Undhandled Set request type. ") + CN_stars );
strcat (WebSocketFrameCollectionBuffer, "ERROR");

Expand Down
8 changes: 0 additions & 8 deletions ESPixelStick/src/WiFiMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ void c_WiFiMgr::connectWifi (const String & ssid, const String & passphrase)
if (ResetWiFi)
return;

LOG_PORT.println();
SetUpIp ();

// Hostname must be set after the mode on ESP8266 and before on ESP32
Expand Down Expand Up @@ -362,7 +361,6 @@ void c_WiFiMgr::AnnounceState ()

String StateName;
pCurrentFsmState->GetStateName (StateName);
LOG_PORT.println ("");
logcon (String (F ("WiFi Entering State: ")) + StateName);

// DEBUG_END;
Expand Down Expand Up @@ -429,7 +427,6 @@ void fsm_WiFi_state_ConnectingUsingConfig::Poll ()
{
if (CurrentTimeMS - WiFiMgr.GetFsmStartTime() > (1000 * WiFiMgr.GetConfigPtr()->sta_timeout))
{
LOG_PORT.println ("");
logcon (F ("WiFi Failed to connect using Configured Credentials"));
fsm_WiFi_state_ConnectingDefault_imp.Init ();
}
Expand Down Expand Up @@ -487,7 +484,6 @@ void fsm_WiFi_state_ConnectingUsingDefaults::Poll ()
{
if (CurrentTimeMS - WiFiMgr.GetFsmStartTime () > (1000 * WiFiMgr.GetConfigPtr ()->sta_timeout))
{
LOG_PORT.println ("");
logcon (F ("WiFi Failed to connect using default Credentials"));
fsm_WiFi_state_ConnectingAsAP_imp.Init ();
}
Expand Down Expand Up @@ -539,11 +535,8 @@ void fsm_WiFi_state_ConnectingAsAP::Poll ()
}
else
{
LOG_PORT.print (".");

if (millis () - WiFiMgr.GetFsmStartTime () > (1000 * WiFiMgr.GetConfigPtr ()->ap_timeout))
{
LOG_PORT.println ("");
logcon (F ("WiFi STA Failed to connect"));
fsm_WiFi_state_ConnectionFailed_imp.Init ();
}
Expand Down Expand Up @@ -680,7 +673,6 @@ void fsm_WiFi_state_ConnectedToSta::Init ()
WiFiMgr.setIpAddress (WiFi.softAPIP ());
WiFiMgr.setIpSubNetMask (IPAddress (255, 255, 255, 0));

LOG_PORT.println ("");
logcon (String (F ("Connected to STA with IP: ")) + WiFiMgr.getIpAddress ().toString ());

WiFiMgr.SetIsWiFiConnected (true);
Expand Down
6 changes: 3 additions & 3 deletions ESPixelStick/src/WiFiMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class fsm_WiFi_state_ConnectingUsingConfig : public fsm_WiFi_state
virtual void Init (void);
virtual void GetStateName (String& sName) { sName = F ("Connecting Using Config Credentials"); }
virtual void OnConnect (void);
virtual void OnDisconnect (void) { LOG_PORT.print ("."); }
virtual void OnDisconnect (void) {}

}; // fsm_WiFi_state_ConnectingUsingConfig

Expand All @@ -145,7 +145,7 @@ class fsm_WiFi_state_ConnectingUsingDefaults : public fsm_WiFi_state
virtual void Init (void);
virtual void GetStateName (String& sName) { sName = F ("Connecting Using Default Credentials"); }
virtual void OnConnect (void);
virtual void OnDisconnect (void) { LOG_PORT.print ("."); }
virtual void OnDisconnect (void) {}

}; // fsm_WiFi_state_ConnectingUsingConfig

Expand All @@ -169,7 +169,7 @@ class fsm_WiFi_state_ConnectingAsAP : public fsm_WiFi_state
virtual void Init (void);
virtual void GetStateName (String& sName) { sName = F ("Connecting As AP"); }
virtual void OnConnect (void);
virtual void OnDisconnect (void) { LOG_PORT.print ("."); }
virtual void OnDisconnect (void) {}

}; // fsm_WiFi_state_ConnectingAsAP

Expand Down
2 changes: 0 additions & 2 deletions ESPixelStick/src/input/InputDDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,8 @@ void c_InputDDP::ProcessReceivedData (DDP_packet_t & Packet)

uint32_t InputBufferOffset = ntohl (header.channelOffset);
uint32_t packetDataLength = ntohs (header.dataLen);
uint32_t LastCharOffset = packetDataLength + InputBufferOffset;

// DEBUG_V (String (" packetDataLength: ") + String (packetDataLength));
// DEBUG_V (String (" LastCharOffset: ") + String (LastCharOffset));
// DEBUG_V (String (" InputDataBufferSize: ") + String (InputDataBufferSize));

if (InputBufferOffset >= InputDataBufferSize)
Expand Down
Loading