legatoproject / legato-af

Legato Application Framework
Mozilla Public License 2.0
153 stars 118 forks source link

Platform Service MQTT client - connection lost handling broken #65

Open fishkeeper87 opened 3 years ago

fishkeeper87 commented 3 years ago

Hello, I'm using the platform service for the sample mqtt client directory /apps/platformservices/mqttClient

I've been doing some testing with this client and noted that the logic included inside function ConnectionLostEventHandler never works as the return to the following is always NULL:

mqtt_Session* s = le_ref_Lookup(SessionRefMap, reportPtr);

I looked at how the event handler is factored for the MessageReceivedEventHandler and this implementation is working. I changed the functions as follows and the ConnectionLostEventHandler now processes successfully. I hope this fix can be included to save other people's time in diagnosing what is wrong. I've left my debug statements below.


static void ConnectionLostHandler
(
    void* contextPtr, ///< context parameter contains the session for which the connection was lost
    char* causePtr    ///< paho library doesn't currently populate this
)
{
    LE_DEBUG("ConnectionLostHandler with session 0x%p.",contextPtr);

    mqtt_Message* storedMsgPtr = le_mem_ForceAlloc(MessagePoolRef);
    LE_ASSERT(storedMsgPtr);
    memset(storedMsgPtr, 0, sizeof(*storedMsgPtr));

    storedMsgPtr->sessionRef = contextPtr;
    le_event_Report(ConnectionLostThreadEventId, &storedMsgPtr, sizeof(void*));
}

//--------------------------------------------------------------------------------------------------
/**
 * The event handler for the connection lost event that is generated by ConnectionLostHandler.
 * This function calls the handler supplied by the client.
 */
//--------------------------------------------------------------------------------------------------
static void ConnectionLostEventHandler
(
    void* reportPtr
)
{
    mqtt_Message* storedMsgPtr = *((mqtt_Message**)reportPtr);

    LE_DEBUG("Session lookup for session=%p", storedMsgPtr->sessionRef);
    mqtt_Session* s = le_ref_Lookup(SessionRefMap, storedMsgPtr->sessionRef);
    if (s == NULL)
    {
        LE_ERROR("Session with 0x%p not found.",&reportPtr);
        LE_KILL_CLIENT("Session doesn't exist");
        return;
    }