summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoão Paulo Rechi Vita <jprvita@gmail.com>2013-12-19 13:19:35 -0300
committerJoão Paulo Rechi Vita <jprvita@gmail.com>2014-01-15 15:03:56 -0300
commitd1aa8ddaf12f17bbb674e24ee1ca45d3249db438 (patch)
treeb2a275fd9035604c68250441b79ffc8dce6e4f19
parentb4e7d0f4b1481a1d338b0bf4459000884d02754f (diff)
lib: Create API for GATT characteristic notifications
-rw-r--r--lib/ble.c80
-rw-r--r--lib/ble.h63
2 files changed, 141 insertions, 2 deletions
diff --git a/lib/ble.c b/lib/ble.c
index 369047c..5e71355 100644
--- a/lib/ble.c
+++ b/lib/ble.c
@@ -695,6 +695,82 @@ int ble_gatt_write_req_desc(int conn_id, int desc_id, int auth,
return ble_gatt_op(6, conn_id, desc_id, auth, value, len);
}
+/* Called when the registration for notifications on a char finishes */
+static void register_for_notification_cb(int conn_id, int registered,
+ int status,
+ btgatt_srvc_id_t *srvc_id,
+ btgatt_char_id_t *char_id) {
+ int id = 0;
+
+ if (data.cbs.char_notification_register_cb)
+ data.cbs.char_notification_register_cb(conn_id, id, registered, status);
+}
+
+/* Called when notifications of a characteristic are received */
+void notify_cb(int conn_id, btgatt_notify_params_t *p_data) {
+ int id = 0;
+
+ if (data.cbs.char_notification_cb)
+ data.cbs.char_notification_cb(conn_id, id, p_data->value, p_data->len,
+ !p_data->is_notify);
+}
+
+static int ble_gatt_char_notification(uint8_t operation, int conn_id,
+ int char_id) {
+ ble_device_t *dev;
+ bt_status_t s = BT_STATUS_UNSUPPORTED;
+ btgatt_srvc_id_t *srvc;
+ btgatt_char_id_t *ch;
+
+ if (char_id < 0)
+ return -1;
+
+ if (!data.client)
+ return -1;
+
+ if (!data.gattiface)
+ return -1;
+
+ if (!data.adapter_state)
+ return -1;
+
+ dev = find_device_by_conn_id(conn_id);
+ if (!dev)
+ return -1;
+
+ if (dev->char_count <= 0 || char_id >= dev->char_count)
+ return -1;
+
+ srvc = &dev->chars[char_id].s;
+ ch = &dev->chars[char_id].c;
+
+ switch (operation) {
+ case 0:
+ s = data.gattiface->client->register_for_notification(data.client,
+ &dev->bda,
+ srvc, ch);
+ break;
+ case 1:
+ s = data.gattiface->client->deregister_for_notification(data.client,
+ &dev->bda,
+ srvc, ch);
+ break;
+ }
+
+ if (s != BT_STATUS_SUCCESS)
+ return -s;
+
+ return 0;
+}
+
+int ble_gatt_register_char_notification(int conn_id, int char_id) {
+ return ble_gatt_char_notification(0, conn_id, char_id);
+}
+
+int ble_gatt_unregister_char_notification(int conn_id, int char_id) {
+ return ble_gatt_char_notification(1, conn_id, char_id);
+}
+
/* 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) {
@@ -716,8 +792,8 @@ static const btgatt_client_callbacks_t gattccbs = {
characteristic_discovery_cb,
descriptor_discovery_cb,
NULL, /* get_included_service_cb */
- NULL, /* register_for_notification_cb */
- NULL, /* notify_cb */
+ register_for_notification_cb,
+ notify_cb,
read_characteristic_cb,
write_characteristic_cb,
read_descriptor_cb,
diff --git a/lib/ble.h b/lib/ble.h
index 5580a22..8d072ad 100644
--- a/lib/ble.h
+++ b/lib/ble.h
@@ -147,6 +147,35 @@ typedef void (*ble_gatt_response_cb_t)(int conn_id, int id,
uint16_t value_type, int status);
/**
+ * Type that represents a callback function to notify that a registration for
+ * characteristic notification has finished.
+ *
+ * @param conn_id The identifier of the connected remote device.
+ * @param char_id ID of the characteristic to register for notifications.
+ * @param registered Whether notifications for the characteristic has been
+ * successfully registered: 1 yes, 0 no.
+ * @param status The response status of the registration operation.
+ */
+typedef void (*ble_gatt_notification_register_cb_t)(int conn_id, int char_id,
+ int registered, int status);
+
+/**
+ * Type that represents a callback function to forward a GATT notification or
+ * indication.
+ *
+ * @param conn_id The identifier of the connected remote device.
+ * @param char_id ID of the characteristic that the notification refers to.
+ * @param value The new value of the characteristic.
+ * @param value_len The length of the data pointed by the value parameter.
+ * @param is_indication Whether the received packed is a notification or an
+ * indication: 0 notification, 1 indication.
+ */
+typedef void (*ble_gatt_notification_cb_t)(int conn_id, int char_id,
+ const uint8_t *value,
+ uint16_t value_len,
+ uint8_t is_indication);
+
+/**
* List of callbacks for BLE operations.
*/
typedef struct ble_cbs {
@@ -166,6 +195,8 @@ typedef struct ble_cbs {
ble_gatt_response_cb_t desc_read_cb;
ble_gatt_response_cb_t char_write_cb;
ble_gatt_response_cb_t desc_write_cb;
+ ble_gatt_notification_register_cb_t char_notification_register_cb;
+ ble_gatt_notification_cb_t char_notification_cb;
} ble_cbs_t;
/**
@@ -418,4 +449,36 @@ int ble_gatt_write_cmd_desc(int conn_id, int desc_id, int auth,
*/
int ble_gatt_write_req_desc(int conn_id, int desc_id, int auth,
const char *value, int len);
+
+/**
+ * Register for notifications of changes in 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 register for
+ * notifications.
+ *
+ * @return 0 if registration for characteristic notifications have been
+ * successfully requested.
+ * @return -1 if failed to request registration for characteristic
+ * notifications.
+ */
+int ble_gatt_register_char_notification(int conn_id, int char_id);
+
+/**
+ * Unregister for notifications of changes in 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 unregister for
+ * notifications.
+ *
+ * @return 0 if deregistration for characteristic notifications have been
+ * successfully requested.
+ * @return -1 if failed to request deregistration for characteristic
+ * notifications.
+ */
+int ble_gatt_unregister_char_notification(int conn_id, int char_id);
#endif