From 4ec11c1bd94f7d0667a26e005a8ef8e58f288271 Mon Sep 17 00:00:00 2001
From: athatheocsd <athatheo@csd.auth.gr>
Date: Tue, 25 Dec 2018 18:24:55 +0200
Subject: [PATCH] Added more code

---
 inc/Services/EventActionService.hpp | 11 ++--
 src/Services/EventActionService.cpp | 87 +++++++++++++++++++++--------
 2 files changed, 70 insertions(+), 28 deletions(-)

diff --git a/inc/Services/EventActionService.hpp b/inc/Services/EventActionService.hpp
index 7456b705..5c7dfc13 100644
--- a/inc/Services/EventActionService.hpp
+++ b/inc/Services/EventActionService.hpp
@@ -4,6 +4,7 @@
 #include "Service.hpp"
 #include "MessageParser.hpp"
 #include "etl/String.hpp"
+
 /**
  * Implementation of ST[19] event-action Service
  *
@@ -20,21 +21,20 @@
  */
 class EventActionService : public Service {
 private:
-	uint16_t nextEventDefinitionIndex;
-	uint8_t eventActionFunctionStatus;
-	// Maybe an array of available slots??? Coz there will be random empty slots after deletion
+	uint8_t eventActionFunctionStatus; // Indicates if actions are enabled
 	struct EventActionDefinition {
 		uint8_t empty = 1; // 1 means empty, 0 means full
 		uint16_t applicationId = 0;
 		uint16_t eventDefinitionID = 65535;
 		String<ECSS_MAX_STRING_SIZE> request = "";
 	};
+	// If the size is changed maybe then indexOfAvailableSlots as well as the initiating loop in the
+	// constructor should be changed from uint16_t
 	EventActionDefinition eventActionDefinitionArray[256];
 public:
 	EventActionService() {
 		serviceType = 19;
 		eventActionFunctionStatus = enabledFunction;
-		nextEventDefinitionIndex = 0;
 	}
 
 	/**
@@ -44,6 +44,7 @@ public:
 		disabledFunction = 0,
 		enabledFunction = 1,
 	};
+
 	/**
 	 * TC[19,1] add event-action definitions
 	 */
@@ -98,7 +99,7 @@ public:
 	/**
 	 * Setter for event-action function status
 	 */
-	void setEventActionFunctionStatus(EventActionFunctionStatus e){
+	void setEventActionFunctionStatus(EventActionFunctionStatus e) {
 		eventActionFunctionStatus = e;
 	}
 };
diff --git a/src/Services/EventActionService.cpp b/src/Services/EventActionService.cpp
index a6a6bbb4..8cb8949b 100644
--- a/src/Services/EventActionService.cpp
+++ b/src/Services/EventActionService.cpp
@@ -1,22 +1,36 @@
 #include "Services/EventActionService.hpp"
 #include "Message.hpp"
 #include "MessageParser.hpp"
+
 /**
- * @todo: Should a check be added for lastAddedEventDefinitionIndex to not exceed the
- * eventActionDefinitionArray ?
+ * @todo: Should a check be added for index to not exceed the size of eventActionDefinitionArray
+ * ? Also check if there is needed a uint16_t (in case of changing the size of
+ * eventActionDefinitionArray
  */
-void EventActionService::addEventActionDefinitions(Message message){
+void EventActionService::addEventActionDefinitions(Message message) {
 	// TC[19,1]
 	if (message.messageType == 1 && message.packetType == Message::TC && message.serviceType
-	                                                                     == 19){
+	                                                                     == 19) {
 		uint8_t *data;
 		uint16_t N = message.readUint16();
-		for (uint16_t i = 0; i < N; i++){
-			eventActionDefinitionArray[].applicationId = message
-				.readEnum16();
-			eventActionDefinitionArray[].eventDefinitionID = message.readEnum16();
-			message.readString(data,ECSS_MAX_STRING_SIZE);
-			eventActionDefinitionArray[].request = String<256>(data);
+		uint8_t index = 0;
+		uint8_t flag = 0; // used as boolean 0 is false, 1 is true
+		for (uint16_t i = 0; i < N; i++) {
+			while (eventActionDefinitionArray[index].empty == 0){
+				if (index == 255){ // 255 should be changed depending on size of the array
+					flag = 1;
+					break;
+				}
+				index++;
+			}
+			if (flag == 1){
+				eventActionDefinitionArray[index].empty = 0;
+				eventActionDefinitionArray[index].applicationId = message.readEnum16();
+				eventActionDefinitionArray[index].eventDefinitionID = message.readEnum16();
+				message.readString(data, ECSS_MAX_STRING_SIZE);
+				eventActionDefinitionArray[index].request = String<256>(data);
+				index++;
+			}
 		}
 
 	}
@@ -25,10 +39,30 @@ void EventActionService::addEventActionDefinitions(Message message){
 void EventActionService::deleteEventActionDefinitions(Message message) {
 	// TC[19,2]
 	if (message.messageType == 2 && message.packetType == Message::TC && message.serviceType
-	                                                                     == 19){
+																		 == 19) {
 		uint16_t N = message.readUint16();
-		for (uint16_t i = 0; i < N; i++){
-
+		uint8_t index = 0;
+		uint16_t applicationID;
+		uint16_t eventDefinitionID;
+		uint8_t flag = 0; // used as boolean 0 is false, 1 is true
+		for (uint16_t i = 0; i < N; i++) {
+			applicationID = message.readEnum16();
+			eventDefinitionID = message.readEnum16();
+			while (eventActionDefinitionArray[index].applicationId != applicationID ||
+			eventActionDefinitionArray[index].eventDefinitionID != eventDefinitionID){
+				if (index == 255){ // 255 should be changed depending on size of the array
+					flag = 1;
+					break;
+				}
+				index++;
+			}
+			if (flag == 0){ // Found
+				eventActionDefinitionArray[index].empty = 1;
+				eventActionDefinitionArray[index].eventDefinitionID = 65535;
+				eventActionDefinitionArray[index].request = "";
+				eventActionDefinitionArray[index].applicationId = 0;
+			}
+			index = 0;
 		}
 
 	}
@@ -37,17 +71,24 @@ void EventActionService::deleteEventActionDefinitions(Message message) {
 void EventActionService::deleteAllEventActionDefinitions(Message message) {
 	// TC[19,3]
 	if (message.messageType == 3 && message.packetType == Message::TC && message.serviceType
-	                                                                     == 19){
-
+	                                                                     == 19) {
+		for (uint16_t index = 0; index < 256; index++) {
+			if (eventActionDefinitionArray[index].empty == 0){
+				eventActionDefinitionArray[index].empty = 1;
+				eventActionDefinitionArray[index].eventDefinitionID = 65535;
+				eventActionDefinitionArray[index].request = "";
+				eventActionDefinitionArray[index].applicationId = 0;
+			}
+		}
 	}
 }
 
 void EventActionService::enableEventActionDefinitions(Message message) {
 	// TC[19,4]
 	if (message.messageType == 4 && message.packetType == Message::TC && message.serviceType
-	                                                                     == 19){
+	                                                                     == 19) {
 		uint16_t N = message.readUint16();
-		for (uint16_t i = 0; i < N; i++){
+		for (uint16_t i = 0; i < N; i++) {
 
 		}
 
@@ -57,9 +98,9 @@ void EventActionService::enableEventActionDefinitions(Message message) {
 void EventActionService::disableEventActionDefinitions(Message message) {
 	// TC[19,5]
 	if (message.messageType == 5 && message.packetType == Message::TC && message.serviceType
-	                                                                     == 19){
+	                                                                     == 19) {
 		uint16_t N = message.readUint16();
-		for (uint16_t i = 0; i < N; i++){
+		for (uint16_t i = 0; i < N; i++) {
 
 		}
 
@@ -69,7 +110,7 @@ void EventActionService::disableEventActionDefinitions(Message message) {
 void EventActionService::requestEventActionDefinitionStatus(Message message) {
 	// TC[19,6]
 	if (message.messageType == 6 && message.packetType == Message::TC && message.serviceType
-	                                                                     == 19){
+	                                                                     == 19) {
 
 	}
 }
@@ -84,7 +125,7 @@ void EventActionService::eventActionStatusReport() {
 void EventActionService::enableEventActionFunction(Message message) {
 	// TC[19,8]
 	if (message.messageType == 8 && message.packetType == Message::TC && message.serviceType
-	                                                                     == 19){
+	                                                                     == 19) {
 		setEventActionFunctionStatus(EventActionFunctionStatus::enabledFunction);
 	}
 }
@@ -92,14 +133,14 @@ void EventActionService::enableEventActionFunction(Message message) {
 void EventActionService::disableEventActionFunction(Message message) {
 	// TC[19,9]
 	if (message.messageType == 9 && message.packetType == Message::TC && message.serviceType
-	                                                                     == 19){
+	                                                                     == 19) {
 		setEventActionFunctionStatus(EventActionFunctionStatus::disabledFunction);
 	}
 }
 
 void EventActionService::executeAction() {
 	// Custom function
-	if (enabledFunction){
+	if (enabledFunction) {
 
 	}
 }
-- 
GitLab