Bluetooth: Add bt_gatt_exchange_mtu

bt_gatt_exchange_mtu can be used to exchange MTU used by GATT, the MTU
is selected automatically based on the amount of data bt_buf can hold.

Change-Id: Id49a506663d922132e81c0a753a983b93579ffd0
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
This commit is contained in:
Luiz Augusto von Dentz 2015-07-01 11:03:54 +03:00 committed by Anas Nashif
commit 96fbd6636c
2 changed files with 55 additions and 0 deletions

View file

@ -624,4 +624,23 @@ void bt_gatt_connected(struct bt_conn *conn);
*/
void bt_gatt_disconnected(struct bt_conn *conn);
/* Client API */
/*! @brief Response callback function
*
* @param conn Connection object.
* @param err Error code.
*/
typedef void (*bt_gatt_rsp_func_t)(struct bt_conn *conn, uint8_t err);
/*! @brief Exchange MTU
*
* This client procedure can be used to set the MTU to the maximum possible
* size the buffers can hold.
* NOTE: Shall only be used once per connection.
*
* @param conn Connection object.
*/
int bt_gatt_exchange_mtu(struct bt_conn *conn, bt_gatt_rsp_func_t func);
#endif /* __BT_GATT_H */

View file

@ -461,3 +461,39 @@ void bt_gatt_disconnected(struct bt_conn *conn)
BT_DBG("conn %p\n", conn);
bt_gatt_foreach_attr(0x0001, 0xffff, disconnected_cb, conn);
}
static void gatt_mtu_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;
func(conn, err);
}
int bt_gatt_exchange_mtu(struct bt_conn *conn, bt_gatt_rsp_func_t func)
{
struct bt_att_exchange_mtu_req *req;
struct bt_buf *buf;
uint16_t mtu;
if (!conn || !func) {
return -EINVAL;
}
buf = bt_att_create_pdu(conn, BT_ATT_OP_MTU_REQ, sizeof(*req));
if (!buf) {
return -ENOMEM;
}
/* Select MTU based on the amount of room we have in bt_buf including
* one extra byte for ATT header.
*/
mtu = bt_buf_tailroom(buf) + 1;
BT_DBG("Client MTU %u\n", mtu);
req = bt_buf_add(buf, sizeof(*req));
req->mtu = sys_cpu_to_le16(mtu);
return bt_att_send(conn, buf, gatt_mtu_rsp, func, NULL);
}