Debian Codegraph @

crt/aws-c-mqtt/include/aws/mqtt/request-response/request_response_client.h

raw symbols SCIP
1 #ifndef AWS_MQTT_REQUEST_RESPONSE_REQUEST_RESPONSE_CLIENT_H
2 #define AWS_MQTT_REQUEST_RESPONSE_REQUEST_RESPONSE_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 struct aws_event_loop;
12 struct aws_mqtt_request_response_client;
13 struct aws_mqtt_client_connection;
14 struct aws_mqtt5_client;
15
16 /*
17 * A response path is a pair of values - MQTT topic and a JSON path - that describe where a response to
18 * an MQTT-based request may arrive. For a given request type, there may be multiple response paths and each
19 * one is associated with a separate JSON schema for the response body.
20 */
21 struct aws_mqtt_request_operation_response_path {
22
23 /*
24 * MQTT topic that a response may arrive on.
25 */
26 struct aws_byte_cursor topic;
27
28 /*
29 * JSON path for finding correlation tokens within payloads that arrive on this path's topic.
30 */
31 struct aws_byte_cursor correlation_token_json_path;
32 };
33
34 /*
35 * An event emitted by a streaming operation's subscription.
36 */
37 struct aws_mqtt_rr_incoming_publish_event {
38 struct aws_byte_cursor payload;
39 struct aws_byte_cursor topic;
40 /* Below are MQTT optional fields. For MQTT3, they will always be empty, as MQTT3 does not support them. For MQTT5,
41 * they will be set if they are present in a packet. */
42 const struct aws_byte_cursor *content_type;
43 size_t user_property_count;
44 const struct aws_mqtt5_user_property *user_properties;
45 /* Even though this field is supposed to be used by MQTT broker to determine if a message-to-be-sent is expired,
46 * certain services use this field to specify client-side timeouts. */
47 const uint32_t *message_expiry_interval_seconds;
48 };
49
50 /*
51 * Callback signature for request-response completion.
52 *
53 * Invariants:
54 * If error_code is non-zero then publish_event will be NULL.
55 * If publish_event is not NULL, then error_code will be 0.
56 */
57 typedef void(aws_mqtt_request_operation_completion_fn)(
58 const struct aws_mqtt_rr_incoming_publish_event *publish_event,
59 int error_code,
60 void *user_data);
61
62 /*
63 * Configuration options for a request-response operation.
64 */
65 struct aws_mqtt_request_operation_options {
66
67 /*
68 * Set of topic filters that should be subscribed to in order to cover all possible response paths. Sometimes
69 * we can use wildcards to cut down on the subscriptions needed; sometimes we can't.
70 */
71 struct aws_byte_cursor *subscription_topic_filters;
72 size_t subscription_topic_filter_count;
73
74 /*
75 * Set of all possible response paths associated with this request type
76 */
77 struct aws_mqtt_request_operation_response_path *response_paths;
78 size_t response_path_count;
79
80 /*
81 * Topic to publish the request to once response subscriptions have been established.
82 */
83 struct aws_byte_cursor publish_topic;
84
85 /*
86 * Payload to publish in order to initiate the request
87 */
88 struct aws_byte_cursor serialized_request;
89
90 /*
91 * Correlation token embedded in the request that must be found in a response message. This can be the
92 * empty cursor to support certain services which don't use correlation tokens. In this case, the client
93 * only allows one request at a time to use the associated subscriptions; no concurrency is possible. There
94 * are some optimizations we could make here but for now, they're not worth the complexity.
95 */
96 struct aws_byte_cursor correlation_token;
97
98 /*
99 * Callback (and associated user data) to invoke when the request is completed.
100 */
101 aws_mqtt_request_operation_completion_fn *completion_callback;
102 void *user_data;
103 };
104
105 /*
106 * Describes a change to the state of a streaming operation subscription
107 */
108 enum aws_rr_streaming_subscription_event_type {
109
110 /*
111 * The streaming operation is successfully subscribed to its topic (filter)
112 */
113 ARRSSET_SUBSCRIPTION_ESTABLISHED,
114
115 /*
116 * The streaming operation has temporarily lost its subscription to its topic (filter)
117 */
118 ARRSSET_SUBSCRIPTION_LOST,
119
120 /*
121 * The streaming operation has entered a terminal state where it has given up trying to subscribe
122 * to its topic (filter). This is always due to user error (bad topic filter or IoT Core permission policy).
123 */
124 ARRSSET_SUBSCRIPTION_HALTED,
125 };
126
127 /*
128 * Callback signature for when the subscription status of a streaming operation changes.
129 */
130 typedef void(aws_mqtt_streaming_operation_subscription_status_fn)(
131 enum aws_rr_streaming_subscription_event_type status,
132 int error_code,
133 void *user_data);
134
135 /*
136 * Callback signature for when a publish arrives that matches a streaming operation's subscription
137 */
138 typedef void(aws_mqtt_streaming_operation_incoming_publish_fn)(
139 const struct aws_mqtt_rr_incoming_publish_event *publish_event,
140 void *user_data);
141
142 /*
143 * Callback signature for when a streaming operation is fully destroyed and no more events will be emitted.
144 */
145 typedef void(aws_mqtt_streaming_operation_terminated_fn)(void *user_data);
146
147 /*
148 * Configuration options for a streaming operation.
149 */
150 struct aws_mqtt_streaming_operation_options {
151
152 /*
153 * Topic filter that the streaming operation should listen on
154 */
155 struct aws_byte_cursor topic_filter;
156
157 /*
158 * Callback for subscription status events
159 */
160 aws_mqtt_streaming_operation_subscription_status_fn *subscription_status_callback;
161
162 /*
163 * Callback for publish messages that match the operation's topic filter
164 */
165 aws_mqtt_streaming_operation_incoming_publish_fn *incoming_publish_callback;
166
167 /*
168 * Callback for streaming operation final shutdown
169 */
170 aws_mqtt_streaming_operation_terminated_fn *terminated_callback;
171
172 /*
173 * Data passed to all streaming operation callbacks
174 */
175 void *user_data;
176 };
177
178 typedef void(aws_mqtt_request_response_client_initialized_callback_fn)(void *user_data);
179 typedef void(aws_mqtt_request_response_client_terminated_callback_fn)(void *user_data);
180
181 /*
182 * Request-response client configuration options
183 */
184 struct aws_mqtt_request_response_client_options {
185
186 /*
187 * Maximum number of subscriptions that the client will concurrently use for request-response operations
188 */
189 size_t max_request_response_subscriptions;
190
191 /*
192 * Maximum number of subscriptions that the client will concurrently use for streaming operations
193 */
194 size_t max_streaming_subscriptions;
195
196 /*
197 * Duration, in seconds, that a request-response operation will wait for completion before giving up
198 */
199 uint32_t operation_timeout_seconds;
200
201 /*
202 * Request-response client initialization is asynchronous. This callback is invoked when the client is fully
203 * initialized.
204 *
205 * Do not bind the initialized callback; it exists mostly for tests and should not be exposed
206 */
207 aws_mqtt_request_response_client_initialized_callback_fn *initialized_callback;
208
209 /*
210 * Callback invoked when the client's asynchronous destruction process has fully completed.
211 */
212 aws_mqtt_request_response_client_terminated_callback_fn *terminated_callback;
213
214 /*
215 * Arbitrary data to pass to the client callbacks
216 */
217 void *user_data;
218 };
219
220 AWS_EXTERN_C_BEGIN
221
222 /*
223 * Create a new request-response client that uses an MQTT311 client.
224 */
225 AWS_MQTT_API struct aws_mqtt_request_response_client *aws_mqtt_request_response_client_new_from_mqtt311_client(
226 struct aws_allocator *allocator,
227 struct aws_mqtt_client_connection *client,
228 const struct aws_mqtt_request_response_client_options *options);
229
230 /*
231 * Create a new request-response client that uses an MQTT5 client.
232 */
233 AWS_MQTT_API struct aws_mqtt_request_response_client *aws_mqtt_request_response_client_new_from_mqtt5_client(
234 struct aws_allocator *allocator,
235 struct aws_mqtt5_client *client,
236 const struct aws_mqtt_request_response_client_options *options);
237
238 /*
239 * Add a reference to a request-response client
240 */
241 AWS_MQTT_API struct aws_mqtt_request_response_client *aws_mqtt_request_response_client_acquire(
242 struct aws_mqtt_request_response_client *client);
243
244 /*
245 * Remove a reference from a request-response client
246 */
247 AWS_MQTT_API struct aws_mqtt_request_response_client *aws_mqtt_request_response_client_release(
248 struct aws_mqtt_request_response_client *client);
249
250 /*
251 * Submits a request operation to the client
252 */
253 AWS_MQTT_API int aws_mqtt_request_response_client_submit_request(
254 struct aws_mqtt_request_response_client *client,
255 const struct aws_mqtt_request_operation_options *request_options);
256
257 /*
258 * Creates a new streaming operation. Streaming operations start in an inactive state and must be
259 * activated before its subscription can be established.
260 */
261 AWS_MQTT_API struct aws_mqtt_rr_client_operation *aws_mqtt_request_response_client_create_streaming_operation(
262 struct aws_mqtt_request_response_client *client,
263 const struct aws_mqtt_streaming_operation_options *streaming_options);
264
265 /*
266 * Returns the event loop used by the request-response client's protocol client
267 */
268 AWS_MQTT_API struct aws_event_loop *aws_mqtt_request_response_client_get_event_loop(
269 struct aws_mqtt_request_response_client *client);
270
271 /*
272 * Initiates a streaming operation's subscription process.
273 */
274 AWS_MQTT_API int aws_mqtt_rr_client_operation_activate(struct aws_mqtt_rr_client_operation *operation);
275
276 /*
277 * Add a reference to a streaming operation.
278 */
279 AWS_MQTT_API struct aws_mqtt_rr_client_operation *aws_mqtt_rr_client_operation_acquire(
280 struct aws_mqtt_rr_client_operation *operation);
281
282 /*
283 * Remove a reference from a streaming operation
284 */
285 AWS_MQTT_API struct aws_mqtt_rr_client_operation *aws_mqtt_rr_client_operation_release(
286 struct aws_mqtt_rr_client_operation *operation);
287
288 AWS_EXTERN_C_END
289
290 #endif /* AWS_MQTT_REQUEST_RESPONSE_REQUEST_RESPONSE_CLIENT_H */