AzureMXChip/MXCHIPAZ3166/core/lib/netxduo/addons/mqtt/nxd_mqtt_client.c
static UINT _nxd_mqtt_client_create_internal(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)
{
UINT                status;

#ifdef NXD_MQTT_CLOUD_ENABLE
    NX_PARAMETER_NOT_USED(stack_ptr);
    NX_PARAMETER_NOT_USED(stack_size);
    NX_PARAMETER_NOT_USED(mqtt_thread_priority);
#endif /* NXD_MQTT_CLOUD_ENABLE */

    /* Clear the MQTT Client control block. */
    NXD_MQTT_SECURE_MEMSET((void *)client_ptr, 0, sizeof(NXD_MQTT_CLIENT));

#ifndef NXD_MQTT_CLOUD_ENABLE

    /* Create MQTT mutex.  */
    status = tx_mutex_create(&client_ptr -> nxd_mqtt_protection, client_name, TX_NO_INHERIT);

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

        return(NXD_MQTT_INTERNAL_ERROR);
    }
    client_ptr -> nxd_mqtt_client_mutex_ptr = &(client_ptr -> nxd_mqtt_protection);

    /* Now create MQTT client thread */
    status = tx_thread_create(&(client_ptr -> nxd_mqtt_thread), client_name, _nxd_mqtt_thread_entry,
                              (ULONG)client_ptr, stack_ptr, stack_size, mqtt_thread_priority, mqtt_thread_priority,
                              NXD_MQTT_CLIENT_THREAD_TIME_SLICE, TX_DONT_START);

    /* Determine if an error occurred. */
    if (status != TX_SUCCESS)
    {
        /* Delete the mutex. */
        tx_mutex_delete(&client_ptr -> nxd_mqtt_protection);

        /* Return error code. */
        return(NXD_MQTT_INTERNAL_ERROR);
    }

    status = tx_event_flags_create(&(client_ptr -> nxd_mqtt_events), client_name);

    if (status != TX_SUCCESS)
    {
        /* Delete the mutex. */
        tx_mutex_delete(&client_ptr -> nxd_mqtt_protection);

        /* Delete the thread. */
        tx_thread_delete(&(client_ptr -> nxd_mqtt_thread));
        
        /* Return error code. */
        return(NXD_MQTT_INTERNAL_ERROR);
    }
#endif /* NXD_MQTT_CLOUD_ENABLE */

    /* Record the client ID information. */
    client_ptr -> nxd_mqtt_client_id = client_id;
    client_ptr -> nxd_mqtt_client_id_length = client_id_length;
    client_ptr -> nxd_mqtt_client_ip_ptr = ip_ptr;
    client_ptr -> nxd_mqtt_client_packet_pool_ptr = pool_ptr;
    client_ptr -> nxd_mqtt_client_name = client_name;

    /* Create the socket. */
    status = nx_tcp_socket_create(client_ptr -> nxd_mqtt_client_ip_ptr, &(client_ptr -> nxd_mqtt_client_socket), client_ptr -> nxd_mqtt_client_name,
                                  NX_IP_NORMAL, NX_DONT_FRAGMENT, 0x80, NXD_MQTT_CLIENT_SOCKET_WINDOW_SIZE,
                                  NX_NULL, _mqtt_client_disconnect_callback);

    /* Determine if an error occurred. */
    if (status != NX_SUCCESS)
    {
#ifndef NXD_MQTT_CLOUD_ENABLE
        /* Delete the mutex. */
        tx_mutex_delete(&client_ptr -> nxd_mqtt_protection);

        /* Delete the event flags. */
        tx_event_flags_delete(&(client_ptr -> nxd_mqtt_events));

        /* Delete the thread. */
        tx_thread_delete(&(client_ptr -> nxd_mqtt_thread));
#endif /* NXD_MQTT_CLOUD_ENABLE */

        return(NXD_MQTT_INTERNAL_ERROR);
    }

    /* Record the client_ptr in the socket structure. */
    client_ptr -> nxd_mqtt_client_socket.nx_tcp_socket_reserved_ptr = (VOID *)client_ptr;

#ifndef NXD_MQTT_CLOUD_ENABLE
    /* Start MQTT thread. */
    tx_thread_resume(&(client_ptr -> nxd_mqtt_thread));
