/**
 * @file  MainCoreGlobal.h
 * @brief This file contains
 *  Created on: 03-Jun-2025
 *      Author: ubuntu22
 */

#ifndef HEADER_MAINCOREGLOBAL_H_
#define HEADER_MAINCOREGLOBAL_H_


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <inttypes.h>


#define SPINLOCKNO_MBN_INFO                          (34u)

/* -------------------------------------------------------------------------- */
/* Sub module identifiers                                                     */
/* -------------------------------------------------------------------------- */


/**
* @enum e_mbn_service_parameter_name
* @brief Enumeration representing the parameter name  of MBN.
*/
typedef enum
{

    MBN_IDENTIFICATION_INFO =1,

    MBN_Iden_DeviceType,
    MBN_Iden_VendorName,
    MBN_Iden_VendorId,
    MBN_Iden_DeviceName,
    MBN_Iden_DeviceId,
    MBN_Iden_VariantId,
    MBN_Iden_HWRev,
    MBN_Iden_SerialNo,
    MBN_Iden_SWPartNo,
    MBN_Iden_SWRev,

    MBN_POST_ERROR_STATUS,
    MBN_VA_VOLATGE,
    MBN_VB_VOLATGE,
    MBN_OPERATING_MINS,
    MBN_OPERATING_HRS,

    MBN_MINIMUM_TEMPERATURE,
    MBN_MAXIMUM_TEMPERATURE,
    MBN_MEASURED_TEMPERATURE,

    MBNMODULE_VALVEISLAND_SHORTCODE,
    MBNMODULE_OPERATINGMODE,

    MBN_MAC_ADDRESS_PORT1,
    MBN_MAC_ADDRESS_PORT2,
    MBN_MAC_ADDRESS_PORT3,

    MBN_IPADDRESS_PORT1,
    MBN_IPADDRESS_PORT2,
    MBN_IPADDRESS_PORT3,

    MBN_PIN_VAL,
    MBN_FW_PASSWORD,

    MBN_ISEMDIAG_WARNEVENTCLR,
    MBN_ISEMDIAG_FAULTEVENTCLR,
    MBN_DV_WARNEVNTCLR,
    MBN_DV_FAULTEVNTCLR,
    MBN_EVENTSCLRALL,

    MBN_CLOUD_CONNECTED_STATUS,
    MBN_CIPNETWORK_STATUS,

    MBN_LINUX_FW_VERSION,

    MBN_ACTIVE_APPLICATION,
    MBN_CIP_ERROR_STATUS,
    MBN_CAN_ERROR_STATUS,
    MBN_MAIN_CORE_ERROR_STATUS,
    MBN_WEBSERVER_ERROR_STATUS,
    MBN_OPC_ERROR_STATUS,

    MBN_FW_IO_BCKPLANE_PSWD,
    MBN_FW_ISEM_BCKPLN_PSWD,
    MBN_FW_PROP_BCKPLN_PSWD,

    MBN_APPLICATION_CRC,
    MBN_CAN_CRC,
    MBN_CIP_CRC,

    //production param
    MBN_Prod_DeviceType,
    MBN_Prod_VendorName,
    MBN_Prod_VendorId,
    MBN_Prod_DeviceName,
    MBN_Prod_DeviceId,
    MBN_Prod_VariantId,
    MBN_Prod_HWRev,
    MBN_Prod_SerialNo,

    MBN_PRODUCTION_INFO,

    Upgrade_Process = 254,

MBN_LAST_PARAM

}mbnapp_access_paramid;


/**
 * @brief
 * Structure used from calls and tasks for passing MBN's Identification Details.
 *
 *  DeviceType          :   DeviceType.                                4 bytes
 *  VendorID            :   VendorID.                                  4 bytes
 *  DeviceID            :   DeviceID.                                  4 bytes
 *  VendorName[16]      :   VendorName.                               16 bytes
 *  DeviceName[16]      :   DeviceName.                               16 bytes
 *  VariantID[16]       :   VariantID.                                16 bytes
 *  HWRev[16]           :   HWRev.                                    16 bytes
 *  SlNo[16]            :   SlNo.                                     16 bytes
 *  SWPartNo[16]        :   SWPartNo.                                 16 bytes
 *  SWRev[16]           :   SWRev.                                    16 bytes
 *                                                           Total : 124 bytes
 */
