2015-04-28 10:44:37 +03:00
|
|
|
/* att.c - Attribute protocol handling */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2) Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3) Neither the name of Intel Corporation nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <nanokernel.h>
|
|
|
|
#include <toolchain.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2015-05-06 15:58:46 +03:00
|
|
|
#include <stdbool.h>
|
2015-04-28 10:44:37 +03:00
|
|
|
#include <misc/byteorder.h>
|
2015-05-04 17:22:10 +03:00
|
|
|
#include <misc/util.h>
|
2015-04-28 10:44:37 +03:00
|
|
|
|
|
|
|
#include <bluetooth/hci.h>
|
|
|
|
#include <bluetooth/bluetooth.h>
|
2015-05-18 10:57:05 +03:00
|
|
|
#include <bluetooth/uuid.h>
|
2015-04-28 10:44:37 +03:00
|
|
|
|
|
|
|
#include "hci_core.h"
|
|
|
|
#include "conn.h"
|
|
|
|
#include "l2cap.h"
|
|
|
|
#include "att.h"
|
|
|
|
|
2015-05-13 14:06:34 +03:00
|
|
|
#if !defined(CONFIG_BLUETOOTH_DEBUG_ATT)
|
|
|
|
#undef BT_DBG
|
|
|
|
#define BT_DBG(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
2015-05-21 20:43:42 +03:00
|
|
|
/* ATT channel specific context */
|
|
|
|
struct bt_att {
|
|
|
|
/* The connection this context is associated with */
|
|
|
|
struct bt_conn *conn;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct bt_att bt_att_pool[CONFIG_BLUETOOTH_MAX_CONN];
|
|
|
|
|
2015-04-28 10:44:37 +03:00
|
|
|
static void send_err_rsp(struct bt_conn *conn, uint8_t req, uint16_t handle,
|
|
|
|
uint8_t err)
|
|
|
|
{
|
|
|
|
struct bt_att_error_rsp *rsp;
|
|
|
|
struct bt_buf *buf;
|
|
|
|
|
2015-05-04 19:12:51 +03:00
|
|
|
buf = bt_att_create_pdu(conn, BT_ATT_OP_ERROR_RSP, sizeof(*rsp));
|
2015-05-05 10:50:14 +03:00
|
|
|
if (!buf) {
|
2015-04-28 10:44:37 +03:00
|
|
|
return;
|
2015-05-05 10:50:14 +03:00
|
|
|
}
|
2015-04-28 10:44:37 +03:00
|
|
|
|
2015-05-18 10:18:21 +03:00
|
|
|
rsp = bt_buf_add(buf, sizeof(*rsp));
|
2015-04-28 10:44:37 +03:00
|
|
|
rsp->request = req;
|
|
|
|
rsp->handle = sys_cpu_to_le16(handle);
|
|
|
|
rsp->error = err;
|
|
|
|
|
2015-05-20 14:58:18 +03:00
|
|
|
bt_l2cap_send(conn, BT_L2CAP_CID_ATT, buf);
|
2015-04-28 10:44:37 +03:00
|
|
|
}
|
|
|
|
|
2015-05-04 17:22:10 +03:00
|
|
|
static void att_mtu_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_exchange_mtu_req *req;
|
|
|
|
struct bt_att_exchange_mtu_rsp *rsp;
|
|
|
|
struct bt_buf *buf;
|
|
|
|
uint16_t mtu;
|
|
|
|
|
|
|
|
if (data->len != sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_MTU_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
mtu = sys_le16_to_cpu(req->mtu);
|
|
|
|
|
|
|
|
BT_DBG("Client MTU %u\n", mtu);
|
|
|
|
|
|
|
|
if (mtu > BT_ATT_MAX_LE_MTU || mtu < BT_ATT_DEFAULT_LE_MTU) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_MTU_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bt_att_create_pdu(conn, BT_ATT_OP_MTU_RSP, sizeof(*rsp));
|
|
|
|
if (!buf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Select MTU based on the amount of room we have in bt_buf including
|
|
|
|
* one extra byte for ATT header.
|
|
|
|
*/
|
|
|
|
mtu = min(mtu, bt_buf_tailroom(buf) + 1);
|
|
|
|
|
|
|
|
BT_DBG("Server MTU %u\n", mtu);
|
|
|
|
|
|
|
|
/* TODO: Store the MTU negotiated */
|
|
|
|
|
2015-05-18 10:18:21 +03:00
|
|
|
rsp = bt_buf_add(buf, sizeof(*rsp));
|
2015-05-04 17:22:10 +03:00
|
|
|
rsp->mtu = sys_cpu_to_le16(mtu);
|
|
|
|
|
2015-05-20 14:58:18 +03:00
|
|
|
bt_l2cap_send(conn, BT_L2CAP_CID_ATT, buf);
|
2015-05-04 17:22:10 +03:00
|
|
|
}
|
|
|
|
|
2015-05-06 15:58:46 +03:00
|
|
|
static bool range_is_valid(uint16_t start, uint16_t end, uint16_t *err)
|
|
|
|
{
|
|
|
|
/* Handle 0 is invalid */
|
|
|
|
if (!start || !end) {
|
2015-05-14 10:29:56 +03:00
|
|
|
if (err) {
|
2015-05-06 15:58:46 +03:00
|
|
|
*err = 0;
|
2015-05-14 10:29:56 +03:00
|
|
|
}
|
2015-05-06 15:58:46 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if range is valid */
|
|
|
|
if (start > end) {
|
2015-05-14 10:29:56 +03:00
|
|
|
if (err) {
|
2015-05-06 15:58:46 +03:00
|
|
|
*err = start;
|
2015-05-14 10:29:56 +03:00
|
|
|
}
|
2015-05-06 15:58:46 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-06 14:44:09 +03:00
|
|
|
static void att_find_info_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_find_info_req *req;
|
2015-05-06 15:58:46 +03:00
|
|
|
uint16_t start_handle, end_handle, err_handle;
|
2015-05-06 14:44:09 +03:00
|
|
|
|
|
|
|
if (data->len != sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_FIND_INFO_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
start_handle = sys_le16_to_cpu(req->start_handle);
|
|
|
|
end_handle = sys_le16_to_cpu(req->end_handle);
|
|
|
|
|
|
|
|
BT_DBG("start_handle %u end_handle %u\n", start_handle, end_handle);
|
|
|
|
|
2015-05-06 15:58:46 +03:00
|
|
|
if (!range_is_valid(start_handle, end_handle, &err_handle)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_FIND_TYPE_REQ, err_handle,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
return;
|
2015-05-06 14:44:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_FIND_INFO_REQ, start_handle,
|
|
|
|
BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-06 15:03:11 +03:00
|
|
|
static void att_find_type_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_find_type_req *req;
|
2015-05-06 15:58:46 +03:00
|
|
|
uint16_t start_handle, end_handle, err_handle, type;
|
2015-05-06 15:03:11 +03:00
|
|
|
uint8_t *value;
|
|
|
|
|
|
|
|
if (data->len < sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_FIND_TYPE_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
start_handle = sys_le16_to_cpu(req->start_handle);
|
|
|
|
end_handle = sys_le16_to_cpu(req->end_handle);
|
|
|
|
type = sys_le16_to_cpu(req->type);
|
|
|
|
value = bt_buf_pull(data, sizeof(*req));
|
|
|
|
|
|
|
|
BT_DBG("start_handle %u end_handle %u type %u\n", start_handle,
|
|
|
|
end_handle, type);
|
|
|
|
|
2015-05-06 15:58:46 +03:00
|
|
|
if (!range_is_valid(start_handle, end_handle, &err_handle)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_FIND_TYPE_REQ, err_handle,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
return;
|
2015-05-06 15:03:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_FIND_TYPE_REQ, start_handle,
|
|
|
|
BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
|
|
|
|
|
|
|
|
return;
|
2015-05-06 15:58:46 +03:00
|
|
|
}
|
2015-05-06 15:03:11 +03:00
|
|
|
|
2015-05-12 17:38:39 +03:00
|
|
|
static bool uuid_create(struct bt_uuid *uuid, struct bt_buf *data)
|
2015-05-06 15:58:46 +03:00
|
|
|
{
|
2015-05-12 17:38:39 +03:00
|
|
|
if (data->len > sizeof(uuid->u128)) {
|
2015-05-06 15:58:46 +03:00
|
|
|
return false;
|
2015-05-14 10:29:56 +03:00
|
|
|
}
|
2015-05-06 15:58:46 +03:00
|
|
|
|
2015-05-12 17:38:39 +03:00
|
|
|
switch (data->len) {
|
2015-05-06 15:58:46 +03:00
|
|
|
case 2:
|
|
|
|
uuid->type = BT_UUID_16;
|
2015-05-12 17:38:39 +03:00
|
|
|
uuid->u16 = bt_buf_pull_le16(data);
|
2015-05-06 15:58:46 +03:00
|
|
|
return true;
|
|
|
|
case 16:
|
|
|
|
uuid->type = BT_UUID_128;
|
2015-05-12 17:38:39 +03:00
|
|
|
memcpy(uuid->u128, data->data, data->len);
|
2015-05-06 15:58:46 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void att_read_type_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_read_type_req *req;
|
|
|
|
uint16_t start_handle, end_handle, err_handle;
|
|
|
|
struct bt_uuid uuid;
|
|
|
|
|
|
|
|
/* Type can only be UUID16 or UUID128 */
|
|
|
|
if (data->len != sizeof(*req) + sizeof(uuid.u16) &&
|
|
|
|
data->len != sizeof(*req) + sizeof(uuid.u128)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_TYPE_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
start_handle = sys_le16_to_cpu(req->start_handle);
|
|
|
|
end_handle = sys_le16_to_cpu(req->end_handle);
|
|
|
|
bt_buf_pull(data, sizeof(*req));
|
|
|
|
|
2015-05-12 17:38:39 +03:00
|
|
|
if (!uuid_create(&uuid, data)) {
|
2015-05-06 15:58:46 +03:00
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_TYPE_REQ, 0,
|
|
|
|
BT_ATT_ERR_UNLIKELY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("start_handle %u end_handle %u type %u\n",
|
|
|
|
start_handle, end_handle, uuid.u16);
|
|
|
|
|
|
|
|
if (!range_is_valid(start_handle, end_handle, &err_handle)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_TYPE_REQ, err_handle,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_TYPE_REQ, start_handle,
|
|
|
|
BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
|
|
|
|
return;
|
2015-05-06 15:03:11 +03:00
|
|
|
}
|
|
|
|
|
2015-05-07 12:12:46 +03:00
|
|
|
static void att_read_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_read_req *req;
|
|
|
|
uint16_t handle;
|
|
|
|
|
|
|
|
if (data->len != sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
handle = sys_le16_to_cpu(req->handle);
|
|
|
|
|
|
|
|
BT_DBG("handle %u\n", handle);
|
|
|
|
|
|
|
|
if (!handle) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_REQ, handle,
|
|
|
|
BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:20:13 +03:00
|
|
|
static void att_read_blob_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_read_blob_req *req;
|
|
|
|
uint16_t handle, offset;
|
|
|
|
|
|
|
|
if (data->len != sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_BLOB_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
handle = sys_le16_to_cpu(req->handle);
|
|
|
|
offset = sys_le16_to_cpu(req->offset);
|
|
|
|
|
|
|
|
BT_DBG("handle %u offset %u\n", handle, offset);
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_BLOB_REQ, handle,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
|
2015-05-07 13:05:14 +03:00
|
|
|
static void att_read_mult_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_read_mult_req *req;
|
|
|
|
uint16_t handle1, handle2;
|
|
|
|
|
|
|
|
if (data->len < sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_MULT_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
handle1 = sys_le16_to_cpu(req->handle1);
|
|
|
|
handle2 = sys_le16_to_cpu(req->handle2);
|
|
|
|
|
|
|
|
BT_DBG("handle1 %u handle2 %u ...\n", handle1, handle2);
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_MULT_REQ, handle1,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
|
2015-05-07 13:57:46 +03:00
|
|
|
static void att_read_group_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_read_group_req *req;
|
|
|
|
uint16_t start_handle, end_handle, err_handle;
|
|
|
|
struct bt_uuid uuid;
|
|
|
|
|
|
|
|
/* Type can only be UUID16 or UUID128 */
|
|
|
|
if (data->len != sizeof(*req) + sizeof(uuid.u16) &&
|
|
|
|
data->len != sizeof(*req) + sizeof(uuid.u128)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
start_handle = sys_le16_to_cpu(req->start_handle);
|
|
|
|
end_handle = sys_le16_to_cpu(req->end_handle);
|
|
|
|
bt_buf_pull(data, sizeof(*req));
|
|
|
|
|
2015-05-12 17:38:39 +03:00
|
|
|
if (!uuid_create(&uuid, data)) {
|
2015-05-07 13:57:46 +03:00
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ, 0,
|
|
|
|
BT_ATT_ERR_UNLIKELY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("start_handle %u end_handle %u type %u\n",
|
|
|
|
start_handle, end_handle, uuid.u16);
|
|
|
|
|
|
|
|
if (!range_is_valid(start_handle, end_handle, &err_handle)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_FIND_TYPE_REQ, err_handle,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Core v4.2, Vol 3, sec 2.5.3 Attribute Grouping:
|
|
|
|
* Not all of the grouping attributes can be used in the ATT
|
|
|
|
* Read By Group Type Request. The «Primary Service» and «Secondary
|
|
|
|
* Service» grouping types may be used in the Read By Group Type
|
|
|
|
* Request. The «Characteristic» grouping type shall not be used in
|
|
|
|
* the ATT Read By Group Type Request.
|
|
|
|
*/
|
|
|
|
if (uuid.type == BT_UUID_16) {
|
|
|
|
if (uuid.u16 != 0x2800 && uuid.u16 != 0x2801) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ,
|
|
|
|
start_handle,
|
|
|
|
BT_ATT_ERR_UNSUPPORTED_GROUP_TYPE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} /* TODO: Add UUID helpers for UUID formats */
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_READ_GROUP_REQ, start_handle,
|
|
|
|
BT_ATT_ERR_ATTRIBUTE_NOT_FOUND);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-08 11:06:34 +03:00
|
|
|
static void att_write_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_write_req *req;
|
|
|
|
uint16_t handle;
|
|
|
|
|
|
|
|
if (data->len < sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_WRITE_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
handle = sys_le16_to_cpu(req->handle);
|
2015-05-08 14:10:49 +03:00
|
|
|
bt_buf_pull(data, sizeof(*req));
|
2015-05-08 11:06:34 +03:00
|
|
|
|
|
|
|
BT_DBG("handle %u\n", handle);
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_WRITE_REQ, handle,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
|
2015-05-08 14:31:50 +03:00
|
|
|
static void att_prepare_write_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_prepare_write_req *req;
|
|
|
|
uint16_t handle, offset;
|
|
|
|
|
|
|
|
if (data->len < sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_PREPARE_WRITE_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
handle = sys_le16_to_cpu(req->handle);
|
|
|
|
offset = sys_le16_to_cpu(req->offset);
|
|
|
|
|
|
|
|
BT_DBG("handle %u offset %u\n", handle);
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_PREPARE_WRITE_REQ, handle,
|
|
|
|
BT_ATT_ERR_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
|
2015-05-08 14:39:51 +03:00
|
|
|
static void att_exec_write_req(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
|
|
|
struct bt_att_exec_write_req *req;
|
|
|
|
|
|
|
|
if (data->len != sizeof(*req)) {
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_EXEC_WRITE_REQ, 0,
|
|
|
|
BT_ATT_ERR_INVALID_PDU);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
BT_DBG("flags %u\n", req->flags);
|
|
|
|
|
|
|
|
/* TODO: Generate proper response once a database is defined */
|
|
|
|
|
|
|
|
send_err_rsp(conn, BT_ATT_OP_EXEC_WRITE_REQ, 0,
|
|
|
|
BT_ATT_ERR_NOT_SUPPORTED);
|
|
|
|
}
|
|
|
|
|
2015-05-08 14:10:49 +03:00
|
|
|
static void att_write_cmd(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
2015-05-13 16:26:19 +03:00
|
|
|
struct bt_att_write_cmd *req;
|
2015-05-08 14:10:49 +03:00
|
|
|
uint16_t handle;
|
|
|
|
|
|
|
|
if (data->len < sizeof(*req)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
handle = sys_le16_to_cpu(req->handle);
|
|
|
|
|
|
|
|
BT_DBG("handle %u\n", handle);
|
|
|
|
|
|
|
|
/* TODO: Perform write once database is defined */
|
|
|
|
}
|
|
|
|
|
2015-05-08 14:28:42 +03:00
|
|
|
static void att_signed_write_cmd(struct bt_conn *conn, struct bt_buf *data)
|
|
|
|
{
|
2015-05-13 16:26:19 +03:00
|
|
|
struct bt_att_signed_write_cmd *req;
|
2015-05-08 14:28:42 +03:00
|
|
|
uint16_t handle;
|
|
|
|
|
|
|
|
if (data->len < sizeof(*req) + sizeof(struct bt_att_signature)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = (void *)data->data;
|
|
|
|
|
|
|
|
handle = sys_le16_to_cpu(req->handle);
|
|
|
|
bt_buf_pull(data, sizeof(*req));
|
|
|
|
|
|
|
|
BT_DBG("handle %u\n", handle);
|
|
|
|
|
|
|
|
/* TODO: Perform write once database is defined */
|
|
|
|
}
|
|
|
|
|
2015-05-21 18:53:13 +03:00
|
|
|
static void bt_att_recv(struct bt_conn *conn, struct bt_buf *buf)
|
2015-04-28 10:44:37 +03:00
|
|
|
{
|
|
|
|
struct bt_att_hdr *hdr = (void *)buf->data;
|
|
|
|
|
|
|
|
if (buf->len < sizeof(*hdr)) {
|
|
|
|
BT_ERR("Too small ATT PDU received\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-05-06 11:52:17 +03:00
|
|
|
BT_DBG("Received ATT code %u len %u\n", hdr->code, buf->len);
|
2015-04-28 10:44:37 +03:00
|
|
|
|
2015-05-04 16:23:56 +03:00
|
|
|
bt_buf_pull(buf, sizeof(*hdr));
|
2015-04-28 10:44:37 +03:00
|
|
|
|
|
|
|
switch (hdr->code) {
|
2015-05-04 17:22:10 +03:00
|
|
|
case BT_ATT_OP_MTU_REQ:
|
|
|
|
att_mtu_req(conn, buf);
|
|
|
|
break;
|
2015-05-06 14:44:09 +03:00
|
|
|
case BT_ATT_OP_FIND_INFO_REQ:
|
|
|
|
att_find_info_req(conn, buf);
|
|
|
|
break;
|
2015-05-06 15:03:11 +03:00
|
|
|
case BT_ATT_OP_FIND_TYPE_REQ:
|
|
|
|
att_find_type_req(conn, buf);
|
|
|
|
break;
|
2015-05-06 15:58:46 +03:00
|
|
|
case BT_ATT_OP_READ_TYPE_REQ:
|
|
|
|
att_read_type_req(conn, buf);
|
|
|
|
break;
|
2015-05-07 12:12:46 +03:00
|
|
|
case BT_ATT_OP_READ_REQ:
|
|
|
|
att_read_req(conn, buf);
|
|
|
|
break;
|
2015-05-07 12:20:13 +03:00
|
|
|
case BT_ATT_OP_READ_BLOB_REQ:
|
|
|
|
att_read_blob_req(conn, buf);
|
|
|
|
break;
|
2015-05-07 13:05:14 +03:00
|
|
|
case BT_ATT_OP_READ_MULT_REQ:
|
|
|
|
att_read_mult_req(conn, buf);
|
|
|
|
break;
|
2015-05-07 13:57:46 +03:00
|
|
|
case BT_ATT_OP_READ_GROUP_REQ:
|
|
|
|
att_read_group_req(conn, buf);
|
|
|
|
break;
|
2015-05-08 11:06:34 +03:00
|
|
|
case BT_ATT_OP_WRITE_REQ:
|
|
|
|
att_write_req(conn, buf);
|
|
|
|
break;
|
2015-05-08 14:31:50 +03:00
|
|
|
case BT_ATT_OP_PREPARE_WRITE_REQ:
|
|
|
|
att_prepare_write_req(conn, buf);
|
|
|
|
break;
|
2015-05-08 14:39:51 +03:00
|
|
|
case BT_ATT_OP_EXEC_WRITE_REQ:
|
|
|
|
att_exec_write_req(conn, buf);
|
|
|
|
break;
|
2015-05-08 14:10:49 +03:00
|
|
|
case BT_ATT_OP_WRITE_CMD:
|
|
|
|
att_write_cmd(conn, buf);
|
|
|
|
break;
|
2015-05-08 14:28:42 +03:00
|
|
|
case BT_ATT_OP_SIGNED_WRITE_CMD:
|
|
|
|
att_signed_write_cmd(conn, buf);
|
|
|
|
break;
|
2015-04-28 10:44:37 +03:00
|
|
|
default:
|
2015-05-13 15:45:59 +03:00
|
|
|
BT_WARN("Unhandled ATT code %u\n", hdr->code);
|
2015-04-28 10:44:37 +03:00
|
|
|
send_err_rsp(conn, hdr->code, 0, BT_ATT_ERR_NOT_SUPPORTED);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
bt_buf_put(buf);
|
|
|
|
}
|
2015-05-04 19:12:51 +03:00
|
|
|
|
|
|
|
struct bt_buf *bt_att_create_pdu(struct bt_conn *conn, uint8_t op, size_t len)
|
|
|
|
{
|
|
|
|
struct bt_att_hdr *hdr;
|
|
|
|
struct bt_buf *buf;
|
|
|
|
|
2015-05-20 14:58:18 +03:00
|
|
|
buf = bt_l2cap_create_pdu(conn);
|
2015-05-04 19:12:51 +03:00
|
|
|
if (!buf) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-18 10:18:21 +03:00
|
|
|
hdr = bt_buf_add(buf, sizeof(*hdr));
|
2015-05-04 19:12:51 +03:00
|
|
|
hdr->code = op;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2015-05-21 18:53:13 +03:00
|
|
|
|
2015-05-21 20:43:42 +03:00
|
|
|
static void bt_att_connected(struct bt_conn *conn)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
BT_DBG("conn %p handle %u\n", conn, conn->handle);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(bt_att_pool); i++) {
|
|
|
|
struct bt_att *att = &bt_att_pool[i];
|
|
|
|
|
|
|
|
if (!att->conn) {
|
|
|
|
att->conn = conn;
|
|
|
|
conn->att = att;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_ERR("No available ATT context for conn %p\n", conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bt_att_disconnected(struct bt_conn *conn)
|
|
|
|
{
|
|
|
|
struct bt_att *att = conn->att;
|
|
|
|
|
|
|
|
if (!att)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BT_DBG("conn %p handle %u\n", conn, conn->handle);
|
|
|
|
|
|
|
|
conn->att = NULL;
|
|
|
|
memset(att, 0, sizeof(*att));
|
|
|
|
}
|
|
|
|
|
2015-05-21 18:53:13 +03:00
|
|
|
void bt_att_init(void)
|
|
|
|
{
|
|
|
|
static struct bt_l2cap_chan chan = {
|
|
|
|
.cid = BT_L2CAP_CID_ATT,
|
|
|
|
.recv = bt_att_recv,
|
2015-05-21 20:43:42 +03:00
|
|
|
.connected = bt_att_connected,
|
|
|
|
.disconnected = bt_att_disconnected,
|
2015-05-21 18:53:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
bt_l2cap_chan_register(&chan);
|
|
|
|
}
|