Code is based on Nordic UART Service Client sample on nRF Connect SDK v1.8.0
I am trying to create several client connections for different servers. The code is incomplete at the moment as the focus is on solving this error.
This is tested with a partner device flashed with the Nordic Uart Service Server.
Error:
#define EAGAIN 11 /**< No more contexts */
Error is returned when bt_conn_le_create is called
[00:02:28.366,363] [0m<inf> sdc_hci_driver: SoftDevice Controller build revision:
df c0 4e d6 1f 7c 66 09 0a f5 2b a0 98 f2 43 64 |..N..|f. ..+...Cd
62 c5 a6 2a |b..* [0m
[00:02:28.371,826] [0m<inf> central_uart: Bluetooth initialized[0m
[00:02:28.371,856] [0m<inf> central_uart: Scan module initialized[0m
[00:02:28.371,856] [0m<inf> central_uart: NUS Client module initialized[0m
[00:02:28.376,403] [0m<inf> central_uart: Scanning successfully started[0m
[00:02:34.937,377] [0m<inf> central_uart: Filters matched. Address: E3:D0:DE:84:11:A0 (random) connectable: 0[0m
[00:02:34.937,408] [1;31m<err> central_uart: Create conn to E3:D0:DE:84:11:A0 (random) failed (-11)[0m
[00:02:35.238,891] [0m<inf> central_uart: Filters matched. Address: E3:D0:DE:84:11:A0 (random) connectable: 0[0m
[00:02:35.238,922] [1;31m<err> central_uart: Create conn to E3:D0:DE:84:11:A0 (random) failed (-11)[0m
[00:02:35.345,947] [0m<inf> central_uart: Filters matched. Address: E3:D0:DE:84:11:A0 (random) connectable: 0[0m
[00:02:35.345,977] [1;31m<err> central_uart: Create conn to E3:D0:DE:84:11:A0 (random) failed (-11)[0m
[00:02:35.772,705] [0m<inf> central_uart: Filters matched. Address: E3:D0:DE:84:11:A0 (random) connectable: 0[0m
[00:02:35.772,705] [1;31m<err> central_uart: Create conn to E3:D0:DE:84:11:A0 (random) failed (-11)[0m
[00:02:35.880,004] [0m<inf> central_uart: Filters matched. Address: <log_strdup alloc failed> connectable: 0
The error is triggered on line 2590 of the conn.c library:
if (atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
return -EAGAIN;
}
Not sure what BT_DEV_EXPLICIT_SCAN means and how to change that flag.
Below is the complete code. Thanks
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
/** @file
* @brief Nordic UART Service Client sample
*/
#include <errno.h>
#include <zephyr.h>
#include <sys/byteorder.h>
#include <sys/printk.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/services/nus.h>
#include <bluetooth/services/nus_client.h>
#include <bluetooth/gatt_dm.h>
#include <bluetooth/scan.h>
#include <settings/settings.h>
#include <drivers/uart.h>
#include <logging/log.h>
#define LOG_MODULE_NAME central_uart
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define NUS_WRITE_TIMEOUT K_MSEC(150)
#define INIT_INTERVAL 0x0010 /* 10 ms */
#define INIT_WINDOW 0x0010 /* 10 ms */
#define CONN_INTERVAL 0x00A0 /* 200 ms */
#define CONN_LATENCY 0
#define CONN_TIMEOUT MIN(MAX((CONN_INTERVAL * 125 * \
MAX(CONFIG_BT_MAX_CONN, 6) / 1000), 10), 3200)
K_SEM_DEFINE(nus_write_sem, 0, 1);
static K_FIFO_DEFINE(fifo_uart_tx_data);
static K_FIFO_DEFINE(fifo_uart_rx_data);
static struct bt_conn *peripheral_connecting_conn;
static struct bt_nus_client nus_client[CONFIG_BT_MAX_CONN];
static uint8_t volatile peripheral_conn_count = 0;
static void ble_data_sent(struct bt_nus_client *nus, uint8_t err,
const uint8_t *const data, uint16_t len)
{
ARG_UNUSED(nus);
//struct uart_data_t *buf;
///* Retrieve buffer context. */
//buf = CONTAINER_OF(data, struct uart_data_t, data);
//k_free(buf);
k_sem_give(&nus_write_sem);
if (err) {
LOG_WRN("ATT error code: 0x%02X", err);
}
}
static uint8_t ble_data_received(struct bt_nus_client *nus,
const uint8_t *data, uint16_t len)
{
ARG_UNUSED(nus);
struct bt_conn_info info;
bt_conn_get_info(nus->conn, &info);
LOG_INF("BLE Data Received Id:%d", info.id);
return BT_GATT_ITER_CONTINUE;
}
static void discovery_complete(struct bt_gatt_dm *dm,
void *context)
{
struct bt_nus_client *nus = context;
LOG_INF("Service discovery completed");
bt_gatt_dm_data_print(dm);
bt_nus_handles_assign(dm, nus);
bt_nus_subscribe_receive(nus);
bt_gatt_dm_data_release(dm);
}
static void discovery_service_not_found(struct bt_conn *conn,
void *context)
{
LOG_INF("Service not found");
}
static void discovery_error(struct bt_conn *conn,
int err,
void *context)
{
LOG_WRN("Error while discovering GATT database: (%d)", err);
}
struct bt_gatt_dm_cb discovery_cb = {
.completed = discovery_complete,
.service_not_found = discovery_service_not_found,
.error_found = discovery_error,
};
static void gatt_discover(struct bt_conn *conn)
{
int err;
// TODO#1 peripheral_connecting_conn is not correctly
// initialize on bt_conn_le_create function
//if (conn != peripheral_connecting_conn) {
// return;
//}
err = bt_gatt_dm_start(conn,
BT_UUID_NUS_SERVICE,
&discovery_cb,
&nus_client[peripheral_conn_count]);
if (err) {
LOG_ERR("could not start the discovery procedure, error "
"code: %d", err);
return;
}
peripheral_conn_count++;
}
static void exchange_func(struct bt_conn *conn, uint8_t err, struct bt_gatt_exchange_params *params)
{
if (!err) {
LOG_INF("MTU exchange done");
} else {
LOG_WRN("MTU exchange failed (err %" PRIu8 ")", err);
}
}
static void connected(struct bt_conn *conn, uint8_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) {
LOG_INF("Failed to connect to %s (%d)", log_strdup(addr),
conn_err);
bt_conn_unref(peripheral_connecting_conn);
peripheral_connecting_conn = NULL;
return;
}
LOG_INF("Connected: %s", log_strdup(addr));
static struct bt_gatt_exchange_params exchange_params;
exchange_params.func = exchange_func;
err = bt_gatt_exchange_mtu(conn, &exchange_params);
if (err) {
LOG_WRN("MTU exchange failed (err %d)", err);
}
gatt_discover(conn);
peripheral_connecting_conn = NULL;
//err = bt_scan_stop();
//if ((!err) && (err != -EALREADY)) {
// LOG_ERR("Stop LE scan failed (err %d)", err);
//}
}
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
//int err;
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Disconnected: %s (reason %u)", log_strdup(addr),
reason);
peripheral_conn_count--;
//err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
//if (err) {
// LOG_ERR("Scanning failed to start (err %d)",
// err);
//}
}
static struct bt_conn_cb conn_callbacks = {
.connected = connected,
.disconnected = disconnected,
};
static void scan_filter_match(struct bt_scan_device_info *device_info,
struct bt_scan_filter_match *filter_match,
bool connectable)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(device_info->recv_info->addr, addr, sizeof(addr));
LOG_INF("Filters matched. Address: %s connectable: %d",
log_strdup(addr), connectable);
// Exit if there is already a connection being established
if (peripheral_connecting_conn)
return;
struct bt_conn_le_create_param create_param = {
.options = BT_CONN_LE_OPT_NONE,
.interval = INIT_INTERVAL,
.window = INIT_WINDOW,
.interval_coded = 0,
.window_coded = 0,
.timeout = 0,
};
struct bt_le_conn_param conn_param = {
.interval_min = CONN_INTERVAL,
.interval_max = CONN_INTERVAL,
.latency = CONN_LATENCY,
.timeout = CONN_TIMEOUT,
};
int err = bt_conn_le_create(device_info->recv_info->addr, &create_param, &conn_param,
&peripheral_connecting_conn);
if (err) {
LOG_ERR("Create conn to %s failed (%d)", log_strdup(addr), err);
}
}
static int nus_client_init(void)
{
int err;
struct bt_nus_client_init_param init = {
.cb = {
.received = ble_data_received,
.sent = ble_data_sent,
}
};
for (size_t i = 0; i < CONFIG_BT_MAX_CONN; i++) {
err = bt_nus_client_init(&nus_client[i], &init);
if (err) {
LOG_ERR("NUS Client initialization failed (err %d)", err);
return err;
}
}
LOG_INF("NUS Client module initialized");
return err;
}
BT_SCAN_CB_INIT(scan_cb, scan_filter_match, NULL,
NULL, NULL);
static int scan_init(void)
{
int err;
struct bt_scan_init_param scan_init = {
.connect_if_match = 0,
};
bt_scan_init(&scan_init);
bt_scan_cb_register(&scan_cb);
err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_NUS_SERVICE);
if (err) {
LOG_ERR("Scanning filters cannot be set (err %d)", err);
return err;
}
err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER, false);
if (err) {
LOG_ERR("Filters cannot be turned on (err %d)", err);
return err;
}
LOG_INF("Scan module initialized");
return err;
}
void main(void)
{
int err;
err = bt_enable(NULL);
if (err) {
LOG_ERR("Bluetooth init failed (err %d)", err);
return;
}
LOG_INF("Bluetooth initialized");
bt_conn_cb_register(&conn_callbacks);
int (*module_init[])(void) = {scan_init, nus_client_init};
for (size_t i = 0; i < ARRAY_SIZE(module_init); i++) {
err = (*module_init[i])();
if (err) {
return;
}
}
printk("Starting Bluetooth Central UART example\n");
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
LOG_ERR("Scanning failed to start (err %d)", err);
return;
}
LOG_INF("Scanning successfully started");
}