Skip to content

Commit

Permalink
Samples: Bluetooth: HCI error prints to use bt_hci_err_to_str()
Browse files Browse the repository at this point in the history
When developing Bluetooth applications,
you typically run into some errors.
If you are an experienced Bluetooth developer,
you would typically know how to translate the error codes into
string representations. Others might not.

This commit starts using `bt_hci_err_to_str()` for all
samples under `samples/bluetooth`. Similar changes can be
done in other applications and samples.

To enable string printing, enable CONFIG_BT_HCI_ERR_TO_STR.

The format used is equal to the one added in
zephyrproject-rtos/zephyr#75442.

Signed-off-by: Rubin Gerritsen <[email protected]>
  • Loading branch information
rugeGerritsen authored and rlubos committed Aug 5, 2024
1 parent c177ed8 commit 7359e3d
Show file tree
Hide file tree
Showing 37 changed files with 107 additions and 80 deletions.
6 changes: 4 additions & 2 deletions samples/bluetooth/central_and_peripheral_hr/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/hci.h>
#include <bluetooth/gatt_dm.h>
#include <bluetooth/scan.h>

Expand Down Expand Up @@ -228,7 +229,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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);
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
bt_hci_err_to_str(conn_err));

if (conn == central_conn) {
bt_conn_unref(central_conn);
Expand Down Expand Up @@ -268,7 +270,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

if (conn == central_conn) {
dk_set_led_off(CENTRAL_CON_STATUS_LED);
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/central_bas/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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);
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
bt_hci_err_to_str(conn_err));
if (conn == default_conn) {
bt_conn_unref(default_conn);
default_conn = NULL;
Expand Down Expand Up @@ -203,7 +204,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

if (default_conn != conn) {
return;
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/central_hids/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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);
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
bt_hci_err_to_str(conn_err));
if (conn == default_conn) {
bt_conn_unref(default_conn);
default_conn = NULL;
Expand Down Expand Up @@ -220,7 +221,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
auth_conn = NULL;
}

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

if (bt_hogp_assign_check(&hogp)) {
printk("HIDS client active - releasing");
Expand Down
6 changes: 4 additions & 2 deletions samples/bluetooth/central_hr_coded/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/hci.h>
#include <bluetooth/gatt_dm.h>
#include <bluetooth/scan.h>
#include <bluetooth/services/hrs_client.h>
Expand Down Expand Up @@ -189,7 +190,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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);
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
bt_hci_err_to_str(conn_err));

bt_conn_unref(default_conn);
default_conn = NULL;
Expand Down Expand Up @@ -228,7 +230,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

if (default_conn != conn) {
return;
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/central_nfc_pairing/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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);
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
bt_hci_err_to_str(conn_err));
if (conn == default_conn) {
bt_conn_unref(default_conn);
default_conn = NULL;
Expand Down Expand Up @@ -181,7 +182,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

if (default_conn != conn) {
return;
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/central_smp_client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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);
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
bt_hci_err_to_str(conn_err));
if (conn == default_conn) {
bt_conn_unref(default_conn);
default_conn = NULL;
Expand Down Expand Up @@ -186,7 +187,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

if (default_conn != conn) {
return;
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/central_uart/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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)", addr, conn_err);
LOG_INF("Failed to connect to %s, 0x%02x %s", addr, conn_err,
bt_hci_err_to_str(conn_err));

if (default_conn == conn) {
bt_conn_unref(default_conn);
Expand Down Expand Up @@ -400,7 +401,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

LOG_INF("Disconnected: %s (reason %u)", addr, reason);
LOG_INF("Disconnected: %s, reason 0x%02x %s", addr, reason, bt_hci_err_to_str(reason));

if (default_conn != conn) {
return;
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/conn_time_sync/src/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/sys/atomic.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/hci.h>
#include <bluetooth/services/nus.h>
#include <../subsys/bluetooth/host/conn_internal.h>
#include "conn_time_sync.h"
Expand Down Expand Up @@ -192,7 +193,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

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

scan_start();
return;
Expand Down Expand Up @@ -227,7 +228,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

conn_count--;

Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/conn_time_sync/src/peripheral.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/sys/atomic.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/hci.h>
#include <bluetooth/services/nus.h>
#include <bluetooth/services/nus_client.h>
#include <../subsys/bluetooth/host/conn_internal.h>
Expand Down Expand Up @@ -84,7 +85,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

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

adv_start();
return;
Expand All @@ -99,7 +100,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

adv_start();
}
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/direction_finding_central/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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);
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
bt_hci_err_to_str(conn_err));

