| 1 | ## AWS C MQTT |
| 2 | |
| 3 | C99 implementation of the MQTT 3.1.1 and MQTT 5 specifications. |
| 4 | |
| 5 | ## License |
| 6 | |
| 7 | This library is licensed under the Apache 2.0 License. |
| 8 | |
| 9 | ## Usage |
| 10 | |
| 11 | ### Building |
| 12 | |
| 13 | CMake 3.9+ is required to build. |
| 14 | |
| 15 | `<install-path>` must be an absolute path in the following instructions. |
| 16 | |
| 17 | #### Linux-Only Dependencies |
| 18 | |
| 19 | If you are building on Linux, you will need to build aws-lc and s2n-tls first. |
| 20 | |
| 21 | ``` |
| 22 | git clone [email protected]:awslabs/aws-lc.git |
| 23 | cmake -S aws-lc -B aws-lc/build -DCMAKE_INSTALL_PREFIX=<install-path> |
| 24 | cmake --build aws-lc/build --target install |
| 25 | |
| 26 | git clone [email protected]:aws/s2n-tls.git |
| 27 | cmake -S s2n-tls -B s2n-tls/build -DCMAKE_INSTALL_PREFIX=<install-path> -DCMAKE_PREFIX_PATH=<install-path> |
| 28 | cmake --build s2n-tls/build --target install |
| 29 | ``` |
| 30 | |
| 31 | #### Building aws-c-mqtt and Remaining Dependencies |
| 32 | |
| 33 | ``` |
| 34 | git clone [email protected]:awslabs/aws-c-common.git |
| 35 | cmake -S aws-c-common -B aws-c-common/build -DCMAKE_INSTALL_PREFIX=<install-path> |
| 36 | cmake --build aws-c-common/build --target install |
| 37 | |
| 38 | git clone [email protected]:awslabs/aws-c-cal.git |
| 39 | cmake -S aws-c-cal -B aws-c-cal/build -DCMAKE_INSTALL_PREFIX=<install-path> -DCMAKE_PREFIX_PATH=<install-path> |
| 40 | cmake --build aws-c-cal/build --target install |
| 41 | |
| 42 | git clone [email protected]:awslabs/aws-c-io.git |
| 43 | cmake -S aws-c-io -B aws-c-io/build -DCMAKE_INSTALL_PREFIX=<install-path> -DCMAKE_PREFIX_PATH=<install-path> |
| 44 | cmake --build aws-c-io/build --target install |
| 45 | |
| 46 | git clone [email protected]:awslabs/aws-c-compression.git |
| 47 | cmake -S aws-c-compression -B aws-c-compression/build -DCMAKE_INSTALL_PREFIX=<install-path> -DCMAKE_PREFIX_PATH=<install-path> |
| 48 | cmake --build aws-c-compression/build --target install |
| 49 | |
| 50 | git clone [email protected]:awslabs/aws-c-http.git |
| 51 | cmake -S aws-c-http -B aws-c-http/build -DCMAKE_INSTALL_PREFIX=<install-path> -DCMAKE_PREFIX_PATH=<install-path> |
| 52 | cmake --build aws-c-http/build --target install |
| 53 | |
| 54 | git clone [email protected]:awslabs/aws-c-mqtt.git |
| 55 | cmake -S aws-c-mqtt -B aws-c-mqtt/build -DCMAKE_INSTALL_PREFIX=<install-path> -DCMAKE_PREFIX_PATH=<install-path> |
| 56 | cmake --build aws-c-mqtt/build --target install |
| 57 | ``` |
| 58 | |
| 59 | ### Overview |
| 60 | |
| 61 | This library contains an MQTT implementation that is simple and easy to use, but also quite powerful and low on |
| 62 | unnecessary copies. Here is a general overview of the API: |
| 63 | |
| 64 | ### `struct aws_mqtt_client;` |
| 65 | |
| 66 | `aws_mqtt_client` is meant to be created once per application to pool common resources required for opening MQTT |
| 67 | connections. The instance does not need to be allocated, and may be managed by the user. |
| 68 | |
| 69 | ```c |
| 70 | int aws_mqtt_client_init( |
| 71 | struct aws_mqtt_client *client, |
| 72 | struct aws_allocator *allocator, |
| 73 | struct aws_event_loop_group *elg); |
| 74 | ``` |
| 75 | Initializes an instance of `aws_mqtt_client` with the required parameters. |
| 76 | * `client` is effectively the `this` parameter. |
| 77 | * `allocator` will be used to initialize the client (note that the client itself is NOT allocated). |
| 78 | *This resource must outlive `client`*. |
| 79 | * `bootstrap` will be used to initiate new socket connections MQTT. |
| 80 | *This resource must outlive `client`*. |
| 81 | See [aws-c-io][aws-c-io] for more information about `aws_client_bootstrap`. |
| 82 | |
| 83 | ```c |
| 84 | void aws_mqtt_client_clean_up(struct aws_mqtt_client *client); |
| 85 | ``` |
| 86 | Cleans up a client and frees all owned resources. |
| 87 | |
| 88 | **NOTE**: DO NOT CALL THIS FUNCTION UNTIL ALL OUTSTANDING CONNECTIONS ARE CLOSED. |
| 89 | |
| 90 | ### `struct aws_mqtt_client_connection;` |
| 91 | |
| 92 | ```c |
| 93 | struct aws_mqtt_client_connection *aws_mqtt_client_connection_new( |
| 94 | struct aws_mqtt_client *client, |
| 95 | struct aws_mqtt_client_connection_callbacks callbacks, |
| 96 | const struct aws_byte_cursor *host_name, |
| 97 | uint16_t port, |
| 98 | struct aws_socket_options *socket_options, |
| 99 | struct aws_tls_ctx_options *tls_options); |
| 100 | ``` |
| 101 | Allocates and initializes a new connection object (does NOT actually connect). You may use the returned object to |
| 102 | configure connection parameters, and then call `aws_mqtt_client_connection_connect` to actually open the connection. |
| 103 | * `client` is required in order to use an existing DNS resolver, event loop group, and allocator. |
| 104 | * `callbacks` provides the connection-level (not operation level) callbacks and the userdata to be given back. |
| 105 | * `host_name` lists the end point to connect to. This may be a DNS address or an IP address. |
| 106 | *This resource may be freed immediately after return.* |
| 107 | * `port` the port to connect to on `host_name`. |
| 108 | * `socket_options` describes how to open the connection. |
| 109 | See [aws-c-io][aws-c-io] for more information about `aws_socket_options`. |
| 110 | * `tls_options` provides TLS credentials to connect with. Pass `NULL` to not use TLS (**NOT RECOMMENDED**). |
| 111 | See [aws-c-io][aws-c-io] for more information about `aws_tls_ctx_options`. |
| 112 | |
| 113 | ```c |
| 114 | void aws_mqtt_client_connection_destroy(struct aws_mqtt_client_connection *connection); |
| 115 | ``` |
| 116 | Destroys a connection and frees all outstanding resources. |
| 117 | |
| 118 | **NOTE**: DO NOT CALL THIS FUNCTION UNTIL THE CONNECTION IS CLOSED. |
| 119 | |
| 120 | ```c |
| 121 | int aws_mqtt_client_connection_set_will( |
| 122 | struct aws_mqtt_client_connection *connection, |
| 123 | const struct aws_byte_cursor *topic, |
| 124 | enum aws_mqtt_qos qos, |
| 125 | bool retain, |
| 126 | const struct aws_byte_cursor *payload); |
| 127 | ``` |
| 128 | Sets the last will and testament to be distributed by the server upon client disconnection. Must be called before |
| 129 | `aws_mqtt_client_connection_connect`. See `aws_mqtt_client_connection_publish` for information on the parameters. |
| 130 | `topic` and `payload` must persist past the call to `aws_mqtt_client_connection_connect`. |
| 131 | |
| 132 | ```c |
| 133 | int aws_mqtt_client_connection_set_login( |
| 134 | struct aws_mqtt_client_connection *connection, |
| 135 | const struct aws_byte_cursor *username, |
| 136 | const struct aws_byte_cursor *password); |
| 137 | ``` |
| 138 | Sets the username and password to be sent to the server on connection. Must be called before |
| 139 | `aws_mqtt_client_connection_connect`. `username` and `password` must persist past the call to |
| 140 | `aws_mqtt_client_connection_connect`. |
| 141 | |
| 142 | ```c |
| 143 | int aws_mqtt_client_connection_set_reconnect_timeout( |
| 144 | struct aws_mqtt_client_connection *connection, |
| 145 | uint64_t min_timeout, |
| 146 | uint64_t max_timeout); |
| 147 | ``` |
| 148 | Sets the minimum and maximum reconnect timeouts. The time between reconnect attempts will start at min and multipy by 2 |
| 149 | until max is reached. |
| 150 | |
| 151 | ```c |
| 152 | int aws_mqtt_client_connection_connect( |
| 153 | struct aws_mqtt_client_connection *connection, |
| 154 | const struct aws_byte_cursor *client_id, |
| 155 | bool clean_session, |
| 156 | uint16_t keep_alive_time); |
| 157 | ``` |
| 158 | Connects to the remote endpoint. The parameters here are set in the MQTT CONNECT packet directly. `client_id` must persist until the `on_connack` connection callback is called. |
| 159 | |
| 160 | ```c |
| 161 | int aws_mqtt_client_connection_disconnect(struct aws_mqtt_client_connection *connection); |
| 162 | ``` |
| 163 | Closes an open connection. Does not clean up any resources, that's to be done by `aws_mqtt_client_connection_destroy`, |
| 164 | probably from the `on_disconnected` connection callback. |
| 165 | |
| 166 | ```c |
| 167 | uint16_t aws_mqtt_client_connection_subscribe_single( |
| 168 | struct aws_mqtt_client_connection *connection, |
| 169 | const struct aws_byte_cursor *topic_filter, |
| 170 | enum aws_mqtt_qos qos, |
| 171 | aws_mqtt_client_publish_received_fn *on_publish, |
| 172 | void *on_publish_ud, |
| 173 | aws_mqtt_suback_single_fn *on_suback, |
| 174 | void *on_suback_ud); |
| 175 | ``` |
| 176 | Subscribes to the topic filter given with the given QoS. `on_publish` will be called whenever a packet matching |
| 177 | `topic_filter` arrives. `on_suback` will be called when the SUBACK packet has been received. `topic_filter` must persist until `on_suback` is called. The packet_id of the SUBSCRIBE packet will be returned, or 0 on error. |
| 178 | |
| 179 | ```c |
| 180 | uint16_t aws_mqtt_client_connection_unsubscribe( |
| 181 | struct aws_mqtt_client_connection *connection, |
| 182 | const struct aws_byte_cursor *topic_filter, |
| 183 | aws_mqtt_op_complete_fn *on_unsuback, |
| 184 | void *on_unsuback_ud); |
| 185 | ``` |
| 186 | Unsubscribes from the topic filter given. `topic_filter` must persist until `on_unsuback` is called. The packet_id of |
| 187 | the UNSUBSCRIBE packet will be returned, or 0 on error. |
| 188 | |
| 189 | ```c |
| 190 | uint16_t aws_mqtt_client_connection_publish( |
| 191 | struct aws_mqtt_client_connection *connection, |
| 192 | const struct aws_byte_cursor *topic, |
| 193 | enum aws_mqtt_qos qos, |
| 194 | bool retain, |
| 195 | const struct aws_byte_cursor *payload, |
| 196 | aws_mqtt_op_complete_fn *on_complete, |
| 197 | void *userdata); |
| 198 | ``` |
| 199 | Publish a payload to the topic specified. For QoS 0, `on_complete` will be called as soon as the packet is sent over |
| 200 | the wire. For QoS 1, as soon as PUBACK comes back. For QoS 2, PUBCOMP. `topic` and `payload` must persist until |
| 201 | `on_complete`. |
| 202 | |
| 203 | ```c |
| 204 | int aws_mqtt_client_connection_ping(struct aws_mqtt_client_connection *connection); |
| 205 | ``` |
| 206 | Sends a PINGREQ packet to the server. |
| 207 | |
| 208 | [aws-c-io]: https://github.com/awslabs/aws-c-io |