Bluetooth: Make connection lookup functions return a new ref

To be able to make these APIs usable for apps where the calls may
happen from preemptible tasks, it's safest to return a dedicated
reference and let the caller do bt_conn_put() once its done with the
object.

Change-Id: If227e088385b8c6f61f4061a54c745c0bff7a6c3
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2015-06-17 12:42:54 +03:00 committed by Anas Nashif
commit 029046e02e
3 changed files with 21 additions and 11 deletions

View file

@ -306,7 +306,7 @@ struct bt_conn *bt_conn_lookup_handle(uint16_t handle)
}
if (conns[i].handle == handle) {
return &conns[i];
return bt_conn_get(&conns[i]);
}
}
@ -323,7 +323,7 @@ struct bt_conn *bt_conn_lookup_addr_le(const bt_addr_le_t *peer)
}
if (!bt_addr_le_cmp(peer, &conns[i].dst)) {
return &conns[i];
return bt_conn_get(&conns[i]);
}
}

View file

@ -34,6 +34,7 @@
#include <toolchain.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <misc/byteorder.h>
#include <misc/util.h>
@ -46,6 +47,7 @@
#include "hci_core.h"
#include "conn_internal.h"
#include "keys.h"
#include "l2cap.h"
#include "att.h"
@ -228,19 +230,18 @@ int bt_gatt_attr_write_ccc(const bt_addr_le_t *peer,
uint8_t len, uint16_t offset)
{
struct _bt_gatt_ccc *ccc = attr->user_data;
struct bt_conn *conn;
const uint16_t *data = buf;
bool bonded;
size_t i;
if (len != sizeof(*data) || offset) {
return -EINVAL;
}
conn = bt_conn_lookup_addr_le(peer);
if (!conn) {
BT_WARN("%s not connected", bt_addr_le_str(peer));
return -ENOTCONN;
}
if (bt_keys_get_addr(peer))
bonded = true;
else
bonded = false;
for (i = 0; i < ccc->cfg_len; i++) {
/* Check for existing configuration */
@ -255,7 +256,7 @@ int bt_gatt_attr_write_ccc(const bt_addr_le_t *peer,
if (!ccc->cfg[i].valid) {
bt_addr_le_copy(&ccc->cfg[i].peer, peer);
/* Only set valid if bonded */
ccc->cfg[i].valid = conn->keys ? 1 : 0;
ccc->cfg[i].valid = bonded;
break;
}
}
@ -338,6 +339,7 @@ static uint8_t notify_cb(const struct bt_gatt_attr *attr, void *user_data)
sizeof(*nfy) + data->len);
if (!buf) {
BT_WARN("No buffer available to send notification");
bt_conn_put(conn);
return BT_GATT_ITER_STOP;
}
@ -350,6 +352,7 @@ static uint8_t notify_cb(const struct bt_gatt_attr *attr, void *user_data)
memcpy(nfy->value, data->data, data->len);
bt_l2cap_send(conn, BT_L2CAP_CID_ATT, buf);
bt_conn_put(conn);
}
return BT_GATT_ITER_CONTINUE;

View file

@ -277,6 +277,7 @@ static void hci_acl(struct bt_buf *buf)
}
bt_conn_recv(conn, buf, flags);
bt_conn_put(conn);
}
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_PRINTK)
@ -380,6 +381,7 @@ static void hci_disconn_complete(struct bt_buf *buf)
analyze_stacks(conn, &conn);
bt_conn_del(conn);
bt_conn_put(conn);
if (dev.adv_enable) {
struct bt_buf *buf;
@ -414,6 +416,7 @@ static void hci_encrypt_change(struct bt_buf *buf)
conn->encrypt = evt->encrypt;
bt_l2cap_encrypt_change(conn);
bt_conn_put(conn);
}
static void hci_reset_complete(struct bt_buf *buf)
@ -556,6 +559,7 @@ static void hci_encrypt_key_refresh_complete(struct bt_buf *buf)
}
bt_l2cap_encrypt_change(conn);
bt_conn_put(conn);
}
static void copy_id_addr(struct bt_conn *conn, const bt_addr_le_t *addr)
@ -665,7 +669,7 @@ static void le_ltk_request(struct bt_buf *buf)
sizeof(*cp));
if (!buf) {
BT_ERR("Out of command buffers\n");
return;
goto done;
}
cp = bt_buf_add(buf, sizeof(*cp));
@ -680,7 +684,7 @@ static void le_ltk_request(struct bt_buf *buf)
sizeof(*cp));
if (!buf) {
BT_ERR("Out of command buffers\n");
return;
goto done;
}
cp = bt_buf_add(buf, sizeof(*cp));
@ -688,6 +692,9 @@ static void le_ltk_request(struct bt_buf *buf)
bt_hci_cmd_send(BT_HCI_OP_LE_LTK_REQ_NEG_REPLY, buf);
}
done:
bt_conn_put(conn);
}
static void hci_le_meta_event(struct bt_buf *buf)