| 1 | #ifndef AWS_MQTT_MQTT5_CLIENT_H |
| 2 | #define AWS_MQTT_MQTT5_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/mqtt/mqtt.h> |
| 10 | |
| 11 | #include <aws/io/retry_strategy.h> |
| 12 | #include <aws/mqtt/v5/mqtt5_types.h> |
| 13 | |
| 14 | AWS_PUSH_SANE_WARNING_LEVEL |
| 15 | |
| 16 | struct aws_allocator; |
| 17 | struct aws_client_bootstrap; |
| 18 | struct aws_host_resolution_config; |
| 19 | struct aws_http_message; |
| 20 | struct aws_mqtt5_client; |
| 21 | struct aws_mqtt5_client_lifecycle_event; |
| 22 | struct aws_tls_connection_options; |
| 23 | struct aws_socket_options; |
| 24 | |
| 25 | /* public client-related enums */ |
| 26 | |
| 27 | /** |
| 28 | * Controls how the mqtt client should behave with respect to mqtt sessions. |
| 29 | */ |
| 30 | enum aws_mqtt5_client_session_behavior_type { |
| 31 | /** |
| 32 | * Maps to AWS_MQTT5_CSBT_CLEAN |
| 33 | */ |
| 34 | AWS_MQTT5_CSBT_DEFAULT, |
| 35 | |
| 36 | /** |
| 37 | * Always join a new, clean session |
| 38 | */ |
| 39 | AWS_MQTT5_CSBT_CLEAN, |
| 40 | |
| 41 | /** |
| 42 | * Always attempt to rejoin an existing session after an initial connection success. |
| 43 | */ |
| 44 | AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS, |
| 45 | |
| 46 | /** |
| 47 | * Always attempt to rejoin an existing session. Since the client does not support durable session persistence, |
| 48 | * this option is not guaranteed to be spec compliant because any unacknowledged qos1 publishes (which are |
| 49 | * part of the client session state) will not be present on the initial connection. Until we support |
| 50 | * durable session resumption, this option is technically spec-breaking, but useful. |
| 51 | */ |
| 52 | AWS_MQTT5_CSBT_REJOIN_ALWAYS, |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * Outbound topic aliasing behavior is controlled by this type. |
| 57 | * |
| 58 | * Topic alias behavior is described in https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901113 |
| 59 | * |
| 60 | * If the server allows topic aliasing, this setting controls how topic aliases are used on PUBLISH packets sent |
| 61 | * from the client to the server. |
| 62 | * |
| 63 | * If topic aliasing is not supported by the server, this setting has no effect and any attempts to directly |
| 64 | * manipulate the topic alias id in outbound publishes will be ignored. |
| 65 | */ |
| 66 | enum aws_mqtt5_client_outbound_topic_alias_behavior_type { |
| 67 | /** |
| 68 | * Maps to AWS_MQTT5_COTABT_DISABLED This keeps the client from being broken (by default) if the broker |
| 69 | * topic aliasing implementation has a problem. |
| 70 | */ |
| 71 | AWS_MQTT5_COTABT_DEFAULT, |
| 72 | |
| 73 | /** |
| 74 | * Outbound aliasing is the user's responsibility. Client will cache and use |
| 75 | * previously-established aliases if they fall within the negotiated limits of the connection. |
| 76 | * |
| 77 | * The user must still always submit a full topic in their publishes because disconnections disrupt |
| 78 | * topic alias mappings unpredictably. The client will properly use the alias when the current connection |
| 79 | * has seen the alias binding already. |
| 80 | */ |
| 81 | AWS_MQTT5_COTABT_MANUAL, |
| 82 | |
| 83 | /** |
| 84 | * Client ignores any user-specified topic aliasing and acts on the outbound alias set as an LRU cache. |
| 85 | */ |
| 86 | AWS_MQTT5_COTABT_LRU, |
| 87 | |
| 88 | /** |
| 89 | * Completely disable outbound topic aliasing. |
| 90 | */ |
| 91 | AWS_MQTT5_COTABT_DISABLED |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * Inbound topic aliasing behavior is controlled by this type. |
| 96 | * |
| 97 | * Topic alias behavior is described in https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901113 |
| 98 | * |
| 99 | * This setting controls whether or not the client will send a positive topic alias maximum to the server |
| 100 | * in its CONNECT packets. |
| 101 | * |
| 102 | * If topic aliasing is not supported by the server, this setting has no net effect. |
| 103 | */ |
| 104 | enum aws_mqtt5_client_inbound_topic_alias_behavior_type { |
| 105 | /** |
| 106 | * Maps to AWS_MQTT5_CITABT_DISABLED |
| 107 | */ |
| 108 | AWS_MQTT5_CITABT_DEFAULT, |
| 109 | |
| 110 | /** |
| 111 | * Allow the server to send PUBLISH packets to the client that use topic aliasing |
| 112 | */ |
| 113 | AWS_MQTT5_CITABT_ENABLED, |
| 114 | |
| 115 | /** |
| 116 | * Forbid the server from sending PUBLISH packets to the client that use topic aliasing |
| 117 | */ |
| 118 | AWS_MQTT5_CITABT_DISABLED |
| 119 | }; |
| 120 | |
| 121 | /** |
| 122 | * Configuration struct for all client topic aliasing behavior. If this is left null, then all default options |
| 123 | * (as it zeroed) will be used. |
| 124 | */ |
| 125 | struct aws_mqtt5_client_topic_alias_options { |
| 126 | |
| 127 | /** |
| 128 | * Controls what kind of outbound topic aliasing behavior the client should attempt to use. |
| 129 | */ |
| 130 | enum aws_mqtt5_client_outbound_topic_alias_behavior_type outbound_topic_alias_behavior; |
| 131 | |
| 132 | /** |
| 133 | * If outbound topic aliasing is set to LRU, this controls the maximum size of the cache. If outbound topic |
| 134 | * aliasing is set to LRU and this is zero, a sensible default is used (25). If outbound topic aliasing is not |
| 135 | * set to LRU, then this setting has no effect. |
| 136 | * |
| 137 | * The final size of the cache is determined by the minimum of this setting and the value of the |
| 138 | * topic_alias_maximum property of the received CONNACK. If the received CONNACK does not have an explicit |
| 139 | * positive value for that field, outbound topic aliasing is disabled for the duration of that connection. |
| 140 | */ |
| 141 | uint16_t outbound_alias_cache_max_size; |
| 142 | |
| 143 | /** |
| 144 | * Controls what kind of inbound topic aliasing behavior the client should use. |
| 145 | * |
| 146 | * Even if inbound topic aliasing is enabled, it is up to the server to choose whether or not to use it. |
| 147 | */ |
| 148 | enum aws_mqtt5_client_inbound_topic_alias_behavior_type inbound_topic_alias_behavior; |
| 149 | |
| 150 | /** |
| 151 | * If inbound topic aliasing is enabled, this will control the size of the inbound alias cache. If inbound |
| 152 | * aliases are enabled and this is zero, then a sensible default will be used (25). If inbound aliases are |
| 153 | * disabled, this setting has no effect. |
| 154 | * |
| 155 | * Behaviorally, this value overrides anything present in the topic_alias_maximum field of |
| 156 | * the CONNECT packet options. |
| 157 | */ |
| 158 | uint16_t inbound_alias_cache_size; |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Extended validation and flow control options |
| 163 | * |
| 164 | * Potentially a point of expansion in the future. We could add custom controls letting people override |
| 165 | * the Aws IOT Core limits based on their account properties. We could, with IoT Core support, add dynamic |
| 166 | * limit recognition via user properties as well. |
| 167 | */ |
| 168 | enum aws_mqtt5_extended_validation_and_flow_control_options { |
| 169 | /** |
| 170 | * Do not do any additional validation or flow control outside of the MQTT5 spec |
| 171 | */ |
| 172 | AWS_MQTT5_EVAFCO_NONE, |
| 173 | |
| 174 | /** |
| 175 | * Apply additional client-side operational flow control that respects the |
| 176 | * default AWS IoT Core limits. |
| 177 | * |
| 178 | * Applies the following flow control: |
| 179 | * (1) Outbound throughput throttled to 512KB/s |
| 180 | * (2) Outbound publish TPS throttled to 100 |
| 181 | */ |
| 182 | AWS_MQTT5_EVAFCO_AWS_IOT_CORE_DEFAULTS, |
| 183 | }; |
| 184 | |
| 185 | /** |
| 186 | * Controls how disconnects affect the queued and in-progress operations submitted to the client. Also controls |
| 187 | * how operations are handled while the client is not connected. In particular, if the client is not connected, |
| 188 | * then any operation that would be failed on disconnect (according to these rules) will be rejected. |
| 189 | */ |
| 190 | enum aws_mqtt5_client_operation_queue_behavior_type { |
| 191 | |
| 192 | /* |
| 193 | * Maps to AWS_MQTT5_COQBT_FAIL_QOS0_PUBLISH_ON_DISCONNECT |
| 194 | */ |
| 195 | AWS_MQTT5_COQBT_DEFAULT, |
| 196 | |
| 197 | /* |
| 198 | * Requeues QoS 1+ publishes on disconnect; unacked publishes go to the front, unprocessed publishes stay |
| 199 | * in place. All other operations (QoS 0 publishes, subscribe, unsubscribe) are failed. |
| 200 | */ |
| 201 | AWS_MQTT5_COQBT_FAIL_NON_QOS1_PUBLISH_ON_DISCONNECT, |
| 202 | |
| 203 | /* |
| 204 | * Qos 0 publishes that are not complete at the time of disconnection are failed. Unacked QoS 1+ publishes are |
| 205 | * requeued at the head of the line for immediate retransmission on a session resumption. All other operations |
| 206 | * are requeued in original order behind any retransmissions. |
| 207 | */ |
| 208 | AWS_MQTT5_COQBT_FAIL_QOS0_PUBLISH_ON_DISCONNECT, |
| 209 | |
| 210 | /* |
| 211 | * All operations that are not complete at the time of disconnection are failed, except those operations that |
| 212 | * the mqtt 5 spec requires to be retransmitted (unacked qos1+ publishes). |
| 213 | */ |
| 214 | AWS_MQTT5_COQBT_FAIL_ALL_ON_DISCONNECT, |
| 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * Type of a client lifecycle event |
| 219 | */ |
| 220 | enum aws_mqtt5_client_lifecycle_event_type { |
| 221 | /** |
| 222 | * Emitted when the client begins an attempt to connect to the remote endpoint. |
| 223 | * |
| 224 | * Mandatory event fields: client, user_data |
| 225 | */ |
| 226 | AWS_MQTT5_CLET_ATTEMPTING_CONNECT, |
| 227 | |
| 228 | /** |
| 229 | * Emitted after the client connects to the remote endpoint and receives a successful CONNACK. |
| 230 | * Every ATTEMPTING_CONNECT will be followed by exactly one CONNECTION_SUCCESS or one CONNECTION_FAILURE. |
| 231 | * |
| 232 | * Mandatory event fields: client, user_data, connack_data, settings |
| 233 | */ |
| 234 | AWS_MQTT5_CLET_CONNECTION_SUCCESS, |
| 235 | |
| 236 | /** |
| 237 | * Emitted at any point during the connection process when it has conclusively failed. |
| 238 | * Every ATTEMPTING_CONNECT will be followed by exactly one CONNECTION_SUCCESS or one CONNECTION_FAILURE. |
| 239 | * |
| 240 | * Mandatory event fields: client, user_data, error_code |
| 241 | * Conditional event fields: connack_data |
| 242 | */ |
| 243 | AWS_MQTT5_CLET_CONNECTION_FAILURE, |
| 244 | |
| 245 | /** |
| 246 | * Lifecycle event containing information about a disconnect. Every CONNECTION_SUCCESS will eventually be |
| 247 | * followed by one and only one DISCONNECTION. |
| 248 | * |
| 249 | * Mandatory event fields: client, user_data, error_code |
| 250 | * Conditional event fields: disconnect_data |
| 251 | */ |
| 252 | AWS_MQTT5_CLET_DISCONNECTION, |
| 253 | |
| 254 | /** |
| 255 | * Lifecycle event notifying the user that the client has entered the STOPPED state. Entering this state will |
| 256 | * cause the client to wipe all MQTT session state. |
| 257 | * |
| 258 | * Mandatory event fields: client, user_data |
| 259 | */ |
| 260 | AWS_MQTT5_CLET_STOPPED, |
| 261 | }; |
| 262 | |
| 263 | /* client-related callback function signatures */ |
| 264 | |
| 265 | /** |
| 266 | * Signature of the continuation function to be called after user-code transforms a websocket handshake request |
| 267 | */ |
| 268 | typedef void(aws_mqtt5_transform_websocket_handshake_complete_fn)( |
| 269 | struct aws_http_message *request, |
| 270 | int error_code, |
| 271 | void *complete_ctx); |
| 272 | |
| 273 | /** |
| 274 | * Signature of the websocket handshake request transformation function. After transformation, the completion |
| 275 | * function must be invoked to send the request. |
| 276 | */ |
| 277 | typedef void(aws_mqtt5_transform_websocket_handshake_fn)( |
| 278 | struct aws_http_message *request, |
| 279 | void *user_data, |
| 280 | aws_mqtt5_transform_websocket_handshake_complete_fn *complete_fn, |
| 281 | void *complete_ctx); |
| 282 | |
| 283 | /** |
| 284 | * Callback signature for mqtt5 client lifecycle events. |
| 285 | */ |
| 286 | typedef void(aws_mqtt5_client_connection_event_callback_fn)(const struct aws_mqtt5_client_lifecycle_event *event); |
| 287 | |
| 288 | /** |
| 289 | * Signature of callback to invoke on Publish success/failure. |
| 290 | */ |
| 291 | typedef void(aws_mqtt5_publish_completion_fn)( |
| 292 | enum aws_mqtt5_packet_type packet_type, |
| 293 | const void *packet, |
| 294 | int error_code, |
| 295 | void *complete_ctx); |
| 296 | |
| 297 | /** |
| 298 | * Signature of callback invoked when a manual publish acknowledgement operation completes. |
| 299 | * |
| 300 | * @param puback_result result of the publish acknowledgement operation |
| 301 | * @param completion_user_data user data passed in with the completion options |
| 302 | */ |
| 303 | typedef void(aws_mqtt5_manual_publish_acknowledgement_completion_fn)( |
| 304 | enum aws_mqtt5_manual_publish_acknowledgement_result publish_acknowledgement_result, |
| 305 | void *completion_user_data); |
| 306 | |
| 307 | /** |
| 308 | * Signature of callback to invoke on Subscribe success/failure. |
| 309 | */ |
| 310 | typedef void(aws_mqtt5_subscribe_completion_fn)( |
| 311 | const struct aws_mqtt5_packet_suback_view *suback, |
| 312 | int error_code, |
| 313 | void *complete_ctx); |
| 314 | |
| 315 | /** |
| 316 | * Signature of callback to invoke on Unsubscribe success/failure. |
| 317 | */ |
| 318 | typedef void(aws_mqtt5_unsubscribe_completion_fn)( |
| 319 | const struct aws_mqtt5_packet_unsuback_view *unsuback, |
| 320 | int error_code, |
| 321 | void *complete_ctx); |
| 322 | |
| 323 | /** |
| 324 | * Signature of callback to invoke on Publish received |
| 325 | */ |
| 326 | typedef void(aws_mqtt5_publish_received_fn)(const struct aws_mqtt5_packet_publish_view *publish, void *user_data); |
| 327 | |
| 328 | /** |
| 329 | * Signature of a listener publish received callback that returns an indicator whether or not the publish |
| 330 | * was handled by the listener. |
| 331 | */ |
| 332 | typedef bool( |
| 333 | aws_mqtt5_listener_publish_received_fn)(const struct aws_mqtt5_packet_publish_view *publish, void *user_data); |
| 334 | |
| 335 | /** |
| 336 | * Signature of callback to invoke when a DISCONNECT is fully written to the socket (or fails to be) |
| 337 | */ |
| 338 | typedef void(aws_mqtt5_disconnect_completion_fn)(int error_code, void *complete_ctx); |
| 339 | |
| 340 | /** |
| 341 | * Signature of callback invoked when a client has completely destroyed itself |
| 342 | */ |
| 343 | typedef void(aws_mqtt5_client_termination_completion_fn)(void *complete_ctx); |
| 344 | |
| 345 | /* operation completion options structures */ |
| 346 | |
| 347 | /** |
| 348 | * Completion options for the Publish operation |
| 349 | */ |
| 350 | struct aws_mqtt5_publish_completion_options { |
| 351 | aws_mqtt5_publish_completion_fn *completion_callback; |
| 352 | void *completion_user_data; |
| 353 | |
| 354 | /** Overrides the client's ack timeout with this value, for this operation only */ |
| 355 | uint32_t ack_timeout_seconds_override; |
| 356 | }; |
| 357 | |
| 358 | /** |
| 359 | * Completion options for the manual publish acknowledgement operation |
| 360 | */ |
| 361 | struct aws_mqtt5_manual_publish_acknowledgement_completion_options { |
| 362 | aws_mqtt5_manual_publish_acknowledgement_completion_fn *completion_callback; |
| 363 | void *completion_user_data; |
| 364 | }; |
| 365 | |
| 366 | /** |
| 367 | * Completion options for the Subscribe operation |
| 368 | */ |
| 369 | struct aws_mqtt5_subscribe_completion_options { |
| 370 | aws_mqtt5_subscribe_completion_fn *completion_callback; |
| 371 | void *completion_user_data; |
| 372 | |
| 373 | /** Overrides the client's ack timeout with this value, for this operation only */ |
| 374 | uint32_t ack_timeout_seconds_override; |
| 375 | }; |
| 376 | |
| 377 | /** |
| 378 | * Completion options for the Unsubscribe operation |
| 379 | */ |
| 380 | struct aws_mqtt5_unsubscribe_completion_options { |
| 381 | aws_mqtt5_unsubscribe_completion_fn *completion_callback; |
| 382 | void *completion_user_data; |
| 383 | |
| 384 | /** Overrides the client's ack timeout with this value, for this operation only */ |
| 385 | uint32_t ack_timeout_seconds_override; |
| 386 | }; |
| 387 | |
| 388 | /** |
| 389 | * Completion options for the a DISCONNECT operation |
| 390 | */ |
| 391 | struct aws_mqtt5_disconnect_completion_options { |
| 392 | aws_mqtt5_disconnect_completion_fn *completion_callback; |
| 393 | void *completion_user_data; |
| 394 | }; |
| 395 | |
| 396 | /** |
| 397 | * Mqtt behavior settings that are dynamically negotiated as part of the CONNECT/CONNACK exchange. |
| 398 | */ |
| 399 | struct aws_mqtt5_negotiated_settings { |
| 400 | /** |
| 401 | * The maximum QoS used between the server and client. |
| 402 | */ |
| 403 | enum aws_mqtt5_qos maximum_qos; |
| 404 | |
| 405 | /** |
| 406 | * the amount of time in seconds the server will retain the session after a disconnect. |
| 407 | */ |
| 408 | uint32_t session_expiry_interval; |
| 409 | |
| 410 | /** |
| 411 | * the number of QoS 1 and QoS2 publications the server is willing to process concurrently. |
| 412 | */ |
| 413 | uint16_t receive_maximum_from_server; |
| 414 | |
| 415 | /** |
| 416 | * the maximum packet size the server is willing to accept. |
| 417 | */ |
| 418 | uint32_t maximum_packet_size_to_server; |
| 419 | |
| 420 | /** |
| 421 | * the highest value that the server will accept as a Topic Alias sent by the client. |
| 422 | */ |
| 423 | uint16_t topic_alias_maximum_to_server; |
| 424 | |
| 425 | /** |
| 426 | * the highest value that the client will accept as a Topic Alias sent by the server. |
| 427 | */ |
| 428 | uint16_t topic_alias_maximum_to_client; |
| 429 | |
| 430 | /** |
| 431 | * the amount of time in seconds before the server will disconnect the client for inactivity. |
| 432 | */ |
| 433 | uint16_t server_keep_alive; |
| 434 | |
| 435 | /** |
| 436 | * whether the server supports retained messages. |
| 437 | */ |
| 438 | bool retain_available; |
| 439 | |
| 440 | /** |
| 441 | * whether the server supports wildcard subscriptions. |
| 442 | */ |
| 443 | bool wildcard_subscriptions_available; |
| 444 | |
| 445 | /** |
| 446 | * whether the server supports subscription identifiers |
| 447 | */ |
| 448 | bool subscription_identifiers_available; |
| 449 | |
| 450 | /** |
| 451 | * whether the server supports shared subscriptions |
| 452 | */ |
| 453 | bool shared_subscriptions_available; |
| 454 | |
| 455 | /** |
| 456 | * whether the client has rejoined an existing session. |
| 457 | */ |
| 458 | bool rejoined_session; |
| 459 | |
| 460 | struct aws_byte_buf client_id_storage; |
| 461 | }; |
| 462 | |
| 463 | /** |
| 464 | * Contains some simple statistics about the current state of the client's queue of operations |
| 465 | */ |
| 466 | struct aws_mqtt5_client_operation_statistics { |
| 467 | /* |
| 468 | * total number of operations submitted to the client that have not yet been completed. Unacked operations |
| 469 | * are a subset of this. |
| 470 | */ |
| 471 | uint64_t incomplete_operation_count; |
| 472 | |
| 473 | /* |
| 474 | * total packet size of operations submitted to the client that have not yet been completed. Unacked operations |
| 475 | * are a subset of this. |
| 476 | */ |
| 477 | uint64_t incomplete_operation_size; |
| 478 | |
| 479 | /* |
| 480 | * total number of operations that have been sent to the server and are waiting for a corresponding ACK before |
| 481 | * they can be completed. |
| 482 | */ |
| 483 | uint64_t unacked_operation_count; |
| 484 | |
| 485 | /* |
| 486 | * total packet size of operations that have been sent to the server and are waiting for a corresponding ACK before |
| 487 | * they can be completed. |
| 488 | */ |
| 489 | uint64_t unacked_operation_size; |
| 490 | }; |
| 491 | |
| 492 | /** |
| 493 | * Details about a client lifecycle event. |
| 494 | */ |
| 495 | struct aws_mqtt5_client_lifecycle_event { |
| 496 | |
| 497 | /** |
| 498 | * Type of event this is. |
| 499 | */ |
| 500 | enum aws_mqtt5_client_lifecycle_event_type event_type; |
| 501 | |
| 502 | /** |
| 503 | * Client this event corresponds to. Necessary (can't be replaced with user data) because the client |
| 504 | * doesn't exist at the time the event callback user data is configured. |
| 505 | */ |
| 506 | struct aws_mqtt5_client *client; |
| 507 | |
| 508 | /** |
| 509 | * Aws-c-* error code associated with the event |
| 510 | */ |
| 511 | int error_code; |
| 512 | |
| 513 | /** |
| 514 | * User data associated with the client's lifecycle event handler. Set with client configuration. |
| 515 | */ |
| 516 | void *user_data; |
| 517 | |
| 518 | /** |
| 519 | * If this event was caused by receiving a CONNACK, this will be a view of that packet. |
| 520 | */ |
| 521 | const struct aws_mqtt5_packet_connack_view *connack_data; |
| 522 | |
| 523 | /** |
| 524 | * If this is a successful connection establishment, this will contain the negotiated mqtt5 behavioral settings |
| 525 | */ |
| 526 | const struct aws_mqtt5_negotiated_settings *settings; |
| 527 | |
| 528 | /** |
| 529 | * If this event was caused by receiving a DISCONNECT, this will be a view of that packet. |
| 530 | */ |
| 531 | const struct aws_mqtt5_packet_disconnect_view *disconnect_data; |
| 532 | }; |
| 533 | |
| 534 | /** |
| 535 | * Basic mqtt5 client configuration struct. |
| 536 | * |
| 537 | * Contains desired connection properties |
| 538 | * Configuration that represents properties of the mqtt5 CONNECT packet go in the connect view (connect_options) |
| 539 | */ |
| 540 | struct aws_mqtt5_client_options { |
| 541 | |
| 542 | /** |
| 543 | * Host to establish mqtt connections to |
| 544 | */ |
| 545 | struct aws_byte_cursor host_name; |
| 546 | |
| 547 | /** |
| 548 | * Port to establish mqtt connections to |
| 549 | */ |
| 550 | uint32_t port; |
| 551 | |
| 552 | /** |
| 553 | * Client bootstrap to use whenever this client establishes a connection |
| 554 | */ |
| 555 | struct aws_client_bootstrap *bootstrap; |
| 556 | |
| 557 | /** |
| 558 | * Socket options to use whenever this client establishes a connection |
| 559 | */ |
| 560 | const struct aws_socket_options *socket_options; |
| 561 | |
| 562 | /** |
| 563 | * (Optional) Tls options to use whenever this client establishes a connection |
| 564 | */ |
| 565 | const struct aws_tls_connection_options *tls_options; |
| 566 | |
| 567 | /** |
| 568 | * (Optional) Http proxy options to use whenever this client establishes a connection |
| 569 | */ |
| 570 | const struct aws_http_proxy_options *http_proxy_options; |
| 571 | |
| 572 | /** |
| 573 | * (Optional) Websocket handshake transformation function and user data. Websockets are used if the |
| 574 | * transformation function is non-null. |
| 575 | */ |
| 576 | aws_mqtt5_transform_websocket_handshake_fn *websocket_handshake_transform; |
| 577 | void *websocket_handshake_transform_user_data; |
| 578 | |
| 579 | /** |
| 580 | * All CONNECT-related options, includes the will configuration, if desired |
| 581 | */ |
| 582 | const struct aws_mqtt5_packet_connect_view *connect_options; |
| 583 | |
| 584 | /** |
| 585 | * Controls session rejoin behavior |
| 586 | */ |
| 587 | enum aws_mqtt5_client_session_behavior_type session_behavior; |
| 588 | |
| 589 | /** |
| 590 | * Controls if any additional AWS-specific validation or flow control should be performed by the client. |
| 591 | */ |
| 592 | enum aws_mqtt5_extended_validation_and_flow_control_options extended_validation_and_flow_control_options; |
| 593 | |
| 594 | /** |
| 595 | * Controls how the client treats queued/in-progress operations when the connection drops for any reason. |
| 596 | */ |
| 597 | enum aws_mqtt5_client_operation_queue_behavior_type offline_queue_behavior; |
| 598 | |
| 599 | /** |
| 600 | * Controls the exponential backoff behavior when the client is waiting to reconnect. |
| 601 | * |
| 602 | * See: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ |
| 603 | */ |
| 604 | enum aws_exponential_backoff_jitter_mode retry_jitter_mode; |
| 605 | |
| 606 | /** |
| 607 | * Minimum amount of time in ms to wait before attempting to reconnect. If this is zero, a default of 1000 ms will |
| 608 | * be used. |
| 609 | */ |
| 610 | uint64_t min_reconnect_delay_ms; |
| 611 | |
| 612 | /** |
| 613 | * Maximum amount of time in ms to wait before attempting to reconnect. If this is zero, a default of 120000 ms |
| 614 | * will be used. |
| 615 | */ |
| 616 | uint64_t max_reconnect_delay_ms; |
| 617 | |
| 618 | /** |
| 619 | * Amount of time that must elapse with a good connection before the reconnect delay is reset to the minimum. If |
| 620 | * this zero, a default of 30000 ms will be used. |
| 621 | */ |
| 622 | uint64_t min_connected_time_to_reset_reconnect_delay_ms; |
| 623 | |
| 624 | /** |
| 625 | * Time interval to wait after sending a PINGREQ for a PINGRESP to arrive. If one does not arrive, the connection |
| 626 | * will be shut down. If this is zero, a default of 30000 ms will be used. |
| 627 | */ |
| 628 | uint32_t ping_timeout_ms; |
| 629 | |
| 630 | /** |
| 631 | * Time interval to wait after sending a CONNECT request for a CONNACK to arrive. If one does not arrive, the |
| 632 | * connection will be shut down. If this zero, a default of 20000 ms will be used. |
| 633 | */ |
| 634 | uint32_t connack_timeout_ms; |
| 635 | |
| 636 | /** |
| 637 | * Time interval to wait for an ack after sending a SUBSCRIBE, UNSUBSCRIBE, or PUBLISH with QoS 1+ before |
| 638 | * failing the packet, notifying the client of failure, and removing it. If this is zero, a default of 60 seconds |
| 639 | * will be used. |
| 640 | */ |
| 641 | uint32_t ack_timeout_seconds; |
| 642 | |
| 643 | /** |
| 644 | * Controls how the client uses mqtt5 topic aliasing. If NULL, zero-based defaults will be used. |
| 645 | */ |
| 646 | const struct aws_mqtt5_client_topic_alias_options *topic_aliasing_options; |
| 647 | |
| 648 | /** |
| 649 | * Callback for received publish packets |
| 650 | */ |
| 651 | aws_mqtt5_publish_received_fn *publish_received_handler; |
| 652 | void *publish_received_handler_user_data; |
| 653 | |
| 654 | /** |
| 655 | * Callback and user data for all client lifecycle events. |
| 656 | * Life cycle events include: |
| 657 | * ConnectionSuccess |
| 658 | * ConnectionFailure, |
| 659 | * Disconnect |
| 660 | * (client) Stopped |
| 661 | * |
| 662 | * Disconnect lifecycle events are 1-1 with -- strictly after -- ConnectionSuccess events. |
| 663 | */ |
| 664 | aws_mqtt5_client_connection_event_callback_fn *lifecycle_event_handler; |
| 665 | void *lifecycle_event_handler_user_data; |
| 666 | |
| 667 | /** |
| 668 | * Callback for when the client has completely destroyed itself |
| 669 | */ |
| 670 | aws_mqtt5_client_termination_completion_fn *client_termination_handler; |
| 671 | void *client_termination_handler_user_data; |
| 672 | |
| 673 | /** |
| 674 | * Options to override aspects of DNS resolution. If unspecified, use a default that matches the regular |
| 675 | * configuration but changes the refresh frequency to a value that prevents DNS pinging. |
| 676 | */ |
| 677 | struct aws_host_resolution_config *host_resolution_override; |
| 678 | |
| 679 | /** |
| 680 | * IoT SDK metrics configuration |
| 681 | */ |
| 682 | const struct aws_mqtt_iot_metrics *metrics; |
| 683 | }; |
| 684 | |
| 685 | AWS_EXTERN_C_BEGIN |
| 686 | |
| 687 | /** |
| 688 | * Creates a new mqtt5 client using the supplied configuration |
| 689 | * |
| 690 | * @param allocator allocator to use with all memory operations related to this client's creation and operation |
| 691 | * @param options mqtt5 client configuration |
| 692 | * @return a new mqtt5 client or NULL |
| 693 | */ |
| 694 | AWS_MQTT_API |
| 695 | struct aws_mqtt5_client *aws_mqtt5_client_new( |
| 696 | struct aws_allocator *allocator, |
| 697 | const struct aws_mqtt5_client_options *options); |
| 698 | |
| 699 | /** |
| 700 | * Acquires a reference to an mqtt5 client |
| 701 | * |
| 702 | * @param client client to acquire a reference to. May be NULL. |
| 703 | * @return what was passed in as the client (a client or NULL) |
| 704 | */ |
| 705 | AWS_MQTT_API |
| 706 | struct aws_mqtt5_client *aws_mqtt5_client_acquire(struct aws_mqtt5_client *client); |
| 707 | |
| 708 | /** |
| 709 | * Release a reference to an mqtt5 client. When the client ref count drops to zero, the client will automatically |
| 710 | * trigger a stop and once the stop completes, the client will delete itself. |
| 711 | * |
| 712 | * @param client client to release a reference to. May be NULL. |
| 713 | * @return NULL |
| 714 | */ |
| 715 | AWS_MQTT_API |
| 716 | struct aws_mqtt5_client *aws_mqtt5_client_release(struct aws_mqtt5_client *client); |
| 717 | |
| 718 | /** |
| 719 | * Asynchronous notify to the mqtt5 client that you want it to attempt to connect to the configured endpoint. |
| 720 | * The client will attempt to stay connected using the properties of the reconnect-related parameters |
| 721 | * in the mqtt5 client configuration. |
| 722 | * |
| 723 | * @param client mqtt5 client to start |
| 724 | * @return success/failure in the synchronous logic that kicks off the start process |
| 725 | */ |
| 726 | AWS_MQTT_API |
| 727 | int aws_mqtt5_client_start(struct aws_mqtt5_client *client); |
| 728 | |
| 729 | /** |
| 730 | * Asynchronous notify to the mqtt5 client that you want it to transition to the stopped state. When the client |
| 731 | * reaches the stopped state, all session state is erased. |
| 732 | * |
| 733 | * @param client mqtt5 client to stop |
| 734 | * @param disconnect_options (optional) properties of a DISCONNECT packet to send as part of the shutdown process |
| 735 | * @return success/failure in the synchronous logic that kicks off the stop process |
| 736 | */ |
| 737 | AWS_MQTT_API |
| 738 | int aws_mqtt5_client_stop( |
| 739 | struct aws_mqtt5_client *client, |
| 740 | const struct aws_mqtt5_packet_disconnect_view *disconnect_options, |
| 741 | const struct aws_mqtt5_disconnect_completion_options *completion_options); |
| 742 | |
| 743 | /** |
| 744 | * Queues a Publish operation in an mqtt5 client |
| 745 | * |
| 746 | * @param client mqtt5 client to queue a Publish for |
| 747 | * @param publish_options configuration options for the Publish operation |
| 748 | * @param completion_options completion callback configuration. Successful QoS 0 publishes invoke the callback when |
| 749 | * the data has been written to the socket. Successful QoS1+ publishes invoke the callback when the corresponding ack |
| 750 | * is received. Unsuccessful publishes invoke the callback at the point in time a failure condition is reached. |
| 751 | * @return success/failure in the synchronous logic that kicks off the publish operation |
| 752 | */ |
| 753 | AWS_MQTT_API |
| 754 | int aws_mqtt5_client_publish( |
| 755 | struct aws_mqtt5_client *client, |
| 756 | const struct aws_mqtt5_packet_publish_view *publish_options, |
| 757 | const struct aws_mqtt5_publish_completion_options *completion_options); |
| 758 | |
| 759 | /** |
| 760 | * Takes manual control of publish acknowledgement for the given PUBLISH packet. |
| 761 | * |
| 762 | * This MUST only be called from within the publish received callback. A return value of 0 indicates an |
| 763 | * invalid control id. |
| 764 | * |
| 765 | * @param client mqtt5 client that received the PUBLISH packet to take manual publish acknowledgement control from. |
| 766 | * @param publish_view the view of the PUBLISH packet that publish acknowledgement control is taken from. |
| 767 | * @return pub_ack_control_id of the PUBLISH packet. This can be used to schedule a publish acknowledgement for the |
| 768 | * PUBLISH packet when used with the aws_mqtt5_client_invoke_publish_acknowledgement function call. |
| 769 | */ |
| 770 | AWS_MQTT_API uint64_t aws_mqtt5_client_acquire_publish_acknowledgement( |
| 771 | struct aws_mqtt5_client *client, |
| 772 | const struct aws_mqtt5_packet_publish_view *publish_view); |
| 773 | |
| 774 | /** |
| 775 | * Send publish acknowledgement for provided control id. Callback in completion_options will be invoked with an |
| 776 | * aws_mqtt5_manual_publish_acknowledgement_result once a publish acknowledgement operation has been completed. |
| 777 | * |
| 778 | * @param client mqtt5 client to queue a puback for |
| 779 | * @param pub_ack_control_id Control ID of aws_mqtt5_manual_pub_ack_entry to send to broker/server |
| 780 | * @return success/failure of starting the manual publish acknowledgement operation. |
| 781 | */ |
| 782 | AWS_MQTT_API |
| 783 | int aws_mqtt5_client_invoke_publish_acknowledgement( |
| 784 | struct aws_mqtt5_client *client, |
| 785 | uint64_t pub_ack_control_id, |
| 786 | const struct aws_mqtt5_manual_publish_acknowledgement_completion_options *completion_options); |
| 787 | |
| 788 | /** |
| 789 | * Queues a Subscribe operation in an mqtt5 client |
| 790 | * |
| 791 | * @param client mqtt5 client to queue a Subscribe for |
| 792 | * @param subscribe_options configuration options for the Subscribe operation |
| 793 | * @param completion_options Completion callback configuration. Invoked when the corresponding SUBACK is received or |
| 794 | * a failure condition is reached. An error code implies complete failure of the subscribe, while a success code |
| 795 | * implies the user must still check all of the SUBACK's reason codes for per-subscription feedback. |
| 796 | * @return success/failure in the synchronous logic that kicks off the Subscribe operation |
| 797 | */ |
| 798 | AWS_MQTT_API |
| 799 | int aws_mqtt5_client_subscribe( |
| 800 | struct aws_mqtt5_client *client, |
| 801 | const struct aws_mqtt5_packet_subscribe_view *subscribe_options, |
| 802 | const struct aws_mqtt5_subscribe_completion_options *completion_options); |
| 803 | |
| 804 | /** |
| 805 | * Queues an Unsubscribe operation in an mqtt5 client |
| 806 | * |
| 807 | * @param client mqtt5 client to queue an Unsubscribe for |
| 808 | * @param unsubscribe_options configuration options for the Unsubscribe operation |
| 809 | * @param completion_options Completion callback configuration. Invoked when the corresponding UNSUBACK is received or |
| 810 | * a failure condition is reached. An error code implies complete failure of the unsubscribe, while a success code |
| 811 | * implies the user must still check all of the UNSUBACK's reason codes for per-topic-filter feedback. |
| 812 | * @return success/failure in the synchronous logic that kicks off the Unsubscribe operation |
| 813 | */ |
| 814 | AWS_MQTT_API |
| 815 | int aws_mqtt5_client_unsubscribe( |
| 816 | struct aws_mqtt5_client *client, |
| 817 | const struct aws_mqtt5_packet_unsubscribe_view *unsubscribe_options, |
| 818 | const struct aws_mqtt5_unsubscribe_completion_options *completion_options); |
| 819 | |
| 820 | /** |
| 821 | * Queries the client's internal statistics for incomplete operations. |
| 822 | * @param client client to get statistics for |
| 823 | * @param stats set of incomplete operation statistics |
| 824 | */ |
| 825 | AWS_MQTT_API |
| 826 | void aws_mqtt5_client_get_stats(struct aws_mqtt5_client *client, struct aws_mqtt5_client_operation_statistics *stats); |
| 827 | |
| 828 | /* Misc related type APIs */ |
| 829 | |
| 830 | /** |
| 831 | * Initializes the Client ID byte buf in negotiated settings |
| 832 | * |
| 833 | * @param allocator allocator to use for memory allocation |
| 834 | * @param negotiated_settings settings to apply client id to |
| 835 | * @param client_id client id to set |
| 836 | */ |
| 837 | AWS_MQTT_API int aws_mqtt5_negotiated_settings_init( |
| 838 | struct aws_allocator *allocator, |
| 839 | struct aws_mqtt5_negotiated_settings *negotiated_settings, |
| 840 | const struct aws_byte_cursor *client_id); |
| 841 | |
| 842 | /** |
| 843 | * Makes an owning copy of a negotiated settings structure. |
| 844 | * |
| 845 | * @param source settings to copy from |
| 846 | * @param dest settings to copy into. Must be in a zeroed or initialized state because it gets clean up |
| 847 | * called on it as the first step of the copy process. |
| 848 | * @return success/failure |
| 849 | * |
| 850 | * Used in downstream. |
| 851 | */ |
| 852 | AWS_MQTT_API int aws_mqtt5_negotiated_settings_copy( |
| 853 | const struct aws_mqtt5_negotiated_settings *source, |
| 854 | struct aws_mqtt5_negotiated_settings *dest); |
| 855 | |
| 856 | /** |
| 857 | * Clean up owned memory in negotiated_settings |
| 858 | * |
| 859 | * @param negotiated_settings settings to clean up |
| 860 | */ |
| 861 | AWS_MQTT_API void aws_mqtt5_negotiated_settings_clean_up(struct aws_mqtt5_negotiated_settings *negotiated_settings); |
| 862 | |
| 863 | AWS_EXTERN_C_END |
| 864 | AWS_POP_SANE_WARNING_LEVEL |
| 865 | |
| 866 | #endif /* AWS_MQTT_MQTT5_CLIENT_H */ |