Bluetooth: Move bt_conn timeout handling into conn.c

Change-Id: I865e86d41e2744a41653195f2b99d365426aee7d
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2015-07-22 13:03:51 +03:00 committed by Anas Nashif
commit 9e1c434ad9
3 changed files with 45 additions and 37 deletions

View file

@ -54,6 +54,9 @@
#define BT_DBG(fmt, ...)
#endif
/* How long until we cancel HCI_LE_Create_Connection */
#define CONN_TIMEOUT (3 * sys_clock_ticks_per_sec)
static struct bt_conn conns[CONFIG_BLUETOOTH_MAX_CONN];
#if defined(CONFIG_BLUETOOTH_DEBUG_CONN)
@ -294,6 +297,17 @@ struct bt_conn *bt_conn_add(const bt_addr_le_t *peer, uint8_t role)
return conn;
}
static void timeout_fiber(int arg1, int arg2)
{
struct bt_conn *conn = (struct bt_conn *)arg1;
ARG_UNUSED(arg2);
conn->timeout = NULL;
bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
bt_conn_put(conn);
}
void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state)
{
bt_conn_state_t old_state;
@ -308,13 +322,29 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state)
old_state = conn->state;
conn->state = state;
/* Take a reference for the first state transition after
* bt_conn_add() and keep it until reaching DISCONNECTED again.
*/
if (old_state == BT_CONN_DISCONNECTED) {
/* Actions needed for exiting the old state */
switch (old_state) {
case BT_CONN_DISCONNECTED:
/* Take a reference for the first state transition after
* bt_conn_add() and keep it until reaching DISCONNECTED
* again.
*/
bt_conn_get(conn);
break;
case BT_CONN_CONNECT:
if (conn->timeout) {
fiber_fiber_delayed_start_cancel(conn->timeout);
conn->timeout = NULL;
/* Drop the reference taken by timeout fiber */
bt_conn_put(conn);
}
break;
default:
break;
}
/* Actions needed for entering the new state */
switch (conn->state){
case BT_CONN_CONNECTED:
nano_fifo_init(&conn->tx_queue);
@ -337,7 +367,15 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state)
break;
case BT_CONN_CONNECT_SCAN:
break;
case BT_CONN_CONNECT:
/* Add LE Create Connection timeout */
conn->timeout = fiber_delayed_start(conn->stack,
sizeof(conn->stack),
timeout_fiber,
(int)bt_conn_get(conn),
0, 7, 0, CONN_TIMEOUT);
break;
case BT_CONN_DISCONNECT:
break;
default:
@ -507,7 +545,6 @@ static int bt_hci_connect_le_cancel(struct bt_conn *conn)
if (conn->timeout) {
fiber_fiber_delayed_start_cancel(conn->timeout);
conn->timeout = NULL;
/* Drop the reference took by timeout fiber */

View file

@ -85,9 +85,9 @@ struct bt_conn {
/* Handle allowing to cancel timeout fiber */
void *timeout;
/* Stack for Tx fiber and timeout fiber.
* Since this fibers don't overlap, one stack can be used by both of
* them.
/* Stack for TX fiber and timeout fiber.
* Since these fibers don't overlap, one stack can be used by
* both of them.
*/
BT_STACK(stack, 256);
};

View file

@ -58,8 +58,6 @@
#define ACL_IN_MAX 7
#define ACL_OUT_MAX 7
#define BT_CREATE_CONN_TIMEOUT (3 * sys_clock_ticks_per_sec)
/* Stacks for the fibers */
static BT_STACK_NOINIT(rx_fiber_stack, 1024);
static BT_STACK_NOINIT(rx_prio_fiber_stack, 256);
@ -741,15 +739,6 @@ static void le_conn_complete(struct bt_buf *buf)
conn = bt_conn_lookup_state(&evt->peer_addr, BT_CONN_CONNECT);
}
if (conn && conn->timeout) {
fiber_fiber_delayed_start_cancel(conn->timeout);
conn->timeout = NULL;
/* Drop the reference took by timeout fiber */
bt_conn_put(conn);
}
if (evt->status) {
if (!conn) {
return;
@ -794,17 +783,6 @@ static void le_conn_complete(struct bt_buf *buf)
bt_le_scan_update();
}
static void timeout_fiber(int arg1, int arg2)
{
struct bt_conn *conn = (struct bt_conn *)arg1;
ARG_UNUSED(arg2);
conn->timeout = NULL;
bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
bt_conn_put(conn);
}
static void check_pending_conn(const bt_addr_le_t *addr, uint8_t evtype,
struct bt_keys *keys)
{
@ -835,13 +813,6 @@ static void check_pending_conn(const bt_addr_le_t *addr, uint8_t evtype,
bt_conn_set_state(conn, BT_CONN_CONNECT);
/* Add LE Create Connection timeout */
conn->timeout = fiber_fiber_delayed_start(conn->stack,
sizeof(conn->stack),
timeout_fiber,
(int)bt_conn_get(conn), 0, 7,
0, BT_CREATE_CONN_TIMEOUT);
done:
bt_conn_put(conn);
}