/*
 * AppGlobal.h
 *
 *  Created on: Nov 7, 2025
 *      Author: tummalanr
 */

#ifndef APPGLOBAL_H_
#define APPGLOBAL_H_

#include <kernel/dpl/ClockP.h>

/**
* Debug Statements Enable / Disable flag.
*/

#define             Debug_Verbose               1


/**
* Enable/Disable MCAN0 Channel
*/
#define         APP_MCAN0                   1u

/**
* Enable/Disable MCAN1 Channel
*/
#define         APP_MCAN1                   1u

/**
* MCAN Channel Number used in Application Calls
*/
#define         App_Line0                   0u
#define         App_Line1                   1u
#define         App_LineNone                0xFFu



/**
* Maximum Valve Node  Modules can be assembled. (MAX_ISEM_BCKPLANE(10) + MAX_INTRPROP_BCKPLANE(10) + MAX_EXTRPROP_BCKPLANE(16))
*/
#define                 MAX_VALVE_NODES                 36u


/**
 * @ RTOS User MessageQueue Send and Receive Queue access Time out.
 */
#define         MSGQUEUE_SEND_TO                pdMS_TO_TICKS( 2U )
#define         MSGQUEUE_RECV_TO                pdMS_TO_TICKS( 2U )

#define         Delay_Us_Ticks(dlydur)          (ClockP_usecToTicks(dlydur))
#define         Delay_Ms_Ticks(dlydur)          (ClockP_usecToTicks((dlydur )* 1000u))


/**
* USDO Request  Write Message Queue length.
*/
#define     MAXUSDOWRMSGQLEN                32u
/**
* USDO Response  Read Message Queue length.
*/
#define     MAXUSDORDMSGQLEN                32u

/**
* Maximum MessageQueue size for  USDO Write Resp Verify Queue in USDO tasks
*/
#define     MAXUSDOWRRSPVERFYLEN            32u

/**
* Maximum MessageQueue size for  USDO Read Resp Verify Queue in USDO tasks
*/
#define     MAXUSDORDRSPVERFYLEN            32u

/**
* Stack ObjectDictonery(OD)  Write Message Queue length.
*/
#define     MAXODWRMSGQLEN                  32u
/**
* Stack ObjectDictonery(OD)  Read Message Queue length.
*/
#define     MAXODRDMSGQLEN                  64u

/**
* Shared Memory  Write Message Queue length.
*/
#define                 MAXSHMWRMSGQLEN                 32u

/**
* CANFD Stack USDO message Read Timeout to wait.    250ms
*/
#define     USDO_READ_TO                    pdMS_TO_TICKS( 100u )
/**
* CANFD Stack USDO message Write Timeout to wait.   250ms
*/
#define     USDO_WRITE_TO                   pdMS_TO_TICKS( 100u )
/**
* CANFD Stack USDO Write Response Timeout to wait.   5ms
*/
#define     USDO_WRITERESP_TO               pdMS_TO_TICKS( 5u )

/**
 * AI Module Max samples per channel as per configuration
 */
#define    AI_MAXSAMPLESPERCHANNEL                   3u


/**
*  Modules Discovery Completed Success.
*/
#define     ModuleDiscovery_Success             0x00u
/**
*  Modules Discovery Completed Partially.
*/
#define     ModuleDiscovery_Partial             0x01u
/**
*  Modules Discovery Not Matching with Configured Value.
*/
#define    ModuleDiscovery_NotMatchig          0x02u
/**
*  Modules Discovery Failure.
*/
#define     ModuleDiscovery_Failure             0xFFu

/**
*  CANFD LSS Command Timeout.
*/
#define     LSS_TIMEOUT                         20u

/**
* IO Modules Master Node Id Assigned.
*/
#define     IO_MasterNodeId                     1u
/**
* Valve Modules Master Node Id Assigned.
*/
#define     Valve_MasterNodeId                  30u

/**
 * USDO Requested Client App types ID
 */
