Overview

group index

Cypress MQTT library provides an easy-to-use APIs for Cypress devices to connect with cloud MQTT brokers and perform MQTT publish and subscribe operations.

Library Components

This MQTT library consists of two components:

1) MQTT wrapper layer.

2) AWS IoT Device SDK C library.

AWS IoT Device SDK library is an open source library and it uses the aws-iot-device-sdk-port library functions to perform MQTT network related operations. The application that uses this library should invoke the MQTT wrapper layer API functions and which internally calls AWS IoT Device SDK library and aws-iot-device-sdk-port library API functions to perform MQTT publish/subscribe operations.

AWS-IoT Device SDK C Library Link

MQTT wrapper layer

This wrapper layer provides the APIs to users that are required to perform MQTT publish and subscribe operations on the Cypress platform. This layer contains APIs that are used for creating MQTT instances, Establishing Secure or non secure MQTT client session with MQTT broker to send and receive MQTT messages. This layer also handles retry logic for failed publish messages.

AWS IoT Device SDK C - Tag: 202011.00

This is an open source MQTT library developed and maintained by Amazon. This library implements the core MQTT protocol logic.

  • “AWS IoT Device SDK C” library provides following features:

    • MQTT 3.1.1 client

    • Synchronous API for MQTT operations.

    • Multithreaded API by default.

    • Complete separation of MQTT and network stack, allowing MQTT to run on top of any network stack.

    • MQTT persistent session support.

    • Supports Quality of Service (QoS) levels 0, 1 and 2.

    • Supports MQTT connection over both secured and non secured TCP connections.

Supported Platforms

This library and its features are supported on following Cypress platforms:

Dependent libraries

This MQTT client library depends on the following libraries. Both these libraries are included by default.

Quick start

  • The application must include *./include/cy_mqtt_api.h* to use library API’s and structure.

  • The reference *./include/core_mqtt_config.h* file that is bundled with this library provides the configurations required for the AWS IoT Device SDK library. The application must copy this file to the root directory where the application Makefile is present, and suitably adjust the default settings.

  • A “reasonable amount of time” to wait for keepalive response from the MQTT broker is configured using MQTT_PINGRESP_TIMEOUT_MS in *./include/core_mqtt_config.h*. This value may be adjusted to suit the use case and network environment.

  • A “reasonable amount of time” to wait for receiving the acknowledgment packet from the MQTT broker for MQTT publish/Subscribe messages with QoS1/QoS2. The timeout value can be configured using the macro CY_MQTT_ACK_RECEIVE_TIMEOUT_MS in the application makefile. This value may be adjusted to suit the use case and network conditions. The Makefile entry would look like the following:

    DEFINES += CY_MQTT_ACK_RECEIVE_TIMEOUT_MS=3000
    

  • MQTT message send timeout can be configured using the macro CY_MQTT_MESSAGE_SEND_TIMEOUT_MS in the application makefile. This value can be adjusted to suit the use case and network conditions. The Makefile entry would look like the following:

    DEFINES += CY_MQTT_MESSAGE_SEND_TIMEOUT_MS=3000
    

  • MQTT message receive timeout can be configured using the macro CY_MQTT_MESSAGE_RECEIVE_TIMEOUT_MS in the application makefile. This value can be adjusted to suit the use case and network conditions. The Makefile entry would look like the following:

    DEFINES += CY_MQTT_MESSAGE_RECEIVE_TIMEOUT_MS=500
    

  • MQTT library provides a retry mechanism for MQTT publish/subscribe/unsubcribe messages, incase if the acknowledgement is not received from the broker on time. User can configure the maximum number of retries by defining the macro CY_MQTT_MAX_RETRY_VALUE in the application makefile. The Makefile entry would look like the following:

    DEFINES += CY_MQTT_MAX_RETRY_VALUE=3
    

  • This MQTT Client library does not support secure connections to the public test.mosquitto.org broker by default. because the server uses the SHA1 hashing algorithm. As cautioned by Mbed TLS, SHA-1 is considered a weak message digest and is therefore not enabled in Mbed TLS by default. The use of SHA-1 for certificate signing constitutes a security risk. It is recommended to avoid dependencies on it, and consider stronger message digests instead.

  • A set of pre-defined configuration files have been bundled with wifi-mw-core library for FreeRTOS, LwIP, and Mbed TLS. You should review the configuration and make the required adjustments. See the “Quick Start” section in README.md for more details.

  • Define following COMPONENTS in the application’s makefile for the MQTT Library. For additional information, see the “Quick Start” section in README.md

    COMPONENTS=FREERTOS MBEDTLS LWIP SECURE_SOCKETS
    

  • The “aws-iot-device-sdk-port” layer includes the “coreHTTP” and “coreMQTT” modules of the “aws-iot-device-sdk-embedded-C” library by default. If the user application doesn’t use HTTP client features, then update the application makefile to exclude the coreHTTP source files from the build. The Makefile entry would look like the following:

    CY_IGNORE+= $(SEARCH_aws-iot-device-sdk-embedded-C)/libraries/standard/coreHTTP
    CY_IGNORE+= libs/aws-iot-device-sdk-embedded-C/libraries/standard/coreHTTP
    

  • MQTT Library disables all the debug log messages by default. To enable log messages, the application must perform the following:

    1. Add ENABLE_MQTT_LOGS macro to the DEFINES in the code example’s Makefile. The Makefile entry would look like as follows:

      DEFINES+=ENABLE_MQTT_LOGS
      

    2. Call the cy_log_init() function provided by the cy-log module. cy-log is part of the connectivity-utilities library. See connectivity-utilities library API documentation for cy-log details.

