Skip to content

Commit 74d7b01

Browse files
Control: Add support for HSM
Add dycrypt and encrypt support Co-authored-by: Tehila Aharonovich <[email protected]>
1 parent 0dc7c01 commit 74d7b01

File tree

13 files changed

+272
-77
lines changed

13 files changed

+272
-77
lines changed

control/CMakeLists.txt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ include_directories(${BSON_INCLUDE_DIRS})
1818
file(GLOB SOURCES "src/*.cpp")
1919
list(REMOVE_ITEM SOURCES "${CMAKE_SOURCE_DIR}/src/main.cpp")
2020

21+
# Path to local communication directory
22+
2123
file(GLOB SOURCES_COMMUNICATION "../communication/src/*.cpp")
2224
file(GLOB HEADERS_COMMUNICATION "../communication/include/*.h")
2325
file(GLOB SOCKETS_COMMUNICATIONS "../communication/sockets/*.*" )
@@ -27,12 +29,25 @@ file(GLOB PARSER "../parser_json/src/*.*")
2729
file(GLOB LOGGER "../logger/*.*")
2830

2931
# Main executable
30-
add_executable(${PROJECT_NAME} ${SOURCES} ${PARSER} ${SOURCES_COMMUNICATION} ${HEADERS_COMMUNICATION} ${SOCKETS_COMMUNICATIONS} ${LOGGER} src/main.cpp)
32+
add_executable(${PROJECT_NAME} ${SOURCES} ${PARSER} ${SOURCES_COMMUNICATION} ${HEADERS_COMMUNICATION} ${SOCKETS_COMMUNICATIONS} ${LOGGER} src/main.cpp )
3133
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)
32-
target_link_libraries(${PROJECT_NAME} PRIVATE ${BSON_LIBRARIES})
34+
target_link_libraries(${PROJECT_NAME} PRIVATE ${BSON_LIBRARIES} )
3335

3436
# Test executable, including additional source files
3537
file(GLOB TEST_SOURCES "test/*.cpp")
36-
add_executable(RunTests ${SOURCES} ${PARSER} ${SOURCES_COMMUNICATION} ${HEADERS_COMMUNICATION} ${SOCKETS_COMMUNICATIONS} ${LOGGER} ${TEST_SOURCES})
38+
add_executable(RunTests ${SOURCES} ${PARSER} ${SOURCES_COMMUNICATION} ${HEADERS_COMMUNICATION} ${SOCKETS_COMMUNICATIONS} ${LOGGER} ${TEST_SOURCES} )
3739
target_include_directories(RunTests PRIVATE ${CMAKE_SOURCE_DIR}/include)
38-
target_link_libraries(RunTests PRIVATE ${BSON_LIBRARIES} ${GTEST_LIBRARIES} pthread)
40+
target_link_libraries(RunTests PRIVATE ${BSON_LIBRARIES} ${GTEST_LIBRARIES} pthread)
41+
42+
43+
include(../hsm-client/use_hsm_client.cmake)
44+
find_library(HSM_CLIENT_LIB hsm_client_lib PATHS ${HSM_CLIENT_LIB_PATH})
45+
if(HSM_CLIENT_LIB)
46+
message(STATUS "Found hsm_client_lib: ${HSM_CLIENT_LIB}")
47+
target_link_libraries(${PROJECT_NAME} PRIVATE ${HSM_CLIENT_LIB} ${HSM_CLIENT_LIB_DEPS})
48+
target_link_libraries(RunTests PRIVATE ${HSM_CLIENT_LIB} ${HSM_CLIENT_LIB_DEPS})
49+
target_include_directories(${PROJECT_NAME} PRIVATE ${HSM_CLIENT_INCLUDE_DIRS})
50+
target_include_directories(RunTests PRIVATE ${HSM_CLIENT_INCLUDE_DIRS})
51+
else()
52+
message(FATAL_ERROR "Could not find hsm_client_lib at ${HSM_CLIENT_LIB_PATH}")
53+
endif()

control/camera.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"endianness": "little",
3+
"HSMusage": true,
4+
"fields": [
5+
{
6+
"fields": [
7+
{
8+
"name": "MessageType",
9+
"size": 1,
10+
"type": "unsigned_int",
11+
"defaultValue": 1
12+
},
13+
{
14+
"name": "Level",
15+
"size": 3,
16+
"type": "unsigned_int",
17+
"defaultValue": 1
18+
},
19+
{
20+
"name": "ObjectType",
21+
"size": 4,
22+
"type": "unsigned_int",
23+
"defaultValue": 1
24+
}
25+
],
26+
"name": "AlertDetails",
27+
"size": 8,
28+
"type": "bit_field"
29+
},
30+
{
31+
"name": "ObjectDistance",
32+
"size": 32,
33+
"type": "float_fixed",
34+
"defaultValue": 100.0
35+
},
36+
{
37+
"name": "relativeVelocity",
38+
"size": 32,
39+
"type": "float_fixed",
40+
"defaultValue": 100
41+
}
42+
]
43+
}

control/include/full_condition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "basic_condition.h"
1515
#include "root.h"
1616
#include "condition_factory.h"
17+
#include "hsm_support.h"
1718

