From ed11ca1744f98fadd3ffd847660931d530522f12 Mon Sep 17 00:00:00 2001 From: Joakim Andersson Date: Sun, 23 Feb 2020 11:13:13 +0100 Subject: [PATCH] Bluetooth: host: Add error code to directed advertiser Add error code to API for starting directed advertiser. Also rename the API in order to follow the established naming pattern. Signed-off-by: Joakim Andersson --- include/bluetooth/conn.h | 23 +++++++++++++++++++---- subsys/bluetooth/host/conn.c | 16 ++++++++-------- subsys/bluetooth/shell/bt.c | 7 ++++--- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/bluetooth/conn.h b/include/bluetooth/conn.h index 64116d09304..afa23629648 100644 --- a/include/bluetooth/conn.h +++ b/include/bluetooth/conn.h @@ -461,13 +461,28 @@ int bt_le_set_auto_conn(const bt_addr_le_t *addr, * 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. + * @param[in] peer Remote address. + * @param[in] param Directed advertising parameters. + * @param[out] conn Valid connection object on success. * - * @return Valid connection object on success or NULL otherwise. + * @return Zero on success or (negative) error code on failure. */ +int bt_conn_le_create_slave(const bt_addr_le_t *peer, + const struct bt_le_adv_param *param, + struct bt_conn **conn); + +__deprecated static inline struct bt_conn *bt_conn_create_slave_le(const bt_addr_le_t *peer, - const struct bt_le_adv_param *param); + const struct bt_le_adv_param *param) +{ + struct bt_conn *conn; + + if (bt_conn_le_create_slave(peer, param, &conn)) { + return NULL; + } + + return conn; +} /** Security level. */ typedef enum __packed { diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 304de3d4c5f..f14a14a6736 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -2373,8 +2373,9 @@ int bt_le_set_auto_conn(const bt_addr_le_t *addr, #endif /* CONFIG_BT_CENTRAL */ #if defined(CONFIG_BT_PERIPHERAL) -struct bt_conn *bt_conn_create_slave_le(const bt_addr_le_t *peer, - const struct bt_le_adv_param *param) +int bt_conn_le_create_slave(const bt_addr_le_t *peer, + const struct bt_le_adv_param *param, + struct bt_conn **ret_conn) { int err; struct bt_conn *conn; @@ -2397,25 +2398,24 @@ struct bt_conn *bt_conn_create_slave_le(const bt_addr_le_t *peer, BT_WARN("Found valid connection in %s state", state2str(conn->state)); bt_conn_unref(conn); - return NULL; + return -EINVAL; } conn = bt_conn_add_le(param->id, peer); if (!conn) { - return NULL; + return -ENOMEM; } bt_conn_set_state(conn, BT_CONN_CONNECT_DIR_ADV); err = bt_le_adv_start_internal(¶m_int, NULL, 0, NULL, 0, peer); if (err) { - BT_WARN("Directed advertising could not be started: %d", err); - bt_conn_unref(conn); - return NULL; + return err; } - return conn; + *ret_conn = conn; + return 0; } #endif /* CONFIG_BT_PERIPHERAL */ diff --git a/subsys/bluetooth/shell/bt.c b/subsys/bluetooth/shell/bt.c index 6d322f94597..ae8222cd9e0 100644 --- a/subsys/bluetooth/shell/bt.c +++ b/subsys/bluetooth/shell/bt.c @@ -790,9 +790,10 @@ static int cmd_directed_adv(const struct shell *shell, } } - conn = bt_conn_create_slave_le(&addr, param); - if (!conn) { - shell_error(shell, "Failed to start directed advertising"); + err = bt_conn_le_create_slave(&addr, param, &conn); + if (err) { + shell_error(shell, "Failed to start directed advertising (%d)", + err); return -ENOEXEC; } else { shell_print(shell, "Started directed advertising");