Debian Codegraph @

crt/aws-c-mqtt/include/aws/mqtt/private/v5/mqtt5_to_mqtt3_adapter_impl.h

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #ifndef AWS_MQTT_MQTT5_TO_MQTT3_ADAPTER_IMPL_H
7 #define AWS_MQTT_MQTT5_TO_MQTT3_ADAPTER_IMPL_H
8
9 #include <aws/mqtt/mqtt.h>
10
11 #include <aws/common/mutex.h>
12 #include <aws/common/rw_lock.h>
13 #include <aws/common/task_scheduler.h>
14 #include <aws/mqtt/client.h>
15 #include <aws/mqtt/private/client_impl_shared.h>
16 #include <aws/mqtt/v5/mqtt5_client.h>
17
18 struct aws_mqtt_subscription_set;
19
20 struct aws_mqtt5_to_mqtt3_adapter_publish_options {
21 struct aws_mqtt_client_connection_5_impl *adapter;
22
23 const struct aws_byte_cursor topic;
24 enum aws_mqtt_qos qos;
25 bool retain;
26 const struct aws_byte_cursor payload;
27
28 aws_mqtt_op_complete_fn *on_complete;
29 void *on_complete_userdata;
30 };
31
32 /*
33 * A subscribe with no subscriptions represents a re-subscribe of all internally tracked topics. While this
34 * is a bit hacky, the alternative is to copy-and-paste almost the entire multi-subscribe adapter operation and
35 * supporting logic, which is approximately 300 lines.
36 */
37 struct aws_mqtt5_to_mqtt3_adapter_subscribe_options {
38 struct aws_mqtt_client_connection_5_impl *adapter;
39
40 struct aws_mqtt_topic_subscription *subscriptions;
41 size_t subscription_count;
42
43 aws_mqtt_suback_fn *on_suback;
44 void *on_suback_user_data;
45
46 aws_mqtt_suback_multi_fn *on_multi_suback;
47 void *on_multi_suback_user_data;
48 };
49
50 struct aws_mqtt5_to_mqtt3_adapter_unsubscribe_options {
51 struct aws_mqtt_client_connection_5_impl *adapter;
52
53 struct aws_byte_cursor topic_filter;
54
55 aws_mqtt_op_complete_fn *on_unsuback;
56 void *on_unsuback_user_data;
57 };
58
59 enum aws_mqtt5_to_mqtt3_adapter_operation_type {
60 AWS_MQTT5TO3_AOT_PUBLISH,
61 AWS_MQTT5TO3_AOT_SUBSCRIBE,
62 AWS_MQTT5TO3_AOT_UNSUBSCRIBE,
63 };
64
65 struct aws_mqtt5_to_mqtt3_adapter_operation_vtable {
66 void (*fail_fn)(void *impl, int error_code);
67 };
68
69 struct aws_mqtt5_to_mqtt3_adapter_operation_base {
70 struct aws_allocator *allocator;
71 struct aws_ref_count ref_count;
72 const struct aws_mqtt5_to_mqtt3_adapter_operation_vtable *vtable;
73
74 void *impl;
75
76 /*
77 * Holds an internal reference to the adapter while traveling to the event loop. Reference gets released
78 * after intake on the event loop.
79 *
80 * We avoid calling back into a deleted adapter by zeroing out the
81 * mqtt5 operation callbacks for everything we've submitted before final mqtt5 client release.
82 */
83 struct aws_mqtt_client_connection_5_impl *adapter;
84 bool holding_adapter_ref;
85
86 struct aws_task submission_task;
87
88 enum aws_mqtt5_to_mqtt3_adapter_operation_type type;
89 uint16_t id;
90 };
91
92 struct aws_mqtt5_to_mqtt3_adapter_operation_publish {
93 struct aws_mqtt5_to_mqtt3_adapter_operation_base base;
94
95 /*
96 * holds a reference to the MQTT5 client publish operation until the operation completes or our adapter
97 * goes away.
98 *
99 * In the case where we're going away, we zero out the MQTT5 operation callbacks to prevent crash-triggering
100 * notifications.
101 */
102 struct aws_mqtt5_operation_publish *publish_op;
103
104 aws_mqtt_op_complete_fn *on_publish_complete;
105 void *on_publish_complete_user_data;
106 };
107
108 struct aws_mqtt5_to_mqtt3_adapter_operation_subscribe {
109 struct aws_mqtt5_to_mqtt3_adapter_operation_base base;
110
111 /*
112 * holds a reference to the MQTT5 client subscribe operation until the operation completes or our adapter
113 * goes away.
114 *
115 * In the case where we're going away, we zero out the MQTT5 operation callbacks to prevent crash-triggering
116 * notifications.
117 */
118 struct aws_mqtt5_operation_subscribe *subscribe_op;
119
120 /* aws_array_list<struct aws_mqtt_subscription_set_subscription_record *> */
121 struct aws_array_list subscriptions;
122
123 aws_mqtt_suback_fn *on_suback;
124 void *on_suback_user_data;
125
126 aws_mqtt_suback_multi_fn *on_multi_suback;
127 void *on_multi_suback_user_data;
128 };
129
130 struct aws_mqtt5_to_mqtt3_adapter_operation_unsubscribe {
131 struct aws_mqtt5_to_mqtt3_adapter_operation_base base;
132
133 /*
134 * holds a reference to the MQTT5 client unsubscribe operation until the operation completes or our adapter
135 * goes away.
136 *
137 * In the case where we're going away, we zero out the MQTT5 operation callbacks to prevent crash-triggering
138 * notifications.
139 */
140 struct aws_mqtt5_operation_unsubscribe *unsubscribe_op;
141
142 struct aws_byte_buf topic_filter;
143
144 aws_mqtt_op_complete_fn *on_unsuback;
145 void *on_unsuback_user_data;
146 };
147
148 /*
149
150 Sequencing (PUBLISH example):
151
152 Mqtt311 public API call
153 Create cross thread task
154 Create adapter op -> Create and attach mqtt5 op
155 allocate id and add operation to adapter table
156 Add adapter op's internal ref to adapter
157 submit cross thread task to event loop
158 return id or 0
159
160 Adapter Op reaches event loop task function: (from this point, all callbacks must be safe-guarded)
161 terminated = true
162 Safe handler:
163 If adapter not terminated:
164 terminated = false
165 Synchronously enqueue operation to mqtt5 client
166 if terminated:
167 remove adapter op from table
168 destroy adapter op
169 Release adapter op's internal ref to adapter
170
171 On publish completion:
172 Safe handler:
173 If not terminated:
174 invoke mqtt311 callback
175 Remove adapter op from table
176 Destroy adapter op
177
178 On final destroy (zero internal refs):
179 Iterate all incomplete adapter operations and cancel them: zero callbacks and remove from queue if in queue and
180 unbound
181 Destroy all adapter ops
182 Clear table
183 */
184
185 struct aws_mqtt5_to_mqtt3_adapter_operation_table {
186 struct aws_mutex lock;
187
188 struct aws_hash_table operations;
189 uint16_t next_id;
190 };
191
192 /*
193 * The adapter maintains a notion of state based on how its 311 API has been used. This state guides how it handles
194 * external lifecycle events.
195 *
196 * Operational (sourced from the adapter) events are always relayed unless the adapter has been terminated.
197 */
198 enum aws_mqtt_adapter_state {
199
200 /*
201 * The 311 API has had connect() called but that connect has not yet resolved.
202 *
203 * If it resolves successfully we will move to the STAY_CONNECTED state which will relay lifecycle callbacks
204 * transparently.
205 *
206 * If it resolves unsuccessfully, we will move to the STAY_DISCONNECTED state where we will ignore lifecycle
207 * events because, from the 311 API's perspective, nothing should be getting emitted.
208 */
209 AWS_MQTT_AS_FIRST_CONNECT,
210
211 /*
212 * A call to the 311 connect API has resolved successfully. Relay all lifecycle events until told otherwise.
213 */
214 AWS_MQTT_AS_STAY_CONNECTED,
215
216 /*
217 * We have not observed a successful initial connection attempt via the 311 API (or disconnect has been
218 * invoked afterwards). Ignore all lifecycle events.
219 */
220 AWS_MQTT_AS_STAY_DISCONNECTED,
221 };
222
223 struct aws_mqtt_client_connection_5_impl {
224
225 struct aws_allocator *allocator;
226
227 struct aws_mqtt_client_connection base;
228
229 struct aws_mqtt5_client *client;
230 struct aws_mqtt5_listener *listener;
231 struct aws_event_loop *loop;
232
233 /*
234 * The current adapter state based on the sequence of connect(), disconnect(), and connection completion events.
235 * This affects how the adapter reacts to incoming mqtt5 events. Under certain conditions, we may change
236 * this state value based on unexpected events (stopping the mqtt5 client underneath the adapter, for example)
237 */
238 enum aws_mqtt_adapter_state adapter_state;
239
240 /*
241 * Tracks all references from external sources (ie users). Incremented and decremented by the public
242 * acquire/release APIs of the 311 connection.
243 *
244 * When this value drops to zero, the terminated flag is set and no further callbacks will be invoked. This
245 * also starts the asynchronous destruction process for the adapter.
246 */
247 struct aws_ref_count external_refs;
248
249 /*
250 * Tracks all references to the adapter from internal sources (temporary async processes that need the
251 * adapter to stay alive for an interval of time, like sending tasks across thread boundaries).
252 *
253 * Starts with a single reference that is held until the adapter's listener has fully detached from the mqtt5
254 * client.
255 *
256 * Once the internal ref count drops to zero, the adapter may be destroyed synchronously.
257 */
258 struct aws_ref_count internal_refs;
259
260 struct aws_mqtt5_to_mqtt3_adapter_operation_table operational_state;
261
262 struct aws_mqtt_subscription_set *subscriptions;
263
264 /* All fields after here are internal to the adapter event loop thread */
265
266 /* 311 interface callbacks */
267 aws_mqtt_client_on_connection_interrupted_fn *on_interrupted;
268 void *on_interrupted_user_data;
269
270 aws_mqtt_client_on_connection_resumed_fn *on_resumed;
271 void *on_resumed_user_data;
272
273 aws_mqtt_client_on_connection_closed_fn *on_closed;
274 void *on_closed_user_data;
275
276 aws_mqtt_client_on_connection_success_fn *on_connection_success;
277 void *on_connection_success_user_data;
278
279 aws_mqtt_client_on_connection_failure_fn *on_connection_failure;
280 void *on_connection_failure_user_data;
281
282 aws_mqtt_client_publish_received_fn *on_any_publish;
283 void *on_any_publish_user_data;
284
285 aws_mqtt_transform_websocket_handshake_fn *websocket_handshake_transformer;
286 void *websocket_handshake_transformer_user_data;
287
288 aws_mqtt5_transform_websocket_handshake_complete_fn *mqtt5_websocket_handshake_completion_function;
289 void *mqtt5_websocket_handshake_completion_user_data;
290
291 /* (mutually exclusive) 311 interface one-time transient callbacks */
292 aws_mqtt_client_on_disconnect_fn *on_disconnect;
293 void *on_disconnect_user_data;
294
295 aws_mqtt_client_on_connection_complete_fn *on_connection_complete;
296 void *on_connection_complete_user_data;
297
298 aws_mqtt_client_on_connection_termination_fn *on_termination;
299 void *on_termination_user_data;
300 };
301
302 AWS_EXTERN_C_BEGIN
303
304 AWS_MQTT_API void aws_mqtt5_to_mqtt3_adapter_operation_table_init(
305 struct aws_mqtt5_to_mqtt3_adapter_operation_table *table,
306 struct aws_allocator *allocator);
307
308 AWS_MQTT_API void aws_mqtt5_to_mqtt3_adapter_operation_table_clean_up(
309 struct aws_mqtt5_to_mqtt3_adapter_operation_table *table);
310
311 AWS_MQTT_API int aws_mqtt5_to_mqtt3_adapter_operation_table_add_operation(
312 struct aws_mqtt5_to_mqtt3_adapter_operation_table *table,
313 struct aws_mqtt5_to_mqtt3_adapter_operation_base *operation);
314
315 AWS_MQTT_API void aws_mqtt5_to_mqtt3_adapter_operation_table_remove_operation(
316 struct aws_mqtt5_to_mqtt3_adapter_operation_table *table,
317 uint16_t operation_id);
318
319 AWS_MQTT_API struct aws_mqtt5_to_mqtt3_adapter_operation_publish *aws_mqtt5_to_mqtt3_adapter_operation_new_publish(
320 struct aws_allocator *allocator,
321 const struct aws_mqtt5_to_mqtt3_adapter_publish_options *options);
322
323 AWS_MQTT_API struct aws_mqtt5_to_mqtt3_adapter_operation_subscribe *aws_mqtt5_to_mqtt3_adapter_operation_new_subscribe(
324 struct aws_allocator *allocator,
325 const struct aws_mqtt5_to_mqtt3_adapter_subscribe_options *options,
326 struct aws_mqtt_client_connection_5_impl *adapter);
327
328 AWS_MQTT_API struct aws_mqtt5_to_mqtt3_adapter_operation_unsubscribe *
329 aws_mqtt5_to_mqtt3_adapter_operation_new_unsubscribe(
330 struct aws_allocator *allocator,
331 const struct aws_mqtt5_to_mqtt3_adapter_unsubscribe_options *options);
332
333 AWS_MQTT_API struct aws_mqtt5_to_mqtt3_adapter_operation_base *aws_mqtt5_to_mqtt3_adapter_operation_release(
334 struct aws_mqtt5_to_mqtt3_adapter_operation_base *operation);
335
336 AWS_MQTT_API struct aws_mqtt5_to_mqtt3_adapter_operation_base *aws_mqtt5_to_mqtt3_adapter_operation_acquire(
337 struct aws_mqtt5_to_mqtt3_adapter_operation_base *operation);
338
339 AWS_EXTERN_C_END
340
341 #endif /* AWS_MQTT_MQTT5_TO_MQTT3_ADAPTER_IMPL_H */