Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth: Document bt_conn_unref in public API and fix missing unref in samples #23066

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions include/bluetooth/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ void bt_conn_foreach(int type, void (*func)(struct bt_conn *conn, void *data),
*
* Look up an existing connection based on the remote address.
*
* The caller gets a new reference to the connection object which must be
* released with bt_conn_unref() once done using the object.
*
* @param id Local identity (in most cases BT_ID_DEFAULT).
* @param peer Remote address.
*
* @return Connection object or NULL if not found. The caller gets a
* new reference to the connection object which must be released with
* bt_conn_unref() once done using the object.
* @return Connection object or NULL if not found.
*/
struct bt_conn *bt_conn_lookup_addr_le(u8_t id, const bt_addr_le_t *peer);

Expand Down Expand Up @@ -280,7 +281,9 @@ int bt_conn_disconnect(struct bt_conn *conn, u8_t reason);
/** @brief Initiate an LE connection to a remote device.
*
* Allows initiate new LE link to remote peer using its address.
* Returns a new reference that the the caller is responsible for managing.
*
* The caller gets a new reference to the connection object which must be
* released with bt_conn_unref() once done using the object.
*
* This uses the General Connection Establishment procedure.
*
Expand Down Expand Up @@ -345,7 +348,8 @@ int bt_le_set_auto_conn(const bt_addr_le_t *addr,
*
* The advertising may be canceled with bt_conn_disconnect().
*
* Returns a new reference that the the caller is responsible for managing.
* The caller gets a new reference to the connection object which must be
* released with bt_conn_unref() once done using the object.
*
* @param peer Remote address.
* @param param Directed advertising parameters.
Expand Down Expand Up @@ -1023,7 +1027,9 @@ struct bt_br_conn_param {
/** @brief Initiate an BR/EDR connection to a remote device.
*
* Allows initiate new BR/EDR link to remote peer using its address.
* Returns a new reference that the the caller is responsible for managing.
*
* The caller gets a new reference to the connection object which must be
* released with bt_conn_unref() once done using the object.
*
* @param peer Remote address.
* @param param Initial connection parameters.
Expand All @@ -1036,7 +1042,9 @@ struct bt_conn *bt_conn_create_br(const bt_addr_t *peer,
/** @brief Initiate an SCO connection to a remote device.
*
* Allows initiate new SCO link to remote peer using its address.
* Returns a new reference that the the caller is responsible for managing.
*
* The caller gets a new reference to the connection object which must be
* released with bt_conn_unref() once done using the object.
*
* @param peer Remote address.
*
Expand Down
34 changes: 21 additions & 13 deletions samples/bluetooth/central/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type,
default_conn = bt_conn_create_le(addr, BT_LE_CONN_PARAM_DEFAULT);
}

static void start_scan(void)
{
int err;

/* This demo doesn't require active scan */
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
return;
}

printk("Scanning successfully started\n");
}

static void connected(struct bt_conn *conn, u8_t err)
{
char addr[BT_ADDR_LE_STR_LEN];
Expand All @@ -58,6 +72,11 @@ static void connected(struct bt_conn *conn, u8_t err)

if (err) {
printk("Failed to connect to %s (%u)\n", addr, err);

bt_conn_unref(default_conn);
default_conn = NULL;

start_scan();
return;
}

Expand All @@ -73,7 +92,6 @@ static void connected(struct bt_conn *conn, u8_t err)
static void disconnected(struct bt_conn *conn, u8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
int err;

if (conn != default_conn) {
return;
Expand All @@ -86,11 +104,7 @@ static void disconnected(struct bt_conn *conn, u8_t reason)
bt_conn_unref(default_conn);
default_conn = NULL;

/* This demo doesn't require active scan */
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
}
start_scan();
}

static struct bt_conn_cb conn_callbacks = {
Expand All @@ -112,11 +126,5 @@ void main(void)

bt_conn_cb_register(&conn_callbacks);

err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, device_found);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
return;
}

printk("Scanning successfully started\n");
start_scan();
}
92 changes: 47 additions & 45 deletions samples/bluetooth/central_hr/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,6 @@ static u8_t discover_func(struct bt_conn *conn,
return BT_GATT_ITER_STOP;
}

static void connected(struct bt_conn *conn, u8_t conn_err)
{
char addr[BT_ADDR_LE_STR_LEN];
int err;

bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

if (conn_err) {
printk("Failed to connect to %s (%u)\n", addr, conn_err);
return;
}

printk("Connected: %s\n", addr);

if (conn == default_conn) {
memcpy(&uuid, BT_UUID_HRS, sizeof(uuid));
discover_params.uuid = &uuid.uuid;
discover_params.func = discover_func;
discover_params.start_handle = 0x0001;
discover_params.end_handle = 0xffff;
discover_params.type = BT_GATT_DISCOVER_PRIMARY;

err = bt_gatt_discover(default_conn, &discover_params);
if (err) {
printk("Discover failed(err %d)\n", err);
return;
}
}
}

static bool eir_found(struct bt_data *data, void *user_data)
{
bt_addr_le_t *addr = user_data;
Expand Down Expand Up @@ -180,8 +150,10 @@ static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type,
}
}

static int scan_start(void)
static void start_scan(void)
{
int err;

/* Use active scanning and disable duplicate filtering to handle any
* devices that might update their advertising data at runtime. */
struct bt_le_scan_param scan_param = {
Expand All @@ -191,16 +163,56 @@ static int scan_start(void)
.window = BT_GAP_SCAN_FAST_WINDOW,
};

return bt_le_scan_start(&scan_param, device_found);
err = bt_le_scan_start(&scan_param, device_found);
if (err) {
printk("Scanning failed to start (err %d)\n", err);
return;
}

printk("Scanning successfully started\n");
}

static void disconnected(struct bt_conn *conn, u8_t reason)
static void connected(struct bt_conn *conn, u8_t conn_err)
{
char addr[BT_ADDR_LE_STR_LEN];
int err;

bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

if (conn_err) {
printk("Failed to connect to %s (%u)\n", addr, conn_err);

bt_conn_unref(default_conn);
default_conn = NULL;

start_scan();
return;
}

printk("Connected: %s\n", addr);

if (conn == default_conn) {
memcpy(&uuid, BT_UUID_HRS, sizeof(uuid));
discover_params.uuid = &uuid.uuid;
discover_params.func = discover_func;
discover_params.start_handle = 0x0001;
discover_params.end_handle = 0xffff;
discover_params.type = BT_GATT_DISCOVER_PRIMARY;

err = bt_gatt_discover(default_conn, &discover_params);
if (err) {
printk("Discover failed(err %d)\n", err);
return;
}
}
}

static void disconnected(struct bt_conn *conn, u8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];

bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);

if (default_conn != conn) {
Expand All @@ -210,10 +222,7 @@ static void disconnected(struct bt_conn *conn, u8_t reason)
bt_conn_unref(default_conn);
default_conn = NULL;

err = scan_start();
if (err) {
printk("Scanning failed to start (err %d)\n", err);
}
start_scan();
}

static struct bt_conn_cb conn_callbacks = {
Expand All @@ -235,12 +244,5 @@ void main(void)

bt_conn_cb_register(&conn_callbacks);

err = scan_start();

if (err) {
printk("Scanning failed to start (err %d)\n", err);
return;
}

printk("Scanning successfully started\n");
start_scan();
}