From 48425723f4daad198afed88d3dc5df5edd14bd7c Mon Sep 17 00:00:00 2001 From: Grigoris Pavlakis <grigpavl@ece.auth.gr> Date: Tue, 29 Jan 2019 17:31:45 +0200 Subject: [PATCH] Rework reportParameterIds() to use a map instead of an array in order to fix the bug with the uninitialized structs and make the code more robust and readable --- inc/Services/ParameterService.hpp | 12 ++++++---- src/Services/ParameterService.cpp | 40 +++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/inc/Services/ParameterService.hpp b/inc/Services/ParameterService.hpp index f78910e6..8a10dd92 100644 --- a/inc/Services/ParameterService.hpp +++ b/inc/Services/ParameterService.hpp @@ -2,6 +2,7 @@ #define ECSS_SERVICES_PARAMETERSERVICE_HPP #include "Service.hpp" +#include "etl/map.h" #define CONFIGLENGTH 5 @@ -17,10 +18,11 @@ * 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) - uint16_t paramId; // Unique ID of the parameter uint32_t settingData; // Actual data defining the operation of a peripheral or subsystem. @@ -33,13 +35,15 @@ struct Parameter { * Holds the list with the parameters and provides functions * for parameter reporting and modification. * - * @todo Ensure that the parameter list is sorted by ID + * The parameter list is stored in a map with the parameter IDs as keys and values + * corresponding Parameter structs containing the PTC, PFC and the parameter's value. */ + class ParameterService : public Service { private: - Parameter paramsList[CONFIGLENGTH]; - // CONFIGLENGTH is just a dummy number for now, this should be statically set + + etl::map<ParamId, Parameter, CONFIGLENGTH> paramsList; static uint16_t numOfValidIds(Message idMsg); //count the valid ids in a given TC[20, 1] public: diff --git a/src/Services/ParameterService.cpp b/src/Services/ParameterService.cpp index 55a1060d..9186bcd6 100644 --- a/src/Services/ParameterService.cpp +++ b/src/Services/ParameterService.cpp @@ -16,15 +16,25 @@ ParameterService::ParameterService() { time_t currTime = time(nullptr); struct tm *today = localtime(&currTime); - paramsList[0].paramId = 0; // 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 = 1; // random parameter ID - paramsList[1].settingData = today->tm_min; // the current minute - paramsList[1].ptc = 3; // unsigned int - paramsList[1].pfc = 14; // 32 bits + Parameter test1, test2; + + test1.settingData = today->tm_hour; // the current hour + test1.ptc = 3; // unsigned int + test1.pfc = 14; // 32 bits + + test2.settingData = today->tm_min; // the current minute + test2.ptc = 3; // unsigned int + test2.pfc = 14; // 32 bits + + // MAKE SURE THE IDS ARE UNIQUE WHEN INSERTING! + /** + * @todo: Make a separate insert() function for parameter insertion to protect from blunders + * if needed to + */ + + paramsList.insert(std::make_pair(0, test1)); + paramsList.insert(std::make_pair(1, test2)); + #endif } @@ -39,11 +49,17 @@ void ParameterService::reportParameterIds(Message paramIds) { for (int i = 0; i < ids; i++) { uint16_t currId = paramIds.readUint16(); // current ID to be appended - if (currId < CONFIGLENGTH) { // (crappy) check to prevent out-of-bounds access due to - // invalid id, see numOfValidIds + etl::map<ParamId, Parameter, CONFIGLENGTH>::iterator iter = paramsList.find(currId); + if (iter != paramsList.end()) { reqParam.appendUint16(currId); reqParam.appendUint32(paramsList[currId].settingData); - } else { + } +// if (currId < CONFIGLENGTH) { // (crappy) check to prevent out-of-bounds access due to +// // invalid id, see numOfValidIds +// reqParam.appendUint16(currId); +// reqParam.appendUint32(paramsList[currId].settingData); +// } + else { // generate failure of execution notification for ST[06] continue; //ignore the invalid ID } -- GitLab