tao_z
2022-06-11 d8a5f3225d105585a4f181ed884adb2a861e0d13
Application/source/uds_modules/uds.c
@@ -155,7 +155,7 @@
#define RSP_DATA_LEN (6)
#define UDS_TASK_RATE (10)
#define CANTP_RX_BUFFER_SIZE (128)
#define CANTP_RX_BUFFER_SIZE (266)
#define FBL_RX_BUFFER_SIZE (256)
#define CANTP_TX_BUFFER_SIZE (128)
@@ -223,7 +223,7 @@
static UDS_CONTOL_TYPE_e UDS_L_CommunicationControlType_e = EnableRxAndTx;
static uint8_t UDS_L_SecurityFault_cnt;
static uint8_t UDS_L_SecurityStartDelay = 1;
static uint8_t UDS_L_SecurityStartDelay = 0;
static uint8_t UDS_L_SecurityAccessStep_e = SECURITY_ACCESS_STEP_MAX;
/*****************************************************************************/
/*  Local Variables Definition                                               */
@@ -260,6 +260,7 @@
static uint16_t ResponsePendingTimer_u16;
static bool UDS_L_TransferDataInProgressing_b = false;
static bool UDS_L_CRCCheckFail_b = false;
static uint32_t UDS_L_CRCReceived = 0;
static UDS_TRANFER_DATA_STATE_Te UDS_L_TransferDataState_e;
static uint8_t expectedSequenceCnt;        /* Block sequence counter             */
@@ -356,6 +357,9 @@
#define ROUTINE_CONTROL_MIN_LEN (0x04u)
#define TESTER_PRESENT_MIN_LEN (0x02u)
#define CONTROL_DTC_SETTING_MIN_LEN (0x02u)
#define REQUEST_DOWNLOAD_MIN_LEN (0xB)
#define TRANSFER_DATA_MIN_LEN (0XC)
#define REQUEST_TRANSFER_EXIT_MIN_LEN (0X01)
typedef struct
{
@@ -382,9 +386,9 @@
    {ROUTINE_CONTROL,               UDS_RoutineControl,         false, true,  false,   false, SECURITY_ACCESS_UNLOCK_LEVEL_2},
    {TESTER_PRESENT,                UDS_TesterPresent,          true,  true,  true,    true,  SECURITY_ACCESS_LOCKED},
    // {CONTROL_DTC_SETTING,           UDS_ControlDTCSetting,      false, true,  false,    true,  SECURITY_ACCESS_LOCKED},
    {REQUEST_DOWNLOAD,              UDS_RequestDownload,      false, true,  false,    true,  SECURITY_ACCESS_UNLOCK_LEVEL_2},
    {TRANSFER_DATA,                 UDS_TransferData,      false, true,  false,    true,  SECURITY_ACCESS_UNLOCK_LEVEL_2},
    {REQUEST_TRANSFER_EXIT,         UDS_RequestTransferExit,      false, true,  false,    true,  SECURITY_ACCESS_UNLOCK_LEVEL_2},
    {REQUEST_DOWNLOAD,              UDS_RequestDownload,      false, true,  false,    false,  SECURITY_ACCESS_UNLOCK_LEVEL_2},
    {TRANSFER_DATA,                 UDS_TransferData,      false, true,  false,    false,  SECURITY_ACCESS_UNLOCK_LEVEL_2},
    {REQUEST_TRANSFER_EXIT,         UDS_RequestTransferExit,      false, true,  false,    false,  SECURITY_ACCESS_UNLOCK_LEVEL_2},
};
/* clang-format on */
@@ -423,153 +427,170 @@
    uint8_t sid;
    sid = msg_buf[0];
    if (msg_dlc < 2)
    {
        result = false;
    }
    else
    {
        subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
    subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
        switch (sid)
    switch (sid)
    {
    case SESSION_CONTROL:
    {
        if (msg_dlc == SESSION_CONTROL_MIN_LEN)
        {
        case SESSION_CONTROL:
        {
            if (msg_dlc == SESSION_CONTROL_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
            result = true;
        }
        break;
        case ECU_RESET:
        else
        {
            if (msg_dlc == ECU_RESET_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
        break;
        case CLEAR_DIAGNOSTIC_INFORMATION:
        {
            if (msg_dlc == CLEAR_DIAGNOSTIC_INFORMATION_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
        break;
        case READ_DTC_INFORMATION:
        {
            if ((subfunction == REPORT_DTC_NUMBER_BY_STATUS_MASK && msg_dlc != (READ_DTC_INFORMATION_MIN_LEN + 1)) ||
                (subfunction == REPORT_DTC_BY_STATUS_MASK && msg_dlc != (READ_DTC_INFORMATION_MIN_LEN + 1)) ||
                (subfunction == REPORT_SUPPORTED_DTC && msg_dlc != READ_DTC_INFORMATION_MIN_LEN))
            {
                result = false;
            }
            else
            {
                result = true;
            }
        }
        break;
        case READ_DATA_BY_IDENTIFIER:
        {
            if (msg_dlc == READ_DATA_BY_IDENTIFIER_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
        break;
        case SECURITY_ACCESS:
        {
            if ((subfunction == UDS_REQUEST_SEED && msg_dlc != SECURITY_ACCESS_MIN_LEN) ||
                (subfunction == UDS_SEND_KEY && msg_dlc != (SECURITY_ACCESS_MIN_LEN + KEY_SIZE)))
            {
                result = false;
            }
            else
            {
                result = true;
            }
        }
        break;
        case COMMUNICATION_CONTROL:
        {
            if (msg_dlc == COMMUNICATION_CONTROL_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
        break;
        case WRITE_DATA_BY_IDENTIFIER:
        {
            if (msg_dlc >= WRITE_DATA_BY_IDENTIFIER_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
        break;
        case ROUTINE_CONTROL:
            if (msg_dlc >= ROUTINE_CONTROL_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
            break;
        case TESTER_PRESENT:
        {
            if (msg_dlc == TESTER_PRESENT_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
        break;
        case CONTROL_DTC_SETTING:
        {
            if (msg_dlc == CONTROL_DTC_SETTING_MIN_LEN)
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
        break;
        default:
            result = false;
            break;
        }
    }
    break;
    case ECU_RESET:
    {
        if (msg_dlc == ECU_RESET_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }
    break;
    case CLEAR_DIAGNOSTIC_INFORMATION:
    {
        if (msg_dlc == CLEAR_DIAGNOSTIC_INFORMATION_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }
    break;
    case READ_DTC_INFORMATION:
    {
        if ((subfunction == REPORT_DTC_NUMBER_BY_STATUS_MASK && msg_dlc != (READ_DTC_INFORMATION_MIN_LEN + 1)) ||
            (subfunction == REPORT_DTC_BY_STATUS_MASK && msg_dlc != (READ_DTC_INFORMATION_MIN_LEN + 1)) ||
            (subfunction == REPORT_SUPPORTED_DTC && msg_dlc != READ_DTC_INFORMATION_MIN_LEN))
        {
            result = false;
        }
        else
        {
            result = true;
        }
    }
    break;
    case READ_DATA_BY_IDENTIFIER:
    {
        if (msg_dlc == READ_DATA_BY_IDENTIFIER_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }
    break;
    case SECURITY_ACCESS:
    {
        if ((subfunction == UDS_REQUEST_SEED && msg_dlc != SECURITY_ACCESS_MIN_LEN) ||
            (subfunction == UDS_SEND_KEY && msg_dlc != (SECURITY_ACCESS_MIN_LEN + KEY_SIZE)))
        {
            result = false;
        }
        else
        {
            result = true;
        }
    }
    break;
    case COMMUNICATION_CONTROL:
    {
        if (msg_dlc == COMMUNICATION_CONTROL_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }
    break;
    case WRITE_DATA_BY_IDENTIFIER:
    {
        if (msg_dlc >= WRITE_DATA_BY_IDENTIFIER_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }
    break;
    case ROUTINE_CONTROL:
        if (msg_dlc >= ROUTINE_CONTROL_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        break;
    case TESTER_PRESENT:
    {
        if (msg_dlc == TESTER_PRESENT_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }
    break;
    case CONTROL_DTC_SETTING:
    {
        if (msg_dlc == CONTROL_DTC_SETTING_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
    }
    break;
    case REQUEST_DOWNLOAD:
        if (msg_dlc == REQUEST_DOWNLOAD_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        break;
    case TRANSFER_DATA:
        if (msg_dlc >= TRANSFER_DATA_MIN_LEN)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        break;
    case REQUEST_TRANSFER_EXIT:
        result = true;
        break;
    default:
        result = false;
        break;
    }
    return result;
}
@@ -700,7 +721,7 @@
    TesterPresentTimer_u16 = 0;
    UDS_NS_SessionTimeout_u16 = 5000;
    UDS_L_SecurityFault_cnt = 0;
    UDS_L_SecurityStartDelay = 1;
    UDS_L_SecurityStartDelay = 0;
    UDS_L_Diag_session_e = DEFAULT;
    UDS_O_SecurityAccessState_e = SECURITY_ACCESS_LOCKED;
    UDS_L_Current_SID = 0;
@@ -759,11 +780,20 @@
static void UDS_RxTask_1ms(void)
{
    static TP_PDU_t msg_rx_temp = {0};
    CAN_PDU_t msg_can_temp = {0};
    uint8_t msg_num = MCAN_GetReceiveMessageCounter(MCAN);
    while (msg_num--)
    {
        MCAN_ReceiveData(MCAN, (MCAN_MsgTypeDef *)(&msg_rx_temp));
        MCAN_ReceiveData(MCAN, (MCAN_MsgTypeDef *)(&msg_can_temp));
        msg_rx_temp.arbId = msg_can_temp.MCAN_ID;
        msg_rx_temp.dlc = msg_can_temp.MCAN_DLC;
        for (TP_uint8_t i = 0; i < msg_can_temp.MCAN_DLC; i++)
        {
            msg_rx_temp.frame[i] = msg_can_temp.MCAN_Data[i];
        }
        if (msg_rx_temp.arbId == UDS_PHY_REQ_RID)
        {
            UDS_RxMessage(&msg_rx_temp);
@@ -1031,12 +1061,12 @@
            if (CDT_HASEXPIRED(TesterPresentTimer_u16) != false)
            {
                UDS_SwitchtoDefaultSession(true);
                UDS_SwitchtoDefaultSession(false);
            }
        }
        else
        {
            UDS_SwitchtoDefaultSession(true);
            UDS_SwitchtoDefaultSession(false);
        }
    }
    else
@@ -1450,14 +1480,15 @@
        break;
    case PROGRAMMING:
        /* UDS_SendNegResp(NRC_SUBFUNCTION_NOT_SUPPORTED); */
        if (((UDS_L_Diag_session_e == EXTENDED) || (UDS_L_Diag_session_e == PROGRAMMING)) && (UDS_L_ProgrammingPreconditionCheckOK_b == true))
        if (((UDS_L_Diag_session_e == EXTENDED) || (UDS_L_Diag_session_e == PROGRAMMING)))
        {
            /**/
            CDT_RESET(TesterPresentTimer_u16,
                      UDS_NS_SessionTimeout_u16 / UDS_TASK_RATE);
            UDS_L_Diag_session_e = PROGRAMMING;
            UDS_SessionSetOk_b = true;
            FblSetFblProgramRequestFlag();
        }
        else if ((UDS_L_Diag_session_e == DEFAULT) || (UDS_L_ProgrammingPreconditionCheckOK_b == false)) /*DEFAULT session can not trans to PROGRAMMING*/
        else if ((UDS_L_Diag_session_e == DEFAULT)) /*DEFAULT session can not trans to PROGRAMMING*/
        {
            UDS_SendNegResp(NRC_CONDITIONS_NOT_CORRECT);
        }
@@ -1468,6 +1499,7 @@
    case DEFAULT:
        if (UDS_L_Diag_session_e == PROGRAMMING)
        {
            UDS_L_Diag_session_e = DEFAULT;
            UDS_L_tx_msg_buffer[0] = GET_RESPONSE_SERVICE_ID(SESSION_CONTROL);
            UDS_L_tx_msg_buffer[1] = UDS_L_Diag_session_e;
            UDS_L_tx_msg_buffer[2] = 0;
@@ -1481,6 +1513,7 @@
            /* Compose a response message and send a positive */
            /* response with the request data */
            UDS_RespTxMessage();
            FblClrFblProgramRequestFlag();
            while (cnt--)
            {
                ; /// wait send command send out and then reset
@@ -1772,7 +1805,7 @@
            {
                if (transferType == DOWNLOAD_FLASH)
                {
                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_CODEFLASH);
                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_CODEFLASH, UDS_L_CRCReceived);
                    if (UDS_L_CRCCheckFail_b == false)
                    {
                        NVM_ReInitFlashDriveRam(); // CLEAR FLASH DRIVE IN RAM
@@ -1780,29 +1813,26 @@
                }
                else
                {
                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_RAM);
                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_RAM, UDS_L_CRCReceived);
                    if (UDS_L_CRCCheckFail_b == true)
                    {
                        NVM_ReInitFlashDriveRam(); // CLEAR FLASH DRIVE IN RAM
                    }
                }
                if (UDS_L_CRCCheckFail_b == false)
                {
                    UDS_L_tx_msg_buffer[0] = GET_RESPONSE_SERVICE_ID(ROUTINE_CONTROL);
                    UDS_L_tx_msg_buffer[1] = 0x01;
                    UDS_L_tx_msg_buffer[2] = 0xf0;
                    UDS_L_tx_msg_buffer[3] = 0x01;
                    UDS_L_tx_msg_buffer[4] = (uint8_t)UDS_L_CRCCheckFail_b;
                    /* Specify the send payload size */
                    UDS_L_tx_msg_size = 0x05;
                UDS_L_tx_msg_buffer[0] = GET_RESPONSE_SERVICE_ID(ROUTINE_CONTROL);
                UDS_L_tx_msg_buffer[1] = 0x01;
                UDS_L_tx_msg_buffer[2] = 0xf0;
                UDS_L_tx_msg_buffer[3] = 0x01;
                UDS_L_tx_msg_buffer[4] = (uint8_t)UDS_L_CRCCheckFail_b;
                /* Specify the send payload size */
                UDS_L_tx_msg_size = 0x05;
                    /* Compose a response message and send a positive */
                    /* response with the request data */
                    UDS_RespTxMessage();
                /* Compose a response message and send a positive */
                /* response with the request data */
                UDS_RespTxMessage();
                    UDS_L_ResponsePendingRequest_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
                    UDS_L_ResponsePendingRelease_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
                }
                UDS_L_ResponsePendingRequest_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
                UDS_L_ResponsePendingRelease_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
            }
        }
    }
@@ -1909,7 +1939,6 @@
        return;
    }
    UDS_L_ResponsePendingRequest_e |= ERASE_RESP_PENDING;
    UDS_SendNegResp(NRC_SERVICE_BUSY);
    erase_state = NVM_EraseData(eraseAddress, eraseRemainder);
    if (erase_state == OP_STATUS_OK)
@@ -1973,7 +2002,7 @@
      FBL_UDS_L_ResponsePendingRelease_e &=(~CHECK_PROGRAMMING_INTERGRITY_PENDING); 
   }
#endif
    UDS_L_CRCReceived = (uint32_t)payload[4] << 24 | (uint32_t)payload[5] << 16 | (uint32_t)payload[6] << 8 | (uint32_t)payload[7];
    UDS_L_ResponsePendingRequest_e |= CHECK_PROGRAMMING_INTERGRITY_PENDING;
    CDT_RESET(ResponsePendingTimer_u16, P2SERVER_PENDING_TIMEROUT);
}
@@ -2188,7 +2217,7 @@
    operation_write.current_sequenc = expectedSequenceCnt;
    operation_write.prev_seqence = currentSequenceCnt;
    UDS_SendNegResp(NRC_SERVICE_BUSY);              // first send busy
    // UDS_SendNegResp(NRC_SERVICE_BUSY);              // first send busy
    write_result = NEM_WriteData(&operation_write); // todo write all data at this function
    if (write_result == OP_STATUS_OK)
    {