| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/mqtt/private/request-response/protocol_adapter.h> |
| 7 |
|
| 8 |
#include <aws/common/clock.h> |
| 9 |
#include <aws/io/event_loop.h> |
| 10 |
#include <aws/mqtt/private/client_impl.h> |
| 11 |
#include <aws/mqtt/private/client_impl_shared.h> |
| 12 |
#include <aws/mqtt/private/mqtt311_listener.h> |
| 13 |
#include <aws/mqtt/private/v5/mqtt5_client_impl.h> |
| 14 |
#include <aws/mqtt/private/v5/mqtt5_to_mqtt3_adapter_impl.h> |
| 15 |
#include <aws/mqtt/request-response/request_response_client.h> |
| 16 |
#include <aws/mqtt/v5/mqtt5_client.h> |
| 17 |
#include <aws/mqtt/v5/mqtt5_listener.h> |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
enum aws_mqtt_protocol_adapter_operation_type { |
| 39 |
AMPAOT_SUBSCRIBE_UNSUBSCRIBE, |
| 40 |
AMPAOT_PUBLISH, |
| 41 |
}; |
| 42 |
|
| 43 |
struct aws_mqtt_protocol_adapter_sub_unsub_data { |
| 44 |
struct aws_byte_buf topic_filter; |
| 45 |
}; |
| 46 |
|
| 47 |
struct aws_mqtt_protocol_adapter_publish_data { |
| 48 |
void (*completion_callback_fn)(int, void *); |
| 49 |
void *user_data; |
| 50 |
}; |
| 51 |
|
| 52 |
struct aws_mqtt_protocol_adapter_operation_userdata { |
| 53 |
struct aws_allocator *allocator; |
| 54 |
|
| 55 |
struct aws_linked_list_node node; |
| 56 |
void *adapter; |
| 57 |
|
| 58 |
enum aws_mqtt_protocol_adapter_operation_type operation_type; |
| 59 |
|
| 60 |
union { |
| 61 |
struct aws_mqtt_protocol_adapter_sub_unsub_data sub_unsub_data; |
| 62 |
struct aws_mqtt_protocol_adapter_publish_data publish_data; |
| 63 |
} operation_data; |
| 64 |
}; |
| 65 |
|
| 66 |
static struct aws_mqtt_protocol_adapter_operation_userdata *s_aws_mqtt_protocol_adapter_sub_unsub_data_new( |
| 67 |
struct aws_allocator *allocator, |
| 68 |
struct aws_byte_cursor topic_filter, |
| 69 |
void *adapter) { |
| 70 |
|
| 71 |
struct aws_mqtt_protocol_adapter_operation_userdata *subscribe_data = |
| 72 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_protocol_adapter_operation_userdata)); |
| 73 |
|
| 74 |
subscribe_data->allocator = allocator; |
| 75 |
subscribe_data->operation_type = AMPAOT_SUBSCRIBE_UNSUBSCRIBE; |
| 76 |
subscribe_data->adapter = adapter; |
| 77 |
aws_byte_buf_init_copy_from_cursor( |
| 78 |
&subscribe_data->operation_data.sub_unsub_data.topic_filter, allocator, topic_filter); |
| 79 |
|
| 80 |
return subscribe_data; |
| 81 |
} |
| 82 |
|
| 83 |
static struct aws_mqtt_protocol_adapter_operation_userdata *s_aws_mqtt_protocol_adapter_publish_data_new( |
| 84 |
struct aws_allocator *allocator, |
| 85 |
const struct aws_protocol_adapter_publish_options *publish_options, |
| 86 |
void *adapter) { |
| 87 |
|
| 88 |
struct aws_mqtt_protocol_adapter_operation_userdata *publish_data = |
| 89 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_protocol_adapter_operation_userdata)); |
| 90 |
|
| 91 |
publish_data->allocator = allocator; |
| 92 |
publish_data->operation_type = AMPAOT_PUBLISH; |
| 93 |
publish_data->adapter = adapter; |
| 94 |
|
| 95 |
publish_data->operation_data.publish_data.completion_callback_fn = publish_options->completion_callback_fn; |
| 96 |
publish_data->operation_data.publish_data.user_data = publish_options->user_data; |
| 97 |
|
| 98 |
return publish_data; |
| 99 |
} |
| 100 |
|
| 101 |
static void s_aws_mqtt_protocol_adapter_operation_user_data_destroy( |
| 102 |
struct aws_mqtt_protocol_adapter_operation_userdata *userdata) { |
| 103 |
if (userdata == NULL) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if (aws_linked_list_node_next_is_valid(&userdata->node) && aws_linked_list_node_prev_is_valid(&userdata->node)) { |
| 108 |
aws_linked_list_remove(&userdata->node); |
| 109 |
} |
| 110 |
|
| 111 |
if (userdata->operation_type == AMPAOT_SUBSCRIBE_UNSUBSCRIBE) { |
| 112 |
aws_byte_buf_clean_up(&userdata->operation_data.sub_unsub_data.topic_filter); |
| 113 |
} |
| 114 |
|
| 115 |
aws_mem_release(userdata->allocator, userdata); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
struct aws_mqtt_protocol_adapter_311_impl { |
| 121 |
struct aws_allocator *allocator; |
| 122 |
struct aws_mqtt_protocol_adapter base; |
| 123 |
|
| 124 |
struct aws_linked_list incomplete_operations; |
| 125 |
struct aws_mqtt_protocol_adapter_options config; |
| 126 |
|
| 127 |
struct aws_event_loop *loop; |
| 128 |
struct aws_mqtt_client_connection *connection; |
| 129 |
struct aws_mqtt311_listener *listener; |
| 130 |
}; |
| 131 |
|
| 132 |
static void s_aws_mqtt_protocol_adapter_311_destroy(void *impl) { |
| 133 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = impl; |
| 134 |
|
| 135 |
|
| 136 |
aws_mqtt311_listener_release(adapter->listener); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
static void s_protocol_adapter_311_subscribe_completion( |
| 142 |
struct aws_mqtt_client_connection *connection, |
| 143 |
uint16_t packet_id, |
| 144 |
const struct aws_byte_cursor *topic, |
| 145 |
enum aws_mqtt_qos qos, |
| 146 |
int error_code, |
| 147 |
void *userdata) { |
| 148 |
(void)connection; |
| 149 |
(void)topic; |
| 150 |
(void)packet_id; |
| 151 |
|
| 152 |
struct aws_mqtt_protocol_adapter_operation_userdata *subscribe_data = userdata; |
| 153 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = subscribe_data->adapter; |
| 154 |
|
| 155 |
if (adapter == NULL) { |
| 156 |
goto done; |
| 157 |
} |
| 158 |
|
| 159 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 160 |
if (qos >= 128) { |
| 161 |
error_code = AWS_ERROR_MQTT_PROTOCOL_ADAPTER_FAILING_REASON_CODE; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
struct aws_protocol_adapter_subscription_event subscribe_event = { |
| 166 |
.topic_filter = aws_byte_cursor_from_buf(&subscribe_data->operation_data.sub_unsub_data.topic_filter), |
| 167 |
.event_type = AWS_PASET_SUBSCRIBE, |
| 168 |
.error_code = error_code, |
| 169 |
.retryable = true, |
| 170 |
}; |
| 171 |
|
| 172 |
(*adapter->config.subscription_event_callback)(&subscribe_event, adapter->config.user_data); |
| 173 |
|
| 174 |
done: |
| 175 |
|
| 176 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(subscribe_data); |
| 177 |
} |
| 178 |
|
| 179 |
int s_aws_mqtt_protocol_adapter_311_subscribe(void *impl, struct aws_protocol_adapter_subscribe_options *options) { |
| 180 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = impl; |
| 181 |
struct aws_mqtt_client_connection_311_impl *connection_impl = adapter->connection->impl; |
| 182 |
|
| 183 |
struct aws_mqtt_protocol_adapter_operation_userdata *subscribe_data = |
| 184 |
s_aws_mqtt_protocol_adapter_sub_unsub_data_new(adapter->allocator, options->topic_filter, adapter); |
| 185 |
|
| 186 |
aws_linked_list_push_back(&adapter->incomplete_operations, &subscribe_data->node); |
| 187 |
|
| 188 |
uint64_t timeout_nanos = |
| 189 |
aws_timestamp_convert(options->ack_timeout_seconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 190 |
if (aws_mqtt_client_connection_311_subscribe( |
| 191 |
connection_impl, |
| 192 |
&options->topic_filter, |
| 193 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 194 |
NULL, |
| 195 |
NULL, |
| 196 |
NULL, |
| 197 |
s_protocol_adapter_311_subscribe_completion, |
| 198 |
subscribe_data, |
| 199 |
timeout_nanos) == 0) { |
| 200 |
goto error; |
| 201 |
} |
| 202 |
|
| 203 |
return AWS_OP_SUCCESS; |
| 204 |
|
| 205 |
error: |
| 206 |
|
| 207 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(subscribe_data); |
| 208 |
|
| 209 |
return AWS_OP_ERR; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
static bool s_is_retryable_unsubscribe311(int error_code) { |
| 215 |
return error_code == AWS_ERROR_MQTT_TIMEOUT; |
| 216 |
} |
| 217 |
|
| 218 |
static void s_protocol_adapter_311_unsubscribe_completion( |
| 219 |
struct aws_mqtt_client_connection *connection, |
| 220 |
uint16_t packet_id, |
| 221 |
int error_code, |
| 222 |
void *userdata) { |
| 223 |
(void)connection; |
| 224 |
(void)packet_id; |
| 225 |
|
| 226 |
struct aws_mqtt_protocol_adapter_operation_userdata *unsubscribe_data = userdata; |
| 227 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = unsubscribe_data->adapter; |
| 228 |
|
| 229 |
if (adapter == NULL) { |
| 230 |
goto done; |
| 231 |
} |
| 232 |
|
| 233 |
struct aws_protocol_adapter_subscription_event unsubscribe_event = { |
| 234 |
.topic_filter = aws_byte_cursor_from_buf(&unsubscribe_data->operation_data.sub_unsub_data.topic_filter), |
| 235 |
.event_type = AWS_PASET_UNSUBSCRIBE, |
| 236 |
.error_code = error_code, |
| 237 |
.retryable = s_is_retryable_unsubscribe311(error_code), |
| 238 |
}; |
| 239 |
|
| 240 |
(*adapter->config.subscription_event_callback)(&unsubscribe_event, adapter->config.user_data); |
| 241 |
|
| 242 |
done: |
| 243 |
|
| 244 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(unsubscribe_data); |
| 245 |
} |
| 246 |
|
| 247 |
int s_aws_mqtt_protocol_adapter_311_unsubscribe(void *impl, struct aws_protocol_adapter_unsubscribe_options *options) { |
| 248 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = impl; |
| 249 |
struct aws_mqtt_client_connection_311_impl *connection_impl = adapter->connection->impl; |
| 250 |
|
| 251 |
struct aws_mqtt_protocol_adapter_operation_userdata *unsubscribe_data = |
| 252 |
s_aws_mqtt_protocol_adapter_sub_unsub_data_new(adapter->allocator, options->topic_filter, adapter); |
| 253 |
|
| 254 |
aws_linked_list_push_back(&adapter->incomplete_operations, &unsubscribe_data->node); |
| 255 |
|
| 256 |
uint64_t timeout_nanos = |
| 257 |
aws_timestamp_convert(options->ack_timeout_seconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 258 |
if (aws_mqtt_client_connection_311_unsubscribe( |
| 259 |
connection_impl, |
| 260 |
&options->topic_filter, |
| 261 |
s_protocol_adapter_311_unsubscribe_completion, |
| 262 |
unsubscribe_data, |
| 263 |
timeout_nanos) == 0) { |
| 264 |
goto error; |
| 265 |
} |
| 266 |
|
| 267 |
return AWS_OP_SUCCESS; |
| 268 |
|
| 269 |
error: |
| 270 |
|
| 271 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(unsubscribe_data); |
| 272 |
|
| 273 |
return AWS_OP_ERR; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static void s_protocol_adapter_311_publish_completion( |
| 279 |
struct aws_mqtt_client_connection *connection, |
| 280 |
uint16_t packet_id, |
| 281 |
int error_code, |
| 282 |
void *userdata) { |
| 283 |
|
| 284 |
(void)connection; |
| 285 |
(void)packet_id; |
| 286 |
|
| 287 |
struct aws_mqtt_protocol_adapter_operation_userdata *publish_data = userdata; |
| 288 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = publish_data->adapter; |
| 289 |
|
| 290 |
if (adapter == NULL) { |
| 291 |
goto done; |
| 292 |
} |
| 293 |
|
| 294 |
(*publish_data->operation_data.publish_data.completion_callback_fn)( |
| 295 |
error_code, publish_data->operation_data.publish_data.user_data); |
| 296 |
|
| 297 |
done: |
| 298 |
|
| 299 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(publish_data); |
| 300 |
} |
| 301 |
|
| 302 |
int s_aws_mqtt_protocol_adapter_311_publish(void *impl, struct aws_protocol_adapter_publish_options *options) { |
| 303 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = impl; |
| 304 |
struct aws_mqtt_client_connection_311_impl *connection_impl = adapter->connection->impl; |
| 305 |
|
| 306 |
struct aws_mqtt_protocol_adapter_operation_userdata *publish_data = |
| 307 |
s_aws_mqtt_protocol_adapter_publish_data_new(adapter->allocator, options, adapter); |
| 308 |
|
| 309 |
aws_linked_list_push_back(&adapter->incomplete_operations, &publish_data->node); |
| 310 |
|
| 311 |
uint64_t timeout_nanos = |
| 312 |
aws_timestamp_convert(options->ack_timeout_seconds, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 313 |
if (aws_mqtt_client_connection_311_publish( |
| 314 |
connection_impl, |
| 315 |
&options->topic, |
| 316 |
AWS_MQTT_QOS_AT_LEAST_ONCE, |
| 317 |
false, |
| 318 |
&options->payload, |
| 319 |
s_protocol_adapter_311_publish_completion, |
| 320 |
publish_data, |
| 321 |
timeout_nanos) == 0) { |
| 322 |
goto error; |
| 323 |
} |
| 324 |
|
| 325 |
return AWS_OP_SUCCESS; |
| 326 |
|
| 327 |
error: |
| 328 |
|
| 329 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(publish_data); |
| 330 |
|
| 331 |
return AWS_OP_ERR; |
| 332 |
} |
| 333 |
|
| 334 |
static void s_protocol_adapter_mqtt311_listener_publish_received( |
| 335 |
struct aws_mqtt_client_connection *connection, |
| 336 |
const struct aws_byte_cursor *topic, |
| 337 |
const struct aws_byte_cursor *payload, |
| 338 |
bool dup, |
| 339 |
enum aws_mqtt_qos qos, |
| 340 |
bool retain, |
| 341 |
void *userdata) { |
| 342 |
|
| 343 |
(void)connection; |
| 344 |
(void)dup; |
| 345 |
(void)qos; |
| 346 |
(void)retain; |
| 347 |
|
| 348 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = userdata; |
| 349 |
|
| 350 |
struct aws_mqtt_rr_incoming_publish_event publish_event = { |
| 351 |
.topic = *topic, |
| 352 |
.payload = *payload, |
| 353 |
}; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
(*adapter->config.incoming_publish_callback)(&publish_event, adapter->config.user_data); |
| 358 |
} |
| 359 |
|
| 360 |
static void s_protocol_adapter_mqtt311_listener_connection_success( |
| 361 |
struct aws_mqtt_client_connection *connection, |
| 362 |
enum aws_mqtt_connect_return_code return_code, |
| 363 |
bool session_present, |
| 364 |
void *userdata) { |
| 365 |
(void)connection; |
| 366 |
(void)return_code; |
| 367 |
|
| 368 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = userdata; |
| 369 |
|
| 370 |
if (adapter->config.connection_event_callback != NULL) { |
| 371 |
struct aws_protocol_adapter_connection_event connection_event = { |
| 372 |
.event_type = AWS_PACET_CONNECTED, |
| 373 |
.joined_session = session_present, |
| 374 |
}; |
| 375 |
|
| 376 |
(*adapter->config.connection_event_callback)(&connection_event, adapter->config.user_data); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
static void s_protocol_adapter_mqtt311_emit_disconnect_event(struct aws_mqtt_protocol_adapter_311_impl *adapter) { |
| 381 |
if (adapter->config.connection_event_callback != NULL) { |
| 382 |
struct aws_protocol_adapter_connection_event connection_event = { |
| 383 |
.event_type = AWS_PACET_DISCONNECTED, |
| 384 |
}; |
| 385 |
|
| 386 |
(*adapter->config.connection_event_callback)(&connection_event, adapter->config.user_data); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
static void s_protocol_adapter_mqtt311_listener_connection_interrupted( |
| 391 |
struct aws_mqtt_client_connection *connection, |
| 392 |
int error_code, |
| 393 |
void *userdata) { |
| 394 |
(void)connection; |
| 395 |
(void)error_code; |
| 396 |
|
| 397 |
s_protocol_adapter_mqtt311_emit_disconnect_event(userdata); |
| 398 |
} |
| 399 |
|
| 400 |
static void s_aws_mqtt_protocol_adapter_311_disconnect_fn( |
| 401 |
struct aws_mqtt_client_connection *connection, |
| 402 |
void *userdata) { |
| 403 |
(void)connection; |
| 404 |
|
| 405 |
s_protocol_adapter_mqtt311_emit_disconnect_event(userdata); |
| 406 |
} |
| 407 |
|
| 408 |
static bool s_aws_mqtt_protocol_adapter_311_is_connected(void *impl) { |
| 409 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = impl; |
| 410 |
struct aws_mqtt_client_connection_311_impl *connection_impl = adapter->connection->impl; |
| 411 |
|
| 412 |
AWS_FATAL_ASSERT(aws_event_loop_thread_is_callers_thread(connection_impl->loop)); |
| 413 |
|
| 414 |
mqtt_connection_lock_synced_data(connection_impl); |
| 415 |
enum aws_mqtt_client_connection_state current_state = connection_impl->synced_data.state; |
| 416 |
mqtt_connection_unlock_synced_data(connection_impl); |
| 417 |
|
| 418 |
return current_state == AWS_MQTT_CLIENT_STATE_CONNECTED; |
| 419 |
} |
| 420 |
|
| 421 |
static void s_release_incomplete_operations(struct aws_linked_list *incomplete_operations) { |
| 422 |
struct aws_linked_list dummy_list; |
| 423 |
aws_linked_list_init(&dummy_list); |
| 424 |
aws_linked_list_swap_contents(incomplete_operations, &dummy_list); |
| 425 |
|
| 426 |
while (!aws_linked_list_empty(&dummy_list)) { |
| 427 |
struct aws_linked_list_node *head = aws_linked_list_pop_front(&dummy_list); |
| 428 |
struct aws_mqtt_protocol_adapter_operation_userdata *userdata = |
| 429 |
AWS_CONTAINER_OF(head, struct aws_mqtt_protocol_adapter_operation_userdata, node); |
| 430 |
|
| 431 |
userdata->adapter = NULL; |
| 432 |
|
| 433 |
if (userdata->operation_type == AMPAOT_PUBLISH) { |
| 434 |
struct aws_mqtt_protocol_adapter_publish_data *publish_data = &userdata->operation_data.publish_data; |
| 435 |
if (publish_data->completion_callback_fn != NULL) { |
| 436 |
(*userdata->operation_data.publish_data.completion_callback_fn)( |
| 437 |
AWS_ERROR_MQTT_REQUEST_RESPONSE_CLIENT_SHUT_DOWN, publish_data->user_data); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
static void s_protocol_adapter_mqtt311_listener_termination_callback(void *user_data) { |
| 444 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = user_data; |
| 445 |
struct aws_mqtt_client_connection_311_impl *impl = adapter->connection->impl; |
| 446 |
|
| 447 |
AWS_FATAL_ASSERT(aws_event_loop_thread_is_callers_thread(impl->loop)); |
| 448 |
|
| 449 |
s_release_incomplete_operations(&adapter->incomplete_operations); |
| 450 |
|
| 451 |
aws_mqtt_client_connection_release(adapter->connection); |
| 452 |
|
| 453 |
aws_protocol_adapter_terminate_callback_fn *terminate_callback = adapter->config.terminate_callback; |
| 454 |
void *terminate_user_data = adapter->config.user_data; |
| 455 |
|
| 456 |
aws_mem_release(adapter->allocator, adapter); |
| 457 |
|
| 458 |
if (terminate_callback) { |
| 459 |
(*terminate_callback)(terminate_user_data); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
static struct aws_event_loop *s_aws_mqtt_protocol_adapter_311_get_event_loop(void *impl) { |
| 464 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = impl; |
| 465 |
|
| 466 |
return adapter->loop; |
| 467 |
} |
| 468 |
|
| 469 |
static struct aws_mqtt_protocol_adapter_vtable s_protocol_adapter_mqtt311_vtable = { |
| 470 |
.aws_mqtt_protocol_adapter_destroy_fn = s_aws_mqtt_protocol_adapter_311_destroy, |
| 471 |
.aws_mqtt_protocol_adapter_subscribe_fn = s_aws_mqtt_protocol_adapter_311_subscribe, |
| 472 |
.aws_mqtt_protocol_adapter_unsubscribe_fn = s_aws_mqtt_protocol_adapter_311_unsubscribe, |
| 473 |
.aws_mqtt_protocol_adapter_publish_fn = s_aws_mqtt_protocol_adapter_311_publish, |
| 474 |
.aws_mqtt_protocol_adapter_is_connected_fn = s_aws_mqtt_protocol_adapter_311_is_connected, |
| 475 |
.aws_mqtt_protocol_adapter_get_event_loop_fn = s_aws_mqtt_protocol_adapter_311_get_event_loop, |
| 476 |
}; |
| 477 |
|
| 478 |
struct aws_mqtt_protocol_adapter *aws_mqtt_protocol_adapter_new_from_311( |
| 479 |
struct aws_allocator *allocator, |
| 480 |
struct aws_mqtt_protocol_adapter_options *options, |
| 481 |
struct aws_mqtt_client_connection *connection) { |
| 482 |
|
| 483 |
if (options == NULL || connection == NULL) { |
| 484 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 485 |
return NULL; |
| 486 |
} |
| 487 |
|
| 488 |
if (aws_mqtt_client_connection_get_impl_type(connection) != AWS_MQTT311_IT_311_CONNECTION) { |
| 489 |
struct aws_mqtt_client_connection_5_impl *adapter_impl = connection->impl; |
| 490 |
return aws_mqtt_protocol_adapter_new_from_5(allocator, options, adapter_impl->client); |
| 491 |
} |
| 492 |
|
| 493 |
struct aws_mqtt_client_connection_311_impl *impl = connection->impl; |
| 494 |
|
| 495 |
struct aws_mqtt_protocol_adapter_311_impl *adapter = |
| 496 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_protocol_adapter_311_impl)); |
| 497 |
|
| 498 |
adapter->allocator = allocator; |
| 499 |
adapter->base.impl = adapter; |
| 500 |
adapter->base.vtable = &s_protocol_adapter_mqtt311_vtable; |
| 501 |
aws_linked_list_init(&adapter->incomplete_operations); |
| 502 |
adapter->config = *options; |
| 503 |
adapter->loop = impl->loop; |
| 504 |
adapter->connection = aws_mqtt_client_connection_acquire(connection); |
| 505 |
|
| 506 |
struct aws_mqtt311_listener_config listener_options = { |
| 507 |
.connection = connection, |
| 508 |
.listener_callbacks = |
| 509 |
{ |
| 510 |
.publish_received_handler = s_protocol_adapter_mqtt311_listener_publish_received, |
| 511 |
.connection_success_handler = s_protocol_adapter_mqtt311_listener_connection_success, |
| 512 |
.connection_interrupted_handler = s_protocol_adapter_mqtt311_listener_connection_interrupted, |
| 513 |
.disconnect_handler = s_aws_mqtt_protocol_adapter_311_disconnect_fn, |
| 514 |
.user_data = adapter, |
| 515 |
}, |
| 516 |
.termination_callback = s_protocol_adapter_mqtt311_listener_termination_callback, |
| 517 |
.termination_callback_user_data = adapter, |
| 518 |
}; |
| 519 |
|
| 520 |
adapter->listener = aws_mqtt311_listener_new(allocator, &listener_options); |
| 521 |
|
| 522 |
return &adapter->base; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
struct aws_mqtt_protocol_adapter_5_impl { |
| 528 |
struct aws_allocator *allocator; |
| 529 |
struct aws_mqtt_protocol_adapter base; |
| 530 |
struct aws_linked_list incomplete_operations; |
| 531 |
struct aws_mqtt_protocol_adapter_options config; |
| 532 |
|
| 533 |
struct aws_event_loop *loop; |
| 534 |
struct aws_mqtt5_client *client; |
| 535 |
struct aws_mqtt5_listener *listener; |
| 536 |
}; |
| 537 |
|
| 538 |
static void s_aws_mqtt_protocol_adapter_5_destroy(void *impl) { |
| 539 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = impl; |
| 540 |
|
| 541 |
|
| 542 |
aws_mqtt5_listener_release(adapter->listener); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
static bool s_is_retryable_subscribe(enum aws_mqtt5_suback_reason_code reason_code, int error_code) { |
| 548 |
if (error_code == AWS_ERROR_MQTT5_PACKET_VALIDATION || error_code == AWS_ERROR_MQTT5_SUBSCRIBE_OPTIONS_VALIDATION) { |
| 549 |
return false; |
| 550 |
} else if (error_code != AWS_ERROR_SUCCESS) { |
| 551 |
return true; |
| 552 |
} |
| 553 |
|
| 554 |
switch (reason_code) { |
| 555 |
case AWS_MQTT5_SARC_GRANTED_QOS_0: |
| 556 |
case AWS_MQTT5_SARC_GRANTED_QOS_1: |
| 557 |
case AWS_MQTT5_SARC_GRANTED_QOS_2: |
| 558 |
case AWS_MQTT5_SARC_UNSPECIFIED_ERROR: |
| 559 |
case AWS_MQTT5_SARC_PACKET_IDENTIFIER_IN_USE: |
| 560 |
case AWS_MQTT5_SARC_IMPLEMENTATION_SPECIFIC_ERROR: |
| 561 |
case AWS_MQTT5_SARC_QUOTA_EXCEEDED: |
| 562 |
return true; |
| 563 |
|
| 564 |
default: |
| 565 |
return false; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
static void s_protocol_adapter_5_subscribe_completion( |
| 570 |
const struct aws_mqtt5_packet_suback_view *suback, |
| 571 |
int error_code, |
| 572 |
void *complete_ctx) { |
| 573 |
struct aws_mqtt_protocol_adapter_operation_userdata *subscribe_data = complete_ctx; |
| 574 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = subscribe_data->adapter; |
| 575 |
|
| 576 |
if (adapter == NULL) { |
| 577 |
goto done; |
| 578 |
} |
| 579 |
|
| 580 |
enum aws_mqtt5_suback_reason_code reason_code = AWS_MQTT5_SARC_GRANTED_QOS_0; |
| 581 |
if (suback != NULL && suback->reason_code_count > 0) { |
| 582 |
reason_code = suback->reason_codes[0]; |
| 583 |
} |
| 584 |
bool is_retryable = s_is_retryable_subscribe(reason_code, error_code); |
| 585 |
|
| 586 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 587 |
if (suback == NULL || suback->reason_code_count != 1 || suback->reason_codes[0] >= 128) { |
| 588 |
error_code = AWS_ERROR_MQTT_PROTOCOL_ADAPTER_FAILING_REASON_CODE; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
struct aws_protocol_adapter_subscription_event subscribe_event = { |
| 593 |
.topic_filter = aws_byte_cursor_from_buf(&subscribe_data->operation_data.sub_unsub_data.topic_filter), |
| 594 |
.event_type = AWS_PASET_SUBSCRIBE, |
| 595 |
.error_code = error_code, |
| 596 |
.retryable = is_retryable, |
| 597 |
}; |
| 598 |
|
| 599 |
(*adapter->config.subscription_event_callback)(&subscribe_event, adapter->config.user_data); |
| 600 |
|
| 601 |
done: |
| 602 |
|
| 603 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(subscribe_data); |
| 604 |
} |
| 605 |
|
| 606 |
int s_aws_mqtt_protocol_adapter_5_subscribe(void *impl, struct aws_protocol_adapter_subscribe_options *options) { |
| 607 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = impl; |
| 608 |
|
| 609 |
struct aws_mqtt_protocol_adapter_operation_userdata *subscribe_data = |
| 610 |
s_aws_mqtt_protocol_adapter_sub_unsub_data_new(adapter->allocator, options->topic_filter, adapter); |
| 611 |
|
| 612 |
aws_linked_list_push_back(&adapter->incomplete_operations, &subscribe_data->node); |
| 613 |
|
| 614 |
struct aws_mqtt5_subscription_view subscription_view = { |
| 615 |
.qos = AWS_MQTT5_QOS_AT_LEAST_ONCE, |
| 616 |
.topic_filter = options->topic_filter, |
| 617 |
}; |
| 618 |
|
| 619 |
struct aws_mqtt5_packet_subscribe_view subscribe_view = { |
| 620 |
.subscriptions = &subscription_view, |
| 621 |
.subscription_count = 1, |
| 622 |
}; |
| 623 |
|
| 624 |
struct aws_mqtt5_subscribe_completion_options completion_options = { |
| 625 |
.ack_timeout_seconds_override = options->ack_timeout_seconds, |
| 626 |
.completion_callback = s_protocol_adapter_5_subscribe_completion, |
| 627 |
.completion_user_data = subscribe_data, |
| 628 |
}; |
| 629 |
|
| 630 |
if (aws_mqtt5_client_subscribe(adapter->client, &subscribe_view, &completion_options)) { |
| 631 |
goto error; |
| 632 |
} |
| 633 |
|
| 634 |
return AWS_OP_SUCCESS; |
| 635 |
|
| 636 |
error: |
| 637 |
|
| 638 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(subscribe_data); |
| 639 |
|
| 640 |
return AWS_OP_ERR; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
static bool s_is_retryable_unsubscribe5(enum aws_mqtt5_unsuback_reason_code reason_code, int error_code) { |
| 646 |
if (error_code == AWS_ERROR_MQTT5_PACKET_VALIDATION || |
| 647 |
error_code == AWS_ERROR_MQTT5_UNSUBSCRIBE_OPTIONS_VALIDATION) { |
| 648 |
return false; |
| 649 |
} else if (error_code == AWS_ERROR_MQTT_TIMEOUT) { |
| 650 |
return true; |
| 651 |
} |
| 652 |
|
| 653 |
switch (reason_code) { |
| 654 |
case AWS_MQTT5_UARC_UNSPECIFIED_ERROR: |
| 655 |
case AWS_MQTT5_UARC_IMPLEMENTATION_SPECIFIC_ERROR: |
| 656 |
return true; |
| 657 |
|
| 658 |
default: |
| 659 |
return false; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
static void s_protocol_adapter_5_unsubscribe_completion( |
| 664 |
const struct aws_mqtt5_packet_unsuback_view *unsuback, |
| 665 |
int error_code, |
| 666 |
void *complete_ctx) { |
| 667 |
struct aws_mqtt_protocol_adapter_operation_userdata *unsubscribe_data = complete_ctx; |
| 668 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = unsubscribe_data->adapter; |
| 669 |
|
| 670 |
if (adapter == NULL) { |
| 671 |
goto done; |
| 672 |
} |
| 673 |
|
| 674 |
enum aws_mqtt5_unsuback_reason_code reason_code = AWS_MQTT5_UARC_SUCCESS; |
| 675 |
if (unsuback != NULL && unsuback->reason_code_count > 0) { |
| 676 |
reason_code = unsuback->reason_codes[0]; |
| 677 |
} |
| 678 |
|
| 679 |
bool is_retryable = s_is_retryable_unsubscribe5(reason_code, error_code); |
| 680 |
|
| 681 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 682 |
if (unsuback == NULL || unsuback->reason_code_count != 1 || unsuback->reason_codes[0] >= 128) { |
| 683 |
error_code = AWS_ERROR_MQTT_PROTOCOL_ADAPTER_FAILING_REASON_CODE; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
struct aws_protocol_adapter_subscription_event unsubscribe_event = { |
| 688 |
.topic_filter = aws_byte_cursor_from_buf(&unsubscribe_data->operation_data.sub_unsub_data.topic_filter), |
| 689 |
.event_type = AWS_PASET_UNSUBSCRIBE, |
| 690 |
.error_code = error_code, |
| 691 |
.retryable = is_retryable, |
| 692 |
}; |
| 693 |
|
| 694 |
(*adapter->config.subscription_event_callback)(&unsubscribe_event, adapter->config.user_data); |
| 695 |
|
| 696 |
done: |
| 697 |
|
| 698 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(unsubscribe_data); |
| 699 |
} |
| 700 |
|
| 701 |
int s_aws_mqtt_protocol_adapter_5_unsubscribe(void *impl, struct aws_protocol_adapter_unsubscribe_options *options) { |
| 702 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = impl; |
| 703 |
|
| 704 |
struct aws_mqtt_protocol_adapter_operation_userdata *unsubscribe_data = |
| 705 |
s_aws_mqtt_protocol_adapter_sub_unsub_data_new(adapter->allocator, options->topic_filter, adapter); |
| 706 |
|
| 707 |
aws_linked_list_push_back(&adapter->incomplete_operations, &unsubscribe_data->node); |
| 708 |
|
| 709 |
struct aws_mqtt5_packet_unsubscribe_view unsubscribe_view = { |
| 710 |
.topic_filters = &options->topic_filter, |
| 711 |
.topic_filter_count = 1, |
| 712 |
}; |
| 713 |
|
| 714 |
struct aws_mqtt5_unsubscribe_completion_options completion_options = { |
| 715 |
.ack_timeout_seconds_override = options->ack_timeout_seconds, |
| 716 |
.completion_callback = s_protocol_adapter_5_unsubscribe_completion, |
| 717 |
.completion_user_data = unsubscribe_data, |
| 718 |
}; |
| 719 |
|
| 720 |
if (aws_mqtt5_client_unsubscribe(adapter->client, &unsubscribe_view, &completion_options)) { |
| 721 |
goto error; |
| 722 |
} |
| 723 |
|
| 724 |
return AWS_OP_SUCCESS; |
| 725 |
|
| 726 |
error: |
| 727 |
|
| 728 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(unsubscribe_data); |
| 729 |
|
| 730 |
return AWS_OP_ERR; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
static void s_protocol_adapter_5_publish_completion( |
| 736 |
enum aws_mqtt5_packet_type packet_type, |
| 737 |
const void *packet, |
| 738 |
int error_code, |
| 739 |
void *complete_ctx) { |
| 740 |
struct aws_mqtt_protocol_adapter_operation_userdata *publish_data = complete_ctx; |
| 741 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = publish_data->adapter; |
| 742 |
|
| 743 |
if (adapter == NULL) { |
| 744 |
goto done; |
| 745 |
} |
| 746 |
|
| 747 |
if (error_code == AWS_ERROR_SUCCESS && packet_type == AWS_MQTT5_PT_PUBACK) { |
| 748 |
const struct aws_mqtt5_packet_puback_view *puback = packet; |
| 749 |
if (puback->reason_code >= 128) { |
| 750 |
error_code = AWS_ERROR_MQTT_PROTOCOL_ADAPTER_FAILING_REASON_CODE; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
(*publish_data->operation_data.publish_data.completion_callback_fn)( |
| 755 |
error_code, publish_data->operation_data.publish_data.user_data); |
| 756 |
|
| 757 |
done: |
| 758 |
|
| 759 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(publish_data); |
| 760 |
} |
| 761 |
|
| 762 |
int s_aws_mqtt_protocol_adapter_5_publish(void *impl, struct aws_protocol_adapter_publish_options *options) { |
| 763 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = impl; |
| 764 |
struct aws_mqtt_protocol_adapter_operation_userdata *publish_data = |
| 765 |
s_aws_mqtt_protocol_adapter_publish_data_new(adapter->allocator, options, adapter); |
| 766 |
|
| 767 |
aws_linked_list_push_back(&adapter->incomplete_operations, &publish_data->node); |
| 768 |
|
| 769 |
struct aws_mqtt5_packet_publish_view publish_view = { |
| 770 |
.topic = options->topic, .qos = AWS_MQTT5_QOS_AT_LEAST_ONCE, .payload = options->payload}; |
| 771 |
|
| 772 |
struct aws_mqtt5_publish_completion_options completion_options = { |
| 773 |
.ack_timeout_seconds_override = options->ack_timeout_seconds, |
| 774 |
.completion_callback = s_protocol_adapter_5_publish_completion, |
| 775 |
.completion_user_data = publish_data, |
| 776 |
}; |
| 777 |
|
| 778 |
if (aws_mqtt5_client_publish(adapter->client, &publish_view, &completion_options)) { |
| 779 |
goto error; |
| 780 |
} |
| 781 |
|
| 782 |
return AWS_OP_SUCCESS; |
| 783 |
|
| 784 |
error: |
| 785 |
|
| 786 |
s_aws_mqtt_protocol_adapter_operation_user_data_destroy(publish_data); |
| 787 |
|
| 788 |
return AWS_OP_ERR; |
| 789 |
} |
| 790 |
|
| 791 |
static bool s_aws_mqtt_protocol_adapter_5_is_connected(void *impl) { |
| 792 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = impl; |
| 793 |
|
| 794 |
AWS_FATAL_ASSERT(aws_event_loop_thread_is_callers_thread(adapter->client->loop)); |
| 795 |
|
| 796 |
enum aws_mqtt5_client_state current_state = adapter->client->current_state; |
| 797 |
|
| 798 |
return current_state == AWS_MCS_CONNECTED; |
| 799 |
} |
| 800 |
|
| 801 |
static bool s_protocol_adapter_mqtt5_listener_publish_received( |
| 802 |
const struct aws_mqtt5_packet_publish_view *publish, |
| 803 |
void *user_data) { |
| 804 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = user_data; |
| 805 |
|
| 806 |
struct aws_mqtt_rr_incoming_publish_event publish_event = { |
| 807 |
.topic = publish->topic, |
| 808 |
.payload = publish->payload, |
| 809 |
.content_type = publish->content_type, |
| 810 |
.user_property_count = publish->user_property_count, |
| 811 |
.user_properties = publish->user_properties, |
| 812 |
.message_expiry_interval_seconds = publish->message_expiry_interval_seconds, |
| 813 |
}; |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
(*adapter->config.incoming_publish_callback)(&publish_event, adapter->config.user_data); |
| 818 |
|
| 819 |
return false; |
| 820 |
} |
| 821 |
|
| 822 |
static void s_protocol_adapter_mqtt5_lifecycle_event_callback(const struct aws_mqtt5_client_lifecycle_event *event) { |
| 823 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = event->user_data; |
| 824 |
|
| 825 |
switch (event->event_type) { |
| 826 |
case AWS_MQTT5_CLET_CONNECTION_SUCCESS: { |
| 827 |
struct aws_protocol_adapter_connection_event connection_event = { |
| 828 |
.event_type = AWS_PACET_CONNECTED, |
| 829 |
.joined_session = event->settings->rejoined_session, |
| 830 |
}; |
| 831 |
|
| 832 |
(*adapter->config.connection_event_callback)(&connection_event, adapter->config.user_data); |
| 833 |
break; |
| 834 |
} |
| 835 |
case AWS_MQTT5_CLET_DISCONNECTION: { |
| 836 |
struct aws_protocol_adapter_connection_event connection_event = { |
| 837 |
.event_type = AWS_PACET_DISCONNECTED, |
| 838 |
}; |
| 839 |
|
| 840 |
(*adapter->config.connection_event_callback)(&connection_event, adapter->config.user_data); |
| 841 |
break; |
| 842 |
} |
| 843 |
default: |
| 844 |
break; |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
static void s_protocol_adapter_mqtt5_listener_termination_callback(void *user_data) { |
| 849 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = user_data; |
| 850 |
|
| 851 |
AWS_FATAL_ASSERT(aws_event_loop_thread_is_callers_thread(adapter->client->loop)); |
| 852 |
|
| 853 |
s_release_incomplete_operations(&adapter->incomplete_operations); |
| 854 |
|
| 855 |
aws_mqtt5_client_release(adapter->client); |
| 856 |
|
| 857 |
aws_protocol_adapter_terminate_callback_fn *terminate_callback = adapter->config.terminate_callback; |
| 858 |
void *terminate_user_data = adapter->config.user_data; |
| 859 |
|
| 860 |
aws_mem_release(adapter->allocator, adapter); |
| 861 |
|
| 862 |
if (terminate_callback) { |
| 863 |
(*terminate_callback)(terminate_user_data); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
static struct aws_event_loop *s_aws_mqtt_protocol_adapter_5_get_event_loop(void *impl) { |
| 868 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = impl; |
| 869 |
|
| 870 |
return adapter->loop; |
| 871 |
} |
| 872 |
|
| 873 |
static struct aws_mqtt_protocol_adapter_vtable s_protocol_adapter_mqtt5_vtable = { |
| 874 |
.aws_mqtt_protocol_adapter_destroy_fn = s_aws_mqtt_protocol_adapter_5_destroy, |
| 875 |
.aws_mqtt_protocol_adapter_subscribe_fn = s_aws_mqtt_protocol_adapter_5_subscribe, |
| 876 |
.aws_mqtt_protocol_adapter_unsubscribe_fn = s_aws_mqtt_protocol_adapter_5_unsubscribe, |
| 877 |
.aws_mqtt_protocol_adapter_publish_fn = s_aws_mqtt_protocol_adapter_5_publish, |
| 878 |
.aws_mqtt_protocol_adapter_is_connected_fn = s_aws_mqtt_protocol_adapter_5_is_connected, |
| 879 |
.aws_mqtt_protocol_adapter_get_event_loop_fn = s_aws_mqtt_protocol_adapter_5_get_event_loop}; |
| 880 |
|
| 881 |
struct aws_mqtt_protocol_adapter *aws_mqtt_protocol_adapter_new_from_5( |
| 882 |
struct aws_allocator *allocator, |
| 883 |
struct aws_mqtt_protocol_adapter_options *options, |
| 884 |
struct aws_mqtt5_client *client) { |
| 885 |
|
| 886 |
if (options == NULL || client == NULL) { |
| 887 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 888 |
return NULL; |
| 889 |
} |
| 890 |
|
| 891 |
struct aws_mqtt_protocol_adapter_5_impl *adapter = |
| 892 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_protocol_adapter_5_impl)); |
| 893 |
|
| 894 |
adapter->allocator = allocator; |
| 895 |
adapter->base.impl = adapter; |
| 896 |
adapter->base.vtable = &s_protocol_adapter_mqtt5_vtable; |
| 897 |
aws_linked_list_init(&adapter->incomplete_operations); |
| 898 |
adapter->config = *options; |
| 899 |
adapter->loop = client->loop; |
| 900 |
adapter->client = aws_mqtt5_client_acquire(client); |
| 901 |
|
| 902 |
struct aws_mqtt5_listener_config listener_options = { |
| 903 |
.client = client, |
| 904 |
.listener_callbacks = |
| 905 |
{ |
| 906 |
.listener_publish_received_handler = s_protocol_adapter_mqtt5_listener_publish_received, |
| 907 |
.listener_publish_received_handler_user_data = adapter, |
| 908 |
.lifecycle_event_handler = s_protocol_adapter_mqtt5_lifecycle_event_callback, |
| 909 |
.lifecycle_event_handler_user_data = adapter, |
| 910 |
}, |
| 911 |
.termination_callback = s_protocol_adapter_mqtt5_listener_termination_callback, |
| 912 |
.termination_callback_user_data = adapter, |
| 913 |
}; |
| 914 |
|
| 915 |
adapter->listener = aws_mqtt5_listener_new(allocator, &listener_options); |
| 916 |
|
| 917 |
return &adapter->base; |
| 918 |
} |
| 919 |
|
| 920 |
void aws_mqtt_protocol_adapter_destroy(struct aws_mqtt_protocol_adapter *adapter) { |
| 921 |
(*adapter->vtable->aws_mqtt_protocol_adapter_destroy_fn)(adapter->impl); |
| 922 |
} |
| 923 |
|
| 924 |
int aws_mqtt_protocol_adapter_subscribe( |
| 925 |
struct aws_mqtt_protocol_adapter *adapter, |
| 926 |
struct aws_protocol_adapter_subscribe_options *options) { |
| 927 |
return (*adapter->vtable->aws_mqtt_protocol_adapter_subscribe_fn)(adapter->impl, options); |
| 928 |
} |
| 929 |
|
| 930 |
int aws_mqtt_protocol_adapter_unsubscribe( |
| 931 |
struct aws_mqtt_protocol_adapter *adapter, |
| 932 |
struct aws_protocol_adapter_unsubscribe_options *options) { |
| 933 |
return (*adapter->vtable->aws_mqtt_protocol_adapter_unsubscribe_fn)(adapter->impl, options); |
| 934 |
} |
| 935 |
|
| 936 |
int aws_mqtt_protocol_adapter_publish( |
| 937 |
struct aws_mqtt_protocol_adapter *adapter, |
| 938 |
struct aws_protocol_adapter_publish_options *options) { |
| 939 |
return (*adapter->vtable->aws_mqtt_protocol_adapter_publish_fn)(adapter->impl, options); |
| 940 |
} |
| 941 |
|
| 942 |
bool aws_mqtt_protocol_adapter_is_connected(struct aws_mqtt_protocol_adapter *adapter) { |
| 943 |
return (*adapter->vtable->aws_mqtt_protocol_adapter_is_connected_fn)(adapter->impl); |
| 944 |
} |
| 945 |
|
| 946 |
struct aws_event_loop *aws_mqtt_protocol_adapter_get_event_loop(struct aws_mqtt_protocol_adapter *adapter) { |
| 947 |
return (*adapter->vtable->aws_mqtt_protocol_adapter_get_event_loop_fn)(adapter->impl); |
| 948 |
} |
| 949 |
|
| 950 |
const char *aws_protocol_adapter_subscription_event_type_to_c_str( |
| 951 |
enum aws_protocol_adapter_subscription_event_type type) { |
| 952 |
switch (type) { |
| 953 |
case AWS_PASET_SUBSCRIBE: |
| 954 |
return "Subscribe"; |
| 955 |
|
| 956 |
case AWS_PASET_UNSUBSCRIBE: |
| 957 |
return "Unsubscribe"; |
| 958 |
} |
| 959 |
|
| 960 |
return "Unknown"; |
| 961 |
} |
| 962 |
|
| 963 |
const char *aws_protocol_adapter_connection_event_type_to_c_str(enum aws_protocol_adapter_connection_event_type type) { |
| 964 |
switch (type) { |
| 965 |
case AWS_PACET_CONNECTED: |
| 966 |
return "Connected"; |
| 967 |
|
| 968 |
case AWS_PACET_DISCONNECTED: |
| 969 |
return "Disconnected"; |
| 970 |
} |
| 971 |
|
| 972 |
return "Unknown"; |
| 973 |
} |