/*
 * CANCore_DataSync.c
 *
 *  Created on: Nov 24, 2024
 *      Author: tummalanr
 */

/**
* \brief Calling Initialization Functions for IPC interface with CAN core.
*
* \file AIOModuleReadWrite.c
* contains  Functions for Initializing IPC with CAN core.
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>

#include <inttypes.h>
#include <kernel/dpl/DebugP.h>
#include <kernel/dpl/AddrTranslateP.h>
#include <kernel/dpl/QueueP.h>
#include <kernel/dpl/EventP.h>
#include <kernel/dpl/SemaphoreP.h>
#include <kernel/dpl/ClockP.h>
#include <drivers/spinlock.h>
#include <drivers/ipc_notify.h>

#include    "IPCDataSynch.h"
#include    "IPCDataSyncGlobal.h"
#include    "SharedMemoryExterns.h"
#include    "SystemStsConfig.h"

#define             ACyclic_IOModule_UpdateEvnt_BIT         (0x000100u)
#define             ACyclic_PZxModule_UpdateEvnt_BIT        (0x000080u)

uint16_t MBN_IPC_ClientId = 6;
/*!
 *  <!-- Description: -->
 *
 *  \brief
 *
 *   IPC Notify Callback to Notify Message Received From CAN Core for System Status and Configuration
 *
 *  <!-- Parameters and return values: -->
 *  \param[in]  remoteCoreId    - RemoteCoreId from where Message Received
 *              localClientId   - Local ClientId on which Application Core is waiting for Messages from CAN Core
 *              msgValue        - CYCLICMSG_NOTIFY_ID : Cyclic Msg Received / ACYCLICMSG_NOTIFY_ID : ACyclic Msg Received
 *              crcStatus       - Not Used
 *              *args           - Not Used
 *  \return     None.
 */
void ipc_notify_msg_handler_sysstsconfig(uint32_t remoteCoreId, uint16_t localClientId, uint32_t msgValue, int32_t crcStatus, void *args)
{
    if(remoteCoreId == CSL_CORE_ID_R5FSS1_0)
    {
            if(sysstscnfgreqidindex == SHM_Max_ReqIds) {
                sysstscnfgreqidindex = 0;
            }
            SystemStatusConfigReqIDs[sysstscnfgreqidindex++] = msgValue;

            int32_t status = EventP_setBits(&gSysConfigSts_DataRDWRSynchEvent, System_StatusConfig_RDWREvnt_BIT);
            (void)status;
    }

    return;
}

uint32_t ipciocoreid = 0;
uint16_t ipcioclientid = 0;
uint32_t ipciomsgid = 0;

/*!
 *  <!-- Description: -->
 *
 *  \brief
 *
 *   IPC Notify Callback to Notify Message Received From CAN Core for IO Modules
 *
 *  <!-- Parameters and return values: -->
 *  \param[in]  remoteCoreId    - RemoteCoreId from where Message Received
 *              localClientId   - Local ClientId on which Application Core is waiting for Messages from CIP Core
 *              msgValue        - CYCLICMSG_NOTIFY_ID : Cyclic Msg Received / ACYCLICMSG_NOTIFY_ID : ACyclic Msg Received
 *              crcStatus       - Not Used
 *              *args           - Not Used
 *  \return     None.
 */
void ipc_notify_msg_handler_iomodules(uint32_t remoteCoreId, uint16_t localClientId, uint32_t msgValue, int32_t crcStatus, void *args)
{

    if(remoteCoreId == CSL_CORE_ID_R5FSS1_0)
    {

                if(ioacyclicreqidindex >= SHM_Max_ReqIds) {
                        ioacyclicreqidindex = 0;
                }
                IOAcyclicReqIDs[ioacyclicreqidindex++] = msgValue;

                int32_t status = EventP_setBits(&gACyclic_IOModule_SynchEvent, ACyclic_IOModule_UpdateEvnt_BIT);
                (void)status;
    }

    ipciocoreid = remoteCoreId;
    ipcioclientid = localClientId;
    ipciomsgid = msgValue;

    return;
}