Library Usage Notes

Addtional Information

API Call Sequence

This section provides the details of the API call sequence for performing various MQTT operations. This call sequence uses APIs defined by MQTT library.

For Init, Create, Connect, Publish and Subscribe

The API call sequence given below should be used to connect to a MQTT broker and to perform MQTT subscribe and publish operations:

                    +-----------------------+
                    |     cy_mqtt_init()    |
                    +-----------------------+
                                |
                                v
                    +-----------------------+
                    |    cy_mqtt_create()   |
                    +-----------------------+
                                |
                                v
                    +-----------------------+
                    |   cy_mqtt_connect()   |
                    +-----------------------+
                                |
                                v
                    +-----------------------+
                    |  cy_mqtt_subscribe()  |
                    +-----------------------+
                                |
                                v
                    +-----------------------+
                    |   cy_mqtt_publish()   |
                    +-----------------------+

For UnSubscribe, Disconnect, Delete and DeInit.

The API call sequence given below should be used to perform MQTT unsubscribe and disconnect operations with the MQTT broker:

                   +-----------------------+
                   | cy_mqtt_unsubscribe() |
                   +-----------------------+
                               |
                               v
                   +-----------------------+
                   |  cy_mqtt_disconnect() |
                   +-----------------------+
                               |
                               v
                   +-----------------------+
                   |    cy_mqtt_delete()   |
                   +-----------------------+
                               |
                               v
                   +-----------------------+
                   |    cy_mqtt_deinit()   |
                   +-----------------------+

Note :

  • Functions cy_mqtt_init and cy_mqtt_deinit are not thread-safe.

  • Application should allocate network buffer and same buffer has to be passed while creating the MQTT object. MQTT library use this buffer for sending and receiving MQTT packets. CY_MQTT_MIN_NETWORK_BUFFER_SIZE is the minimun size of network buffer required for library to handle MQTT packets.

  • Application should call cy_mqtt_init before calling any other MQTT library function, and should not call other MQTT library functions after calling cy_mqtt_deinit.

  • MQTT instance is created using cy_mqtt_create function. MQTT library allocates resource for each created instance, hence application should delete the MQTT instance by calling cy_mqtt_delete after performing the desired MQTT operations.

  • After calling cy_mqtt_delete API function, all the resource associated with MQTT handle are freed. So user should not use same MQTT handle after delete.

  • MQTT disconnection event notification is not sent to application when disconnection is initiated from application.

  • When application receives MQTT disconnection event notification due to physical network disconnection or not receiving the PING response on time, application should call cy_mqtt_disconnect function to release the resource allocated during cy_mqtt_connect.

Code Snippets

Code Snippet 1: Initialize MQTT network socket

This code snippet demonstrates the initialization of the network sockets required for the MQTT library.


void snippet_cy_mqtt_init( void )
{
    /* Status variables for various operations. */
    cy_rslt_t TestRes = CY_RSLT_SUCCESS;
    (void)TestRes;

    /* Initialize the MQTT network socket. */
    TestRes = cy_mqtt_init();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
    }
}

Code Snippet 2: Creating MQTT instance for Secure connection with MQTT broker

This code snippet demonstrates the initialization of the configuration structures required for creating an MQTT client handle for establishing secured connection with MQTT broker, and the usage of the cy_mqtt_create() API function.


#define MQTT_BROKER                         " " /* Add the broker end point info here. */
#define MQTT_PORT_SECURED                   ( 8883 )

#define NETWORK_BUFFER_SIZE                 ( 1024U )

static const char client_cert[] = { /* Client certificate here.*/ };
static const char client_key[]  = { /* Client key here.*/ };
static const char root_ca_cert[] = { /* Root CA here.*/ };

cy_mqtt_t         secure_mqtthandle;

/* Callback function registered with MQTT library while creating handle for MQTT connection.
   This callback function will be called by MQTT library upon receiving MQTT messages on subscribed topics or when
   network disconnection happens. */
void mqtt_secure_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_received_msg_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;

    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;

        case CY_MQTT_EVENT_TYPE_SUBSCRIPTION_MESSAGE_RECEIVE :
            /* Received MQTT messages on subscribed topic. */
            received_msg = &(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
            break;

        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}

void snippet_cy_mqtt_create_secure( void )
{
    /* Status variables for various operations. */
    cy_rslt_t                     TestRes = CY_RSLT_SUCCESS;
    cy_mqtt_broker_info_t         broker_info;
    cy_awsport_ssl_credentials_t  credentials;
    cy_awsport_ssl_credentials_t  *security = NULL;
    uint8_t                       *buffer = NULL;
    (void)TestRes;

    /* Initialize the MQTT network socket. */
    TestRes = cy_mqtt_init();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /*Allocate the network buffer. */
    buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
    if( buffer == NULL )
    {
        /* Failure path. */
        return;
    }

    memset( &broker_info, 0x00, sizeof( cy_mqtt_broker_info_t ) );
    memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );

    /* Set the credential information. */
    credentials.client_cert = (const char *) &client_cert;
    credentials.client_cert_size = sizeof( client_cert );
    credentials.private_key = (const char *) &client_key;
    credentials.private_key_size = sizeof( client_key );
    credentials.root_ca = (const char *) &root_ca_cert;
    credentials.root_ca_size = sizeof( root_ca_cert );

    security = &credentials;

    /* MQTT broker information. */
    broker_info.hostname = MQTT_BROKER;
    broker_info.hostname_len = strlen(MQTT_BROKER);
    broker_info.port = MQTT_PORT_SECURED;

    /* Create MQTT Handle. */
    TestRes = cy_mqtt_create( buffer, NETWORK_BUFFER_SIZE,
                              security, &broker_info,
                              (cy_mqtt_callback_t)mqtt_secure_event_cb, NULL,
                              &secure_mqtthandle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }
}

Code Snippet 3: Creating MQTT instance for Non secure connection with MQTT broker

This code snippet demonstrates the initialization of the configuration structures required for creating an MQTT client handle for establishing non secured connection with MQTT broker, and the usage of the cy_mqtt_create() API function.


#define MQTT_BROKER                         " " /* Add the broker end point info here. */
#define MQTT_PORT_NON_SECURED               ( 1883 )

#define NETWORK_BUFFER_SIZE                 ( 1024U )

cy_mqtt_t         nonsecure_mqtthandle;

/* Callback function registered with MQTT library while creating handle for MQTT connection.
   This callback function will be called by MQTT library upon receiving MQTT messages on subscribed topics or when
   network disconnection happens. */
void mqtt_non_secure_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_received_msg_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;

    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;

        case CY_MQTT_EVENT_TYPE_SUBSCRIPTION_MESSAGE_RECEIVE :
            /* Received MQTT messages on subscribed topic. */
            received_msg = &(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
            break;

        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}

void snippet_cy_mqtt_create_non_secure( void )
{
    /* Status variables for various operations. */
    cy_rslt_t                     TestRes = CY_RSLT_SUCCESS;
    cy_mqtt_broker_info_t         broker_info;
    uint8_t                       *buffer = NULL;
    (void)TestRes;

    /* Initialize the MQTT network socket. */
    TestRes = cy_mqtt_init();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /*Allocate the network buffer. */
    buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
    if( buffer == NULL )
    {
        /* Failure path. */
        return;
    }

    memset( &broker_info, 0x00, sizeof( cy_mqtt_broker_info_t ) );

    /* MQTT broker information. */
    broker_info.hostname = MQTT_BROKER;
    broker_info.hostname_len = strlen(MQTT_BROKER);
    broker_info.port = MQTT_PORT_NON_SECURED;

    /* Create MQTT Handle. */
    TestRes = cy_mqtt_create( buffer, NETWORK_BUFFER_SIZE,
                              NULL, &broker_info,
                              (cy_mqtt_callback_t)mqtt_non_secure_event_cb, NULL,
                              &nonsecure_mqtthandle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }
}

Code Snippet 4: Establishing MQTT client session

This code snippet demonstrates the steps to establish MQTT client session with MQTT broker using the previously created MQTT handle, and the usage of the cy_mqtt_connect() API function.


#define MQTT_BROKER                         " " /* Add the broker end point info here. */
#define MQTT_PORT_SECURED                   ( 8883 )

#define MQTT_CLIENT_IDENTIFIER              "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH       ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS    ( 60U )

#define MQTT_QOS2                           ( 2U )
#define MQTT_TOPIC                          "Test_Topic"
#define MQTT_TOPIC_LENGTH                   ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE                   "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH            ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )

#define NETWORK_BUFFER_SIZE                 ( 1024U )