typedef struct __attribute__((packed))
{
    uint32_t      DeviceType;
    uint32_t      VendorID;
    uint32_t      DeviceID;

    uint8_t       VendorName[16];
    uint8_t       DeviceName[16];
    uint8_t       VariantID[16];
    uint8_t       HWRev[16];
    uint8_t       SlNo[16];
    uint8_t       SWPartNo[16];
    uint8_t       SWRev[16];
}mbn_identification_info;

/**
 * @brief
 * Structure used from calls and tasks for passing MBN's Production Details.
 *
 *  DeviceType          :   DeviceType.                                4 bytes
 *  VendorID            :   VendorID.                                  4 bytes
 *  DeviceID            :   DeviceID.                                  4 bytes
 *  VendorName[16]      :   VendorName.                               16 bytes
 *  DeviceName[16]      :   DeviceName.                               16 bytes
 *  VariantID[16]       :   VariantID.                                16 bytes
 *  HWRev[16]           :   HWRev.                                    16 bytes
 *  SlNo[16]            :   SlNo.                                     16 bytes
 *                                                           Total :  92 bytes
 */
typedef struct __attribute__((packed))
{
    uint32_t      DeviceType;
    uint32_t      VendorID;
    uint32_t      DeviceID;

    uint8_t       VendorName[16];
    uint8_t       DeviceName[16];
    uint8_t       VariantID[16];
    uint8_t       HWRev[16];
    uint8_t       SlNo[16];
}mbn_production_info;

/**
 * @struct s_mbn_servicedata
 * @brief Structure related to Main bus service data
 */
/**
 * @brief
 * Structure used from calls and tasks for passing AI Module's Identification Details.
 *
 *  mbn_identification_info          :   IO Module DeviceType.                                124 bytes
 *  posterrsts                       :   MBN POST.                                              4 bytes
 *  Voltage_VA                       :   Voltage_VA.                                            4 bytes
 *  Voltage_VB                       :   Voltage_VA.                                            4 bytes
 *  opertiontimehr                   :   MBN Operating Hours.                                   4 bytes
 *  opertiontimemin                  :   MBN Operating Mins.                                    2 bytes
 *  mintemperature                   :   Min Temperature.                                       2 bytes
 *  maxtemperature                   :   Max Temperature.                                       2 bytes
 *  measuredtemperature              :   Measured Temperature.                                  2 bytes
 *  mbneeprommemorylifecycle         :   Eeprom LifeCycle.                                      4 bytes
 *  validation_identifier[16]        :   Validation Identifier Short code.                     16 bytes
 *  valve_island_reservered[16]      :   Reserved.                                             16 bytes
 *  mbn_operation_mode               :   MBN Operation Mode.                                    1 bytes
 *  ioforcemode_state                :   IO Force Mode State.                                   1 bytes
 *  mbn_system_status                :   MBN System Status.                                     1 bytes
 *  mac_address_port1                :   MAC Address Port-1.                                    6 bytes
 *  mac_address_port2                :   MAC Address Port-2.                                    6 bytes
 *  mac_address_port3                :   MAC Address Port-3.                                    6 bytes
 *  port1_ipaddress                  :   IP Address Port-1.                                     4 bytes
 *  port2_ipaddress                  :   IP Address Port-2.                                     4 bytes
 *  port3_ipaddress                  :   IP Address Port-3.                                     4 bytes
 *  mbn_pin                          :   Pin Value.                                             4 bytes
 *  mbn_pswd                         :   Password Value.                                        4 bytes
 *  modules_warning_clear_flag       :   Warn Clear Flag.                                       1 bytes
 *  modules_fault_clear_flag         :   Fault Clear Flag.                                      1 bytes
 *  cloud_connected_status           :   Cloud Connection Status.                               1 bytes
 *  network_connected_status         :   Network Connection Status.                             1 bytes
 *  cip_status                       :   CIP Status.                                            1 bytes
 *                                                                                    Total : 230 bytes
 */