1819
class Root;
1920

control/include/global_properties.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "input.h"
1414
#include "full_condition.h"
1515
#include "sensor.h"
16+
#include "hsm_support.h"
1617

1718
#include "../../communication/include/communication.h"
1819
#include "../../logger/logger.h"
@@ -23,6 +24,8 @@ class FullCondition;
2324
// Forward declaration instead of #include
2425
class Sensor;
2526

27+
constexpr int BITS_IN_BYTE = 8;
28+
2629
// Singleton class managing global properties
2730
class GlobalProperties {
2831
private:
@@ -52,4 +55,4 @@ class GlobalProperties {
5255
static logger controlLogger;
5356
};
5457

55-
#endif // _GLOBAL_PROPERTIES_H_
58+
#endif // _GLOBAL_PROPERTIES_H_

control/include/sensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Sensor {
2323
std::string name;
2424
PacketParser *parser;
2525
std::map<std::string, Field> fieldsMap;
26+
int msgLength;
27+
bool isUsingHSM;
2628

2729
// Variables for the timer functions
2830
int timeForUpdate;

control/src/full_condition.cpp

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ void defineCurrentSensor(const string &condition, int &index)
2222
index = closeBracket + 1;
2323
currentSensor = instanceGP.sensors[id];
2424

25-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "The current sensor id is: " + to_string(currentSensor->id));
25+
GlobalProperties::controlLogger.logMessage(
26+
logger::LogLevel::DEBUG,
27+
"The current sensor id is: " + to_string(currentSensor->id));
2628
}
2729

2830
// Recursively builds the condition tree from the condition string.
2931
Condition *FullCondition::buildNode(const string &condition, int &index,
3032
map<int, int> bracketIndexes)
3133
{
32-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Entering buildNode function, condition[index] = " + condition[index]);
34+
GlobalProperties::controlLogger.logMessage(
35+
logger::LogLevel::DEBUG,
36+
"Entering buildNode function, condition[index] = " + condition[index]);
3337
GlobalProperties &instanceGP = GlobalProperties::getInstance();
3438

3539
if (condition.empty())
36-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, "Condition string is empty");
40+
GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR,
41+
"Condition string is empty");
3742

