From a86443655900239b84881944b64fa439989f1788 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Mon, 10 Jul 2017 14:55:39 +0300 Subject: [PATCH] Bluetooth: Shell: Add second vendor service This adds a second vendor service testing if service changed indications works with more than one change in a row and as a bonus it implements echo attribute which notifies any data that is written to it: > ACL Data TX: Handle 3585 flags 0x00 dlen 9 ATT: Write Command (0x52) len 4 Handle: 0x0013 Data: 0000 < ACL Data RX: Handle 3585 flags 0x02 dlen 9 ATT: Handle Value Notification (0x1b) len 4 Handle: 0x0013 Data: 0000 Signed-off-by: Luiz Augusto von Dentz --- subsys/bluetooth/shell/gatt.c | 48 +++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/subsys/bluetooth/shell/gatt.c b/subsys/bluetooth/shell/gatt.c index 6f5bf478d47..a3243ceff09 100644 --- a/subsys/bluetooth/shell/gatt.c +++ b/subsys/bluetooth/shell/gatt.c @@ -514,6 +514,34 @@ static const struct bt_uuid_128 vnd_long_uuid2 = BT_UUID_INIT_128( static u8_t vnd_value[] = { 'V', 'e', 'n', 'd', 'o', 'r' }; +static struct bt_uuid_128 vnd1_uuid = BT_UUID_INIT_128( + 0xf4, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); + +static const struct bt_uuid_128 vnd1_echo_uuid = BT_UUID_INIT_128( + 0xf5, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, + 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); + +static struct bt_gatt_ccc_cfg vnd1_ccc_cfg[BT_GATT_CCC_MAX] = {}; +static u8_t echo_enabled; + +static void vnd1_ccc_cfg_changed(const struct bt_gatt_attr *attr, u16_t value) +{ + echo_enabled = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0; +} + +static ssize_t write_vnd1(struct bt_conn *conn, const struct bt_gatt_attr *attr, + const void *buf, u16_t len, u16_t offset, + u8_t flags) +{ + if (echo_enabled) { + printk("Echo attr len %u\n", len); + bt_gatt_notify(conn, attr, buf, len); + } + + return len; +} + static ssize_t read_vnd(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { @@ -602,11 +630,26 @@ static struct bt_gatt_attr vnd_attrs[] = { static struct bt_gatt_service vnd_svc = BT_GATT_SERVICE(vnd_attrs); +static struct bt_gatt_attr vnd1_attrs[] = { + /* Vendor Primary Service Declaration */ + BT_GATT_PRIMARY_SERVICE(&vnd1_uuid), + + BT_GATT_CHARACTERISTIC(&vnd1_echo_uuid.uuid, + BT_GATT_CHRC_WRITE_WITHOUT_RESP | + BT_GATT_CHRC_NOTIFY), + BT_GATT_DESCRIPTOR(&vnd1_echo_uuid.uuid, + BT_GATT_PERM_WRITE, NULL, write_vnd1, NULL), + BT_GATT_CCC(vnd1_ccc_cfg, vnd1_ccc_cfg_changed), +}; + +static struct bt_gatt_service vnd1_svc = BT_GATT_SERVICE(vnd1_attrs); + int cmd_gatt_register_test_svc(int argc, char *argv[]) { bt_gatt_service_register(&vnd_svc); + bt_gatt_service_register(&vnd1_svc); - printk("Registering test vendor service\n"); + printk("Registering test vendor services\n"); return 0; } @@ -614,8 +657,9 @@ int cmd_gatt_register_test_svc(int argc, char *argv[]) int cmd_gatt_unregister_test_svc(int argc, char *argv[]) { bt_gatt_service_unregister(&vnd_svc); + bt_gatt_service_unregister(&vnd1_svc); - printk("Unregistering test vendor service\n"); + printk("Unregistering test vendor services\n"); return 0; }