/
On Complete Callback Type

On Complete Callback Type

This callback function type is used by every callback that gets notified after a message exchange with the broker has been completed, successfully or not. The parameters passed are the same in every callback and are detailed below.

Prototype

typedef  void  (*MQTTc_CMPL_CALLBACK) (MQTTc_CONN  *p_conn,
                                       MQTTc_MSG   *p_msg,
                                       void        *p_arg,
                                       MQTTc_ERR    err);

Arguments

p_conn

Pointer to the current MQTTc Connection Object.

p_msg

Pointer to the MQTTc Message Object used to send the message to the broker.

In the generic complete callback, p_msg->Type can be used to know the type of message that has completed. The other fields of p_msg may not be relied upon.

p_arg

Pointer to the argument specified by the application via MQTTc_ConnSetParam with the MQTTc_PARAM_TYPE_CALLBACK_ARG_PTR type.

err

Error code for this operation.

Return Values

None.

Notes / Warnings

None.

Example Template

Listing - CONNECT Complete Callback Example
static  void  AppMQTTc_OnConnectCmplCallbackFnct (MQTTc_CONN  *p_conn,
                                                  MQTTc_MSG   *p_msg,
                                                  void        *p_arg,
                                                  MQTTc_ERR    err)
{
    MQTTc_ERR  err_mqttc;


    (void)&p_arg;

    if (err != MQTTc_ERR_NONE) {
        printf("ConnectCmpl callback called with err (%i). NOT sending PUBLISH message.\n\r", err);
    } else {
        printf("ConnectCmpl callback called. Sending PUBLISH message.\n\r");
        MQTTc_Publish(p_conn,
                      p_msg,
                      APP_MQTTc_DOMAIN_PUBLISH,
                      APP_MQTTc_PUBLISH_TEST_QoS,
                      DEF_YES,
                      APP_MQTTc_PUBLISH_TEST_MSG,
                      Str_Len(APP_MQTTc_PUBLISH_TEST_MSG),
                     &err_mqttc);
        if (err_mqttc != MQTTc_ERR_NONE) {
            printf("!!! APP ERROR !!! Failed to Publish test string. Err: %i\n\r.", err_mqttc);
        }
    }
}



Listing - Generic Complete Callback Example
static  void  AppMQTTc_OnCmplCallbackFnct (MQTTc_CONN  *p_conn,
                                           MQTTc_MSG   *p_msg,
                                           void        *p_arg,
                                           MQTTc_ERR    err)
{
    (void)&p_conn;
    (void)&p_arg;

    if (err != MQTTc_ERR_NONE) {
        printf("Operation completed with err (%i). ", err);
    }

    switch (p_msg->Type) {
        case MQTTc_MSG_TYPE_CONNECT:                            /* Gen callback called for event type: Connect Cmpl.    */
             printf("Gen callback called for event type: Connect Cmpl.\n\r");
             break;
        case MQTTc_MSG_TYPE_PUBLISH:                            /* Gen callback called for event type: Publish Cmpl.    */
             printf("Gen callback called for event type: Publish Cmpl.\n\r");
             break;
        case MQTTc_MSG_TYPE_SUBSCRIBE:                          /* Gen callback called for event type: Subscribe Cmpl.  */
             printf("Gen callback called for event type: Subscribe Cmpl.\n\r");
             break;
        case MQTTc_MSG_TYPE_UNSUBSCRIBE:                        /* Gen callback called for event type: Unsubscribe Cmpl.*/
             printf("Gen callback called for event type: Unsubscribe Cmpl.\n\r");
             break;
        case MQTTc_MSG_TYPE_PINGREQ:                            /* Gen callback called for event type: PingReq Cmpl.    */
             printf("Gen callback called for event type: PingReq Cmpl.\n\r");
             break;
        case MQTTc_MSG_TYPE_DISCONNECT:                         /* Gen callback called for event type: Disconnect Cmpl. */
             printf("Gen callback called for event type: Disconnect Cmpl.\n\r");
             break;
        default:
             printf("Gen callback called for event type: default. !!! ERROR !!! %i\n\r", p_msg->Type);
             break;
    }
}


Related content