#define     CLIENT_APP                          (uint8_t)0x00u
#define     CLIENT_CIP_IO                       (uint8_t)0x10u
#define     CLIENT_GUIAPP                       (uint8_t)0x20u


/*READ or WRITE*/
#define     DATA_WR                     0x00u
#define     DATA_RD                     0x01u

#define             PARAM_NONE          0x00u
#define             PARAM_WRONLY        0x01u
#define             PARAM_RDONLY        0x02u
#define             PARAM_RDWR          0x03u

#define             PARAM_DATANOTAVAIL  0x00u
#define             PARAM_DATAAVAIL     0x01u

#define             INSTANCE_NONE        0x00u

#define             STR_MAX_LEN         16u
/**
 * SpinLock Wait Timeout.
 * 1500 : 1500 * 1.25 ns = 1.875 us
 * 2500 : 2500 * 1.25 ns = 3.125 us
 * 3500 : 3500 * 1.25 ns = 4.375 us
 */
#define     SPINLOCK_GET_TIMEOUT                1500u

/**
 * Event Bits for Cyclic  Group Received to/from CIP
 */
//latter remove below one event
#define     CIPCyclicEventGrp_OutSts_BIT            (uint32_t)( 1u << 0u )             //BIT 0        Cyclic data Output Status Received from CIP (Current Output Cyclic Data) /PDO
#define     CIPCyclicEventGrp_InputSts_BIT          (uint32_t)( 1u << 1u )             //BIT 1        Send Response (Current Input Cyclic Data) to CIP / PDI
#define     CIPCyclicEventGrp_DOPSts_BIT            (uint32_t)( 1u << 2u )             //BIT 2        Cyclic Digital data Output Status Received from CIP
#define     CIPCyclicEventGrp_AOValSts_BIT          (uint32_t)( 1u << 3u )             //BIT 3        Cyclic Analog Value data Output Status Received from CIP
#define     CIPCyclicEventGrp_AOCntSts_BIT          (uint32_t)( 1u << 4u )             //BIT 4        Cyclic Analog Count data Output Status Received from CIP

/**
 * Event Bits for ACyclic Group Received to/ from CIP
 */
#define     CIPACyclicEventGrp_WRReq_BIT            (uint32_t)( 1u << 0u )             //BIT 0        Acyclic Request Received from CIP for Write request
#define     CIPACyclicEventGrp_RDReq_BIT            (uint32_t)( 1u << 1u )             //BIT 1        Acyclic Request Received from CIP for Read request
#define     CIPACyclicEventGrp_Resp_BIT             (uint32_t)( 1u << 2u )             //BIT 2        Send  Acyclic Response to CIP
#define     CIPACyclicEvntGrp_PrmUpdate_BIT         (uint32_t)( 1u << 3u )             //BIT 3        Update  Acyclic  Parameter Data based avail flag


#define     CIPCyclicEvntGrpPZx_STNOP_BIT           (uint32_t)( 1u << 0u )             //BIT 0        Cyclic data Output Status Received from CIP (Current Output Cyclic Data) /PDO
//#define CIPCyclicEvntGrpPZx_StnErr_BIT        ( 1u << 1u )             //BIT 1        Cyclic data Input Status Received from CIP (Current Input Cyclic Data) /PDI
#define CIPCyclicEvtGrpPZx_InputSts_BIT             (uint32_t)( 1u << 1u )             //BIT 1        Cyclic data Input Status Received from backplane (Current Input Cyclic Data) /PDI

#define     CIPCyclEntGrpPZx_IsemDiagOPBIT          (uint32_t)( 1u << 2u )             //BIT 2        Cyclic data ISEM-DIAG Output Status Received from CIP (Current Output Cyclic Data) /PDO
//#define CIPCyclEntGrpPZx_IsemDiagIPBIT        ( 1u << 3u )             //BIT 3        Cyclic data ISEM-DIAG Output Status Received from CIP (Current Input Cyclic Data) /PDI