#endif /* NXD_MQTT_CLOUD_ENABLE */
    return(NXD_MQTT_SUCCESS);
}
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxd_mqtt_client_create_internal                    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      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_thread_create                      Create a thread               */
/*    tx_mutex_create                       Create mutex                  */
/*    tx_mutex_get                                                        */
/*    tx_mutex_put                                                        */
/*    tx_event_flag_create                  Create event flag             */
/*                                                                        */
/*  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_internal                    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

OUTPUT

status                                Completion status

CALLS



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_internal 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 OUTPUT status Completion status CALLS 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
static UINT _nxd_mqtt_client_create_internal(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)
{
UINT                status;
#ifdef NXD_MQTT_CLOUD_ENABLE
    NX_PARAMETER_NOT_USED(stack_ptr);
    NX_PARAMETER_NOT_USED(stack_size);
    NX_PARAMETER_NOT_USED(mqtt_thread_priority);
#endif 
    NXD_MQTT_SECURE_MEMSET((void *)client_ptr, 0, sizeof(NXD_MQTT_CLIENT));
#ifndef NXD_MQTT_CLOUD_ENABLE
    status = tx_mutex_create(&client_ptr -> nxd_mqtt_protection, client_name, TX_NO_INHERIT);
    if (status != TX_SUCCESS)
    {
        return(NXD_MQTT_INTERNAL_ERROR);
    }
    client_ptr -> nxd_mqtt_client_mutex_ptr = &(client_ptr -> nxd_mqtt_protection);
    status = tx_thread_create(&(client_ptr -> nxd_mqtt_thread), client_name, _nxd_mqtt_thread_entry,
                              (ULONG)client_ptr, stack_ptr, stack_size, mqtt_thread_priority, mqtt_thread_priority,
                              NXD_MQTT_CLIENT_THREAD_TIME_SLICE, TX_DONT_START);
    if (status != TX_SUCCESS)
    {
        tx_mutex_delete(&client_ptr -> nxd_mqtt_protection);
        return(NXD_MQTT_INTERNAL_ERROR);
    }
    status = tx_event_flags_create(&(client_ptr -> nxd_mqtt_events), client_name);
    if (status != TX_SUCCESS)
    {
        tx_mutex_delete(&client_ptr -> nxd_mqtt_protection);
        tx_thread_delete(&(client_ptr -> nxd_mqtt_thread));
        return(NXD_MQTT_INTERNAL_ERROR);
    }
#endif 
    client_ptr -> nxd_mqtt_client_id = client_id;
    client_ptr -> nxd_mqtt_client_id_length = client_id_length;
    client_ptr -> nxd_mqtt_client_ip_ptr = ip_ptr;
    client_ptr -> nxd_mqtt_client_packet_pool_ptr = pool_ptr;
    client_ptr -> nxd_mqtt_client_name = client_name;
    status = nx_tcp_socket_create(client_ptr -> nxd_mqtt_client_ip_ptr, &(client_ptr -> nxd_mqtt_client_socket), client_ptr -> nxd_mqtt_client_name,
                                  NX_IP_NORMAL, NX_DONT_FRAGMENT, 0x80, NXD_MQTT_CLIENT_SOCKET_WINDOW_SIZE,
                                  NX_NULL, _mqtt_client_disconnect_callback);
    if (status != NX_SUCCESS)
    {
#ifndef NXD_MQTT_CLOUD_ENABLE
        tx_mutex_delete(&client_ptr -> nxd_mqtt_protection);
        tx_event_flags_delete(&(client_ptr -> nxd_mqtt_events));
        tx_thread_delete(&(client_ptr -> nxd_mqtt_thread));
#endif 
        return(NXD_MQTT_INTERNAL_ERROR);
    }
    client_ptr -> nxd_mqtt_client_socket.nx_tcp_socket_reserved_ptr = (VOID *)client_ptr;
#ifndef NXD_MQTT_CLOUD_ENABLE
    tx_thread_resume(&(client_ptr -> nxd_mqtt_thread));
#endif 
    return(NXD_MQTT_SUCCESS);
}
static UINT _nxd_mqtt_client_create_internal ( 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 ) { UINT status ; #ifdef NXD_MQTT_CLOUD_ENABLE NX_PARAMETER_NOT_USED ( stack_ptr ) ; NX_PARAMETER_NOT_USED ( stack_size ) ; NX_PARAMETER_NOT_USED ( mqtt_thread_priority ) ; #endif NXD_MQTT_SECURE_MEMSET ( ( void * ) client_ptr , 0 , sizeof ( NXD_MQTT_CLIENT ) ) ; #ifndef NXD_MQTT_CLOUD_ENABLE status = tx_mutex_create ( & client_ptr -> nxd_mqtt_protection , client_name , TX_NO_INHERIT ) ; if ( status != TX_SUCCESS ) { return ( NXD_MQTT_INTERNAL_ERROR ) ; } client_ptr -> nxd_mqtt_client_mutex_ptr = & ( client_ptr -> nxd_mqtt_protection ) ; status = tx_thread_create ( & ( client_ptr -> nxd_mqtt_thread ) , client_name , _nxd_mqtt_thread_entry , ( ULONG ) client_ptr , stack_ptr , stack_size , mqtt_thread_priority , mqtt_thread_priority , NXD_MQTT_CLIENT_THREAD_TIME_SLICE , TX_DONT_START ) ; if ( status != TX_SUCCESS ) { tx_mutex_delete ( & client_ptr -> nxd_mqtt_protection ) ; return ( NXD_MQTT_INTERNAL_ERROR ) ; } status = tx_event_flags_create ( & ( client_ptr -> nxd_mqtt_events ) , client_name ) ; if ( status != TX_SUCCESS ) { tx_mutex_delete ( & client_ptr -> nxd_mqtt_protection ) ; tx_thread_delete ( & ( client_ptr -> nxd_mqtt_thread ) ) ; return ( NXD_MQTT_INTERNAL_ERROR ) ; } #endif client_ptr -> nxd_mqtt_client_id = client_id ; client_ptr -> nxd_mqtt_client_id_length = client_id_length ; client_ptr -> nxd_mqtt_client_ip_ptr = ip_ptr ; client_ptr -> nxd_mqtt_client_packet_pool_ptr = pool_ptr ; client_ptr -> nxd_mqtt_client_name = client_name ; status = nx_tcp_socket_create ( client_ptr -> nxd_mqtt_client_ip_ptr , & ( client_ptr -> nxd_mqtt_client_socket ) , client_ptr -> nxd_mqtt_client_name , NX_IP_NORMAL , NX_DONT_FRAGMENT , 0x80 , NXD_MQTT_CLIENT_SOCKET_WINDOW_SIZE , NX_NULL , _mqtt_client_disconnect_callback ) ; if ( status != NX_SUCCESS ) { #ifndef NXD_MQTT_CLOUD_ENABLE tx_mutex_delete ( & client_ptr -> nxd_mqtt_protection ) ; tx_event_flags_delete ( & ( client_ptr -> nxd_mqtt_events ) ) ; tx_thread_delete ( & ( client_ptr -> nxd_mqtt_thread ) ) ; #endif return ( NXD_MQTT_INTERNAL_ERROR ) ; } client_ptr -> nxd_mqtt_client_socket . nx_tcp_socket_reserved_ptr = ( VOID * ) client_ptr ; #ifndef NXD_MQTT_CLOUD_ENABLE tx_thread_resume ( & ( client_ptr -> nxd_mqtt_thread ) ) ; #endif return ( NXD_MQTT_SUCCESS ) ; }
FUNCTION                                               RELEASE
FUNCTION RELEASE
/* NXD_MQTT_CLOUD_ENABLE */ /* Clear the MQTT Client control block. */ /* Create MQTT mutex.  */ /* Determine if an error occurred. */ /* Now create MQTT client thread */ /* Determine if an error occurred. */ /* Delete the mutex. */ /* Return error code. */ /* Delete the mutex. */ /* Delete the thread. */ /* Return error code. */ /* NXD_MQTT_CLOUD_ENABLE */ /* Record the client ID information. */ /* Create the socket. */ /* Determine if an error occurred. */ /* Delete the mutex. */ /* Delete the event flags. */ /* Delete the thread. */ /* NXD_MQTT_CLOUD_ENABLE */ /* Record the client_ptr in the socket structure. */ /* Start MQTT thread. */ /* NXD_MQTT_CLOUD_ENABLE */