[PATCH v3 11/15] thermometer: Add common function to write characteristics CCC

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There are now few separate functions to write CCC for measurement
characteristics which looks similar. This patch adds common function
to write given value into CCC for given characteristics and leaves
dedicated functions to act only as simple wrappers.
---
 profiles/thermometer/thermometer.c | 108 ++++++++-----------------------------
 1 file changed, 23 insertions(+), 85 deletions(-)

diff --git a/profiles/thermometer/thermometer.c b/profiles/thermometer/thermometer.c
index b39fd9b..72cdc79 100644
--- a/profiles/thermometer/thermometer.c
+++ b/profiles/thermometer/thermometer.c
@@ -376,7 +376,7 @@ static void valid_range_desc_cb(guint8 status, const guint8 *pdu, guint16 len,
 	change_property(desc->ch->t, "Minimum", &min);
 }
 
-static void measurement_cb(guint8 status, const guint8 *pdu,
+static void write_ccc_cb(guint8 status, const guint8 *pdu,
 						guint16 len, gpointer user_data)
 {
 	char *msg = user_data;
@@ -426,7 +426,7 @@ static void process_thermometer_desc(struct descriptor *desc)
 
 		att_put_u16(val, atval);
 		gatt_write_char(ch->t->attrib, desc->handle, atval, 2,
-							measurement_cb, msg);
+							write_ccc_cb, msg);
 		return;
 	}
 
@@ -720,9 +720,8 @@ static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
 	return write_attr_interval(t, msg, value);
 }
 
-static void enable_final_measurement(gpointer data, gpointer user_data)
+static void write_ccc(struct thermometer *t, const char *uuid, uint16_t value)
 {
-	struct thermometer *t = data;
 	struct characteristic *ch;
 	struct descriptor *desc;
 	bt_uuid_t btuuid;
@@ -732,116 +731,55 @@ static void enable_final_measurement(gpointer data, gpointer user_data)
 	if (t->attrib == NULL)
 		return;
 
-	ch = get_characteristic(t, TEMPERATURE_MEASUREMENT_UUID);
+	ch = get_characteristic(t, uuid);
 	if (ch == NULL) {
-		DBG("Temperature measurement characteristic not found");
+		DBG("Characteristic %s not found", uuid);
 		return;
 	}
 
 	bt_uuid16_create(&btuuid, GATT_CLIENT_CHARAC_CFG_UUID);
 	desc = get_descriptor(ch, &btuuid);
 	if (desc == NULL) {
-		DBG("Client characteristic configuration descriptor not found");
+		DBG("CCC descriptor for %s not found", uuid);
 		return;
 	}
 
-	atval[0] = 0x02;
-	atval[1] = 0x00;
-	msg = g_strdup("Enable final measurement");
-	gatt_write_char(t->attrib, desc->handle, atval, 2, measurement_cb, msg);
+	att_put_u16(value, atval);
+
+	msg = g_strdup_printf("Write CCC: %04x for %s", value, uuid);
+
+	gatt_write_char(t->attrib, desc->handle, atval, sizeof(atval),
+							write_ccc_cb, msg);
 }
 
-static void enable_intermediate_measurement(gpointer data, gpointer user_data)
+static void enable_final_measurement(gpointer data, gpointer user_data)
 {
 	struct thermometer *t = data;
-	struct characteristic *ch;
-	struct descriptor *desc;
-	bt_uuid_t btuuid;
-	uint8_t atval[2];
-	char *msg;
-
-	if (t->attrib == NULL || !t->intermediate)
-		return;
 
-	ch = get_characteristic(t, INTERMEDIATE_TEMPERATURE_UUID);
-	if (ch == NULL) {
-		DBG("Intermediate measurement characteristic not found");
-		return;
-	}
+	write_ccc(t, TEMPERATURE_MEASUREMENT_UUID,
+					GATT_CLIENT_CHARAC_CFG_IND_BIT);
+}
 
-	bt_uuid16_create(&btuuid, GATT_CLIENT_CHARAC_CFG_UUID);
-	desc = get_descriptor(ch, &btuuid);
-	if (desc == NULL) {
-		DBG("Client characteristic configuration descriptor not found");
-		return;
-	}
+static void enable_intermediate_measurement(gpointer data, gpointer user_data)
+{
+	struct thermometer *t = data;
 
-	atval[0] = 0x01;
-	atval[1] = 0x00;
-	msg = g_strdup("Enable intermediate measurement");
-	gatt_write_char(t->attrib, desc->handle, atval, 2, measurement_cb, msg);
+	write_ccc(t, INTERMEDIATE_TEMPERATURE_UUID,
+					GATT_CLIENT_CHARAC_CFG_NOTIF_BIT);
 }
 
 static void disable_final_measurement(gpointer data, gpointer user_data)
 {
 	struct thermometer *t = data;
-	struct characteristic *ch;
-	struct descriptor *desc;
-	bt_uuid_t btuuid;
-	uint8_t atval[2];
-	char *msg;
 
-	if (t->attrib == NULL)
-		return;
-
-	ch = get_characteristic(t, TEMPERATURE_MEASUREMENT_UUID);
-	if (ch == NULL) {
-		DBG("Temperature measurement characteristic not found");
-		return;
-	}
-
-	bt_uuid16_create(&btuuid, GATT_CLIENT_CHARAC_CFG_UUID);
-	desc = get_descriptor(ch, &btuuid);
-	if (desc == NULL) {
-		DBG("Client characteristic configuration descriptor not found");
-		return;
-	}
-
-	atval[0] = 0x00;
-	atval[1] = 0x00;
-	msg = g_strdup("Disable final measurement");
-	gatt_write_char(t->attrib, desc->handle, atval, 2, measurement_cb, msg);
+	write_ccc(t, TEMPERATURE_MEASUREMENT_UUID, 0x0000);
 }
 
 static void disable_intermediate_measurement(gpointer data, gpointer user_data)
 {
 	struct thermometer *t = data;
-	struct characteristic *ch;
-	struct descriptor *desc;
-	bt_uuid_t btuuid;
-	uint8_t atval[2];
-	char *msg;
-
-	if (t->attrib == NULL)
-		return;
-
-	ch = get_characteristic(t, INTERMEDIATE_TEMPERATURE_UUID);
-	if (ch == NULL) {
-		DBG("Intermediate measurement characteristic not found");
-		return;
-	}
-
-	bt_uuid16_create(&btuuid, GATT_CLIENT_CHARAC_CFG_UUID);
-	desc = get_descriptor(ch, &btuuid);
-	if (desc == NULL) {
-		DBG("Client characteristic configuration descriptor not found");
-		return;
-	}
 
-	atval[0] = 0x00;
-	atval[1] = 0x00;
-	msg = g_strdup("Disable intermediate measurement");
-	gatt_write_char(t->attrib, desc->handle, atval, 2, measurement_cb, msg);
+	write_ccc(t, INTERMEDIATE_TEMPERATURE_UUID, 0x0000);
 }
 
 static void remove_int_watcher(struct thermometer_adapter *tadapter,
-- 
1.7.11.3

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux