Debian Codegraph @

crt/aws-c-mqtt/include/aws/mqtt/private/request-response/protocol_adapter.h

raw symbols SCIP
1 #ifndef AWS_MQTT_PRIVATE_REQUEST_RESPONSE_PROTOCOL_ADAPTER_H
2 #define AWS_MQTT_PRIVATE_REQUEST_RESPONSE_PROTOCOL_ADAPTER_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/exports.h>
10 #include <aws/mqtt/mqtt.h>
11
12 #include <aws/common/byte_buf.h>
13
14 struct aws_allocator;
15 struct aws_event_loop;
16 struct aws_mqtt_client_connection;
17 struct aws_mqtt5_client;
18 struct aws_mqtt_rr_incoming_publish_event;
19
20 /*
21 * The request-response protocol adapter is a translation layer that sits between the request-response native client
22 * implementation and a protocol client capable of subscribing, unsubscribing, and publishing MQTT messages.
23 * Valid protocol clients include the CRT MQTT5 client, the CRT MQTT311 client, and an eventstream RPC connection
24 * that belongs to a Greengrass IPC client. Each of these protocol clients has a different (or even implicit)
25 * contract for carrying out pub-sub operations. The protocol adapter abstracts these details with a simple,
26 * minimal interface based on the requirements identified in the request-response design documents.
27 */
28
29 /*
30 * Minimal MQTT subscribe options
31 */
32 struct aws_protocol_adapter_subscribe_options {
33 struct aws_byte_cursor topic_filter;
34 uint32_t ack_timeout_seconds;
35 };
36
37 /*
38 * Minimal MQTT unsubscribe options
39 */
40 struct aws_protocol_adapter_unsubscribe_options {
41 struct aws_byte_cursor topic_filter;
42 uint32_t ack_timeout_seconds;
43 };
44
45 /*
46 * Minimal MQTT publish options
47 */
48 struct aws_protocol_adapter_publish_options {
49 struct aws_byte_cursor topic;
50 struct aws_byte_cursor payload;
51 uint32_t ack_timeout_seconds;
52
53 /*
54 * Invoked on success/failure of the publish itself. Our implementations use QoS1 which means that success
55 * will be on puback receipt.
56 */
57 void (*completion_callback_fn)(int, void *);
58
59 /*
60 * User data to pass in when invoking the completion callback
61 */
62 void *user_data;
63 };
64
65 /*
66 * Describes the type of subscription event (relative to a topic filter)
67 */
68 enum aws_protocol_adapter_subscription_event_type {
69 AWS_PASET_SUBSCRIBE,
70 AWS_PASET_UNSUBSCRIBE,
71 };
72
73 /*
74 * An event emitted by the protocol adapter when a subscribe or unsubscribe is completed by the adapted protocol
75 * client.
76 */
77 struct aws_protocol_adapter_subscription_event {
78 struct aws_byte_cursor topic_filter;
79 enum aws_protocol_adapter_subscription_event_type event_type;
80 int error_code;
81 bool retryable;
82 };
83
84 enum aws_protocol_adapter_connection_event_type {
85 AWS_PACET_CONNECTED,
86 AWS_PACET_DISCONNECTED,
87 };
88
89 /*
90 * An event emitted by the protocol adapter whenever the protocol client's connection status changes
91 */
92 struct aws_protocol_adapter_connection_event {
93 enum aws_protocol_adapter_connection_event_type event_type;
94 bool joined_session;
95 };
96
97 typedef void(aws_protocol_adapter_subscription_event_fn)(
98 const struct aws_protocol_adapter_subscription_event *event,
99 void *user_data);
100
101 typedef void(aws_protocol_adapter_incoming_publish_fn)(
102 const struct aws_mqtt_rr_incoming_publish_event *publish,
103 void *user_data);
104
105 typedef void(aws_protocol_adapter_terminate_callback_fn)(void *user_data);
106
107 typedef void(aws_protocol_adapter_connection_event_fn)(
108 const struct aws_protocol_adapter_connection_event *event,
109 void *user_data);
110
111 /*
112 * Set of callbacks invoked by the protocol adapter. These must all be set.
113 */
114 struct aws_mqtt_protocol_adapter_options {
115 aws_protocol_adapter_subscription_event_fn *subscription_event_callback;
116 aws_protocol_adapter_incoming_publish_fn *incoming_publish_callback;
117 aws_protocol_adapter_terminate_callback_fn *terminate_callback;
118 aws_protocol_adapter_connection_event_fn *connection_event_callback;
119
120 /*
121 * User data to pass into all singleton protocol adapter callbacks. Likely either the request-response client
122 * or the subscription manager component of the request-response client.
123 */
124 void *user_data;
125 };
126
127 struct aws_mqtt_protocol_adapter_vtable {
128
129 void (*aws_mqtt_protocol_adapter_destroy_fn)(void *);
130
131 int (*aws_mqtt_protocol_adapter_subscribe_fn)(void *, struct aws_protocol_adapter_subscribe_options *);
132
133 int (*aws_mqtt_protocol_adapter_unsubscribe_fn)(void *, struct aws_protocol_adapter_unsubscribe_options *);
134
135 int (*aws_mqtt_protocol_adapter_publish_fn)(void *, struct aws_protocol_adapter_publish_options *);
136
137 bool (*aws_mqtt_protocol_adapter_is_connected_fn)(void *);
138
139 struct aws_event_loop *(*aws_mqtt_protocol_adapter_get_event_loop_fn)(void *);
140 };
141
142 struct aws_mqtt_protocol_adapter {
143 const struct aws_mqtt_protocol_adapter_vtable *vtable;
144 void *impl;
145 };
146
147 AWS_EXTERN_C_BEGIN
148
149 /*
150 * Creates a new request-response protocol adapter from an MQTT311 client
151 */
152 AWS_MQTT_API struct aws_mqtt_protocol_adapter *aws_mqtt_protocol_adapter_new_from_311(
153 struct aws_allocator *allocator,
154 struct aws_mqtt_protocol_adapter_options *options,
155 struct aws_mqtt_client_connection *connection);
156
157 /*
158 * Creates a new request-response protocol adapter from an MQTT5 client
159 */
160 AWS_MQTT_API struct aws_mqtt_protocol_adapter *aws_mqtt_protocol_adapter_new_from_5(
161 struct aws_allocator *allocator,
162 struct aws_mqtt_protocol_adapter_options *options,
163 struct aws_mqtt5_client *client);
164
165 /*
166 * Destroys a request-response protocol adapter. Destruction is an asynchronous process and the caller must
167 * wait for the termination callback to be invoked before assuming that no further callbacks will be invoked.
168 */
169 AWS_MQTT_API void aws_mqtt_protocol_adapter_destroy(struct aws_mqtt_protocol_adapter *adapter);
170
171 /*
172 * Asks the adapted protocol client to perform an MQTT subscribe operation
173 */
174 AWS_MQTT_API int aws_mqtt_protocol_adapter_subscribe(
175 struct aws_mqtt_protocol_adapter *adapter,
176 struct aws_protocol_adapter_subscribe_options *options);
177
178 /*
179 * Asks the adapted protocol client to perform an MQTT unsubscribe operation
180 */
181 AWS_MQTT_API int aws_mqtt_protocol_adapter_unsubscribe(
182 struct aws_mqtt_protocol_adapter *adapter,
183 struct aws_protocol_adapter_unsubscribe_options *options);
184
185 /*
186 * Asks the adapted protocol client to perform an MQTT publish operation
187 */
188 AWS_MQTT_API int aws_mqtt_protocol_adapter_publish(
189 struct aws_mqtt_protocol_adapter *adapter,
190 struct aws_protocol_adapter_publish_options *options);
191
192 /*
193 * Synchronously checks the connection state of the adapted protocol client. May only be called from the
194 * protocol client's event loop.
195 */
196 AWS_MQTT_API bool aws_mqtt_protocol_adapter_is_connected(struct aws_mqtt_protocol_adapter *adapter);
197
198 /*
199 * Returns the event loop that the protocol client is bound to.
200 */
201 AWS_MQTT_API struct aws_event_loop *aws_mqtt_protocol_adapter_get_event_loop(struct aws_mqtt_protocol_adapter *adapter);
202
203 AWS_MQTT_API const char *aws_protocol_adapter_subscription_event_type_to_c_str(
204 enum aws_protocol_adapter_subscription_event_type type);
205
206 AWS_MQTT_API const char *aws_protocol_adapter_connection_event_type_to_c_str(
207 enum aws_protocol_adapter_connection_event_type type);
208
209 AWS_EXTERN_C_END
210
211 #endif /* AWS_MQTT_PRIVATE_REQUEST_RESPONSE_PROTOCOL_ADAPTER_H */