From aab9e1324004ac7b6492048ae31f7570805ce3d6 Mon Sep 17 00:00:00 2001
From: Grigoris Pavlakis <grigpavl@ece.auth.gr>
Date: Tue, 20 Nov 2018 22:30:29 +0200
Subject: [PATCH] Complete basic implementation of parameter setting, untested

---
 inc/Services/ParameterService.hpp |  4 +-
 src/Services/ParameterService.cpp | 66 +++++++++++++++++++++----------
 src/main.cpp                      |  2 +
 3 files changed, 49 insertions(+), 23 deletions(-)

diff --git a/inc/Services/ParameterService.hpp b/inc/Services/ParameterService.hpp
index f42a8745..65f37e65 100644
--- a/inc/Services/ParameterService.hpp
+++ b/inc/Services/ParameterService.hpp
@@ -45,9 +45,9 @@ class ParameterService : public Service {
 public:
 	ParameterService();
 
-	Message reportParameter(Message paramId);
+	Message reportParameterId(Message paramId);
 
-	//void setParamData(Message& newParamValues);
+	void setParamData(Message newParamValues);
 
 };
 
diff --git a/src/Services/ParameterService.cpp b/src/Services/ParameterService.cpp
index 59bcf278..c5f40d90 100644
--- a/src/Services/ParameterService.cpp
+++ b/src/Services/ParameterService.cpp
@@ -5,6 +5,7 @@
 #ifdef DEMOMODE
 
 #include <ctime>
+#include <cstdlib>
 
 #endif
 
@@ -12,35 +13,32 @@ ParameterService::ParameterService() {
 #ifdef DEMOMODE
 	/**
 	 * Initializes the parameter list with some dummy values for now.
-	 * This normally will be initialized with actual values on boot.
 	 */
 
-	for (int i = 0; i < CONFIGLENGTH; i++) {
-
-		paramsList[i].paramId = 0;
-		paramsList[i].settingData = 0;
-		paramsList[i].pfc = 1;
-		paramsList[i].ptc = 1;
-	}
-
-	// Test code, setting up one of the parameter fields
+	// Test code, setting up some of the parameter fields
 
 	time_t currTime = time(nullptr);
 	struct tm *today = localtime(&currTime);
 
-	paramsList[2].paramId = 341;                   // random parameter ID
-	paramsList[2].settingData = today->tm_min;   // the minute of the current hour
-	paramsList[2].ptc = 3;                         // unsigned int
-	paramsList[2].pfc = 14;                        // 32 bits
+	paramsList[0].paramId = 341;                   // random parameter ID
+	paramsList[0].settingData = today->tm_hour;    // the current hour
+	paramsList[0].ptc = 3;                         // unsigned int
+	paramsList[0].pfc = 14;                        // 32 bits
+
+	paramsList[1].paramId = 345;                   // random parameter ID
+	paramsList[1].settingData = today->tm_min;     // the current minute
+	paramsList[1].ptc = 3;                         // unsigned int
+	paramsList[1].pfc = 14;                        // 32 bits
 #endif
 }
 
-Message ParameterService::reportParameter(Message paramId) {
+Message ParameterService::reportParameterId(Message paramId) {
 
 	/**
 	 * This function receives a TC[20, 1] packet and returns a TM[20, 2] packet
-	 * containing the current configuration. No error checking for now, just whether
-	 * the package is of the correct type (in which case it returns an empty message)
+	 * containing the current configuration **for the parameter specified in the carried ID**.
+	 * No sophisticated error checking for now, just whether the package is of the correct type
+	 * (in which case it returns an empty message)
 	 *
 	 * @param paramId: a valid TC[20, 1] packet carrying the requested parameter ID
 	 * @return A TM[20, 2] packet containing the parameter ID
@@ -57,8 +55,8 @@ Message ParameterService::reportParameter(Message paramId) {
 
 			if (paramsList[i].paramId == reqParamId) {
 
-				reqParam.appendUint16(paramsList[i].paramId);
-				reqParam.appendUint32(paramsList[i].settingData);
+				reqParam.appendUint16(paramsList[i].paramId);  // first 16 bits are the parameter ID
+				reqParam.appendUint32(paramsList[i].settingData); // rest 32 are the current setting
 				break;
 			}
 		}
@@ -67,6 +65,32 @@ Message ParameterService::reportParameter(Message paramId) {
 	return reqParam;
 }
 
-/*void ParameterService::setParamData(Message& newParamValues) {
+void ParameterService::setParamData(Message newParamValues) {
+
+	/**
+	 * This function receives a TC[20, 3] message and after checking whether its type is correct,
+	 * replaces the setting specified in the settingData field of the parameter with the ID
+	 * contained in the message with the value that the message carries. If the message type is
+	 * not correct, the settings stay as they are.
+	 *
+	 * @param newParamValues: a valid TC[20, 3] message carrying parameter ID and replacement value
+	 * @return None
+	 * @todo Use pointers for changing and storing addresses to comply with the standard
+	 */
+
+	uint16_t reqParamId = newParamValues.readUint16();
+
+	if (newParamValues.packetType == Message::TC && newParamValues.serviceType == 20 &&
+		newParamValues.messageType == 1) {
 
-}*/
+		//TODO: Separate searching from rest of code
+		for (int i = 0; i < CONFIGLENGTH; i++) {
+
+			if (paramsList[i].paramId == reqParamId) {
+
+				paramsList[i].settingData = newParamValues.readUint32();
+				break;
+			}
+		}
+	}
+}
diff --git a/src/main.cpp b/src/main.cpp
index 20f3283d..0cb6e00a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -31,6 +31,8 @@ int main() {
 
 	//ST[20] test
 	ParameterService paramService;
+
+	//Test code for reportParameter
 	Message sentPacket = Message(20, 1, Message::TC, 1);  //application id is a dummy number (1)
 	sentPacket.appendUint16(341);  //the packet sent contains the ID of the desired parameter
 	Message returnedPacket = paramService.reportParameter(sentPacket);
-- 
GitLab