Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.vscode
.claude
.venv
_*/
cmake-build-debug/
4 changes: 4 additions & 0 deletions CMLibStorage.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
FIND_PACKAGE(CMLIB REQUIRED COMPONENTS CMCONF)

CMCONF_INIT_SYSTEM(FLEET_PROTOCOL)

SET(STORAGE_LIST DEP)

SET(STORAGE_LIST_DEP "https://github.com/bacpack-system/package-tracker.git")
16 changes: 4 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ IF (NOT BRINGAUTO_SYSTEM_DEP)
ENDIF ()

FIND_PACKAGE(nlohmann_json 3.10.5 REQUIRED)
FIND_PACKAGE(Protobuf 3.21.12 REQUIRED)
FIND_PACKAGE(fleet-protocol-interface 2.0.0 REQUIRED)
FIND_PACKAGE(fleet-protocol-cxx-helpers-static 1.1.1 REQUIRED)
FIND_PACKAGE(async-function-execution-shared 1.0.0 REQUIRED)
FIND_PACKAGE(aeron 1.48.6 REQUIRED)

IF (FLEET_PROTOCOL_BUILD_EXTERNAL_SERVER)
FIND_PACKAGE(fleet-http-client-shared 2.0.0 REQUIRED)
FIND_PACKAGE(fleet-http-client-shared 2.0.1 REQUIRED)
### fleet-http-client dependencies
FIND_PACKAGE(Boost CONFIG REQUIRED)
FIND_PACKAGE(ZLIB REQUIRED)
FIND_PACKAGE(cpprestsdk REQUIRED)
FIND_PACKAGE(libbringauto_logger 1.2.0 REQUIRED)
ENDIF ()