#define     CIPCyclEntGrpPZx_DiagValOPBIT           (uint32_t)( 1u << 4u )             //BIT 4        Cyclic data DIAG-Val Output Status Received from CIP (Current Output Cyclic Data) /PDO
//#define CIPCyclEntGrpPZx_DiagValIPBIT         ( 1u << 5u )             //BIT 5        Cyclic data DIAG-Val Output Status Received from CIP (Current Input Cyclic Data) /PDI
//#define CIPCyclEntGrpPZx_DiagValErrBIT        ( 1 << 6 )             //BIT 5        Cyclic data DIAG-Val Output Status Received from CIP (Current Input Cyclic Data) /PDI

#define     CIPCyclEntGrpPZx_INTPropOPBIT           (uint32_t)( 1u << 7u )             //BIT 6        Cyclic data INTPROP-Val Output Status Received from CIP (Current Output Cyclic Data) /PDO
#define     CIPCyclEntGrpPZx_PropIPBIT              (uint32_t)( 1u << 8u )             //BIT 7        Cyclic data PROP-Val Output Status Received from CIP (Current Input Cyclic Data) /PDI

#define CIPCyclEntGrpPZx_EXTPropOPBIT            (uint32_t)( 1u << 9u )             //BIT 6        Cyclic data EXT PROP-Val Output Status Received from CIP (Current Output Cyclic Data) /PDO



/**
 * Module Parameter types
 */
#define             CONSTANT_INFORMATION_DATA           (uint8_t)0x01u            //constant  data - one time parameters which will not change iNFORMATION DATA
#define             VARIABLE_CONFIG_DATA                (uint8_t)0x02u            //variable  configuration data - variable parameters which  may change
#define             VARIABLE_OERATIONAL_DATA            (uint8_t)0x04u            //variable OPERATIONAL  data - variable parameters which   may change
#define             VARIABLE_DIAGNOSTICS_DATA           (uint8_t)0x08u            //variable OPERATIONAL  data - variable parameters which   may change
#define             CONSTANT_SERVICE_DATA               (uint8_t)0x10u            //constant  data - one time parameters which will not change SERVICE DATA

#define             DYNAMIC_DATA                        (uint8_t)0x08u            //dynamic  data - dynamic parameters which will change


/**
 * @brief App Func Return typedef defines
 */
