From 4830a79de36f7be0e38c937debf15b1a39811b3b Mon Sep 17 00:00:00 2001
From: Grigoris Pavlakis <grigpavl@ece.auth.gr>
Date: Sun, 11 Aug 2019 19:58:19 +0300
Subject: [PATCH] Convert the Parameter struct to its own class

in order to make construction and adding more natural
---
 inc/Services/Parameter.hpp        | 39 +++++++++++++++++++++++++++++++
 inc/Services/ParameterService.hpp | 19 +--------------
 src/Services/ParameterService.cpp |  7 ++----
 3 files changed, 42 insertions(+), 23 deletions(-)
 create mode 100644 inc/Services/Parameter.hpp

diff --git a/inc/Services/Parameter.hpp b/inc/Services/Parameter.hpp
new file mode 100644
index 00000000..ad2436d5
--- /dev/null
+++ b/inc/Services/Parameter.hpp
@@ -0,0 +1,39 @@
+#ifndef ECSS_SERVICES_PARAMETER_HPP
+#define ECSS_SERVICES_PARAMETER_HPP
+
+#include "etl/bitset.h"
+
+// Number of binary flags in every parameter.
+#define NUM_OF_FLAGS 5
+
+/**
+ * Generic parameter structure
+ * PTC and PFC for each parameter shall be specified as in
+ * ECSS-E-ST-70-41C, chapter 7.3
+ */
+
+typedef uint16_t ParamId;             // parameter IDs are given sequentially
+typedef void(*UpdatePtr)(uint32_t*);  // pointer to the update function of this parameter
+// (argument is a pointer to the variable where the value will be returned, in
+// this case currentValue)
+
+class Parameter {
+	uint8_t ptc = 0;                                        // Packet field type code (PTC)
+	uint8_t pfc = 0;                                        // Packet field format code (PFC)
+	UpdatePtr ptr = nullptr;                                // Function pointer used for updates
+	uint32_t currentValue = 0;                              // Last good value of the parameter
+	etl::bitset<NUM_OF_FLAGS> flags = {false};
+	// Various flags (TBD which. Ideas: update with priority, do not poll, etc.)
+
+	public:
+		Parameter(uint8_t new_ptc, uint8_t new_pfc, UpdatePtr new_ptr) {
+			ptc = new_ptc;
+			pfc = new_pfc;
+			ptr = new_ptr;
+
+			(*ptr)(&currentValue);  // call the update function for the initial value
+		}
+};
+
+
+#endif //ECSS_SERVICES_PARAMETER_HPP
diff --git a/inc/Services/ParameterService.hpp b/inc/Services/ParameterService.hpp
index 4974dbc7..e4093a41 100644
--- a/inc/Services/ParameterService.hpp
+++ b/inc/Services/ParameterService.hpp
@@ -3,14 +3,12 @@
 
 #include "Service.hpp"
 #include "ErrorHandler.hpp"
+#include "Parameter.hpp"
 #include "etl/map.h"
-#include "etl/bitset.h"
 
 // Number of stored parameters. MAX_PARAMS is just a dummy number for now.
 #define MAX_PARAMS 5
 
-// Number of binary flags in every parameter.
-#define NUM_OF_FLAGS 5
 /**
  * Implementation of the ST[20] parameter management service,
  * as defined in ECSS-E-ST-70-41C
@@ -18,21 +16,6 @@
  * @author Grigoris Pavlakis <grigpavl@ece.auth.gr>
  */
 
-/**
- * Generic parameter structure
- * PTC and PFC for each parameter shall be specified as in
- * ECSS-E-ST-70-41C, chapter 7.3
- */
-typedef uint16_t ParamId; // parameter IDs are given sequentially
-struct Parameter {
-	uint8_t ptc;                       // Packet field type code (PTC)
-	uint8_t pfc;                       // Packet field format code (PFC)
-	void (*updatePtr)();               // Pointer to the function used to update the value
-	uint32_t currentValue;             // Last good value of the parameter
-	etl::bitset<NUM_OF_FLAGS> flags;   // Various flags (TBD which).
-	                                   // (Ideas: update with priority, do not poll, etc.)
-};
-
 /**
  * Parameter manager - ST[20]
  * Holds the list with the parameters and provides functions
diff --git a/src/Services/ParameterService.cpp b/src/Services/ParameterService.cpp
index 2bd6f638..a22b730e 100644
--- a/src/Services/ParameterService.cpp
+++ b/src/Services/ParameterService.cpp
@@ -1,12 +1,7 @@
 #include "Services/ParameterService.hpp"
 
-//#define DEMOMODE
-
-//#include <ctime>
-//#include <cstdlib>
 
 ParameterService::ParameterService() {
-//#ifdef DEMOMODE
 //	// Test code, setting up some of the parameter fields
 //
 //	time_t currTime = time(nullptr);
@@ -34,6 +29,8 @@ ParameterService::ParameterService() {
 //#endif
 }
 
+void
+
 void ParameterService::reportParameterIds(Message& paramIds) {
 	paramIds.assertTC(20, 1);
 	Message reqParam(20, 2, Message::TM, 1); // empty TM[20, 2] parameter report message
-- 
GitLab