CMDEF_ADD_LIBRARY(
Expand All @@ -74,18 +74,11 @@ CMDEF_ADD_LIBRARY(
VERSION ${MISSION_MODULE_VERSION}
)

# Protobuf, JSON mission payload
SET(Protobuf_USE_STATIC_LIBS ON)
FILE(GLOB_RECURSE protobuf_mission_cpp_files "lib/protobuf-mission-module/*")
ADD_LIBRARY(message_handler_lib STATIC ${protobuf_mission_cpp_files})
TARGET_INCLUDE_DIRECTORIES(message_handler_lib PUBLIC "lib/protobuf-mission-module/")
TARGET_LINK_LIBRARIES(message_handler_lib PUBLIC protobuf::libprotobuf nlohmann_json::nlohmann_json)

FILE(GLOB_RECURSE HEADERS "include/*")

ADD_LIBRARY(mission_module_hpp INTERFACE ${HEADERS})
TARGET_INCLUDE_DIRECTORIES(mission_module_hpp INTERFACE "include/")
TARGET_LINK_LIBRARIES(mission_module_hpp INTERFACE message_handler_lib)
TARGET_LINK_LIBRARIES(mission_module_hpp INTERFACE nlohmann_json::nlohmann_json)

FILE(GLOB_RECURSE SOURCES "source/bringauto/*")

Expand All @@ -95,7 +88,6 @@ TARGET_LINK_LIBRARIES(mission_module_sources PUBLIC
mission_module_hpp
fleet-protocol-interface::common-headers-interface
fleet-protocol-cxx-helpers-static::fleet-protocol-cxx-helpers-static
message_handler_lib
)

IF (FLEET_PROTOCOL_BUILD_MODULE_GATEWAY)
Expand Down
8 changes: 4 additions & 4 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
SET(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH FALSE)

BA_PACKAGE_LIBRARY(fleet-protocol-interface v2.0.0 NO_DEBUG ON)
BA_PACKAGE_LIBRARY(fleet-protocol-cxx-helpers-static v1.1.1)
BA_PACKAGE_LIBRARY(fleet-protocol-cpp v1.2.0)
BA_PACKAGE_LIBRARY(async-function-execution v1.0.0)
BA_PACKAGE_LIBRARY(aeron v1.48.6)
BA_PACKAGE_LIBRARY(zlib v1.2.11)
BA_PACKAGE_LIBRARY(nlohmann-json v3.10.5 NO_DEBUG ON)
BA_PACKAGE_LIBRARY(protobuf v4.21.12)

IF (FLEET_PROTOCOL_BUILD_EXTERNAL_SERVER)
BA_PACKAGE_LIBRARY(fleet-http-client-shared v2.0.0)
BA_PACKAGE_LIBRARY(fleet-http-client-shared v2.0.1)
BA_PACKAGE_LIBRARY(boost v1.86.0)
BA_PACKAGE_LIBRARY(cpprestsdk v2.10.20)
BA_PACKAGE_LIBRARY(ba-logger v1.2.0)
ENDIF ()
10 changes: 5 additions & 5 deletions docs/mission_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ flowchart TD

## Structure

Below, the data passed in the messages used in the External Protocol are described. The data structure can also be found in the [.proto](../lib/protobuf-mission-module/) file.
Below, the data passed in the messages used in the External Protocol are described.

## Status payload data

Expand Down Expand Up @@ -166,8 +166,8 @@ Example of command payload when there is no mission defined for the car:

## Validation

The validation of each of the messages is done against the message structure described above (and in the [.proto](../lib/protobuf-mission-module/) file). The functions for validation:
The validation of each of the messages is done against the message structure described above. The functions for validation are in `JsonValidator`:

- `parseAutonomyStatus` for status,
- `parseAutonomyError` for status error,
- `parseAutonomyCommand` for command.
- `JsonValidator::validateAutonomyStatus` for status,
- `JsonValidator::validateAutonomyError` for status error,
- `JsonValidator::validateAutonomyCommand` for command.
81 changes: 81 additions & 0 deletions include/bringauto/JsonHelper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include <bringauto/modules/mission_module/AutonomyAction.hpp>
#include <bringauto/modules/mission_module/AutonomyState.hpp>
#include <fleet_protocol/common_headers/memory_management.h>

#include <nlohmann/json.hpp>


namespace bringauto {

/**
* @brief Utility class for converting between buffers, JSON objects, and mission module enums
*
* Represents a collection of static conversion and validation utilities tied to the
* mission module's JSON message format. Its purpose is to centralise all JSON serialisation,
* deserialisation, and structural validation so that the rest of the module does not
* depend directly on the nlohmann::json API.
*/
class JsonHelper {
public:
/**
* @brief Parses a buffer into a JSON object
* @param json Output JSON object
* @param buffer Input buffer containing a JSON string
* @return OK on success, NOT_OK if parsing fails
*/
static int bufferToJson(nlohmann::ordered_json& json, const buffer& buffer);

/**
* @brief Serializes a JSON object into a buffer
* @param buffer Output buffer
* @param json Input JSON object
* @return OK on success, NOT_OK if allocation fails
*/
static int jsonToBuffer(buffer* buffer, const nlohmann::ordered_json& json);

/**
* @brief Converts a string representation of autonomy state to the corresponding enum value
* @param state String state value (e.g. "DRIVE", "IN_STOP")
* @return Corresponding AutonomyState enum value, ERROR if unrecognized
*/
static modules::mission_module::AutonomyState stringToAutonomyState(std::string_view state);

/**
* @brief Converts an AutonomyState enum value to its string representation
* @param state AutonomyState enum value
* @return String representation (e.g. "DRIVE", "IN_STOP")
*/
static std::string autonomyStateToString(modules::mission_module::AutonomyState state);

/**
* @brief Converts an AutonomyAction enum value to its string representation
* @param action AutonomyAction enum value
* @return String representation (e.g. "START", "STOP", "NO_ACTION")
*/
static std::string autonomyActionToString(modules::mission_module::AutonomyAction action);

/**
* @brief Checks whether a JSON object contains all required fields for an AutonomyStatus message
* @param status JSON object to validate
* @return true if all required fields are present
*/
static bool isValidAutonomyStatus(const nlohmann::ordered_json& status);

/**
* @brief Checks whether a JSON object contains all required fields for an AutonomyCommand message
* @param command JSON object to validate
* @return true if all required fields are present
*/
static bool isValidAutonomyCommand(const nlohmann::ordered_json& command);

/**
* @brief Checks whether a JSON object contains all required fields for an AutonomyError message
* @param errorMessage JSON object to validate
* @return true if all required fields are present
*/
static bool isValidAutonomyError(const nlohmann::ordered_json& errorMessage);
};

}
35 changes: 35 additions & 0 deletions include/bringauto/JsonValidator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <string>


namespace bringauto {

/**
* @brief Validates JSON messages against the expected mission module message schemas
*/
class JsonValidator {
public:
/**
* @brief Validates that a JSON string contains the required fields for an AutonomyStatus message
* @param status JSON string to validate
* @return OK if valid, NOT_OK otherwise
*/
static int validateAutonomyStatus(const std::string &status);

/**
* @brief Validates that a JSON string contains the required fields for an AutonomyCommand message
* @param command JSON string to validate
* @return OK if valid, NOT_OK otherwise
*/
static int validateAutonomyCommand(const std::string &command);

/**
* @brief Validates that a JSON string contains the required fields for an AutonomyError message
* @param errorMessage JSON string to validate
* @return OK if valid, NOT_OK otherwise
*/
static int validateAutonomyError(const std::string &errorMessage);
};

}
26 changes: 0 additions & 26 deletions include/bringauto/ba_json/JsonHelper.hpp

This file was deleted.

15 changes: 15 additions & 0 deletions include/bringauto/modules/mission_module/AutonomyAction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once


namespace bringauto::modules::mission_module {

/**
* @brief Represents the action to be performed by the autonomy device
*/
enum class AutonomyAction {
NO_ACTION,
STOP,
START
};

}
17 changes: 17 additions & 0 deletions include/bringauto/modules/mission_module/AutonomyState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once


namespace bringauto::modules::mission_module {

/**
* @brief Represents the driving state of the autonomy device
*/
enum class AutonomyState {
IDLE,
DRIVE,
IN_STOP,
OBSTACLE,
ERROR
};

}
19 changes: 0 additions & 19 deletions include/bringauto/protobuf/ProtobufHelper.hpp

This file was deleted.

68 changes: 68 additions & 0 deletions source/bringauto/JsonHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <fleet_protocol/common_headers/general_error_codes.h>

#include <bringauto/JsonHelper.hpp>


namespace bringauto {

using json = nlohmann::ordered_json;
using namespace bringauto::modules::mission_module;

int JsonHelper::bufferToJson(json& json, const buffer& buffer) {
const auto buffer_data = static_cast<char*> (buffer.data);
try {
json = json::parse(buffer_data, buffer_data + buffer.size_in_bytes);
} catch (json::parse_error &) {
return NOT_OK;
}
return OK;
}

int JsonHelper::jsonToBuffer(buffer *buffer, const json& json) {
const std::string tmp = nlohmann::to_string(json);
if (allocate(buffer, tmp.size()) == NOT_OK) {
return NOT_OK;
}
std::memcpy(buffer->data, tmp.c_str(), tmp.size());
return OK;
}

AutonomyState JsonHelper::stringToAutonomyState(const std::string_view state) {

Check warning on line 30 in source/bringauto/JsonHelper.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reduce verbosity with "using enum" for "bringauto::modules::mission_module::AutonomyState".

See more on https://sonarcloud.io/project/issues?id=bringauto_mission-module&issues=AZ0kEzU-NWet5u0PAEO4&open=AZ0kEzU-NWet5u0PAEO4&pullRequest=27
if (state == "IDLE") return AutonomyState::IDLE;
if (state == "DRIVE") return AutonomyState::DRIVE;
if (state == "IN_STOP") return AutonomyState::IN_STOP;
if (state == "OBSTACLE") return AutonomyState::OBSTACLE;
return AutonomyState::ERROR;
}

std::string JsonHelper::autonomyStateToString(const AutonomyState state) {
switch (state) {

Check warning on line 39 in source/bringauto/JsonHelper.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reduce verbosity with "using enum" for "bringauto::modules::mission_module::AutonomyState".

See more on https://sonarcloud.io/project/issues?id=bringauto_mission-module&issues=AZ0kEzU-NWet5u0PAEO5&open=AZ0kEzU-NWet5u0PAEO5&pullRequest=27
case AutonomyState::IDLE: return "IDLE";
case AutonomyState::DRIVE: return "DRIVE";
case AutonomyState::IN_STOP: return "IN_STOP";
case AutonomyState::OBSTACLE: return "OBSTACLE";
default: return "ERROR";
}
}

std::string JsonHelper::autonomyActionToString(const AutonomyAction action) {
switch (action) {
case AutonomyAction::STOP: return "STOP";
case AutonomyAction::START: return "START";
default: return "NO_ACTION";
}
}

bool JsonHelper::isValidAutonomyStatus(const json& status) {
return status.contains("state") && status.contains("telemetry") && status.contains("nextStop");
}

bool JsonHelper::isValidAutonomyCommand(const json& command) {
return command.contains("action") && command.contains("stops") && command.contains("route");
}

bool JsonHelper::isValidAutonomyError(const json& errorMessage) {
return errorMessage.contains("finishedStops");
}

}
Loading