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

Upgrade to ArduinoJSON v7.2.0 #181

Merged
merged 2 commits into from
Oct 23, 2024
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
15 changes: 6 additions & 9 deletions lib/ConvoyFollower/src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*****************************************************************************/
Expand Down Expand Up @@ -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<JSON_BIRTHMESSAGE_MAX_SIZE> 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.");
Expand Down
15 changes: 6 additions & 9 deletions lib/ConvoyLeader/src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*****************************************************************************/
Expand Down Expand Up @@ -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<JSON_BIRTHMESSAGE_MAX_SIZE> 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.");
Expand Down
3 changes: 1 addition & 2 deletions lib/MainNative/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 33 additions & 38 deletions lib/PlatoonService/src/V2VCommManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*****************************************************************************/
Expand Down Expand Up @@ -302,8 +296,8 @@ bool V2VCommManager::sendIVS(const int32_t ivs) const
V2VEventType type = V2V_EVENT_IVS;

/* Create JSON document. */
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
String payload;
JsonDocument jsonPayload;
String payload;

jsonPayload["ivs"] = ivs;

Expand Down Expand Up @@ -375,29 +369,29 @@ int32_t V2VCommManager::getPlatoonLength() const
void V2VCommManager::eventCallback(const String& payload)
{
/* Deserialize payload. */
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
JsonDocument jsonPayload;
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());

if (DeserializationError::Ok != error)
{
LOG_ERROR("JSON Deserialization Error %d.", error);
}
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()))
{
bool pushEventToQueue = false;
uint8_t eventVehicleId = jsonEventVehicleId.as<uint8_t>();
V2VEventType eventType = jsonEventType.as<V2VEventType>();
uint32_t eventTimestamp = jsonEventTimestamp.as<uint32_t>();
JsonObject eventDataAsJson = jsonEventData.as<JsonObject>();
void* eventData = nullptr;
bool pushEventToQueue = false;
uint8_t eventVehicleId = jsonEventVehicleId.as<uint8_t>();
V2VEventType eventType = jsonEventType.as<V2VEventType>();
uint32_t eventTimestamp = jsonEventTimestamp.as<uint32_t>();
JsonObjectConst eventDataAsJson = jsonEventData.as<JsonObjectConst>();
void* eventData = nullptr;

switch (eventType)
{
Expand Down Expand Up @@ -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()))
{
Expand All @@ -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<uint32_t>();
StaticJsonDocument<JSON_HEARTBEAT_MAX_SIZE> heartbeatDoc;
heartbeatDoc["timestamp"] = eventDataTimestamp;
heartbeatDoc["status"] = m_vehicleStatus;
uint32_t eventDataTimestamp = jsonEventDataTimestamp.as<uint32_t>();
JsonDocument jsonHeartbeatDoc;

jsonHeartbeatDoc["timestamp"] = eventDataTimestamp;
jsonHeartbeatDoc["status"] = m_vehicleStatus;

if (false == publishEvent(m_heartbeatResponseTopic, V2V_EVENT_VEHICLE_HEARTBEAT,
heartbeatDoc.as<JsonObject>()))
jsonHeartbeatDoc.as<JsonObject>()))
{
LOG_ERROR("Failed to publish MQTT message to %s.", m_heartbeatResponseTopic.c_str());
}
Expand All @@ -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())
{
Expand Down Expand Up @@ -729,13 +724,13 @@ bool V2VCommManager::sendPlatoonHeartbeat()
bool isSuccessful = false;

/* Send platoon heartbeat. */
StaticJsonDocument<JSON_HEARTBEAT_MAX_SIZE> 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.");
}
Expand All @@ -762,8 +757,8 @@ bool V2VCommManager::publishEvent(const String& topic, V2VEventType type, const
bool isSuccessful = false;

/* Create JSON document. */
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
String payload;
JsonDocument jsonPayload;
String payload;

jsonPayload["id"] = m_vehicleId;
jsonPayload["type"] = type;
Expand Down Expand Up @@ -844,8 +839,8 @@ bool V2VCommManager::sendPlatoonLength(const int32_t length) const
V2VEventType type = V2V_EVENT_IVS;

/* Create JSON document. */
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
String payload;
JsonDocument jsonPayload;
String payload;

jsonPayload["length"] = length;

Expand Down
56 changes: 24 additions & 32 deletions lib/PlatoonService/src/Waypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSON_DOC_DEFAULT_SIZE> jsonPayload;
DeserializationError error = deserializeJson(jsonPayload, serializedWaypoint.c_str());
Waypoint* waypoint = nullptr;
JsonDocument jsonPayload;
DeserializationError error = deserializeJson(jsonPayload, serializedWaypoint.c_str());

if (DeserializationError::Ok != error)
{
Expand All @@ -82,41 +79,36 @@ Waypoint* Waypoint::deserialize(const String& serializedWaypoint)
return waypoint;
}

Waypoint* Waypoint::fromJsonObject(const JsonObject& jsonWaypoint)
Waypoint* Waypoint::fromJsonObject(const JsonObjectConst& 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>();
int32_t yPos = jsonYPos.as<int32_t>();
int32_t orientation = jsonOrientation.as<int32_t>();
int32_t left = jsonLeft.as<int32_t>();
int32_t right = jsonRight.as<int32_t>();
int32_t center = jsonCenter.as<int32_t>();

waypoint = new (std::nothrow) Waypoint(xPos, yPos, orientation, left, right, center);
}

int32_t xPos = jsonXPos.as<int32_t>();
int32_t yPos = jsonYPos.as<int32_t>();
int32_t orientation = jsonOrientation.as<int32_t>();
int32_t left = jsonLeft.as<int32_t>();
int32_t right = jsonRight.as<int32_t>();
int32_t center = jsonCenter.as<int32_t>();

waypoint = new (std::nothrow) Waypoint(xPos, yPos, orientation, left, right, center);
}

