Bluetooth: Refactor internal bt_conn_add() API

Delegates connection object to new connection when there exists
in pool of available connections an object with zeroed destination
LE address.
When there's no reference to the active connection object resets its
peer LE address member.
Replaces routine's input connection handle parameter with LE address.
Moves to the caller setting connection state, connection handle and
call to bt_l2cap_update_conn_param() since connection handle needs
to be valid when such update going to be made.

Change-Id: I81743a915da6cb008f9593dd1940c186a357be6b
Signed-off-by: Arkadiusz Lichwa <arkadiusz.lichwa@tieto.com>
This commit is contained in:
Arkadiusz Lichwa 2015-06-23 11:56:50 +02:00 committed by Anas Nashif
commit 5e79bfb662
3 changed files with 15 additions and 11 deletions

View file

@ -262,13 +262,14 @@ static void conn_tx_fiber(int arg1, int arg2)
bt_conn_put(conn);
}
struct bt_conn *bt_conn_add(struct bt_dev *dev, uint16_t handle, uint8_t role)
struct bt_conn *bt_conn_add(struct bt_dev *dev, const bt_addr_le_t *peer,
uint8_t role)
{
struct bt_conn *conn = NULL;
int i;
for (i = 0; i < ARRAY_SIZE(conns); i++) {
if (!conns[i].handle) {
if (!bt_addr_le_cmp(&conns[i].dst, BT_ADDR_LE_ANY)) {
conn = &conns[i];
break;
}
@ -281,14 +282,9 @@ struct bt_conn *bt_conn_add(struct bt_dev *dev, uint16_t handle, uint8_t role)
memset(conn, 0, sizeof(*conn));
conn->ref = 1;
conn->handle = handle;
conn->dev = dev;
conn->role = role;
bt_conn_set_state(conn, BT_CONN_CONNECTED);
if (role == BT_HCI_ROLE_SLAVE) {
bt_l2cap_update_conn_param(conn);
}
bt_addr_le_copy(&conn->dst, peer);
return conn;
}
@ -382,7 +378,7 @@ void bt_conn_put(struct bt_conn *conn)
return;
}
conn->handle = 0;
bt_addr_le_copy(&conn->dst, BT_ADDR_LE_ANY);
}
const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn)

View file

@ -86,7 +86,8 @@ void bt_conn_recv(struct bt_conn *conn, struct bt_buf *buf, uint8_t flags);
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, uint8_t role);
struct bt_conn *bt_conn_add(struct bt_dev *dev, const bt_addr_le_t *peer,
uint8_t role);
/* Look up an existing connection */
struct bt_conn *bt_conn_lookup_handle(uint16_t handle);

View file

@ -365,6 +365,7 @@ static void hci_disconn_complete(struct bt_buf *buf)
bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
bt_conn_put(conn);
conn->handle = 0;
if (dev.adv_enable) {
struct bt_buf *buf;
@ -579,16 +580,22 @@ static void le_conn_complete(struct bt_buf *buf)
return;
}
conn = bt_conn_add(&dev, handle, evt->role);
conn = bt_conn_add(&dev, &evt->peer_addr, evt->role);
if (!conn) {
BT_ERR("Unable to add new conn for handle %u\n", handle);
return;
}
conn->handle = handle;
conn->src.type = BT_ADDR_LE_PUBLIC;
memcpy(conn->src.val, dev.bdaddr.val, sizeof(dev.bdaddr.val));
copy_id_addr(conn, &evt->peer_addr);
conn->le_conn_interval = sys_le16_to_cpu(evt->interval);
bt_conn_set_state(conn, BT_CONN_CONNECTED);
if (evt->role == BT_HCI_ROLE_SLAVE) {
bt_l2cap_update_conn_param(conn);
}
bt_connected(conn);
bt_l2cap_connected(conn);