mardi 18 décembre 2018

What might cause a value that is only set at initialization to get corrupted?

I have the following code

    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;
};

Then I have a few instances of this class, like:

    DoorState                                                            fDriverDoorState;
    DoorState                                                            fPassengerDoorState;

This is how I initialize the instances:

    , fDriverDoorState(DRIVER_DOOR, DS::instance().get<uint8_t>(DS::ComStatZvKlappen_StDswDrd_ID))
    , fPassengerDoorState(PASSENGER_DOOR, DS::instance().get<uint8_t>(DS::ComStatZvKlappen_StDswPsd_ID))

Then I am checking if the state of the door has changed and publising a message if it has:

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));
    }
}

For the most part it is working perfectly. The strange part is that sometimes I see this log being posted: "Door closed event published for INVALID DOOR", which should not happen as no instance is initialized with INVALID DOOR, and fCarElement, which stores the door type, is never modified. Which means that somehow fCarElement gets corrupted. Question is where or why?

Regards, Gabriel

Aucun commentaire:

Enregistrer un commentaire