| 1 | #ifndef AWS_MQTT_CLIENT_H |
| 2 | #define AWS_MQTT_CLIENT_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/common/hash_table.h> |
| 10 | |
| 11 | #include <aws/common/byte_buf.h> |
| 12 | #include <aws/common/ref_count.h> |
| 13 | #include <aws/common/string.h> |
| 14 | |
| 15 | #include <aws/io/event_loop.h> |
| 16 | #include <aws/io/host_resolver.h> |
| 17 | |
| 18 | #include <aws/mqtt/mqtt.h> |
| 19 | |
| 20 | AWS_PUSH_SANE_WARNING_LEVEL |
| 21 | |
| 22 | /* forward declares */ |
| 23 | struct aws_client_bootstrap; |
| 24 | struct aws_http_header; |
| 25 | struct aws_http_message; |
| 26 | struct aws_http_proxy_options; |
| 27 | struct aws_mqtt5_client; |
| 28 | struct aws_socket_options; |
| 29 | struct aws_tls_connection_options; |
| 30 | |
| 31 | /** |
| 32 | * Empty struct that is passed when on_connection_closed is called. |
| 33 | * Currently holds nothing but will allow expanding in the future should it be needed. |
| 34 | */ |
| 35 | struct on_connection_closed_data; |
| 36 | |
| 37 | struct aws_mqtt_client { |
| 38 | struct aws_allocator *allocator; |
| 39 | struct aws_client_bootstrap *bootstrap; |
| 40 | struct aws_ref_count ref_count; |
| 41 | }; |
| 42 | |
| 43 | struct aws_mqtt_client_connection; |
| 44 | |
| 45 | /** |
| 46 | * Callback called when a request roundtrip is complete (QoS0 immediately, QoS1 on PUBACK, QoS2 on PUBCOMP). Either |
| 47 | * succeed or not |
| 48 | */ |
| 49 | typedef void(aws_mqtt_op_complete_fn)( |
| 50 | struct aws_mqtt_client_connection *connection, |
| 51 | uint16_t packet_id, |
| 52 | int error_code, |
| 53 | void *userdata); |
| 54 | |
| 55 | /** |
| 56 | * Called when a connection attempt is completed, either in success or error. |
| 57 | * |
| 58 | * If error code is AWS_ERROR_SUCCESS, then a CONNACK has been received from the server and return_code and |
| 59 | * session_present contain the values received. If error_code is not AWS_ERROR_SUCCESS, it refers to the internal error |
| 60 | * that occurred during connection, and return_code and session_present are invalid. |
| 61 | */ |
| 62 | typedef void(aws_mqtt_client_on_connection_complete_fn)( |
| 63 | struct aws_mqtt_client_connection *connection, |
| 64 | int error_code, |
| 65 | enum aws_mqtt_connect_return_code return_code, |
| 66 | bool session_present, |
| 67 | void *userdata); |
| 68 | |
| 69 | /* Called when a connection attempt succeed (with a successful CONNACK) |
| 70 | * |
| 71 | * The callback is derived from aws_mqtt_client_on_connection_complete_fn. |
| 72 | * It gets triggered when connection succeed (with a successful CONNACK) |
| 73 | */ |
| 74 | typedef void(aws_mqtt_client_on_connection_success_fn)( |
| 75 | struct aws_mqtt_client_connection *connection, |
| 76 | enum aws_mqtt_connect_return_code return_code, |
| 77 | bool session_present, |
| 78 | void *userdata); |
| 79 | |
| 80 | /* Called if the connection attempt failed. |
| 81 | * |
| 82 | * The callback is derived from aws_mqtt_client_on_connection_complete_fn. |
| 83 | * It gets triggered when connection failed. |
| 84 | */ |
| 85 | typedef void(aws_mqtt_client_on_connection_failure_fn)( |
| 86 | struct aws_mqtt_client_connection *connection, |
| 87 | int error_code, |
| 88 | void *userdata); |
| 89 | |
| 90 | /* Called if the connection to the server is lost. */ |
| 91 | typedef void(aws_mqtt_client_on_connection_interrupted_fn)( |
| 92 | struct aws_mqtt_client_connection *connection, |
| 93 | int error_code, |
| 94 | void *userdata); |
| 95 | |
| 96 | /** |
| 97 | * Called if the connection to the server is closed by user request |
| 98 | * Note: Currently the "data" argument is always NULL, but this may change in the future if additional data is needed to |
| 99 | * be sent. |
| 100 | */ |
| 101 | typedef void(aws_mqtt_client_on_connection_closed_fn)( |
| 102 | struct aws_mqtt_client_connection *connection, |
| 103 | struct on_connection_closed_data *data, |
| 104 | void *userdata); |
| 105 | |
| 106 | /** |
| 107 | * Called when a connection to the server is resumed |
| 108 | * (if clean_session is true, calling aws_mqtt_resubscribe_existing_topics is suggested) |
| 109 | */ |
| 110 | typedef void(aws_mqtt_client_on_connection_resumed_fn)( |
| 111 | struct aws_mqtt_client_connection *connection, |
| 112 | enum aws_mqtt_connect_return_code return_code, |
| 113 | bool session_present, |
| 114 | void *userdata); |
| 115 | |
| 116 | /** |
| 117 | * Called when a multi-topic subscription request is complete. |
| 118 | * Note: If any topic_suback's qos value is AWS_MQTT_QOS_FAILURE, |
| 119 | * then that topic subscription was rejected by the broker. |
| 120 | */ |
| 121 | typedef void(aws_mqtt_suback_multi_fn)( |
| 122 | struct aws_mqtt_client_connection *connection, |
| 123 | uint16_t packet_id, |
| 124 | const struct aws_array_list *topic_subacks, /* contains aws_mqtt_topic_subscription pointers */ |
| 125 | int error_code, |
| 126 | void *userdata); |
| 127 | |
| 128 | /** |
| 129 | * Called when a single-topic subscription request is complete. |
| 130 | * Note: If the qos value is AWS_MQTT_QOS_FAILURE, |
| 131 | * then the subscription was rejected by the broker. |
| 132 | */ |
| 133 | typedef void(aws_mqtt_suback_fn)( |
| 134 | struct aws_mqtt_client_connection *connection, |
| 135 | uint16_t packet_id, |
| 136 | const struct aws_byte_cursor *topic, |
| 137 | enum aws_mqtt_qos qos, |
| 138 | int error_code, |
| 139 | void *userdata); |
| 140 | |
| 141 | /** |
| 142 | * Called when a publish message is received. |
| 143 | * |
| 144 | * \param[in] connection The connection object |
| 145 | * \param[in] topic The information channel to which the payload data was published. |
| 146 | * \param[in] payload The payload data. |
| 147 | * \param[in] dup DUP flag. If true, this might be re-delivery of an earlier attempt to send the message. |
| 148 | * \param[in] qos Quality of Service used to deliver the message. |
| 149 | * \param[in] retain Retain flag. If true, the message was sent as a result of a new subscription being |
| 150 | * made by the client. |
| 151 | */ |
| 152 | typedef void(aws_mqtt_client_publish_received_fn)( |
| 153 | struct aws_mqtt_client_connection *connection, |
| 154 | const struct aws_byte_cursor *topic, |
| 155 | const struct aws_byte_cursor *payload, |
| 156 | bool dup, |
| 157 | enum aws_mqtt_qos qos, |
| 158 | bool retain, |
| 159 | void *userdata); |
| 160 | |
| 161 | /** Called when a connection is closed, right before any resources are deleted */ |
| 162 | typedef void(aws_mqtt_client_on_disconnect_fn)(struct aws_mqtt_client_connection *connection, void *userdata); |
| 163 | |
| 164 | /** |
| 165 | * Signature of callback invoked on a connection destruction. |
| 166 | */ |
| 167 | typedef void(aws_mqtt_client_on_connection_termination_fn)(void *userdata); |
| 168 | |
| 169 | /** |
| 170 | * Function to invoke when the websocket handshake request transformation completes. |
| 171 | * This function MUST be invoked or the application will soft-lock. |
| 172 | * |
| 173 | * `request` and `complete_ctx` must be the same pointers provided to the `aws_mqtt_transform_websocket_handshake_fn`. |
| 174 | * `error_code` should should be AWS_ERROR_SUCCESS if transformation was successful, |
| 175 | * otherwise pass a different AWS_ERROR_X value. |
| 176 | */ |
| 177 | typedef void(aws_mqtt_transform_websocket_handshake_complete_fn)( |
| 178 | struct aws_http_message *request, |
| 179 | int error_code, |
| 180 | void *complete_ctx); |
| 181 | |
| 182 | /** |
| 183 | * Function that may transform the websocket handshake request. |
| 184 | * Called each time a websocket connection is attempted. |
| 185 | * |
| 186 | * The default request uses path "/mqtt". All required headers are present, |
| 187 | * plus the optional header "Sec-WebSocket-Protocol: mqtt". |
| 188 | * |
| 189 | * The user MUST invoke the `complete_fn` when transformation is complete or the application will soft-lock. |
| 190 | * When invoking the `complete_fn`, pass along the `request` and `complete_ctx` provided here and an error code. |
| 191 | * The error code should be AWS_ERROR_SUCCESS if transformation was successful, |
| 192 | * otherwise pass a different AWS_ERROR_X value. |
| 193 | */ |
| 194 | typedef void(aws_mqtt_transform_websocket_handshake_fn)( |
| 195 | struct aws_http_message *request, |
| 196 | void *user_data, |
| 197 | aws_mqtt_transform_websocket_handshake_complete_fn *complete_fn, |
| 198 | void *complete_ctx); |
| 199 | |
| 200 | /** |
| 201 | * Function that may accept or reject a websocket handshake response. |
| 202 | * Called each time a valid websocket connection is established. |
| 203 | * |
| 204 | * All required headers have been checked already (ex: "Sec-Websocket-Accept"), |
| 205 | * |
| 206 | * Return AWS_OP_SUCCESS to accept the connection or AWS_OP_ERR to stop the connection attempt. |
| 207 | */ |
| 208 | typedef int aws_mqtt_validate_websocket_handshake_fn( |
| 209 | struct aws_mqtt_client_connection *connection, |
| 210 | const struct aws_http_header *header_array, |
| 211 | size_t num_headers, |
| 212 | void *userdata); |
| 213 | |
| 214 | /** Passed to subscribe() and suback callbacks */ |
| 215 | struct aws_mqtt_topic_subscription { |
| 216 | struct aws_byte_cursor topic; |
| 217 | enum aws_mqtt_qos qos; |
| 218 | |
| 219 | aws_mqtt_client_publish_received_fn *on_publish; |
| 220 | aws_mqtt_userdata_cleanup_fn *on_cleanup; |
| 221 | void *on_publish_ud; |
| 222 | }; |
| 223 | |
| 224 | /** |
| 225 | * host_name The server name to connect to. This resource may be freed immediately on return. |
| 226 | * port The port on the server to connect to |
| 227 | * client_id The clientid to place in the CONNECT packet. |
| 228 | * socket_options The socket options to pass to the aws_client_bootstrap functions. |
| 229 | * This is copied into the connection |
| 230 | * tls_options TLS settings to use when opening a connection. |
| 231 | * This is copied into the connection |
| 232 | * Pass NULL to connect without TLS (NOT RECOMMENDED) |
| 233 | * clean_session True to discard all server session data and start fresh |
| 234 | * keep_alive_time_secs The keep alive value to place in the CONNECT PACKET, a PING will automatically |
| 235 | * be sent at this interval as well. If you specify 0, defaults will be used |
| 236 | * and a ping will be sent once per 20 minutes. |
| 237 | * This duration must be longer than ping_timeout_ms. |
| 238 | * ping_timeout_ms Network connection is re-established if a ping response is not received |
| 239 | * within this amount of time (milliseconds). If you specify 0, a default value of 3 seconds |
| 240 | * is used. Alternatively, tcp keep-alive may be away to accomplish this in a more efficient |
| 241 | * (low-power) scenario, but keep-alive options may not work the same way on every platform |
| 242 | * and OS version. This duration must be shorter than keep_alive_time_secs. |
| 243 | * protocol_operation_timeout_ms |
| 244 | * Timeout when waiting for the response to some operation requires response by protocol. |
| 245 | * Set to zero to disable timeout. Otherwise, the operation will fail with error |
| 246 | * AWS_ERROR_MQTT_TIMEOUT if no response is received within this amount of time after |
| 247 | * the packet is written to the socket. The timer is reset if the connection is interrupted. |
| 248 | * It applied to PUBLISH (QoS>0) and UNSUBSCRIBE now. |
| 249 | * Note: While the MQTT 3 specification states that a broker MUST respond, |
| 250 | * some brokers are known to ignore publish packets in exceptional circumstances |
| 251 | * (e.g. AWS IoT Core will not respond if the publish quota is exceeded). |
| 252 | * on_connection_complete The callback to fire when the connection attempt completes |
| 253 | * user_data Passed to the userdata param of on_connection_complete |
| 254 | */ |
| 255 | struct aws_mqtt_connection_options { |
| 256 | struct aws_byte_cursor host_name; |
| 257 | uint32_t port; |
| 258 | struct aws_socket_options *socket_options; |
| 259 | struct aws_tls_connection_options *tls_options; |
| 260 | struct aws_byte_cursor client_id; |
| 261 | uint16_t keep_alive_time_secs; |
| 262 | uint32_t ping_timeout_ms; |
| 263 | uint32_t protocol_operation_timeout_ms; |
| 264 | aws_mqtt_client_on_connection_complete_fn *on_connection_complete; |
| 265 | void *user_data; |
| 266 | bool clean_session; |
| 267 | }; |
| 268 | |
| 269 | /** |
| 270 | * Contains some simple statistics about the current state of the connection's queue of operations |
| 271 | */ |
| 272 | struct aws_mqtt_connection_operation_statistics { |
| 273 | /** |
| 274 | * total number of operations submitted to the connection that have not yet been completed. Unacked operations |
| 275 | * are a subset of this. |
| 276 | */ |
| 277 | uint64_t incomplete_operation_count; |
| 278 | |
| 279 | /** |
| 280 | * total packet size of operations submitted to the connection that have not yet been completed. Unacked operations |
| 281 | * are a subset of this. |
| 282 | */ |
| 283 | uint64_t incomplete_operation_size; |
| 284 | |
| 285 | /** |
| 286 | * total number of operations that have been sent to the server and are waiting for a corresponding ACK before |
| 287 | * they can be completed. |
| 288 | */ |
| 289 | uint64_t unacked_operation_count; |
| 290 | |
| 291 | /** |
| 292 | * total packet size of operations that have been sent to the server and are waiting for a corresponding ACK before |
| 293 | * they can be completed. |
| 294 | */ |
| 295 | uint64_t unacked_operation_size; |
| 296 | }; |
| 297 | |
| 298 | AWS_EXTERN_C_BEGIN |
| 299 | |
| 300 | /** |
| 301 | * Creates an instance of aws_mqtt_client. |
| 302 | * |
| 303 | * \param[in] allocator The allocator the client will use for all future allocations |
| 304 | * \param[in] bootstrap The client bootstrap to use to initiate new socket connections |
| 305 | * |
| 306 | * \returns a new instance of an aws_mqtt_client if successful, NULL otherwise |
| 307 | */ |
| 308 | AWS_MQTT_API |
| 309 | struct aws_mqtt_client *aws_mqtt_client_new(struct aws_allocator *allocator, struct aws_client_bootstrap *bootstrap); |
| 310 | |
| 311 | /** |
| 312 | * Increments the ref count to an mqtt client, allowing the caller to take a reference to it |
| 313 | * |
| 314 | * \param[in] client The client to increment the ref count on |
| 315 | * |
| 316 | * \returns the mqtt client |
| 317 | */ |
| 318 | AWS_MQTT_API |
| 319 | struct aws_mqtt_client *aws_mqtt_client_acquire(struct aws_mqtt_client *client); |
| 320 | |
| 321 | /** |
| 322 | * Decrements the ref count on an mqtt client. If the ref count drops to zero, the client is cleaned up. |
| 323 | * |
| 324 | * \param[in] client The client to release a ref count on |
| 325 | */ |
| 326 | AWS_MQTT_API |
| 327 | void aws_mqtt_client_release(struct aws_mqtt_client *client); |
| 328 | |
| 329 | /** |
| 330 | * Spawns a new connection object. |
| 331 | * |
| 332 | * \param[in] client The client to spawn the connection from |
| 333 | * |
| 334 | * \returns a new mqtt connection on success, NULL otherwise |
| 335 | */ |
| 336 | AWS_MQTT_API |
| 337 | struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(struct aws_mqtt_client *client); |
| 338 | |
| 339 | /** |
| 340 | * Creates a new MQTT311 connection object that uses an MQTT5 client under the hood |
| 341 | * |
| 342 | * \param[in] client The mqtt5 client to create the connection from |
| 343 | * |
| 344 | * \returns a new mqtt (311) connection on success, NULL otherwise |
| 345 | */ |
| 346 | AWS_MQTT_API |
| 347 | struct aws_mqtt_client_connection *aws_mqtt_client_connection_new_from_mqtt5_client(struct aws_mqtt5_client *client); |
| 348 | |
| 349 | /** |
| 350 | * Increments the ref count to an mqtt client connection, allowing the caller to take a reference to it |
| 351 | * |
| 352 | * \param[in] connection The connection object |
| 353 | * |
| 354 | * \returns the mqtt connection |
| 355 | */ |
| 356 | AWS_MQTT_API |
| 357 | struct aws_mqtt_client_connection *aws_mqtt_client_connection_acquire(struct aws_mqtt_client_connection *connection); |
| 358 | |
| 359 | /** |
| 360 | * Decrements the ref count on an mqtt connection. If the ref count drops to zero, the connection is cleaned up. |
| 361 | * Note: cannot call this with lock held, since it will start the destroy process and cause a dead lock. |
| 362 | * |
| 363 | * \param[in] connection The connection object |
| 364 | */ |
| 365 | AWS_MQTT_API |
| 366 | void aws_mqtt_client_connection_release(struct aws_mqtt_client_connection *connection); |
| 367 | |
| 368 | /** |
| 369 | * Sets the will message to send with the CONNECT packet. |
| 370 | * |
| 371 | * \param[in] connection The connection object |
| 372 | * \param[in] topic The topic to publish the will on |
| 373 | * \param[in] qos The QoS to publish the will with |
| 374 | * \param[in] retain The retain flag to publish the will with |
| 375 | * \param[in] payload The data if the will message |
| 376 | */ |
| 377 | AWS_MQTT_API |
| 378 | int aws_mqtt_client_connection_set_will( |
| 379 | struct aws_mqtt_client_connection *connection, |
| 380 | const struct aws_byte_cursor *topic, |
| 381 | enum aws_mqtt_qos qos, |
| 382 | bool retain, |
| 383 | const struct aws_byte_cursor *payload); |
| 384 | |
| 385 | /** |
| 386 | * Sets the username and/or password to send with the CONNECT packet. |
| 387 | * |
| 388 | * \param[in] connection The connection object |
| 389 | * \param[in] username The username to connect with |
| 390 | * \param[in] password [optional] The password to connect with |
| 391 | */ |
| 392 | AWS_MQTT_API |
| 393 | int aws_mqtt_client_connection_set_login( |
| 394 | struct aws_mqtt_client_connection *connection, |
| 395 | const struct aws_byte_cursor *username, |
| 396 | const struct aws_byte_cursor *password); |
| 397 | |
| 398 | /** |
| 399 | * Use MQTT over websockets when connecting. |
| 400 | * Requires the MQTT_WITH_WEBSOCKETS build option. |
| 401 | * |
| 402 | * In this scenario, an HTTP connection is established, which is then upgraded to a websocket connection, |
| 403 | * which is then used to send MQTT data. |
| 404 | * |
| 405 | * \param[in] connection The connection object. |
| 406 | * \param[in] transformer [optional] Function that may transform the websocket handshake request. |
| 407 | * See `aws_mqtt_transform_websocket_handshake_fn` for more info. |
| 408 | * \param[in] transformer_ud [optional] Userdata for request_transformer. |
| 409 | * \param[in] validator [optional] Function that may reject the websocket handshake response. |
| 410 | * \param[in] validator_ud [optional] Userdata for response_validator. |
| 411 | */ |
| 412 | AWS_MQTT_API |
| 413 | int aws_mqtt_client_connection_use_websockets( |
| 414 | struct aws_mqtt_client_connection *connection, |
| 415 | aws_mqtt_transform_websocket_handshake_fn *transformer, |
| 416 | void *transformer_ud, |
| 417 | aws_mqtt_validate_websocket_handshake_fn *validator, |
| 418 | void *validator_ud); |
| 419 | |
| 420 | /** |
| 421 | * Set http proxy options for the connection. |
| 422 | */ |
| 423 | AWS_MQTT_API |
| 424 | int aws_mqtt_client_connection_set_http_proxy_options( |
| 425 | struct aws_mqtt_client_connection *connection, |
| 426 | struct aws_http_proxy_options *proxy_options); |
| 427 | |
| 428 | /** |
| 429 | * Set host resolution ooptions for the connection. |
| 430 | */ |
| 431 | AWS_MQTT_API |
| 432 | int aws_mqtt_client_connection_set_host_resolution_options( |
| 433 | struct aws_mqtt_client_connection *connection, |
| 434 | const struct aws_host_resolution_config *host_resolution_config); |
| 435 | |
| 436 | /** |
| 437 | * Sets the minimum and maximum reconnect timeouts. |
| 438 | * |
| 439 | * The time between reconnect attempts will start at min and multiply by 2 until max is reached. |
| 440 | * |
| 441 | * \param[in] connection The connection object |
| 442 | * \param[in] min_timeout The timeout to start with |
| 443 | * \param[in] max_timeout The highest allowable wait time between reconnect attempts |
| 444 | */ |
| 445 | AWS_MQTT_API |
| 446 | int aws_mqtt_client_connection_set_reconnect_timeout( |
| 447 | struct aws_mqtt_client_connection *connection, |
| 448 | uint64_t min_timeout, |
| 449 | uint64_t max_timeout); |
| 450 | |
| 451 | /** |
| 452 | * Sets the callbacks to call when a connection succeeds or fails |
| 453 | * |
| 454 | * \param[in] connection The connection object |
| 455 | * \param[in] on_connection_success The function to call when a connection is successful or gets resumed |
| 456 | * \param[in] on_connection_success_ud Userdata for on_connection_success |
| 457 | * \param[in] on_connection_failure The function to call when a connection fails |
| 458 | * \param[in] on_connection_failure_ud Userdata for on_connection_failure |
| 459 | */ |
| 460 | AWS_MQTT_API |
| 461 | int aws_mqtt_client_connection_set_connection_result_handlers( |
| 462 | struct aws_mqtt_client_connection *connection, |
| 463 | aws_mqtt_client_on_connection_success_fn *on_connection_success, |
| 464 | void *on_connection_success_ud, |
| 465 | aws_mqtt_client_on_connection_failure_fn *on_connection_failure, |
| 466 | void *on_connection_failure_ud); |
| 467 | |
| 468 | /** |
| 469 | * Sets the callbacks to call when a connection is interrupted and resumed. |
| 470 | * |
| 471 | * \param[in] connection The connection object |
| 472 | * \param[in] on_interrupted The function to call when a connection is lost |
| 473 | * \param[in] on_interrupted_ud Userdata for on_interrupted |
| 474 | * \param[in] on_resumed The function to call when a connection is resumed |
| 475 | (if clean_session is true, calling aws_mqtt_resubscribe_existing_topics is suggested) |
| 476 | * \param[in] on_resumed_ud Userdata for on_resumed |
| 477 | */ |
| 478 | AWS_MQTT_API |
| 479 | int aws_mqtt_client_connection_set_connection_interruption_handlers( |
| 480 | struct aws_mqtt_client_connection *connection, |
| 481 | aws_mqtt_client_on_connection_interrupted_fn *on_interrupted, |
| 482 | void *on_interrupted_ud, |
| 483 | aws_mqtt_client_on_connection_resumed_fn *on_resumed, |
| 484 | void *on_resumed_ud); |
| 485 | |
| 486 | /** |
| 487 | * Sets the callback to call when the connection is closed normally by user request. |
| 488 | * This is different than the connection interrupted or lost, this only covers successful |
| 489 | * closure. |
| 490 | * |
| 491 | * \param[in] connection The connection object |
| 492 | * \param[in] on_closed The function to call when a connection is closed |
| 493 | * \param[in] on_closed_ud Userdata for on_closed |
| 494 | */ |
| 495 | AWS_MQTT_API |
| 496 | int aws_mqtt_client_connection_set_connection_closed_handler( |
| 497 | struct aws_mqtt_client_connection *connection, |
| 498 | aws_mqtt_client_on_connection_closed_fn *on_closed, |
| 499 | void *on_closed_ud); |
| 500 | |
| 501 | /** |
| 502 | * Sets the callback to call whenever ANY publish packet is received. Only safe to set when connection is not connected. |
| 503 | * |
| 504 | * \param[in] connection The connection object |
| 505 | * \param[in] on_any_publish The function to call when a publish is received (pass NULL to unset) |
| 506 | * \param[in] on_any_publish_ud Userdata for on_any_publish |
| 507 | */ |
| 508 | AWS_MQTT_API |
| 509 | int aws_mqtt_client_connection_set_on_any_publish_handler( |
| 510 | struct aws_mqtt_client_connection *connection, |
| 511 | aws_mqtt_client_publish_received_fn *on_any_publish, |
| 512 | void *on_any_publish_ud); |
| 513 | |
| 514 | /** |
| 515 | * Sets the callback to call on a connection destruction. |
| 516 | * |
| 517 | * \param[in] connection The connection object. |
| 518 | * \param[in] on_termination The function to call when a connection is destroyed. |
| 519 | * \param[in] on_termination_ud Userdata for on_termination. |
| 520 | */ |
| 521 | AWS_MQTT_API |
| 522 | int aws_mqtt_client_connection_set_connection_termination_handler( |
| 523 | struct aws_mqtt_client_connection *connection, |
| 524 | aws_mqtt_client_on_connection_termination_fn *on_termination, |
| 525 | void *on_termination_ud); |
| 526 | |
| 527 | /** |
| 528 | * Opens the actual connection defined by aws_mqtt_client_connection_new. |
| 529 | * Once the connection is opened, on_connack will be called. Only called when connection is disconnected. |
| 530 | * |
| 531 | * \param[in] connection The connection object |
| 532 | * \param[in] connection_options Configuration information for the connection attempt |
| 533 | * |
| 534 | * \returns AWS_OP_SUCCESS if the connection has been successfully initiated, |
| 535 | * otherwise AWS_OP_ERR and aws_last_error() will be set. |
| 536 | */ |
| 537 | AWS_MQTT_API |
| 538 | int aws_mqtt_client_connection_connect( |
| 539 | struct aws_mqtt_client_connection *connection, |
| 540 | const struct aws_mqtt_connection_options *connection_options); |
| 541 | |
| 542 | /** |
| 543 | * DEPRECATED |
| 544 | * Opens the actual connection defined by aws_mqtt_client_connection_new. |
| 545 | * Once the connection is opened, on_connack will be called. |
| 546 | * |
| 547 | * Must be called on a connection that has previously been open, |
| 548 | * as the parameters passed during the last connection will be reused. |
| 549 | * |
| 550 | * \param[in] connection The connection object |
| 551 | * \param[in] on_connection_complete The callback to fire when the connection attempt completes |
| 552 | * \param[in] userdata (nullable) Passed to the userdata param of on_connection_complete |
| 553 | * |
| 554 | * \returns AWS_OP_SUCCESS if the connection has been successfully initiated, |
| 555 | * otherwise AWS_OP_ERR and aws_last_error() will be set. |
| 556 | */ |
| 557 | AWS_MQTT_API |
| 558 | int aws_mqtt_client_connection_reconnect( |
| 559 | struct aws_mqtt_client_connection *connection, |
| 560 | aws_mqtt_client_on_connection_complete_fn *on_connection_complete, |
| 561 | void *userdata); |
| 562 | |
| 563 | /** |
| 564 | * Closes the connection asynchronously, calls the on_disconnect callback. |
| 565 | * All uncompleted requests (publish/subscribe/unsubscribe) will be cancelled, regardless to the status of |
| 566 | * clean_session. DISCONNECT packet will be sent, which deletes the will message from server. |
| 567 | * |
| 568 | * \param[in] connection The connection to close |
| 569 | * \param[in] on_disconnect (nullable) Callback function to invoke when the connection is completely disconnected. |
| 570 | * \param[in] userdata (nullable) passed to on_disconnect |
| 571 | * |
| 572 | * \returns AWS_OP_SUCCESS if the connection is open and is being shutdown, |
| 573 | * otherwise AWS_OP_ERR and aws_last_error() is set. |
| 574 | */ |
| 575 | AWS_MQTT_API |
| 576 | int aws_mqtt_client_connection_disconnect( |
| 577 | struct aws_mqtt_client_connection *connection, |
| 578 | aws_mqtt_client_on_disconnect_fn *on_disconnect, |
| 579 | void *userdata); |
| 580 | |
| 581 | /** |
| 582 | * Subscribe to topic filters. on_publish will be called when a PUBLISH matching each topic_filter is received. |
| 583 | * |
| 584 | * \param[in] connection The connection to subscribe on |
| 585 | * \param[in] topic_filters An array_list of aws_mqtt_topic_subscription (NOT pointers) describing the requests. |
| 586 | * \param[in] on_suback (nullable) Called when a SUBACK has been received from the server and the subscription |
| 587 | * is complete. Broker may fail one of the topics, check the qos in |
| 588 | * aws_mqtt_topic_subscription from the callback |
| 589 | * \param[in] on_suback_ud (nullable) Passed to on_suback |
| 590 | * |
| 591 | * \returns The packet id of the subscribe packet if successfully sent, otherwise 0. |
| 592 | */ |
| 593 | AWS_MQTT_API |
| 594 | uint16_t aws_mqtt_client_connection_subscribe_multiple( |
| 595 | struct aws_mqtt_client_connection *connection, |
| 596 | const struct aws_array_list *topic_filters, |
| 597 | aws_mqtt_suback_multi_fn *on_suback, |
| 598 | void *on_suback_ud); |
| 599 | |
| 600 | /** |
| 601 | * Subscribe to a single topic filter. on_publish will be called when a PUBLISH matching topic_filter is received. |
| 602 | * |
| 603 | * \param[in] connection The connection to subscribe on |
| 604 | * \param[in] topic_filter The topic filter to subscribe on. This resource must persist until on_suback. |
| 605 | * \param[in] qos The maximum QoS of messages to receive |
| 606 | * \param[in] on_publish (nullable) Called when a PUBLISH packet matching topic_filter is received |
| 607 | * \param[in] on_publish_ud (nullable) Passed to on_publish |
| 608 | * \param[in] on_ud_cleanup (nullable) Called when a subscription is removed, on_publish_ud is passed. |
| 609 | * \param[in] on_suback (nullable) Called when a SUBACK has been received from the server and the subscription is |
| 610 | * complete |
| 611 | * \param[in] on_suback_ud (nullable) Passed to on_suback |
| 612 | * |
| 613 | * \returns The packet id of the subscribe packet if successfully sent, otherwise 0. |
| 614 | */ |
| 615 | AWS_MQTT_API |
| 616 | uint16_t aws_mqtt_client_connection_subscribe( |
| 617 | struct aws_mqtt_client_connection *connection, |
| 618 | const struct aws_byte_cursor *topic_filter, |
| 619 | enum aws_mqtt_qos qos, |
| 620 | aws_mqtt_client_publish_received_fn *on_publish, |
| 621 | void *on_publish_ud, |
| 622 | aws_mqtt_userdata_cleanup_fn *on_ud_cleanup, |
| 623 | aws_mqtt_suback_fn *on_suback, |
| 624 | void *on_suback_ud); |
| 625 | |
| 626 | /** |
| 627 | * Resubscribe to all topics currently subscribed to. This is to help when resuming a connection with a clean session. |
| 628 | * |
| 629 | * \param[in] connection The connection to subscribe on |
| 630 | * \param[in] on_suback (nullable) Called when a SUBACK has been received from the server and the subscription is |
| 631 | * complete |
| 632 | * \param[in] on_suback_ud (nullable) Passed to on_suback |
| 633 | * |
| 634 | * \returns The packet id of the subscribe packet if successfully sent, otherwise 0 (and aws_last_error() will be set). |
| 635 | */ |
| 636 | AWS_MQTT_API |
| 637 | uint16_t aws_mqtt_resubscribe_existing_topics( |
| 638 | struct aws_mqtt_client_connection *connection, |
| 639 | aws_mqtt_suback_multi_fn *on_suback, |
| 640 | void *on_suback_ud); |
| 641 | |
| 642 | /** |
| 643 | * Unsubscribe to a topic filter. |
| 644 | * |
| 645 | * \param[in] connection The connection to unsubscribe on |
| 646 | * \param[in] topic_filter The topic filter to unsubscribe on. This resource must persist until on_unsuback. |
| 647 | * \param[in] on_unsuback (nullable) Called when a UNSUBACK has been received from the server and the subscription |
| 648 | * is removed |
| 649 | * \param[in] on_unsuback_ud (nullable) Passed to on_unsuback |
| 650 | * |
| 651 | * \returns The packet id of the unsubscribe packet if successfully sent, otherwise 0. |
| 652 | */ |
| 653 | AWS_MQTT_API |
| 654 | uint16_t aws_mqtt_client_connection_unsubscribe( |
| 655 | struct aws_mqtt_client_connection *connection, |
| 656 | const struct aws_byte_cursor *topic_filter, |
| 657 | aws_mqtt_op_complete_fn *on_unsuback, |
| 658 | void *on_unsuback_ud); |
| 659 | |
| 660 | /** |
| 661 | * Send a PUBLISH packet over connection. |
| 662 | * |
| 663 | * \param[in] connection The connection to publish on |
| 664 | * \param[in] topic The topic to publish on |
| 665 | * \param[in] qos The requested QoS of the packet |
| 666 | * \param[in] retain True to have the server save the packet, and send to all new subscriptions matching topic |
| 667 | * \param[in] payload The data to send as the payload of the publish |
| 668 | * \param[in] on_complete (nullable) For QoS 0, called as soon as the packet is sent |
| 669 | * For QoS 1, called when PUBACK is received |
| 670 | * For QoS 2, called when PUBCOMP is received |
| 671 | * \param[in] user_data (nullable) Passed to on_complete |
| 672 | * |
| 673 | * \returns The packet id of the publish packet if successfully sent, otherwise 0. |
| 674 | */ |
| 675 | AWS_MQTT_API |
| 676 | uint16_t aws_mqtt_client_connection_publish( |
| 677 | struct aws_mqtt_client_connection *connection, |
| 678 | const struct aws_byte_cursor *topic, |
| 679 | enum aws_mqtt_qos qos, |
| 680 | bool retain, |
| 681 | const struct aws_byte_cursor *payload, |
| 682 | aws_mqtt_op_complete_fn *on_complete, |
| 683 | void *userdata); |
| 684 | |
| 685 | /** |
| 686 | * Queries the connection's internal statistics for incomplete/unacked operations. |
| 687 | * \param connection connection to get statistics for |
| 688 | * \param stats set of incomplete/unacked operation statistics |
| 689 | * \returns AWS_OP_SUCCESS if getting the operation statistics were successful, AWS_OP_ERR otherwise |
| 690 | */ |
| 691 | AWS_MQTT_API |
| 692 | int aws_mqtt_client_connection_get_stats( |
| 693 | struct aws_mqtt_client_connection *connection, |
| 694 | struct aws_mqtt_connection_operation_statistics *stats); |
| 695 | |
| 696 | /** |
| 697 | * Sets IoT SDK metrics configuration for the connection. |
| 698 | * These metrics will be appended to the username field during connection. |
| 699 | * |
| 700 | * \param connection The connection object |
| 701 | * \param metrics The IoT SDK metrics configuration (pass NULL to disable metrics) |
| 702 | * \returns AWS_OP_SUCCESS if successful, AWS_OP_ERR otherwise |
| 703 | */ |
| 704 | AWS_MQTT_API |
| 705 | int aws_mqtt_client_connection_set_metrics( |
| 706 | struct aws_mqtt_client_connection *connection, |
| 707 | const struct aws_mqtt_iot_metrics *metrics); |
| 708 | |
| 709 | AWS_EXTERN_C_END |
| 710 | AWS_POP_SANE_WARNING_LEVEL |
| 711 | |
| 712 | #endif /* AWS_MQTT_CLIENT_H */ |