diff --git a/control/CMakeLists.txt b/control/CMakeLists.txt index 9a9251c1..70cd4d15 100644 --- a/control/CMakeLists.txt +++ b/control/CMakeLists.txt @@ -1,11 +1,13 @@ -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.10) project("MainControl" LANGUAGES CXX) -# Path to local GoogleTest directory -set(GOOGLETEST_PATH "${CMAKE_SOURCE_DIR}/googletest") +# Set the C++ standard +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED True) -# Add GoogleTest -add_subdirectory(${GOOGLETEST_PATH} ${CMAKE_BINARY_DIR}/googletest) +# Locate GTest +find_package(GTest REQUIRED) +include_directories(${GTEST_INCLUDE_DIRS}) # Find and include bson library find_package(PkgConfig REQUIRED) @@ -16,20 +18,36 @@ include_directories(${BSON_INCLUDE_DIRS}) file(GLOB SOURCES "src/*.cpp") list(REMOVE_ITEM SOURCES "${CMAKE_SOURCE_DIR}/src/main.cpp") +# Path to local communication directory + file(GLOB SOURCES_COMMUNICATION "../communication/src/*.cpp") file(GLOB HEADERS_COMMUNICATION "../communication/include/*.h") - file(GLOB SOCKETS_COMMUNICATIONS "../communication/sockets/*.*" ) file(GLOB PARSER "../parser_json/src/*.*") +file(GLOB LOGGER "../logger/*.*") + # Main executable -add_executable(${PROJECT_NAME} ${SOURCES} ${PARSER} ${SOURCES_COMMUNICATION} ${HEADERS_COMMUNICATION} ${SOCKETS_COMMUNICATIONS} ../logger/logger.h ../logger/logger.cpp src/main.cpp) +add_executable(${PROJECT_NAME} ${SOURCES} ${PARSER} ${SOURCES_COMMUNICATION} ${HEADERS_COMMUNICATION} ${SOCKETS_COMMUNICATIONS} ${LOGGER} src/main.cpp ) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(${PROJECT_NAME} PRIVATE ${BSON_LIBRARIES}) +target_link_libraries(${PROJECT_NAME} PRIVATE ${BSON_LIBRARIES} ) # Test executable, including additional source files file(GLOB TEST_SOURCES "test/*.cpp") -add_executable(RunTests ${SOURCES} ${PARSER} ${SOURCES_COMMUNICATION} ${HEADERS_COMMUNICATION} ${SOCKETS_COMMUNICATIONS} ../logger/logger.h ../logger/logger.cpp ${TEST_SOURCES}) +add_executable(RunTests ${SOURCES} ${PARSER} ${SOURCES_COMMUNICATION} ${HEADERS_COMMUNICATION} ${SOCKETS_COMMUNICATIONS} ${LOGGER} ${TEST_SOURCES} ) target_include_directories(RunTests PRIVATE ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(RunTests PRIVATE ${BSON_LIBRARIES} gtest_main) \ No newline at end of file +target_link_libraries(RunTests PRIVATE ${BSON_LIBRARIES} ${GTEST_LIBRARIES} pthread) + + +include(../hsm-client/use_hsm_client.cmake) +find_library(HSM_CLIENT_LIB hsm_client_lib PATHS ${HSM_CLIENT_LIB_PATH}) +if(HSM_CLIENT_LIB) + message(STATUS "Found hsm_client_lib: ${HSM_CLIENT_LIB}") + target_link_libraries(${PROJECT_NAME} PRIVATE ${HSM_CLIENT_LIB} ${HSM_CLIENT_LIB_DEPS}) + target_link_libraries(RunTests PRIVATE ${HSM_CLIENT_LIB} ${HSM_CLIENT_LIB_DEPS}) + target_include_directories(${PROJECT_NAME} PRIVATE ${HSM_CLIENT_INCLUDE_DIRS}) + target_include_directories(RunTests PRIVATE ${HSM_CLIENT_INCLUDE_DIRS}) +else() + message(FATAL_ERROR "Could not find hsm_client_lib at ${HSM_CLIENT_LIB_PATH}") +endif() \ No newline at end of file diff --git a/control/README.md b/control/README.md index dd1769a4..6418576b 100644 --- a/control/README.md +++ b/control/README.md @@ -1,6 +1,29 @@ -# Project-in-CMake -*link to the sensor docs:*
-https://docs.google.com/document/d/1BF9DZk_YQt1yE8mERVzNyPVUT7JgtQxV-Xuj32yoiyM +# Vehicle Computing Simulator +## Main Computer + +[link to the main control design](https://docs.google.com/document/d/1oIO82QiBhqgoNgWj_fBRt7iy48YRa9Y28SvaeaOq1Ms)
-*link to the main control design:* -https://docs.google.com/document/d/1oIO82QiBhqgoNgWj_fBRt7iy48YRa9Y28SvaeaOq1Ms \ No newline at end of file +**Installations:** +
+```bash + +sudo apt-get install libbson-dev +sudo apt-get install nlohmann-json3-dev +sudo apt install pkg-config + +``` + +google tests: +
+```bash + +sudo apt update +sudo apt install cmake libgtest-dev +cd /usr/src/googletest/googletest +sudo mkdir build +cd build +sudo cmake .. +sudo make +sudo make install + +``` diff --git a/control/camera.json b/control/camera.json new file mode 100644 index 00000000..7a1f8801 --- /dev/null +++ b/control/camera.json @@ -0,0 +1,43 @@ +{ + "endianness": "little", + "HSMusage": true, + "fields": [ + { + "fields": [ + { + "name": "MessageType", + "size": 1, + "type": "unsigned_int", + "defaultValue": 1 + }, + { + "name": "Level", + "size": 3, + "type": "unsigned_int", + "defaultValue": 1 + }, + { + "name": "ObjectType", + "size": 4, + "type": "unsigned_int", + "defaultValue": 1 + } + ], + "name": "AlertDetails", + "size": 8, + "type": "bit_field" + }, + { + "name": "ObjectDistance", + "size": 32, + "type": "float_fixed", + "defaultValue": 100.0 + }, + { + "name": "relativeVelocity", + "size": 32, + "type": "float_fixed", + "defaultValue": 100 + } + ] +} \ No newline at end of file diff --git a/control/include/full_condition.h b/control/include/full_condition.h index 0c89669b..a98e41a1 100644 --- a/control/include/full_condition.h +++ b/control/include/full_condition.h @@ -14,6 +14,7 @@ #include "basic_condition.h" #include "root.h" #include "condition_factory.h" +#include "hsm_support.h" class Root; diff --git a/control/include/global_properties.h b/control/include/global_properties.h index cc3eac17..9dddd8d7 100644 --- a/control/include/global_properties.h +++ b/control/include/global_properties.h @@ -13,6 +13,7 @@ #include "input.h" #include "full_condition.h" #include "sensor.h" +#include "hsm_support.h" #include "../../communication/include/communication.h" #include "../../logger/logger.h" @@ -23,6 +24,8 @@ class FullCondition; // Forward declaration instead of #include class Sensor; +constexpr int BITS_IN_BYTE = 8; + // Singleton class managing global properties class GlobalProperties { private: @@ -52,4 +55,4 @@ class GlobalProperties { static logger controlLogger; }; -#endif // _GLOBAL_PROPERTIES_H_ \ No newline at end of file +#endif // _GLOBAL_PROPERTIES_H_ diff --git a/control/include/input.h b/control/include/input.h index 8fcf3b6f..314e9717 100644 --- a/control/include/input.h +++ b/control/include/input.h @@ -19,10 +19,12 @@ class Input { static void s_buildSensors(std::unordered_map &sensors); // Function that builds the conditions according to the bson file static void s_buildConditions(); + // Function to set the path to the BSON file - for using specific path for the tests + static void s_setPathBson(); private: // Function that read the bson file - static bson_t *s_readData(); + static bson_t *s_readData(string fileName); // Member that contains the bson file data static bson_t *document; }; diff --git a/control/include/sensor.h b/control/include/sensor.h index 1bb1beeb..d3f86376 100644 --- a/control/include/sensor.h +++ b/control/include/sensor.h @@ -6,7 +6,10 @@ #include #include #include -#include +#include +#include +#include +#include #include "operator_types.h" #include "global_properties.h" @@ -20,6 +23,14 @@ class Sensor { std::string name; PacketParser *parser; std::map fieldsMap; + int msgLength; + bool isUsingHSM; + + // Variables for the timer functions + int timeForUpdate; + std::atomic timerCounter; // Flag to indicate the remaining time in seconds + std::mutex mtx; + std::thread timerThread; // Contains the current values of various fields and a list of basic conditions associated with each field std::map>> fields; @@ -35,6 +46,13 @@ class Sensor { private: template bool applyComparison(T a, T b, OperatorTypes op); + + // Function that running until the timeout is reached and activate the update. + void delayedFunction(); + // Function that activate or reset the timer + void startOrResetTimer(); + // Function that update the fields to be the default values + void updateDefualtValues(); }; #endif // _SENSOR_H_ \ No newline at end of file diff --git a/control/src/full_condition.cpp b/control/src/full_condition.cpp index 7dd0cb84..e26dbb90 100644 --- a/control/src/full_condition.cpp +++ b/control/src/full_condition.cpp @@ -22,18 +22,23 @@ void defineCurrentSensor(const string &condition, int &index) index = closeBracket + 1; currentSensor = instanceGP.sensors[id]; - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "The current sensor id is: " + to_string(currentSensor->id)); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + "The current sensor id is: " + to_string(currentSensor->id)); } // Recursively builds the condition tree from the condition string. Condition *FullCondition::buildNode(const string &condition, int &index, map bracketIndexes) { - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Entering buildNode function, condition[index] = " + condition[index]); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + "Entering buildNode function, condition[index] = " + condition[index]); GlobalProperties &instanceGP = GlobalProperties::getInstance(); if (condition.empty()) - GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, "Condition string is empty"); + GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, + "Condition string is empty"); // Handling sensor reference if (condition[index] == '[') @@ -48,11 +53,13 @@ Condition *FullCondition::buildNode(const string &condition, int &index, (currentSensor ? to_string(currentSensor->id) : "-") + condition.substr(index, bracketIndexes[openBracketIndex] - index + 1); - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Generated condition key: " + key); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, "Generated condition key: " + key); // Check if the key already exists in the existingConditions map if (s_existingConditions.find(key) != s_existingConditions.end()) { - instanceGP.controlLogger.logMessage(logger::LogLevel::DEBUG, "Condition key already exists: " + key); + instanceGP.controlLogger.logMessage( + logger::LogLevel::DEBUG, "Condition key already exists: " + key); index = bracketIndexes[openBracketIndex] + 1; if (condition[index] == ',') @@ -65,7 +72,9 @@ Condition *FullCondition::buildNode(const string &condition, int &index, OperatorTypes operatorType = convertStringToOperatorTypes( condition.substr(index, openBracketIndex - index)); - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Operator type: " + std::to_string(operatorType)); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + "Operator type: " + std::to_string(operatorType)); Condition *conditionPtr = createCondition(operatorType); @@ -111,7 +120,8 @@ Condition *FullCondition::buildNode(const string &condition, int &index, string name = condition.substr(openBracketIndex + 1, commaIndex - openBracketIndex - 1); - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Field name: " + name); + GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, + "Field name: " + name); int closeBracket = bracketIndexes[openBracketIndex]; @@ -139,7 +149,8 @@ Condition *FullCondition::buildNode(const string &condition, int &index, // Maps the positions of opening bracket indexes to their corresponding closing bracket indexes map findBrackets(string condition) { - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Generate a map with the brackets indexes"); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, "Generate a map with the brackets indexes"); map mapIndexes; stack stackIndexes; // Scans the input string for brackets and uses a stack to keep track of their positions @@ -172,7 +183,8 @@ FullCondition::FullCondition(string condition, this->buildNode(condition, index, bracketsIndexes); root = new Root(this->id, firstCondition); - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "The tree created successfully "); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, "The tree created successfully "); firstCondition->parents.push_back(root); currentSensor = nullptr; @@ -188,6 +200,28 @@ void FullCondition::activateActions() const char *message = action.second.c_str(); size_t dataSize = strlen(message) + 1; uint32_t destID = action.first; + if (instanceGP.sensors[destID]->isUsingHSM) { + // Get the length of the encrypted data + size_t encryptedLength = + hsm::getEncryptedLen(instanceGP.srcID, dataSize); + uint8_t encryptedData[encryptedLength]; + + if (hsm::encryptData((const void *)message, dataSize, encryptedData, + encryptedLength, instanceGP.srcID, destID)) { + instanceGP.controlLogger.logMessage( + logger::LogLevel::INFO, + "The message encrypted successfully"); + instanceGP.comm->sendMessage(encryptedData, encryptedLength, + destID, instanceGP.srcID, false); + } + else { + instanceGP.controlLogger.logMessage( + logger::LogLevel::ERROR, "The message encryption failed"); + instanceGP.comm->sendMessage((void *)message, dataSize, destID, + instanceGP.srcID, false); + } + } + else instanceGP.comm->sendMessage((void *)message, dataSize, destID, instanceGP.srcID, false); } diff --git a/control/src/global_properties.cpp b/control/src/global_properties.cpp index 4a4b78cf..884983b7 100644 --- a/control/src/global_properties.cpp +++ b/control/src/global_properties.cpp @@ -1,17 +1,25 @@ #include "global_properties.h" using namespace std; -void handleMesseage(uint32_t senderId,void *data) +void handleMesseage(uint32_t senderId, void *data) { GlobalProperties &instanceGP = GlobalProperties::getInstance(); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::INFO, "Received message from id " + senderId); - GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "Received message from id " + senderId); + if (instanceGP.sensors[senderId]->isUsingHSM) { + if (hsm::decryptData(data, senderId, instanceGP.srcID)) { + instanceGP.controlLogger.logMessage( + logger::LogLevel::INFO, "The message dycrypted successfully"); + } + else { + instanceGP.controlLogger.logMessage( + logger::LogLevel::ERROR, "The message dycryption failed"); + instanceGP.sensors[senderId]->isUsingHSM = false; + } + } - char * msg = "I got message"; - size_t dataSize = strlen(msg) + 1; - instanceGP.comm->sendMessage((void*)msg, dataSize, senderId, instanceGP.srcID, false); instanceGP.sensors[senderId]->handleMessage(data); - for (int cId : instanceGP.trueConditions) instanceGP.conditions[cId]->activateActions(); @@ -25,17 +33,21 @@ int readIdFromJson() // Check if the input is correct if (!f.is_open()) - GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, "Failed to open config.json"); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::ERROR, "Failed to open config.json"); json *data = NULL; // Try parse to json type try { data = new json(json::parse(f)); f.close(); - GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "The id was successfully read from config.json"); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::INFO, + "The id was successfully read from config.json"); } catch (exception e) { - GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, e.what()); + GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, + e.what()); } return (*data)["ID"]; @@ -45,10 +57,11 @@ int readIdFromJson() GlobalProperties::GlobalProperties() { controlLogger.logMessage(logger::LogLevel::INFO, "Initializing..."); - + // Build the sensors according the json file Input::s_buildSensors(sensors); - controlLogger.logMessage(logger::LogLevel::INFO, "Sensors built successfully"); + controlLogger.logMessage(logger::LogLevel::INFO, + "Sensors built successfully"); srcID = readIdFromJson(); // Creating the communication object with the callback function to process the data diff --git a/control/src/input.cpp b/control/src/input.cpp index 1f2a1f48..b04acc44 100644 --- a/control/src/input.cpp +++ b/control/src/input.cpp @@ -193,10 +193,16 @@ void Input::s_buildConditions() GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "Conditions built successfully"); } +// Function to set the path to the BSON file - for using specific path for the tests. +void Input::s_setPathBson() +{ + // Load the bson file into document + document = s_readData("../test/conditions.bson"); +} + // Function that read the bson file -bson_t *Input::s_readData() +bson_t *Input::s_readData(string fileName) { - string fileName = "../conditions.bson"; ifstream file(fileName, ios::binary); if (file.is_open()) { // Get the file size @@ -234,4 +240,4 @@ bson_t *Input::s_readData() } // Load the bson file into document -bson_t *Input::document = s_readData(); \ No newline at end of file +bson_t *Input::document = s_readData("../conditions.bson"); \ No newline at end of file diff --git a/control/src/main.cpp b/control/src/main.cpp index 9290680c..58de2ea5 100644 --- a/control/src/main.cpp +++ b/control/src/main.cpp @@ -4,7 +4,7 @@ #include "input.h" #include "full_condition.h" #include "global_properties.h" -// #include "../parser_json/src/packet_parser.h" + using namespace std; int main() @@ -13,7 +13,9 @@ int main() // Build the conditions from the bson file Input::s_buildConditions(); - GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "Initialized successfully, Starting Communication..."); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::INFO, + "Initialized successfully, Starting Communication..."); // Starting communication with the server instanceGP.comm->startConnection(); diff --git a/control/src/operator_node.cpp b/control/src/operator_node.cpp index b54ccb66..02f0c3f2 100644 --- a/control/src/operator_node.cpp +++ b/control/src/operator_node.cpp @@ -9,8 +9,6 @@ void OperatorNode::updateTree() // Update the status of the current node this->updateStatus(); - // TODO: Check what would happen if the root was true and we got to it with true - // Check if the status changed if (prevStatus != this->status) { // Go over all the parents and update them diff --git a/control/src/sensor.cpp b/control/src/sensor.cpp index c63d1c27..0bbd6b08 100644 --- a/control/src/sensor.cpp +++ b/control/src/sensor.cpp @@ -1,32 +1,120 @@ #include "sensor.h" using namespace std; +// Function that running until the timeout is reached and activate the update. +void Sensor::delayedFunction() +{ + while (timerCounter > 0) { + this_thread::sleep_for( + chrono::milliseconds(1000)); // Sleep for 1 second + timerCounter.fetch_sub(1); // Atomically decrement the timer + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + "Timer of " + name + " sensor with id: " + to_string(id) + + ", time left: " + to_string(timerCounter.load()) + " seconds"); + } + updateDefualtValues(); +} + +// Function that activate or reset the timer +void Sensor::startOrResetTimer() +{ + lock_guard lock(mtx); + if (timerCounter == 0) { + // Timer is not active, set the timer to 10 seconds and start it + timerCounter = timeForUpdate; + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, "Starting new timer for " + name + + " sensor with id " + to_string(id)); + timerThread = thread(&Sensor::delayedFunction, this); + timerThread.detach(); // Detach the thread so it can run independently + } + else { + // Timer is already running, just reset it to 10 seconds + timerCounter = timeForUpdate; + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, name + " sensor with id " + to_string(id) + + ": Timer is active, resetting to " + + to_string(timeForUpdate) + " seconds"); + } +} + +// Function that update the fields to be the default values +void Sensor::updateDefualtValues() +{ + GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, + "Update defualt values: "); + for (auto field : fieldsMap) { + updateTrueRoots(field.second.name, field.second.defaultValue, + parser->getFieldType(field.second.type)); + } + + // TODO: check if the default value have to send a message + GlobalProperties &instanceGP = GlobalProperties::getInstance(); + for (int cId : instanceGP.trueConditions) + instanceGP.conditions[cId]->activateActions(); +} + +bool readHsmUsage(string jsonFilePath) +{ + // Read the json file + ifstream f(jsonFilePath); + + // Check if the input is correct + if (!f.is_open()) + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::ERROR, "Failed to open " + jsonFilePath); + json *data = NULL; + + // Try parse to json type + try { + data = new json(json::parse(f)); + f.close(); + } + catch (exception e) { + GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, + e.what()); + } + + return (bool)(*data)["HSMusage"]; +} + // C-tor initializes the id member variable. Sensor::Sensor(int id, string name, string jsonFilePath) : id(id), name(name) { + timeForUpdate = 10; + timerCounter = 0; + isUsingHSM = readHsmUsage(jsonFilePath); + msgLength = 0; + parser = new PacketParser(jsonFilePath); - std::vector tempFields = parser->getFields(); + vector tempFields = parser->getFields(); for (auto field : tempFields) { - if (field.type == "bit_field") + if (field.type == "bit_field") for (auto subField : parser->getBitFieldFields(field.name)) { - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, subField.name + " : " + subField.type); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + subField.name + " : " + subField.type); fieldsMap[subField.name] = subField; } else { - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, field.name + " : " + field.type); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, field.name + " : " + field.type); fieldsMap[field.name] = field; - } + } + msgLength += field.size; } } void Sensor::handleMessage(void *msg) { + startOrResetTimer(); // Starts the timer + parser->setBuffer(msg); for (auto field : fieldsMap) { string fieldName = field.first; - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Processing field: " + fieldName); updateTrueRoots(fieldName, parser->getFieldValue(fieldName), parser->getFieldType(field.second.type)); @@ -47,42 +135,62 @@ void Sensor::updateTrueRoots(string field, FieldValue value, FieldType type) switch (type) { case FieldType::UNSIGNED_INT: { - flag = - applyComparison(get(value), - get(bcValue), op); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + field + " : " + to_string(get(value))); + flag = applyComparison(get(value), + get(bcValue), op); break; } case FieldType::SIGNED_INT: { - flag = applyComparison(get(value), - get(bcValue), op); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + field + " : " + to_string(get(value))); + flag = applyComparison(get(value), get(bcValue), op); break; } case FieldType::CHAR_ARRAY: { - flag = applyComparison(get(value), - get(bcValue), op); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + field + " : " + get(value)); + flag = applyComparison(get(value), get(bcValue), + op); break; } - case FieldType::FLOAT_FIXED: { - flag = applyComparison(get(value), - get(bcValue), op); + case FieldType::FLOAT_FIXED: { + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + field + " : " + to_string(get(value))); + flag = + applyComparison(get(value), get(bcValue), op); break; } case FieldType::FLOAT_MANTISSA: { - flag = applyComparison(get(value), - get(bcValue), op); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + field + " : " + to_string(get(value))); + flag = + applyComparison(get(value), get(bcValue), op); break; } case FieldType::BOOLEAN: { - flag = applyComparison(get(value), - get(bcValue), op); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + field + " : " + to_string(get(value))); + flag = + applyComparison(get(value), get(bcValue), op); break; } case FieldType::DOUBLE: { - flag = applyComparison(get(value), - get(bcValue), op); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + field + " : " + to_string(get(value))); + flag = applyComparison(get(value), get(bcValue), + op); } default: { - GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, "Invalid FieldType encountered"); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::ERROR, "Invalid FieldType encountered"); break; } } @@ -92,14 +200,18 @@ void Sensor::updateTrueRoots(string field, FieldValue value, FieldType type) // If the condition's status has changed if (flag != prevStatus) { - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Condition status changed for field: " + field); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, + "Condition status changed for field: " + field); // Update parent conditions and check if the root condition is true for (Node *parent : bc->parents) { (bc->status) ? parent->countTrueConditions++ : parent->countTrueConditions--; parent->updateTree(); - GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "Updated parent tree for field: " + field); + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::INFO, + "Updated parent tree for field: " + field); } } } @@ -109,20 +221,40 @@ void Sensor::updateTrueRoots(string field, FieldValue value, FieldType type) template bool Sensor::applyComparison(T a, T b, OperatorTypes op) { - GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "applyComparison"); + GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, + "Apply comparison: "); + switch (op) { - case OperatorTypes::e: + case OperatorTypes::e: { + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, ((a == b) ? "True" : "False")); return a == b; - case OperatorTypes::ne: + } + case OperatorTypes::ne: { + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, ((a != b) ? "True" : "False")); return a != b; - case OperatorTypes::l: + } + case OperatorTypes::l: { + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, ((a < b) ? "True" : "False")); return a < b; - case OperatorTypes::b: + } + case OperatorTypes::b: { + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, ((a > b) ? "True" : "False")); return a > b; - case OperatorTypes::le: + } + case OperatorTypes::le: { + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, ((a <= b) ? "True" : "False")); return a <= b; - case OperatorTypes::be: + } + case OperatorTypes::be: { + GlobalProperties::controlLogger.logMessage( + logger::LogLevel::DEBUG, ((a >= b) ? "True" : "False")); return a >= b; - } + } + } return false; } \ No newline at end of file diff --git a/control/test/sensors_data/camera.json b/control/test/sensors_data/camera.json index e281715a..7a1f8801 100644 --- a/control/test/sensors_data/camera.json +++ b/control/test/sensors_data/camera.json @@ -1,22 +1,26 @@ { "endianness": "little", + "HSMusage": true, "fields": [ { "fields": [ { "name": "MessageType", "size": 1, - "type": "unsigned_int" + "type": "unsigned_int", + "defaultValue": 1 }, { "name": "Level", "size": 3, - "type": "unsigned_int" + "type": "unsigned_int", + "defaultValue": 1 }, { "name": "ObjectType", "size": 4, - "type": "unsigned_int" + "type": "unsigned_int", + "defaultValue": 1 } ], "name": "AlertDetails", @@ -26,17 +30,14 @@ { "name": "ObjectDistance", "size": 32, - "type": "float_fixed" + "type": "float_fixed", + "defaultValue": 100.0 }, { - "name": "CarSpeed", + "name": "relativeVelocity", "size": 32, - "type": "unsigned_int" - }, - { - "name": "ObjectSpeed", - "size": 32, - "type": "unsigned_int" + "type": "float_fixed", + "defaultValue": 100 } ] } \ No newline at end of file diff --git a/control/test/sensors_data/communication.json b/control/test/sensors_data/communication.json index ae06b915..0e862c75 100644 --- a/control/test/sensors_data/communication.json +++ b/control/test/sensors_data/communication.json @@ -1,20 +1,24 @@ { "endianness": "little", + "HSMusage": true, "fields": [ { "size": 32, "type": "signed_int", - "name": "Status" + "name": "Status", + "defaultValue": 100 }, { "size": 32, "type": "signed_int", - "name": "Code" + "name": "Code", + "defaultValue": 100 }, { "size": 32, "type": "signed_int", - "name": "Flags" + "name": "Flags", + "defaultValue": 100 } ] } \ No newline at end of file diff --git a/control/test/sensors_data/speed.json b/control/test/sensors_data/speed.json index 070817c4..aa04c3bd 100644 --- a/control/test/sensors_data/speed.json +++ b/control/test/sensors_data/speed.json @@ -1,20 +1,24 @@ { "endianness": "little", + "HSMusage": true, "fields": [ { "size": 8, "type": "boolean", - "name": "Status" + "name": "Status", + "defaultValue": true }, { "size": 64, "type": "char_array", - "name": "Msg" + "name": "Msg", + "defaultValue": "average current speed" }, { "size": 32, "type": "signed_int", - "name": "Speed" + "name": "Speed", + "defaultValue": 100 } ] } \ No newline at end of file diff --git a/control/test/sensors_data/tirePressure.json b/control/test/sensors_data/tirePressure.json index 9edf9d9b..0680d1af 100644 --- a/control/test/sensors_data/tirePressure.json +++ b/control/test/sensors_data/tirePressure.json @@ -1,15 +1,18 @@ { "endianness": "little", + "HSMusage": false, "fields": [ { "size": 32, "type": "float_fixed", - "name": "Temperature" + "name": "Temperature", + "defaultValue": 100 }, { "size": 32, "type": "float_fixed", - "name": "Pressure" + "name": "Pressure", + "defaultValue": 80 } ] } \ No newline at end of file diff --git a/control/test/test_simple_build.cpp b/control/test/test_simple_build.cpp index d6229b43..c086c24f 100644 --- a/control/test/test_simple_build.cpp +++ b/control/test/test_simple_build.cpp @@ -118,6 +118,7 @@ TEST(OrAndCondition, ComplexConditionTest) int main(int argc, char **argv) { + Input::s_setPathBson(); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } \ No newline at end of file