AzureMXChip/MXCHIPAZ3166/core/lib/netxduo/addons/mqtt/nxd_mqtt_client.c
UINT _nxd_mqtt_client_create(NXD_MQTT_CLIENT *client_ptr, CHAR *client_name,
                             CHAR *client_id, UINT client_id_length,
                             NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr,
                             VOID *stack_ptr, ULONG stack_size, UINT mqtt_thread_priority,
                             VOID *memory_ptr, ULONG memory_size)
{

UINT    status;


    NX_PARAMETER_NOT_USED(memory_ptr);
    NX_PARAMETER_NOT_USED(memory_size);

    /* Create MQTT client.  */
    status = _nxd_mqtt_client_create_internal(client_ptr, client_name, client_id, client_id_length, ip_ptr,
                                              pool_ptr, stack_ptr, stack_size, mqtt_thread_priority);

    /* Check status.  */
    if (status)
    {
        return(status);
    }

#ifdef NXD_MQTT_CLOUD_ENABLE

    /* Create cloud helper.  */
    status = nx_cloud_create(&(client_ptr -> nxd_mqtt_client_cloud), "Cloud Helper", stack_ptr, stack_size, mqtt_thread_priority);

    /* Determine if an error occurred.  */
    if (status != NX_SUCCESS)
    {

        /* Delete internal resource created in _nxd_mqtt_client_create_internal().  */

        /* Delete socket.  */
        nx_tcp_socket_delete(&(client_ptr -> nxd_mqtt_client_socket));

        return(NXD_MQTT_INTERNAL_ERROR);
    }

    /* Save the cloud pointer.  */
    client_ptr -> nxd_mqtt_client_cloud_ptr = &(client_ptr -> nxd_mqtt_client_cloud);

    /* Save the mutex pointer.  */
    client_ptr -> nxd_mqtt_client_mutex_ptr = &(client_ptr -> nxd_mqtt_client_cloud.nx_cloud_mutex);

    /* Register MQTT on cloud helper.  */
    status = nx_cloud_module_register(client_ptr -> nxd_mqtt_client_cloud_ptr, &(client_ptr -> nxd_mqtt_client_cloud_module), client_name, NX_CLOUD_MODULE_MQTT_EVENT,
                                      _nxd_mqtt_client_event_process, client_ptr);

    /* Determine if an error occurred.  */
    if (status != NX_SUCCESS)
    {

        /* Delete own created cloud.  */
        nx_cloud_delete(&(client_ptr -> nxd_mqtt_client_cloud));

        /* Delete internal resource created in _nxd_mqtt_client_create_internal().  */

        /* Delete socket.  */
        nx_tcp_socket_delete(&(client_ptr -> nxd_mqtt_client_socket));

        return(NXD_MQTT_INTERNAL_ERROR);
    }

#endif /* NXD_MQTT_CLOUD_ENABLE */

    /* Update state.  */
    client_ptr -> nxd_mqtt_client_state = NXD_MQTT_CLIENT_STATE_IDLE;

    /* Return.  */
    return(NXD_MQTT_SUCCESS);
}
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxd_mqtt_client_create                             PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates an instance for MQTT Client.  It initializes  */
/*    MQTT Client control block, creates a thread, mutex and event queue  */
/*    for MQTT Client, and creates the TCP socket for MQTT messaging.     */
/*                                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    client_ptr                            Pointer to MQTT Client        */
/*    client_id                             Client ID for this instance   */
/*    client_id_length                      Length of Client ID, in bytes */
/*    ip_ptr                                Pointer to IP instance        */
/*    pool_ptr                              Pointer to packet pool        */
/*    stack_ptr                             Client thread's stack pointer */
/*    stack_size                            Client thread's stack size    */
/*    mqtt_thread_priority                  Priority for MQTT thread      */
/*    memory_ptr                            Deprecated and not used       */
/*    memory_size                           Deprecated and not used       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nxd_mqtt_client_create_internal      Create mqtt client            */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
/*  09-30-2020     Yuxin Zhou               Modified comment(s), and      */
/*                                            corrected mqtt client state,*/
/*                                            resulting in version 6.1    */
/*                                                                        */
/**************************************************************************/
FUNCTION                                               RELEASE

_nxd_mqtt_client_create                             PORTABLE C
6.1
AUTHOR

Yuxin Zhou, Microsoft Corporation

DESCRIPTION

This function creates an instance for MQTT Client.  It initializes
MQTT Client control block, creates a thread, mutex and event queue
for MQTT Client, and creates the TCP socket for MQTT messaging.

INPUT

client_ptr                            Pointer to MQTT Client
client_id                             Client ID for this instance
client_id_length                      Length of Client ID, in bytes
ip_ptr                                Pointer to IP instance
pool_ptr                              Pointer to packet pool
stack_ptr                             Client thread's stack pointer
stack_size                            Client thread's stack size
mqtt_thread_priority                  Priority for MQTT thread
memory_ptr                            Deprecated and not used
memory_size                           Deprecated and not used

OUTPUT

status                                Completion status

CALLS

_nxd_mqtt_client_create_internal      Create mqtt client

CALLED BY

Application Code

RELEASE HISTORY

DATE              NAME                      DESCRIPTION

05-19-2020     Yuxin Zhou               Initial Version 6.0
09-30-2020     Yuxin Zhou               Modified comment(s), and
corrected mqtt client state,
resulting in version 6.1
FUNCTION RELEASE _nxd_mqtt_client_create PORTABLE C 6 . 1 AUTHOR Yuxin Zhou Microsoft Corporation DESCRIPTION This function creates an instance for MQTT Client . It initializes MQTT Client control block creates a thread mutex and event queue for MQTT Client and creates the TCP socket for MQTT messaging . INPUT client_ptr Pointer to MQTT Client client_id Client ID for this instance client_id_length Length of Client ID in bytes ip_ptr Pointer to IP instance pool_ptr Pointer to packet pool stack_ptr Client thread ' s stack pointer stack_size Client thread ' s stack size mqtt_thread_priority Priority for MQTT thread memory_ptr Deprecated and not used memory_size Deprecated and not used OUTPUT status Completion status CALLS _nxd_mqtt_client_create_internal Create mqtt client CALLED BY Application Code RELEASE HISTORY DATE NAME DESCRIPTION 05 - 19 - 2020 Yuxin Zhou Initial Version 6 . 0 09 - 30 - 2020 Yuxin Zhou Modified comment ( s ) and corrected mqtt client state resulting in version 6 . 1
UINT _nxd_mqtt_client_create(NXD_MQTT_CLIENT *client_ptr, CHAR *client_name,
                             CHAR *client_id, UINT client_id_length,
                             NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr,
                             VOID *stack_ptr, ULONG stack_size, UINT mqtt_thread_priority,
                             VOID *memory_ptr, ULONG memory_size)
{
UINT    status;
    NX_PARAMETER_NOT_USED(memory_ptr);
    NX_PARAMETER_NOT_USED(memory_size);
    status = _nxd_mqtt_client_create_internal(client_ptr, client_name, client_id, client_id_length, ip_ptr,
                                              pool_ptr, stack_ptr, stack_size, mqtt_thread_priority);
    if (status)
    {
        return(status);
    }
#ifdef NXD_MQTT_CLOUD_ENABLE
    status = nx_cloud_create(&(client_ptr -> nxd_mqtt_client_cloud), "Cloud Helper", stack_ptr, stack_size, mqtt_thread_priority);
    if (status != NX_SUCCESS)
    {
        nx_tcp_socket_delete(&(client_ptr -> nxd_mqtt_client_socket));
        return(NXD_MQTT_INTERNAL_ERROR);
    }
    client_ptr -> nxd_mqtt_client_cloud_ptr = &(client_ptr -> nxd_mqtt_client_cloud);
    client_ptr -> nxd_mqtt_client_mutex_ptr = &(client_ptr -> nxd_mqtt_client_cloud.nx_cloud_mutex);
    status = nx_cloud_module_register(client_ptr -> nxd_mqtt_client_cloud_ptr, &(client_ptr -> nxd_mqtt_client_cloud_module), client_name, NX_CLOUD_MODULE_MQTT_EVENT,
                                      _nxd_mqtt_client_event_process, client_ptr);
    if (status != NX_SUCCESS)
    {
        nx_cloud_delete(&(client_ptr -> nxd_mqtt_client_cloud));
        nx_tcp_socket_delete(&(client_ptr -> nxd_mqtt_client_socket));
        return(NXD_MQTT_INTERNAL_ERROR);
    }
#endif 
    client_ptr -> nxd_mqtt_client_state = NXD_MQTT_CLIENT_STATE_IDLE;
    return(NXD_MQTT_SUCCESS);
}
UINT _nxd_mqtt_client_create ( NXD_MQTT_CLIENT * client_ptr , CHAR * client_name , CHAR * client_id , UINT client_id_length , NX_IP * ip_ptr , NX_PACKET_POOL * pool_ptr , VOID * stack_ptr , ULONG stack_size , UINT mqtt_thread_priority , VOID * memory_ptr , ULONG memory_size ) { UINT status ; NX_PARAMETER_NOT_USED ( memory_ptr ) ; NX_PARAMETER_NOT_USED ( memory_size ) ; status = _nxd_mqtt_client_create_internal ( client_ptr , client_name , client_id , client_id_length , ip_ptr , pool_ptr , stack_ptr , stack_size , mqtt_thread_priority ) ; if ( status ) { return ( status ) ; } #ifdef NXD_MQTT_CLOUD_ENABLE status = nx_cloud_create ( & ( client_ptr -> nxd_mqtt_client_cloud ) , " " , stack_ptr , stack_size , mqtt_thread_priority ) ; if ( status != NX_SUCCESS ) { nx_tcp_socket_delete ( & ( client_ptr -> nxd_mqtt_client_socket ) ) ; return ( NXD_MQTT_INTERNAL_ERROR ) ; } client_ptr -> nxd_mqtt_client_cloud_ptr = & ( client_ptr -> nxd_mqtt_client_cloud ) ; client_ptr -> nxd_mqtt_client_mutex_ptr = & ( client_ptr -> nxd_mqtt_client_cloud . nx_cloud_mutex ) ; status = nx_cloud_module_register ( client_ptr -> nxd_mqtt_client_cloud_ptr , & ( client_ptr -> nxd_mqtt_client_cloud_module ) , client_name , NX_CLOUD_MODULE_MQTT_EVENT , _nxd_mqtt_client_event_process , client_ptr ) ; if ( status != NX_SUCCESS ) { nx_cloud_delete ( & ( client_ptr -> nxd_mqtt_client_cloud ) ) ; nx_tcp_socket_delete ( & ( client_ptr -> nxd_mqtt_client_socket ) ) ; return ( NXD_MQTT_INTERNAL_ERROR ) ; } #endif client_ptr -> nxd_mqtt_client_state = NXD_MQTT_CLIENT_STATE_IDLE ; return ( NXD_MQTT_SUCCESS ) ; }
FUNCTION                                               RELEASE
FUNCTION RELEASE
/* Create MQTT client.  */ /* Check status.  */ /* Create cloud helper.  */ /* Determine if an error occurred.  */ /* Delete internal resource created in _nxd_mqtt_client_create_internal().  */ /* Delete socket.  */ /* Save the cloud pointer.  */ /* Save the mutex pointer.  */ /* Register MQTT on cloud helper.  */ /* Determine if an error occurred.  */ /* Delete own created cloud.  */ /* Delete internal resource created in _nxd_mqtt_client_create_internal().  */ /* Delete socket.  */ /* NXD_MQTT_CLOUD_ENABLE */ /* Update state.  */ /* Return.  */