static MQTTStatus_t createAndAddCommand()

in source/core_mqtt_agent.c [702:758]


static MQTTStatus_t createAndAddCommand( MQTTAgentCommandType_t commandType,
                                         const MQTTAgentContext_t * pMqttAgentContext,
                                         void * pMqttInfoParam,
                                         MQTTAgentCommandCallback_t commandCompleteCallback,
                                         MQTTAgentCommandContext_t * pCommandCompleteCallbackContext,
                                         uint32_t blockTimeMs )
{
    MQTTStatus_t statusReturn = MQTTBadParameter;
    MQTTAgentCommand_t * pCommand;
    bool commandReleased = false;

    /* If the packet ID is zero then the MQTT context has not been initialized as 0
     * is the initial value but not a valid packet ID. */
    if( pMqttAgentContext->mqttContext.nextPacketId != MQTT_PACKET_ID_INVALID )
    {
        pCommand = pMqttAgentContext->agentInterface.getCommand( blockTimeMs );

        if( pCommand != NULL )
        {
            statusReturn = createCommand( commandType,
                                          pMqttAgentContext,
                                          pMqttInfoParam,
                                          commandCompleteCallback,
                                          pCommandCompleteCallbackContext,
                                          pCommand );

            if( statusReturn == MQTTSuccess )
            {
                statusReturn = addCommandToQueue( pMqttAgentContext, pCommand, blockTimeMs );
            }

            if( statusReturn != MQTTSuccess )
            {
                /* Could not send the command to the queue so release the command
                 * structure again. */
                commandReleased = pMqttAgentContext->agentInterface.releaseCommand( pCommand );

                if( !commandReleased )
                {
                    LogError( ( "Command %p could not be released.",
                                ( void * ) pCommand ) );
                }
            }
        }
        else
        {
            /* Ran out of MQTTAgentCommand_t structures - pool is empty. */
            statusReturn = MQTTNoMemory;
        }
    }
    else
    {
        LogError( ( "MQTT context must be initialized." ) );
    }

    return statusReturn;
}