static const char clientcert[] = { /* Client certificate here.*/ };
static const char clientkey[]  = { /* Client key here.*/ };
static const char rootcacertificate[] = { /* Root CA here.*/ };

cy_mqtt_t         mqtthandle;

/* Callback function registered with MQTT library while creating handle for MQTT connection.
   This callback function will be called by MQTT library upon receiving MQTT messages on subscribed topics or when
   network disconnection happens. */
void mqtt_event_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_received_msg_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;

    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;

        case CY_MQTT_EVENT_TYPE_SUBSCRIPTION_MESSAGE_RECEIVE :
            /* Received MQTT messages on subscribed topic. */
            received_msg = &(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
            break;

        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}

void snippet_cy_mqtt_connect( void )
{
    /* Status variables for various operations. */
    cy_rslt_t                     TestRes = CY_RSLT_SUCCESS;
    cy_mqtt_publish_info_t        will_msg;
    cy_mqtt_connect_info_t        connect_info;
    cy_mqtt_broker_info_t         broker_info;
    cy_awsport_ssl_credentials_t  credentials;
    cy_awsport_ssl_credentials_t  *security = NULL;
    uint8_t                       *buffer = NULL;
    (void)TestRes;

    /* Initialize the MQTT network socket. */
    TestRes = cy_mqtt_init();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /*Allocate the network buffer. */
    buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
    if( buffer == NULL )
    {
        /* Failure path. */
        return;
    }

    memset( &will_msg, 0x00, sizeof( cy_mqtt_publish_info_t ) );
    memset( &broker_info, 0x00, sizeof( cy_mqtt_broker_info_t ) );
    memset( &connect_info, 0x00, sizeof( cy_mqtt_connect_info_t ) );
    memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );

    /* Set the will information. */
    will_msg.qos = MQTT_QOS2;
    will_msg.topic = MQTT_TOPIC;
    will_msg.topic_len = MQTT_TOPIC_LENGTH;
    will_msg.payload = MQTT_WILL_MESSAGE;
    will_msg.payload_len = MQTT_WILL_MESSAGE_LENGTH;

    /* Set the credential information. */
    credentials.client_cert = (const char *) &clientcert;
    credentials.client_cert_size = sizeof( clientcert );
    credentials.private_key = (const char *) &clientkey;
    credentials.private_key_size = sizeof( clientkey );
    credentials.root_ca = (const char *) &rootcacertificate;
    credentials.root_ca_size = sizeof( rootcacertificate );

    security = &credentials;

    connect_info.client_id = MQTT_CLIENT_IDENTIFIER;
    connect_info.client_id_len = MQTT_CLIENT_IDENTIFIER_LENGTH;
    connect_info.keep_alive_sec = MQTT_KEEP_ALIVE_INTERVAL_SECONDS;
    connect_info.will_info = &will_msg;

    /* MQTT broker information. */
    broker_info.hostname = MQTT_BROKER;
    broker_info.hostname_len = strlen(MQTT_BROKER);
    broker_info.port = MQTT_PORT_SECURED;

    /* Create MQTT Handle. */
    TestRes = cy_mqtt_create( buffer, NETWORK_BUFFER_SIZE,
                              security, &broker_info,
                              (cy_mqtt_callback_t)mqtt_event_cb, NULL,
                              &mqtthandle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    TestRes = cy_mqtt_connect( mqtthandle, &connect_info );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }
}

Code Snippet 5: MQTT subscribe and unsubscribe

This code snippet demonstrates the steps to subscribe for particular topic, unsubscribe from previously subscribed MQTT topic, and the usage of the cy_mqtt_subscribe() and cy_mqtt_unsubscribe() API functions.


#define MQTT_BROKER                         " " /* Add the broker end point info here. */
#define MQTT_PORT_SECURED                   ( 8883 )

#define MQTT_CLIENT_IDENTIFIER              "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH       ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS    ( 60U )

#define MQTT_QOS2                           ( 2U )
#define MQTT_TOPIC                          "Test_Topic"
#define MQTT_TOPIC_LENGTH                   ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE                   "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH            ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )

#define MQTT_NUM_OF_SUBS_IN_SINGLE_REQ      ( 1U )
#define MQTT_NUM_OF_UNSUBS_IN_SINGLE_REQ    MQTT_NUM_OF_SUBS_IN_SINGLE_REQ

#define NETWORK_BUFFER_SIZE                 ( 1024U )

static const char client_certificate[]  = { /* Client certificate here.*/ };
static const char client_private_key[]  = { /* Client key here.*/ };
static const char root_ca_certificate[] = { /* Root CA here.*/ };
cy_mqtt_t         mqtt_handle;

/* Callback function registered with MQTT library while creating handle for MQTT connection.
   This callback function will be called by MQTT library upon receiving MQTT messages on subscribed topics or when
   network disconnection happens. */
void mqtt_eventcb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_received_msg_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;

    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;

        case CY_MQTT_EVENT_TYPE_SUBSCRIPTION_MESSAGE_RECEIVE :
            /* Received MQTT messages on subscribed topic. */
            received_msg = &(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
            break;

        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}

void snippet_cy_mqtt_subscribe_unsubscribe( void )
{
    /* Status variables for various operations. */
    cy_rslt_t                     TestRes = CY_RSLT_SUCCESS;
    cy_mqtt_publish_info_t        will_msg;
    cy_mqtt_connect_info_t        connect_info;
    cy_mqtt_broker_info_t         broker_info;
    cy_awsport_ssl_credentials_t  credentials;
    cy_awsport_ssl_credentials_t  *security = NULL;
    uint8_t                       *buffer = NULL;
    cy_mqtt_subscribe_info_t      sub_msg[1];
    cy_mqtt_unsubscribe_info_t    unsub_msg[1];

    (void)TestRes;

    /* Initialize the MQTT network socket. */
    TestRes = cy_mqtt_init();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /*Allocate the network buffer. */
    buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
    if( buffer == NULL )
    {
        /* Failure path. */
        return;
    }

    memset( &will_msg, 0x00, sizeof( cy_mqtt_publish_info_t ) );
    memset( &broker_info, 0x00, sizeof( cy_mqtt_broker_info_t ) );
    memset( &connect_info, 0x00, sizeof( cy_mqtt_connect_info_t ) );
    memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
    memset( &sub_msg, 0x00, sizeof( cy_mqtt_subscribe_info_t ) );
    memset( &unsub_msg, 0x00, sizeof( cy_mqtt_unsubscribe_info_t ) );

    /* Set the will information. */
    will_msg.qos = MQTT_QOS2;
    will_msg.topic = MQTT_TOPIC;
    will_msg.topic_len = MQTT_TOPIC_LENGTH;
    will_msg.payload = MQTT_WILL_MESSAGE;
    will_msg.payload_len = MQTT_WILL_MESSAGE_LENGTH;

    /* Set the credential information. */
    credentials.client_cert = (const char *) &client_certificate;
    credentials.client_cert_size = sizeof( client_certificate );
    credentials.private_key = (const char *) &client_private_key;
    credentials.private_key_size = sizeof( client_private_key );
    credentials.root_ca = (const char *) &root_ca_certificate;
    credentials.root_ca_size = sizeof( root_ca_certificate );

    security = &credentials;

    connect_info.client_id = MQTT_CLIENT_IDENTIFIER;
    connect_info.client_id_len = MQTT_CLIENT_IDENTIFIER_LENGTH;
    connect_info.keep_alive_sec = MQTT_KEEP_ALIVE_INTERVAL_SECONDS;
    connect_info.will_info = &will_msg;

    /* MQTT broker information. */
    broker_info.hostname = MQTT_BROKER;
    broker_info.hostname_len = strlen(MQTT_BROKER);
    broker_info.port = MQTT_PORT_SECURED;

    /* Create MQTT Handle. */
    TestRes = cy_mqtt_create( buffer, NETWORK_BUFFER_SIZE,
                              security, &broker_info,
                              (cy_mqtt_callback_t)mqtt_eventcb, NULL,
                              &mqtt_handle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    TestRes = cy_mqtt_connect( mqtt_handle, &connect_info );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /* Subscribe request. */
    sub_msg[0].qos = MQTT_QOS2;
    sub_msg[0].topic = MQTT_TOPIC;
    sub_msg[0].topic_len = MQTT_TOPIC_LENGTH;

    TestRes = cy_mqtt_subscribe( mqtt_handle, &sub_msg[0], MQTT_NUM_OF_SUBS_IN_SINGLE_REQ );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /* Check status of MQTT broker allocated QoS level for subscribed topics in subscribe request. */
    if( sub_msg[0].allocated_qos == CY_MQTT_QOS_INVALID )
    {
        /* MQTT broker rejected subscribe request for the given topic/QoS level. */
    }
    else
    {
        /* MQTT broker assigned the QoS level 'sub_msg[0].allocated_qos' for the subscribed topic. */
    }

    /*
     * Wait for some time for subscribed topic messages.
     * Once after receiving the message on the subscribed topic, callback function
     * mqtt_eventcb() will be called from mqtt library context with
     * receive event type 'CY_MQTT_EVENT_TYPE_SUBSCRIPTION_MESSAGE_RECEIVE' and payload.
     *
     * ----- Add the code logic here to wait for the subscribed messages. -----
     *
     */

    /* Unsubscribe request. */
    unsub_msg[0].qos = MQTT_QOS2;
    unsub_msg[0].topic = MQTT_TOPIC;
    unsub_msg[0].topic_len = MQTT_TOPIC_LENGTH;

    TestRes = cy_mqtt_unsubscribe( mqtt_handle, &unsub_msg[0], MQTT_NUM_OF_UNSUBS_IN_SINGLE_REQ );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }
}