typedef enum {
    AppRET_OK,                       //< all ok
    AppRET_GenericErr,               //<  Generic Error

    AppExecCore_Err,                 //Execution Core Error
    AppMCan0_DrvErr,                 //MCan0 Driver Init Error
    AppMCan1_DrvErr,                 //MCan1 Driver Init Error
    AppMCan_StackTmrInitErr,         //MCan Stack Timer Init Error
    AppMCan_StackInitErr,            //MCan Stack Init Error
    AppMCan_StackNMTRegErr,          //MCan Stack NMT Register Error
    AppMCan_ErrCtrlRegErr,           //MCan Stack Err Cntrl Register Error
    AppMCan_USDOSrvRdCBRegErr,       //MCan Stack USDO Server RD Register Error
    AppMCan_USDOSrvChkWrCBRegErr,    //MCan Stack USDO Server Check Write Register Error
    AppMCan_USDOSrvWrCBRegErr,       //MCan Stack USDO Server WR Register Error
    AppMCan_PDORecvdCBRegErr,        //MCan Stack PDO Received Register Error
    AppMCan_BusStateCBRegErr,        //MCan Stack Bus  State Register Error
    AppMCan_ComStateCBRegErr,        //MCan Stack Comm  State Register Error
    AppMCan_EmcyConsmStateCBRegErr,  //MCan Stack Emmergency State Register Error
    AppMCan_EmcyStateCBRegErr,       //MCan Stack Emmergency State Register Error
    AppMCan_USDOClntRdCBRegErr,      //MCan Stack USDO Client RD Register Error
    AppMCan_USDOClntWrCBRegErr,      //MCan Stack USDO Client WR Register Error
    AppMCan_PDOUpdateCBRegErr,       //MCan Stack LSS State Register Error
    AppMCan_LSSStateCBRegErr,        //MCan Stack LSS State Register Error
    AppMCan0_DrvEnableErr,           //MCan0 Driver Enable Error
    AppMCan1_DrvEnableErr,           //MCan1 Driver Enable Error

    AppMCan_USDOEventGrpCreateErr,   //MCan USDO Task Event Group Create Error
    AppMCan_USDOQueueCreateErr,      //MCan USDO RW Queue Create Error

    AppMCan_USDOTaskCreateErr,       //MCan Tasks Create Error

    AppMCan_USDORdTimeOutErr,       //MCan USDO Read Timeout Error
    AppMCan_USDORdProcErr,          //MCan USDO Read Process Error
    AppMCan_USDORdRespErr,          //MCan  USDO Read Resp Error
    AppMCan_USDOWrTimeOutErr,       //MCan USDO Read Timeout Error
    AppMCan_USDOWrProcErr,          //MCan USDO Read Process Error
    AppMCan_USDOWrRespErr,          //MCan  USDO Write Resp Error

    AppMCan_ODUpdateErr,            //OD update Error
    AppMCan_ODEventGrpCreateErr,    //MCan OD Task Event Group Create Error
    AppMCan_ODUpdtQueueCreateErr,   //MCan OD Update RW Queue Create Error
    AppMCan_ODUpdtTaskCreateErr,        //MCan Tasks Create Error

    AppMCan_MainTaskCreateErr,       //MCan MainTask Create Error
    AppMCan_LSSTaskCreateErr,        //LSS task Create Error
    AppMCanHBEMCYMONTaskCreateErr,       //Node HB / Emergency MonitorTask Create Error
    AppMCan_NodeOprSetErr,          //Node Set Operational State Error

    AppMCan0_DiscoveryTimeoutErr,    //MCan0 IO Module discovery Timeout Error
    AppMCan0_DiscoveryErr,          //MCan0 IO Module discovery  Error

    AppMCan1_DiscoveryTimeoutErr,    //MCan1 Valve Module discovery Timeout Error
    AppMCan1_DiscoveryErr,          //MCan1 Valve Module discovery  Error

    AppMCan0_SetHBDurErr,           //MCan0 IO Module Set HB Dur  Error
    AppMCan1_SetHBDurErr,           //MCan1 Valve Module Set HB Dur  Error

    AppPZ_GetModuleInfoErr,        //Get PZ Modules Info  Error

    AppValParaIndex_Err,           //Valve parameter index error

    AppHWTimerCreate_Err,           //SW TimerCreate Error

    AppIODiscoveryList_Err,         //IO Modules Discovered List Save Error
    AppIOReqInst_Err,               //Requested IO Modules Instance Error
    AppIOReqBrdId_Err,               //Requested IO Modules Board Id Error

    AppSharedMem_EventGrpCreateErr,    //AppSharedMem Process Task Event Group Create Error
    AppSharedMem_InitErr,           //Error while Creating and Initializing Shared Memory
    App_SHMWRQueueCreateErr,        //SharedMemory Write Queue Create Error
    AppGetSHMLockErr,               //Get Shared Memory Access Lock Error
    AppGetSHMLockTimeOutErr,        //Get Shared Memory Access Lock Timeout Error
    AppSHMUpdateErr,                //Updating IPC shared memory Error

    APPAcyclicDataNotChanged,       //Acyclic Data of the Module Not changed
    APPAcyclicDataChanged,          //Acyclic Data of the Module changed
    APPAcyclicUnknownPara,          //Unknown Acyclic Data requested
    APPAcyclicProcessErr,           //Acyclic ProcessErr

    APPCyclicDataNotChanged,       //Cyclic Data of the Module Not changed
    APPCyclicDataChanged,          //Cyclic Data of the Module changed

    AppIPC_MsgEventGrpCreateErr,    //IPC Msg Process Task Event Group Create Error
    AppIPC_MsgProcessTaskCreateErr,  //IPC Msg process Task Create Error

    AppMainCoreTaskCreateErr,           //Main Core SharedMemory Update Task Creat Error

    AppGetIPCSHMLockErr,               //Get IPC Shared Memory Access Lock Error
    AppGetIPCSHMLockTimeOutErr,     //Get IPC Shared Memory Access Lock Timeout Error
    AppIPCSHMUpdateErr,             //Updating IPC shared memory Error
    AppMCan_HBEMCYEventGrpCreateErr,    //eVENT gROUP FRO hb mONITOR AND eMCY
    AppMCan_EmcyQueueCreateErr,         //MCan Emergency Msg Queue Create Error
    AppMCan_EmcyMonQueueCreateErr,        //MCan Emcy Monitor Queue Create Error

    //upto 74
    TemperatureSensorNotFound = 75, //Temperature Sensor not found on i2c0

    EepromTestError,               //Eeprom Test Error used in CIP
    EepromWriteError,               //EepromWrite Error used in CIP
    EepromReadError,               //EepromRead Error used in CIP
    EepromCRCError,               //Eeprom CRC Error used in CIP

    ISEMDiagNotPresent,
    DiagValNotPresent,
    App_UnknownErr                  //Unknown Error
}AppRET_T;



