161 lines
4.8 KiB
C
161 lines
4.8 KiB
C
|
/*
|
||
|
* Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
|
||
|
* exclusively with CVA's microcontroller products. This file can be freely
|
||
|
* distributed within development tools that are supporting such microcontroller
|
||
|
* products.
|
||
|
*
|
||
|
* THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
|
||
|
* OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
|
||
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
|
||
|
* CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
|
||
|
* OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
|
||
|
*/
|
||
|
|
||
|
/*******************************************************************************
|
||
|
* the includes
|
||
|
******************************************************************************/
|
||
|
|
||
|
#include "uds_service36.h"
|
||
|
|
||
|
|
||
|
/*******************************************************************************
|
||
|
* the defines
|
||
|
******************************************************************************/
|
||
|
|
||
|
/*******************************************************************************
|
||
|
* the typedefs
|
||
|
******************************************************************************/
|
||
|
|
||
|
/*******************************************************************************
|
||
|
* the globals
|
||
|
******************************************************************************/
|
||
|
|
||
|
static uint32_t s_memAddr = 0;
|
||
|
static uint32_t s_memSize = 0;
|
||
|
static UdsTransDirType s_dataTransDir = UDS_TRANSFER_DIR_NONE;
|
||
|
|
||
|
static uint8_t s_curRcvSequence = 0;
|
||
|
static uint8_t s_lastRcvSqunce = 0;
|
||
|
static uint32_t s_rcvDataTotalLen = 0;
|
||
|
|
||
|
/*******************************************************************************
|
||
|
* the constants
|
||
|
******************************************************************************/
|
||
|
|
||
|
/*******************************************************************************
|
||
|
* the functions
|
||
|
******************************************************************************/
|
||
|
|
||
|
void Uds_SetMemAddr(uint32_t addr)
|
||
|
{
|
||
|
s_memAddr = addr;
|
||
|
}
|
||
|
|
||
|
uint32_t Uds_GetMemAddr(void)
|
||
|
{
|
||
|
return s_memAddr;
|
||
|
}
|
||
|
|
||
|
void Uds_SetMemSize(uint32_t size)
|
||
|
{
|
||
|
s_memSize = size;
|
||
|
}
|
||
|
|
||
|
uint32_t Uds_GetMemSize(void)
|
||
|
{
|
||
|
return s_memSize;
|
||
|
}
|
||
|
|
||
|
void Uds_SetDataTransferDirect(UdsTransDirType dirType)
|
||
|
{
|
||
|
s_dataTransDir = dirType;
|
||
|
|
||
|
/* clear sequence number */
|
||
|
s_lastRcvSqunce = 0x00;
|
||
|
s_curRcvSequence = 0x00;
|
||
|
|
||
|
/* clear receive data total number */
|
||
|
s_rcvDataTotalLen = 0x00;
|
||
|
}
|
||
|
|
||
|
UdsTransDirType Uds_GetDataTransferDirect(void)
|
||
|
{
|
||
|
return s_dataTransDir;
|
||
|
}
|
||
|
|
||
|
uint32_t Uds_GetRcvDataTotalLen(void)
|
||
|
{
|
||
|
return s_rcvDataTotalLen;
|
||
|
}
|
||
|
|
||
|
void UdsService36_TransferData(UdsType *obj, const uint8_t msgBuf[], uint16_t msgLen)
|
||
|
{
|
||
|
uint16_t rspLen = 0;
|
||
|
uint8_t rspBuffer[UDS_RSP_LEN_MAX] = {0}; /* polyspace DEFECT:PARTIALLY_ACCESSED_ARRAY [No action planned:Low] "Still reserve though member2~511 is not written" */
|
||
|
|
||
|
if(obj->session == UDS_SESSION_DEFAULT)
|
||
|
{
|
||
|
Uds_NegativeResponse(obj, 0x36, NRC_CONDITIONS_NOT_CORRECT);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if(msgLen < obj->seviceTable[obj->curServiceIdx].minLen)
|
||
|
{
|
||
|
Uds_NegativeResponse(obj, 0x36, NRC_INVALID_MESSAGE_LENGTH_OR_FORMAT);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
switch(s_dataTransDir)
|
||
|
{
|
||
|
case UDS_TRANSFER_DIR_DOWNLOAD:
|
||
|
s_curRcvSequence = msgBuf[1];
|
||
|
|
||
|
if((s_curRcvSequence != s_lastRcvSqunce) && (s_curRcvSequence != ((uint8_t)(s_lastRcvSqunce + 1))))
|
||
|
{
|
||
|
Uds_NegativeResponse(obj, 0x36, NRC_WRONG_BLOCK_SEQUENCE_COUNTER);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
rspBuffer[rspLen++] = UDS_GET_POSITIVE_RSP(0x36);
|
||
|
rspBuffer[rspLen++] = msgBuf[1];
|
||
|
if(s_curRcvSequence == s_lastRcvSqunce)
|
||
|
{
|
||
|
Uds_PositiveResponse(obj, rspBuffer, rspLen);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if((s_rcvDataTotalLen + msgLen - 2) > s_memSize)
|
||
|
{
|
||
|
Uds_NegativeResponse(obj, 0x36, NRC_TRANSFER_DATA_SUSPENDED);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/* user add code to deal with download data */
|
||
|
/* polyspace-begin DEFECT:DEAD_CODE [No action planned:Low] "Still reserve though it's maybe unreachable" */
|
||
|
if(1) /* deal success */
|
||
|
{
|
||
|
s_rcvDataTotalLen += msgLen - 2;
|
||
|
s_lastRcvSqunce = s_curRcvSequence;
|
||
|
Uds_PositiveResponse(obj, rspBuffer, rspLen);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Uds_NegativeResponse(obj, 0x36, NRC_GENERAL_PROGRAMMING_FAILURE);
|
||
|
}
|
||
|
/* polyspace-end DEFECT:DEAD_CODE [No action planned:Low] "Still reserve though it's maybe unreachable" */
|
||
|
break;
|
||
|
case UDS_TRANSFER_DIR_UPLOAD:
|
||
|
Uds_NegativeResponse(obj, 0x36, NRC_REQUEST_OUT_OF_RANGE);
|
||
|
break;
|
||
|
default:
|
||
|
Uds_NegativeResponse(obj, 0x36, NRC_REQUEST_SEQUENCE_ERROR);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|