Skip to content

Commit 55dad8a

Browse files
committed
add aws_mqtt_has_non_empty_username
1 parent 78d36ec commit 55dad8a

5 files changed

Lines changed: 32 additions & 9 deletions

File tree

include/aws/mqtt/private/mqtt_iot_metrics.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,17 @@ int aws_mqtt_append_sdk_metrics_to_username(
5656
AWS_MQTT_API
5757
int aws_mqtt_validate_iot_metrics(const struct aws_mqtt_iot_metrics *metrics);
5858

59+
/**
60+
* Checks if username should be included in the CONNECT packet.
61+
* Returns true if either username is provided or metrics are configured.
62+
*
63+
* @param username The username cursor (can be NULL)
64+
* @param metrics_storage The metrics storage (can be NULL)
65+
* @return true if username should be included, false otherwise
66+
*/
67+
AWS_MQTT_API
68+
bool aws_mqtt_has_non_empty_username(
69+
const struct aws_byte_cursor *username,
70+
const struct aws_mqtt_iot_metrics_storage *metrics_storage);
71+
5972
#endif /* AWS_MQTT_IOT_METRICS_H */

source/client.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ static void s_mqtt_client_init(
635635
&connect, topic_cur, connection->will.qos, connection->will.retain, payload_cur);
636636
}
637637

638-
if (connection->username || connection->metrics_storage) {
638+
if (aws_mqtt_has_non_empty_username(connection->username, connection->metrics_storage)) {
639639
struct aws_byte_cursor username_cur;
640640
AWS_ZERO_STRUCT(username_cur);
641641
if (connection->username) {
@@ -2083,7 +2083,7 @@ static enum aws_mqtt_client_request_state s_subscribe_send(uint16_t packet_id, b
20832083
return AWS_MQTT_CLIENT_REQUEST_ERROR;
20842084
}
20852085

2086-
AWS_VARIABLE_LENGTH_ARRAY(uint8_t, transaction_buf, num_topics * aws_mqtt_topic_tree_action_size);
2086+
AWS_VARIABLE_LENGTH_ARRAY(uint8_t, transaction_buf, num_topics *aws_mqtt_topic_tree_action_size);
20872087
struct aws_array_list transaction;
20882088
aws_array_list_init_static(&transaction, transaction_buf, num_topics, aws_mqtt_topic_tree_action_size);
20892089

@@ -2876,7 +2876,7 @@ static enum aws_mqtt_client_request_state s_unsubscribe_send(
28762876

28772877
static const size_t num_topics = 1;
28782878

2879-
AWS_VARIABLE_LENGTH_ARRAY(uint8_t, transaction_buf, num_topics * aws_mqtt_topic_tree_action_size);
2879+
AWS_VARIABLE_LENGTH_ARRAY(uint8_t, transaction_buf, num_topics *aws_mqtt_topic_tree_action_size);
28802880
struct aws_array_list transaction;
28812881
aws_array_list_init_static(&transaction, transaction_buf, num_topics, aws_mqtt_topic_tree_action_size);
28822882

source/mqtt_iot_metrics.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,9 @@ int aws_mqtt_validate_iot_metrics(const struct aws_mqtt_iot_metrics *metrics) {
430430

431431
return AWS_OP_SUCCESS;
432432
}
433+
434+
bool aws_mqtt_has_non_empty_username(
435+
const struct aws_byte_cursor *username,
436+
const struct aws_mqtt_iot_metrics_storage *metrics_storage) {
437+
return (username != NULL && username->len > 0) || metrics_storage != NULL;
438+
}

source/v5/mqtt5_options_storage.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ static size_t s_aws_mqtt5_packet_connect_compute_storage_size(
646646

647647
storage_size += view->client_id.len;
648648

649-
if (view->username != NULL || (options && options->metrics_storage != NULL)) {
649+
if (aws_mqtt_has_non_empty_username(view->username, options ? options->metrics_storage : NULL)) {
650650

651651
if (options) {
652652
size_t username_size = 0;
@@ -701,7 +701,8 @@ int aws_mqtt5_packet_connect_storage_init(
701701
return AWS_OP_ERR;
702702
}
703703

704-
if (view->username != NULL || (client_options_storage && client_options_storage->metrics_storage != NULL)) {
704+
if (aws_mqtt_has_non_empty_username(
705+
view->username, client_options_storage ? client_options_storage->metrics_storage : NULL)) {
705706
if (view->username) {
706707
storage->username = *view->username;
707708
}

tests/v5/mqtt5_to_mqtt3_adapter_tests.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,7 +4168,8 @@ static int s_mqtt5to3_adapter_resubscribe_nothing_fn(struct aws_allocator *alloc
41684168
s_wait_for_n_adapter_operation_events(&fixture, AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, 1);
41694169

41704170
struct aws_mqtt3_operation_event resubscribe_ack[] = {{
4171-
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE, .error_code = AWS_ERROR_MQTT_CONNECTION_RESUBSCRIBE_NO_TOPICS,
4171+
.type = AWS_MQTT3_OET_SUBSCRIBE_COMPLETE,
4172+
.error_code = AWS_ERROR_MQTT_CONNECTION_RESUBSCRIBE_NO_TOPICS,
41724173
/* no granted subscriptions */
41734174
}};
41744175

@@ -4512,9 +4513,11 @@ static int s_mqtt5to3_adapter_connection_set_metrics_invalid_utf8_library_fn(
45124513
/* Invalid UTF-8 sequence */
45134514
struct aws_byte_cursor invalid_utf8_library = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("TestSDK\xFF\xFE");
45144515

4515-
struct aws_mqtt_metadata_entry metadata_entries[] = {
4516-
{.key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("key1\xFF\xFE"),
4517-
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("value1\xFF\xFE")}};
4516+
struct aws_mqtt_metadata_entry metadata_entries[] = {{
4517+
.key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("key1\xFF\xFE"),
4518+
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("value1\xFF\xFE"),
4519+
}};
4520+
45184521
struct aws_mqtt_iot_metrics metrics = {
45194522
.library_name = invalid_utf8_library,
45204523
.metadata_count = AWS_ARRAY_SIZE(metadata_entries),

0 commit comments

Comments
 (0)