diff options
author | João Paulo Rechi Vita <jprvita@gmail.com> | 2013-12-18 18:12:25 -0300 |
---|---|---|
committer | João Paulo Rechi Vita <jprvita@gmail.com> | 2014-01-15 15:03:55 -0300 |
commit | 7cd8d8fd1db1ca8eacffb8ad13d3df69290c8ab0 (patch) | |
tree | ff64b5d4371ddab09f2e42be58f889399bcacb2a | |
parent | 5c35b1f5e0f5839b1bf995e9d81157ff9ad627f1 (diff) |
lib: Create API to write a GATT characteristic
-rw-r--r-- | lib/ble.c | 47 | ||||
-rw-r--r-- | lib/ble.h | 39 |
2 files changed, 82 insertions, 4 deletions
@@ -559,7 +559,22 @@ static void read_descriptor_cb(int conn_id, int status, p_data->value.len, p_data->value_type, status); } -static int ble_gatt_op(int operation, int conn_id, int id, int auth) { +/* Called when a GATT write characteristic operation returns */ +static void write_characteristic_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_characteristic(dev, &p_data->srvc_id, &p_data->char_id); + + if (data.cbs.char_write_cb) + data.cbs.char_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; bt_status_t s = BT_STATUS_UNSUPPORTED; @@ -601,6 +616,20 @@ static int ble_gatt_op(int operation, int conn_id, int id, int auth) { &dev->descs[id].d, auth); break; + + case 2: /* Write characteristic with write command */ + case 3: /* Write characteristic with write request */ + case 4: /* Write characteristic with prepare write */ + if (dev->char_count <= 0 || id >= dev->char_count) + return -1; + + s = data.gattiface->client->write_characteristic(conn_id, + &dev->chars[id].s, + &dev->chars[id].c, + operation-1, len, + auth, + (char *) value); + break; } if (s != BT_STATUS_SUCCESS) @@ -610,11 +639,21 @@ static int ble_gatt_op(int operation, int conn_id, int id, int auth) { } int ble_gatt_read_char(int conn_id, int char_id, int auth) { - return ble_gatt_op(0, conn_id, char_id, auth); + return ble_gatt_op(0, conn_id, char_id, auth, NULL, 0); } int ble_gatt_read_desc(int conn_id, int desc_id, int auth) { - return ble_gatt_op(1, conn_id, desc_id, auth); + return ble_gatt_op(1, conn_id, desc_id, auth, NULL, 0); +} + +int ble_gatt_write_cmd_char(int conn_id, int char_id, int auth, + const char *value, int len) { + return ble_gatt_op(2, conn_id, char_id, auth, value, len); +} + +int ble_gatt_write_req_char(int conn_id, int char_id, int auth, + const char *value, int len) { + return ble_gatt_op(3, conn_id, char_id, auth, value, len); } /* Called when the client registration is finished */ @@ -641,7 +680,7 @@ static const btgatt_client_callbacks_t gattccbs = { NULL, /* register_for_notification_cb */ NULL, /* notify_cb */ read_characteristic_cb, - NULL, /* write_characteristic_cb */ + write_characteristic_cb, read_descriptor_cb, NULL, /* write_descriptor_cb */ NULL, /* execute_write_cb */ @@ -164,6 +164,7 @@ typedef struct ble_cbs { ble_gatt_finished_cb_t desc_finished_cb; 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_cbs_t; /** @@ -340,4 +341,42 @@ int ble_gatt_read_char(int conn_id, int char_id, int auth); * @return -1 if failed to request characteristic read. */ int ble_gatt_read_desc(int conn_id, int desc_id, int auth); + +/** + * Write the value of a characteristic 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 char_id The identifier of the characteristic to be written. + * @param auth Whether or not link authentication should be requested before + * trying to write the characteristic: 1 request, 0 do not request. + * @param value Pointer to the value that should be written on the + * characteristic. + * @param len The length of the data pointed by the value parameter. + * + * @return 0 if characteristic write has been successfully requested. + * @return -1 if failed to request characteristic write. + */ +int ble_gatt_write_cmd_char(int conn_id, int char_id, int auth, + const char *value, int len); + +/** + * Write the value of a characteristic 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 char_id The identifier of the characteristic to be written. + * @param auth Whether or not link authentication should be requested before + * trying to write the characteristic: 1 request, 0 do not request. + * @param value Pointer to the value that should be written on the + * characteristic. + * @param len The length of the data pointed by the value parameter. + * + * @return 0 if characteristic write has been successfully requested. + * @return -1 if failed to request characteristic write. + */ +int ble_gatt_write_req_char(int conn_id, int char_id, int auth, + const char *value, int len); #endif |