Bluetooth: Add connection abstration

This patch adds a new bt_conn struct which abstracts the details of an
connection. Each connection has two fibers: one for incoming and
another for outgoing data. For now only one connection is supported.
The patch also adds a basic L2CAP header definition so that fragmented
ACL packets can be properly parsed.

Change-Id: Ida38b2657a0e908a18219c3aa2997eb314f2c8a1
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2015-04-28 10:39:50 +03:00 committed by Anas Nashif
commit 4831529193
4 changed files with 485 additions and 1 deletions

326
net/bluetooth/conn.c Normal file
View file

@ -0,0 +1,326 @@
/* conn.c - Bluetooth connection 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>
#include <misc/byteorder.h>
#include <bluetooth/hci.h>
#include <bluetooth/bluetooth.h>
#include "hci_core.h"
#include "conn.h"
#include "l2cap.h"
#define MAX_CONN_COUNT 1
static struct bt_conn conns[MAX_CONN_COUNT];
static void bt_conn_reset_rx_state(struct bt_conn *conn)
{
if (!conn->rx_len)
return;
bt_buf_put(conn->rx);
conn->rx = NULL;
conn->rx_len = 0;
}
void bt_conn_recv(struct bt_conn *conn, struct bt_buf *buf, uint8_t flags)
{
struct bt_l2cap_hdr *hdr;
uint16_t len;
BT_DBG("handle %u len %u flags %x\n", conn->handle, buf->len, flags);
/* Check packet boundary flags */
switch (flags) {
case 0x02:
/* First packet */
hdr = (void *)buf->data;
len = sys_le16_to_cpu(hdr->len);
BT_DBG("First, len %u final %u\n", buf->len, len);
if (conn->rx_len) {
BT_ERR("Unexpected first L2CAP frame\n");
bt_conn_reset_rx_state(conn);
}
conn->rx_len = (sizeof(*hdr) + len) - buf->len;
BT_DBG("rx_len %u\n", conn->rx_len);
if (conn->rx_len) {
conn->rx = buf;
return;
}
break;
case 0x01:
/* Continuation */
if (!conn->rx_len) {
BT_ERR("Unexpected L2CAP continuation\n");
bt_conn_reset_rx_state(conn);
bt_buf_put(buf);
return;
}
if (buf->len > conn->rx_len) {
BT_ERR("L2CAP data overflow\n");
bt_conn_reset_rx_state(conn);
bt_buf_put(buf);
return;
}
BT_DBG("Cont, len %u rx_len %u\n", buf->len, conn->rx_len);
if (buf->len > bt_buf_tailroom(conn->rx)) {
BT_ERR("Not enough buffer space for L2CAP data\n");
bt_conn_reset_rx_state(conn);
bt_buf_put(buf);
return;
}
memcpy(bt_buf_add(conn->rx, buf->len), buf->data, buf->len);
conn->rx_len -= buf->len;
bt_buf_put(buf);
if (conn->rx_len)
return;
buf = conn->rx;
conn->rx = NULL;
conn->rx_len = 0;
break;
default:
BT_ERR("Unexpected ACL flags (%u)\n", flags);
bt_conn_reset_rx_state(conn);
bt_buf_put(buf);
return;
}
hdr = (void *)buf->data;
len = sys_le16_to_cpu(hdr->len);
if (sizeof(*hdr) + len != buf->len) {
BT_ERR("ACL len mismatch (%u != %u)\n", len, buf->len);
bt_buf_put(buf);
return;
}
BT_DBG("Successfully parsed %u byte L2CAP packet\n", buf->len);
nano_fiber_fifo_put(&conn->rx_queue, buf);
}
void bt_conn_send(struct bt_conn *conn, struct bt_buf *buf)
{
BT_DBG("conn handle %u buf len %u\n", conn->handle, buf->len);
nano_fifo_put(&conn->tx_queue, buf);
}
static void conn_rx_fiber(int arg1, int arg2)
{
struct bt_conn *conn = (struct bt_conn *)arg1;
struct bt_buf *buf;
BT_DBG("Started for handle %u\n", conn->handle);
while (conn->state == BT_CONN_CONNECTED) {
buf = nano_fifo_get_wait(&conn->rx_queue);
/* check for disconnection */
if (conn->state != BT_CONN_CONNECTED) {
bt_buf_put(buf);
break;
}
bt_buf_put(buf);
}
BT_DBG("handle %u disconnected - cleaning up\n", conn->handle);
/* Give back any allocated buffers */
while ((buf = nano_fifo_get(&conn->rx_queue)))
bt_buf_put(buf);
bt_conn_reset_rx_state(conn);
BT_DBG("handle %u exiting\n", conn->handle);
bt_conn_put(conn);
}
static void conn_tx_fiber(int arg1, int arg2)
{
struct bt_conn *conn = (struct bt_conn *)arg1;
struct bt_dev *dev = conn->dev;
struct bt_buf *buf;
BT_DBG("Started for handle %u\n", conn->handle);
while (conn->state == BT_CONN_CONNECTED) {
/* Wait until the controller can accept ACL packets */
nano_fiber_sem_take_wait(&dev->le_pkts_sem);
/* check for disconnection */
if (conn->state != BT_CONN_CONNECTED) {
nano_fiber_sem_give(&dev->le_pkts_sem);
break;
}
/* Get next ACL packet for connection */
buf = nano_fifo_get_wait(&conn->tx_queue);
if (conn->state != BT_CONN_CONNECTED) {
nano_fiber_sem_give(&dev->le_pkts_sem);
bt_buf_put(buf);
break;
}
dev->drv->send(buf);
bt_buf_put(buf);
}
BT_DBG("handle %u disconnected - cleaning up\n", conn->handle);
/* Give back any allocated buffers */
while ((buf = nano_fifo_get(&conn->tx_queue)))
bt_buf_put(buf);
BT_DBG("handle %u exiting\n", conn->handle);
bt_conn_put(conn);
}
struct bt_conn *bt_conn_add(struct bt_dev *dev, uint16_t handle)
{
struct bt_conn *conn = NULL;
int i;
for (i = 0; i < MAX_CONN_COUNT; i++) {
if (!conns[i].handle) {
conn = &conns[i];
break;
}
}
if (!conn)
return NULL;
memset(conn, 0, sizeof(*conn));
conn->ref = 1;
conn->state = BT_CONN_CONNECTED;
conn->handle = handle;
conn->dev = dev;
nano_fifo_init(&conn->tx_queue);
nano_fifo_init(&conn->rx_queue);
fiber_start(conn->rx_stack, BT_CONN_RX_STACK_SIZE, conn_rx_fiber,
(int)bt_conn_get(conn), 0, 7, 0);
fiber_start(conn->tx_stack, BT_CONN_TX_STACK_SIZE, conn_tx_fiber,
(int)bt_conn_get(conn), 0, 7, 0);
return conn;
}
void bt_conn_del(struct bt_conn *conn)
{
BT_DBG("handle %u\n", conn->handle);
if (conn->state != BT_CONN_CONNECTED)
return;
conn->state = BT_CONN_DISCONNECTED;
/* Send dummy buffers to wake up and kill the fibers */
nano_fifo_put(&conn->tx_queue, bt_buf_get());
nano_fifo_put(&conn->rx_queue, bt_buf_get());
bt_conn_put(conn);
}
struct bt_conn *bt_conn_lookup(uint16_t handle)
{
int i;
for (i = 0; i < MAX_CONN_COUNT; i++) {
if (conns[i].state != BT_CONN_CONNECTED)
continue;
if (conns[i].handle == handle)
return &conns[i];
}
return NULL;
}
struct bt_conn *bt_conn_get(struct bt_conn *conn)
{
conn->ref++;
BT_DBG("handle %u ref %u\n", conn->handle, conn->ref);
return conn;
}
void bt_conn_put(struct bt_conn *conn)
{
conn->ref--;
BT_DBG("handle %u ref %u\n", conn->handle, conn->ref);
if (conn->ref)
return;
conn->handle = 0;
}
struct bt_buf *bt_conn_create_pdu(struct bt_conn *conn, size_t len)
{
struct bt_dev *dev = conn->dev;
struct bt_hci_acl_hdr *hdr;
struct bt_buf *buf;
buf = bt_buf_get_reserve(dev->drv->head_reserve);
if (!buf)
return NULL;
buf->type = BT_ACL;
hdr = (void *)bt_buf_add(buf, sizeof(*hdr));
hdr->handle = sys_cpu_to_le16(conn->handle);
hdr->len = len;
return buf;
}

86
net/bluetooth/conn.h Normal file
View file

@ -0,0 +1,86 @@
/* conn.h - Bluetooth connection 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.
*/
#define BT_CONN_TX_STACK_SIZE 256
#define BT_CONN_RX_STACK_SIZE 1024
enum {
BT_CONN_DISCONNECTED,
BT_CONN_CONNECTED,
};
struct bt_conn {
struct bt_dev *dev;
uint16_t handle;
uint16_t rx_len;
struct bt_buf *rx;
/* Queue for outgoing ACL data */
struct nano_fifo tx_queue;
struct nano_fifo rx_queue;
uint8_t le_conn_interval;
uint8_t ref;
uint8_t state;
uint8_t l2_ident;
char tx_stack[BT_CONN_TX_STACK_SIZE];
char rx_stack[BT_CONN_RX_STACK_SIZE];
};
/* Prepare a new buffer to be sent over the connection */
struct bt_buf *bt_conn_create_pdu(struct bt_conn *conn, size_t len);
/* Process incoming data for a connection */
void bt_conn_recv(struct bt_conn *conn, struct bt_buf *buf, uint8_t flags);
/* Send data over a connection */
void bt_conn_send(struct bt_conn *conn, struct bt_buf *buf);
/* Add a new connection */
struct bt_conn *bt_conn_add(struct bt_dev *dev, uint16_t handle);
/* Delete an existing connection */
void bt_conn_del(struct bt_conn *conn);
/* Look up an existing connection */
struct bt_conn *bt_conn_lookup(uint16_t handle);
/* Increment conn reference count */
struct bt_conn *bt_conn_get(struct bt_conn *conn);
/* Decrement conn reference count */
void bt_conn_put(struct bt_conn *conn);

