From b3fbe1aacb85bf66032d09ad9d006fbcd7df757f Mon Sep 17 00:00:00 2001 From: Justin Figueroa Date: Thu, 29 Sep 2022 22:32:30 -0400 Subject: [PATCH] Fix #51, remove mid draft --- fsw/src/ds_app.c | 7 +++ fsw/src/ds_cmds.c | 86 ++++++++++++++++++++++++++++++++ fsw/src/ds_cmds.h | 23 +++++++++ fsw/src/ds_events.h | 44 ++++++++++++++-- fsw/src/ds_msg.h | 13 +++++ fsw/src/ds_msgdefs.h | 34 ++++++++++++- fsw/src/ds_table.c | 52 +++++++++++++++++++ fsw/src/ds_table.h | 21 ++++++++ unit-test/stubs/ds_cmds_stubs.c | 13 +++++ unit-test/stubs/ds_table_stubs.c | 16 +++++- 10 files changed, 302 insertions(+), 7 deletions(-) diff --git a/fsw/src/ds_app.c b/fsw/src/ds_app.c index 545e195..1c80f8d 100644 --- a/fsw/src/ds_app.c +++ b/fsw/src/ds_app.c @@ -469,6 +469,13 @@ void DS_AppProcessCmd(const CFE_SB_Buffer_t *BufPtr) DS_CmdAddMID(BufPtr); break; + /* + ** Remove message ID from filter table... + */ + case DS_REMOVE_MID_CC: + DS_CmdRemoveMID(BufPtr); + break; + /* ** Close all destination files (next packet will re-open)... */ diff --git a/fsw/src/ds_cmds.c b/fsw/src/ds_cmds.c index 997eb29..63c2bad 100644 --- a/fsw/src/ds_cmds.c +++ b/fsw/src/ds_cmds.c @@ -1442,6 +1442,92 @@ void DS_CmdAddMID(const CFE_SB_Buffer_t *BufPtr) } } /* End of DS_CmdAddMID() */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/* */ +/* DS_CmdRemoveMID() - remove message ID from packet filter table */ +/* */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +void DS_CmdRemoveMID(const CFE_SB_Buffer_t *BufPtr) +{ + DS_RemoveMidCmd_t *DS_RemoveMidCmd = (DS_RemoveMidCmd_t *)BufPtr; + size_t ActualLength = 0; + size_t ExpectedLength = sizeof(DS_RemoveMidCmd_t); + DS_PacketEntry_t * pPacketEntry = NULL; + int32 FilterTableIndex = 0; + int32 HashTableIndex = 0; + + if (ExpectedLength != ActualLength) + { + /* + ** Invalid command packet length... + */ + DS_AppData.CmdRejectedCounter++; + + CFE_EVS_SendEvent(DS_REMOVE_MID_CMD_ERR_EID, CFE_EVS_EventType_ERROR, + "Invalid REMOVE MID command length: expected = %d, actual = %d", (int)ExpectedLength, + (int)ActualLength); + } + else if (!CFE_SB_IsValidMsgId(DS_RemoveMidCmd->MessageID)) + { + /* + ** Invalid packet message ID - can be anything but unused... + */ + DS_AppData.CmdRejectedCounter++; + + CFE_EVS_SendEvent(DS_REMOVE_MID_CMD_ERR_EID, CFE_EVS_EventType_ERROR, + "Invalid REMOVE MID command arg: invalid MID = 0x%08lX", + (unsigned long)CFE_SB_MsgIdToValue(DS_RemoveMidCmd->MessageID)); + } + else if (DS_AppData.FilterTblPtr == (DS_FilterTable_t *)NULL) + { + /* + ** Must have a valid packet filter table loaded... + */ + DS_AppData.CmdRejectedCounter++; + + CFE_EVS_SendEvent(DS_REMOVE_MID_CMD_ERR_EID, CFE_EVS_EventType_ERROR, + "Invalid REMOVE MID command: filter table is not loaded"); + } + else if ((FilterTableIndex = DS_TableFindMsgID(DS_RemoveMidCmd->MessageID)) == DS_INDEX_NONE) + { + /* + ** Message ID is not in packet filter table... + */ + DS_AppData.CmdRejectedCounter++; + + CFE_EVS_SendEvent(DS_REMOVE_MID_CMD_ERR_EID, CFE_EVS_EventType_ERROR, + "Invalid REMOVE MID command: MID = 0x%08lX is not in filter table", + (unsigned long)CFE_SB_MsgIdToValue(DS_RemoveMidCmd->MessageID)); + } + else + { + /* + ** Free used packet filter entry for used message ID... + */ + pPacketEntry = &DS_AppData.FilterTblPtr->Packet[FilterTableIndex]; + + free(pPacketEntry); + + /* Remove the message ID from the hash table as well */ + HashTableIndex = DS_TableRemoveMsgID(DS_RemoveMidCmd->MessageID, FilterTableIndex); + + CFE_SB_Unsubscribe(DS_RemoveMidCmd->MessageID, DS_AppData.InputPipe); + + /* + ** Notify cFE that we have modified the table data... + */ + CFE_TBL_Modified(DS_AppData.FilterTblHandle); + + DS_AppData.CmdAcceptedCounter++; + + CFE_EVS_SendEvent(DS_REMOVE_MID_CMD_EID, CFE_EVS_EventType_DEBUG, + "REMOVE MID command: MID = 0x%08lX, filter index = %d, hash index = %d", + (unsigned long)CFE_SB_MsgIdToValue(DS_RemoveMidCmd->MessageID), (int)FilterTableIndex, + (int)HashTableIndex); + } +} /* End of DS_CmdRemoveMID() */ + /************************/ /* End of File Comment */ /************************/ diff --git a/fsw/src/ds_cmds.h b/fsw/src/ds_cmds.h index 26f4e8c..f029ae9 100644 --- a/fsw/src/ds_cmds.h +++ b/fsw/src/ds_cmds.h @@ -444,4 +444,27 @@ void DS_CmdGetFileInfo(const CFE_SB_Buffer_t *BufPtr); */ void DS_CmdAddMID(const CFE_SB_Buffer_t *BufPtr); +/** + * \brief Remove Message ID from Packet Filter Table + * + * \par Description + * Remove used packet filter table entry + * Reject invalid commands + * - generate error event if invalid command packet length + * - generate error event if MID argument is invalid (cannot be zero) + * - generate error event if packet filter table is not loaded + * - generate error event if MID is already in packet filter table + * - generate error event if no unused packet filter table entries + * Accept valid commands + * - generate success event (event type = debug) + * + * \par Assumptions, External Events, and Notes: + * (none) + * + * \param[in] BufPtr Software Bus message pointer + * + * \sa #DS_REMOVE_MID_CC, #DS_RemoveMidCmd_t + */ +void DS_CmdRemoveMID(const CFE_SB_Buffer_t *BufPtr); + #endif diff --git a/fsw/src/ds_events.h b/fsw/src/ds_events.h index c41dbbf..1884e94 100644 --- a/fsw/src/ds_events.h +++ b/fsw/src/ds_events.h @@ -787,6 +787,40 @@ */ #define DS_ADD_MID_CMD_ERR_EID 65 +/** + * \brief DS Remove Message ID from Filter Table Command Event ID + * + * \par Type: DEBUG + * + * \par Cause: + * + * This event signals the successful execution of a command to remove + * a message ID from the Packet Filter Table. + * + * The Packet Filter Table must be loaded and have a used entry + * for removing the message ID. The message ID must not be zero + * be and must already exist in the table. + */ +#define DS_REMOVE_MID_CMD_EID 66 + +/** + * \brief DS Remove Message ID from Filter Table Command Invalid Event ID + * + * \par Type: ERROR + * + * \par Cause: + * + * This event signals the failed execution of a command to remove a + * message ID from the Packet Filter Table. The cause of the failure + * may be an invalid command packet length or an invalid message ID. + * + * The failure may also result from not having a Packet Filter Table + * loaded at the time the command was invoked. The loaded table + * must have an unused entry available for the new message ID and + * must not already contain the new message ID. + */ +#define DS_REMOVE_MID_CMD_ERR_EID 67 + /** * \brief DS Close All Destination Files Command Event ID * @@ -799,7 +833,7 @@ * * This command will succeed regardless of whether there was an open file. */ -#define DS_CLOSE_ALL_CMD_EID 66 +#define DS_CLOSE_ALL_CMD_EID 68 /** * \brief DS Close All Destination Files Command Invalid Event ID @@ -812,7 +846,7 @@ * all open Destination Files. The cause of the failure can only * be an invalid command packet length. */ -#define DS_CLOSE_ALL_CMD_ERR_EID 67 +#define DS_CLOSE_ALL_CMD_ERR_EID 69 /** * \brief DS File Create Invalid Name Event ID @@ -824,7 +858,7 @@ * This event is generated when DS_FileCreateName is invoked with an * empty path name. */ -#define DS_FILE_CREATE_EMPTY_PATH_ERR_EID 68 +#define DS_FILE_CREATE_EMPTY_PATH_ERR_EID 70 /** * \brief DS Filter Table Name Invalid Event ID @@ -836,7 +870,7 @@ * This event is issued when an invalid filter table name is passed to * the CFE_TBL_GetInfo. */ -#define DS_APPHK_FILTER_TBL_ERR_EID 69 +#define DS_APPHK_FILTER_TBL_ERR_EID 71 /** * \brief DS Filter Table Name Create Failed Event ID @@ -848,7 +882,7 @@ * This event is issued when the filter table name is not successfully * created (via snprintf) in the DS_AppProcessHK function. */ -#define DS_APPHK_FILTER_TBL_PRINT_ERR_EID 70 +#define DS_APPHK_FILTER_TBL_PRINT_ERR_EID 72 /**@}*/ diff --git a/fsw/src/ds_msg.h b/fsw/src/ds_msg.h index 3e8c54a..482dd96 100644 --- a/fsw/src/ds_msg.h +++ b/fsw/src/ds_msg.h @@ -293,6 +293,19 @@ typedef struct } DS_AddMidCmd_t; +/** + * \brief Remove Message ID from Packet Filter Table + * + * For command details see #DS_REMOVE_MID_CC + */ +typedef struct +{ + CFE_MSG_CommandHeader_t CmdHeader; /**< \brief cFE Software Bus command message header */ + + CFE_SB_MsgId_t MessageID; /**< \brief Message ID to add to Packet Filter Table */ + +} DS_RemoveMidCmd_t; + /**\}*/ /** diff --git a/fsw/src/ds_msgdefs.h b/fsw/src/ds_msgdefs.h index a616fd4..690978c 100644 --- a/fsw/src/ds_msgdefs.h +++ b/fsw/src/ds_msgdefs.h @@ -549,6 +549,38 @@ */ #define DS_ADD_MID_CC 16 +/** + * \brief Remove Message ID from Packet Filter Table + * + * \par Description + * This command will change the Message ID selection for an + * unused Packet Filter Table entry to the indicated value. + * + * \par Command Structure + * #DS_RemoveMidCmd_t + * + * \par Command Verification + * Evidence of success may be found in the following telemetry: + * - #DS_HkPacket_t.CmdAcceptedCounter will increment + * - The #DS_ADD_MID_CMD_EID debug event message will be sent + * + * \par Error Conditions + * This command can fail for the following reasons: + * - Invalid command packet length + * - Message ID is invalid (can be anything but zero) + * - Packet filter table is not currently loaded + * - Message ID already exists in packet filter table + * - All packet filter table entries are already in use + * + * Evidence of failure may be found in the following telemetry: + * - #DS_HkPacket_t.CmdRejectedCounter will increment + * - The #DS_ADD_MID_CMD_ERR_EID error event message will be sent + * + * \par Criticality + * None + */ +#define DS_REMOVE_MID_CC 17 + /** * \brief Close All Destination Files * @@ -577,7 +609,7 @@ * \par Criticality * None */ -#define DS_CLOSE_ALL_CC 17 +#define DS_CLOSE_ALL_CC 18 /**\}*/ diff --git a/fsw/src/ds_table.c b/fsw/src/ds_table.c index fadca97..dde6833 100644 --- a/fsw/src/ds_table.c +++ b/fsw/src/ds_table.c @@ -1109,6 +1109,58 @@ int32 DS_TableAddMsgID(CFE_SB_MsgId_t MessageID, int32 FilterIndex) } /* End of DS_TableAddMsgID() */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/* */ +/* DS_TableRemoveMsgID() - get hash table index for MID */ +/* */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int32 DS_TableRemoveMsgID(CFE_SB_MsgId_t MessageID, int32 FilterIndex) +{ + int32 HashIndex = 0; + DS_HashLink_t *RemoveLink = NULL; + DS_HashLink_t *LinkList = NULL; + DS_HashLink_t *PrevLink = NULL; + + /* Get used linked list entry (one link entry per filter table entry) */ + RemoveLink = &DS_AppData.HashTable[FilterIndex]; + + /* Hash table function converts MID into hash table index */ + HashIndex = DS_TableHashFunction(NewLink->MessageID); + + if (DS_AppData.HashTable[HashIndex]->Index == RemoveLink->Index) + { + /* Remove first link in this hash table entry linked list */ + free(RemoveLink); + } + else + { + /* + ** Get start of linked list (all MID's with same hash result) + ** Keep track of previous link as we need need to change 'PrevLink->Next' + */ + /* Get start of linked list (all MID's with same hash result) */ + PrevLink = DS_AppData.HashTable[HashIndex]; + + LinkList = DS_AppData.HashTable[HashIndex]->Next; + + /* Find link to remove */ + while (LinkList->Index != RemoveLink->Index) + { + PrevLink = LinkList; + LinkList = LinkList->Next; + } + + /* Remove link */ + PrevLink->Next = LinkList->Next; + + free(LinkList); + } + + return (HashIndex); + +} /* End of DS_TableRemoveMsgID() */ + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* */ /* DS_TableFindMsgID() - get filter table index for MID */ diff --git a/fsw/src/ds_table.h b/fsw/src/ds_table.h index 4a3fad7..814f180 100644 --- a/fsw/src/ds_table.h +++ b/fsw/src/ds_table.h @@ -613,6 +613,27 @@ void DS_TableCreateHash(void); */ int32 DS_TableAddMsgID(CFE_SB_MsgId_t MessageID, int32 FilterIndex); +/** + * \brief Removes a message ID from the hash table + * + * \par Description + * This function removes a message ID from the hash table + * + * \par Called From: + * - Command to remove a MID + * + * \par Assumptions, External Events, and Notes: + * (none) + * + * \param[in] MessageID Message ID + * \param[in] FilterIndex Filter table index for message ID + * + * \return Hash table index for message ID + * + * \sa #DS_HashLink_t, #DS_TableHashFunction, #DS_TableFindMsgID + */ +int32 DS_TableRemoveMsgID(CFE_SB_MsgId_t MessageID, int32 FilterIndex); + /** * \brief Search packet filter table for message ID * diff --git a/unit-test/stubs/ds_cmds_stubs.c b/unit-test/stubs/ds_cmds_stubs.c index 3158a1a..cdcd447 100644 --- a/unit-test/stubs/ds_cmds_stubs.c +++ b/unit-test/stubs/ds_cmds_stubs.c @@ -279,6 +279,19 @@ void DS_CmdAddMID(const CFE_SB_Buffer_t *BufPtr) } /* End of DS_CmdAddMID() */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/* */ +/* DS_CmdRemoveMID() - remove message ID from packet filter table */ +/* */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +void DS_CmdRemoveMID(const CFE_SB_Buffer_t *BufPtr) +{ + UT_Stub_RegisterContextGenericArg(UT_KEY(DS_CmdRemoveMID), BufPtr); + UT_DEFAULT_IMPL(DS_CmdRemoveMID); + +} /* End of DS_CmdRemoveMID() */ + /************************/ /* End of File Comment */ /************************/ diff --git a/unit-test/stubs/ds_table_stubs.c b/unit-test/stubs/ds_table_stubs.c index 3b87862..2be6959 100644 --- a/unit-test/stubs/ds_table_stubs.c +++ b/unit-test/stubs/ds_table_stubs.c @@ -306,7 +306,7 @@ void DS_TableCreateHash(void) /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* */ -/* DS_TableFindMsgID() - get filter table index for MID */ +/* DS_TableAddMsgID() - get filter table index for MID */ /* */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -318,6 +318,20 @@ int32 DS_TableAddMsgID(CFE_SB_MsgId_t MessageID, int32 FilterIndex) } /* End of DS_TableAddMsgID() */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/* */ +/* DS_TableRemoveMsgID() - get filter table index for MID */ +/* */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +int32 DS_TableRemoveMsgID(CFE_SB_MsgId_t MessageID, int32 FilterIndex) +{ + UT_Stub_RegisterContextGenericArg(UT_KEY(DS_TableAddMsgID), MessageID); + UT_Stub_RegisterContextGenericArg(UT_KEY(DS_TableAddMsgID), FilterIndex); + return UT_DEFAULT_IMPL(DS_TableAddMsgID); + +} /* End of DS_TableRemoveMsgID() */ + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* */ /* DS_TableFindMsgID() - get filter table index for MID */