Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into wlan-op-modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Slider0007 committed Nov 3, 2024
2 parents 92fa46a + dbbaae8 commit dd0e029
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
20 changes: 16 additions & 4 deletions code/components/config_handling/configClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void ConfigClass::readConfigFile(bool unityTest, std::string unityTestData)
streamBuffer.str("{}"); // Ensure any content
}

portENTER_CRITICAL(&mutex);
// Modify hook to use SPIRAM for cJSON object
cJSON_Hooks hooks;
hooks.malloc_fn = malloc_psram_heap_cjson;
Expand All @@ -153,6 +154,11 @@ void ConfigClass::readConfigFile(bool unityTest, std::string unityTestData)

// Parse content to cJSON object structure
cJsonObject = cJSON_Parse(streamBuffer.str().c_str());

// Reset cJSON hooks to default
cJSON_InitHooks(NULL);
portEXIT_CRITICAL(&mutex);

if (cJsonObject == NULL) {
LogFile.writeToFile(ESP_LOG_ERROR, TAG, "parseConfig: Failed to parse JSON data | Fallback: Use default config");
// Continue to try to restore WLAN config from NVS, otherwise Access Point is getting started to reconfigure.
Expand Down Expand Up @@ -192,6 +198,7 @@ esp_err_t ConfigClass::setConfigRequest(httpd_req_t *req)
remaining -= received;
}

portENTER_CRITICAL(&mutex);
// Modify hook to use SPIRAM for cJSON object
cJSON_Hooks hooks;
hooks.malloc_fn = malloc_psram_heap_cjson;
Expand All @@ -203,6 +210,11 @@ esp_err_t ConfigClass::setConfigRequest(httpd_req_t *req)

// Parse content to cJSON object structure
cJsonObject = cJSON_Parse(jsonBuffer);

// Reset cJSON hooks to default
cJSON_InitHooks(NULL);
portEXIT_CRITICAL(&mutex);

if (cJsonObject == NULL) {
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "E91: Failed to parse JSON data, e.g. malformed notation");
return ESP_FAIL;
Expand Down Expand Up @@ -1409,8 +1421,6 @@ esp_err_t ConfigClass::parseConfig(httpd_req_t *req, bool init, bool unityTest)
if (cJSON_IsNumber(objEl))
cfgDataTemp.sectionWebUi.AutoRefresh.dataGraphPage.refreshTime = std::max(objEl->valueint, 1);

cJSON_InitHooks(NULL); // Reset cJSON hooks to default (cJSON_Delete -> not needed)

// Init active config struct with latest configuration data
if (init) {
cfgData = cfgDataTemp;
Expand Down Expand Up @@ -1438,6 +1448,7 @@ esp_err_t ConfigClass::parseConfig(httpd_req_t *req, bool init, bool unityTest)

esp_err_t ConfigClass::serializeConfig(bool unityTest)
{
portENTER_CRITICAL(&mutex);
// Modify hook to use SPIRAM for cJSON object
cJSON_Hooks hooks;
hooks.malloc_fn = malloc_psram_heap_cjson;
Expand Down Expand Up @@ -2045,13 +2056,14 @@ esp_err_t ConfigClass::serializeConfig(bool unityTest)
if (cJSON_AddNumberToObject(webuiAutorefreshDataGraph, "refreshtime", cfgDataTemp.sectionWebUi.AutoRefresh.dataGraphPage.refreshTime) == NULL)
retVal = ESP_FAIL;

cJSON_InitHooks(NULL); // Reset cJSON hooks to default
portEXIT_CRITICAL(&mutex);

jsonBuffer[0] = '\0'; // Reset content
// Print to preallocted buffer
if (!cJSON_PrintPreallocated(cJsonObject, jsonBuffer, CONFIG_HANDLING_PREALLOCATED_BUFFER_SIZE, unityTest ? 0 : 1))
retVal = ESP_FAIL;

cJSON_InitHooks(NULL); // Reset cJSON hooks to default (cJSON_Delete -> not needed)

return retVal;
}

Expand Down
2 changes: 2 additions & 0 deletions code/components/config_handling/configClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <vector>

#include <freertos/FreeRTOS.h>
#include <esp_log.h>
#include <esp_heap_caps.h>
#include <cJSON.h>
Expand All @@ -26,6 +27,7 @@ class ConfigClass
CfgData cfgDataTemp; // Keeps last parameter modifications, but not in use by process (gets promoted to active config by reinitConfig())
CfgData cfgData; // Keep active parameter configuration in use by process

portMUX_TYPE mutex = portMUX_INITIALIZER_UNLOCKED;
cJSON *cJsonObject = NULL;
uint8_t *cJsonObjectBuffer = NULL;
char *jsonBuffer = NULL;
Expand Down
3 changes: 2 additions & 1 deletion code/components/gpio_ctrl/gpioControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ esp_err_t GpioHandler::loadParameter()
void GpioHandler::clearData()
{
if (gpioMap != NULL) {
for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); it++) {
if (it->second->getSmartLed() != NULL) {
delete it->second->getSmartLed();
it->second->setSmartLed(NULL);
Expand Down Expand Up @@ -637,6 +637,7 @@ void GpioHandler::handleMQTTconnect()
if (gpioMap != NULL) {
for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
it->second->mqttPublishPinState();
vTaskDelay(pdMS_TO_TICKS(500));
}
}
}
Expand Down
1 change: 0 additions & 1 deletion code/components/mainprocess_ctrl/ClassFlowWebhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ bool ClassFlowWebhook::doFlow(std::string zwtime)
time_t actTime;
struct tm timeStruct;
time_t timeProcessedUtc = 0L;

cJSON *jsonArray = cJSON_CreateArray();

// Publish data
Expand Down
11 changes: 5 additions & 6 deletions code/components/misc_helper/psram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,20 @@ void *malloc_psram_heap_cjson(size_t size)
return (uint8_t *)cJSONObjectPSRAM.preallocatedMemory + cJSONObjectPSRAM.usedMemory - size;
}
else {
LogFile.writeToFile(ESP_LOG_WARN, TAG, "cJSON: Failed to allocate in preallocated memory. Use default region");
cJSONObjectPSRAM.failedAllocation = true;
LogFile.writeToFile(ESP_LOG_DEBUG, TAG, "cJSON: Use default region");
cJSONObjectPSRAM.useDefaultAllocation = true;
return heap_caps_malloc(size, MALLOC_CAP_DEFAULT);
}
}


void free_psram_heap_cjson(void *ptr)
{
if (!cJSONObjectPSRAM.failedAllocation) {
if (!cJSONObjectPSRAM.useDefaultAllocation) {
cJSONObjectPSRAM.usedMemory = 0;
}
else {
heap_caps_free(ptr);
cJSONObjectPSRAM.useDefaultAllocation = false;
heap_caps_free(ptr);
}

cJSONObjectPSRAM.failedAllocation = false;
}
2 changes: 1 addition & 1 deletion code/components/misc_helper/psram.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct strcJSON {
uint8_t* preallocatedMemory = NULL;
int preallocatedMemorySize = 0;
int usedMemory = 0;
bool failedAllocation = false;
bool useDefaultAllocation = false;
};
extern struct strcJSON cJSONObjectPSRAM;

Expand Down

0 comments on commit dd0e029

Please sign in to comment.