我有以下代码
typedef struct{
uint8_t carOpening:2;
uint8_t fCarElement:4;
}lockState_t;
enum Doors
{
DRIVER_DOOR = 0,
PASSENGER_DOOR,
...
NUMBER_OF_ELEMENTS
};
class DoorState
{
public:
DoorState(Doors door, uint8_t initialState)
{
fState.fCarElement = static_cast<uint8_t>(door);
if(initialState == SIG_VAL_DOOR_CLOSED)
{
fState.carOpening = static_cast<uint8_t>(DOOR_CLOSED);
}
else if(initialState == SIG_VAL_DOOR_OPENED)
{
fState.carOpening = static_cast<uint8_t>(DOOR_OPENED);
}
else
{
fState.carOpening = static_cast<uint8_t>(DOOR_UNKNOWN);
}
};
void checkSignal(uint8_t signal);
enum tState { DOOR_UNKNOWN = 0, DOOR_CLOSED, DOOR_OPENED };
private:
static const uint8_t SIG_VAL_DOOR_CLOSED = 0x00;
static const uint8_t SIG_VAL_DOOR_OPENED = 0x01;
lockState_t fState;
};
然后我有一些此类的实例,例如:
DoorState fDriverDoorState;
DoorState fPassengerDoorState;
这是我初始化实例的方式:
, fDriverDoorState(DRIVER_DOOR
, DS::instance().get<uint8_t>(DS::ComStatZvKlappen_StDswDrd_ID))
, fPassengerDoorState(PASSENGER_DOOR, DS::instance().get<uint8_t>(DS::ComStatZvKlappen_StDswPsd_ID))
然后我正在检查门的状态是否已更改,并发布一条消息,如果它具有:
tState doorState = DOOR_UNKNOWN;
if (SIG_VAL_DOOR_CLOSED == signal)
{
doorState = DOOR_CLOSED;
}
else if (SIG_VAL_DOOR_OPENED == signal)
{
doorState = DOOR_OPENED;
}
if(doorState != static_cast<tState>(fState.carOpening))
{
fState.carOpening = static_cast<uint8_t>(doorState);
if(DOOR_UNKNOWN != doorState)
{
LooperEventBroker::publish(::csm::events::CsmEvents::VEHICLE_LOCK_STATE_EVENT(), &fState);
Logger::info(VEHICLE, "Door %s event published for %s",
doorState == DOOR_OPENED ? "openend" : "closed", opening2string(fState.fCarElement));
}
}
在大多数情况下,它运行良好。奇怪的是,有时我看到此日志被发布: “不会为INVALID DOOR发布门关闭事件”,这不会发生,因为没有实例使用INVALID DOOR初始化,并且从未修改存储门类型的fCarElement。这意味着fCarElement以某种方式被损坏。问题是在哪里或为什么?
这里要求执行opening2string函数:
static const char* opening2string(uint8_t opening)
{
switch (opening)
{
case DRIVER_DOOR:
return "DRIVER DOOR";
case PASSENGER_DOOR:
return "PASSENGER DOOR";
...
default:
return "INVALID DOOR";
}
}
关于, 加布里埃尔