346 lines
14 KiB
C
346 lines
14 KiB
C
//
|
|
// Created by cfif on 05.04.2024.
|
|
//
|
|
#include <SystemDelayInterface.h>
|
|
#include "CanSpamReceiver.h"
|
|
#include "CmsisRtosThreadUtils.h"
|
|
#include "CanPorts.h"
|
|
#include "string.h"
|
|
#include "HVAC_model.h"
|
|
#include "MainStack.h"
|
|
|
|
#define LOG_SIGN "CAN_RECEIVED"
|
|
#define LOGGER env->logger
|
|
|
|
char GET_HVAC_VIN[17] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
|
|
|
|
void CanSpamReceiver_Init(tCanSpamReceiver *env,
|
|
tSerialPortFrameIO *ioCanFrame,
|
|
tLoggerInterface *logger) {
|
|
|
|
env->ioCanFrame = ioCanFrame;
|
|
env->logger = logger;
|
|
env->access = osMutexNew(NULL);
|
|
|
|
|
|
env->ccu_candb_rx_LOCAL_HVAC_VIN = osMessageQueueNew(1, sizeof(GET_HVAC_VIN), NULL);
|
|
env->ccu_candb_rx_LOCAL_HVAC_UDS_VIN = osMessageQueueNew(1, sizeof(GET_HVAC_VIN), NULL);
|
|
env->ccu_candb_rx_TM_CP = osMessageQueueNew(1, sizeof(ccu_candb_rx.TM_CP), NULL);
|
|
env->ccu_candb_rx_FIU_CCU1 = osMessageQueueNew(1, sizeof(ccu_candb_rx.FIU_CCU1), NULL);
|
|
env->ccu_candb_rx_FIU_CCU2 = osMessageQueueNew(1, sizeof(ccu_candb_rx.FIU_CCU2), NULL);
|
|
env->ccu_candb_rx_BCM_Powertrain = osMessageQueueNew(1, sizeof(ccu_candb_rx.BCM_Powertrain), NULL);
|
|
env->ccu_candb_rx_BCM_CLIMATIC_DATA = osMessageQueueNew(1, sizeof(ccu_candb_rx.BCM_CLIMATIC_DATA), NULL);
|
|
env->ccu_candb_rx_EMS_Veh = osMessageQueueNew(1, sizeof(ccu_candb_rx.EMS_Veh), NULL);
|
|
env->ccu_candb_rx_ESC_04 = osMessageQueueNew(1, sizeof(ccu_candb_rx.ESC_04), NULL);
|
|
env->ccu_candb_rx_BCM_VEH_STATE = osMessageQueueNew(1, sizeof(ccu_candb_rx.BCM_VEH_STATE), NULL);
|
|
env->ccu_candb_rx_EMS_HVC_Req_Msg = osMessageQueueNew(1, sizeof(ccu_candb_rx.EMS_HVC_Req_Msg), NULL);
|
|
env->ccu_candb_rx_HVC_Err_Status_Msg = osMessageQueueNew(1, sizeof(ccu_candb_rx.HVC_Err_Status_Msg), NULL);
|
|
|
|
|
|
env->thread.stack = tCanSpamReceiver_stack;
|
|
InitThreadAtr(&env->thread.attr, "CanSpamRec", &env->thread.controlBlock, sizeof(env->thread.controlBlock),
|
|
env->thread.stack, tCanSpamReceiver_stack_size, osPriorityNormal);
|
|
env->thread.id = 0;
|
|
|
|
// InitThreadAtrStatic(&env->thread.attr, "CanSpamRec", env->thread.controlBlock, env->thread.stack,
|
|
// osPriorityNormal);
|
|
}
|
|
|
|
static void set_CanSpamReceiver(tCanSpamReceiver *env, uint32_t recid) {
|
|
osStatus_t status = osOK;
|
|
|
|
uint32_t countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_TM_CP);
|
|
if ((countQueue_ccu_candb_rx) && (recid == TM_CP_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_TM_CP, &ccu_candb_rx.TM_CP, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_FIU_CCU1);
|
|
if ((countQueue_ccu_candb_rx) && (recid == FIU_CCU1_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_FIU_CCU1, &ccu_candb_rx.FIU_CCU1, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_FIU_CCU2);
|
|
if ((countQueue_ccu_candb_rx) && (recid == FIU_CCU2_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_FIU_CCU2, &ccu_candb_rx.FIU_CCU2, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_BCM_Powertrain);
|
|
if ((countQueue_ccu_candb_rx) && (recid == BCM_Powertrain_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_BCM_Powertrain, &ccu_candb_rx.BCM_Powertrain, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_BCM_CLIMATIC_DATA);
|
|
if ((countQueue_ccu_candb_rx) && (recid == BCM_CLIMATIC_DATA_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_BCM_CLIMATIC_DATA, &ccu_candb_rx.BCM_CLIMATIC_DATA, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_EMS_Veh);
|
|
if ((countQueue_ccu_candb_rx) && (recid == EMS_Veh_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_EMS_Veh, &ccu_candb_rx.EMS_Veh, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_ESC_04);
|
|
if ((countQueue_ccu_candb_rx) && (recid == ESC_04_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_ESC_04, &ccu_candb_rx.ESC_04, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_BCM_VEH_STATE);
|
|
if ((countQueue_ccu_candb_rx) && (recid == BCM_VEH_STATE_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_BCM_VEH_STATE, &ccu_candb_rx.BCM_VEH_STATE, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_EMS_HVC_Req_Msg);
|
|
if ((countQueue_ccu_candb_rx) && (recid == EMS_HVC_Req_Msg_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_EMS_HVC_Req_Msg, &ccu_candb_rx.EMS_HVC_Req_Msg, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_HVC_Err_Status_Msg);
|
|
if ((countQueue_ccu_candb_rx) && (recid == HVC_Err_Status_Msg_CANID)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_HVC_Err_Status_Msg, &ccu_candb_rx.HVC_Err_Status_Msg, 0, 0U);
|
|
return;
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_LOCAL_HVAC_VIN);
|
|
if ((countQueue_ccu_candb_rx) && (recid == VEH_VIN_CANID)) {
|
|
|
|
if (ccu_candb_rx.VEH_VIN.VIN_Addressing == 0) {
|
|
env->SET_HVAC_VIN[0] = ccu_candb_rx.VEH_VIN.VIN_Char1;
|
|
env->SET_HVAC_VIN[1] = ccu_candb_rx.VEH_VIN.VIN_Char2;
|
|
env->SET_HVAC_VIN[2] = ccu_candb_rx.VEH_VIN.VIN_Char3;
|
|
env->SET_HVAC_VIN[3] = ccu_candb_rx.VEH_VIN.VIN_Char4;
|
|
env->SET_HVAC_VIN[4] = ccu_candb_rx.VEH_VIN.VIN_Char5;
|
|
env->SET_HVAC_VIN[5] = ccu_candb_rx.VEH_VIN.VIN_Char6;
|
|
env->SET_HVAC_VIN[6] = ccu_candb_rx.VEH_VIN.VIN_Char7;
|
|
env->getVIN0 = true;
|
|
}
|
|
|
|
if (ccu_candb_rx.VEH_VIN.VIN_Addressing == 1) {
|
|
env->SET_HVAC_VIN[7] = ccu_candb_rx.VEH_VIN.VIN_Char8;
|
|
env->SET_HVAC_VIN[8] = ccu_candb_rx.VEH_VIN.VIN_Char9;
|
|
env->SET_HVAC_VIN[9] = ccu_candb_rx.VEH_VIN.VIN_Char10;
|
|
env->SET_HVAC_VIN[10] = ccu_candb_rx.VEH_VIN.VIN_Char11;
|
|
env->SET_HVAC_VIN[11] = ccu_candb_rx.VEH_VIN.VIN_Char12;
|
|
env->SET_HVAC_VIN[12] = ccu_candb_rx.VEH_VIN.VIN_Char13;
|
|
env->SET_HVAC_VIN[13] = ccu_candb_rx.VEH_VIN.VIN_Char14;
|
|
env->getVIN1 = true;
|
|
}
|
|
|
|
if (ccu_candb_rx.VEH_VIN.VIN_Addressing == 2) {
|
|
env->SET_HVAC_VIN[14] = ccu_candb_rx.VEH_VIN.VIN_Char15;
|
|
env->SET_HVAC_VIN[15] = ccu_candb_rx.VEH_VIN.VIN_Char16;
|
|
env->SET_HVAC_VIN[16] = ccu_candb_rx.VEH_VIN.VIN_Char17;
|
|
env->getVIN2 = true;
|
|
}
|
|
|
|
if ((env->getVIN0) && (env->getVIN1) && (env->getVIN2)) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_LOCAL_HVAC_VIN, env->SET_HVAC_VIN, 0, 0U);
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_LOCAL_HVAC_UDS_VIN);
|
|
|
|
if (countQueue_ccu_candb_rx) {
|
|
status = osMessageQueuePut(env->ccu_candb_rx_LOCAL_HVAC_UDS_VIN, env->SET_HVAC_VIN, 0, 0U);
|
|
}
|
|
|
|
env->getVIN0 = false;
|
|
env->getVIN1 = false;
|
|
env->getVIN2 = false;
|
|
}
|
|
|
|
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
static void ListenCanSpamReceiver(tCanSpamReceiver *env) {
|
|
|
|
uint16_t recv = env->ioCanFrame->receive(env->ioCanFrame->env, PROTOCOL_CAN_RAW, (uint8_t *) &env->canFrame, 1,
|
|
1000);
|
|
|
|
if (recv == 0) {
|
|
return;
|
|
}
|
|
|
|
if (osMutexAcquire(env->access, 5000) == osOK) {
|
|
|
|
uint32_t id = env->canFrame.standard_id;
|
|
|
|
if (env->canFrame.id_type == FLEXCAN_ID_EXT) {
|
|
id = env->canFrame.extended_id;
|
|
}
|
|
|
|
env->logger->loggingCan(env->logger->env, 1, 0, env->canFrame.dlc, env->canFrame.standard_id,
|
|
env->canFrame.data, SystemGetMs());
|
|
|
|
uint32_t recid = ccu_candb_Receive(&ccu_candb_rx, env->canFrame.data, id, env->canFrame.dlc);
|
|
|
|
set_CanSpamReceiver(env, recid);
|
|
|
|
osMutexRelease(env->access);
|
|
} else {
|
|
LoggerErrorStatic(LOGGER, LOG_SIGN, "Access error ListenCanSpamReceiver");
|
|
}
|
|
|
|
}
|
|
|
|
bool get_is_NoNull_CanSpam_BCM_PowerTrain_Receiver(tCanSpamReceiver *env) {
|
|
if (osMutexAcquire(env->access, 5000) == osOK) {
|
|
|
|
uint8_t *data = (uint8_t *) &ccu_candb_rx.BCM_Powertrain;
|
|
|
|
for (uint8_t i = 0; i < (uint8_t) sizeof(BCM_Powertrain_t); ++i) {
|
|
if (data[i] != 0) {
|
|
osMutexRelease(env->access);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
osMutexRelease(env->access);
|
|
} else {
|
|
LoggerErrorStatic(LOGGER, LOG_SIGN, "Access error get_CanSpamReceiver");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void get_CanSpamReceiver(tCanSpamReceiver *env) {
|
|
|
|
osStatus_t status = osOK;
|
|
|
|
uint32_t countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_TM_CP);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_TM_CP, &rtDW.TM_CP_model, 0, 0U);
|
|
}
|
|
|
|
memset(&rtDW.FIU_CCU1_MODEL, 0, sizeof(rtDW.FIU_CCU1_MODEL));
|
|
memset(&rtDW.FIU_CCU2_MODEL, 0, sizeof(rtDW.FIU_CCU2_MODEL));
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_FIU_CCU1);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_FIU_CCU1, &rtDW.FIU_CCU1_MODEL, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_FIU_CCU2);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_FIU_CCU2, &rtDW.FIU_CCU2_MODEL, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_BCM_Powertrain);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_BCM_Powertrain, &rtDW.CCUCAN_BCM_Powertrain_model, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_BCM_CLIMATIC_DATA);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_BCM_CLIMATIC_DATA, &rtDW.CCUCAN_BCM_Climatic_model, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_EMS_Veh);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_EMS_Veh, &rtDW.CCUCAN_EMS_Veh_model, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_ESC_04);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_ESC_04, &rtDW.CCUCAN_ESC_04_model, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_BCM_VEH_STATE);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_BCM_VEH_STATE, &rtDW.CCUCAN_BCM_VEH_STATE_model, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_EMS_HVC_Req_Msg);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_EMS_HVC_Req_Msg, &rtDW.CCUCAN_EMS_HVC_REQ_MSG_MODEL, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_HVC_Err_Status_Msg);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_HVC_Err_Status_Msg, &rtDW.HVC_ERR_STATUS_MSG_MODEL, 0, 0U);
|
|
}
|
|
|
|
countQueue_ccu_candb_rx = osMessageQueueGetSpace(env->ccu_candb_rx_LOCAL_HVAC_VIN);
|
|
if (countQueue_ccu_candb_rx == 0) {
|
|
status = osMessageQueueGet(env->ccu_candb_rx_LOCAL_HVAC_VIN, GET_HVAC_VIN, 0, 0U);
|
|
}
|
|
|
|
/*
|
|
if (osMutexAcquire(env->access, 5000) == osOK) {
|
|
memcpy(&rtDW.TM_CP_model, &ccu_candb_rx.TM_CP, sizeof(rtDW.TM_CP_model));
|
|
memcpy(&rtDW.FIU_CCU1_MODEL, &ccu_candb_rx.FIU_CCU1, sizeof(rtDW.FIU_CCU1_MODEL));
|
|
memcpy(&rtDW.FIU_CCU2_MODEL, &ccu_candb_rx.FIU_CCU2, sizeof(rtDW.FIU_CCU2_MODEL));
|
|
// memcpy(&rtDW.FIU_CCU3_MODEL, &ccu_candb_rx.FIU_CCU3, sizeof(rtDW.FIU_CCU3_MODEL));
|
|
|
|
memset(&ccu_candb_rx.FIU_CCU1,0,sizeof(ccu_candb_rx.FIU_CCU1));
|
|
memset(&ccu_candb_rx.FIU_CCU2,0,sizeof(ccu_candb_rx.FIU_CCU2));
|
|
|
|
memcpy(&rtDW.CCUCAN_BCM_Powertrain_model, &ccu_candb_rx.BCM_Powertrain,
|
|
sizeof(rtDW.CCUCAN_BCM_Powertrain_model));
|
|
memcpy(&rtDW.CCUCAN_BCM_Climatic_model, &ccu_candb_rx.BCM_CLIMATIC_DATA,
|
|
sizeof(rtDW.CCUCAN_BCM_Climatic_model));
|
|
memcpy(&rtDW.CCUCAN_EMS_Veh_model, &ccu_candb_rx.EMS_Veh, sizeof(rtDW.CCUCAN_EMS_Veh_model));
|
|
memcpy(&rtDW.CCUCAN_ESC_04_model, &ccu_candb_rx.ESC_04, sizeof(rtDW.CCUCAN_ESC_04_model));
|
|
memcpy(&rtDW.CCUCAN_BCM_VEH_STATE_model, &ccu_candb_rx.BCM_VEH_STATE, sizeof(rtDW.CCUCAN_BCM_VEH_STATE_model));
|
|
memcpy(&rtDW.CCUCAN_EMS_HVC_REQ_MSG_MODEL, &ccu_candb_rx.EMS_HVC_Req_Msg,
|
|
sizeof(rtDW.CCUCAN_EMS_HVC_REQ_MSG_MODEL));
|
|
memcpy(&rtDW.HVC_ERR_STATUS_MSG_MODEL, &ccu_candb_rx.HVC_Err_Status_Msg, sizeof(rtDW.HVC_ERR_STATUS_MSG_MODEL));
|
|
|
|
osMutexRelease(env->access);
|
|
|
|
} else {
|
|
LoggerErrorStatic(LOGGER, LOG_SIGN, "Access error get_CanSpamReceiver");
|
|
}
|
|
*/
|
|
|
|
}
|
|
|
|
bool get_CanSpamReceiverUDS(tCanSpamReceiver *env, char *VIN, func_update_func update_func, void *update_func_env,
|
|
uint32_t timeout, uint32_t count) {
|
|
|
|
|
|
uint32_t u32Time = SystemGetMs() + timeout;
|
|
|
|
|
|
for (uint32_t i=0; i<count; ++i) {
|
|
|
|
osStatus_t status = osMessageQueueGet(env->ccu_candb_rx_LOCAL_HVAC_UDS_VIN, VIN, 0, timeout);
|
|
|
|
if (update_func != NULL) {
|
|
if (SystemGetMs() > u32Time) {
|
|
update_func(update_func_env);
|
|
u32Time = SystemGetMs() + timeout;
|
|
}
|
|
}
|
|
|
|
if (status == osOK) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
static _Noreturn void CanSpamReceiver_Thread(tCanSpamReceiver *env) {
|
|
for (;;) {
|
|
ListenCanSpamReceiver(env);
|
|
}
|
|
}
|
|
|
|
void CanSpamReceiver_StartThread(tCanSpamReceiver *env) {
|
|
if (!env->thread.id) {
|
|
env->thread.id = osThreadNew((osThreadFunc_t) (CanSpamReceiver_Thread), (void *) (env), &env->thread.attr);
|
|
}
|
|
} |