DHB: replyLen in replyMap now

both maps are closer together now as well
This commit is contained in:
Robin Müller 2020-04-19 15:01:27 +02:00
parent 74b8c3eef4
commit eacedf7ed6
2 changed files with 52 additions and 45 deletions

View File

@ -334,37 +334,35 @@ ReturnValue_t DeviceHandlerBase::isModeCombinationValid(Mode_t mode,
} }
} }
ReturnValue_t DeviceHandlerBase::insertInCommandAndReplyMap( ReturnValue_t DeviceHandlerBase::insertInCommandAndReplyMap(DeviceCommandId_t deviceCommand,
DeviceCommandId_t deviceCommand, uint16_t maxDelayCycles, uint16_t maxDelayCycles, size_t replyLen, uint8_t periodic,
uint8_t periodic, bool hasDifferentReplyId, DeviceCommandId_t replyId) { bool hasDifferentReplyId, DeviceCommandId_t replyId) {
//No need to check, as we may try to insert multiple times. //No need to check, as we may try to insert multiple times.
insertInCommandMap(deviceCommand); insertInCommandMap(deviceCommand);
if (hasDifferentReplyId) { if (hasDifferentReplyId) {
return insertInReplyMap(replyId, maxDelayCycles, periodic); return insertInReplyMap(replyId, maxDelayCycles, replyLen, periodic);
} else { } else {
return insertInReplyMap(deviceCommand, maxDelayCycles, periodic); return insertInReplyMap(deviceCommand, maxDelayCycles, replyLen, periodic);
} }
} }
ReturnValue_t DeviceHandlerBase::insertInReplyMap(DeviceCommandId_t replyId, ReturnValue_t DeviceHandlerBase::insertInReplyMap(DeviceCommandId_t replyId,
uint16_t maxDelayCycles, uint8_t periodic) { uint16_t maxDelayCycles, size_t replyLen, uint8_t periodic) {
DeviceReplyInfo info; DeviceReplyInfo info;
info.maxDelayCycles = maxDelayCycles; info.maxDelayCycles = maxDelayCycles;
info.periodic = periodic; info.periodic = periodic;
info.delayCycles = 0; info.delayCycles = 0;
info.replyLen = replyLen;
info.command = deviceCommandMap.end(); info.command = deviceCommandMap.end();
std::pair<std::map<DeviceCommandId_t, DeviceReplyInfo>::iterator, bool> returnValue; std::pair<DeviceReplyIter, bool> result = deviceReplyMap.emplace(replyId, info);
returnValue = deviceReplyMap.insert( if (result.second) {
std::pair<DeviceCommandId_t, DeviceReplyInfo>(replyId, info));
if (returnValue.second) {
return RETURN_OK; return RETURN_OK;
} else { } else {
return RETURN_FAILED; return RETURN_FAILED;
} }
} }
ReturnValue_t DeviceHandlerBase::insertInCommandMap( ReturnValue_t DeviceHandlerBase::insertInCommandMap(DeviceCommandId_t deviceCommand) {
DeviceCommandId_t deviceCommand) {
DeviceCommandInfo info; DeviceCommandInfo info;
info.expectedReplies = 0; info.expectedReplies = 0;
info.isExecuting = false; info.isExecuting = false;
@ -380,9 +378,8 @@ ReturnValue_t DeviceHandlerBase::insertInCommandMap(
} }
} }
ReturnValue_t DeviceHandlerBase::updateReplyMapEntry( ReturnValue_t DeviceHandlerBase::updateReplyMapEntry(DeviceCommandId_t deviceReply,
DeviceCommandId_t deviceReply, uint16_t delayCycles, uint16_t delayCycles, uint16_t maxDelayCycles, uint8_t periodic) {
uint16_t maxDelayCycles, uint8_t periodic) {
std::map<DeviceCommandId_t, DeviceReplyInfo>::iterator iter = std::map<DeviceCommandId_t, DeviceReplyInfo>::iterator iter =
deviceReplyMap.find(deviceReply); deviceReplyMap.find(deviceReply);
if (iter == deviceReplyMap.end()) { if (iter == deviceReplyMap.end()) {
@ -551,7 +548,17 @@ void DeviceHandlerBase::doGetWrite() {
void DeviceHandlerBase::doSendRead() { void DeviceHandlerBase::doSendRead() {
ReturnValue_t result; ReturnValue_t result;
size_t requestLen = 0;
DeviceReplyIter iter = deviceReplyMap.find(cookieInfo.pendingCommand->first);
if(iter != deviceReplyMap.end()) {
requestLen = iter->second.replyLen;
}
else {
requestLen = 0;
}
result = communicationInterface->requestReceiveMessage(cookie); result = communicationInterface->requestReceiveMessage(cookie);
if (result == RETURN_OK) { if (result == RETURN_OK) {
cookieInfo.state = COOKIE_READ_SENT; cookieInfo.state = COOKIE_READ_SENT;
} else { } else {

View File

@ -557,6 +557,29 @@ protected:
*/ */
Cookie *cookie; Cookie *cookie;
struct DeviceCommandInfo {
bool isExecuting; //!< Indicates if the command is already executing.
uint8_t expectedReplies; //!< Dynamic value to indicate how many replies are expected. Inititated with 0.
MessageQueueId_t sendReplyTo; //!< if this is != NO_COMMANDER, DHB was commanded externally and shall report everything to commander.
};
using DeviceCommandMap = std::map<DeviceCommandId_t, DeviceCommandInfo> ;
/**
* @brief Information about expected replies
*
* This is used to keep track of pending replies
*/
struct DeviceReplyInfo {
uint16_t maxDelayCycles; //!< The maximum number of cycles the handler should wait for a reply to this command.
uint16_t delayCycles; //!< The currently remaining cycles the handler should wait for a reply, 0 means there is no reply expected
size_t replyLen = 0; //!< Expected size of the reply.
uint8_t periodic; //!< if this is !=0, the delayCycles will not be reset to 0 but to maxDelayCycles
DeviceCommandMap::iterator command; //!< The command that expects this reply.
};
using DeviceReplyMap = std::map<DeviceCommandId_t, DeviceReplyInfo> ;
using DeviceReplyIter = DeviceReplyMap::iterator;
/** /**
* The MessageQueue used to receive device handler commands and to send replies. * The MessageQueue used to receive device handler commands and to send replies.
*/ */
@ -736,7 +759,7 @@ protected:
* @return RETURN_OK when the command was successfully inserted, COMMAND_MAP_ERROR else. * @return RETURN_OK when the command was successfully inserted, COMMAND_MAP_ERROR else.
*/ */
ReturnValue_t insertInCommandAndReplyMap(DeviceCommandId_t deviceCommand, ReturnValue_t insertInCommandAndReplyMap(DeviceCommandId_t deviceCommand,
uint16_t maxDelayCycles, uint8_t periodic = 0, uint16_t maxDelayCycles, size_t replyLen = 0, uint8_t periodic = 0,
bool hasDifferentReplyId = false, DeviceCommandId_t replyId = 0); bool hasDifferentReplyId = false, DeviceCommandId_t replyId = 0);
/** /**
* This is a helper method to insert replies in the reply map. * This is a helper method to insert replies in the reply map.
@ -747,7 +770,7 @@ protected:
* @return RETURN_OK when the command was successfully inserted, COMMAND_MAP_ERROR else. * @return RETURN_OK when the command was successfully inserted, COMMAND_MAP_ERROR else.
*/ */
ReturnValue_t insertInReplyMap(DeviceCommandId_t deviceCommand, ReturnValue_t insertInReplyMap(DeviceCommandId_t deviceCommand,
uint16_t maxDelayCycles, uint8_t periodic = 0); uint16_t maxDelayCycles, size_t replyLen = 0, uint8_t periodic = 0);
/** /**
* A simple command to add a command to the commandList. * A simple command to add a command to the commandList.
* @param deviceCommand The command to add * @param deviceCommand The command to add
@ -802,13 +825,6 @@ protected:
*/ */
virtual void modeChanged(void); virtual void modeChanged(void);
struct DeviceCommandInfo {
bool isExecuting; //!< Indicates if the command is already executing.
uint8_t expectedReplies; //!< Dynamic value to indicate how many replies are expected.
MessageQueueId_t sendReplyTo; //!< if this is != NO_COMMANDER, DHB was commanded externally and shall report everything to commander.
};
typedef std::map<DeviceCommandId_t, DeviceCommandInfo> DeviceCommandMap;
/** /**
* Enable the reply checking for a command * Enable the reply checking for a command
* *
@ -819,16 +835,16 @@ protected:
* When found, copies maxDelayCycles to delayCycles in the reply information and sets the command to * When found, copies maxDelayCycles to delayCycles in the reply information and sets the command to
* expect one reply. * expect one reply.
* *
* Can be overwritten by the child, if a command activates multiple replies or replyId differs from * Can be overwritten by the child, if a command activates multiple replies
* commandId. * or replyId differs from commandId.
* Notes for child implementations: * Notes for child implementations:
* - If the command was not found in the reply map, NO_REPLY_EXPECTED MUST be returned. * - If the command was not found in the reply map, NO_REPLY_EXPECTED MUST be returned.
* - A failure code may be returned if something went fundamentally wrong. * - A failure code may be returned if something went fundamentally wrong.
* *
* @param deviceCommand * @param deviceCommand
* @return - RETURN_OK if a reply was activated. * @return - RETURN_OK if a reply was activated.
* - NO_REPLY_EXPECTED if there was no reply found. This is not an error case as many commands * - NO_REPLY_EXPECTED if there was no reply found. This is not an
* do not expect a reply. * error case as many commands do not expect a reply.
*/ */
virtual ReturnValue_t enableReplyInReplyMap(DeviceCommandMap::iterator cmd, virtual ReturnValue_t enableReplyInReplyMap(DeviceCommandMap::iterator cmd,
uint8_t expectedReplies = 1, bool useAlternateId = false, uint8_t expectedReplies = 1, bool useAlternateId = false,
@ -927,22 +943,6 @@ protected:
bool commandIsExecuting(DeviceCommandId_t commandId); bool commandIsExecuting(DeviceCommandId_t commandId);
/**
* Information about expected replies
*
* This is used to keep track of pending replies
*/
struct DeviceReplyInfo {
uint16_t maxDelayCycles; //!< The maximum number of cycles the handler should wait for a reply to this command.
uint16_t delayCycles; //!< The currently remaining cycles the handler should wait for a reply, 0 means there is no reply expected
uint8_t periodic; //!< if this is !=0, the delayCycles will not be reset to 0 but to maxDelayCycles
DeviceCommandMap::iterator command; //!< The command that expects this reply.
};
/**
* Definition for the important reply Map.
*/
typedef std::map<DeviceCommandId_t, DeviceReplyInfo> DeviceReplyMap;
/** /**
* This map is used to check and track correct reception of all replies. * This map is used to check and track correct reception of all replies.
* *