View file

@ -40,6 +40,7 @@
#include <bluetooth/bluetooth.h>
#include "hci_core.h"
#include "conn.h"
/* Stacks for the fibers */
#define RX_STACK_SIZE 1024
@ -182,6 +183,7 @@ static void hci_acl(struct bt_buf *buf)
{
struct bt_hci_acl_hdr *hdr = (void *)buf->data;
uint16_t handle, len = sys_le16_to_cpu(hdr->len);
struct bt_conn *conn;
uint8_t flags;
handle = sys_le16_to_cpu(hdr->handle);
@ -198,7 +200,14 @@ static void hci_acl(struct bt_buf *buf)
return;
}
bt_buf_put(buf);
conn = bt_conn_lookup(handle);
if (!conn) {
BT_ERR("Unable to find conn for handle %u\n", handle);
bt_buf_put(buf);
return;
}
bt_conn_recv(conn, buf, flags);
}
/* HCI event processing */
@ -207,10 +216,19 @@ static void hci_disconn_complete(struct bt_buf *buf)
{
struct bt_hci_evt_disconn_complete *evt = (void *)buf->data;
uint16_t handle = bt_acl_handle(sys_le16_to_cpu(evt->handle));
struct bt_conn *conn;
BT_DBG("status %u handle %u reason %u\n", evt->status, handle,
evt->reason);
conn = bt_conn_lookup(handle);
if (!conn) {
BT_ERR("Unable to look up conn with handle %u\n", handle);
return;
}
bt_conn_del(conn);
if (dev.adv_enable) {
struct bt_buf *buf;
@ -417,9 +435,18 @@ static void le_conn_complete(struct bt_buf *buf)
{
struct bt_hci_evt_le_conn_complete *evt = (void *)buf->data;
uint16_t handle = sys_le16_to_cpu(evt->handle);
struct bt_conn *conn;
BT_DBG("status %u handle %u role %u peer_type %u\n", evt->status,
handle, evt->role, evt->peer_addr_type);
conn = bt_conn_add(&dev, handle);
if (!conn) {
BT_ERR("Unable to add new conn for handle %u\n", handle);
return;
}
conn->le_conn_interval = sys_le16_to_cpu(evt->interval);
}
static void hci_le_meta_event(struct bt_buf *buf)

45
net/bluetooth/l2cap.h Normal file
View file

@ -0,0 +1,45 @@
/* l2cap.h - L2CAP 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.
*/
#define BT_L2CAP_CID_ATT 0x0004
#define BT_L2CAP_CID_LE_SIG 0x0005
struct bt_l2cap_hdr {
uint16_t len;
uint16_t cid;
} PACK_STRUCT;
struct bt_l2cap_sig_hdr {
uint8_t code;
uint8_t ident;
uint16_t len;
} PACK_STRUCT;