Skip to content
Snippets Groups Projects
Commit 674a529d authored by Grigoris Pavlakis's avatar Grigoris Pavlakis Committed by kongr45gpen
Browse files

Fix unnecessary code duplication at call()

parent e1c18787
No related branches found
No related tags found
No related merge requests found
...@@ -39,16 +39,16 @@ ...@@ -39,16 +39,16 @@
typedef String<MAXFUNCNAMELENGTH> functionName; typedef String<MAXFUNCNAMELENGTH> functionName;
typedef etl::map<functionName, void(*)(String<MAXARGLENGTH>), FUNCMAPSIZE> typedef etl::map<functionName, void(*)(String<MAXARGLENGTH>), FUNCMAPSIZE>
PointerMap; FunctionMap;
class FunctionManagementService : public Service { 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 FUNCMAPSIZE
*/ */
#ifdef TESTMODE #ifdef TESTMODE
public: PointerMap funcPtrIndex; public: FunctionMap funcPtrIndex;
#else #else
PointerMap funcPtrIndex; FunctionMap funcPtrIndex;
#endif #endif
public: public:
...@@ -62,28 +62,22 @@ public: ...@@ -62,28 +62,22 @@ public:
/** /**
* Calls the function described in the TC[8,1] message *msg*, passing the arguments contained * Calls the function described in the TC[8,1] message *msg*, passing the arguments contained
* and, if non-existent, generates a failed start of execution notification. * and, if non-existent, generates a failed start of execution notification. Returns an unneeded
* int, for testing purposes.
* @param msg A TC[8,1] message * @param msg A TC[8,1] message
*/ */
#ifdef TESTMODE
int call(Message msg); int call(Message msg);
#else
void call(Message msg);
#endif
/** /**
* Includes a new function in the pointer map. This enables it to be called by way of a valid * Includes a new function in the pointer map. This enables it to be called by way of a valid
* TC [8,1] message. * 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.
* *
* @param funcName the function's name. Max. length is MAXFUNCNAMELENGTH bytes. * @param funcName the function's name. Max. length is MAXFUNCNAMELENGTH bytes.
* @param ptr pointer to a function of void return type and a MAXARGLENGTH-lengthed byte * @param ptr pointer to a function of void return type and a MAXARGLENGTH-lengthed byte
* string as argument (which contains the actual arguments of the function) * string as argument (which contains the actual arguments of the function)
*/ */
#ifdef TESTMODE
int include(String<MAXFUNCNAMELENGTH> funcName, void(*ptr)(String<MAXARGLENGTH>)); int include(String<MAXFUNCNAMELENGTH> funcName, void(*ptr)(String<MAXARGLENGTH>));
#else
void include(String<MAXFUNCNAMELENGTH> funcName, void(*ptr)(String<MAXARGLENGTH>));
#endif
}; };
#endif //ECSS_SERVICES_FUNCTIONMANAGEMENTSERVICE_HPP #endif //ECSS_SERVICES_FUNCTIONMANAGEMENTSERVICE_HPP
...@@ -31,7 +31,6 @@ FunctionManagementService::FunctionManagementService() { ...@@ -31,7 +31,6 @@ FunctionManagementService::FunctionManagementService() {
} }
#endif #endif
#ifdef TESTMODE
int FunctionManagementService::call(Message msg){ int FunctionManagementService::call(Message msg){
assert(msg.messageType == 1); assert(msg.messageType == 1);
assert(msg.serviceType == 8); assert(msg.serviceType == 8);
...@@ -51,7 +50,7 @@ int FunctionManagementService::call(Message msg){ ...@@ -51,7 +50,7 @@ int FunctionManagementService::call(Message msg){
// locate the appropriate function pointer // locate the appropriate function pointer
String<MAXFUNCNAMELENGTH> name(funcName); String<MAXFUNCNAMELENGTH> name(funcName);
PointerMap::iterator iter = funcPtrIndex.find(name); FunctionMap::iterator iter = funcPtrIndex.find(name);
void(*selected)(String<MAXARGLENGTH>); void(*selected)(String<MAXARGLENGTH>);
if (iter != funcPtrIndex.end()) { if (iter != funcPtrIndex.end()) {
...@@ -69,7 +68,6 @@ int FunctionManagementService::call(Message msg){ ...@@ -69,7 +68,6 @@ int FunctionManagementService::call(Message msg){
return 0; return 0;
} }
// TEST VERSION OF include()!
int FunctionManagementService::include(String<MAXFUNCNAMELENGTH> funcName, void(*ptr) int FunctionManagementService::include(String<MAXFUNCNAMELENGTH> funcName, void(*ptr)
(String<MAXARGLENGTH>)) { (String<MAXARGLENGTH>)) {
...@@ -85,62 +83,3 @@ int FunctionManagementService::include(String<MAXFUNCNAMELENGTH> funcName, void( ...@@ -85,62 +83,3 @@ int FunctionManagementService::include(String<MAXFUNCNAMELENGTH> funcName, void(
return 0; return 0;
} }
#else
void FunctionManagementService::call(Message msg){
assert(msg.messageType == 1);
assert(msg.serviceType == 8);
uint8_t funcName[MAXFUNCNAMELENGTH]; // the function's name
uint8_t funcArgs[MAXARGLENGTH]; // arguments for the function
msg.readString(funcName, MAXFUNCNAMELENGTH);
msg.readString(funcArgs, MAXARGLENGTH);
if (msg.readPosition < MAXFUNCNAMELENGTH + MAXARGLENGTH) {
/**
* @todo Send failed start of execution (too long message)
*/
return;
}
// locate the appropriate function pointer
String<MAXFUNCNAMELENGTH> name(funcName);
PointerMap::iterator iter = funcPtrIndex.find(name);
void(*selected)(String<MAXARGLENGTH>);
if (iter != funcPtrIndex.end()) {
selected = *iter->second;
}
else {
/**
* @todo Send failed start of execution (function not found)
*/
return;
}
// execute the function if there are no obvious flaws (defined in the standard, pg.158)
selected(funcArgs);
}
void FunctionManagementService::include(String<MAXFUNCNAMELENGTH> funcName,
void (*ptr)(String<MAXARGLENGTH>)) {
if (funcName.length() > MAXFUNCNAMELENGTH) {
/**
* @todo Generate suitable notification (function name exceeds maximum allowed length)
*/
return;
}
else if (funcPtrIndex.full()) {
/**
* @todo Generate suitable notification (index is full)
*/
return;
}
else {
funcName.append(MAXFUNCNAMELENGTH - funcName.length(), '\0');
funcPtrIndex.insert(std::make_pair(funcName, ptr));
}
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment