Bluetooth: Add bt_conn_lookup_state function

bt_conn_lookup_state looks up for connection with "peer" in specific
state "state". Returns NULL if there is no connection with peer or
connection state differs from the given one.

Passing BT_ADDR_LE_ANY will return the first connection with the
specific state.

Change-Id: Iaa3bb22c9aa31192b8782adb6b11c5051b403758
Signed-off-by: Mariusz Skamra <mariusz.skamra@tieto.com>
This commit is contained in:
Mariusz Skamra 2015-06-25 11:31:17 +02:00 committed by Anas Nashif
commit 648a4923ed
2 changed files with 25 additions and 0 deletions

View file

@ -361,6 +361,25 @@ struct bt_conn *bt_conn_lookup_addr_le(const bt_addr_le_t *peer)
return NULL;
}
struct bt_conn *bt_conn_lookup_state(const bt_addr_le_t *peer,
const bt_conn_state_t state)
{
int i;
for (i = 0; i < ARRAY_SIZE(conns); i++) {
if (bt_addr_le_cmp(peer, BT_ADDR_LE_ANY) &&
bt_addr_le_cmp(peer, &conns[i].dst)) {
continue;
}
if (conns[i].state == state) {
return bt_conn_get(&conns[i]);
}
}
return NULL;
}
struct bt_conn *bt_conn_get(struct bt_conn *conn)
{
atomic_inc(&conn->ref);

View file

@ -93,5 +93,11 @@ struct bt_conn *bt_conn_add(struct bt_dev *dev, const bt_addr_le_t *peer,
/* Look up an existing connection */
struct bt_conn *bt_conn_lookup_handle(uint16_t handle);
/* Look up a connection state. For BT_ADDR_LE_ANY, returns the first connection
* with the specific state
*/
struct bt_conn *bt_conn_lookup_state(const bt_addr_le_t *peer,
const bt_conn_state_t state);
/* Set connection object in certain state and perform action related to state */
void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state);