Skip to content

Commit

Permalink
Fixed name of control channel payload.
Browse files Browse the repository at this point in the history
Ensure compatibility by removing non-trivial assignements
  • Loading branch information
gabryelreyes committed Nov 22, 2023
1 parent f144226 commit 4188420
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/SerialMuxProtCommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ SOFTWARE.
#define CONTROL_CHANNEL_NUMBER (0U)

/** DLC of Control Channel Payload. */
#define CONTROL_CHANNEL_PAYLOAD_LENGTH (sizeof(Command))
#define CONTROL_CHANNEL_PAYLOAD_LENGTH (sizeof(ControlChannelPayload))

/** Period of Heartbeat when Synced. */
#define HEATBEAT_PERIOD_SYNCED (5000U)
Expand Down Expand Up @@ -168,15 +168,15 @@ enum COMMANDS : uint8_t
};

/**
* Command Channel Payload Structure.
* Control Channel Payload Structure.
*/
typedef struct _Command
typedef struct _ControlChannelPayload
{
uint8_t commandByte; /**< Command Byte */
uint32_t timestamp; /**< Timestamp */
uint8_t channelNumber; /**< Channel Number */
char channelName[CHANNEL_NAME_MAX_LEN]; /**< Channel Name */
} __attribute__((packed)) Command; /**< Command */
uint8_t commandByte; /**< Command Byte */
uint32_t timestamp; /**< Timestamp */
uint8_t channelNumber; /**< Channel Number */
char channelName[CHANNEL_NAME_MAX_LEN]; /**< Channel Name */
} __attribute__((packed)) ControlChannelPayload; /**< ControlChannelPayload */

#endif /* SERIALMUXPROT_COMMON_H_ */
/** @} */
25 changes: 16 additions & 9 deletions src/SerialMuxProtServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ class SerialMuxProtServer
*/
void cmdSYNC(const uint32_t rcvTimestamp)
{
Command output = {.commandByte = COMMANDS::SYNC_RSP, .timestamp = rcvTimestamp};
ControlChannelPayload output;
output.commandByte = COMMANDS::SYNC_RSP;
output.timestamp = rcvTimestamp;

/* Ignore return as SYNC_RSP can fail */
(void)send(CONTROL_CHANNEL_NUMBER, &output, sizeof(Command));
(void)send(CONTROL_CHANNEL_NUMBER, &output, sizeof(ControlChannelPayload));
}

/**
Expand Down Expand Up @@ -301,15 +303,17 @@ class SerialMuxProtServer
*/
void cmdSCRB(const char* channelName)
{
Command output = {.commandByte = COMMANDS::SCRB_RSP, .channelNumber = getTxChannelNumber(channelName)};
ControlChannelPayload output;
output.commandByte = COMMANDS::SCRB_RSP;
output.channelNumber = getTxChannelNumber(channelName);

/* Using strnlen in case the name is not null-terminated. */
uint8_t nameLength = strnlen(channelName, CHANNEL_NAME_MAX_LEN);

/* Name is always sent back. */
memcpy(output.channelName, channelName, nameLength);

if (false == send(CONTROL_CHANNEL_NUMBER, &output, sizeof(Command)))
if (false == send(CONTROL_CHANNEL_NUMBER, &output, sizeof(ControlChannelPayload)))
{
/* Fall out of sync if failed to send. */
m_isSynced = false;
Expand Down Expand Up @@ -367,7 +371,7 @@ class SerialMuxProtServer
{
if ((nullptr != payload) && (CONTROL_CHANNEL_PAYLOAD_LENGTH == payloadSize))
{
const Command* parsedPayload = reinterpret_cast<const Command*>(payload);
const ControlChannelPayload* parsedPayload = reinterpret_cast<const ControlChannelPayload*>(payload);

switch (parsedPayload->commandByte)
{
Expand Down Expand Up @@ -514,9 +518,11 @@ class SerialMuxProtServer
}

/* Send SYNC Command. */
Command payload = {.commandByte = COMMANDS::SYNC, .timestamp = currentTimestamp};
ControlChannelPayload payload;
payload.commandByte = COMMANDS::SYNC;
payload.timestamp = currentTimestamp;

if (true == send(CONTROL_CHANNEL_NUMBER, &payload, sizeof(Command)))
if (true == send(CONTROL_CHANNEL_NUMBER, &payload, sizeof(ControlChannelPayload)))
{
m_lastSyncCommand = currentTimestamp;
}
Expand All @@ -537,10 +543,11 @@ class SerialMuxProtServer
/* Suscribe to channel. */
/* Using strnlen in case the name is not null-terminated. */
uint8_t nameLength = strnlen(m_pendingSuscribeChannels[idx].m_name, CHANNEL_NAME_MAX_LEN);
Command output = {.commandByte = COMMANDS::SCRB};
ControlChannelPayload output;
output.commandByte = COMMANDS::SCRB;
memcpy(output.channelName, m_pendingSuscribeChannels[idx].m_name, nameLength);

if (false == send(CONTROL_CHANNEL_NUMBER, &output, sizeof(Command)))
if (false == send(CONTROL_CHANNEL_NUMBER, &output, sizeof(ControlChannelPayload)))
{
/* Out-of-Sync on failed send. */
m_isSynced = false;
Expand Down

0 comments on commit 4188420

Please sign in to comment.