我有以下代码
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";
}
}
问候,加布里埃尔