bt_conn_unref(default_conn);
default_conn = NULL;
Expand All @@ -219,7 +220,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

if (default_conn != conn) {
return;
Expand Down
4 changes: 2 additions & 2 deletions samples/bluetooth/direction_finding_peripheral/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void enable_cte_response(struct bt_conn *conn)
static void connected(struct bt_conn *conn, uint8_t err)
{
if (err) {
printk("Connection failed (err 0x%02x)\n", err);
printk("Connection failed, err 0x%02x %s\n", err, bt_hci_err_to_str(err));
} else {
printk("Connected\n");

Expand All @@ -84,7 +84,7 @@ static void connected(struct bt_conn *conn, uint8_t err)

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
printk("Disconnected (reason 0x%02x)\n", reason);
printk("Disconnected, reason 0x%02x %s\n", reason, bt_hci_err_to_str(reason));
}

BT_CONN_CB_DEFINE(conn_callbacks) = {
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/event_trigger/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_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);
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
bt_hci_err_to_str(conn_err));

if (conn == active_conn) {
bt_conn_unref(active_conn);
Expand All @@ -148,7 +149,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

if (conn == active_conn) {
bt_conn_unref(active_conn);
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/fast_pair/input_device/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <zephyr/sys/__assert.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/settings/settings.h>
#include <bluetooth/services/fast_pair/fast_pair.h>
#include <bluetooth/adv_prov/fast_pair.h>
Expand Down Expand Up @@ -200,7 +201,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
__ASSERT_NO_MSG(!peer);

if (err) {
LOG_WRN("Connection failed (err %u)", err);
LOG_WRN("Connection failed, err 0x%02x %s", err, bt_hci_err_to_str(err));
ret = k_work_submit(&bt_adv_restart);
__ASSERT_NO_MSG(ret == 1);
return;
Expand All @@ -214,7 +215,7 @@ static void connected(struct bt_conn *conn, uint8_t err)

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
LOG_INF("Disconnected (reason %u)", reason);
LOG_INF("Disconnected, reason 0x%02x %s", reason, bt_hci_err_to_str(reason));

dk_set_led_off(CON_STATUS_LED);
peer = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/iso.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/sys/ring_buffer.h>

#include "iso_combined_bis_and_cis.h"
Expand Down Expand Up @@ -226,7 +227,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

if (err) {
LOG_INF("Failed to connect to %s (%u)", addr, err);
LOG_INF("Failed to connect to %s, 0x%02x %s", addr, err, bt_hci_err_to_str(err));

scan_start();
return;
Expand Down Expand Up @@ -263,7 +264,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

LOG_INF("Disconnected: %s (reason 0x%02x)", addr, reason);
LOG_INF("Disconnected: %s, reason 0x%02x %s", addr, reason, bt_hci_err_to_str(reason));

scan_start();
}
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/iso_time_sync/src/cis_central.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/iso.h>
#include <zephyr/bluetooth/hci.h>

#include "iso_time_sync.h"

Expand Down Expand Up @@ -127,7 +128,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

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

scan_start();
return;
Expand Down Expand Up @@ -162,7 +163,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

scan_start();
}
Expand Down
5 changes: 3 additions & 2 deletions samples/bluetooth/iso_time_sync/src/cis_peripheral.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/iso.h>
#include <zephyr/bluetooth/hci.h>

#include "iso_time_sync.h"

Expand All @@ -30,7 +31,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

if (err) {
printk("Failed to connect to %s (%u)\n", addr, err);
printk("Failed to connect to %s 0x%02x %s\n", addr, err, bt_hci_err_to_str(err));
return;
}

Expand All @@ -43,7 +44,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)

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

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

static struct bt_conn_cb conn_callbacks = {
Expand Down
4 changes: 2 additions & 2 deletions samples/bluetooth/llpm/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static void scan_start(void)
static void connected(struct bt_conn *conn, uint8_t err)
{
if (err) {
printk("Connection failed (err %u)\n", err);
printk("Connection failed, err 0x%02x %s\n", err, bt_hci_err_to_str(err));
return;
}

Expand Down Expand Up @@ -209,7 +209,7 @@ static void connected(struct bt_conn *conn, uint8_t err)

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
printk("Disconnected (reason %u)\n", reason);
printk("Disconnected, reason 0x%02x %s\n", reason, bt_hci_err_to_str(reason));

test_ready = false;

Expand Down
Loading

0 comments on commit 7359e3d

Please sign in to comment.