/*!
 *  <!-- Description: -->
 *
 *  \brief
 *
 *   IPC Notify Callback to Notify Message Received From CAN Core for PressureZone Modules
 *
 *  <!-- Parameters and return values: -->
 *  \param[in]  remoteCoreId    - RemoteCoreId from where Message Received
 *              localClientId   - Local ClientId on which Application Core is waiting for Messages from CIP Core
 *              msgValue        - CYCLICMSG_NOTIFY_ID : Cyclic Msg Received / ACYCLICMSG_NOTIFY_ID : ACyclic Msg Received
 *              crcStatus       - Not Used
 *              *args           - Not Used
 *  \return     None.
 */
void ipc_notify_msg_handler_pzxmodules(uint32_t remoteCoreId, uint16_t localClientId, uint32_t msgValue, int32_t crcStatus, void *args)
{
    if(msgValue & ACYCLICTYPE_NOTIFY_ID_MASK)
    {
            if(pzxacyclicreqidindex >= SHM_Max_ReqIds) {
                pzxacyclicreqidindex = 0;
            }
            PZxAcyclicReqIDs[pzxacyclicreqidindex++] = msgValue;

            int32_t status = EventP_setBits(&gACyclic_PZxModule_SynchEvent, ACyclic_PZxModule_UpdateEvnt_BIT);
            (void)status;
    }

}

/*!
 *  <!-- Description: -->
 *
 *  \brief
 *
 *   IPC Notify Callback to Notify Messsage Received From CIP Core for MBN info
 *
 *  <!-- Parameters and return values: -->
 *  \param[in]  remoteCoreId    - RemoteCoreId from where Message Received
 *              localClientId   - Local ClientId on which Application Core is waiting for Messages from CIP Core
 *              msgValue        - MsgId Received
 *              crcStatus       - Not Used
 *              *args           - Not Used
 *  \return     None.
 */
void ipc_notify_msg_handler_mbninfo(uint32_t remoteCoreId, uint16_t localClientId, uint32_t msgValue, int32_t crcStatus, void *args)
{

    mbnreqrecvd = msgValue;
    SemaphoreP_post(&mbn_datasyncObj);

    return;
}

/*!
 *  <!-- Description: -->
 *
 *  \brief
 *
 *   IPC Initialization With CAN Core.
 *
 *  <!-- Parameters and return values: -->
 *  \param[in]  :   None
 *  \return     :   AppRET_T    on success AppRET_OK otherwise Error.
 */
AppRET_T App_CANCoreDataSync_Init(void)
{
    int32_t     status;

    //System Configuartion and status CallBack Registration for notifying messages Received from CIP
    status = IpcNotify_registerClient(SysStatusConfig_IPC_ClientId, &ipc_notify_msg_handler_sysstsconfig, NULL);
    DebugP_assert(status==SystemP_SUCCESS);

    //IO Modules CallBack Registration for notifying messages Received from CIP
    status = IpcNotify_registerClient(IOModules_IPC_ClientId, &ipc_notify_msg_handler_iomodules, NULL);
    DebugP_assert(status==SystemP_SUCCESS);

    //PZx Modules CallBack Registration for notifying messages Received from CIP
    status = IpcNotify_registerClient(PZxModules_IPC_ClientId, &ipc_notify_msg_handler_pzxmodules, NULL);
    DebugP_assert(status==SystemP_SUCCESS);

    //MBN Data Acces Response CallBack Registration for notifying messages Received from CIP
    status = IpcNotify_registerClient(MBN_IPC_ClientId, &ipc_notify_msg_handler_mbninfo, NULL);
    DebugP_assert(status==SystemP_SUCCESS);


    #ifdef Debug_Verbose
        DebugP_log("[CIP] IpcNotify_syncAll(SystemP_WAIT_FOREVER); Before[%" PRId64 " ms] \r\n",(ClockP_getTimeUsec()/1000u));
    #endif

    /* Wait for all cores to start */
    IpcNotify_syncAll(SystemP_WAIT_FOREVER);

    #ifdef Debug_Verbose
        DebugP_log("[CIP] After IpcNotify_syncAll(SystemP_WAIT_FOREVER);[%" PRId64 " ms]\r\n",(ClockP_getTimeUsec()/1000u));
    #endif



    return AppRET_OK;
}


