Diff between e557ec343d8bcce44cd9aa02a7f867c7624b390c and ed6a37b3503e069655270b978b874605b9a3d116

Changed Files

File Additions Deletions Status
tools/btgatt-server.c +58 -2 modified

Full Patch

diff --git a/tools/btgatt-server.c b/tools/btgatt-server.c
index f661b12..2b89be1 100644
--- a/tools/btgatt-server.c
+++ b/tools/btgatt-server.c
@@ -83,8 +83,10 @@ struct server {
 	uint8_t *device_name;
 	size_t name_len;
 
+	uint16_t gatt_svc_chngd_handle;
 	bool svc_chngd_enabled;
 
+	uint16_t hr_handle;
 	uint16_t hr_msrmt_handle;
 	uint16_t hr_energy_expended;
 	bool hr_visible;
@@ -445,17 +447,19 @@ static void populate_gap_service(struct server *server)
 static void populate_gatt_service(struct server *server)
 {
 	bt_uuid_t uuid;
-	struct gatt_db_attribute *service;
+	struct gatt_db_attribute *service, *svc_chngd;
 
 	/* Add the GATT service */
 	bt_uuid16_create(&uuid, UUID_GATT);
 	service = gatt_db_add_service(server->db, &uuid, true, 4);
 
 	bt_uuid16_create(&uuid, GATT_CHARAC_SERVICE_CHANGED);
-	gatt_db_service_add_characteristic(service, &uuid, BT_ATT_PERM_READ,
+	svc_chngd = gatt_db_service_add_characteristic(service, &uuid,
+			BT_ATT_PERM_READ,
 			BT_GATT_CHRC_PROP_READ | BT_GATT_CHRC_PROP_INDICATE,
 			gatt_service_changed_cb,
 			NULL, server);
+	server->gatt_svc_chngd_handle = gatt_db_attribute_get_handle(svc_chngd);
 
 	bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID);
 	gatt_db_service_add_descriptor(service, &uuid,
@@ -475,6 +479,7 @@ static void populate_hr_service(struct server *server)
 	/* Add Heart Rate Service */
 	bt_uuid16_create(&uuid, UUID_HEART_RATE);
 	service = gatt_db_add_service(server->db, &uuid, true, 8);
+	server->hr_handle = gatt_db_attribute_get_handle(service);
 
 	/* HR Measurement Characteristic */
 	bt_uuid16_create(&uuid, UUID_HEART_RATE_MSRMT);
@@ -820,6 +825,56 @@ done:
 	free(value);
 }
 
+static void heart_rate_usage(void)
+{
+	printf("Usage: heart-rate on|off\n");
+}
+
+static void cmd_heart_rate(struct server *server, char *cmd_str)
+{
+	bool enable;
+	uint8_t pdu[4];
+	struct gatt_db_attribute *attr;
+
+	if (!cmd_str) {
+		heart_rate_usage();
+		return;
+	}
+
+	if (strcmp(cmd_str, "on") == 0)
+		enable = true;
+	else if (strcmp(cmd_str, "off") == 0)
+		enable = false;
+	else {
+		heart_rate_usage();
+		return;
+	}
+
+	if (enable == server->hr_visible) {
+		printf("Heart Rate Service already %s\n",
+						enable ? "visible" : "hidden");
+		return;
+	}
+
+	server->hr_visible = enable;
+	attr = gatt_db_get_attribute(server->db, server->hr_handle);
+	gatt_db_service_set_active(attr, server->hr_visible);
+	update_hr_msrmt_simulation(server);
+
+	if (!server->svc_chngd_enabled)
+		return;
+
+	put_le16(server->hr_handle, pdu);
+	put_le16(server->hr_handle + 7, pdu + 2);
+
+	server->hr_msrmt_enabled = false;
+	update_hr_msrmt_simulation(server);
+
+	bt_gatt_server_send_indication(server->gatt,
+						server->gatt_svc_chngd_handle,
+						pdu, 4, conf_cb, NULL, NULL);
+}
+
 static void cmd_help(struct server *server, char *cmd_str);
 
 typedef void (*command_func_t)(struct server *server, char *cmd_str);
@@ -831,6 +886,7 @@ static struct {
 } command[] = {
 	{ "help", cmd_help, "\tDisplay help message" },
 	{ "notify", cmd_notify, "\tSend handle-value notification" },
+	{ "heart-rate", cmd_heart_rate, "\tHide/Unhide Heart Rate Service" },
 	{ }
 };