From 6c94c6b20425ae799b7adab5d18cf34656f16a2e Mon Sep 17 00:00:00 2001 From: Magnus Persson Date: Thu, 4 Aug 2022 16:12:14 +0200 Subject: [PATCH] Precommit fixes --- src/calc.cpp | 10 +++-- src/gyro.cpp | 3 +- src/helper.cpp | 2 +- src/helper.hpp | 2 +- src/pushtarget.cpp | 9 ++-- src/templating.cpp | 23 +++++----- src/templating.hpp | 105 +++++++++++++++++++++++++++++++++------------ src/webserver.cpp | 57 +++++++++++------------- src/webserver.hpp | 1 + src/wifi.cpp | 2 +- 10 files changed, 136 insertions(+), 78 deletions(-) diff --git a/src/calc.cpp b/src/calc.cpp index 020de25..43bbb5c 100644 --- a/src/calc.cpp +++ b/src/calc.cpp @@ -97,8 +97,10 @@ int createFormula(RawFormulaData &fd, char *formulaBuffer, // If the deviation is more than 2 degress we mark it as failed. if (dev * 1000 > myAdvancedConfig.getMaxFormulaCreationDeviation()) { - writeErrorLog("CALC: Validation failed on angle %F, deviation too large %.2F SG, formula order %d", - fd.a[i], dev * 1000, order); + writeErrorLog( + "CALC: Validation failed on angle %F, deviation too large %.2F " + "SG, formula order %d", + fd.a[i], dev * 1000, order); valid = false; } } @@ -203,7 +205,9 @@ double gravityTemperatureCorrectionC(double gravitySG, double tempC, return g; } - writeErrorLog("CALC: Failed to parse expression for gravity temperature correction %d", err); + writeErrorLog( + "CALC: Failed to parse expression for gravity temperature correction %d", + err); return gravitySG; } diff --git a/src/gyro.cpp b/src/gyro.cpp index 660f1f9..2e050c1 100644 --- a/src/gyro.cpp +++ b/src/gyro.cpp @@ -293,7 +293,8 @@ void GyroSensor::applyCalibration() { if ((_calibrationOffset.ax + _calibrationOffset.ay + _calibrationOffset.az + _calibrationOffset.gx + _calibrationOffset.gy + _calibrationOffset.gz) == 0) { - writeErrorLog("GYRO: No valid calibration values, please calibrate the device."); + writeErrorLog( + "GYRO: No valid calibration values, please calibrate the device."); return; } diff --git a/src/helper.cpp b/src/helper.cpp index 4966a34..9f15da5 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -47,7 +47,7 @@ void tcp_cleanup() { while (tcp_tw_pcbs) tcp_abort(tcp_tw_pcbs); } -void writeErrorLog(const char *format, ...) { +void writeErrorLog(const char* format, ...) { File f = LittleFS.open(ERR_FILENAME, "a"); if (f && f.size() > ERR_FILEMAXSIZE) { diff --git a/src/helper.hpp b/src/helper.hpp index 0e6f995..3c31f86 100644 --- a/src/helper.hpp +++ b/src/helper.hpp @@ -37,7 +37,7 @@ SOFTWARE. void tcp_cleanup(); // Error logging -void writeErrorLog(const char *format, ...); +void writeErrorLog(const char* format, ...); // Sleep mode void deepSleep(int t); diff --git a/src/pushtarget.cpp b/src/pushtarget.cpp index 072e865..aad8a8d 100644 --- a/src/pushtarget.cpp +++ b/src/pushtarget.cpp @@ -254,7 +254,8 @@ void PushTarget::addHttpHeader(HTTPClient& http, String header) { value.c_str()); http.addHeader(name, value); } else { - writeErrorLog("PUSH: Unable to set header, invalid value %s", header.c_str()); + writeErrorLog("PUSH: Unable to set header, invalid value %s", + header.c_str()); } } @@ -330,7 +331,8 @@ void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, _lastSuccess = true; Log.notice(F("PUSH: HTTP post successful, response=%d" CR), _lastCode); } else { - writeErrorLog("PUSH: HTTP post failed response=%d http%d", _lastCode, index+1); + writeErrorLog("PUSH: HTTP post failed response=%d http%d", _lastCode, + index + 1); } if (isSecure) { @@ -485,7 +487,8 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure, Log.notice(F("PUSH: MQTT publish successful on %s" CR), topic.c_str()); _lastCode = 0; } else { - writeErrorLog("PUSH: MQTT push on %s failed error=%d", topic.c_str(), mqtt.lastError()); + writeErrorLog("PUSH: MQTT push on %s failed error=%d", topic.c_str(), + mqtt.lastError()); } } diff --git a/src/templating.cpp b/src/templating.cpp index d082aac..90f6818 100644 --- a/src/templating.cpp +++ b/src/templating.cpp @@ -141,30 +141,30 @@ void TemplatingEngine::initialize(float angle, float gravitySG, // // Create the data using defined template. // -const String& TemplatingEngine::create(TemplatingEngine::Templates idx) { +const char* TemplatingEngine::create(TemplatingEngine::Templates idx) { String fname; - baseTemplate.reserve(600); + _baseTemplate.reserve(600); // Load templates from memory switch (idx) { case TEMPLATE_HTTP1: - baseTemplate = String(iSpindleFormat); + _baseTemplate = String(iSpindleFormat); fname = TPL_FNAME_HTTP1; break; case TEMPLATE_HTTP2: - baseTemplate = String(iSpindleFormat); + _baseTemplate = String(iSpindleFormat); fname = TPL_FNAME_HTTP2; break; case TEMPLATE_HTTP3: - baseTemplate = String(iHttpGetFormat); + _baseTemplate = String(iHttpGetFormat); fname = TPL_FNAME_HTTP3; break; case TEMPLATE_INFLUX: - baseTemplate = String(influxDbFormat); + _baseTemplate = String(influxDbFormat); fname = TPL_FNAME_INFLUXDB; break; case TEMPLATE_MQTT: - baseTemplate = String(mqttFormat); + _baseTemplate = String(mqttFormat); fname = TPL_FNAME_MQTT; break; } @@ -175,7 +175,7 @@ const String& TemplatingEngine::create(TemplatingEngine::Templates idx) { char buf[file.size() + 1]; memset(&buf[0], 0, file.size() + 1); file.readBytes(&buf[0], file.size()); - baseTemplate = String(&buf[0]); + _baseTemplate = String(&buf[0]); file.close(); Log.notice(F("TPL : Template loaded from disk %s." CR), fname.c_str()); } @@ -185,13 +185,16 @@ const String& TemplatingEngine::create(TemplatingEngine::Templates idx) { #endif // Insert data into template. - transform(baseTemplate); + transform(); + _baseTemplate.clear(); #if LOG_LEVEL == 6 // Log.verbose(F("TPL : Transformed '%s'." CR), baseTemplate.c_str()); #endif - return baseTemplate; + if (_output) return _output; + + return ""; } // EOF diff --git a/src/templating.hpp b/src/templating.hpp index 81127fb..9ca83d4 100644 --- a/src/templating.hpp +++ b/src/templating.hpp @@ -75,34 +75,35 @@ class TemplatingEngine { String val; }; - KeyVal items[23] = {{TPL_MDNS, ""}, {TPL_ID, ""}, - {TPL_SLEEP_INTERVAL, ""}, {TPL_TEMP, ""}, - {TPL_TEMP_C, ""}, {TPL_TEMP_F, ""}, - {TPL_TEMP_UNITS, ""}, {TPL_BATTERY, ""}, - {TPL_RSSI, ""}, {TPL_RUN_TIME, ""}, - {TPL_ANGLE, ""}, {TPL_TILT, ""}, - {TPL_GRAVITY, ""}, {TPL_GRAVITY_G, ""}, - {TPL_GRAVITY_P, ""}, {TPL_GRAVITY_CORR, ""}, - {TPL_GRAVITY_CORR_G, ""}, {TPL_GRAVITY_CORR_P, ""}, - {TPL_GRAVITY_UNIT, ""}, {TPL_TOKEN, ""}, - {TPL_TOKEN2, ""}, {TPL_APP_VER, ""}, - {TPL_APP_BUILD, ""}}; + KeyVal _items[23] = {{TPL_MDNS, ""}, {TPL_ID, ""}, + {TPL_SLEEP_INTERVAL, ""}, {TPL_TEMP, ""}, + {TPL_TEMP_C, ""}, {TPL_TEMP_F, ""}, + {TPL_TEMP_UNITS, ""}, {TPL_BATTERY, ""}, + {TPL_RSSI, ""}, {TPL_RUN_TIME, ""}, + {TPL_ANGLE, ""}, {TPL_TILT, ""}, + {TPL_GRAVITY, ""}, {TPL_GRAVITY_G, ""}, + {TPL_GRAVITY_P, ""}, {TPL_GRAVITY_CORR, ""}, + {TPL_GRAVITY_CORR_G, ""}, {TPL_GRAVITY_CORR_P, ""}, + {TPL_GRAVITY_UNIT, ""}, {TPL_TOKEN, ""}, + {TPL_TOKEN2, ""}, {TPL_APP_VER, ""}, + {TPL_APP_BUILD, ""}}; - char buffer[20]; - String baseTemplate; + char _buffer[20]; + String _baseTemplate; + char *_output = 0; void setVal(String key, float val, int dec = 2) { - String s = convertFloatToString(val, &buffer[0], dec); + String s = convertFloatToString(val, &_buffer[0], dec); s.trim(); setVal(key, s); } void setVal(String key, int val) { setVal(key, String(val)); } void setVal(String key, char val) { setVal(key, String(val)); } void setVal(String key, String val) { - int max = sizeof(items) / sizeof(KeyVal); + int max = sizeof(_items) / sizeof(KeyVal); for (int i = 0; i < max; i++) { - if (items[i].key.equals(key)) { - items[i].val = val; + if (_items[i].key.equals(key)) { + _items[i].val = val; return; } } @@ -110,21 +111,64 @@ class TemplatingEngine { Log.warning(F("TPL : Key not found %s." CR), key.c_str()); } - void transform(String& s) { - int max = sizeof(items) / sizeof(KeyVal); - for (int i = 0; i < max; i++) { - while (s.indexOf(items[i].key) != -1) - s.replace(items[i].key, items[i].val); + void transform() { + const char *format = _baseTemplate.c_str(); + int len = _baseTemplate.length(); + int size = len; + + // Lets check how much memory will be needed to transform the template + for (int j = 0; j < len - 2; j++) { + if (*(format + j) == '$' && *(format + j + 1) == '{') { + // Start of format tag found + int max = sizeof(_items) / sizeof(KeyVal); + for (int i = 0; i < max; i++) { + if (strncmp(format + j, _items[i].key.c_str(), + _items[i].key.length()) == 0) { + // Found key + size = size - _items[i].key.length() + _items[i].val.length(); + } + } + } } + + _output = static_cast(malloc(size + 20)); + + if (!_output) { + Log.error(F("TPL : Unable to allocate memory for transforming template, " + "needed %d." CR), + size); + return; + } + + memset(_output, 0, size + 20); + + // Lets do the transformation + int k = 0; + for (int j = 0; j < len - 2; j++) { + if (*(format + j) == '$' && *(format + j + 1) == '{') { + // Start of format tag found + int max = sizeof(_items) / sizeof(KeyVal); + for (int i = 0; i < max; i++) { + if (strncmp(format + j, _items[i].key.c_str(), + _items[i].key.length()) == 0) { + // Found key + strncat(_output, format + k, j - k); + strcat(_output, _items[i].val.c_str()); + k = j + _items[i].key.length(); + } + } + } + } + strcat(_output, format + k); } void dumpAll() { - int max = sizeof(items) / sizeof(KeyVal); + int max = sizeof(_items) / sizeof(KeyVal); for (int i = 0; i < max; i++) { Serial.print("Key=\'"); - Serial.print(items[i].key.c_str()); + Serial.print(_items[i].key.c_str()); Serial.print("\', Val=\'"); - Serial.print(items[i].val.c_str()); + Serial.print(_items[i].val.c_str()); Serial.println("\'"); } } @@ -138,9 +182,16 @@ class TemplatingEngine { TEMPLATE_MQTT = 4 }; + TemplatingEngine() {} + ~TemplatingEngine() { freeMemory(); } + + void freeMemory() { + if (_output) free(_output); + _output = 0; + } void initialize(float angle, float gravitySG, float corrGravitySG, float tempC, float runTime); - const String& create(TemplatingEngine::Templates idx); + const char *create(TemplatingEngine::Templates idx); }; #endif // SRC_TEMPLATING_HPP_ diff --git a/src/webserver.cpp b/src/webserver.cpp index b067ddc..78dc4e6 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -232,7 +232,8 @@ void WebServerHandler::webHandleUploadFile() { delay(500); ESP_RESET(); } else { - writeErrorLog("WEB : Failed to finish firmware flashing error=%d", Update.getError()); + writeErrorLog("WEB : Failed to finish firmware flashing error=%d", + Update.getError()); _uploadReturn = 500; } } else { @@ -290,9 +291,6 @@ void WebServerHandler::webHandleCalibrate() { LOG_PERF_STOP("webserver-api-calibrate"); } -// -// Callback from webServer when / has been accessed. -// void WebServerHandler::webHandleFactoryDefaults() { String id = _server->arg(PARAM_ID); Log.notice(F("WEB : webServer callback for /api/factory." CR)); @@ -316,9 +314,20 @@ void WebServerHandler::webHandleFactoryDefaults() { } } -// -// Callback from webServer when / has been accessed. -// +void WebServerHandler::webHandleLogClear() { + String id = _server->arg(PARAM_ID); + Log.notice(F("WEB : webServer callback for /api/clearlog." CR)); + + if (!id.compareTo(myConfig.getID())) { + _server->send(200, "text/plain", "Removing logfiles..."); + LittleFS.remove(ERR_FILENAME); + LittleFS.remove(ERR_FILENAME2); + _server->send(200, "text/plain", "Logfiles cleared."); + } else { + _server->send(400, "text/plain", "Unknown ID."); + } +} + void WebServerHandler::webHandleStatus() { LOG_PERF_START("webserver-api-status"); Log.notice(F("WEB : webServer callback for /api/status(get)." CR)); @@ -385,9 +394,6 @@ void WebServerHandler::webHandleStatus() { LOG_PERF_STOP("webserver-api-status"); } -// -// Callback from webServer when / has been accessed. -// void WebServerHandler::webHandleClearWIFI() { String id = _server->arg(PARAM_ID); Log.notice(F("WEB : webServer callback for /api/clearwifi." CR)); @@ -408,9 +414,6 @@ void WebServerHandler::webHandleClearWIFI() { } } -// -// Used to force the device to never sleep. -// void WebServerHandler::webHandleStatusSleepmode() { LOG_PERF_START("webserver-api-sleepmode"); String id = _server->arg(PARAM_ID); @@ -1299,27 +1302,22 @@ bool WebServerHandler::setupWebServer() { _server->serveStatic("/runtime", LittleFS, RUNTIME_FILENAME); // Dynamic content - _server->on( - "/api/config", HTTP_GET, - std::bind(&WebServerHandler::webHandleConfig, this)); // Get config.json + _server->on("/api/clearlog", HTTP_GET, + std::bind(&WebServerHandler::webHandleLogClear, this)); + _server->on("/api/config", HTTP_GET, + std::bind(&WebServerHandler::webHandleConfig, this)); _server->on("/api/formula", HTTP_GET, - std::bind(&WebServerHandler::webHandleFormulaRead, - this)); // Get formula.json (calibration page) + std::bind(&WebServerHandler::webHandleFormulaRead, this)); _server->on("/api/formula", HTTP_POST, - std::bind(&WebServerHandler::webHandleFormulaWrite, - this)); // Get formula.json (calibration page) + std::bind(&WebServerHandler::webHandleFormulaWrite, this)); _server->on("/api/calibrate", HTTP_POST, - std::bind(&WebServerHandler::webHandleCalibrate, - this)); // Run calibration routine (param id) + std::bind(&WebServerHandler::webHandleCalibrate, this)); _server->on("/api/factory", HTTP_GET, - std::bind(&WebServerHandler::webHandleFactoryDefaults, - this)); // Reset the device + std::bind(&WebServerHandler::webHandleFactoryDefaults, this)); _server->on("/api/status", HTTP_GET, - std::bind(&WebServerHandler::webHandleStatus, - this)); // Get the status.json + std::bind(&WebServerHandler::webHandleStatus, this)); _server->on("/api/clearwifi", HTTP_GET, - std::bind(&WebServerHandler::webHandleClearWIFI, - this)); // Clear wifi settings + std::bind(&WebServerHandler::webHandleClearWIFI, this)); _server->on( "/api/upload", HTTP_GET, std::bind(&WebServerHandler::webHandleUpload, this)); // Get upload.json @@ -1366,9 +1364,6 @@ bool WebServerHandler::setupWebServer() { return true; } -// -// called from main loop -// void WebServerHandler::loop() { #if defined(ESP8266) MDNS.update(); diff --git a/src/webserver.hpp b/src/webserver.hpp index e166158..e820098 100644 --- a/src/webserver.hpp +++ b/src/webserver.hpp @@ -76,6 +76,7 @@ class WebServerHandler { void webHandleCalibrate(); void webHandleUploadFile(); void webHandleUpload(); + void webHandleLogClear(); void webHandlePageNotFound(); String readFile(String fname); diff --git a/src/wifi.cpp b/src/wifi.cpp index 8cb1d73..35bc2e2 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -203,7 +203,7 @@ bool WifiConnection::waitForConnection(int maxTime) { if (i++ > (maxTime * 10)) { // Try for maxTime seconds. Since delay is 100ms. - writeErrorLog("WIFI: Failed to connect to wifi %d",WiFi.status()); + writeErrorLog("WIFI: Failed to connect to wifi %d", WiFi.status()); WiFi.disconnect(); Serial.print(CR); return false; // Return to main that we have failed to connect.