<commit_before>#include "XcpLight.h"
#include "XcpLight_internals.h"

/*
-------------------------------------------------------------------------------
  Limitations:
    1. Block transfer is not supported (UPLOAD, DOWNLOAD, PROGRAM)

-------------------------------------------------------------------------------
  Todo list:
    0. fix all @todo points
    1. implement daq processor
    2. implement programming (flash file download)
    3. implement resource protection
    4. test test test
-------------------------------------------------------------------------------
*/

/******************************************************************************/
/*** local area ***/
/******************************************************************************/

//------------------------------------------------------------------------------
// local macro constants
//------------------------------------------------------------------------------

/** VERSIONS **/
#define XCP_VER_DRIVER            (0x01u)
#define XCP_VER_PROTOCOL_LAYER    (0x01u)
#define XCP_VER_TRANSPORT_LAYER   (0x01u)

/** INTERNAL RETURN CODES **/
#define MSG_NONE (0)
#define MSG_SEND (1)

#ifdef XCPLIGHT_CFG_ENABLE_STATIC_INLINE
  #define XCP_STATIC_INLINE static inline
#else
  #define XCP_STATIC_INLINE
#endif

//------------------------------------------------------------------------------
// local struct and type definitions (struct, enum and typedef)
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// local data
//------------------------------------------------------------------------------

/* internal data structure for state etc. */
XcpLightInternals_t _XcpLightData = {0};

//------------------------------------------------------------------------------
// local macro functions
//------------------------------------------------------------------------------

#define Uint32FromUint8Ptr(ptr) (*((uint32_t *)(ptr)))

//#define Uint32FromUint8Ptr(ptr) ( ((*((ptr)+3)&0xFFu) << 24u) | \
//                                  ((*((ptr)+2)&0xFFu) << 16u) | \
//                                  ((*((ptr)+1)&0xFFu) <<  8u) | \
//                                  ((*((ptr)+0)&0xFFu) <<  0u))

//------------------------------------------------------------------------------
// local functions - prototypes
//------------------------------------------------------------------------------
XCP_STATIC_INLINE void _BuildErrorMessage(XcpLightMessage_t * pMsg, uint8_t errorCode);
XCP_STATIC_INLINE int _CmdConnect(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg);
XCP_STATIC_INLINE int _CmdDisconnect(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg);

//------------------------------------------------------------------------------
// local functions
//------------------------------------------------------------------------------
XCP_STATIC_INLINE void _BuildErrorMessage(XcpLightMessage_t * pMsg, uint8_t errorCode)
{
  pMsg->length = 2u;
  pMsg->payload[0] = XCP_PID_ERR;
  pMsg->payload[1] = errorCode;
}

XCP_STATIC_INLINE int _CmdConnect(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg)
{
  /* process the connect command anytime -> independence of state */
  pReplyMsg->length = 8u;
  pReplyMsg->payload[0] = XCP_PID_RES;

  pReplyMsg->payload[1] = 0u;
  #ifdef XCPLIGHT_CFG_ENABLE_CALPAG
  pReplyMsg->payload[1] |= XCP_RES_CALPAG;
  #endif
  #ifdef XCPLIGHT_CFG_ENABLE_DAQ
  pReplyMsg->payload[1] |= XCP_RES_DAQ;
  #endif
  #ifdef XCPLIGHT_CFG_ENABLE_STIM
  pReplyMsg->payload[1] |= XCP_RES_STIM;
  #endif
  #ifdef XCPLIGHT_CFG_ENABLE_PGM
  pReplyMsg->payload[1] |= XCP_RES_PGM;
  #endif

  pReplyMsg->payload[2] = 0x80u; // @todo COMM_MODE_BASIC
  pReplyMsg->payload[3] = XCPLIGHT_CFG_XTO_LENGTH;
  pReplyMsg->payload[4] = XCPLIGHT_CFG_XTO_LENGTH;
  pReplyMsg->payload[5] = 0x00u; // reserved
  pReplyMsg->payload[6] = XCP_VER_PROTOCOL_LAYER;
  pReplyMsg->payload[7] = XCP_VER_TRANSPORT_LAYER;

  /* protect all resources */
  #ifdef XCPLIGHT_CFG_ENABLE_RESOURCE_PROTECTION
  _XcpLightData.protectionStatus = XCP_PRT_PROTECT_ALL;
  #endif

  /* set session to connected state */
  _XcpLightData.sessionStatus |= XCP_SES_CONNECTED; // mark session as active (connected)

  return MSG_SEND;
}

