LCOV - code coverage report
Current view: top level - apps/hk/fsw/src - hk_app.c (source / functions) Hit Total Coverage
Test: coverage_total.info Lines: 121 121 100.0 %
Date: 2023-08-28 16:42:09 Functions: 8 8 100.0 %
Branches: 42 42 100.0 %

           Branch data     Line data    Source code
       1                 :            : /************************************************************************
       2                 :            :  * NASA Docket No. GSC-18,919-1, and identified as “Core Flight
       3                 :            :  * System (cFS) Housekeeping (HK) Application version 2.5.1”
       4                 :            :  *
       5                 :            :  * Copyright (c) 2021 United States Government as represented by the
       6                 :            :  * Administrator of the National Aeronautics and Space Administration.
       7                 :            :  * All Rights Reserved.
       8                 :            :  *
       9                 :            :  * Licensed under the Apache License, Version 2.0 (the "License"); you may
      10                 :            :  * not use this file except in compliance with the License. You may obtain
      11                 :            :  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
      12                 :            :  *
      13                 :            :  * Unless required by applicable law or agreed to in writing, software
      14                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      15                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      16                 :            :  * See the License for the specific language governing permissions and
      17                 :            :  * limitations under the License.
      18                 :            :  ************************************************************************/
      19                 :            : 
      20                 :            : /**
      21                 :            :  * @file
      22                 :            :  *  The CFS Housekeeping (HK) Application file containing the application
      23                 :            :  *  initialization routines, the main routine and the command interface.
      24                 :            :  */
      25                 :            : 
      26                 :            : /************************************************************************
      27                 :            : ** Includes
      28                 :            : *************************************************************************/
      29                 :            : #include "hk_app.h"
      30                 :            : #include "hk_events.h"
      31                 :            : #include "hk_msgids.h"
      32                 :            : #include "hk_perfids.h"
      33                 :            : #include "hk_verify.h"
      34                 :            : #include "hk_version.h"
      35                 :            : #include "hk_platform_cfg.h"
      36                 :            : #include "hk_utils.h"
      37                 :            : #include "hk_dispatch.h"
      38                 :            : #include <string.h>
      39                 :            : 
      40                 :            : /************************************************************************
      41                 :            : ** HK global data
      42                 :            : *************************************************************************/
      43                 :            : HK_AppData_t HK_AppData;
      44                 :            : 
      45                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      46                 :            : /*                                                                 */
      47                 :            : /* HK application entry point and main process loop                */
      48                 :            : /*                                                                 */
      49                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
      50                 :          6 : void HK_AppMain(void)
      51                 :            : {
      52                 :          6 :     CFE_Status_t     Status = CFE_SUCCESS;
      53                 :          6 :     CFE_SB_Buffer_t *BufPtr = NULL;
      54                 :            : 
      55                 :            :     /*
      56                 :            :     ** Create the first Performance Log entry
      57                 :            :     */
      58                 :          6 :     CFE_ES_PerfLogEntry(HK_APPMAIN_PERF_ID);
      59                 :            : 
      60                 :            :     /* Perform Application Initialization */
      61                 :          6 :     Status = HK_AppInit();
      62         [ +  + ]:          6 :     if (Status != CFE_SUCCESS)
      63                 :            :     {
      64                 :          1 :         HK_AppData.RunStatus = CFE_ES_RunStatus_APP_ERROR;
      65                 :            :     }
      66                 :            : 
      67                 :            :     /*
      68                 :            :     ** Application Main Loop.
      69                 :            :     */
      70         [ +  + ]:         10 :     while (CFE_ES_RunLoop(&HK_AppData.RunStatus) == true)
      71                 :            :     {
      72                 :            :         /*
      73                 :            :         ** Performance Log Exit Stamp.
      74                 :            :         */
      75                 :          4 :         CFE_ES_PerfLogExit(HK_APPMAIN_PERF_ID);
      76                 :            : 
      77                 :            :         /*
      78                 :            :         ** Pend on the arrival of the next Software Bus message.
      79                 :            :         */
      80                 :          4 :         Status = CFE_SB_ReceiveBuffer(&BufPtr, HK_AppData.CmdPipe, HK_SB_TIMEOUT);
      81                 :            : 
      82         [ +  + ]:          4 :         if (Status == CFE_SUCCESS)
      83                 :            :         {
      84                 :            :             /*
      85                 :            :             ** Performance Log Entry Stamp.
      86                 :            :             */
      87                 :          1 :             CFE_ES_PerfLogEntry(HK_APPMAIN_PERF_ID);
      88                 :            : 
      89                 :            :             /* Perform Message Processing */
      90                 :          1 :             HK_AppPipe(BufPtr);
      91                 :            :         }
      92         [ +  + ]:          3 :         else if (Status == CFE_SB_TIME_OUT)
      93                 :            :         {
      94                 :            :             /* Check for copy table load and runtime dump request. This is
      95                 :            :              * generally done during the housekeeping cycle.  If we are
      96                 :            :              * getting routine messages at a rate of less than 1Hz we do
      97                 :            :              * the routine maintenance here. */
      98         [ +  + ]:          2 :             if (HK_CheckStatusOfTables() != HK_SUCCESS)
      99                 :            :             {
     100                 :          1 :                 HK_AppData.RunStatus = CFE_ES_RunStatus_APP_ERROR;
     101                 :            :             }
     102                 :            :         }
     103                 :            :         else
     104                 :            :         {
     105                 :          1 :             CFE_EVS_SendEvent(HK_RCV_MSG_ERR_EID, CFE_EVS_EventType_ERROR,
     106                 :            :                               "HK_APP Exiting due to CFE_SB_RcvMsg error 0x%08X", (unsigned int)Status);
     107                 :            : 
     108                 :            :             /* Write to syslog in case there is a problem with event services */
     109                 :          1 :             CFE_ES_WriteToSysLog("HK_APP Exiting due to CFE_SB_RcvMsg error 0x%08X\n", (unsigned int)Status);
     110                 :            : 
     111                 :          1 :             HK_AppData.RunStatus = CFE_ES_RunStatus_APP_ERROR;
     112                 :            :         }
     113                 :            :     } /* end while */
     114                 :            : 
     115                 :            :     /*
     116                 :            :      ** Performance Log Exit Stamp.
     117                 :            :      */
     118                 :          6 :     CFE_ES_PerfLogExit(HK_APPMAIN_PERF_ID);
     119                 :            : 
     120                 :            :     /*
     121                 :            :     ** Exit the Application.
     122                 :            :     */
     123                 :          6 :     CFE_ES_ExitApp(HK_AppData.RunStatus);
     124                 :          6 : }
     125                 :            : 
     126                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     127                 :            : /*                                                                 */
     128                 :            : /* HK application initialization routine                           */
     129                 :            : /*                                                                 */
     130                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     131                 :         15 : CFE_Status_t HK_AppInit(void)
     132                 :            : {
     133                 :         15 :     CFE_Status_t Status = CFE_SUCCESS;
     134                 :            : 
     135                 :         15 :     HK_AppData.RunStatus = CFE_ES_RunStatus_APP_RUN;
     136                 :            : 
     137                 :            :     /* Initialize housekeeping packet  */
     138                 :         15 :     CFE_MSG_Init(CFE_MSG_PTR(HK_AppData.HkPacket.TelemetryHeader), CFE_SB_ValueToMsgId(HK_HK_TLM_MID),
     139                 :            :                  sizeof(HK_HkPacket_t));
     140                 :            : 
     141                 :            :     /* Register for event services...        */
     142                 :         15 :     Status = CFE_EVS_Register(NULL, 0, CFE_EVS_EventFilter_BINARY);
     143         [ +  + ]:         15 :     if (Status != CFE_SUCCESS)
     144                 :            :     {
     145                 :          2 :         CFE_ES_WriteToSysLog("HK: error registering for event services: 0x%08X\n", (unsigned int)Status);
     146                 :          2 :         return Status;
     147                 :            :     }
     148                 :            : 
     149                 :            :     /* Create HK Command Pipe */
     150                 :         13 :     Status = CFE_SB_CreatePipe(&HK_AppData.CmdPipe, HK_PIPE_DEPTH, HK_PIPE_NAME);
     151         [ +  + ]:         13 :     if (Status != CFE_SUCCESS)
     152                 :            :     {
     153                 :          1 :         CFE_EVS_SendEvent(HK_CR_PIPE_ERR_EID, CFE_EVS_EventType_ERROR, "Error Creating SB Pipe,RC=0x%08X",
     154                 :            :                           (unsigned int)Status);
     155                 :          1 :         return Status;
     156                 :            :     }
     157                 :            : 
     158                 :            :     /* Subscribe to 'Send Combined HK Pkt' Command */
     159                 :         12 :     Status = CFE_SB_Subscribe(CFE_SB_ValueToMsgId(HK_SEND_COMBINED_PKT_MID), HK_AppData.CmdPipe);
     160         [ +  + ]:         12 :     if (Status != CFE_SUCCESS)
     161                 :            :     {
     162                 :          1 :         CFE_EVS_SendEvent(HK_SUB_CMB_ERR_EID, CFE_EVS_EventType_ERROR,
     163                 :            :                           "Error Subscribing to HK Snd Cmb Pkt, MID=0x%08X, RC=0x%08X", HK_SEND_COMBINED_PKT_MID,
     164                 :            :                           (unsigned int)Status);
     165                 :          1 :         return Status;
     166                 :            :     }
     167                 :            : 
     168                 :            :     /* Subscribe to Housekeeping Request */
     169                 :         11 :     Status = CFE_SB_Subscribe(CFE_SB_ValueToMsgId(HK_SEND_HK_MID), HK_AppData.CmdPipe);
     170         [ +  + ]:         11 :     if (Status != CFE_SUCCESS)
     171                 :            :     {
     172                 :          1 :         CFE_EVS_SendEvent(HK_SUB_REQ_ERR_EID, CFE_EVS_EventType_ERROR,
     173                 :            :                           "Error Subscribing to HK Request, MID=0x%08X, RC=0x%08X", HK_SEND_HK_MID,
     174                 :            :                           (unsigned int)Status);
     175                 :          1 :         return Status;
     176                 :            :     }
     177                 :            : 
     178                 :            :     /* Subscribe to HK ground commands */
     179                 :         10 :     Status = CFE_SB_Subscribe(CFE_SB_ValueToMsgId(HK_CMD_MID), HK_AppData.CmdPipe);
     180         [ +  + ]:         10 :     if (Status != CFE_SUCCESS)
     181                 :            :     {
     182                 :          1 :         CFE_EVS_SendEvent(HK_SUB_CMD_ERR_EID, CFE_EVS_EventType_ERROR,
     183                 :            :                           "Error Subscribing to HK Gnd Cmds, MID=0x%08X, RC=0x%08X", HK_CMD_MID, (unsigned int)Status);
     184                 :          1 :         return Status;
     185                 :            :     }
     186                 :            : 
     187                 :            :     /* Create a memory pool for combined output messages */
     188                 :          9 :     Status = CFE_ES_PoolCreate(&HK_AppData.MemPoolHandle, HK_AppData.MemPoolBuffer, sizeof(HK_AppData.MemPoolBuffer));
     189         [ +  + ]:          9 :     if (Status != CFE_SUCCESS)
     190                 :            :     {
     191                 :          1 :         CFE_EVS_SendEvent(HK_CR_POOL_ERR_EID, CFE_EVS_EventType_ERROR, "Error Creating Memory Pool,RC=0x%08X",
     192                 :            :                           (unsigned int)Status);
     193                 :          1 :         return Status;
     194                 :            :     }
     195                 :            : 
     196                 :          8 :     HK_ResetHkData();
     197                 :            : 
     198                 :            :     /* Register The HK Tables */
     199                 :          8 :     Status = HK_TableInit();
     200         [ +  + ]:          8 :     if (Status != CFE_SUCCESS)
     201                 :            :     {
     202                 :            :         /* Specific failure is detailed in function HK_TableInit */
     203                 :          1 :         return Status;
     204                 :            :     }
     205                 :            : 
     206                 :            :     /* Application initialization event */
     207                 :          7 :     Status = CFE_EVS_SendEvent(HK_INIT_EID, CFE_EVS_EventType_INFORMATION, "HK Initialized.  Version %d.%d.%d.%d",
     208                 :            :                                HK_MAJOR_VERSION, HK_MINOR_VERSION, HK_REVISION, HK_MISSION_REV);
     209                 :            : 
     210         [ +  + ]:          7 :     if (Status != CFE_SUCCESS)
     211                 :            :     {
     212                 :          1 :         CFE_ES_WriteToSysLog("HK App:Error Sending Initialization Event,RC=0x%08X\n", (unsigned int)Status);
     213                 :            :     }
     214                 :            : 
     215                 :          7 :     return Status;
     216                 :            : }
     217                 :            : 
     218                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     219                 :            : /*                                                                 */
     220                 :            : /* HK application table initialization routine                     */
     221                 :            : /*                                                                 */
     222                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     223                 :         17 : CFE_Status_t HK_TableInit(void)
     224                 :            : {
     225                 :         17 :     CFE_Status_t Status = CFE_SUCCESS;
     226                 :            : 
     227                 :            :     /* Register The HK Copy Table */
     228                 :         17 :     Status = CFE_TBL_Register(&HK_AppData.CopyTableHandle, HK_COPY_TABLE_NAME,
     229                 :            :                               (sizeof(hk_copy_table_entry_t) * HK_COPY_TABLE_ENTRIES),
     230                 :            :                               CFE_TBL_OPT_DBL_BUFFER | CFE_TBL_OPT_LOAD_DUMP, HK_ValidateHkCopyTable);
     231                 :            : 
     232         [ +  + ]:         17 :     if (Status != CFE_SUCCESS)
     233                 :            :     {
     234                 :          2 :         CFE_EVS_SendEvent(HK_CPTBL_REG_ERR_EID, CFE_EVS_EventType_ERROR, "Error Registering Copy Table,RC=0x%08X",
     235                 :            :                           (unsigned int)Status);
     236                 :          2 :         return Status;
     237                 :            :     }
     238                 :            : 
     239                 :            :     /* Register The HK Runtime Table */
     240                 :         15 :     Status = CFE_TBL_Register(&HK_AppData.RuntimeTableHandle, HK_RUNTIME_TABLE_NAME,
     241                 :            :                               (sizeof(hk_runtime_tbl_entry_t) * HK_COPY_TABLE_ENTRIES),
     242                 :            :                               CFE_TBL_OPT_SNGL_BUFFER | CFE_TBL_OPT_DUMP_ONLY, NULL);
     243         [ +  + ]:         15 :     if (Status != CFE_SUCCESS)
     244                 :            :     {
     245                 :          1 :         CFE_EVS_SendEvent(HK_RTTBL_REG_ERR_EID, CFE_EVS_EventType_ERROR, "Error Registering Runtime Table,RC=0x%08X",
     246                 :            :                           (unsigned int)Status);
     247                 :          1 :         return Status;
     248                 :            :     }
     249                 :            : 
     250                 :         14 :     Status = CFE_TBL_Load(HK_AppData.CopyTableHandle, CFE_TBL_SRC_FILE, HK_COPY_TABLE_FILENAME);
     251         [ +  + ]:         14 :     if (Status != CFE_SUCCESS)
     252                 :            :     {
     253                 :          1 :         CFE_EVS_SendEvent(HK_CPTBL_LD_ERR_EID, CFE_EVS_EventType_ERROR, "Error Loading Copy Table,RC=0x%08X",
     254                 :            :                           (unsigned int)Status);
     255                 :          1 :         return Status;
     256                 :            :     }
     257                 :            : 
     258                 :         13 :     Status = CFE_TBL_Manage(HK_AppData.CopyTableHandle);
     259         [ +  + ]:         13 :     if (Status != CFE_SUCCESS)
     260                 :            :     {
     261                 :          1 :         CFE_EVS_SendEvent(HK_CPTBL_MNG_ERR_EID, CFE_EVS_EventType_ERROR,
     262                 :            :                           "Error from TBL Manage call for Copy Table,RC=0x%08X", (unsigned int)Status);
     263                 :          1 :         return Status;
     264                 :            :     }
     265                 :            : 
     266                 :         12 :     Status = CFE_TBL_Manage(HK_AppData.RuntimeTableHandle);
     267         [ +  + ]:         12 :     if (Status != CFE_SUCCESS)
     268                 :            :     {
     269                 :          1 :         CFE_EVS_SendEvent(HK_RTTBL_MNG_ERR_EID, CFE_EVS_EventType_ERROR,
     270                 :            :                           "Error from TBL Manage call for Runtime Table,RC=0x%08X", (unsigned int)Status);
     271                 :          1 :         return Status;
     272                 :            :     }
     273                 :            : 
     274                 :         11 :     Status = CFE_TBL_GetAddress((void *)(&HK_AppData.CopyTablePtr), HK_AppData.CopyTableHandle);
     275                 :            :     /* Status should be CFE_TBL_INFO_UPDATED because we loaded it above */
     276         [ +  + ]:         11 :     if (Status != CFE_TBL_INFO_UPDATED)
     277                 :            :     {
     278                 :          3 :         CFE_EVS_SendEvent(HK_CPTBL_GADR_ERR_EID, CFE_EVS_EventType_ERROR, "Error Getting Adr for Cpy Tbl,RC=0x%08X",
     279                 :            :                           (unsigned int)Status);
     280                 :          3 :         return Status;
     281                 :            :     }
     282                 :            : 
     283                 :          8 :     Status = CFE_TBL_GetAddress((void *)(&HK_AppData.RuntimeTablePtr), HK_AppData.RuntimeTableHandle);
     284         [ +  + ]:          8 :     if (Status != CFE_SUCCESS)
     285                 :            :     {
     286                 :          1 :         CFE_EVS_SendEvent(HK_RTTBL_GADR_ERR_EID, CFE_EVS_EventType_ERROR,
     287                 :            :                           "Error Getting Adr for Runtime Table,RC=0x%08X", (unsigned int)Status);
     288                 :          1 :         return Status;
     289                 :            :     }
     290                 :            : 
     291                 :          7 :     Status = HK_ProcessNewCopyTable(HK_AppData.CopyTablePtr, HK_AppData.RuntimeTablePtr);
     292                 :            : 
     293         [ +  + ]:          7 :     if (Status != CFE_SUCCESS)
     294                 :            :     {
     295                 :            :         /* If status was not success, then the copy table function received a NULL pointer argument */
     296                 :          1 :         CFE_EVS_SendEvent(HK_NEWCPYTBL_INIT_FAILED_EID, CFE_EVS_EventType_ERROR,
     297                 :            :                           "Process New Copy Table Failed, status = 0x%08X", (unsigned int)Status);
     298                 :            : 
     299                 :          1 :         return Status;
     300                 :            :     }
     301                 :            : 
     302                 :          6 :     return CFE_SUCCESS;
     303                 :            : } /* HK_TableInit */
     304                 :            : 
     305                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     306                 :            : /*                                                                 */
     307                 :            : /* Send Combined Housekeeping Packet                               */
     308                 :            : /*                                                                 */
     309                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     310                 :          1 : void HK_SendCombinedPktCmd(const CFE_SB_Buffer_t *BufPtr)
     311                 :            : {
     312                 :            :     const HK_SendCombinedPkt_Payload_t *CmdPtr;
     313                 :            : 
     314                 :          1 :     CmdPtr = &((const HK_SendCombinedPktCmd_t *)BufPtr)->Payload;
     315                 :            : 
     316                 :          1 :     HK_SendCombinedHkPacket(CmdPtr->OutMsgToSend);
     317                 :          1 : }
     318                 :            : 
     319                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     320                 :            : /*                                                                 */
     321                 :            : /* Housekeeping request                                            */
     322                 :            : /*                                                                 */
     323                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     324                 :          1 : void HK_SendHkCmd(const CFE_SB_Buffer_t *BufPtr)
     325                 :            : {
     326                 :            :     HK_HkTlm_Payload_t *PayloadPtr;
     327                 :            : 
     328                 :          1 :     PayloadPtr = &HK_AppData.HkPacket.Payload;
     329                 :            : 
     330                 :            :     /* copy data into housekeeping packet */
     331                 :          1 :     PayloadPtr->CmdCounter          = HK_AppData.CmdCounter;
     332                 :          1 :     PayloadPtr->ErrCounter          = HK_AppData.ErrCounter;
     333                 :          1 :     PayloadPtr->MissingDataCtr      = HK_AppData.MissingDataCtr;
     334                 :          1 :     PayloadPtr->CombinedPacketsSent = HK_AppData.CombinedPacketsSent;
     335                 :          1 :     PayloadPtr->MemPoolHandle       = HK_AppData.MemPoolHandle;
     336                 :            : 
     337                 :            :     /* Send housekeeping telemetry packet...        */
     338                 :          1 :     CFE_SB_TimeStampMsg(CFE_MSG_PTR(HK_AppData.HkPacket.TelemetryHeader));
     339                 :          1 :     CFE_SB_TransmitMsg(CFE_MSG_PTR(HK_AppData.HkPacket.TelemetryHeader), true);
     340                 :          1 : }
     341                 :            : 
     342                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     343                 :            : /*                                                                 */
     344                 :            : /* Noop command                                                    */
     345                 :            : /*                                                                 */
     346                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     347                 :          1 : void HK_NoopCmd(const CFE_SB_Buffer_t *BufPtr)
     348                 :            : {
     349                 :          1 :     CFE_EVS_SendEvent(HK_NOOP_CMD_EID, CFE_EVS_EventType_INFORMATION, "HK No-op command, Version %d.%d.%d.%d",
     350                 :            :                       HK_MAJOR_VERSION, HK_MINOR_VERSION, HK_REVISION, HK_MISSION_REV);
     351                 :            : 
     352                 :          1 :     HK_AppData.CmdCounter++;
     353                 :          1 : }
     354                 :            : 
     355                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     356                 :            : /*                                                                 */
     357                 :            : /* Reset counters command                                          */
     358                 :            : /*                                                                 */
     359                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     360                 :          1 : void HK_ResetCountersCmd(const CFE_SB_Buffer_t *BufPtr)
     361                 :            : {
     362                 :          1 :     HK_ResetHkData();
     363                 :          1 :     CFE_EVS_SendEvent(HK_RESET_CNTRS_CMD_EID, CFE_EVS_EventType_DEBUG, "HK Reset Counters command received");
     364                 :          1 : }
     365                 :            : 
     366                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     367                 :            : /*                                                                 */
     368                 :            : /* Reset housekeeping data                                         */
     369                 :            : /*                                                                 */
     370                 :            : /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     371                 :         10 : void HK_ResetHkData(void)
     372                 :            : {
     373                 :         10 :     HK_AppData.CmdCounter          = 0;
     374                 :         10 :     HK_AppData.ErrCounter          = 0;
     375                 :         10 :     HK_AppData.CombinedPacketsSent = 0;
     376                 :         10 :     HK_AppData.MissingDataCtr      = 0;
     377                 :         10 : }
     378                 :            : 
     379                 :            : /************************/
     380                 :            : /*  End of File Comment */
     381                 :            : /************************/

Generated by: LCOV version 1.14