Code Snippet 6: MQTT publish

This code snippet demonstrates the steps to publish messages on MQTT topic, and the usage of the cy_mqtt_publish() API function. User can choose appropriate QoS level for the application. In this snippet QoS level is set as 2.


#define MQTT_BROKER                         " " /* Add the broker end point info here. */
#define MQTT_PORT_SECURED                   ( 8883 )

#define MQTT_CLIENT_IDENTIFIER              "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH       ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS    ( 60U )

#define MQTT_QOS2                           ( 2U )
#define MQTT_TOPIC                          "Test_Topic"
#define MQTT_TOPIC_LENGTH                   ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE                   "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH            ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )

#define MQTT_PUB_MESSAGE                    "Publish - Hello MQTT!"
#define MQTT_PUB_MESSAGE_LENGTH             ( ( uint16_t ) ( sizeof( MQTT_PUB_MESSAGE ) - 1 ) )

#define NETWORK_BUFFER_SIZE                 ( 1024U )

static const char clientcertificate[]  = { /* Client certificate here.*/ };
static const char clientprivate_key[]  = { /* Client key here.*/ };
static const char rootca_certificate[] = { /* Root CA here.*/ };
cy_mqtt_t         MQTThandle;

/* Callback function registered with MQTT library while creating handle for MQTT connection.
   This callback function will be called by MQTT library upon receiving MQTT messages on subscribed topics or when
   network disconnection happens. */
