Skip to content

Commit b1dc99d

Browse files
Thalleynashif
authored andcommitted
Bluetooth: TBS: Return empty string instead of NULL for empty data
If we read an empty GATT characteristic that should contain a string value, then we provide the empty string, instead of NULL, to be true to the API. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 2aee9e3 commit b1dc99d

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

subsys/bluetooth/audio/tbs_client.c

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ static uint8_t handle_string_long_read(struct bt_conn *conn, uint8_t err,
744744
uint16_t offset = params->single.offset;
745745
uint8_t inst_index = tbs_index(conn, inst);
746746
const char *received_string;
747+
uint16_t str_length;
747748
int tbs_err = err;
748749

749750
if ((tbs_err == 0) && (data != NULL) &&
@@ -779,44 +780,38 @@ static uint8_t handle_string_long_read(struct bt_conn *conn, uint8_t err,
779780
return BT_GATT_ITER_CONTINUE;
780781
}
781782

782-
if (inst->net_buf.len == 0) {
783-
received_string = NULL;
784-
} else {
785-
uint16_t str_length = inst->net_buf.len;
783+
str_length = inst->net_buf.len;
786784

787-
/* Ensure there is space for string termination */
788-
if (net_buf_simple_tailroom(&inst->net_buf) < 1) {
789-
LOG_DBG("Truncating string");
790-
if (truncatable) {
791-
/* Truncate */
792-
str_length--;
793-
} else {
794-
tbs_err = BT_ATT_ERR_INSUFFICIENT_RESOURCES;
795-
}
785+
/* Ensure there is space for string termination */
786+
if (net_buf_simple_tailroom(&inst->net_buf) < 1) {
787+
LOG_DBG("Truncating string");
788+
if (truncatable) {
789+
/* Truncate */
790+
str_length--;
791+
} else {
792+
tbs_err = BT_ATT_ERR_INSUFFICIENT_RESOURCES;
796793
}
794+
}
797795

798-
if (tbs_err == 0) {
799-
char *str_data;
796+
if (tbs_err == 0) {
797+
char *str_data;
800798

801-
/* Get a reference to the string buffer */
802-
str_data = net_buf_simple_pull_mem(&inst->net_buf,
803-
inst->net_buf.len);
799+
/* Get a reference to the string buffer */
800+
str_data = net_buf_simple_pull_mem(&inst->net_buf, inst->net_buf.len);
804801

805-
/* All strings are UTF-8, truncate properly if needed */
806-
str_data[str_length] = '\0';
807-
received_string = utf8_trunc(str_data);
802+
/* All strings are UTF-8, truncate properly if needed */
803+
str_data[str_length] = '\0';
804+
received_string = utf8_trunc(str_data);
808805

809-
/* The string might have been truncated */
810-
if (strlen(received_string) < str_length) {
811-
LOG_DBG("Truncating string");
812-
if (!truncatable) {
813-
tbs_err =
814-
BT_ATT_ERR_INSUFFICIENT_RESOURCES;
815-
}
806+
/* The string might have been truncated */
807+
if (strlen(received_string) < str_length) {
808+
LOG_DBG("Truncating string");
809+
if (!truncatable) {
810+
tbs_err = BT_ATT_ERR_INSUFFICIENT_RESOURCES;
816811
}
817-
818-
LOG_DBG("%s", received_string);
819812
}
813+
814+
LOG_DBG("%s", received_string);
820815
}
821816

822817
if (tbs_err) {
@@ -950,7 +945,7 @@ static uint8_t read_uri_list_cb(struct bt_conn *conn, uint8_t err,
950945
{
951946
bt_tbs_client_read_string_cb cb = NULL;
952947

953-
LOG_DBG("Read bearer UCI");
948+
LOG_DBG("Read bearer URI list");
954949

955950
if (tbs_client_cbs != NULL && tbs_client_cbs->uri_list != NULL) {
956951
cb = tbs_client_cbs->uri_list;

0 commit comments

Comments
 (0)