MqttAgent: how to cancell publish command with qos2 after timeout

Ah right. Thanks for pointing this out. The problem with the suggested work around is that it will cause the command loop to return with a MQTTSendFailed return code even if the connection with the broker is still up. There is no way to distinguish between the MQTTSendFailed returned due to packet not being sent because of the timeout and the MQTTSendFailed returned due to a broken connection with the broker. So the safer thing to do here is to close the socket and reconnect when the command loop returns an error.

As far as the call to the callback is concerned, it will be called even if the command returns MQTTSendFailed see this part of the code.

...
operationStatus = commandFunction( pMqttAgentContext, 
                                   pCommandArgs, 
                                   &commandOutParams );

    if( ( operationStatus == MQTTSuccess ) && ... )
    {
        ...
    }

    if( ( pCommand != NULL ) && ( ackAdded != true ) )
    {
        /* The command is complete, call the callback. */
        concludeCommand( pMqttAgentContext, pCommand, operationStatus, NULL );
    }
...

This means that you can still free the memory in the callback.

I agree this is not a very clean way of dealing with the problem you are trying to solve. The library does not give support for it and this is just a work around.

1 Like