void mqtt_event_call_back( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_received_msg_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;

    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;

        case CY_MQTT_EVENT_TYPE_SUBSCRIPTION_MESSAGE_RECEIVE :
            /* Received MQTT messages on subscribed topic. */
            received_msg = &(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
            break;

        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}

void snippet_cy_mqtt_publish( void )
{
    /* Status variables for various operations. */
    cy_rslt_t                     TestRes = CY_RSLT_SUCCESS;
    cy_mqtt_publish_info_t        will_msg;
    cy_mqtt_connect_info_t        connect_info;
    cy_mqtt_broker_info_t         broker_info;
    cy_awsport_ssl_credentials_t  credentials;
    cy_awsport_ssl_credentials_t  *security = NULL;
    uint8_t                       *buffer = NULL;
    cy_mqtt_publish_info_t        pub_msg;

    (void)TestRes;

    /* Initialize the MQTT network socket. */
    TestRes = cy_mqtt_init();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /*Allocate the network buffer. */
    buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
    if( buffer == NULL )
    {
        /* Failure path. */
        return;
    }

    memset( &will_msg, 0x00, sizeof( cy_mqtt_publish_info_t ) );
    memset( &broker_info, 0x00, sizeof( cy_mqtt_broker_info_t ) );
    memset( &connect_info, 0x00, sizeof( cy_mqtt_connect_info_t ) );
    memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
    memset( &pub_msg, 0x00, sizeof( cy_mqtt_publish_info_t ) );

    /* Set the will information. */
    will_msg.qos = MQTT_QOS2;
    will_msg.topic = MQTT_TOPIC;
    will_msg.topic_len = MQTT_TOPIC_LENGTH;
    will_msg.payload = MQTT_WILL_MESSAGE;
    will_msg.payload_len = MQTT_WILL_MESSAGE_LENGTH;

    /* Set the credential information. */
    credentials.client_cert = (const char *) &clientcertificate;
    credentials.client_cert_size = sizeof( clientcertificate );
    credentials.private_key = (const char *) &clientprivate_key;
    credentials.private_key_size = sizeof( clientprivate_key );
    credentials.root_ca = (const char *) &rootca_certificate;
    credentials.root_ca_size = sizeof( rootca_certificate );

    security = &credentials;

    connect_info.client_id = MQTT_CLIENT_IDENTIFIER;
    connect_info.client_id_len = MQTT_CLIENT_IDENTIFIER_LENGTH;
    connect_info.keep_alive_sec = MQTT_KEEP_ALIVE_INTERVAL_SECONDS;
    connect_info.will_info = &will_msg;

    /* MQTT broker information. */
    broker_info.hostname = MQTT_BROKER;
    broker_info.hostname_len = strlen(MQTT_BROKER);
    broker_info.port = MQTT_PORT_SECURED;

    /* Create MQTT Handle. */
    TestRes = cy_mqtt_create( buffer, NETWORK_BUFFER_SIZE,
                              security, &broker_info,
                              (cy_mqtt_callback_t)mqtt_event_call_back, NULL,
                              &MQTThandle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    TestRes = cy_mqtt_connect( MQTThandle, &connect_info );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    pub_msg.qos = MQTT_QOS2;
    pub_msg.topic = MQTT_TOPIC;
    pub_msg.topic_len = MQTT_TOPIC_LENGTH;
    pub_msg.payload = MQTT_PUB_MESSAGE;
    pub_msg.payload_len = MQTT_PUB_MESSAGE_LENGTH;

    TestRes = cy_mqtt_publish( MQTThandle, &pub_msg );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }
}

Code Snippet 7: MQTT disconnect

This code snippet demonstrates the steps to end already established MQTT client session with MQTT broker, and the usage of the cy_mqtt_disconnect() API function.


#define MQTT_BROKER                         " " /* Add the broker end point info here. */
#define MQTT_PORT_SECURED                   ( 8883 )

#define MQTT_CLIENT_IDENTIFIER              "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH       ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS    ( 60U )

#define MQTT_QOS2                           ( 2U )
#define MQTT_TOPIC                          "Test_Topic"
#define MQTT_TOPIC_LENGTH                   ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE                   "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH            ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )

#define NETWORK_BUFFER_SIZE                 ( 1024U )

static const char MQTT_clientcertificate[]  = { /* Client certificate here.*/ };
static const char MQTT_clientprivate_key[]  = { /* Client key here.*/ };
static const char MQTT_rootca_certificate[] = { /* Root CA here.*/ };

cy_mqtt_t         MQTT_handle;

/* Callback function registered with MQTT library while creating handle for MQTT connection.
   This callback function will be called by MQTT library upon receiving MQTT messages on subscribed topics or when
   network disconnection happens. */
void mqtt_events_cb( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_received_msg_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;

    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;

        case CY_MQTT_EVENT_TYPE_SUBSCRIPTION_MESSAGE_RECEIVE :
            /* Received MQTT messages on subscribed topic. */
            received_msg = &(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
            break;

        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}

void snippet_cy_mqtt_disconnect( void )
{
    /* Status variables for various operations. */
    /* Status variables for various operations. */
    cy_rslt_t                     TestRes = CY_RSLT_SUCCESS;
    cy_mqtt_publish_info_t        will_msg;
    cy_mqtt_connect_info_t        connect_info;
    cy_mqtt_broker_info_t         broker_info;
    cy_awsport_ssl_credentials_t  credentials;
    cy_awsport_ssl_credentials_t  *security = NULL;
    uint8_t                       *buffer = NULL;
    (void)TestRes;

    /* Initialize the MQTT network socket. */
    TestRes = cy_mqtt_init();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /*Allocate the network buffer. */
    buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
    if( buffer == NULL )
    {
        /* Failure path. */
        return;
    }

    memset( &will_msg, 0x00, sizeof( cy_mqtt_publish_info_t ) );
    memset( &broker_info, 0x00, sizeof( cy_mqtt_broker_info_t ) );
    memset( &connect_info, 0x00, sizeof( cy_mqtt_connect_info_t ) );
    memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );

    /* Set the will information. */
    will_msg.qos = MQTT_QOS2;
    will_msg.topic = MQTT_TOPIC;
    will_msg.topic_len = MQTT_TOPIC_LENGTH;
    will_msg.payload = MQTT_WILL_MESSAGE;
    will_msg.payload_len = MQTT_WILL_MESSAGE_LENGTH;

    /* Set the credential information. */
    credentials.client_cert = (const char *) &MQTT_clientcertificate;
    credentials.client_cert_size = sizeof( MQTT_clientcertificate );
    credentials.private_key = (const char *) &MQTT_clientprivate_key;
    credentials.private_key_size = sizeof( MQTT_clientprivate_key );
    credentials.root_ca = (const char *) &MQTT_rootca_certificate;
    credentials.root_ca_size = sizeof( MQTT_rootca_certificate );

    security = &credentials;

    connect_info.client_id = MQTT_CLIENT_IDENTIFIER;
    connect_info.client_id_len = MQTT_CLIENT_IDENTIFIER_LENGTH;
    connect_info.keep_alive_sec = MQTT_KEEP_ALIVE_INTERVAL_SECONDS;
    connect_info.will_info = &will_msg;

    /* MQTT broker information. */
    broker_info.hostname = MQTT_BROKER;
    broker_info.hostname_len = strlen(MQTT_BROKER);
    broker_info.port = MQTT_PORT_SECURED;

    /* Create MQTT Handle. */
    TestRes = cy_mqtt_create( buffer, NETWORK_BUFFER_SIZE,
                              security, &broker_info,
                              (cy_mqtt_callback_t)mqtt_events_cb, NULL,
                              &MQTT_handle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    TestRes = cy_mqtt_connect( MQTT_handle, &connect_info );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /* Do MQTT publish and subscribe here. */

    /* Calling MQTT disconnect to end the MQTT client session with the broker. */
    TestRes = cy_mqtt_disconnect( MQTT_handle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }
}

Code Snippet 8: MQTT delete and deinit

This code snippet demonstrates the de-allocation of the configuration structures and resources which are allocated during an MQTT client handle creation, and the usage of the cy_mqtt_delete() and cy_mqtt_deinit() API functions.


#define MQTT_BROKER                         " " /* Add the broker end point info here. */
#define MQTT_PORT_SECURED                   ( 8883 )

#define MQTT_CLIENT_IDENTIFIER              "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH       ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS    ( 60U )

#define MQTT_QOS2                           ( 2U )
#define MQTT_TOPIC                          "Test_Topic"
#define MQTT_TOPIC_LENGTH                   ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE                   "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH            ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )

#define NETWORK_BUFFER_SIZE                 ( 1024U )

static const char MQTT_client_certificate[] = { /* Client certificate here.*/ };
static const char MQTT_client_key[]  = { /* Client key here.*/ };
static const char MQTT_root_ca_certificate[] = { /* Root CA here.*/ };

cy_mqtt_t         MQTT_obj_handle;

/* Callback function registered with MQTT library while creating handle for MQTT connection.
   This callback function will be called by MQTT library upon receiving MQTT messages on subscribed topics or when
   network disconnection happens. */
void mqtt_callback( cy_mqtt_t mqtt_handle, cy_mqtt_event_t event, void *user_data )
{
    cy_mqtt_received_msg_info_t *received_msg;
    printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
    (void)user_data;

    switch( event.type )
    {
        case CY_MQTT_EVENT_TYPE_DISCONNECT :
            if( event.data.reason == CY_MQTT_DISCONN_TYPE_BROKER_DOWN )
            {
                printf( "\nCY_MQTT_DISCONN_TYPE_BROKER_DOWN .....\n" );
            }
            else
            {
                printf( "\nCY_MQTT_DISCONN_REASON_NETWORK_DISCONNECTION .....\n" );
            }
            break;

        case CY_MQTT_EVENT_TYPE_SUBSCRIPTION_MESSAGE_RECEIVE :
            /* Received MQTT messages on subscribed topic. */
            received_msg = &(event.data.pub_msg.received_message);
            printf( "Incoming Publish Topic Name: %.*s\n", received_msg->topic_len, received_msg->topic );
            printf( "Incoming Publish message Packet Id is %u.\n", event.data.pub_msg.packet_id );
            printf( "Incoming Publish Message : %.*s.\n\n", ( int )received_msg->payload_len, ( const char * )received_msg->payload );
            break;

        default :
            printf( "\nUNKNOWN EVENT .....\n" );
            break;
    }
}

void snippet_cy_mqtt_delete( void )
{
    /* Status variables for various operations. */
    /* Status variables for various operations */
    cy_rslt_t                     TestRes = CY_RSLT_SUCCESS;
    cy_mqtt_broker_info_t         broker_info;
    cy_awsport_ssl_credentials_t  credentials;
    cy_awsport_ssl_credentials_t  *security = NULL;
    uint8_t                       *buffer = NULL;
    (void)TestRes;

    /* Initialize the MQTT network socket. */
    TestRes = cy_mqtt_init();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /*Allocate the network buffer. */
    buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
    if( buffer == NULL )
    {
        /* Failure path. */
        return;
    }

    memset( &broker_info, 0x00, sizeof( cy_mqtt_broker_info_t ) );
    memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );

    /* Set the credential information. */
    credentials.client_cert = (const char *) &MQTT_client_certificate;
    credentials.client_cert_size = sizeof( MQTT_client_certificate );
    credentials.private_key = (const char *) &MQTT_client_key;
    credentials.private_key_size = sizeof( MQTT_client_key );
    credentials.root_ca = (const char *) &MQTT_root_ca_certificate;
    credentials.root_ca_size = sizeof( MQTT_root_ca_certificate );

    security = &credentials;

    /* MQTT broker information. */
    broker_info.hostname = MQTT_BROKER;
    broker_info.hostname_len = strlen(MQTT_BROKER);
    broker_info.port = MQTT_PORT_SECURED;

    /* Create MQTT Handle. */
    TestRes = cy_mqtt_create( buffer, NETWORK_BUFFER_SIZE,
                              security, &broker_info,
                              (cy_mqtt_callback_t)mqtt_callback, NULL,
                              &MQTT_obj_handle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }

    /* Calling MQTT Delete to clean up the resources allocated during create. */
    TestRes = cy_mqtt_delete( mqtthandle );
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }
    mqtthandle = NULL;

    /* De-initialize the MQTT network socket. */
    TestRes = cy_mqtt_deinit();
    if( TestRes != CY_RSLT_SUCCESS )
    {
        /* Failure path. */
        return;
    }
}