summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoão Paulo Rechi Vita <jprvita@gmail.com>2013-12-17 20:37:39 -0300
committerJoão Paulo Rechi Vita <jprvita@gmail.com>2014-01-15 15:03:53 -0300
commitdd8e8418a3f9c74d9275d7ca075d8d92d197165e (patch)
treeba84953c49a28ac55ee8ab2e03b7841c08446b55
parenta020d584edce7ce1228bb026cde4eff39e2dded1 (diff)
lib: Create API to read a GATT characteristic
-rw-r--r--lib/ble.c58
-rw-r--r--lib/ble.h33
2 files changed, 90 insertions, 1 deletions
diff --git a/lib/ble.c b/lib/ble.c
index a9e50ad..0a4530d 100644
--- a/lib/ble.c
+++ b/lib/ble.c
@@ -528,6 +528,62 @@ int ble_gatt_discover_descriptors(int conn_id, int char_id) {
return 0;
}
+/* Called when a GATT read characteristic operation returns */
+void read_characteristic_cb(int conn_id, int status,
+ btgatt_read_params_t *p_data) {
+ ble_device_t *dev;
+ int id = -1;
+
+ dev = find_device_by_conn_id(conn_id);
+ if (dev)
+ id = find_characteristic(dev, &p_data->srvc_id, &p_data->char_id);
+
+ if (data.cbs.char_read_cb)
+ data.cbs.char_read_cb(conn_id, id, p_data->value.value,
+ p_data->value.len, p_data->value_type, status);
+}
+
+static int ble_gatt_op(int operation, int conn_id, int id, int auth) {
+ ble_device_t *dev;
+ bt_status_t s = BT_STATUS_UNSUPPORTED;
+
+ if (id < 0)
+ return -1;
+
+ if (conn_id <= 0)
+ return -1;
+
+ if (!data.gattiface)
+ return -1;
+
+ dev = find_device_by_conn_id(conn_id);
+ if (!dev)
+ return -1;
+
+ switch (operation) {
+ case 0: /* Read characteristic */
+ if (dev->char_count <= 0)
+ return -1;
+ if (id >= dev->char_count)
+ return -1;
+
+ s = data.gattiface->client->read_characteristic(conn_id,
+ &dev->chars[id].s,
+ &dev->chars[id].c,
+ auth);
+ break;
+ }
+
+ if (s != BT_STATUS_SUCCESS)
+ return -s;
+
+ return 0;
+}
+
+int ble_gatt_read_char(int conn_id, int char_id, int auth) {
+ return ble_gatt_op(0, conn_id, char_id, auth);
+}
+
/* Called when the client registration is finished */
static void register_client_cb(int status, int client_if, bt_uuid_t *app_uuid) {
if (status == BT_STATUS_SUCCESS) {
@@ -551,7 +607,7 @@ static const btgatt_client_callbacks_t gattccbs = {
NULL, /* get_included_service_cb */
NULL, /* register_for_notification_cb */
NULL, /* notify_cb */
- NULL, /* read_characteristic_cb */
+ read_characteristic_cb,
NULL, /* write_characteristic_cb */
NULL, /* read_descriptor_cb */
NULL, /* write_descriptor_cb */
diff --git a/lib/ble.h b/lib/ble.h
index 5c5c3a9..d963630 100644
--- a/lib/ble.h
+++ b/lib/ble.h
@@ -130,6 +130,23 @@ typedef void (*ble_gatt_found_cb_t)(int conn_id, int id, const uint8_t *uuid,
typedef void (*ble_gatt_finished_cb_t)(int conn_id, int status);
/**
+ * Type that represents a callback function to forward a GATT operation
+ * response.
+ *
+ * @param conn_id The identifier of the connected remote device.
+ * @param id ID of the GATT element that the operation was executed uppon.
+ * @param value The value in the operation response.
+ * @param value_len The length of the data pointed by the value parameter.
+ * @param value_type The type of the data pointed by the value parameter.
+ * @param status The status in which the operation has finished.
+ *
+ * TODO: Document the semantics of value_type
+ */
+typedef void (*ble_gatt_response_cb_t)(int conn_id, int id,
+ const uint8_t *value, uint16_t value_len,
+ uint16_t value_type, int status);
+
+/**
* List of callbacks for BLE operations.
*/
typedef struct ble_cbs {
@@ -145,6 +162,7 @@ typedef struct ble_cbs {
ble_gatt_finished_cb_t char_finished_cb;
ble_gatt_found_cb_t desc_found_cb;
ble_gatt_finished_cb_t desc_finished_cb;
+ ble_gatt_response_cb_t char_read_cb;
} ble_cbs_t;
/**
@@ -291,4 +309,19 @@ int ble_gatt_discover_characteristics(int conn_id, int service_id);
* @return -1 if failed to request descriptor discovery.
*/
int ble_gatt_discover_descriptors(int conn_id, int char_id);
+
+/**
+ * Read the value of a characteristic.
+ *
+ * There should be an active connection with the device.
+ *
+ * @param conn_id The identifier of the connected remote device.
+ * @param char_id The identifier of the characteristic to be read.
+ * @param auth Whether or not link authentication should be requested before
+ * trying to read the characteristic: 1 request, 0 do not request.
+ *
+ * @return 0 if characteristic read has been successfully requested.
+ * @return -1 if failed to request characteristic read.
+ */
+int ble_gatt_read_char(int conn_id, int char_id, int auth);
#endif