From 3d5af2656f6041d6dd62a26db40ac8a09292feea Mon Sep 17 00:00:00 2001 From: Andreas Merkle Date: Tue, 22 Oct 2024 16:33:30 +0200 Subject: [PATCH 1/2] Upgrade to ArduinoJSON v7.2.0 #171 --- lib/ConvoyFollower/src/App.cpp | 15 ++-- lib/ConvoyLeader/src/App.cpp | 15 ++-- lib/MainNative/src/main.cpp | 3 +- lib/PlatoonService/src/V2VCommManager.cpp | 59 +++++++-------- lib/PlatoonService/src/Waypoint.cpp | 54 ++++++------- lib/RemoteControl/src/App.cpp | 29 ++++--- lib/SensorFusion/src/App.cpp | 14 ++-- lib/Service/src/SettingsHandler.cpp | 92 +++++++++++------------ platformio.ini | 14 ++-- 9 files changed, 136 insertions(+), 159 deletions(-) diff --git a/lib/ConvoyFollower/src/App.cpp b/lib/ConvoyFollower/src/App.cpp index 1cef4bc4..e7caf865 100644 --- a/lib/ConvoyFollower/src/App.cpp +++ b/lib/ConvoyFollower/src/App.cpp @@ -85,9 +85,6 @@ const char* App::TOPIC_NAME_BIRTH = "dcs/birth"; /* MQTT topic name for will messages. */ const char* App::TOPIC_NAME_WILL = "dcs/will"; -/** Buffer size for JSON serialization of birth / will message */ -static const uint32_t JSON_BIRTHMESSAGE_MAX_SIZE = 64U; - /****************************************************************************** * Public Methods *****************************************************************************/ @@ -273,14 +270,14 @@ void App::fatalErrorHandler() bool App::setupMqttClient() { /* Setup MQTT Server, Birth and Will messages. */ - bool isSuccessful = false; - SettingsHandler& settings = SettingsHandler::getInstance(); - StaticJsonDocument birthDoc; - String birthMessage; + bool isSuccessful = false; + SettingsHandler& settings = SettingsHandler::getInstance(); + JsonDocument jsonBirthDoc; + String birthMessage; - birthDoc["name"] = settings.getRobotName(); + jsonBirthDoc["name"] = settings.getRobotName(); - if (0U == serializeJson(birthDoc, birthMessage)) + if (0U == serializeJson(jsonBirthDoc, birthMessage)) { /* Non-fatal error. Birth message will be empty. */ LOG_ERROR("Failed to serialize birth message."); diff --git a/lib/ConvoyLeader/src/App.cpp b/lib/ConvoyLeader/src/App.cpp index eadc7fb7..e103f671 100644 --- a/lib/ConvoyLeader/src/App.cpp +++ b/lib/ConvoyLeader/src/App.cpp @@ -85,9 +85,6 @@ const char* App::TOPIC_NAME_BIRTH = "dcs/birth"; /* MQTT topic name for will messages. */ const char* App::TOPIC_NAME_WILL = "dcs/will"; -/** Buffer size for JSON serialization of birth / will message */ -static const uint32_t JSON_BIRTHMESSAGE_MAX_SIZE = 64U; - /****************************************************************************** * Public Methods *****************************************************************************/ @@ -274,14 +271,14 @@ void App::fatalErrorHandler() bool App::setupMqttClient() { /* Setup MQTT Server, Birth and Will messages. */ - bool isSuccessful = false; - SettingsHandler& settings = SettingsHandler::getInstance(); - StaticJsonDocument birthDoc; - String birthMessage; + bool isSuccessful = false; + SettingsHandler& settings = SettingsHandler::getInstance(); + JsonDocument jsonBirthDoc; + String birthMessage; - birthDoc["name"] = settings.getRobotName(); + jsonBirthDoc["name"] = settings.getRobotName(); - if (0U == serializeJson(birthDoc, birthMessage)) + if (0U == serializeJson(jsonBirthDoc, birthMessage)) { /* Non-fatal error. Birth message will be empty. */ LOG_ERROR("Failed to serialize birth message."); diff --git a/lib/MainNative/src/main.cpp b/lib/MainNative/src/main.cpp index d8c00a64..4e1074c5 100644 --- a/lib/MainNative/src/main.cpp +++ b/lib/MainNative/src/main.cpp @@ -527,8 +527,7 @@ static int createConfigFile(const PrgArguments& prgArgs) } else { - const size_t JSON_DOC_SIZE = 2048U; - DynamicJsonDocument jsonDoc(JSON_DOC_SIZE); + JsonDocument jsonDoc; jsonDoc[ConfigurationKeys::ROBOT_NAME] = prgArgs.robotName; jsonDoc[ConfigurationKeys::WIFI][ConfigurationKeys::SSID] = WIFI_SSID_DEFAULT; diff --git a/lib/PlatoonService/src/V2VCommManager.cpp b/lib/PlatoonService/src/V2VCommManager.cpp index 9872633e..2ff21cd2 100644 --- a/lib/PlatoonService/src/V2VCommManager.cpp +++ b/lib/PlatoonService/src/V2VCommManager.cpp @@ -77,12 +77,6 @@ const char* V2VCommManager::TOPIC_NAME_IVS = "ivs"; /* MQTT subtopic name for Platoon Length. */ const char* V2VCommManager::TOPIC_NAME_PLATOON_LENGTH = "length"; -/** Buffer size for JSON serialization of heartbeat messages. */ -static const uint32_t JSON_HEARTBEAT_MAX_SIZE = 128U; - -/** Default size of the JSON Document for parsing. */ -static const uint32_t JSON_DOC_DEFAULT_SIZE = 512U; - /****************************************************************************** * Public Methods *****************************************************************************/ @@ -302,8 +296,8 @@ bool V2VCommManager::sendIVS(const int32_t ivs) const V2VEventType type = V2V_EVENT_IVS; /* Create JSON document. */ - StaticJsonDocument jsonPayload; - String payload; + JsonDocument jsonPayload; + String payload; jsonPayload["ivs"] = ivs; @@ -375,8 +369,8 @@ int32_t V2VCommManager::getPlatoonLength() const void V2VCommManager::eventCallback(const String& payload) { /* Deserialize payload. */ - StaticJsonDocument jsonPayload; - DeserializationError error = deserializeJson(jsonPayload, payload.c_str()); + JsonDocument jsonPayload; + DeserializationError error = deserializeJson(jsonPayload, payload.c_str()); if (DeserializationError::Ok != error) { @@ -384,10 +378,10 @@ void V2VCommManager::eventCallback(const String& payload) } else { - JsonVariant jsonEventVehicleId = jsonPayload["id"]; /* Vehicle ID. */ - JsonVariant jsonEventType = jsonPayload["type"]; /* Event type. */ - JsonVariant jsonEventTimestamp = jsonPayload["timestamp"]; /* Timestamp [ms]. */ - JsonVariant jsonEventData = jsonPayload["data"]; /* Event data. */ + JsonVariantConst jsonEventVehicleId = jsonPayload["id"]; /* Vehicle ID. */ + JsonVariantConst jsonEventType = jsonPayload["type"]; /* Event type. */ + JsonVariantConst jsonEventTimestamp = jsonPayload["timestamp"]; /* Timestamp [ms]. */ + JsonVariantConst jsonEventData = jsonPayload["data"]; /* Event data. */ if ((false == jsonEventVehicleId.isNull()) && (false == jsonEventType.isNull()) && (false == jsonEventTimestamp.isNull()) && (false == jsonEventData.isNull())) @@ -446,8 +440,8 @@ void V2VCommManager::eventCallback(const String& payload) } else { - JsonVariant jsonEventDataStatus = eventDataAsJson["status"]; /* Vehicle status. */ - JsonVariant jsonEventDataTimestamp = eventDataAsJson["timestamp"]; /* Timestamp [ms]. */ + JsonVariantConst jsonEventDataStatus = eventDataAsJson["status"]; /* Vehicle status. */ + JsonVariantConst jsonEventDataTimestamp = eventDataAsJson["timestamp"]; /* Timestamp [ms]. */ if ((false == jsonEventDataStatus.isNull()) && (false == jsonEventDataTimestamp.isNull())) { @@ -466,18 +460,19 @@ void V2VCommManager::eventCallback(const String& payload) case V2V_EVENT_PLATOON_HEARTBEAT: if (PLATOON_LEADER_ID != m_vehicleId) { - JsonVariant jsonEventDataTimestamp = eventDataAsJson["timestamp"]; /* Timestamp [ms]. */ + JsonVariantConst jsonEventDataTimestamp = eventDataAsJson["timestamp"]; /* Timestamp [ms]. */ if (false == jsonEventDataTimestamp.isNull()) { /* Timestamp is sent back to acknowledge synchronization. */ - uint32_t eventDataTimestamp = jsonEventDataTimestamp.as(); - StaticJsonDocument heartbeatDoc; - heartbeatDoc["timestamp"] = eventDataTimestamp; - heartbeatDoc["status"] = m_vehicleStatus; + uint32_t eventDataTimestamp = jsonEventDataTimestamp.as(); + JsonDocument jsonHeartbeatDoc; + + jsonHeartbeatDoc["timestamp"] = eventDataTimestamp; + jsonHeartbeatDoc["status"] = m_vehicleStatus; if (false == publishEvent(m_heartbeatResponseTopic, V2V_EVENT_VEHICLE_HEARTBEAT, - heartbeatDoc.as())) + jsonHeartbeatDoc.as())) { LOG_ERROR("Failed to publish MQTT message to %s.", m_heartbeatResponseTopic.c_str()); } @@ -500,7 +495,7 @@ void V2VCommManager::eventCallback(const String& payload) } else { - JsonVariant jsonEventDataIVS = eventDataAsJson["ivs"]; /* Inter Vehicle Space. */ + JsonVariantConst jsonEventDataIVS = eventDataAsJson["ivs"]; /* Inter Vehicle Space. */ if (false == jsonEventDataIVS.isNull()) { @@ -729,13 +724,13 @@ bool V2VCommManager::sendPlatoonHeartbeat() bool isSuccessful = false; /* Send platoon heartbeat. */ - StaticJsonDocument heartbeatDoc; - String heartbeatPayload; - uint32_t timestamp = millis(); + JsonDocument jsonHeartbeatDoc; + String heartbeatPayload; + uint32_t timestamp = millis(); - heartbeatDoc["timestamp"] = timestamp; + jsonHeartbeatDoc["timestamp"] = timestamp; - if (0U == serializeJson(heartbeatDoc, heartbeatPayload)) + if (0U == serializeJson(jsonHeartbeatDoc, heartbeatPayload)) { LOG_ERROR("Failed to serialize heartbeat."); } @@ -762,8 +757,8 @@ bool V2VCommManager::publishEvent(const String& topic, V2VEventType type, const bool isSuccessful = false; /* Create JSON document. */ - StaticJsonDocument jsonPayload; - String payload; + JsonDocument jsonPayload; + String payload; jsonPayload["id"] = m_vehicleId; jsonPayload["type"] = type; @@ -844,8 +839,8 @@ bool V2VCommManager::sendPlatoonLength(const int32_t length) const V2VEventType type = V2V_EVENT_IVS; /* Create JSON document. */ - StaticJsonDocument jsonPayload; - String payload; + JsonDocument jsonPayload; + String payload; jsonPayload["length"] = length; diff --git a/lib/PlatoonService/src/Waypoint.cpp b/lib/PlatoonService/src/Waypoint.cpp index ea85b4d5..6a6dbd24 100644 --- a/lib/PlatoonService/src/Waypoint.cpp +++ b/lib/PlatoonService/src/Waypoint.cpp @@ -56,18 +56,15 @@ * Local Variables *****************************************************************************/ -/** Default size of the JSON Document for parsing. */ -static const uint32_t JSON_DOC_DEFAULT_SIZE = 1024U; - /****************************************************************************** * Public Methods *****************************************************************************/ Waypoint* Waypoint::deserialize(const String& serializedWaypoint) { - Waypoint* waypoint = nullptr; - StaticJsonDocument jsonPayload; - DeserializationError error = deserializeJson(jsonPayload, serializedWaypoint.c_str()); + Waypoint* waypoint = nullptr; + JsonDocument jsonPayload; + DeserializationError error = deserializeJson(jsonPayload, serializedWaypoint.c_str()); if (DeserializationError::Ok != error) { @@ -84,31 +81,26 @@ Waypoint* Waypoint::deserialize(const String& serializedWaypoint) Waypoint* Waypoint::fromJsonObject(const JsonObject& jsonWaypoint) { - Waypoint* waypoint = nullptr; - - if (jsonWaypoint.containsKey("X") && jsonWaypoint.containsKey("Y") && jsonWaypoint.containsKey("Orientation") && - jsonWaypoint.containsKey("Left") && jsonWaypoint.containsKey("Right") && jsonWaypoint.containsKey("Center")) + Waypoint* waypoint = nullptr; + JsonVariantConst jsonXPos = jsonWaypoint["X"]; /* X position [mm]. */ + JsonVariantConst jsonYPos = jsonWaypoint["Y"]; /* Y position [mm]. */ + JsonVariantConst jsonOrientation = jsonWaypoint["Orientation"]; /* Orientation [mrad]. */ + JsonVariantConst jsonLeft = jsonWaypoint["Left"]; /* Left motor speed [mm/s]. */ + JsonVariantConst jsonRight = jsonWaypoint["Right"]; /* Right motor speed [mm/s]. */ + JsonVariantConst jsonCenter = jsonWaypoint["Center"]; /* Center speed [mm/s]. */ + + if ((false == jsonXPos.isNull()) && (false == jsonYPos.isNull()) && (false == jsonOrientation.isNull()) && + (false == jsonLeft.isNull()) && (false == jsonRight.isNull()) && (false == jsonCenter.isNull())) { - JsonVariant jsonXPos = jsonWaypoint["X"]; /* X position [mm]. */ - JsonVariant jsonYPos = jsonWaypoint["Y"]; /* Y position [mm]. */ - JsonVariant jsonOrientation = jsonWaypoint["Orientation"]; /* Orientation [mrad]. */ - JsonVariant jsonLeft = jsonWaypoint["Left"]; /* Left motor speed [mm/s]. */ - JsonVariant jsonRight = jsonWaypoint["Right"]; /* Right motor speed [mm/s]. */ - JsonVariant jsonCenter = jsonWaypoint["Center"]; /* Center speed [mm/s]. */ - - if ((false == jsonXPos.isNull()) && (false == jsonYPos.isNull()) && (false == jsonOrientation.isNull()) && - (false == jsonLeft.isNull()) && (false == jsonRight.isNull()) && (false == jsonCenter.isNull())) - { - - int32_t xPos = jsonXPos.as(); - int32_t yPos = jsonYPos.as(); - int32_t orientation = jsonOrientation.as(); - int32_t left = jsonLeft.as(); - int32_t right = jsonRight.as(); - int32_t center = jsonCenter.as(); - - waypoint = new (std::nothrow) Waypoint(xPos, yPos, orientation, left, right, center); - } + + int32_t xPos = jsonXPos.as(); + int32_t yPos = jsonYPos.as(); + int32_t orientation = jsonOrientation.as(); + int32_t left = jsonLeft.as(); + int32_t right = jsonRight.as(); + int32_t center = jsonCenter.as(); + + waypoint = new (std::nothrow) Waypoint(xPos, yPos, orientation, left, right, center); } return waypoint; @@ -116,7 +108,7 @@ Waypoint* Waypoint::fromJsonObject(const JsonObject& jsonWaypoint) void Waypoint::serialize(String& serializedWaypoint) const { - StaticJsonDocument jsonPayload; + JsonDocument jsonPayload; jsonPayload["X"] = xPos; /* X position [mm]. */ jsonPayload["Y"] = yPos; /* Y position [mm]. */ diff --git a/lib/RemoteControl/src/App.cpp b/lib/RemoteControl/src/App.cpp index a3383a27..d710487d 100644 --- a/lib/RemoteControl/src/App.cpp +++ b/lib/RemoteControl/src/App.cpp @@ -87,9 +87,6 @@ const char* App::TOPIC_NAME_CMD = "cmd"; /* MQTT topic name for receiving motor speeds. */ const char* App::TOPIC_NAME_MOTOR_SPEEDS = "motorSpeeds"; -/** Default size of the JSON Document for parsing. */ -static const uint32_t JSON_DOC_DEFAULT_SIZE = 1024U; - /** Buffer size for JSON serialization of birth / will message */ static const uint32_t JSON_BIRTHMESSAGE_MAX_SIZE = 64U; @@ -147,12 +144,12 @@ void App::setup() else { /* Setup MQTT Server, Birth and Will messages. */ - StaticJsonDocument birthDoc; - char birthMsgArray[JSON_BIRTHMESSAGE_MAX_SIZE]; - String birthMessage; + JsonDocument jsonBirthDoc; + char birthMsgArray[JSON_BIRTHMESSAGE_MAX_SIZE]; + String birthMessage; - birthDoc["name"] = settings.getRobotName().c_str(); - (void)serializeJson(birthDoc, birthMsgArray); + jsonBirthDoc["name"] = settings.getRobotName().c_str(); + (void)serializeJson(jsonBirthDoc, birthMsgArray); birthMessage = birthMsgArray; /* Setup SerialMuxProt Channels */ @@ -233,7 +230,7 @@ void App::loop() initialVehicleData.orientation = settings.getInitialHeading(); if (true == m_smpServer.sendData(m_serialMuxProtChannelInitialVehicleData, &initialVehicleData, - sizeof(initialVehicleData))) + sizeof(initialVehicleData))) { LOG_DEBUG("Initial vehicle data sent."); m_initialDataSent = true; @@ -275,8 +272,8 @@ void App::fatalErrorHandler() void App::cmdTopicCallback(const String& payload) { - StaticJsonDocument jsonPayload; - DeserializationError error = deserializeJson(jsonPayload, payload.c_str()); + JsonDocument jsonPayload; + DeserializationError error = deserializeJson(jsonPayload, payload.c_str()); if (error != DeserializationError::Ok) { @@ -284,7 +281,7 @@ void App::cmdTopicCallback(const String& payload) } else { - JsonVariant command = jsonPayload["CMD_ID"]; + JsonVariantConst command = jsonPayload["CMD_ID"]; if (false == command.isNull()) { @@ -352,8 +349,8 @@ void App::cmdTopicCallback(const String& payload) void App::motorSpeedsTopicCallback(const String& payload) { - StaticJsonDocument jsonPayload; - DeserializationError error = deserializeJson(jsonPayload, payload.c_str()); + JsonDocument jsonPayload; + DeserializationError error = deserializeJson(jsonPayload, payload.c_str()); if (error != DeserializationError::Ok) { @@ -361,8 +358,8 @@ void App::motorSpeedsTopicCallback(const String& payload) } else { - JsonVariant leftSpeed = jsonPayload["LEFT"]; - JsonVariant rightSpeed = jsonPayload["RIGHT"]; + JsonVariantConst leftSpeed = jsonPayload["LEFT"]; + JsonVariantConst rightSpeed = jsonPayload["RIGHT"]; if ((false == leftSpeed.isNull()) && (false == rightSpeed.isNull())) { diff --git a/lib/SensorFusion/src/App.cpp b/lib/SensorFusion/src/App.cpp index 58928082..547eceb0 100644 --- a/lib/SensorFusion/src/App.cpp +++ b/lib/SensorFusion/src/App.cpp @@ -151,12 +151,12 @@ void App::setup() else { /* Setup MQTT Server, Birth and Will messages. */ - StaticJsonDocument birthDoc; - char birthMsgArray[JSON_BIRTHMESSAGE_MAX_SIZE]; - String birthMessage; + JsonDocument jsonBirthDoc; + char birthMsgArray[JSON_BIRTHMESSAGE_MAX_SIZE]; + String birthMessage; - birthDoc["name"] = settings.getRobotName().c_str(); - (void)serializeJson(birthDoc, birthMsgArray); + jsonBirthDoc["name"] = settings.getRobotName().c_str(); + (void)serializeJson(jsonBirthDoc, birthMsgArray); birthMessage = birthMsgArray; /* Setup SerialMuxProt Channels */ @@ -232,8 +232,8 @@ void App::fatalErrorHandler() void App::publishSensorFusionPosition() { - StaticJsonDocument payloadJson; - char payloadArray[JSON_DOC_DEFAULT_SIZE]; + JsonDocument payloadJson; + char payloadArray[JSON_DOC_DEFAULT_SIZE]; /* Write newest Sensor Fusion Data in JSON String */ IKalmanFilter::PositionData currentPosition = m_sensorFusion.getLatestPosition(); diff --git a/lib/Service/src/SettingsHandler.cpp b/lib/Service/src/SettingsHandler.cpp index bb7f3c67..667570ae 100644 --- a/lib/Service/src/SettingsHandler.cpp +++ b/lib/Service/src/SettingsHandler.cpp @@ -64,10 +64,10 @@ bool SettingsHandler::loadConfigurationFile(const String& filename) { - bool isSuccessful = false; - const uint32_t maxBufferSize = 1024U; - StaticJsonDocument doc; - char buffer[maxBufferSize]; + bool isSuccessful = false; + const uint32_t maxBufferSize = 1024U; + JsonDocument jsonDoc; + char buffer[maxBufferSize]; if (0U == m_fileHandler.readFile(filename, buffer, maxBufferSize)) { @@ -75,7 +75,7 @@ bool SettingsHandler::loadConfigurationFile(const String& filename) } else { - DeserializationError error = deserializeJson(doc, buffer); + DeserializationError error = deserializeJson(jsonDoc, buffer); if (error != DeserializationError::Ok) { @@ -83,25 +83,25 @@ bool SettingsHandler::loadConfigurationFile(const String& filename) } else { - JsonVariantConst jsonRobotName = doc[ConfigurationKeys::ROBOT_NAME]; - JsonVariantConst jsonWifiSsid = doc[ConfigurationKeys::WIFI][ConfigurationKeys::SSID]; - JsonVariantConst jsonWifiPswd = doc[ConfigurationKeys::WIFI][ConfigurationKeys::PASSWORD]; - JsonVariantConst jsonMqttHost = doc[ConfigurationKeys::MQTT][ConfigurationKeys::HOST]; - JsonVariantConst jsonMqttPort = doc[ConfigurationKeys::MQTT][ConfigurationKeys::PORT]; - JsonVariantConst jsonApSSID = doc[ConfigurationKeys::AP][ConfigurationKeys::SSID]; - JsonVariantConst jsonApPswd = doc[ConfigurationKeys::AP][ConfigurationKeys::PASSWORD]; - JsonVariantConst jsonWebServerUser = doc[ConfigurationKeys::WEBSERVER][ConfigurationKeys::USER]; - JsonVariantConst jsonWebServerPswd = doc[ConfigurationKeys::WEBSERVER][ConfigurationKeys::PASSWORD]; - JsonVariantConst jsonPlatoonPlatoonId = doc[ConfigurationKeys::PLATOON][ConfigurationKeys::PLATOON_ID]; - JsonVariantConst jsonPlatoonVehicleId = doc[ConfigurationKeys::PLATOON][ConfigurationKeys::VEHICLE_ID]; + JsonVariantConst jsonRobotName = jsonDoc[ConfigurationKeys::ROBOT_NAME]; + JsonVariantConst jsonWifiSsid = jsonDoc[ConfigurationKeys::WIFI][ConfigurationKeys::SSID]; + JsonVariantConst jsonWifiPswd = jsonDoc[ConfigurationKeys::WIFI][ConfigurationKeys::PASSWORD]; + JsonVariantConst jsonMqttHost = jsonDoc[ConfigurationKeys::MQTT][ConfigurationKeys::HOST]; + JsonVariantConst jsonMqttPort = jsonDoc[ConfigurationKeys::MQTT][ConfigurationKeys::PORT]; + JsonVariantConst jsonApSSID = jsonDoc[ConfigurationKeys::AP][ConfigurationKeys::SSID]; + JsonVariantConst jsonApPswd = jsonDoc[ConfigurationKeys::AP][ConfigurationKeys::PASSWORD]; + JsonVariantConst jsonWebServerUser = jsonDoc[ConfigurationKeys::WEBSERVER][ConfigurationKeys::USER]; + JsonVariantConst jsonWebServerPswd = jsonDoc[ConfigurationKeys::WEBSERVER][ConfigurationKeys::PASSWORD]; + JsonVariantConst jsonPlatoonPlatoonId = jsonDoc[ConfigurationKeys::PLATOON][ConfigurationKeys::PLATOON_ID]; + JsonVariantConst jsonPlatoonVehicleId = jsonDoc[ConfigurationKeys::PLATOON][ConfigurationKeys::VEHICLE_ID]; JsonVariantConst jsonInitialXPosition = - doc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_X_POSITION]; + jsonDoc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_X_POSITION]; JsonVariantConst jsonInitialYPosition = - doc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_Y_POSITION]; + jsonDoc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_Y_POSITION]; JsonVariantConst jsonInitialHeading = - doc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_HEADING]; - JsonVariantConst jsonURosAgentHost = doc[ConfigurationKeys::MICROROS_AGENT][ConfigurationKeys::HOST]; - JsonVariantConst jsonURosAgentPort = doc[ConfigurationKeys::MICROROS_AGENT][ConfigurationKeys::PORT]; + jsonDoc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_HEADING]; + JsonVariantConst jsonURosAgentHost = jsonDoc[ConfigurationKeys::MICROROS_AGENT][ConfigurationKeys::HOST]; + JsonVariantConst jsonURosAgentPort = jsonDoc[ConfigurationKeys::MICROROS_AGENT][ConfigurationKeys::PORT]; if (false == jsonRobotName.isNull()) { @@ -192,32 +192,32 @@ bool SettingsHandler::loadConfigurationFile(const String& filename) bool SettingsHandler::saveConfigurationFile(const String& filename) { - bool isSuccessful = false; - const size_t maxDocSize = 1024U; - StaticJsonDocument doc; - size_t jsonBufferSize = 0U; - size_t bytesToWrite = 0U; - - doc[ConfigurationKeys::ROBOT_NAME] = m_robotName; - doc[ConfigurationKeys::WIFI][ConfigurationKeys::SSID] = m_wifiSSID; - doc[ConfigurationKeys::WIFI][ConfigurationKeys::PASSWORD] = m_wifiPassword; - doc[ConfigurationKeys::MQTT][ConfigurationKeys::HOST] = m_mqttBrokerAddress; - doc[ConfigurationKeys::MQTT][ConfigurationKeys::PORT] = m_mqttPort; - doc[ConfigurationKeys::AP][ConfigurationKeys::SSID] = m_apSSID; - doc[ConfigurationKeys::AP][ConfigurationKeys::PASSWORD] = m_apPassword; - doc[ConfigurationKeys::WEBSERVER][ConfigurationKeys::USER] = m_webServerUser; - doc[ConfigurationKeys::WEBSERVER][ConfigurationKeys::PASSWORD] = m_webServerPassword; - doc[ConfigurationKeys::PLATOON][ConfigurationKeys::PLATOON_ID] = m_platoonPlatoonId; - doc[ConfigurationKeys::PLATOON][ConfigurationKeys::VEHICLE_ID] = m_platoonVehicleId; - doc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_X_POSITION] = m_initialXPosition; - doc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_Y_POSITION] = m_initialYPosition; - doc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_HEADING] = m_initialHeading; - doc[ConfigurationKeys::MICROROS_AGENT][ConfigurationKeys::HOST] = m_microROSAgentAddress; - doc[ConfigurationKeys::MICROROS_AGENT][ConfigurationKeys::PORT] = m_microROSAgentPort; - - jsonBufferSize = measureJsonPretty(doc) + 1U; + bool isSuccessful = false; + const size_t maxDocSize = 1024U; + JsonDocument jsonDoc; + size_t jsonBufferSize = 0U; + size_t bytesToWrite = 0U; + + jsonDoc[ConfigurationKeys::ROBOT_NAME] = m_robotName; + jsonDoc[ConfigurationKeys::WIFI][ConfigurationKeys::SSID] = m_wifiSSID; + jsonDoc[ConfigurationKeys::WIFI][ConfigurationKeys::PASSWORD] = m_wifiPassword; + jsonDoc[ConfigurationKeys::MQTT][ConfigurationKeys::HOST] = m_mqttBrokerAddress; + jsonDoc[ConfigurationKeys::MQTT][ConfigurationKeys::PORT] = m_mqttPort; + jsonDoc[ConfigurationKeys::AP][ConfigurationKeys::SSID] = m_apSSID; + jsonDoc[ConfigurationKeys::AP][ConfigurationKeys::PASSWORD] = m_apPassword; + jsonDoc[ConfigurationKeys::WEBSERVER][ConfigurationKeys::USER] = m_webServerUser; + jsonDoc[ConfigurationKeys::WEBSERVER][ConfigurationKeys::PASSWORD] = m_webServerPassword; + jsonDoc[ConfigurationKeys::PLATOON][ConfigurationKeys::PLATOON_ID] = m_platoonPlatoonId; + jsonDoc[ConfigurationKeys::PLATOON][ConfigurationKeys::VEHICLE_ID] = m_platoonVehicleId; + jsonDoc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_X_POSITION] = m_initialXPosition; + jsonDoc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_Y_POSITION] = m_initialYPosition; + jsonDoc[ConfigurationKeys::INITIAL_POSITION][ConfigurationKeys::INITIAL_HEADING] = m_initialHeading; + jsonDoc[ConfigurationKeys::MICROROS_AGENT][ConfigurationKeys::HOST] = m_microROSAgentAddress; + jsonDoc[ConfigurationKeys::MICROROS_AGENT][ConfigurationKeys::PORT] = m_microROSAgentPort; + + jsonBufferSize = measureJsonPretty(jsonDoc) + 1U; char jsonBuffer[jsonBufferSize]; - bytesToWrite = serializeJsonPretty(doc, jsonBuffer, jsonBufferSize); + bytesToWrite = serializeJsonPretty(jsonDoc, jsonBuffer, jsonBufferSize); if (0U == bytesToWrite) { diff --git a/platformio.ini b/platformio.ini index ed584953..4873778d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -33,7 +33,7 @@ check_skip_packages = yes ; Target environment for ZumoComSystem. ; ***************************************************************************** [target:esp32] -platform = espressif32 @ ~6.4.0 +platform = espressif32 @ ~6.9.0 board = esp32doit-devkit-v1 board_build.filesystem = littlefs framework = arduino @@ -115,7 +115,7 @@ lib_deps = BlueAndi/ArduinoNative @ ~0.2.0 HALTest Utilities - bblanchon/ArduinoJson @ ^6.21.3 + bblanchon/ArduinoJson @ ^7.2.0 MainTestNative lib_ignore = HALSim @@ -133,7 +133,7 @@ lib_deps = Service Utilities gabryelreyes/SerialMuxProt @ ^2.0.0 - bblanchon/ArduinoJson @ ^6.21.3 + bblanchon/ArduinoJson @ ^7.2.0 PlatoonService lib_ignore = ConvoyFollower @@ -152,7 +152,7 @@ lib_deps = Service Utilities gabryelreyes/SerialMuxProt @ ^2.0.0 - bblanchon/ArduinoJson @ ^6.21.3 + bblanchon/ArduinoJson @ ^7.2.0 PlatoonService lib_ignore = ConvoyLeader @@ -171,7 +171,7 @@ lib_deps = Service Utilities gabryelreyes/SerialMuxProt @ ^2.0.0 - bblanchon/ArduinoJson @ ^6.21.3 + bblanchon/ArduinoJson @ ^7.2.0 lib_ignore = ConvoyLeader ConvoyFollower @@ -204,7 +204,7 @@ lib_deps = SensorFusion hideakitai/ArduinoEigen @ ^0.3.0 gabryelreyes/SerialMuxProt @ ^2.0.0 - bblanchon/ArduinoJson @ ^6.21.3 + bblanchon/ArduinoJson @ ^7.2.0 lib_ignore = ConvoyLeader ConvoyFollower @@ -221,7 +221,7 @@ lib_deps = APPTurtle Service Utilities - bblanchon/ArduinoJson @ ^6.21.3 + bblanchon/ArduinoJson @ ^7.2.0 gabryelreyes/SerialMuxProt @ ^2.0.0 lib_ignore = ConvoyLeader From a77dc91ef7844e4b66450d333d296f02b8f6e2f6 Mon Sep 17 00:00:00 2001 From: Andreas Merkle Date: Tue, 22 Oct 2024 16:59:49 +0200 Subject: [PATCH 2/2] Use JSON const objects continuously. --- lib/PlatoonService/src/V2VCommManager.cpp | 12 ++++++------ lib/PlatoonService/src/Waypoint.cpp | 2 +- lib/PlatoonService/src/Waypoint.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/PlatoonService/src/V2VCommManager.cpp b/lib/PlatoonService/src/V2VCommManager.cpp index 2ff21cd2..91135c36 100644 --- a/lib/PlatoonService/src/V2VCommManager.cpp +++ b/lib/PlatoonService/src/V2VCommManager.cpp @@ -386,12 +386,12 @@ void V2VCommManager::eventCallback(const String& payload) if ((false == jsonEventVehicleId.isNull()) && (false == jsonEventType.isNull()) && (false == jsonEventTimestamp.isNull()) && (false == jsonEventData.isNull())) { - bool pushEventToQueue = false; - uint8_t eventVehicleId = jsonEventVehicleId.as(); - V2VEventType eventType = jsonEventType.as(); - uint32_t eventTimestamp = jsonEventTimestamp.as(); - JsonObject eventDataAsJson = jsonEventData.as(); - void* eventData = nullptr; + bool pushEventToQueue = false; + uint8_t eventVehicleId = jsonEventVehicleId.as(); + V2VEventType eventType = jsonEventType.as(); + uint32_t eventTimestamp = jsonEventTimestamp.as(); + JsonObjectConst eventDataAsJson = jsonEventData.as(); + void* eventData = nullptr; switch (eventType) { diff --git a/lib/PlatoonService/src/Waypoint.cpp b/lib/PlatoonService/src/Waypoint.cpp index 6a6dbd24..c407008b 100644 --- a/lib/PlatoonService/src/Waypoint.cpp +++ b/lib/PlatoonService/src/Waypoint.cpp @@ -79,7 +79,7 @@ Waypoint* Waypoint::deserialize(const String& serializedWaypoint) return waypoint; } -Waypoint* Waypoint::fromJsonObject(const JsonObject& jsonWaypoint) +Waypoint* Waypoint::fromJsonObject(const JsonObjectConst& jsonWaypoint) { Waypoint* waypoint = nullptr; JsonVariantConst jsonXPos = jsonWaypoint["X"]; /* X position [mm]. */ diff --git a/lib/PlatoonService/src/Waypoint.h b/lib/PlatoonService/src/Waypoint.h index 06ee6623..687b8a40 100644 --- a/lib/PlatoonService/src/Waypoint.h +++ b/lib/PlatoonService/src/Waypoint.h @@ -114,7 +114,7 @@ struct Waypoint * * @return Pointer to a waypoint object. In case of an error, it returns nullptr. */ - static Waypoint* fromJsonObject(const JsonObject& jsonWaypoint); + static Waypoint* fromJsonObject(const JsonObjectConst& jsonWaypoint); /** * Serialize the waypoint.