diff --git a/inc/Services/ParameterService.hpp b/inc/Services/ParameterService.hpp index 1c0cbbc3cbb9f03d7aecafc1909ab60b85fd82a2..f7f2a04e73a4fb96520e0ec8992c35e99b20b9da 100644 --- a/inc/Services/ParameterService.hpp +++ b/inc/Services/ParameterService.hpp @@ -47,7 +47,7 @@ public: Message reportParameterIds(Message paramIds); - void setParamData(Message newParamValues); + void setParameterIds(Message newParamValues); }; diff --git a/src/Services/ParameterService.cpp b/src/Services/ParameterService.cpp index e2db44d406ea29eb24f8ed1b858c083b68f3f895..f9febf4d20f30f1b3c8c388665ec0e9c79dcdd6b 100644 --- a/src/Services/ParameterService.cpp +++ b/src/Services/ParameterService.cpp @@ -9,10 +9,12 @@ #endif +uint16_t numOfValidIds(Message idMsg); + ParameterService::ParameterService() { #ifdef DEMOMODE /** - * Initializes the parameter list with some dummy values for now. + * Initializes the parameter list with some dummy values. */ // Test code, setting up some of the parameter fields @@ -36,15 +38,17 @@ Message ParameterService::reportParameterIds(Message paramIds) { /** * This function receives a TC[20, 1] packet and returns a TM[20, 2] packet - * containing the current configuration **for the parameters specified in the carried IDs**. + * containing the current configuration + * **for the parameters specified in the carried valid IDs**. + * * No sophisticated error checking for now, just whether the package is of the correct type - * (in which case it returns an empty message) + * and whether the requested IDs are valid, ignoring the invalid ones. * - * @param paramId: a valid TC[20, 1] packet carrying the requested parameter ID - * @return A TM[20, 2] packet containing the parameter ID + * @param paramId: a valid TC[20, 1] packet carrying the requested parameter IDs + * @return A TM[20, 2] packet containing the valid parameter IDs and their settings. + * @return Empty TM[20, 2] packet on wrong type. * - * @todo Implement binary tree search for the lookup in order to be faster when the number of - * params inevitably rises (NOT URGENT YET) + * @todo Generate failure notifs where needed when ST[06] is ready * * NOTES: * Method for valid ID counting is a hack (clones the message and figures out the number @@ -54,31 +58,12 @@ Message ParameterService::reportParameterIds(Message paramIds) { */ Message reqParam(20, 2, Message::TM, 1); // empty TM[20, 2] parameter report message - Message dummy = paramIds; // dummy clone of the given msg, to figure out the # of valid IDs if (paramIds.packetType == Message::TC && paramIds.serviceType == 20 && paramIds.messageType == 1) { - // FIGURING OUT THE # OF VALID IDs - - uint16_t ids = paramIds.readUint16(); // first 16bits of the packet are # of IDs - uint16_t validIds = 0; - - dummy.readUint16(); // skip the number of IDs in the dummy, we already know it - - for (int i = 0; i < ids; i++) { - - uint16_t currId = dummy.readUint16(); - - if (currId < CONFIGLENGTH) { - - validIds++; - } - } - - // ACTUAL APPENDING STARTS HERE - - reqParam.appendUint16(validIds); // include the number of valid IDs + uint16_t ids = paramIds.readUint16(); + reqParam.appendUint16(numOfValidIds(paramIds)); // include the number of valid IDs for (int i = 0; i < ids; i++) { @@ -92,8 +77,9 @@ Message ParameterService::reportParameterIds(Message paramIds) { } else { // generate failed execution notification + // (depends on execution reporting subservice ST[06]) - continue; //ignore the faulty ID + continue; //ignore the invalid ID } } } @@ -101,32 +87,66 @@ Message ParameterService::reportParameterIds(Message paramIds) { return reqParam; } -void ParameterService::setParamData(Message newParamValues) { +void ParameterService::setParameterIds(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. + * iterates over all contained parameter IDs and replaces the settings for each valid parameter, + * while ignoring all invalid IDs. * * @param newParamValues: a valid TC[20, 3] message carrying parameter ID and replacement value * @return None + * + * @todo Generate failure notifications where needed (eg. when an invalid ID is encountered) * @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) { + newParamValues.messageType == 3) { + + uint16_t ids = newParamValues.readUint16(); //get number of ID's - //TODO: Separate searching from rest of code - for (int i = 0; i < CONFIGLENGTH; i++) { + for (int i = 0; i < ids; i++) { - if (paramsList[i].paramId == reqParamId) { + uint16_t currId = newParamValues.readUint16(); - paramsList[i].settingData = newParamValues.readUint32(); - break; + if (currId < CONFIGLENGTH) { + + paramsList[currId].settingData = newParamValues.readUint32(); + } else { + + // generate failure of execution with ST[06] + continue; // ignore the invalid ID } } } } + +uint16_t numOfValidIds(Message idMsg) { + + idMsg.readPosition = 0; + // start reading from the beginning of the idMsg object + // (original obj. will not be influenced if this is called by value) + + uint16_t ids = idMsg.readUint16(); // first 16bits of the packet are # of IDs + uint16_t validIds = 0; + + for (int i = 0; i < ids; i++) { + + uint16_t currId = idMsg.readUint16(); + + if (idMsg.messageType == 3) { + + idMsg.readUint32(); //skip the 32bit settings blocks, we need only the IDs + } + + if (currId < CONFIGLENGTH) { + + validIds++; + } + + } + + return validIds; + +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1262e909026841c41ab6ebeba7bee6340d4a4edc..f26e97984294431fa1fcd47f459a49eb2edcdbe0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,5 +52,25 @@ int main() { std::cout << std::endl << "(First value is hours, second is minutes)" << std::endl; + //Test code for setParameter + Message sentPacket2 = Message(20, 3, Message::TC, 1); //application id is a dummy number (1) + sentPacket2.appendUint16(2); //number of contained IDs + sentPacket2.appendUint16(0); //first parameter ID + sentPacket2.appendUint32(63238); //settings for first parameter + sentPacket2.appendUint16(1); //2nd parameter ID + sentPacket2.appendUint32(45823); //settings for 2nd parameter + + paramService.setParameterIds(sentPacket2); + returnedPacket = paramService.reportParameterIds(sentPacket); + + numOfIds = returnedPacket.readUint16(); + + for (int i = 0; i < numOfIds; i++) { + + std::cout << "Parameter ID: " << std::dec << returnedPacket.readUint16() << std::endl + << "Parameter value: " << std::dec << returnedPacket.readUint32() << std::endl; + + } + return 0; -} +} \ No newline at end of file