aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChen Ganir <chen.ganir@ti.com>2012-03-20 13:00:48 +0200
committerJoão Paulo Rechi Vita <jprvita@openbossa.org>2012-06-21 18:00:19 -0300
commit726227d68fd8df4213646601edd6d808e1a1cff3 (patch)
treefb6004d65cdbbc629f6042d1ec29b76f92e5d895
parent5677d3a93b89bcc527bfec09d818448fc7e306b8 (diff)
downloadbluez-726227d68fd8df4213646601edd6d808e1a1cff3.tar.gz
bluez-726227d68fd8df4213646601edd6d808e1a1cff3.tar.xz
bluez-726227d68fd8df4213646601edd6d808e1a1cff3.zip
battery: Read Battery level characteristic
Add support for reading the battery level characteristic on connection establishment.
-rw-r--r--batterystate/batterystate.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/batterystate/batterystate.c b/batterystate/batterystate.c
index eba910f5..d81d78a8 100644
--- a/batterystate/batterystate.c
+++ b/batterystate/batterystate.c
@@ -122,6 +122,40 @@ static void batterystate_free(gpointer user_data)
g_free(batt);
}
+static void read_batterylevel_cb(guint8 status, const guint8 *pdu, guint16 len,
+ gpointer user_data)
+{
+ struct characteristic *ch = user_data;
+ uint8_t value[ATT_MAX_MTU];
+ ssize_t vlen;
+
+ if (status != 0) {
+ error("Failed to read Battery Level:%s", att_ecode2str(status));
+ return;
+ }
+
+ vlen = dec_read_resp(pdu, len, value, sizeof(value));
+ if (vlen < 0) {
+ error("Failed to read Battery Level: Protocol error\n");
+ return;
+ }
+
+ if (vlen < 1) {
+ error("Failed to read Battery Level: Wrong pdu len");
+ return;
+ }
+
+ ch->level = value[0];
+}
+
+static void process_batteryservice_char(struct characteristic *ch)
+{
+ if (g_strcmp0(ch->attr.uuid, BATTERY_LEVEL_UUID) == 0) {
+ gatt_read_char(ch->batt->attrib, ch->attr.value_handle, 0,
+ read_batterylevel_cb, ch);
+ }
+}
+
static void batterylevel_presentation_format_desc_cb(guint8 status,
const guint8 *pdu, guint16 len,
gpointer user_data)
@@ -289,6 +323,7 @@ static void configure_batterystate_cb(GSList *characteristics, guint8 status,
}
batt->chars = g_slist_append(batt->chars, ch);
+ process_batteryservice_char(ch);
start = c->value_handle + 1;