/************************************************************************** * * Copyright (c) 2026 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file BPModule.c * * @author (last) Varshini Nagabooshanam * @date (last) 18-May-2026 * * @author (original) Varshini Nagabooshanam * @date (original) 18-May-2026 * ***************************************************************************/ #include "AlarmDefs.h" #include "AlarmMgmtTD.h" #include "BPDriver.h" #include "BPModule.h" #include "FpgaTD.h" /** * @addtogroup BPModule * @{ */ // ********** private definitions ********** // TODO remove these when institutional settings are available #define BP_SYSTOLIC_LOW_LIMIT 90 ///< Low systolic blood pressure threshold in mmHg #define BP_SYSTOLIC_HIGH_LIMIT 180 ///< High systolic blood pressure threshold in mmHG #define BP_HEART_RATE_LOW_LIMIT 40 ///< Low heart rate threshold in BPM #define BP_HEART_RATE_HIGH_LIMIT 180 ///< High heart rate threshold in BPM #define BP_LOW_SYSTOLIC_MAX_COUNT 3 ///< Maximum consecutive low systolic pressure events. /// Blood pressure module states. typedef enum { BP_MODULE_IDLE_STATE = 0, ///< Blood Pressure idle state. BP_MODULE_MEASURE_STATE, ///< Blood pressure measurement state BP_MODULE_CHECK_STATE, ///< Blood pressure validation state NUM_OF_BP_MODULE_STATES ///< Number of blood pressure module states. } BP_MODULE_STATE_T; // ********** private data ********** static BP_MODULE_STATE_T bpModuleState = BP_MODULE_IDLE_STATE; ///< Current blood pressure module state static U08 lowSystolicCount; ///< Consecutive low systolic pressure counter static BOOL requestAdultBPReading; ///< Adult BP reading request static BOOL requestPedsBPReading; ///< Pediatric BP reading request static BOOL requestAbortBPReading; ///< Abort BP reading request // ********** private function prototypes ********** static void checkBloodPressureReading( BP_RESULTS_T *bpResults ); static BP_MODULE_STATE_T handleBPModuleIdleState( void ); static BP_MODULE_STATE_T handleBPModuleMeasureState( void ); static BP_MODULE_STATE_T handleBPModuleCheckState( void ); /*********************************************************************//** * @brief * The initBPModule function initializes the blood pressure module. * @details \b Inputs: none * @details \b Outputs: BPModule variables initialized * @return none ***************************************************************************/ void initBPModule( void ) { initBPDriver(); bpModuleState = BP_MODULE_IDLE_STATE; lowSystolicCount = 0; requestAdultBPReading = FALSE; requestPedsBPReading = FALSE; requestAbortBPReading = FALSE; } /*********************************************************************//** * @brief * The execBPModule function executes the blood pressure module state * machine. * @details \b Inputs: bpModuleState * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ void execBPModule( void ) { execBPDriver(); switch ( bpModuleState ) { case BP_MODULE_IDLE_STATE: bpModuleState = handleBPModuleIdleState(); break; case BP_MODULE_MEASURE_STATE: bpModuleState = handleBPModuleMeasureState(); break; case BP_MODULE_CHECK_STATE: bpModuleState = handleBPModuleCheckState(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_TD_SOFTWARE_FAULT, SW_FAULT_ID_TD_BP_MODULE_STATE, bpModuleState ); bpModuleState = BP_MODULE_IDLE_STATE; break; } } /*********************************************************************//** * @brief * The initiateAdultBPReading function initiates an adult blood pressure * measurement. * @details \b Inputs: none * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ void initiateAdultBPReading( void ) { requestAdultBPReading = TRUE; } /*********************************************************************//** * @brief * The initiatePedsBPReading function initiates a pediatric blood pressure * measurement. * @details \b Inputs: none * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ void initiatePedsBPReading( void ) { requestPedsBPReading = TRUE; } /*********************************************************************//** * @brief * The abortBPReading function aborts the active blood pressure * measurement. * @details \b Inputs: none * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ void abortBPReading( void ) { requestAbortBPReading = TRUE; } /*********************************************************************//** * @brief * The checkBloodPressureReading function validates blood pressure * measurement results against configured thresholds and activates * corresponding blood pressure alarms. * @details \b Inputs: bpResults * @details \b Outputs: activate BP alarms * @return none ***************************************************************************/ static void checkBloodPressureReading( BP_RESULTS_T *bpResults ) { if ( NULL != bpResults ) { if ( bpResults->systolic <= BP_SYSTOLIC_LOW_LIMIT ) { lowSystolicCount++; if ( lowSystolicCount >= BP_LOW_SYSTOLIC_MAX_COUNT ) { activateAlarm( ALARM_ID_TD_BP_SYSTOLIC_LOW_TOO_MANY ); } else { activateAlarm( ALARM_ID_TD_BP_SYSTOLIC_LOW ); } } if ( bpResults->systolic >= BP_SYSTOLIC_HIGH_LIMIT ) { activateAlarm( ALARM_ID_TD_BP_SYSTOLIC_HIGH ); } if ( bpResults->heartRate <= BP_HEART_RATE_LOW_LIMIT ) { activateAlarm( ALARM_ID_TD_BP_HEART_RATE_LOW ); } if ( bpResults->heartRate >= BP_HEART_RATE_HIGH_LIMIT ) { activateAlarm( ALARM_ID_TD_BP_HEART_RATE_HIGH ); } } } /*********************************************************************//** * @brief * The handleBPModuleIdleState function executes the BP module idle * state handling. * @details \b Inputs: requestAdultBPReading, requestPedsBPReading, * requestAbortBPReading * @details \b Outputs: flags * @return next BP module state ***************************************************************************/ static BP_MODULE_STATE_T handleBPModuleIdleState( void ) { BP_MODULE_STATE_T nextState = BP_MODULE_IDLE_STATE; if ( TRUE == requestAdultBPReading ) { requestAdultBPReading = FALSE; startAdultBPMeasurement(); nextState = BP_MODULE_MEASURE_STATE; } else if ( TRUE == requestPedsBPReading ) { requestPedsBPReading = FALSE; startPedsBPMeasurement(); nextState = BP_MODULE_MEASURE_STATE; } else if ( TRUE == requestAbortBPReading ) { requestAbortBPReading = FALSE; abortBPMeasurement(); } return nextState; } /*********************************************************************//** * @brief * The handleBPModuleMeasureState function executes the BP module * measurement state handling. * @details \b Inputs: results * @details \b Outputs: bpModuleState * @return next BP module state ***************************************************************************/ static BP_MODULE_STATE_T handleBPModuleMeasureState( void ) { BP_MODULE_STATE_T nextState = BP_MODULE_MEASURE_STATE; if ( TRUE == isBPMeasurementReady() ) { nextState = BP_MODULE_CHECK_STATE; } else if ( TRUE == hasBPDriverError() ) { nextState = BP_MODULE_IDLE_STATE; activateAlarm( ALARM_ID_TD_BP_MODULE_ERROR ); } return nextState; } /*********************************************************************//** * @brief * The handleBPModuleCheckState function executes the BP module * validation state handling. * @details \b Inputs: BP driver measurement results * @details \b Outputs: BP alarms * @return next BP module state. ***************************************************************************/ static BP_MODULE_STATE_T handleBPModuleCheckState( void ) { BP_MODULE_STATE_T nextState = BP_MODULE_IDLE_STATE; BP_RESULTS_T bpResults; if( TRUE == getBPResults( &bpResults ) ) { checkBloodPressureReading( &bpResults ); } return nextState; } /**@}*/