typedef struct __attribute__((packed))
{

    mbn_identification_info  identification_info;

    uint32_t                 posterrsts;
    uint32_t                 Voltage_VA;
    uint32_t                 Voltage_VB;
    uint32_t                 opertiontimehr;
    uint16_t                 opertiontimemin;

    int16_t                  mintemperature;
    int16_t                  maxtemperature;
    int16_t                  measuredtemperature;

    //uint32_t                 eeprommemorylifecycle;

    uint8_t                  mbn_operation_mode; //[1- Production Mode] [ 2- Commissioning Mode] [3-Operation Mode][4- Service Mode]

    //uint8_t                  ioforcemode_state; //Force Mode of the system
    //uint8_t                  mbn_system_status; //YTD

    uint8_t                  mac_address_port1[6u];
    uint8_t                  mac_address_port2[6u];
    uint8_t                  mac_address_port3[6u];

    uint32_t                 port1_ipaddress;
    uint32_t                 port2_ipaddress;
    uint32_t                 port3_ipaddress;

    uint32_t                 mbn_pin;
    uint32_t                 mbn_pswd;

    uint8_t                  modules_warning_clear_flag;
    uint8_t                  modules_fault_clear_flag; //this should be acyclic/cyclic

    uint8_t                  cloud_connected_status;
    uint8_t                  cip_connected_status; //Required or no

    uint8_t                  cip_firmware_version[16];
    uint8_t                  can_firmware_version[16];
    uint8_t                  linux_app_firmware_version[16];

    uint8_t                  active_application_status; /*[0 bit -LCD state] [1st bit - WEB status] [2nd Bit - OPC status]*/
    uint8_t                  protocol_type; //[1- EtehrnetIP] , [2- Ethercat ] [3- Profinet ] [4 - IoLink] [5 - Modbus]

    uint16_t                  cip_err_status;
    uint16_t                  can_err_status;
    uint16_t                  main_core_err_status;
    uint16_t                  webserver_error_status;
    uint16_t                  opc_error_status;

    /*Firmware Upgrade related*/
     uint8_t                  mbn_application_password[8];
     uint8_t                  io_bckplane_password[8];
     uint8_t                  isem_bckplane_passowrd[8];
     uint8_t                  prop_bckplane_password[8];

     /*Application CRC*/
     uint32_t                 mbn_application_crc;
     uint32_t                 mbn_can_crc;
     uint32_t                 mbn_cip_crc;

     uint8_t                  validation_identifier[100u];

     //mbn_production_info      mbn_production_info;

}mbn_servicedata;

/**
 * @struct s_mbn_process data
 * @brief Structure related to Main bus process data
 */
typedef struct __attribute__((packed))
{
    uint8_t  mbn_error_status;
}mbn_processdata;

/**
* @enum
* @brief Enumeration representing the Result status for req from Linux Core.
*/
typedef enum
{

    Req_ProcessSuccess        = 0,

    Req_ProcessInvalidParameter,
    Req_ProcessShmReadErr,
    Req_ProcessShmLockErr,
    Req_ProcessRespTimeoutErr,
    Req_ProcessErrResp,

    Req_ProcessIpAddrWrerr,
    Req_Unknown

}maincoreipc_errsts;


#define MBNINFO_WR_NOTIFY_ID                          0x00000000      //lower byte[7:0] is instance no for    and bit 24 [0] is WRITE Request
#define MBNINFO_RD_NOTIFY_ID                          0x00800000      //lower byte[7:0] is instance no for    and bit 24 [1] is Read Request




#endif /* HEADER_MAINCOREGLOBAL_H_ */
