Skip to content
Open
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
38 changes: 28 additions & 10 deletions control/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
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()
33 changes: 28 additions & 5 deletions control/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
# Project-in-CMake
*link to the sensor docs:*<br>
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)
<br>
*link to the main control design:*
https://docs.google.com/document/d/1oIO82QiBhqgoNgWj_fBRt7iy48YRa9Y28SvaeaOq1Ms
**Installations:**
<br>
```bash

sudo apt-get install libbson-dev
sudo apt-get install nlohmann-json3-dev
sudo apt install pkg-config

```

google tests:
<br>
```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

```
43 changes: 43 additions & 0 deletions control/camera.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
1 change: 1 addition & 0 deletions control/include/full_condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "basic_condition.h"
#include "root.h"
#include "condition_factory.h"
#include "hsm_support.h"

class Root;

Expand Down
5 changes: 4 additions & 1 deletion control/include/global_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -52,4 +55,4 @@ class GlobalProperties {
static logger controlLogger;
};

#endif // _GLOBAL_PROPERTIES_H_
#endif // _GLOBAL_PROPERTIES_H_
4 changes: 3 additions & 1 deletion control/include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ class Input {
static void s_buildSensors(std::unordered_map<int, Sensor *> &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;
};
Expand Down
20 changes: 19 additions & 1 deletion control/include/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
#include <map>
#include <unordered_map>
#include <set>
#include <optional>
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>
#include "operator_types.h"
#include "global_properties.h"

Expand All @@ -20,6 +23,14 @@ class Sensor {
std::string name;
PacketParser *parser;
std::map<std::string, Field> fieldsMap;
int msgLength;
bool isUsingHSM;

// Variables for the timer functions
int timeForUpdate;
std::atomic<int> 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<std::string, std::pair<FieldValue, std::vector<BasicCondition *>>> fields;
Expand All @@ -35,6 +46,13 @@ class Sensor {
private:
template <typename T>
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_
52 changes: 43 additions & 9 deletions control/src/full_condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, int> 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] == '[')
Expand All @@ -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] == ',')
Expand All @@ -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);

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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<int, int> 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<int, int> mapIndexes;
stack<int> stackIndexes;
// Scans the input string for brackets and uses a stack to keep track of their positions
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
Loading