diff options
author | João Paulo Rechi Vita <jprvita@gmail.com> | 2013-12-18 18:14:28 -0300 |
---|---|---|
committer | João Paulo Rechi Vita <jprvita@gmail.com> | 2014-01-15 15:03:56 -0300 |
commit | b4e7d0f4b1481a1d338b0bf4459000884d02754f (patch) | |
tree | 4fa9dc06e56f1f4783b725b1bb8e32bf57ae861e | |
parent | 7cd8d8fd1db1ca8eacffb8ad13d3df69290c8ab0 (diff) |
lib: Create API to write a GATT descriptor
-rw-r--r-- | lib/ble.c | 41 | ||||
-rw-r--r-- | lib/ble.h | 39 |
2 files changed, 79 insertions, 1 deletions
@@ -573,6 +573,21 @@ static void write_characteristic_cb(int conn_id, int status, data.cbs.char_write_cb(conn_id, id, NULL, 0, 0, status); } +/* Called when a GATT write descriptor operation returns */ +static void write_descriptor_cb(int conn_id, int status, + btgatt_write_params_t *p_data) { + ble_device_t *dev; + int id = -1; + + dev = find_device_by_conn_id(conn_id); + if (dev) + id = find_descriptor(dev, &p_data->srvc_id, &p_data->char_id, + &p_data->descr_id); + + if (data.cbs.desc_write_cb) + data.cbs.desc_write_cb(conn_id, id, NULL, 0, 0, status); +} + static int ble_gatt_op(int operation, int conn_id, int id, int auth, const char *value, int len) { ble_device_t *dev; @@ -630,6 +645,20 @@ static int ble_gatt_op(int operation, int conn_id, int id, int auth, auth, (char *) value); break; + + case 5: /* Write descriptor with write command */ + case 6: /* Write descriptor with write request */ + case 7: /* Write descriptor with prepare write */ + if (dev->desc_count <= 0 || id >= dev->desc_count) + return -1; + + s = data.gattiface->client->write_descriptor(conn_id, + &dev->descs[id].c.s, + &dev->descs[id].c.c, + &dev->descs[id].d, + operation-4, len, + auth, (char *) value); + break; } if (s != BT_STATUS_SUCCESS) @@ -656,6 +685,16 @@ int ble_gatt_write_req_char(int conn_id, int char_id, int auth, return ble_gatt_op(3, conn_id, char_id, auth, value, len); } +int ble_gatt_write_cmd_desc(int conn_id, int desc_id, int auth, + const char *value, int len) { + return ble_gatt_op(5, conn_id, desc_id, auth, value, len); +} + +int ble_gatt_write_req_desc(int conn_id, int desc_id, int auth, + const char *value, int len) { + return ble_gatt_op(6, conn_id, desc_id, auth, value, len); +} + /* 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) { @@ -682,7 +721,7 @@ static const btgatt_client_callbacks_t gattccbs = { read_characteristic_cb, write_characteristic_cb, read_descriptor_cb, - NULL, /* write_descriptor_cb */ + write_descriptor_cb, NULL, /* execute_write_cb */ NULL, /* read_remote_rssi_cb */ }; @@ -165,6 +165,7 @@ typedef struct ble_cbs { ble_gatt_response_cb_t char_read_cb; 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_cbs_t; /** @@ -379,4 +380,42 @@ int ble_gatt_write_cmd_char(int conn_id, int char_id, int auth, */ int ble_gatt_write_req_char(int conn_id, int char_id, int auth, const char *value, int len); + +/** + * Write the value of a descriptor using write command (no response). + * + * There should be an active connection with the device. + * + * @param conn_id The identifier of the connected remote device. + * @param desc_id The identifier of the descriptor to be written. + * @param auth Whether or not link encryption should be requested before trying + * to write the descriptor: 1 request, 0 do not request. + * @param value Pointer to the value that should be written on the + * descriptor. + * @param len The length of the data pointed by the value parameter. + * + * @return 0 if descriptor write has been successfully requested. + * @return -1 if failed to request descriptor write. + */ +int ble_gatt_write_cmd_desc(int conn_id, int desc_id, int auth, + const char *value, int len); + +/** + * Write the value of a descriptor using write request (with response). + * + * There should be an active connection with the device. + * + * @param conn_id The identifier of the connected remote device. + * @param desc_id The identifier of the descriptor to be written. + * @param auth Whether or not link encryption should be requested before trying + * to write the descriptor: 1 request, 0 do not request. + * @param value Pointer to the value that should be written on the + * descriptor. + * @param len The length of the data pointed by the value parameter. + * + * @return 0 if descriptor write has been successfully requested. + * @return -1 if failed to request descriptor write. + */ +int ble_gatt_write_req_desc(int conn_id, int desc_id, int auth, + const char *value, int len); #endif |