diff --git a/inc/ErrorHandler.hpp b/inc/ErrorHandler.hpp index 97732f38de00cfe4011e8e163102fcf24cbbfcbc..5dd1c6fb2064dd31a0ac893dda282493bf69b9a3 100644 --- a/inc/ErrorHandler.hpp +++ b/inc/ErrorHandler.hpp @@ -70,9 +70,13 @@ public: */ OtherMessageType = 9, /** - * Attempt to insert new function in a full function map (ST[08]) + * Attempt to insert new element in a full map (ST[08], ST[20]) */ - FunctionMapFull = 10, + MapFull = 10, + /** + * Attempt to overwrite an existing parameter (ST[20]) + */ + ExistingParameterId = 11 }; /** diff --git a/inc/Services/Parameter.hpp b/inc/Services/Parameter.hpp index 2cec0f88e2795f3b56b24d401d34f80feb88127b..54a40bb1e086a73d755f0c954b73979b662ebb1f 100644 --- a/inc/Services/Parameter.hpp +++ b/inc/Services/Parameter.hpp @@ -66,7 +66,7 @@ class Parameter { Parameter(uint8_t newPtc, uint8_t newPfc, uint32_t initialValue = 0, UpdatePtr newPtr = nullptr); void setCurrentValue(ValueType newVal); - void setFlag(const char* flags); + void setFlags(const char* flags); ValueType getCurrentValue(); uint8_t getPTC(); diff --git a/inc/Services/ParameterService.hpp b/inc/Services/ParameterService.hpp index fddef4f66c85df73965d14cb678052557a5e19c3..3d473103009f0da4bfd90ceb88121edee80e6226 100644 --- a/inc/Services/ParameterService.hpp +++ b/inc/Services/ParameterService.hpp @@ -36,13 +36,14 @@ public: ParameterService(); /** - * @brief Adds a new parameter. Returns false if the parameter has not been added - * (either because the map is full or because it already exists in it). + * @brief Adds a new parameter. Emits an InternalError::MapFull if an attempt is made to insert + * parameters in a full map or an InternalError::ExistingParameterId if the given parameter ID + * exists already. * @param id: the desired ID for this parameter * @param param: the parameter field to be included * @param flags: the flags to be set for this field (see Parameter.hpp) */ - bool addNewParameter(uint16_t id, Parameter param, const char* flags = "110"); + void addNewParameter(uint16_t id, Parameter param, const char* flags = "110"); /** * This function receives a TC[20, 1] packet and returns a TM[20, 2] packet diff --git a/src/Services/FunctionManagementService.cpp b/src/Services/FunctionManagementService.cpp index c3831872f501acff1be628955cfb262499df2b62..b6d5638b6efd990136f8173beced1618be85f247 100644 --- a/src/Services/FunctionManagementService.cpp +++ b/src/Services/FunctionManagementService.cpp @@ -42,7 +42,7 @@ void FunctionManagementService::include(String<FUNC_NAME_LENGTH> funcName, void funcName.append(FUNC_NAME_LENGTH - funcName.length(), 0); funcPtrIndex.insert(std::make_pair(funcName, ptr)); } else { - ErrorHandler::reportInternalError(ErrorHandler::InternalErrorType::FunctionMapFull); + ErrorHandler::reportInternalError(ErrorHandler::InternalErrorType::MapFull); } } diff --git a/src/Services/Parameter.cpp b/src/Services/Parameter.cpp index fae8917119ab250cfcb7a4d4545fc122a4bb9d0e..2ab9227164583af68f9a4ead572dd6f7e06cc429 100644 --- a/src/Services/Parameter.cpp +++ b/src/Services/Parameter.cpp @@ -34,6 +34,6 @@ uint8_t Parameter::getPFC() { return pfc; } -void Parameter::setFlag(const char* flags) { +void Parameter::setFlags(const char* flags) { this->flags = Flags(flags); } diff --git a/src/Services/ParameterService.cpp b/src/Services/ParameterService.cpp index 9058e0467ee52ce4fdc6ea36ecbe00c90b23ccf5..2ac5d101a779f2090490fd78d18141bc5bfb9655 100644 --- a/src/Services/ParameterService.cpp +++ b/src/Services/ParameterService.cpp @@ -7,24 +7,21 @@ ParameterService::ParameterService() { // addNewParameter(3, 14); } -bool ParameterService::addNewParameter(uint16_t id, Parameter param, const char* flags) { - try { - try { - paramsList.at(id); - return false; - // if it exists, an iterator will be returned with no exception thrown, - // so the function should return false - // todo: is it a better idea to use find() and if instead of an exception here? - } - catch (etl::map_out_of_bounds& mapOutOfBounds) { - param.setFlag(flags); +void ParameterService::addNewParameter(uint16_t id, Parameter param, const char* flags) { + if (paramsList.full()) { + ErrorHandler::reportInternalError(ErrorHandler::InternalErrorType::MapFull); + return; + } + else { + if (paramsList.find(id) == paramsList.end()) { + param.setFlags(flags); paramsList.insert(std::make_pair(id, param)); - return true; + return; + } + else { + ErrorHandler::reportInternalError(ErrorHandler::InternalErrorType::ExistingParameterId); + return; } - } - catch (etl::map_full &mapFull) { - return false; - // if the map is full, return false } } @@ -51,13 +48,14 @@ void ParameterService::reportParameterIds(Message& paramIds) { for (uint16_t i = 0; i < numOfIds; i++) { uint16_t currId = paramIds.readUint16(); - try { + + if (paramsList.find(currId) != paramsList.end()) { std::pair<uint16_t, ValueType> p = std::make_pair(currId, paramsList.at(currId).getCurrentValue()); // pair containing the parameter's ID as first element and its current value as second validParams.push_back(p); validIds++; } - catch (etl::map_out_of_bounds &mapOutOfBounds) { + else { ErrorHandler::reportError(paramIds, ErrorHandler::ExecutionStartErrorType::UnknownExecutionStartError); continue; // generate failed start of execution notification & ignore } @@ -89,10 +87,10 @@ void ParameterService::setParameterIds(Message& newParamValues) { for (uint16_t i = 0; i < numOfIds; i++) { uint16_t currId = newParamValues.readUint16(); // the parameter is checked for read-only status and manual update availability - try { + if (paramsList.find(currId) != paramsList.end()) { paramsList.at(currId).setCurrentValue(newParamValues.readUint32()); } - catch (etl::map_out_of_bounds &mapOutOfBounds) { + else { ErrorHandler::reportError(newParamValues, ErrorHandler::ExecutionStartErrorType::UnknownExecutionStartError); continue; // generate failed start of execution notification & ignore diff --git a/test/Services/FunctionManagementService.cpp b/test/Services/FunctionManagementService.cpp index 45088a8670351de5ca7efbe02057b739714565b8..64683103f21ee59c80db329077d148ca6e335266 100644 --- a/test/Services/FunctionManagementService.cpp +++ b/test/Services/FunctionManagementService.cpp @@ -71,6 +71,6 @@ TEST_CASE("ST[08] - Insert Tests") { name += std::to_string(i); // different names to fill up the map fms.include(String<FUNC_NAME_LENGTH>(name.c_str()), &test); } - CHECK(ServiceTests::thrownError(ErrorHandler::InternalErrorType::FunctionMapFull)); + CHECK(ServiceTests::thrownError(ErrorHandler::InternalErrorType::MapFull)); } } diff --git a/test/Services/ParameterService.cpp b/test/Services/ParameterService.cpp index c7b3c267535383768cf33726a13c601150731193..ee678cd5a5998f3a256e9283fb0f2362a14bbee8 100644 --- a/test/Services/ParameterService.cpp +++ b/test/Services/ParameterService.cpp @@ -26,7 +26,9 @@ TEST_CASE("Parameter Service - General") { pserv.addNewParameter(3, param3); pserv.addNewParameter(4, param4); - REQUIRE_FALSE(pserv.addNewParameter(5, param5)); // addNewParameter should return false + pserv.addNewParameter(5, param5); // addNewParameter should return false + CHECK(ServiceTests::thrownError(ErrorHandler::InternalErrorType::MapFull)); + ServiceTests::reset(); Services.reset(); // reset all services } @@ -34,12 +36,11 @@ TEST_CASE("Parameter Service - General") { Parameter param0 = Parameter(1, 3); pserv.addNewParameter(0, param0); - CHECK_FALSE(pserv.addNewParameter(0, param0)); + pserv.addNewParameter(0, param0); + CHECK(ServiceTests::thrownError(ErrorHandler::InternalErrorType::ExistingParameterId)); + ServiceTests::reset(); Services.reset(); } - - //SECTION("Passing of null-pointer as update function on construction") - } TEST_CASE("Parameter Report Subservice") {