return waypoint;
}

void Waypoint::serialize(String& serializedWaypoint) const
{
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
JsonDocument jsonPayload;

jsonPayload["X"] = xPos; /* X position [mm]. */
jsonPayload["Y"] = yPos; /* Y position [mm]. */
Expand Down
2 changes: 1 addition & 1 deletion lib/PlatoonService/src/Waypoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 13 additions & 16 deletions lib/RemoteControl/src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -147,12 +144,12 @@ void App::setup()
else
{
/* Setup MQTT Server, Birth and Will messages. */
StaticJsonDocument<JSON_BIRTHMESSAGE_MAX_SIZE> 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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -275,16 +272,16 @@ void App::fatalErrorHandler()

void App::cmdTopicCallback(const String& payload)
{
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
JsonDocument jsonPayload;
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());

if (error != DeserializationError::Ok)
{
LOG_ERROR("JSON Deserialization Error %d.", error);
}
else
{
JsonVariant command = jsonPayload["CMD_ID"];
JsonVariantConst command = jsonPayload["CMD_ID"];

if (false == command.isNull())
{
Expand Down Expand Up @@ -352,17 +349,17 @@ void App::cmdTopicCallback(const String& payload)

void App::motorSpeedsTopicCallback(const String& payload)
{
StaticJsonDocument<JSON_DOC_DEFAULT_SIZE> jsonPayload;
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());
JsonDocument jsonPayload;
DeserializationError error = deserializeJson(jsonPayload, payload.c_str());

if (error != DeserializationError::Ok)
{
LOG_ERROR("JSON deserialization error %d.", error);
}
else
{
JsonVariant leftSpeed = jsonPayload["LEFT"];
JsonVariant rightSpeed = jsonPayload["RIGHT"];
JsonVariantConst leftSpeed = jsonPayload["LEFT"];
JsonVariantConst rightSpeed = jsonPayload["RIGHT"];

if ((false == leftSpeed.isNull()) && (false == rightSpeed.isNull()))
{
Expand Down
Loading