3843
// Handling sensor reference
3944
if (condition[index] == '[')
@@ -48,11 +53,13 @@ Condition *FullCondition::buildNode(const string &condition, int &index,
4853
(currentSensor ? to_string(currentSensor->id) : "-") +
4954
condition.substr(index, bracketIndexes[openBracketIndex] - index + 1);
5055

51-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Generated condition key: " + key);
56+
GlobalProperties::controlLogger.logMessage(
57+
logger::LogLevel::DEBUG, "Generated condition key: " + key);
5258

5359
// Check if the key already exists in the existingConditions map
5460
if (s_existingConditions.find(key) != s_existingConditions.end()) {
55-
instanceGP.controlLogger.logMessage(logger::LogLevel::DEBUG, "Condition key already exists: " + key);
61+
instanceGP.controlLogger.logMessage(
62+
logger::LogLevel::DEBUG, "Condition key already exists: " + key);
5663

5764
index = bracketIndexes[openBracketIndex] + 1;
5865
if (condition[index] == ',')
@@ -65,7 +72,9 @@ Condition *FullCondition::buildNode(const string &condition, int &index,
6572
OperatorTypes operatorType = convertStringToOperatorTypes(
6673
condition.substr(index, openBracketIndex - index));
6774

68-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Operator type: " + std::to_string(operatorType));
75+
GlobalProperties::controlLogger.logMessage(
76+
logger::LogLevel::DEBUG,
77+
"Operator type: " + std::to_string(operatorType));
6978

7079
Condition *conditionPtr = createCondition(operatorType);
7180

@@ -111,7 +120,8 @@ Condition *FullCondition::buildNode(const string &condition, int &index,
111120
string name = condition.substr(openBracketIndex + 1,
112121
commaIndex - openBracketIndex - 1);
113122

114-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Field name: " + name);
123+
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG,
124+
"Field name: " + name);
115125

116126
int closeBracket = bracketIndexes[openBracketIndex];
117127

@@ -139,7 +149,8 @@ Condition *FullCondition::buildNode(const string &condition, int &index,
139149
// Maps the positions of opening bracket indexes to their corresponding closing bracket indexes
140150
map<int, int> findBrackets(string condition)
141151
{
142-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "Generate a map with the brackets indexes");
152+
GlobalProperties::controlLogger.logMessage(
153+
logger::LogLevel::DEBUG, "Generate a map with the brackets indexes");
143154
map<int, int> mapIndexes;
144155
stack<int> stackIndexes;
145156
// Scans the input string for brackets and uses a stack to keep track of their positions
@@ -172,7 +183,8 @@ FullCondition::FullCondition(string condition,
172183
this->buildNode(condition, index, bracketsIndexes);
173184
root = new Root(this->id, firstCondition);
174185

175-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::DEBUG, "The tree created successfully ");
186+
GlobalProperties::controlLogger.logMessage(
187+
logger::LogLevel::DEBUG, "The tree created successfully ");
176188

177189
firstCondition->parents.push_back(root);
178190
currentSensor = nullptr;
@@ -188,6 +200,28 @@ void FullCondition::activateActions()
188200
const char *message = action.second.c_str();
189201
size_t dataSize = strlen(message) + 1;
190202
uint32_t destID = action.first;
203+
if (instanceGP.sensors[destID]->isUsingHSM) {
204+
// Get the length of the encrypted data
205+
size_t encryptedLength =
206+
hsm::getEncryptedLen(instanceGP.srcID, dataSize);
207+
uint8_t encryptedData[encryptedLength];
208+
209+
if (hsm::encryptData((const void *)message, dataSize, encryptedData,
210+
encryptedLength, instanceGP.srcID, destID)) {
211+
instanceGP.controlLogger.logMessage(
212+
logger::LogLevel::INFO,
213+
"The message encrypted successfully");
214+
instanceGP.comm->sendMessage(encryptedData, encryptedLength,
215+
destID, instanceGP.srcID, false);
216+
}
217+
else {
218+
instanceGP.controlLogger.logMessage(
219+
logger::LogLevel::ERROR, "The message encryption failed");
220+
instanceGP.comm->sendMessage((void *)message, dataSize, destID,
221+
instanceGP.srcID, false);
222+
}
223+
}
224+
else
191225
instanceGP.comm->sendMessage((void *)message, dataSize, destID,
192226
instanceGP.srcID, false);
193227
}

control/src/global_properties.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
#include "global_properties.h"
22
using namespace std;
33

4-
void handleMesseage(uint32_t senderId,void *data)
4+
void handleMesseage(uint32_t senderId, void *data)
55
{
66
GlobalProperties &instanceGP = GlobalProperties::getInstance();
7+
GlobalProperties::controlLogger.logMessage(
8+
logger::LogLevel::INFO, "Received message from id " + senderId);
79

8-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "Received message from id " + senderId);
10+
if (instanceGP.sensors[senderId]->isUsingHSM) {
11+
if (hsm::decryptData(data, senderId, instanceGP.srcID)) {
12+
instanceGP.controlLogger.logMessage(
13+
logger::LogLevel::INFO, "The message dycrypted successfully");
14+
}
15+
else {
16+
instanceGP.controlLogger.logMessage(
17+
logger::LogLevel::ERROR, "The message dycryption failed");
18+
instanceGP.sensors[senderId]->isUsingHSM = false;
19+
}
20+
}
921

10-
char * msg = "I got message";
11-
size_t dataSize = strlen(msg) + 1;
12-
instanceGP.comm->sendMessage((void*)msg, dataSize, senderId, instanceGP.srcID, false);
1322
instanceGP.sensors[senderId]->handleMessage(data);
14-
1523
for (int cId : instanceGP.trueConditions)
1624
instanceGP.conditions[cId]->activateActions();
1725

@@ -25,17 +33,21 @@ int readIdFromJson()
2533

2634
// Check if the input is correct
2735
if (!f.is_open())
28-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, "Failed to open config.json");
36+
GlobalProperties::controlLogger.logMessage(
37+
logger::LogLevel::ERROR, "Failed to open config.json");
2938
json *data = NULL;
3039

3140
// Try parse to json type
3241
try {
3342
data = new json(json::parse(f));
3443
f.close();
35-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "The id was successfully read from config.json");
44+
GlobalProperties::controlLogger.logMessage(
45+
logger::LogLevel::INFO,
46+
"The id was successfully read from config.json");
3647
}
3748
catch (exception e) {
38-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR, e.what());
49+
GlobalProperties::controlLogger.logMessage(logger::LogLevel::ERROR,
50+
e.what());
3951
}
4052

4153
return (*data)["ID"];
@@ -45,10 +57,11 @@ int readIdFromJson()
4557
GlobalProperties::GlobalProperties()
4658
{
4759
controlLogger.logMessage(logger::LogLevel::INFO, "Initializing...");
48-
60+
4961
// Build the sensors according the json file
5062
Input::s_buildSensors(sensors);
51-
controlLogger.logMessage(logger::LogLevel::INFO, "Sensors built successfully");
63+
controlLogger.logMessage(logger::LogLevel::INFO,
64+
"Sensors built successfully");
5265

5366
srcID = readIdFromJson();
5467
// Creating the communication object with the callback function to process the data

control/src/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "input.h"
55
#include "full_condition.h"
66
#include "global_properties.h"
7-
// #include "../parser_json/src/packet_parser.h"
7+
88
using namespace std;
99

1010
int main()
@@ -13,7 +13,9 @@ int main()
1313
// Build the conditions from the bson file
1414
Input::s_buildConditions();
1515

16-
GlobalProperties::controlLogger.logMessage(logger::LogLevel::INFO, "Initialized successfully, Starting Communication...");
16+
GlobalProperties::controlLogger.logMessage(
17+
logger::LogLevel::INFO,
18+
"Initialized successfully, Starting Communication...");
1719
// Starting communication with the server
1820
instanceGP.comm->startConnection();
1921

0 commit comments

Comments
 (0)