XCP_STATIC_INLINE int _CmdDisconnect(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg)
{
  /* protect all resources */
  #ifdef XCPLIGHT_CFG_ENABLE_RESOURCE_PROTECTION
  _XcpLightData.protectionStatus = XCP_PRT_PROTECT_ALL;
  #endif

  /* reset all session status bits */
  _XcpLightData.sessionStatus    = XCP_SES_RESET_SESSION;

  pReplyMsg->length = 1u;
  pReplyMsg->payload[0] = XCP_PID_RES;

  return MSG_SEND;
}

/******************************************************************************/
/*** external area ***/
/******************************************************************************/

//------------------------------------------------------------------------------
// external data
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// external functions
//------------------------------------------------------------------------------
void XcpLight_Init(void)
{
  _XcpLightData.timestampCounter = 0u;
  _XcpLightData.sessionStatus    = XCP_SES_RESET_SESSION;

  #ifdef XCPLIGHT_CFG_ENABLE_RESOURCE_PROTECTION
  _XcpLightData.protectionStatus = XCP_PRT_PROTECT_ALL;
  #else
  _XcpLightData.protectionStatus = 0u;
  #endif
}

void XcpLight_UpdateTimestampCounter(void)
{
  _XcpLightData.timestampCounter++;
}

void XcpLight_CommandProcessor(XcpLightMessage_t * pMsg)
{
  XcpLightMessage_t * pReplyMsg = &(_XcpLightData.ctoReplyMsg);

#ifdef XCPLIGHT_CFG_DEBUG_CMD_MSG
  _XcpLightData.ctoCmdMsg = *pMsg; /* copy to internal state (for ext. debugging) */
#endif

  uint8_t sendFlag = 0;

  _XcpLightData.currentCommand = (pMsg->payload[0] & 0xFFu);

  if(_XcpLightData.currentCommand == XCP_CMD_CONNECT)
  {
    sendFlag = _CmdConnect(pMsg, pReplyMsg);
  }
  else
  {
    if(_XcpLightData.sessionStatus & XCP_SES_CONNECTED)
    {
      /* in connected state process all other commands*/
      switch(_XcpLightData.currentCommand)
      {
        case XCP_CMD_DISCONNECT:
          sendFlag = _CmdDisconnect(pMsg, pReplyMsg);
          break;

        case XCP_CMD_SYNCH:
          {
            _BuildErrorMessage(pReplyMsg, XCP_ERR_CMD_SYNCH);
            sendFlag = 1;
          }
          break;

        case XCP_CMD_GET_COMM_MODE_INFO:
          {
            pReplyMsg->length = 8u;
            pReplyMsg->payload[0] = XCP_PID_RES;
            pReplyMsg->payload[1] = 0x00u; // reserved
            pReplyMsg->payload[2] = 0x00u; // @todo: COMM_MODE_OPTIONAL
            pReplyMsg->payload[3] = 0x00u; // reserved
            pReplyMsg->payload[4] = 0x00u; // @todo: MAX_BS
            pReplyMsg->payload[5] = 0x00u; // @todo: MAX_ST
            pReplyMsg->payload[6] = 0x00u; // @todo: QUEUE_SIZE
            pReplyMsg->payload[7] = XCP_VER_DRIVER;
            sendFlag = 1;
          }
          break;

        case XCP_CMD_GET_STATUS:
          {
            pReplyMsg->length = 6u;
            pReplyMsg->payload[0] = XCP_PID_RES;
            pReplyMsg->payload[1] = _XcpLightData.sessionStatus;
            pReplyMsg->payload[2] = _XcpLightData.protectionStatus;
            pReplyMsg->payload[3] = 0x00u; /* STATE_NUMBER: ECU_STATES not supported */

            pReplyMsg->payload[4] = 0x00u; // @todo: Session configuration id<commit_msg>refactoring: moved SYNCH cmd to inline function<commit_after>#include "XcpLight.h"
#include "XcpLight_internals.h"

/*
-------------------------------------------------------------------------------
  Limitations:
    1. Block transfer is not supported (UPLOAD, DOWNLOAD, PROGRAM)

-------------------------------------------------------------------------------
  Todo list:
    0. fix all @todo points
    1. implement daq processor
    2. implement programming (flash file download)
    3. implement resource protection
    4. test test test
-------------------------------------------------------------------------------
*/

/******************************************************************************/
/*** local area ***/
/******************************************************************************/

//------------------------------------------------------------------------------
// local macro constants
//------------------------------------------------------------------------------

/** VERSIONS **/
#define XCP_VER_DRIVER            (0x01u)
#define XCP_VER_PROTOCOL_LAYER    (0x01u)
#define XCP_VER_TRANSPORT_LAYER   (0x01u)

/** INTERNAL RETURN CODES **/
#define MSG_NONE (0)
#define MSG_SEND (1)

#ifdef XCPLIGHT_CFG_ENABLE_STATIC_INLINE
  #define XCP_STATIC_INLINE static inline
#else
  #define XCP_STATIC_INLINE
#endif

//------------------------------------------------------------------------------
// local struct and type definitions (struct, enum and typedef)
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// local data
//------------------------------------------------------------------------------

/* internal data structure for state etc. */
XcpLightInternals_t _XcpLightData = {0};

//------------------------------------------------------------------------------
// local macro functions
//------------------------------------------------------------------------------

#define Uint32FromUint8Ptr(ptr) (*((uint32_t *)(ptr)))

//#define Uint32FromUint8Ptr(ptr) ( ((*((ptr)+3)&0xFFu) << 24u) | \
//                                  ((*((ptr)+2)&0xFFu) << 16u) | \
//                                  ((*((ptr)+1)&0xFFu) <<  8u) | \
//                                  ((*((ptr)+0)&0xFFu) <<  0u))

//------------------------------------------------------------------------------
// local functions - prototypes
//------------------------------------------------------------------------------
XCP_STATIC_INLINE void _BuildErrorMessage(XcpLightMessage_t * pMsg, uint8_t errorCode);
XCP_STATIC_INLINE int _CmdConnect(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg);
XCP_STATIC_INLINE int _CmdDisconnect(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg);
XCP_STATIC_INLINE int _CmdSynch(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg);

//------------------------------------------------------------------------------
// local functions
//------------------------------------------------------------------------------
XCP_STATIC_INLINE void _BuildErrorMessage(XcpLightMessage_t * pMsg, uint8_t errorCode)
{
  pMsg->length = 2u;
  pMsg->payload[0] = XCP_PID_ERR;
  pMsg->payload[1] = errorCode;
}

XCP_STATIC_INLINE int _CmdConnect(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg)
{
  /* process the connect command anytime -> independence of state */
  pReplyMsg->length = 8u;
  pReplyMsg->payload[0] = XCP_PID_RES;

  pReplyMsg->payload[1] = 0u;
  #ifdef XCPLIGHT_CFG_ENABLE_CALPAG
  pReplyMsg->payload[1] |= XCP_RES_CALPAG;
  #endif
  #ifdef XCPLIGHT_CFG_ENABLE_DAQ
  pReplyMsg->payload[1] |= XCP_RES_DAQ;
  #endif
  #ifdef XCPLIGHT_CFG_ENABLE_STIM
  pReplyMsg->payload[1] |= XCP_RES_STIM;
  #endif
  #ifdef XCPLIGHT_CFG_ENABLE_PGM
  pReplyMsg->payload[1] |= XCP_RES_PGM;
  #endif

  pReplyMsg->payload[2] = 0x80u; // @todo COMM_MODE_BASIC
  pReplyMsg->payload[3] = XCPLIGHT_CFG_XTO_LENGTH;
  pReplyMsg->payload[4] = XCPLIGHT_CFG_XTO_LENGTH;
  pReplyMsg->payload[5] = 0x00u; // reserved
  pReplyMsg->payload[6] = XCP_VER_PROTOCOL_LAYER;
  pReplyMsg->payload[7] = XCP_VER_TRANSPORT_LAYER;

  /* protect all resources */
  #ifdef XCPLIGHT_CFG_ENABLE_RESOURCE_PROTECTION
  _XcpLightData.protectionStatus = XCP_PRT_PROTECT_ALL;
  #endif

  /* set session to connected state */
  _XcpLightData.sessionStatus |= XCP_SES_CONNECTED; // mark session as active (connected)

  return MSG_SEND;
}

XCP_STATIC_INLINE int _CmdDisconnect(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg)
{
  /* protect all resources */
  #ifdef XCPLIGHT_CFG_ENABLE_RESOURCE_PROTECTION
  _XcpLightData.protectionStatus = XCP_PRT_PROTECT_ALL;
  #endif

  /* reset all session status bits */
  _XcpLightData.sessionStatus    = XCP_SES_RESET_SESSION;

  pReplyMsg->length = 1u;
  pReplyMsg->payload[0] = XCP_PID_RES;

  return MSG_SEND;
}

XCP_STATIC_INLINE int _CmdSynch(XcpLightMessage_t * pCmdMsg, XcpLightMessage_t * pReplyMsg)
{
  _BuildErrorMessage(pReplyMsg, XCP_ERR_CMD_SYNCH);
  return MSG_SEND;
}

/******************************************************************************/
/*** external area ***/
/******************************************************************************/

//------------------------------------------------------------------------------
// external data
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// external functions
//------------------------------------------------------------------------------
void XcpLight_Init(void)
{
  _XcpLightData.timestampCounter = 0u;
  _XcpLightData.sessionStatus    = XCP_SES_RESET_SESSION;

  #ifdef XCPLIGHT_CFG_ENABLE_RESOURCE_PROTECTION
  _XcpLightData.protectionStatus = XCP_PRT_PROTECT_ALL;
  #else
  _XcpLightData.protectionStatus = 0u;
  #endif
}

void XcpLight_UpdateTimestampCounter(void)
{
  _XcpLightData.timestampCounter++;
}

void XcpLight_CommandProcessor(XcpLightMessage_t * pMsg)
{
  XcpLightMessage_t * pReplyMsg = &(_XcpLightData.ctoReplyMsg);

#ifdef XCPLIGHT_CFG_DEBUG_CMD_MSG
  _XcpLightData.ctoCmdMsg = *pMsg; /* copy to internal state (for ext. debugging) */
#endif

  uint8_t sendFlag = 0;

  _XcpLightData.currentCommand = (pMsg->payload[0] & 0xFFu);

  if(_XcpLightData.currentCommand == XCP_CMD_CONNECT)
  {
    sendFlag = _CmdConnect(pMsg, pReplyMsg);
  }
  else
  {
    if(_XcpLightData.sessionStatus & XCP_SES_CONNECTED)
    {
      /* in connected state process all other commands*/
      switch(_XcpLightData.currentCommand)
      {
        case XCP_CMD_DISCONNECT:
          sendFlag = _CmdDisconnect(pMsg, pReplyMsg);
          break;

        case XCP_CMD_SYNCH:
          sendFlag = _CmdSynch(pMsg, pReplyMsg);
          break;

        case XCP_CMD_GET_COMM_MODE_INFO:
          {
            pReplyMsg->length = 8u;
            pReplyMsg->payload[0] = XCP_PID_RES;
            pReplyMsg->payload[1] = 0x00u; // reserved
            pReplyMsg->payload[2] = 0x00u; // @todo: COMM_MODE_OPTIONAL
            pReplyMsg->payload[3] = 0x00u; // reserved
            pReplyMsg->payload[4] = 0x00u; // @todo: MAX_BS
            pReplyMsg->payload[5] = 0x00u; // @todo: MAX_ST
            pReplyMsg->payload[6] = 0x00u; // @todo: QUEUE_SIZE
            pReplyMsg->payload[7] = XCP_VER_DRIVER;
            sendFlag = 1;
          }
          break;

        case XCP_CMD_GET_STATUS:
          {
            pReplyMsg->length = 6u;
            pReplyMsg->payload[0] = XCP_PID_RES;
            pReplyMsg->payload[1] = _XcpLightData.sessionStatus;
            pReplyMsg->payload[2] = _XcpLightData.protectionStatus;
            pReplyMsg->payload[3] = 0x00u; /* STATE_NUMBER: ECU_STATES not supported */

            pReplyMsg->payload[4] = 0x00u; // @todo: Session configuration id
{'fraction_non_alphanumeric': 0.3184516129032258, 'fraction_numerical': 0.01464516129032258, 'mean_word_length': 1.6832265881945647, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '303692', 'n_tokens_mistral': 4668, 'n_tokens_neox': 4441, 'n_words': 780}