@@ -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.
2931Condition *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
140150map<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 }
0 commit comments