getWorker: if a worker function is registered, return its address, nullptr otherwise
| 13 | |
| 14 | // getWorker: if a worker function is registered, return its address, nullptr otherwise |
| 15 | MBSworker ModbusServer::getWorker(uint8_t serverID, uint8_t functionCode) { |
| 16 | bool serverFound = false; |
| 17 | // Search the FC map associated with the serverID |
| 18 | auto svmap = workerMap.find(serverID); |
| 19 | // Is there one? |
| 20 | if (svmap != workerMap.end()) { |
| 21 | serverFound = true; |
| 22 | // No explicit serverID entry found, but we may have one for ANY_SERVER |
| 23 | } else { |
| 24 | svmap = workerMap.find(ANY_SERVER); |
| 25 | if (svmap != workerMap.end()) { |
| 26 | serverFound = true; |
| 27 | } |
| 28 | } |
| 29 | // Did we find a serverID? |
| 30 | if (serverFound) { |
| 31 | // Yes. Now look for the function code in the inner map |
| 32 | bool functionCodeFound = false; |
| 33 | auto fcmap = svmap->second.find(functionCode);; |
| 34 | // Found it? |
| 35 | if (fcmap != svmap->second.end()) { |
| 36 | // Yes. Return the function pointer for it. |
| 37 | functionCodeFound = true; |
| 38 | // No, no explicit worker found, but may be there is one for ANY_FUNCTION_CODE? |
| 39 | } else { |
| 40 | fcmap = svmap->second.find(ANY_FUNCTION_CODE);; |
| 41 | // Found it? |
| 42 | if (fcmap != svmap->second.end()) { |
| 43 | // Yes. Return the function pointer for it. |
| 44 | functionCodeFound = true; |
| 45 | } |
| 46 | } |
| 47 | if (functionCodeFound) { |
| 48 | // Yes. Return the function pointer for it. |
| 49 | return fcmap->second; |
| 50 | } |
| 51 | } |
| 52 | // No matching function pointer found |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | // unregisterWorker; remove again all or part of the registered workers for a given server ID |
| 57 | // Returns true if the worker was found and removed |