Skip to content
Browse files

Bluetooth: shell: Make show-db print the total number of attributes

In addition to just print each attribute this also include the total
number of attribute and how much the database consumes:

uart:~$ gatt show-db
attr 0x00423460 handle 0x0001 uuid 2800 perm 0x01
attr 0x00423474 handle 0x0002 uuid 2803 perm 0x01
attr 0x00423488 handle 0x0003 uuid 2a05 perm 0x00
attr 0x0042349c handle 0x0004 uuid 2902 perm 0x03
attr 0x004234b0 handle 0x0005 uuid 2803 perm 0x01
attr 0x004234c4 handle 0x0006 uuid 2b29 perm 0x03
attr 0x004234d8 handle 0x0007 uuid 2803 perm 0x01
attr 0x004234ec handle 0x0008 uuid 2b2a perm 0x01
attr 0x00423580 handle 0x0009 uuid 2800 perm 0x01
attr 0x00423594 handle 0x000a uuid 2803 perm 0x01
attr 0x004235a8 handle 0x000b uuid 2a00 perm 0x09
attr 0x004235bc handle 0x000c uuid 2803 perm 0x01
attr 0x004235d0 handle 0x000d uuid 2a01 perm 0x01
attr 0x004235e4 handle 0x000e uuid 2803 perm 0x01
attr 0x004235f8 handle 0x000f uuid 2aa6 perm 0x01
attr 0x0042360c handle 0x0010 uuid 2803 perm 0x01
attr 0x00423620 handle 0x0011 uuid 2a04 perm 0x01
Total: 2 services 17 attributes (476 bytes)

Signed-off-by: Luiz Augusto von Dentz <>
  • Loading branch information...
Vudentz authored and nashif committed Mar 18, 2019
1 parent 09c06a1 commit 26fea23435f29a41f6e9ad3afcef209907cc71e5
Showing with 47 additions and 1 deletion.
  1. +47 −1 subsys/bluetooth/shell/gatt.c
@@ -478,19 +478,65 @@ static int cmd_unsubscribe(const struct shell *shell,

static struct db_stats {
u16_t svc_count;
u16_t attr_count;
u16_t chrc_count;
u16_t ccc_count;
size_t ccc_cfg;
} stats;

static u8_t print_attr(const struct bt_gatt_attr *attr, void *user_data)
const struct shell *shell = user_data;


if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_PRIMARY) ||
!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_SECONDARY)) {

if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_CHRC)) {

if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_CCC) &&
attr->write == bt_gatt_attr_write_ccc) {
struct _bt_gatt_ccc *cfg = attr->user_data;

stats.ccc_cfg += cfg->cfg_len;

shell_print(shell, "attr %p handle 0x%04x uuid %s perm 0x%02x",
attr, attr->handle, bt_uuid_str(attr->uuid), attr->perm);
attr, attr->handle, bt_uuid_str(attr->uuid), attr->perm);


static int cmd_show_db(const struct shell *shell, size_t argc, char *argv[])
size_t total_len;

memset(&stats, 0, sizeof(stats));

bt_gatt_foreach_attr(0x0001, 0xffff, print_attr, (void *)shell);

if (!stats.attr_count) {
return 0;

total_len = stats.svc_count * sizeof(struct bt_gatt_service);
total_len += stats.chrc_count * sizeof(struct bt_gatt_chrc);
total_len += stats.attr_count * sizeof(struct bt_gatt_attr);
total_len += stats.ccc_count * sizeof(struct _bt_gatt_ccc);
total_len += stats.ccc_cfg * sizeof(struct bt_gatt_ccc_cfg);

shell_print(shell, "=================================================");
shell_print(shell, "Total: %u services %u attributes (%u bytes)",
stats.svc_count, stats.attr_count, total_len);

return 0;

0 comments on commit 26fea23

Please sign in to comment.
You can’t perform that action at this time.