From ea253b39fd6bcb2157f0131f2ac4a43c171f2bc6 Mon Sep 17 00:00:00 2001 From: Grigoris Pavlakis <grigpavl@ece.auth.gr> Date: Sat, 30 Mar 2019 23:52:22 +0200 Subject: [PATCH] Implement (most of) the TODOS. Serious bug spotted, do not merge yet. --- inc/ErrorHandler.hpp | 6 ++- inc/ServicePool.hpp | 2 + inc/Services/FunctionManagementService.hpp | 30 ++++-------- src/Services/FunctionManagementService.cpp | 40 +++++++--------- test/Services/FunctionManagementService.cpp | 53 ++++++++++----------- 5 files changed, 57 insertions(+), 74 deletions(-) diff --git a/inc/ErrorHandler.hpp b/inc/ErrorHandler.hpp index 9864f708..75b116b5 100644 --- a/inc/ErrorHandler.hpp +++ b/inc/ErrorHandler.hpp @@ -69,7 +69,11 @@ public: /** * A function received a Message that was not of the correct type */ - OtherMessageType = 9, + OtherMessageType = 9, + /** + * Attempt to insert new function in a full function map (ST[08]) + */ + FunctionMapFull = 10, }; /** diff --git a/inc/ServicePool.hpp b/inc/ServicePool.hpp index 8a1e6035..d928606d 100644 --- a/inc/ServicePool.hpp +++ b/inc/ServicePool.hpp @@ -8,6 +8,7 @@ #include "Services/ParameterService.hpp" #include "Services/TestService.hpp" #include "Services/MemoryManagementService.hpp" +#include "Services/FunctionManagementService.hpp" /** * Defines a class that contains instances of all Services. @@ -25,6 +26,7 @@ public: EventActionService eventAction; TestService testService; ParameterService parameterManagement; + FunctionManagementService functionManagement; /** * The default ServicePool constructor diff --git a/inc/Services/FunctionManagementService.hpp b/inc/Services/FunctionManagementService.hpp index 8fb7bce5..2267f7ea 100644 --- a/inc/Services/FunctionManagementService.hpp +++ b/inc/Services/FunctionManagementService.hpp @@ -1,26 +1,16 @@ #ifndef ECSS_SERVICES_FUNCTIONMANAGEMENTSERVICE_HPP #define ECSS_SERVICES_FUNCTIONMANAGEMENTSERVICE_HPP -#include <cstdint> -#include <iostream> // USED BY THE SAMPLE FUNCTIONS ONLY!! -#include <utility> -#include <typeinfo> - #include "etl/map.h" #include "etl/String.hpp" #include "Message.hpp" #include "Service.hpp" +#include "ErrorHandler.hpp" -#define FUNC_MAP_SIZE 128 // size of the function map (number of elements) +#define FUNC_MAP_SIZE 5 // size of the function map (number of elements) #define FUNC_NAME_LENGTH 32 // max length of the function name #define MAX_ARG_LENGTH 32 // maximum argument byte string length -/** - * @todo: Undef TESTMODE before flight!!!! - * @todo: Replace this with error handler code checking - */ -#define TESTMODE // REMOVE BEFORE FLIGHT!(used by Catch to gain visibility @ test) - /** * Implementation of the ST[08] function management service * @@ -67,13 +57,10 @@ FunctionMap; class FunctionManagementService : public Service { /** - * Map of the function names to their respective pointers. Size controlled by FUNCMAPSIZE + * Map of the function names to their respective pointers. Size controlled by FUNC_MAP_SIZE */ -#ifdef TESTMODE -public: FunctionMap funcPtrIndex; -#else FunctionMap funcPtrIndex; -#endif + public: /** @@ -90,18 +77,19 @@ public: * int, for testing purposes. * @param msg A TC[8,1] message */ - int call(Message& msg); + void call(Message& msg); /** * Includes a new function in the pointer map. This enables it to be called by way of a valid - * TC [8,1] message. After inclusion it returns an unneeded int signalling insertion success - * (0) or failure (2). These returns are there for testing purposes only. + * TC [8,1] message. * * @param funcName the function's name. Max. length is FUNC_NAME_LENGTH bytes. * @param ptr pointer to a function of void return type and a MAX_ARG_LENGTH-lengthed byte * string as argument (which contains the actual arguments of the function) */ - int include(String<FUNC_NAME_LENGTH> funcName, void(*ptr)(String<MAX_ARG_LENGTH>)); + void include(String<FUNC_NAME_LENGTH> funcName, void(*ptr)(String<MAX_ARG_LENGTH>)); + + int getMapSize() { return funcPtrIndex.size(); } }; #endif //ECSS_SERVICES_FUNCTIONMANAGEMENTSERVICE_HPP diff --git a/src/Services/FunctionManagementService.cpp b/src/Services/FunctionManagementService.cpp index 88e91221..7ad300f6 100644 --- a/src/Services/FunctionManagementService.cpp +++ b/src/Services/FunctionManagementService.cpp @@ -1,13 +1,13 @@ #include "Services/FunctionManagementService.hpp" -int FunctionManagementService::call(Message& msg) { - /** - * @todo: Add test for message and service type using the ErrorHandler - * @todo: Convert all functions to void (use error reports for tests instead of return numbers) - */ +void FunctionManagementService::call(Message& msg) { msg.resetRead(); - ErrorHandler::assertInternal(msg.messageType == 1 && msg.serviceType == 8, - ErrorHandler::InternalErrorType::UnacceptablePacket); + ErrorHandler::assertRequest(msg.packetType == Message::TC, msg, + ErrorHandler::AcceptanceErrorType::UnacceptableMessage); + ErrorHandler::assertRequest(msg.messageType == 1, msg, + ErrorHandler::AcceptanceErrorType::UnacceptableMessage); + ErrorHandler::assertRequest(msg.serviceType == 8, msg, + ErrorHandler::AcceptanceErrorType::UnacceptableMessage); uint8_t funcName[FUNC_NAME_LENGTH]; // the function's name uint8_t funcArgs[MAX_ARG_LENGTH]; // arguments for the function @@ -16,10 +16,10 @@ int FunctionManagementService::call(Message& msg) { msg.readString(funcArgs, MAX_ARG_LENGTH); if (msg.dataSize > FUNC_NAME_LENGTH + MAX_ARG_LENGTH) { - /** - * @todo Send failed start of execution (too long message) - */ - return 4; // arbitrary + ErrorHandler::reportError(msg, + ErrorHandler::ExecutionStartErrorType::UnknownExecutionStartError); // report failed + // start of execution as requested by the standard + return; } // locate the appropriate function pointer @@ -30,29 +30,23 @@ int FunctionManagementService::call(Message& msg) { if (iter != funcPtrIndex.end()) { selected = *iter->second; } else { - /** - * @todo Send failed start of execution (function not found) - */ - return 1; // arbitrary + ErrorHandler::reportError(msg, + ErrorHandler::ExecutionStartErrorType::UnknownExecutionStartError); + return; } // execute the function if there are no obvious flaws (defined in the standard, pg.158) selected(funcArgs); - return 0; } -int FunctionManagementService::include(String<FUNC_NAME_LENGTH> funcName, void(*ptr) +void FunctionManagementService::include(String<FUNC_NAME_LENGTH> funcName, void(*ptr) (String<MAX_ARG_LENGTH>)) { if (funcPtrIndex.full()) { - /** - * @todo Generate suitable notification (index is full) - */ - return 2; // arbitrary, for testing purposes + ErrorHandler::reportInternalError(ErrorHandler::InternalErrorType::FunctionMapFull); + return; } funcName.append(FUNC_NAME_LENGTH - funcName.length(), '\0'); funcPtrIndex.insert(std::make_pair(funcName, ptr)); - - return 0; } diff --git a/test/Services/FunctionManagementService.cpp b/test/Services/FunctionManagementService.cpp index 70b8ee78..5b2310b7 100644 --- a/test/Services/FunctionManagementService.cpp +++ b/test/Services/FunctionManagementService.cpp @@ -1,53 +1,48 @@ #include "catch2/catch.hpp" #include "Services/FunctionManagementService.hpp" +#include "ServicePool.hpp" +#include "ServiceTests.hpp" +#include <iostream> + +FunctionManagementService & fms = Services.functionManagement; void test(String<MAX_ARG_LENGTH> a) { std::cout << a.c_str() << std::endl; } TEST_CASE("ST[08] - Call Tests") { - FunctionManagementService fms; SECTION("Malformed name") { + ServiceTests::reset(); fms.include(String<FUNC_NAME_LENGTH>("test"), &test); Message msg(8, 1, Message::TC, 1); msg.appendString(String<FUNC_NAME_LENGTH>("t3st")); - CHECK(fms.call(msg) == 1); + fms.call(msg); + CHECK(ServiceTests::get(0).messageType == 4); + CHECK(ServiceTests::get(0).serviceType == 1); } SECTION("Too long message") { + ServiceTests::reset(); fms.include(String<FUNC_NAME_LENGTH>("test"), &test); Message msg(8, 1, Message::TC, 1); msg.appendString(String<FUNC_NAME_LENGTH>("test")); msg.appendString(String<65> ("eqrhjweghjhwqgthjkrghthjkdsfhgsdfhjsdjsfdhgkjdfsghfjdgkdfsgdfgsgd")); - CHECK(fms.call(msg) == 4); - } -} - -TEST_CASE("ST[08] - Insert Tests") { - - SECTION("Insertion to full pointer map") { - FunctionManagementService fms; - std::string name; // FOR TESTING ONLY! - - // make sure the pointer map is full to the brim - for (int i = 0; i < 15000; i++) { - name = "test" + i; - String<FUNC_NAME_LENGTH> funcName(name.c_str()); - - if (not fms.funcPtrIndex.full()) { - fms.include(funcName, &test); - } - else { - break; - } - } - - CHECK(fms.include(String<FUNC_NAME_LENGTH>("testall"), &test) == 2); + fms.call(msg); + CHECK(ServiceTests::get(0).messageType == 4); + CHECK(ServiceTests::get(0).serviceType == 1); } } -/** - * @todo Add more tests - */ +// WARNING! include() is malfunctioning - do not merge! + +//TEST_CASE("ST[08] - Insert Tests") { +// +// SECTION("Insertion to full pointer map") { +// // make sure the pointer map is full to the brim +// ServiceTests::reset(); +// std::string name = "test"; // FOR TESTING ONLY! +// +// } +//} -- GitLab