1 /*************************************************************************** 2 * Copyright (c) 2024 Microsoft Corporation 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the MIT License which is available at 6 * https://opensource.org/licenses/MIT. 7 * 8 * SPDX-License-Identifier: MIT 9 **************************************************************************/ 10 11 12 /**************************************************************************/ 13 /**************************************************************************/ 14 /** */ 15 /** NetX Component */ 16 /** */ 17 /** MQTT (MQTT) */ 18 /** */ 19 /**************************************************************************/ 20 /**************************************************************************/ 21 22 23 /**************************************************************************/ 24 /* */ 25 /* APPLICATION INTERFACE DEFINITION RELEASE */ 26 /* */ 27 /* nxd_mqtt_client.h PORTABLE C */ 28 /* 6.3.0 */ 29 /* AUTHOR */ 30 /* */ 31 /* Yuxin Zhou, Microsoft Corporation */ 32 /* */ 33 /* DESCRIPTION */ 34 /* */ 35 /* This file defines the NetX MQTT Client component, including all */ 36 /* data types and external references. It is assumed that nx_api.h */ 37 /* and nx_port.h have already been included. */ 38 /* */ 39 /* RELEASE HISTORY */ 40 /* */ 41 /* DATE NAME DESCRIPTION */ 42 /* */ 43 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */ 44 /* 09-30-2020 Yuxin Zhou Modified comment(s), and */ 45 /* added ack receive notify, */ 46 /* resulting in version 6.1 */ 47 /* 11-09-2020 Yuxin Zhou Modified comment(s), and */ 48 /* added packet id parameter, */ 49 /* resulting in version 6.1.2 */ 50 /* 08-02-2021 Yuxin Zhou Modified comment(s), and */ 51 /* supported maximum transmit */ 52 /* queue depth, */ 53 /* resulting in version 6.1.8 */ 54 /* 10-15-2021 Yuxin Zhou Modified comment(s), included */ 55 /* necessary header file, */ 56 /* resulting in version 6.1.9 */ 57 /* 10-31-2022 Bo Chen Modified comment(s), supported*/ 58 /* mqtt over websocket, */ 59 /* resulting in version 6.2.0 */ 60 /* 10-31-2023 Haiqing Zhao Modified comment(s), added */ 61 /* the function prototype for */ 62 /* packet allocation, */ 63 /* resulting in version 6.3.0 */ 64 /* */ 65 /**************************************************************************/ 66 67 #ifndef _NXD_MQTT_CLIENT_H_ 68 #define _NXD_MQTT_CLIENT_H_ 69 70 /* Determine if a C++ compiler is being used. If so, ensure that standard 71 C is used to process the API information. */ 72 73 #ifdef __cplusplus 74 75 /* Yes, C++ compiler is present. Use standard C. */ 76 extern "C" { 77 78 #endif 79 80 #include "nx_api.h" 81 82 #ifdef NX_SECURE_ENABLE 83 #include "nx_secure_tls_api.h" 84 #endif /* NX_SECURE_ENABLE */ 85 86 #ifdef NXD_MQTT_CLOUD_ENABLE 87 #include "nx_cloud.h" 88 #endif /* NXD_MQTT_CLOUD_ENABLE */ 89 90 #ifdef NXD_MQTT_REQUIRE_TLS 91 #ifndef NX_SECURE_ENABLE 92 #error "The feature NXD_MQTT_REQUIRE_TLS requires NX_SECURE_ENABLE." 93 #endif /* NX_SECURE_ENABLE */ 94 #endif /* NXD_MQTT_REQUIRE_TLS */ 95 96 #ifdef NXD_MQTT_OVER_WEBSOCKET 97 #include "nx_websocket_client.h" 98 #endif /* NXD_MQTT_OVER_WEBSOCKET */ 99 100 /* Defined, MQTT transmit queue depth is enabled. It must be positive integer. */ 101 /* 102 #define NXD_MQTT_MAXIMUM_TRANSMIT_QUEUE_DEPTH 20 103 */ 104 105 /* Define MQTT protocol for websocket. */ 106 #define NXD_MQTT_OVER_WEBSOCKET_PROTOCOL "mqtt" 107 108 /* Define memcpy, memset and memcmp functions used internal. */ 109 #ifndef NXD_MQTT_SECURE_MEMCPY 110 #define NXD_MQTT_SECURE_MEMCPY memcpy 111 #endif /* NXD_MQTT_SECURE_MEMCPY */ 112 113 #ifndef NXD_MQTT_SECURE_MEMCMP 114 #define NXD_MQTT_SECURE_MEMCMP memcmp 115 #endif /* NXD_MQTT_SECURE_MEMCMP */ 116 117 #ifndef NXD_MQTT_SECURE_MEMSET 118 #define NXD_MQTT_SECURE_MEMSET memset 119 #endif /* NXD_SECURE_MEMSET */ 120 121 #ifndef NXD_MQTT_SECURE_MEMMOVE 122 #define NXD_MQTT_SECURE_MEMMOVE memmove 123 #endif /* NXD_MQTT_SECURE_MEMMOVE */ 124 125 /* Define the default MQTT Non-TLS (Non-secure) port number */ 126 #define NXD_MQTT_PORT 1883 127 128 129 /* Define the default TCP socket window size. */ 130 #ifndef NXD_MQTT_CLIENT_SOCKET_WINDOW_SIZE 131 #define NXD_MQTT_CLIENT_SOCKET_WINDOW_SIZE 8192 132 #endif /* NXD_MQTT_CLIENT_SOCKET_WINDOW_SIZE */ 133 134 /* Define the default MQTT Thread time slice. */ 135 #ifndef NXD_MQTT_CLIENT_THREAD_TIME_SLICE 136 #define NXD_MQTT_CLIENT_THREAD_TIME_SLICE 2 137 #endif 138 139 /* Set the default timer rate for the keepalive timer, in ThreadX timer ticks. 140 THe default is one second. */ 141 #ifndef NXD_MQTT_KEEPALIVE_TIMER_RATE 142 #define NXD_MQTT_KEEPALIVE_TIMER_RATE (NX_IP_PERIODIC_RATE) 143 #endif 144 145 /* Set the default timeout for PING response. */ 146 /* After sending out the MQTT Ping Request, if the client does not receive Ping Response within this 147 time, the client shall disconnect from the server. The default is one second. */ 148 #ifndef NXD_MQTT_PING_TIMEOUT_DELAY 149 #define NXD_MQTT_PING_TIMEOUT_DELAY (NX_IP_PERIODIC_RATE) 150 #endif 151 152 153 /* Deprecated. This symbol is defined for compatibility. */ 154 #ifndef NXD_MQTT_MAX_TOPIC_NAME_LENGTH 155 #define NXD_MQTT_MAX_TOPIC_NAME_LENGTH 12 156 #endif 157 158 /* Deprecated. This symbol is defined for compatibility. */ 159 #ifndef NXD_MQTT_MAX_MESSAGE_LENGTH 160 #define NXD_MQTT_MAX_MESSAGE_LENGTH 32 161 #endif 162 163 #ifndef NXD_MQTT_INITIAL_PACKET_ID_VALUE 164 #define NXD_MQTT_INITIAL_PACKET_ID_VALUE 1 165 #endif 166 167 /* Set a timeout for socket operations (send, receive, disconnect). */ 168 #ifndef NXD_MQTT_SOCKET_TIMEOUT 169 #define NXD_MQTT_SOCKET_TIMEOUT NX_WAIT_FOREVER 170 #endif 171 172 /* Define the default MQTT TLS (secure) port number */ 173 #define NXD_MQTT_TLS_PORT 8883 174 175 /* Define the default MQTT TLS (secure) over WebSocket port number */ 176 #define NXD_MQTT_OVER_WEBSOCKET_TLS_PORT 443 177 178 179 #define MQTT_PROTOCOL_LEVEL 4 180 181 /* Define bit fields and constant values used in the CONNECT packet. */ 182 #define MQTT_CONNECT_FLAGS_USERNAME (1 << 7) 183 #define MQTT_CONNECT_FLAGS_PASSWORD (1 << 6) 184 #define MQTT_CONNECT_FLAGS_WILL_RETAIN (1 << 5) 185 #define MQTT_CONNECT_FLAGS_WILL_QOS_0 0 186 #define MQTT_CONNECT_FLAGS_WILL_QOS_1 (1 << 3) 187 #define MQTT_CONNECT_FLAGS_WILL_QOS_2 (2 << 3) 188 #define MQTT_CONNECT_FLAGS_WILL_FLAG (1 << 2) 189 #define MQTT_CONNECT_FLAGS_CLEAN_SESSION (1 << 1) 190 #define MQTT_CONNECT_FLAGS_WILL_QOS_FIELD (3 << 3) 191 192 /* Define bit fields and constant values used in the CONNACK packet. */ 193 #define MQTT_CONNACK_CONNECT_FLAGS_SP (1) 194 #define MQTT_CONNACK_CONNECT_RETURN_CODE_ACCEPTED (0) 195 #define MQTT_CONNACK_CONNECT_RETURN_CODE_UNACCEPTABLE_PROTOCOL_VERSION (1) 196 #define MQTT_CONNACK_CONNECT_RETURN_CODE_IDENTIFIER_REJECTED (2) 197 #define MQTT_CONNACK_CONNECT_RETURN_CODE_SERVER_UNAVAILABLE (3) 198 #define MQTT_CONNACK_CONNECT_RETURN_CODE_BAD_USERNAME_PASSWORD (4) 199 #define MQTT_CONNACK_CONNECT_RETURN_CODE_NOT_AUTHORIZED (5) 200 201 /* Define bit fields and constant values used in the PUBLISH packet. */ 202 #define MQTT_PUBLISH_DUP_FLAG (1 << 3) 203 #define MQTT_PUBLISH_QOS_LEVEL_0 (0) 204 #define MQTT_PUBLISH_QOS_LEVEL_1 (1 << 1) 205 #define MQTT_PUBLISH_QOS_LEVEL_2 (2 << 1) 206 #define MQTT_PUBLISH_QOS_LEVEL_FIELD (3 << 1) 207 #define MQTT_PUBLISH_RETAIN (1) 208 209 #define MQTT_CONTROL_PACKET_TYPE_FIELD 0xF0 210 211 #define MQTT_FIXED_HEADER_SIZE 2 212 213 214 #define MQTT_CONTROL_PACKET_TYPE_CONNECT (1) 215 #define MQTT_CONTROL_PACKET_TYPE_CONNACK (2) 216 #define MQTT_CONTROL_PACKET_TYPE_PUBLISH (3) 217 #define MQTT_CONTROL_PACKET_TYPE_PUBACK (4) 218 #define MQTT_CONTROL_PACKET_TYPE_PUBREC (5) 219 #define MQTT_CONTROL_PACKET_TYPE_PUBREL (6) 220 #define MQTT_CONTROL_PACKET_TYPE_PUBCOMP (7) 221 #define MQTT_CONTROL_PACKET_TYPE_SUBSCRIBE (8) 222 #define MQTT_CONTROL_PACKET_TYPE_SUBACK (9) 223 #define MQTT_CONTROL_PACKET_TYPE_UNSUBSCRIBE (10) 224 #define MQTT_CONTROL_PACKET_TYPE_UNSUBACK (11) 225 #define MQTT_CONTROL_PACKET_TYPE_PINGREQ (12) 226 #define MQTT_CONTROL_PACKET_TYPE_PINGRESP (13) 227 #define MQTT_CONTROL_PACKET_TYPE_DISCONNECT (14) 228 /* Determine if a C++ compiler is being used. If so, complete the standard 229 C conditional started above. */ 230 231 232 typedef struct MQTT_PACKET_CONNACK_STRUCT 233 { 234 235 UCHAR mqtt_connack_packet_header; 236 UCHAR mqtt_connack_packet_remaining_length; 237 UCHAR mqtt_connack_packet_ack_flags; 238 UCHAR mqtt_connack_packet_return_code; 239 } MQTT_PACKET_CONNACK; 240 241 typedef struct MQTT_PACKET_PUBLISH_STRUCT 242 { 243 UCHAR mqtt_publish_packet_header; 244 UCHAR mqtt_publish_packet_remaining_length; 245 } MQTT_PACKET_PUBLISH; 246 247 typedef struct MQTT_PACKET_PUBLISH_RESPONSE_STRUCT 248 { 249 UCHAR mqtt_publish_response_packet_header; 250 UCHAR mqtt_publish_response_packet_remaining_length; 251 UCHAR mqtt_publish_response_packet_packet_identifier_msb; 252 UCHAR mqtt_publish_response_packet_packet_identifier_lsb; 253 } MQTT_PACKET_PUBLISH_RESPONSE; 254 255 256 typedef struct MQTT_PACKET_SUBSCRIBE_STRUCT 257 { 258 UCHAR mqtt_subscribe_packet_header; 259 UCHAR mqtt_subscribe_packet_remaining_length; 260 UCHAR mqtt_subscribe_packet_packet_identifier_msb; 261 UCHAR mqtt_subscribe_packet_packet_identifier_lsb; 262 } MQTT_PACKET_SUBSCRIBE; 263 264 typedef struct MQTT_PACKET_PING_STRUCT 265 { 266 UCHAR mqtt_ping_packet_header; 267 UCHAR mqtt_ping_packet_remaining_length; 268 } MQTT_PACKET_PING; 269 270 typedef struct MQTT_PACKET_DISCONNECT_STRUCT 271 { 272 UCHAR mqtt_disconnect_packet_header; 273 UCHAR mqtt_disconnect_packet_remaining_length; 274 } MQTT_PACKET_DISCONNECT; 275 276 277 /* Define the NetX MQTT CLIENT ID. */ 278 #define NXD_MQTT_CLIENT_ID 0x4D515454 279 280 /* Define the default MQTT Non-TLS (Non-secure) port number */ 281 #define NXD_MQTT_CLIENT_NONTLS_PORT 1883 282 283 /* Define the default MQTT TLS (secure) port number */ 284 #define NXD_MQTT_CLIENT_TLS_PORT 8883 285 286 287 #define NXD_MQTT_CLIENT_STATE_INITIALIZE 0 288 #define NXD_MQTT_CLIENT_STATE_IDLE 1 289 #define NXD_MQTT_CLIENT_STATE_CONNECTING 2 290 #define NXD_MQTT_CLIENT_STATE_CONNECTED 3 291 292 293 #define NXD_MQTT_SUCCESS 0x0 294 #define NXD_MQTT_ALREADY_CONNECTED 0x10001 295 #define NXD_MQTT_NOT_CONNECTED 0x10002 296 #define NXD_MQTT_MUTEX_FAILURE 0x10003 297 #define NXD_MQTT_INTERNAL_ERROR 0x10004 298 #define NXD_MQTT_CONNECT_FAILURE 0x10005 299 #define NXD_MQTT_PACKET_POOL_FAILURE 0x10006 300 #define NXD_MQTT_COMMUNICATION_FAILURE 0x10007 301 #define NXD_MQTT_SERVER_MESSAGE_FAILURE 0x10008 302 #define NXD_MQTT_INVALID_PARAMETER 0x10009 303 #define NXD_MQTT_NO_MESSAGE 0x1000A 304 #define NXD_MQTT_PACKET_POOL_EMPTY 0x1000B 305 #define NXD_MQTT_QOS2_NOT_SUPPORTED 0x1000C 306 #define NXD_MQTT_INSUFFICIENT_BUFFER_SPACE 0x1000D 307 #define NXD_MQTT_CLIENT_NOT_RUNNING 0x1000E 308 #define NXD_MQTT_INVALID_PACKET 0x1000F 309 #define NXD_MQTT_PARTIAL_PACKET 0x10010 310 #define NXD_MQTT_CONNECTING 0x10011 311 #define NXD_MQTT_INVALID_STATE 0x10012 312 313 /* The following error codes match the Connect Return code in CONNACK message. */ 314 #define NXD_MQTT_ERROR_CONNECT_RETURN_CODE 0x10080 315 #define NXD_MQTT_ERROR_UNACCEPTABLE_PROTOCOL 0x10081 316 #define NXD_MQTT_ERROR_IDENTIFYIER_REJECTED 0x10082 317 #define NXD_MQTT_ERROR_SERVER_UNAVAILABLE 0x10083 318 #define NXD_MQTT_ERROR_BAD_USERNAME_PASSWORD 0x10084 319 #define NXD_MQTT_ERROR_NOT_AUTHORIZED 0x10085 320 321 322 /* Define the basic MQTT Client control block. */ 323 typedef struct NXD_MQTT_CLIENT_STRUCT 324 { 325 CHAR *nxd_mqtt_client_name; 326 CHAR *nxd_mqtt_client_id; 327 UINT nxd_mqtt_client_id_length; 328 CHAR *nxd_mqtt_client_username; 329 USHORT nxd_mqtt_client_username_length; 330 CHAR *nxd_mqtt_client_password; 331 USHORT nxd_mqtt_client_password_length; 332 UCHAR nxd_mqtt_client_will_qos_retain; 333 const UCHAR *nxd_mqtt_client_will_topic; 334 UINT nxd_mqtt_client_will_topic_length; 335 const UCHAR *nxd_mqtt_client_will_message; 336 UINT nxd_mqtt_client_will_message_length; 337 NX_IP *nxd_mqtt_client_ip_ptr; /* Pointer to associated IP structure */ 338 NX_PACKET_POOL *nxd_mqtt_client_packet_pool_ptr; /* Pointer to client packet pool */ 339 TX_MUTEX *nxd_mqtt_client_mutex_ptr; /* Pointer to client mutex */ 340 TX_TIMER nxd_mqtt_timer; 341 #ifndef NXD_MQTT_CLOUD_ENABLE 342 TX_MUTEX nxd_mqtt_protection; 343 TX_THREAD nxd_mqtt_thread; 344 TX_EVENT_FLAGS_GROUP nxd_mqtt_events; 345 #else 346 NX_CLOUD *nxd_mqtt_client_cloud_ptr; /* Pointer to associated CLOUD structure. */ 347 NX_CLOUD nxd_mqtt_client_cloud; /* MQTT cloud. */ 348 NX_CLOUD_MODULE nxd_mqtt_client_cloud_module; /* Define mqtt module that running on cloud helper thread. */ 349 #endif /* NXD_MQTT_CLOUD_ENABLE */ 350 UINT nxd_mqtt_ping_timeout; 351 UINT nxd_mqtt_ping_not_responded; /* Flag indicating the ping has been responded or not. */ 352 UINT nxd_mqtt_ping_sent_time; /* TX Timer tick when the ping message was sent. */ 353 UINT nxd_mqtt_timeout; /* TX Timer tick when the next timeout happens. */ 354 UINT nxd_mqtt_timer_value; /* MQTT Client periodic timer tick value. */ 355 UINT nxd_mqtt_keepalive; /* Keepalive value, converted to TX ticks. */ 356 UINT nxd_mqtt_clean_session; /* Clean session flag. */ 357 UINT nxd_mqtt_client_state; /* Record client state */ 358 NX_TCP_SOCKET nxd_mqtt_client_socket; 359 struct NXD_MQTT_CLIENT_STRUCT *nxd_mqtt_client_next; 360 UINT nxd_mqtt_client_packet_identifier; 361 NX_PACKET *nxd_mqtt_client_processing_packet; 362 NX_PACKET *message_transmit_queue_head; 363 NX_PACKET *message_transmit_queue_tail; 364 #ifdef NXD_MQTT_MAXIMUM_TRANSMIT_QUEUE_DEPTH 365 UINT message_transmit_queue_depth; 366 #endif /* NXD_MQTT_MAXIMUM_TRANSMIT_QUEUE_DEPTH */ 367 NX_PACKET *message_receive_queue_head; 368 NX_PACKET *message_receive_queue_tail; 369 UINT message_receive_queue_depth; 370 VOID (*nxd_mqtt_client_receive_notify)(struct NXD_MQTT_CLIENT_STRUCT *client_ptr, UINT number_of_messages); 371 VOID (*nxd_mqtt_connect_notify)(struct NXD_MQTT_CLIENT_STRUCT *client_ptr, UINT status, VOID *context); 372 VOID *nxd_mqtt_connect_context; 373 VOID (*nxd_mqtt_disconnect_notify)(struct NXD_MQTT_CLIENT_STRUCT *client_ptr); 374 UINT (*nxd_mqtt_packet_receive_notify)(struct NXD_MQTT_CLIENT_STRUCT *client_ptr, NX_PACKET *packet_ptr, VOID *context); 375 VOID *nxd_mqtt_packet_receive_context; 376 VOID (*nxd_mqtt_ack_receive_notify)(struct NXD_MQTT_CLIENT_STRUCT *client_ptr, UINT type, USHORT packet_id, NX_PACKET *transmit_packet_ptr, VOID *context); 377 VOID *nxd_mqtt_ack_receive_context; 378 #ifdef NX_SECURE_ENABLE 379 UINT nxd_mqtt_client_use_tls; 380 UINT (*nxd_mqtt_tls_setup)(struct NXD_MQTT_CLIENT_STRUCT *, NX_SECURE_TLS_SESSION *, 381 NX_SECURE_X509_CERT *, NX_SECURE_X509_CERT *); 382 NX_SECURE_X509_CERT nxd_mqtt_tls_certificate; 383 NX_SECURE_X509_CERT nxd_mqtt_tls_trusted_certificate; 384 NX_SECURE_TLS_SESSION nxd_mqtt_tls_session; 385 UINT nxd_mqtt_tls_in_progress; 386 #endif 387 #ifdef NXD_MQTT_OVER_WEBSOCKET 388 UINT nxd_mqtt_client_use_websocket; 389 NX_WEBSOCKET_CLIENT nxd_mqtt_client_websocket; 390 UCHAR *nxd_mqtt_client_websocket_host; 391 UINT nxd_mqtt_client_websocket_host_length; 392 UCHAR *nxd_mqtt_client_websocket_uri_path; 393 UINT nxd_mqtt_client_websocket_uri_path_length; 394 #endif /* NXD_MQTT_OVER_WEBSOCKET */ 395 } NXD_MQTT_CLIENT; 396 397 398 399 #ifndef NXD_MQTT_CLIENT_SOURCE_CODE 400 401 /* Application caller is present, perform API mapping. */ 402 403 /* Determine if error checking is desired. If so, map API functions 404 to the appropriate error checking front-ends. Otherwise, map API 405 functions to the core functions that actually perform the work. 406 Note: error checking is enabled by default. */ 407 408 #ifdef NX_DISABLE_ERROR_CHECKING 409 410 #define nxd_mqtt_client_create _nxd_mqtt_client_create 411 #define nxd_mqtt_client_login_set _nxd_mqtt_client_login_set 412 #define nxd_mqtt_client_will_message_set _nxd_mqtt_client_will_message_set 413 #define nxd_mqtt_client_delete _nxd_mqtt_client_delete 414 #define nxd_mqtt_client_connect _nxd_mqtt_client_connect 415 #define nxd_mqtt_client_secure_connect _nxd_mqtt_client_secure_connect 416 #define nxd_mqtt_client_publish _nxd_mqtt_client_publish 417 #define nxd_mqtt_client_subscribe _nxd_mqtt_client_subscribe 418 #define nxd_mqtt_client_unsubscribe _nxd_mqtt_client_unsubscribe 419 #define nxd_mqtt_client_disconnect _nxd_mqtt_client_disconnect 420 #define nxd_mqtt_client_receive_notify_set _nxd_mqtt_client_receive_notify_set 421 #define nxd_mqtt_client_message_get _nxd_mqtt_client_message_get 422 #define nxd_mqtt_client_disconnect_notify_set _nxd_mqtt_client_disconnect_notify_set 423 #ifdef NXD_MQTT_OVER_WEBSOCKET 424 #define nxd_mqtt_client_websocket_set _nxd_mqtt_client_websocket_set 425 #endif /* NXD_MQTT_OVER_WEBSOCKET */ 426 #else /* if !NXD_MQTT_CLIENT_SOURCE_CODE */ 427 428 #define nxd_mqtt_client_create _nxde_mqtt_client_create 429 #define nxd_mqtt_client_login_set _nxde_mqtt_client_login_set 430 #define nxd_mqtt_client_will_message_set _nxde_mqtt_client_will_message_set 431 #define nxd_mqtt_client_delete _nxde_mqtt_client_delete 432 #define nxd_mqtt_client_connect _nxde_mqtt_client_connect 433 #define nxd_mqtt_client_secure_connect _nxde_mqtt_client_secure_connect 434 #define nxd_mqtt_client_publish _nxde_mqtt_client_publish 435 #define nxd_mqtt_client_subscribe _nxde_mqtt_client_subscribe 436 #define nxd_mqtt_client_unsubscribe _nxde_mqtt_client_unsubscribe 437 #define nxd_mqtt_client_disconnect _nxde_mqtt_client_disconnect 438 #define nxd_mqtt_client_receive_notify_set _nxde_mqtt_client_receive_notify_set 439 #define nxd_mqtt_client_message_get _nxde_mqtt_client_message_get 440 #define nxd_mqtt_client_disconnect_notify_set _nxde_mqtt_client_disconnect_notify_set 441 #ifdef NXD_MQTT_OVER_WEBSOCKET 442 #define nxd_mqtt_client_websocket_set _nxde_mqtt_client_websocket_set 443 #endif /* NXD_MQTT_OVER_WEBSOCKET */ 444 #endif /* NX_DISABLE_ERROR_CHECKING */ 445 446 447 UINT nxd_mqtt_client_create(NXD_MQTT_CLIENT *client_ptr, CHAR *client_name, CHAR *client_id, UINT client_id_length, 448 NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, VOID *stack_ptr, ULONG stack_size, UINT mqtt_thread_priority, 449 VOID *memory_ptr, ULONG memory_size); 450 451 UINT nxd_mqtt_client_connect(NXD_MQTT_CLIENT *client_ptr, NXD_ADDRESS *server_ip, UINT server_port, 452 UINT keepalive, UINT clean_session, ULONG timeout); 453 454 UINT nxd_mqtt_client_login_set(NXD_MQTT_CLIENT *client_ptr, 455 CHAR *username, UINT username_length, CHAR *password, UINT password_length); 456 457 UINT nxd_mqtt_client_will_message_set(NXD_MQTT_CLIENT *client_ptr, 458 const UCHAR *will_topic, UINT will_topic_length, UCHAR *will_message, UINT will_message_length, 459 UINT will_retain_flag, UINT will_QoS); 460 #ifdef NX_SECURE_ENABLE 461 UINT nxd_mqtt_client_secure_connect(NXD_MQTT_CLIENT *client_ptr, NXD_ADDRESS *server_ip, UINT server_port, 462 UINT (*tls_setup)(NXD_MQTT_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *, NX_SECURE_X509_CERT *, NX_SECURE_X509_CERT *), 463 UINT keepalive, UINT clean_session, ULONG timeout); 464 #endif /* NX_SECURE_ENABLE */ 465 UINT nxd_mqtt_client_publish(NXD_MQTT_CLIENT *client_ptr, CHAR *topic_name, UINT topic_name_length, CHAR *message, UINT message_length, 466 UINT retain, UINT QoS, ULONG timeout); 467 UINT nxd_mqtt_client_subscribe(NXD_MQTT_CLIENT *mqtt_client_pr, CHAR *topic_name, UINT topic_name_length, UINT QoS); 468 UINT nxd_mqtt_client_unsubscribe(NXD_MQTT_CLIENT *mqtt_client_pr, CHAR *topic_name, UINT topic_name_length); 469 UINT nxd_mqtt_client_receive_notify_set(NXD_MQTT_CLIENT *client_ptr, 470 VOID (*receive_notify)(NXD_MQTT_CLIENT *client_ptr, UINT number_of_messages)); 471 UINT nxd_mqtt_client_message_get(NXD_MQTT_CLIENT *client_ptr, UCHAR *topic_buffer, UINT topic_buffer_size, UINT *actual_topic_length, 472 UCHAR *message_buffer, UINT message_buffer_size, UINT *actual_message_length); 473 UINT nxd_mqtt_client_disconnect(NXD_MQTT_CLIENT *client_ptr); 474 475 UINT nxd_mqtt_client_delete(NXD_MQTT_CLIENT *client_ptr); 476 UINT nxd_mqtt_client_disconnect_notify_set(NXD_MQTT_CLIENT *client_ptr, VOID (*disconnect_notify)(NXD_MQTT_CLIENT *)); 477 #ifdef NXD_MQTT_OVER_WEBSOCKET 478 UINT nxd_mqtt_client_websocket_set(NXD_MQTT_CLIENT *client_ptr, UCHAR *host, UINT host_length, UCHAR *uri_path, UINT uri_path_length); 479 #endif /* NXD_MQTT_OVER_WEBSOCKET */ 480 481 #else /* ifdef NXD_MQTT_CLIENT_SOURCE_CODE */ 482 483 UINT _nxd_mqtt_client_connect(NXD_MQTT_CLIENT *client_ptr, NXD_ADDRESS *server_ip, UINT server_port, 484 UINT keepalive, UINT clean_session, ULONG wait_option); 485 UINT _nxd_mqtt_client_create(NXD_MQTT_CLIENT *client_ptr, CHAR *client_name, 486 CHAR *client_id, UINT client_id_length, 487 NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, 488 VOID *stack_ptr, ULONG stack_size, UINT mqtt_thread_priority, 489 VOID *memory_ptr, ULONG memory_size); 490 UINT _nxd_mqtt_client_delete(NXD_MQTT_CLIENT *client_ptr); 491 UINT _nxd_mqtt_client_disconnect(NXD_MQTT_CLIENT *client_ptr); 492 UINT _nxd_mqtt_client_disconnect_notify_set(NXD_MQTT_CLIENT *client_ptr, VOID (*disconnect_notify)(NXD_MQTT_CLIENT *)); 493 UINT _nxd_mqtt_client_login_set(NXD_MQTT_CLIENT *client_ptr, 494 CHAR *username, UINT username_length, CHAR *password, UINT password_length); 495 UINT _nxd_mqtt_client_message_get(NXD_MQTT_CLIENT *client_ptr, UCHAR *topic_buffer, UINT topic_buffer_size, UINT *actual_topic_length, 496 UCHAR *message_buffer, UINT message_buffer_size, UINT *actual_message_length); 497 UINT _nxd_mqtt_client_packet_allocate(NXD_MQTT_CLIENT *client_ptr, NX_PACKET **packet_ptr, ULONG wait_option); 498 UINT _nxd_mqtt_client_publish_packet_send(NXD_MQTT_CLIENT *client_ptr, NX_PACKET *packet_ptr, 499 USHORT packet_id, UINT QoS, ULONG wait_option); 500 UINT _nxd_mqtt_client_publish(NXD_MQTT_CLIENT *client_ptr, CHAR *topic_name, UINT topic_name_length, 501 CHAR *message, UINT message_length, UINT retain, UINT QoS, ULONG timeout); 502 UINT _nxd_mqtt_client_receive_notify_set(NXD_MQTT_CLIENT *client_ptr, 503 VOID (*receive_notify)(NXD_MQTT_CLIENT *client_ptr, UINT message_count)); 504 UINT _nxd_mqtt_client_release_callback_set(NXD_MQTT_CLIENT *client_ptr, VOID (*memory_release_function)(CHAR *, UINT)); 505 UINT _nxd_mqtt_client_sub_unsub(NXD_MQTT_CLIENT *client_ptr, UINT op, 506 CHAR *topic_name, UINT topic_name_length, USHORT *packet_id_ptr, UINT QoS); 507 UINT _nxd_mqtt_client_subscribe(NXD_MQTT_CLIENT *client_ptr, CHAR *topic_name, UINT topic_name_length, UINT QoS); 508 UINT _nxd_mqtt_client_unsubscribe(NXD_MQTT_CLIENT *client_ptr, CHAR *topic_name, UINT topic_name_length); 509 UINT _nxd_mqtt_client_will_message_set(NXD_MQTT_CLIENT *client_ptr, 510 const UCHAR *will_topic, UINT will_topic_length, const UCHAR *will_message, 511 UINT will_message_length, UINT will_retain_flag, UINT will_QoS); 512 UINT _nxd_mqtt_read_remaining_length(NX_PACKET *packet_ptr, UINT *remaining_length, ULONG *offset_ptr); 513 UINT _nxd_mqtt_client_set_fixed_header(NXD_MQTT_CLIENT *client_ptr, NX_PACKET *packet_ptr, UCHAR control_header, UINT length, UINT wait_option); 514 UINT _nxd_mqtt_client_append_message(NXD_MQTT_CLIENT *client_ptr, NX_PACKET *packet_ptr, CHAR *message, UINT length, ULONG wait_option); 515 VOID _nxd_mqtt_client_connection_end(NXD_MQTT_CLIENT *client_ptr, ULONG wait_option); 516 UINT _nxd_mqtt_process_publish_packet(NX_PACKET *packet_ptr, ULONG *topic_offset_ptr, USHORT *topic_length_ptr, 517 ULONG *message_offset_ptr, ULONG *message_length_ptr); 518 519 UINT _nxde_mqtt_client_connect(NXD_MQTT_CLIENT *client_ptr, NXD_ADDRESS *server_ip, UINT server_port, 520 UINT keepalive, UINT clean_session, ULONG timeout); 521 UINT _nxde_mqtt_client_create(NXD_MQTT_CLIENT *client_ptr, CHAR *client_name, CHAR *client_id, UINT client_id_length, 522 NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, 523 VOID *stack_ptr, ULONG stack_size, UINT mqtt_thread_priority, 524 VOID *memory_ptr, ULONG memory_size); 525 UINT _nxde_mqtt_client_delete(NXD_MQTT_CLIENT *client_ptr); 526 UINT _nxde_mqtt_client_disconnect_notify_set(NXD_MQTT_CLIENT *client_ptr, VOID (*disconnect_notify)(NXD_MQTT_CLIENT *)); 527 UINT _nxde_mqtt_client_disconnect(NXD_MQTT_CLIENT *client_ptr); 528 UINT _nxde_mqtt_client_login_set(NXD_MQTT_CLIENT *client_ptr, 529 CHAR *username, UINT username_length, CHAR *password, UINT password_length); 530 UINT _nxde_mqtt_client_message_get(NXD_MQTT_CLIENT *client_ptr, UCHAR *topic_buffer, UINT topic_buffer_size, UINT *actual_topic_length, 531 UCHAR *message_buffer, UINT message_buffer_size, UINT *actual_message_length); 532 UINT _nxde_mqtt_client_publish(NXD_MQTT_CLIENT *client_ptr, CHAR *topic_name, UINT topic_name_length, 533 CHAR *message, UINT message_length, UINT retain, UINT QoS, ULONG timeout); 534 UINT _nxde_mqtt_client_receive_notify_set(NXD_MQTT_CLIENT *client_ptr, 535 VOID (*receive_notify)(NXD_MQTT_CLIENT *client_ptr, UINT message_count)); 536 UINT _nxde_mqtt_client_release_callback_set(NXD_MQTT_CLIENT *client_ptr, VOID (*release_callback)(CHAR *, UINT)); 537 UINT _nxde_mqtt_client_subscribe(NXD_MQTT_CLIENT *client_ptr, CHAR *topic_name, UINT topic_name_length, UINT QoS); 538 UINT _nxde_mqtt_client_unsubscribe(NXD_MQTT_CLIENT *client_ptr, CHAR *topic_name, UINT topic_name_length); 539 UINT _nxde_mqtt_client_will_message_set(NXD_MQTT_CLIENT *client_ptr, 540 const UCHAR *will_topic, UINT will_topic_length, const UCHAR *will_message, 541 UINT will_message_length, UINT will_retain_flag, UINT will_QoS); 542 543 #ifdef NX_SECURE_ENABLE 544 UINT _nxd_mqtt_client_secure_connect(NXD_MQTT_CLIENT *client_ptr, NXD_ADDRESS *server_ip, UINT server_port, 545 UINT (*tls_setup)(NXD_MQTT_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *, 546 NX_SECURE_X509_CERT *, NX_SECURE_X509_CERT *), 547 UINT keepalive, UINT clean_session, ULONG wait_option); 548 UINT _nxde_mqtt_client_secure_connect(NXD_MQTT_CLIENT *client_ptr, NXD_ADDRESS *server_ip, UINT server_port, 549 UINT (*tls_setup)(NXD_MQTT_CLIENT *client_ptr, NX_SECURE_TLS_SESSION *, 550 NX_SECURE_X509_CERT *, NX_SECURE_X509_CERT *), 551 UINT keepalive, UINT clean_session, ULONG timeout); 552 #endif /* NX_SECURE_ENABLE */ 553 554 #ifdef NXD_MQTT_OVER_WEBSOCKET 555 UINT _nxd_mqtt_client_websocket_set(NXD_MQTT_CLIENT *client_ptr, UCHAR *host, UINT host_length, UCHAR *uri_path, UINT uri_path_length); 556 UINT _nxde_mqtt_client_websocket_set(NXD_MQTT_CLIENT *client_ptr, UCHAR *host, UINT host_length, UCHAR *uri_path, UINT uri_path_length); 557 VOID _nxd_mqtt_client_websocket_connection_status_callback(NX_WEBSOCKET_CLIENT *websocket_client_ptr, VOID *context, UINT status); 558 #endif /* NXD_MQTT_OVER_WEBSOCKET */ 559 560 #endif /* ifndef NXD_MQTT_CLIENT_SOURCE_CODE */ 561 562 #ifdef NXD_MQTT_CLOUD_ENABLE 563 /* MQTT create function based on cloud helper. */ 564 UINT _nxd_mqtt_client_cloud_create(NXD_MQTT_CLIENT *client_ptr, CHAR *client_name, 565 CHAR *client_id, UINT client_id_length, 566 NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, NX_CLOUD *cloud_ptr); 567 #endif /* NXD_MQTT_CLOUD_ENABLE */ 568 569 /* Determine if a C++ compiler is being used. If so, complete the standard 570 C conditional started above. */ 571 #ifdef __cplusplus 572 } 573 #endif 574 575 576 #endif /* NXD_MQTT_CLIENT_H */ 577 578