/************************************************************************** * * Copyright (c) 2024-2024 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 TDInterface.c * * @author (last) Vinayakam Mani * @date (last) 28-Oct-2024 * * @author (original) Vinayakam Mani * @date (original) 28-Oct-2024 * ***************************************************************************/ #include "DialysatePumps.h" #include "Messaging.h" #include "MessagePayloads.h" #include "ModeGenDialysate.h" #include "ModeInitPOST.h" #include "ModeStandby.h" #include "OperationModes.h" #include "PersistentAlarm.h" #include "SystemCommDD.h" #include "TaskGeneral.h" #include "TDInterface.h" #include "Timers.h" /** * @addtogroup TDInterface * @{ */ // ********** private definitions ********** #define TD_DATA_FRESHNESS_TIMEOUT_MS ( 3 * MS_PER_SECOND ) ///< TD data freshness timeout (in ms). // ********** private data ********** // TD status static TD_OP_MODE_T tdCurrentOpMode; ///< Current TD operation mode. static U32 tdSubMode; ///< Current state (sub-mode) of current TD operation mode. static F32 tdDialysateFlowrate; ///< TD dialysate flow rate static F32 tdUFRate; ///< TD ultrafilteration rate static F32 tdTargetDialysateTemp; ///< TD target dialysate temperature static BOOL tdDialyzerBypass; ///< TD dialyzer bypass static DD_ACID_TYPES_T tdAcidType; ///< TD Acid type. static DD_BICARB_TYPES_T tdBicarbType; ///< TD Bicarb type. static BOOL tdOpModeDataFreshFlag = FALSE; ///< Flag to signal/process fresh TD op mode data // ********** private function prototypes ********** static void checkTDDataFreshness( ALARM_ID_T alarmID, BOOL *tdFreshDataFlag ); /*********************************************************************//** * @brief * The initTDInterface function initializes the TD Interface unit. * @details \b Inputs: none * @details \b Outputs: TD Interface unit initialized. * @return none *************************************************************************/ void initTDInterface( void ) { // Initialize unit variables tdCurrentOpMode = MODE_INIT; tdSubMode = 0; tdDialysateFlowrate = MAX_DIALYSATE_FLOW_RATE; // Will update later based on the TD value tdUFRate = 0.0F; tdTargetDialysateTemp = 0.0F; tdDialyzerBypass = FALSE; tdAcidType = ACID_08_1251_1; tdBicarbType = BICARB_08_677753_0; } /**********************************************************************//** * @brief * The checkTDDataFreshness function checks the freshness of data coming from * the TD sub-system. * @details \b Alarm: Given alarm is triggered if TD is communicating but has * not published new data for too long. * @details \b Inputs: TD communicating flag * @details \b Outputs: none * @param alarm ID of alarm to check * @param tdFreshDataFlag Pointer to flag indicating whether new data has been * received since last time this function has seen it. * @return None *************************************************************************/ static void checkTDDataFreshness( ALARM_ID_T alarmID, BOOL *tdFreshDataFlag ) { if ( TRUE == *tdFreshDataFlag ) { *tdFreshDataFlag = FALSE; checkPersistentAlarm( alarmID, FALSE, 0.0, 0.0 ); } else { // Alarm if not receiving TD fresh data message in timely manner if ( TRUE == isTDCommunicating() ) { checkPersistentAlarm( alarmID, TRUE, 0.0, 0.0 ); } else { checkPersistentAlarm( alarmID, FALSE, 0.0, 0.0 ); } } } /*********************************************************************//** * @brief * The execTDInterfaceMonitor function executes the TD Interface monitoring * function. Ensures TD is sending fresh data in a timely manner. * @details \b Inputs: none * @details \b Outputs: none * @return none *************************************************************************/ void execTDInterfaceMonitor( void ) { } /*********************************************************************//** * @brief * The getTDOpMode function gets the current latest reported TD operating mode. * @details \b Inputs: tdCurrentOpMode * @details \b Outputs: none * @return Latest reported TD operating mode. *************************************************************************/ TD_OP_MODE_T getTDOpMode( void ) { return tdCurrentOpMode; } /*********************************************************************//** * @brief * The getTDSubMode function gets the latest reported TD operating sub-mode. * @details \b Inputs: tdSubMode * @details \b Outputs: none * @return Latest reported TD operating sub-mode. *************************************************************************/ U32 getTDSubMode( void ) { return tdSubMode; } /*********************************************************************//** * @brief * The setTDOpMode function sets the latest TD operating mode reported by * the TD (called by TD published message handler). * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT if reported TD mode is invalid. * @details \b Inputs: none * @details \b Outputs: tdCurrentOpMode, ddSubMode, tdOpModeDataFreshFlag * @param opMode The operating mode reported by DD * @param subMode The sub-mode of operating mode reported by TD * @return none *************************************************************************/ void setTDOpMode( U32 opMode, U32 subMode ) { if ( opMode < NUM_OF_MODES ) { // update TD op mode and sub-mode tdCurrentOpMode = (TD_OP_MODE_T)opMode; tdSubMode = subMode; } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_TD_OPERATING_MODE, opMode ); } tdOpModeDataFreshFlag = TRUE; } /*********************************************************************//** * @brief * The setTDDialysateFlowrate function sets the latest TD dialysate flow * rate. * @details \b Inputs: none * @details \b Outputs: tdDialysateFlowrate * @param dialysate flow rate. * @return none. *************************************************************************/ void setTDDialysateFlowrate( F32 dialFlowrate ) { tdDialysateFlowrate = dialFlowrate; } /*********************************************************************//** * @brief * The setTDUFRate function sets the latest TD UF rate. * @details \b Inputs: none * @details \b Outputs: tdUFRate * @param ultrafilteration flow rate. * @return none. *************************************************************************/ void setTDUFRate( F32 ufRate ) { tdUFRate = ufRate; } /*********************************************************************//** * @brief * The setTDTargetDialysateTemperature function sets the latest TD dialysate * temperature. * @details \b Inputs: none * @details \b Outputs: tdTargetDialysateTemp * @param Target dialysate temperature. * @return none. *************************************************************************/ void setTDTargetDialysateTemperature( F32 dialTemperature ) { tdTargetDialysateTemp = dialTemperature; } /*********************************************************************//** * @brief * The setTDDialyzerBypass function sets the latest TD dialyzer bypass * enable. * @details \b Inputs: none * @details \b Outputs: tdDialyzerBypass * @param Dialyzer Bypass enable. * @return none. *************************************************************************/ void setTDDialyzerBypass( F32 dialBypass ) { tdDialyzerBypass = dialBypass; } /*********************************************************************//** * @brief * The setTDAcidAndBicarbType function sets the acid and bicarb types to be * used in dialysate generation. * @details \b Inputs: none * @details \b Outputs: tdAcidType,tdBicarbType * @param acid which is the type of acid * @param bicarb which is the type of bicarb * @return none *************************************************************************/ void setTDAcidAndBicarbType( U32 acid, U32 bicarb ) { tdAcidType = (DD_ACID_TYPES_T)acid; tdBicarbType = (DD_BICARB_TYPES_T)bicarb; } /*********************************************************************//** * @brief * The getTDDialysateFlowrate function gets the latest TD dialysate flow * rate. * @details \b Inputs: tdDialysateFlowrate * @details \b Outputs: none * @return Latest TD dialysate flow rate. *************************************************************************/ F32 getTDDialysateFlowrate( void ) { return tdDialysateFlowrate; } /*********************************************************************//** * @brief * The getTDUFrate function gets the latest TD ultrafilteration flow * rate. * @details \b Inputs: tdUFRate * @details \b Outputs: none * @return Latest UF rate. *************************************************************************/ F32 getTDUFRate( void ) { return tdUFRate; } /*********************************************************************//** * @brief * The getTDTargetDialysateTemperature function gets the latest TD * target dialysate temperature rate. * @details \b Inputs: tdTargetDialysateTemp * @details \b Outputs: none * @return Latest target dialysate temperature. *************************************************************************/ F32 getTDTargetDialysateTemperature( void ) { return tdTargetDialysateTemp; } /*********************************************************************//** * @brief * The getTDDialyzerBypass function gets the latest TD dailyzer bypass valve * enable flag. * @details \b Inputs: tdDialyzerBypass * @details \b Outputs: none * @return Latest dialyzer bypass valve enable. *************************************************************************/ BOOL getTDDialyzerBypass( void ) { return tdDialyzerBypass; } /*********************************************************************//** * @brief * The getTDAcidConcentrateType function gets the latest Acid concentrate * type. * @details \b Inputs: tdAcidType * @details \b Outputs: none * @return Latest acid concentrate type. *************************************************************************/ DD_ACID_TYPES_T getTDAcidConcentrateType( void ) { return tdAcidType; } /*********************************************************************//** * @brief * The getTDBicarbConcentrateType function gets the latest Bicarb concentrate * type. * @details \b Inputs: tdBicarbType * @details \b Outputs: none * @return Latest bicarb concentrate type. *************************************************************************/ DD_BICARB_TYPES_T getTDBicarbConcentrateType( void ) { return tdBicarbType; } /*********************************************************************//** * @brief * The handleDialysateDeliveryRequestMsg function handles a dailysate * delivery request from TD and updates dialysate flowrate, UF rate, * dialysate temperature, dialyzer bypass and concentrate types ( acid * and bicarb types). * @details Inputs: none * @details Outputs: message handled * @param message a pointer to the message to handle * @return TRUE if message is sucessfully parsed, FALSE if not. *************************************************************************/ BOOL handleDialysateDeliveryRequestMsg( MESSAGE_T *message ) { BOOL result = FALSE; if ( message->hdr.payloadLen == sizeof( DIALYSATE_DELIVERY_REQ_PAYLOAD_T ) ) { DIALYSATE_DELIVERY_REQ_PAYLOAD_T startTxRequest; DD_OP_MODE_T ddMode = getCurrentOperationMode(); memcpy( &startTxRequest, message->payload, sizeof( DIALYSATE_DELIVERY_REQ_PAYLOAD_T ) ); // Process the dialysate delivery request message if ( ( DD_MODE_STAN == ddMode ) && ( TRUE == startTxRequest.start ) ) { // Set dialysate flow rate, UF rate and dialysate temperature setTDDialysateFlowrate( startTxRequest.dialRate ); setTDUFRate( startTxRequest.ufRate ); setTDTargetDialysateTemperature( startTxRequest.dialTemp ); // Set concentrate types, Bypass dialyzer setTDAcidAndBicarbType( startTxRequest.acidType, startTxRequest.bicarbType ); setTDDialyzerBypass( startTxRequest.bypassDialyzer ); // start dialysate generation result = requestDDStart(); } else if ( DD_MODE_GEND == ddMode ) { if ( FALSE == startTxRequest.start ) { // stop dialysate generation by transitioning to standby mode requestNewOperationMode( DD_MODE_STAN ); } else { // Set dialysate flow rate, UF rate and dialysate temperature setTDDialysateFlowrate( startTxRequest.dialRate ); setTDUFRate( startTxRequest.ufRate ); setTDTargetDialysateTemperature( startTxRequest.dialTemp ); // Set concentrate types, Bypass dialyzer setTDAcidAndBicarbType( startTxRequest.acidType, startTxRequest.bicarbType ); setTDDialyzerBypass( startTxRequest.bypassDialyzer ); // Signal to update treatement parameters setTreatmentParamUpdate(); } result = TRUE; } } sendAckResponseMsg( (MSG_ID_T)message->hdr.msgID, COMM_BUFFER_OUT_CAN_DD_2_TD, result ); return result; } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /**@}*/