Skip to content

Commit

Permalink
Fix #89, separate msg size verify
Browse files Browse the repository at this point in the history
Move the CMD message input size validation to a separate function, so
the command handler does not do this itself.  This allows the handler to
focus on the real message action as opposed to the structure of the
message itself.
  • Loading branch information
jphickey committed Mar 17, 2023
1 parent 9d3daa7 commit bdea054
Show file tree
Hide file tree
Showing 16 changed files with 1,891 additions and 1,582 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(APP_SRC_FILES
fsw/src/fm_app.c
fsw/src/fm_cmds.c
fsw/src/fm_child.c
fsw/src/fm_dispatch.c
fsw/src/fm_tbl.c
)

Expand Down
190 changes: 21 additions & 169 deletions fsw/src/fm_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "fm_child.h"
#include "fm_cmds.h"
#include "fm_cmd_utils.h"
#include "fm_dispatch.h"
#include "fm_events.h"
#include "fm_perfids.h"
#include "fm_platform_cfg.h"
Expand Down Expand Up @@ -241,191 +242,42 @@ int32 FM_AppInit(void)
return Result;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM application -- input packet processor */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

void FM_ProcessPkt(const CFE_SB_Buffer_t *BufPtr)
{
CFE_SB_MsgId_t MessageID = CFE_SB_INVALID_MSG_ID;

CFE_MSG_GetMsgId(&BufPtr->Msg, &MessageID);

switch (CFE_SB_MsgIdToValue(MessageID))
{
/* Housekeeping request */
case FM_SEND_HK_MID:
FM_SendHkCmd(BufPtr);
break;

/* FM ground commands */
case FM_CMD_MID:
FM_ProcessCmd(BufPtr);
break;

default:
CFE_EVS_SendEvent(FM_MID_ERR_EID, CFE_EVS_EventType_ERROR,
"Main loop error: invalid message ID: mid = 0x%08lX",
(unsigned long)CFE_SB_MsgIdToValue(MessageID));
break;
}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM application -- command packet processor */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ProcessCmd(const CFE_SB_Buffer_t *BufPtr)
{
bool Result = true;
CFE_MSG_FcnCode_t CommandCode = 0;

CFE_MSG_GetFcnCode(&BufPtr->Msg, &CommandCode);

/* Invoke specific command handler */
switch (CommandCode)
{
case FM_NOOP_CC:
Result = FM_NoopCmd(BufPtr);
break;

case FM_RESET_COUNTERS_CC:
Result = FM_ResetCountersCmd(BufPtr);
break;

case FM_COPY_FILE_CC:
Result = FM_CopyFileCmd(BufPtr);
break;

case FM_MOVE_FILE_CC:
Result = FM_MoveFileCmd(BufPtr);
break;

case FM_RENAME_FILE_CC:
Result = FM_RenameFileCmd(BufPtr);
break;

case FM_DELETE_FILE_CC:
Result = FM_DeleteFileCmd(BufPtr);
break;

case FM_DELETE_ALL_FILES_CC:
Result = FM_DeleteAllFilesCmd(BufPtr);
break;

case FM_DECOMPRESS_FILE_CC:
Result = FM_DecompressFileCmd(BufPtr);
break;

case FM_CONCAT_FILES_CC:
Result = FM_ConcatFilesCmd(BufPtr);
break;

case FM_GET_FILE_INFO_CC:
Result = FM_GetFileInfoCmd(BufPtr);
break;

case FM_GET_OPEN_FILES_CC:
Result = FM_GetOpenFilesCmd(BufPtr);
break;

case FM_CREATE_DIRECTORY_CC:
Result = FM_CreateDirectoryCmd(BufPtr);
break;

case FM_DELETE_DIRECTORY_CC:
Result = FM_DeleteDirectoryCmd(BufPtr);
break;

case FM_GET_DIR_LIST_FILE_CC:
Result = FM_GetDirListFileCmd(BufPtr);
break;

case FM_GET_DIR_LIST_PKT_CC:
Result = FM_GetDirListPktCmd(BufPtr);
break;

case FM_MONITOR_FILESYSTEM_SPACE_CC:
Result = FM_MonitorFilesystemSpaceCmd(BufPtr);
break;

case FM_SET_TABLE_STATE_CC:
Result = FM_SetTableStateCmd(BufPtr);
break;

case FM_SET_PERMISSIONS_CC:
Result = FM_SetPermissionsCmd(BufPtr);
break;

default:
Result = false;
CFE_EVS_SendEvent(FM_CC_ERR_EID, CFE_EVS_EventType_ERROR, "Main loop error: invalid command code: cc = %d",
CommandCode);
break;
}

if (Result == true)
{
/* Increment command success counter */
if (CommandCode != FM_RESET_COUNTERS_CC)
{
FM_GlobalData.CommandCounter++;
}
}
else
{
/* Increment command error counter */
FM_GlobalData.CommandErrCounter++;
}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM application -- housekeeping request packet processor */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_SendHkCmd(const CFE_SB_Buffer_t *BufPtr)
{
const char * CmdText = "HK Request";
bool Result = true;
FM_HousekeepingPkt_Payload_t *PayloadPtr;

/* Verify command packet length */
Result = FM_IsValidCmdPktLength(CFE_MSG_PTR(*BufPtr), sizeof(FM_SendHkCmd_t), FM_HK_REQ_ERR_EID, CmdText);

if (Result == true)
{
FM_ReleaseTablePointers();
FM_ReleaseTablePointers();

FM_AcquireTablePointers();
FM_AcquireTablePointers();

/* Initialize housekeeping telemetry message */
CFE_MSG_Init(CFE_MSG_PTR(FM_GlobalData.HousekeepingPkt.TelemetryHeader), CFE_SB_ValueToMsgId(FM_HK_TLM_MID),
sizeof(FM_HousekeepingPkt_t));
/* Initialize housekeeping telemetry message */
CFE_MSG_Init(CFE_MSG_PTR(FM_GlobalData.HousekeepingPkt.TelemetryHeader), CFE_SB_ValueToMsgId(FM_HK_TLM_MID),
sizeof(FM_HousekeepingPkt_t));

PayloadPtr = &FM_GlobalData.HousekeepingPkt.Payload;
PayloadPtr = &FM_GlobalData.HousekeepingPkt.Payload;

/* Report application command counters */
PayloadPtr->CommandCounter = FM_GlobalData.CommandCounter;
PayloadPtr->CommandErrCounter = FM_GlobalData.CommandErrCounter;
/* Report application command counters */
PayloadPtr->CommandCounter = FM_GlobalData.CommandCounter;
PayloadPtr->CommandErrCounter = FM_GlobalData.CommandErrCounter;

PayloadPtr->NumOpenFiles = FM_GetOpenFilesData(NULL);
PayloadPtr->NumOpenFiles = FM_GetOpenFilesData(NULL);

/* Report child task command counters */
PayloadPtr->ChildCmdCounter = FM_GlobalData.ChildCmdCounter;
PayloadPtr->ChildCmdErrCounter = FM_GlobalData.ChildCmdErrCounter;
PayloadPtr->ChildCmdWarnCounter = FM_GlobalData.ChildCmdWarnCounter;
/* Report child task command counters */
PayloadPtr->ChildCmdCounter = FM_GlobalData.ChildCmdCounter;
PayloadPtr->ChildCmdErrCounter = FM_GlobalData.ChildCmdErrCounter;
PayloadPtr->ChildCmdWarnCounter = FM_GlobalData.ChildCmdWarnCounter;

PayloadPtr->ChildQueueCount = FM_GlobalData.ChildQueueCount;
PayloadPtr->ChildQueueCount = FM_GlobalData.ChildQueueCount;

/* Report current and previous commands executed by the child task */
PayloadPtr->ChildCurrentCC = FM_GlobalData.ChildCurrentCC;
PayloadPtr->ChildPreviousCC = FM_GlobalData.ChildPreviousCC;
/* Report current and previous commands executed by the child task */
PayloadPtr->ChildCurrentCC = FM_GlobalData.ChildCurrentCC;
PayloadPtr->ChildPreviousCC = FM_GlobalData.ChildPreviousCC;

CFE_SB_TimeStampMsg(CFE_MSG_PTR(FM_GlobalData.HousekeepingPkt.TelemetryHeader));
CFE_SB_TransmitMsg(CFE_MSG_PTR(FM_GlobalData.HousekeepingPkt.TelemetryHeader), true);
}
CFE_SB_TimeStampMsg(CFE_MSG_PTR(FM_GlobalData.HousekeepingPkt.TelemetryHeader));
CFE_SB_TransmitMsg(CFE_MSG_PTR(FM_GlobalData.HousekeepingPkt.TelemetryHeader), true);
}
28 changes: 0 additions & 28 deletions fsw/src/fm_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,34 +161,6 @@ void FM_AppMain(void);
*/
int32 FM_AppInit(void);

/**
* \brief Process Input Command Packets
*
* \par Description
*
* Branch to appropriate input packet handler: HK request or FM commands.
*
* \par Assumptions, External Events, and Notes: None
*
* \param [in] MessagePtr Pointer to Software Bus message buffer.
*
* \sa #FM_SendHkCmd, #FM_ProcessCmd
*/
void FM_ProcessPkt(const CFE_SB_Buffer_t *MessagePtr);

/**
* \brief Process FM Ground Commands
*
* \par Description
*
* Branch to the command specific handlers for FM ground commands.
*
* \par Assumptions, External Events, and Notes: None
*
* \param [in] BufPtr Pointer to Software Bus message buffer.
*/
void FM_ProcessCmd(const CFE_SB_Buffer_t *BufPtr);

/**
* \brief Housekeeping Request Command Handler
*
Expand Down
26 changes: 0 additions & 26 deletions fsw/src/fm_cmd_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,6 @@
static uint32 OpenFileCount = 0;
static bool FileIsOpen = false;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM utility function -- verify command packet length */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

bool FM_IsValidCmdPktLength(const CFE_MSG_Message_t *MsgPtr, size_t ExpectedLength, uint32 EventID, const char *CmdText)
{
bool FunctionResult = true;
size_t ActualLength = 0;

CFE_MSG_GetSize(MsgPtr, &ActualLength);

/* Verify command packet length */
if (ActualLength != ExpectedLength)
{
FunctionResult = false;

CFE_EVS_SendEvent(EventID, CFE_EVS_EventType_ERROR,
"%s error: invalid command packet length: expected = %d, actual = %d", CmdText,
(int)ExpectedLength, (int)ActualLength);
}

return FunctionResult;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM utility function -- verify state is not invalid */
Expand Down
21 changes: 0 additions & 21 deletions fsw/src/fm_cmd_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,6 @@ typedef enum
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/**
* \brief Verify Command Packet Length Function
*
* \par Description
* This function is invoked from each of the command handlers to verify the
* length of the command packet.
*
* \par Assumptions, External Events, and Notes:
*
* \param [in] MsgPtr Pointer to Message
* \param [in] ExpectedLength Expected packet length (command specific)
* \param [in] EventID Error event ID (command specific)
* \param [in] CmdText Error event text (command specific)
*
* \return Boolean valid packet length response
* \retval true Packet length valid
* \retval false Packet length invalid
*/
bool FM_IsValidCmdPktLength(const CFE_MSG_Message_t *MsgPtr, size_t ExpectedLength, uint32 EventID,
const char *CmdText);

/**
* \brief Verify Target File Overwrite Function
*
Expand Down
Loading

0 comments on commit bdea054

Please sign in to comment.