/**
 * @brief App Func Return typedef defines
 * Cyclic Data Type for IPC to CIP
 * Note: Do not change this order
 */
typedef enum {
    IPC_DI_STS_DATA  = 1,
    IPC_AIVAL_STS_DATA,
    IPC_AICNT_STS_DATA,
    IPC_IOMODULE_ERR_DATA,

    IPC_PZSTDVALVE_ONOFF_DATA,
    IPC_PZSTA_ERR_DATA,

    IPC_PZISEMDIAG_PDI_DATA,

    IPC_PZDIAGVALVE_PDI_DATA,
    //IPC_PZDIAGVALVE_ERR_DATA,

    IPC_PROP_PDI_DATA,
    //IPC_PROP_OUTPUTPORTPRESSURE,
    IPC_PROP_PDO,
    IPC_PROP_SETPOINTPRESSURE,

    ISEM_PZXWARNSTS_BITS_DATA,
    ISEM_PZXFAULTSTS_BITS_DATA,
    IPC_PZSTA_OPSTS_DATA,

    IPC_DO_STS_DATA,
    IPC_AO_STS_DATA,
    Unknown_Type
}IPC_Cyclic_ParamType;


/**
 * @brief  Module Error State Bits in cur_node_status structure.
 */
typedef enum {
    Module_No_Error             = 0u,
    SetNode_Operatinal_Err,
    SetNodeHB_Dur_Error,
    SetNodeHB_MonitorDur_Err,
    GetNodeHB_Status_Err,
    GetNode_BrdId_Err,
    SetNodePzId_Error,

   Unknown_Err                  = 31u
}Module_ErrState;

/**
 * @brief  System Functional Mode.
 */
typedef enum {
    PRODUCTION_MODE             = 1u,
    COMMISSIONING_MODE,
    OPERATIONAL_MODE,
    SERVICE_MODE,

   Unknown_Mode                = 31u
}System_FunctionalMode;

/**
 * @brief Module Functional State.
 */
typedef enum {
    MODULEPREOPERATIONAL_STATE  = 1u,
    MODULEOPERATIONAL_STATE,

   Unknown_STATE                = 31u
}Module_FunctionalState;


//AppTimer.c
extern uint32_t Timeout5ms_Timer;

extern AppRET_T App_Timer_Init(void);
extern void App_Timer_Start(void);


extern void Display_TaskInfo(TaskHandle_t taskhndl, uint8_t alltasks);
extern void App_errorHandler(AppRET_T errorCode);

#endif /* APPGLOBAL_H_ */
