Skip to content

Commit 26fea23

Browse files
Vudentznashif
authored andcommitted
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 <[email protected]>
1 parent 09c06a1 commit 26fea23

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

subsys/bluetooth/shell/gatt.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,65 @@ static int cmd_unsubscribe(const struct shell *shell,
478478
}
479479
#endif /* CONFIG_BT_GATT_CLIENT */
480480

481+
static struct db_stats {
482+
u16_t svc_count;
483+
u16_t attr_count;
484+
u16_t chrc_count;
485+
u16_t ccc_count;
486+
size_t ccc_cfg;
487+
} stats;
488+
481489
static u8_t print_attr(const struct bt_gatt_attr *attr, void *user_data)
482490
{
483491
const struct shell *shell = user_data;
484492

493+
stats.attr_count++;
494+
495+
if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_PRIMARY) ||
496+
!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_SECONDARY)) {
497+
stats.svc_count++;
498+
}
499+
500+
if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_CHRC)) {
501+
stats.chrc_count++;
502+
}
503+
504+
if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_CCC) &&
505+
attr->write == bt_gatt_attr_write_ccc) {
506+
struct _bt_gatt_ccc *cfg = attr->user_data;
507+
508+
stats.ccc_count++;
509+
stats.ccc_cfg += cfg->cfg_len;
510+
}
511+
485512
shell_print(shell, "attr %p handle 0x%04x uuid %s perm 0x%02x",
486-
attr, attr->handle, bt_uuid_str(attr->uuid), attr->perm);
513+
attr, attr->handle, bt_uuid_str(attr->uuid), attr->perm);
487514

488515
return BT_GATT_ITER_CONTINUE;
489516
}
490517

491518
static int cmd_show_db(const struct shell *shell, size_t argc, char *argv[])
492519
{
520+
size_t total_len;
521+
522+
memset(&stats, 0, sizeof(stats));
523+
493524
bt_gatt_foreach_attr(0x0001, 0xffff, print_attr, (void *)shell);
525+
526+
if (!stats.attr_count) {
527+
return 0;
528+
}
529+
530+
total_len = stats.svc_count * sizeof(struct bt_gatt_service);
531+
total_len += stats.chrc_count * sizeof(struct bt_gatt_chrc);
532+
total_len += stats.attr_count * sizeof(struct bt_gatt_attr);
533+
total_len += stats.ccc_count * sizeof(struct _bt_gatt_ccc);
534+
total_len += stats.ccc_cfg * sizeof(struct bt_gatt_ccc_cfg);
535+
536+
shell_print(shell, "=================================================");
537+
shell_print(shell, "Total: %u services %u attributes (%u bytes)",
538+
stats.svc_count, stats.attr_count, total_len);
539+
494540
return 0;
495541
}
496542

0 commit comments

Comments
 (0)