Skip to content

Commit b30b480

Browse files
joerchanjhedberg
authored andcommitted
Bluetooth: UUID: Expose bt_uuid_to_str to application
Expose the bt_uuid_to_str function as an API to the application. This aligns this function with the bt_addr_to_str function call. This allows the application to use this function without having to enable the BT_DEBUG option. Move the in-place bt_uuid_str to internal logging, this is mainly done due to the limitation in the log_strdup that shouldn't be exposed to the application. Signed-off-by: Joakim Andersson <[email protected]>
1 parent 87e917a commit b30b480

File tree

6 files changed

+40
-49
lines changed

6 files changed

+40
-49
lines changed

include/bluetooth/uuid.h

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ struct bt_uuid_128 {
7979
#define BT_UUID_32(__u) CONTAINER_OF(__u, struct bt_uuid_32, uuid)
8080
#define BT_UUID_128(__u) CONTAINER_OF(__u, struct bt_uuid_128, uuid)
8181

82-
8382
/**
8483
* @brief Encode 128 bit UUID into an array values
8584
*
@@ -124,6 +123,16 @@ struct bt_uuid_128 {
124123
(((w32) >> 16) & 0xFF), \
125124
(((w32) >> 24) & 0xFF)
126125

126+
/** @def BT_UUID_STR_LEN
127+
*
128+
* @brief Recommended length of user string buffer for Bluetooth UUID.
129+
*
130+
* @details The recommended length guarantee the output of UUID
131+
* conversion will not lose valuable information about the UUID being
132+
* processed. If the length of the UUID is known the string can be shorter.
133+
*/
134+
#define BT_UUID_STR_LEN 37
135+
127136
/** @def BT_UUID_GAP
128137
* @brief Generic Access
129138
*/
@@ -524,11 +533,10 @@ int bt_uuid_cmp(const struct bt_uuid *u1, const struct bt_uuid *u2);
524533
*/
525534
bool bt_uuid_create(struct bt_uuid *uuid, const u8_t *data, u8_t data_len);
526535

527-
#if defined(CONFIG_BT_DEBUG)
528536
/** @brief Convert Bluetooth UUID to string.
529537
*
530-
* Converts Bluetooth UUID to string. UUID has to be in 16 bits or 128 bits
531-
* format.
538+
* Converts Bluetooth UUID to string.
539+
* UUID can be in any format, 16-bit, 32-bit or 128-bit.
532540
*
533541
* @param uuid Bluetooth UUID
534542
* @param str pointer where to put converted string
@@ -538,34 +546,6 @@ bool bt_uuid_create(struct bt_uuid *uuid, const u8_t *data, u8_t data_len);
538546
*/
539547
void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len);
540548

541-
const char *bt_uuid_str_real(const struct bt_uuid *uuid);
542-
543-
/** @def bt_uuid_str
544-
* @brief Convert Bluetooth UUID to string in place.
545-
*
546-
* Converts Bluetooth UUID to string in place. UUID has to be in 16 bits or
547-
* 128 bits format.
548-
*
549-
* @param uuid Bluetooth UUID
550-
*
551-
* @return String representation of the UUID given
552-
*/
553-
#define bt_uuid_str(_uuid) log_strdup(bt_uuid_str_real(_uuid))
554-
#else
555-
static inline void bt_uuid_to_str(const struct bt_uuid *uuid, char *str,
556-
size_t len)
557-
{
558-
if (len > 0) {
559-
str[0] = '\0';
560-
}
561-
}
562-
563-
static inline const char *bt_uuid_str(const struct bt_uuid *uuid)
564-
{
565-
return "";
566-
}
567-
#endif /* CONFIG_BT_DEBUG */
568-
569549
#ifdef __cplusplus
570550
}
571551
#endif

subsys/bluetooth/common/log.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <zephyr.h>
1818
#include <sys/util.h>
1919
#include <bluetooth/bluetooth.h>
20+
#include <bluetooth/uuid.h>
2021
#include <bluetooth/hci.h>
2122

2223
const char *bt_hex_real(const void *buf, size_t len)
@@ -55,3 +56,12 @@ const char *bt_addr_le_str_real(const bt_addr_le_t *addr)
5556

5657
return str;
5758
}
59+
60+
const char *bt_uuid_str_real(const struct bt_uuid *uuid)
61+
{
62+
static char str[BT_UUID_STR_LEN];
63+
64+
bt_uuid_to_str(uuid, str, sizeof(str));
65+
66+
return str;
67+
}

subsys/bluetooth/common/log.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <logging/log.h>
1818

1919
#include <bluetooth/bluetooth.h>
20+
#include <bluetooth/uuid.h>
2021
#include <bluetooth/hci.h>
2122

2223
#ifdef __cplusplus
@@ -68,10 +69,13 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL);
6869
/* NOTE: These helper functions always encodes into the same buffer storage.
6970
* It is the responsibility of the user of this function to copy the information
7071
* in this string if needed.
72+
*
73+
* NOTE: These functions are not thread-safe!
7174
*/
7275
const char *bt_hex_real(const void *buf, size_t len);
7376
const char *bt_addr_str_real(const bt_addr_t *addr);
7477
const char *bt_addr_le_str_real(const bt_addr_le_t *addr);
78+
const char *bt_uuid_str_real(const struct bt_uuid *uuid);
7579

