Bluetooth: GATT: Add bt_gatt_write

This adds bt_gatt_write which can used to write attribute value.

Change-Id: I45a02e6dbf642ed1bcab8234180f2c48a28e2874
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
This commit is contained in:
Luiz Augusto von Dentz 2015-07-15 13:28:58 +03:00 committed by Anas Nashif
commit dc55a3741e
2 changed files with 78 additions and 0 deletions

View file

@ -731,6 +731,22 @@ typedef void (*bt_gatt_read_func_t)(struct bt_conn *conn, int err,
int bt_gatt_read(struct bt_conn *conn, uint16_t handle, uint16_t offset,
bt_gatt_read_func_t func);
/** @brief Write Attribute Value by handle
*
* This procedure write the attribute value and return the result in the
* callback in case it is set.
*
* @param conn Connection object.
* @param handle Attribute handle.
* @param data Data to be written.
* @param length Data length.
* @param func Callback function.
*
* @return 0 in case of success or negative value in case of error.
*/
int bt_gatt_write(struct bt_conn *conn, uint16_t handle, const void *data,
uint16_t length, bt_gatt_rsp_func_t func);
/** @brief Cancel GATT pending request
*
* @param conn Connection object.

View file

@ -921,6 +921,68 @@ int bt_gatt_read(struct bt_conn *conn, uint16_t handle, uint16_t offset,
return gatt_send(conn, buf, att_read_rsp, func, NULL);
}
static void att_write_rsp(struct bt_conn *conn, uint8_t err, const void *pdu,
uint16_t length, void *user_data)
{
bt_gatt_rsp_func_t func = user_data;
BT_DBG("err 0x%02x\n", err);
func(conn, err);
}
static int gatt_write_cmd(struct bt_conn *conn, uint16_t handle,
const void *data, uint16_t length)
{
struct bt_buf *buf;
struct bt_att_write_cmd *cmd;
buf = bt_att_create_pdu(conn, BT_ATT_OP_WRITE_CMD,
sizeof(*cmd) + length);
if (!buf) {
return -ENOMEM;
}
cmd = bt_buf_add(buf, sizeof(*cmd));
cmd->handle = sys_cpu_to_le16(handle);
memcpy(cmd->value, data, length);
bt_buf_add(buf, length);
BT_DBG("handle 0x%04x length %u\n", handle, length);
return gatt_send(conn, buf, NULL, NULL, NULL);
}
int bt_gatt_write(struct bt_conn *conn, uint16_t handle, const void *data,
uint16_t length, bt_gatt_rsp_func_t func)
{
struct bt_buf *buf;
struct bt_att_write_req *req;
if (!conn || !handle) {
return -EINVAL;
}
if (!func) {
return gatt_write_cmd(conn, handle, data, length);
}
buf = bt_att_create_pdu(conn, BT_ATT_OP_WRITE_REQ,
sizeof(*req) + length);
if (!buf) {
return -ENOMEM;
}
req = bt_buf_add(buf, sizeof(*req));
req->handle = sys_cpu_to_le16(handle);
memcpy(req->value, data, length);
bt_buf_add(buf, length);
BT_DBG("handle 0x%04x length %u\n", handle, length);
return gatt_send(conn, buf, att_write_rsp, func, NULL);
}
void bt_gatt_cancel(struct bt_conn *conn)
{
bt_att_cancel(conn);