7680
/* NOTE: log_strdup does not guarantee a duplication of the string.
7781
* It is therefore still the responsibility of the user to handle the
@@ -80,6 +84,7 @@ const char *bt_addr_le_str_real(const bt_addr_le_t *addr);
8084
#define bt_hex(buf, len) log_strdup(bt_hex_real(buf, len))
8185
#define bt_addr_str(addr) log_strdup(bt_addr_str_real(addr))
8286
#define bt_addr_le_str(addr) log_strdup(bt_addr_le_str_real(addr))
87+
#define bt_uuid_str(uuid) log_strdup(bt_uuid_str_real(uuid))
8388

8489
#ifdef __cplusplus
8590
}

subsys/bluetooth/host/uuid.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ bool bt_uuid_create(struct bt_uuid *uuid, const u8_t *data, u8_t data_len)
9898
return true;
9999
}
100100

101-
#if defined(CONFIG_BT_DEBUG)
102101
void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len)
103102
{
104103
u32_t tmp1, tmp5;
@@ -109,7 +108,7 @@ void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len)
109108
snprintk(str, len, "%04x", BT_UUID_16(uuid)->val);
110109
break;
111110
case BT_UUID_TYPE_32:
112-
snprintk(str, len, "%04x", BT_UUID_32(uuid)->val);
111+
snprintk(str, len, "%08x", BT_UUID_32(uuid)->val);
113112
break;
114113
case BT_UUID_TYPE_128:
115114
memcpy(&tmp0, &BT_UUID_128(uuid)->val[0], sizeof(tmp0));
@@ -127,13 +126,3 @@ void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len)
127126
return;
128127
}
129128
}
130-
131-
const char *bt_uuid_str_real(const struct bt_uuid *uuid)
132-
{
133-
static char str[37];
134-
135-
bt_uuid_to_str(uuid, str, sizeof(str));
136-
137-
return str;
138-
}
139-
#endif /* CONFIG_BT_DEBUG */

subsys/bluetooth/shell/gatt.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static u8_t discover_func(struct bt_conn *conn,
115115
struct bt_gatt_service_val *gatt_service;
116116
struct bt_gatt_chrc *gatt_chrc;
117117
struct bt_gatt_include *gatt_include;
118-
char str[37];
118+
char str[BT_UUID_STR_LEN];
119119

120120
if (!attr) {
121121
shell_print(ctx_shell, "Discover complete");
@@ -555,6 +555,7 @@ static struct db_stats {
555555
static u8_t print_attr(const struct bt_gatt_attr *attr, void *user_data)
556556
{
557557
const struct shell *shell = user_data;
558+
char str[BT_UUID_STR_LEN];
558559

559560
stats.attr_count++;
560561

@@ -572,8 +573,9 @@ static u8_t print_attr(const struct bt_gatt_attr *attr, void *user_data)
572573
stats.ccc_count++;
573574
}
574575

576+
bt_uuid_to_str(attr->uuid, str, sizeof(str));
575577
shell_print(shell, "attr %p handle 0x%04x uuid %s perm 0x%02x",
576-
attr, attr->handle, bt_uuid_str(attr->uuid), attr->perm);
578+
attr, attr->handle, str, attr->perm);
577579

578580
return BT_GATT_ITER_CONTINUE;
579581
}
@@ -926,9 +928,11 @@ static u8_t get_cb(const struct bt_gatt_attr *attr, void *user_data)
926928
struct shell *shell = user_data;
927929
u8_t buf[256];
928930
ssize_t ret;
931+
char str[BT_UUID_STR_LEN];
929932

930-
shell_print(shell, "attr %p uuid %s perm 0x%02x", attr,
931-
bt_uuid_str(attr->uuid), attr->perm);
933+
bt_uuid_to_str(attr->uuid, str, sizeof(str));
934+
shell_print(shell, "attr %p uuid %s perm 0x%02x", attr, str,
935+
attr->perm);
932936

933937
if (!attr->read) {
934938
return BT_GATT_ITER_CONTINUE;

tests/bluetooth/tester/src/gatt.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,12 +1850,15 @@ static void get_attrs(u8_t *data, u16_t len)
18501850
end_handle = sys_le16_to_cpu(cmd->end_handle);
18511851

18521852
if (cmd->type_length) {
1853+
char uuid_str[BT_UUID_STR_LEN];
1854+
18531855
if (btp2bt_uuid(cmd->type, cmd->type_length, &uuid.uuid)) {
18541856
goto fail;
18551857
}
18561858

1859+
bt_uuid_to_str(&uuid.uuid, uuid_str, sizeof(uuid_str));
18571860
LOG_DBG("start 0x%04x end 0x%04x, uuid %s", start_handle,
1858-
end_handle, bt_uuid_str(&uuid.uuid));
1861+
end_handle, uuid_str);
18591862

18601863
foreach.uuid = &uuid.uuid;
18611864
} else {

0 commit comments

Comments
 (0)