Skip to content

Commit 7359e3d

Browse files
rugeGerritsenrlubos
authored andcommitted
Samples: Bluetooth: HCI error prints to use bt_hci_err_to_str()
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]>
1 parent c177ed8 commit 7359e3d

File tree

37 files changed

+107
-80
lines changed

37 files changed

+107
-80
lines changed

samples/bluetooth/central_and_peripheral_hr/src/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <zephyr/bluetooth/conn.h>
99
#include <zephyr/bluetooth/uuid.h>
1010
#include <zephyr/bluetooth/gatt.h>
11+
#include <zephyr/bluetooth/hci.h>
1112
#include <bluetooth/gatt_dm.h>
1213
#include <bluetooth/scan.h>
1314

@@ -228,7 +229,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
228229
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
229230

230231
if (conn_err) {
231-
printk("Failed to connect to %s (%u)\n", addr, conn_err);
232+
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
233+
bt_hci_err_to_str(conn_err));
232234

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

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

271-
printk("Disconnected: %s (reason %u)\n", addr, reason);
273+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
272274

273275
if (conn == central_conn) {
274276
dk_set_led_off(CENTRAL_CON_STATUS_LED);

samples/bluetooth/central_bas/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
170170
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
171171

172172
if (conn_err) {
173-
printk("Failed to connect to %s (%u)\n", addr, conn_err);
173+
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
174+
bt_hci_err_to_str(conn_err));
174175
if (conn == default_conn) {
175176
bt_conn_unref(default_conn);
176177
default_conn = NULL;
@@ -203,7 +204,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
203204

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

206-
printk("Disconnected: %s (reason %u)\n", addr, reason);
207+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
207208

208209
if (default_conn != conn) {
209210
return;

samples/bluetooth/central_hids/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
182182
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
183183

184184
if (conn_err) {
185-
printk("Failed to connect to %s (%u)\n", addr, conn_err);
185+
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
186+
bt_hci_err_to_str(conn_err));
186187
if (conn == default_conn) {
187188
bt_conn_unref(default_conn);
188189
default_conn = NULL;
@@ -220,7 +221,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
220221
auth_conn = NULL;
221222
}
222223

223-
printk("Disconnected: %s (reason %u)\n", addr, reason);
224+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
224225

225226
if (bt_hogp_assign_check(&hogp)) {
226227
printk("HIDS client active - releasing");

samples/bluetooth/central_hr_coded/src/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <zephyr/bluetooth/conn.h>
2020
#include <zephyr/bluetooth/uuid.h>
2121
#include <zephyr/bluetooth/gatt.h>
22+
#include <zephyr/bluetooth/hci.h>
2223
#include <bluetooth/gatt_dm.h>
2324
#include <bluetooth/scan.h>
2425
#include <bluetooth/services/hrs_client.h>
@@ -189,7 +190,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
189190
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
190191

191192
if (conn_err) {
192-
printk("Failed to connect to %s (%u)\n", addr, conn_err);
193+
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
194+
bt_hci_err_to_str(conn_err));
193195

194196
bt_conn_unref(default_conn);
195197
default_conn = NULL;
@@ -228,7 +230,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
228230

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

231-
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
233+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
232234

233235
if (default_conn != conn) {
234236
return;

samples/bluetooth/central_nfc_pairing/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
151151
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
152152

153153
if (conn_err) {
154-
printk("Failed to connect to %s (%u)\n", addr, conn_err);
154+
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
155+
bt_hci_err_to_str(conn_err));
155156
if (conn == default_conn) {
156157
bt_conn_unref(default_conn);
157158
default_conn = NULL;
@@ -181,7 +182,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
181182

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

184-
printk("Disconnected: %s (reason %u)\n", addr, reason);
185+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
185186

186187
if (default_conn != conn) {
187188
return;

samples/bluetooth/central_smp_client/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
139139
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
140140

141141
if (conn_err) {
142-
printk("Failed to connect to %s (%u)\n", addr, conn_err);
142+
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
143+
bt_hci_err_to_str(conn_err));
143144
if (conn == default_conn) {
144145
bt_conn_unref(default_conn);
145146
default_conn = NULL;
@@ -186,7 +187,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
186187

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

189-
printk("Disconnected: %s (reason %u)\n", addr, reason);
190+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
190191

191192
if (default_conn != conn) {
192193
return;

samples/bluetooth/central_uart/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
354354
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
355355

356356
if (conn_err) {
357-
LOG_INF("Failed to connect to %s (%d)", addr, conn_err);
357+
LOG_INF("Failed to connect to %s, 0x%02x %s", addr, conn_err,
358+
bt_hci_err_to_str(conn_err));
358359

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

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

403-
LOG_INF("Disconnected: %s (reason %u)", addr, reason);
404+
LOG_INF("Disconnected: %s, reason 0x%02x %s", addr, reason, bt_hci_err_to_str(reason));
404405

405406
if (default_conn != conn) {
406407
return;

samples/bluetooth/conn_time_sync/src/central.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <zephyr/sys/atomic.h>
88
#include <zephyr/bluetooth/bluetooth.h>
99
#include <zephyr/bluetooth/conn.h>
10+
#include <zephyr/bluetooth/hci.h>
1011
#include <bluetooth/services/nus.h>
1112
#include <../subsys/bluetooth/host/conn_internal.h>
1213
#include "conn_time_sync.h"
@@ -192,7 +193,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
192193
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
193194

194195
if (err) {
195-
printk("Failed to connect to %s (%u)\n", addr, err);
196+
printk("Failed to connect to %s 0x%02x %s\n", addr, err, bt_hci_err_to_str(err));
196197

197198
scan_start();
198199
return;
@@ -227,7 +228,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
227228

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

230-
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
231+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
231232

232233
conn_count--;
233234

samples/bluetooth/conn_time_sync/src/peripheral.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <zephyr/sys/atomic.h>
88
#include <zephyr/bluetooth/bluetooth.h>
99
#include <zephyr/bluetooth/conn.h>
10+
#include <zephyr/bluetooth/hci.h>
1011
#include <bluetooth/services/nus.h>
1112
#include <bluetooth/services/nus_client.h>
1213
#include <../subsys/bluetooth/host/conn_internal.h>
@@ -84,7 +85,7 @@ static void connected(struct bt_conn *conn, uint8_t err)
8485
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
8586

8687
if (err) {
87-
printk("Failed to connect to %s (%u)\n", addr, err);
88+
printk("Failed to connect to %s 0x%02x %s\n", addr, err, bt_hci_err_to_str(err));
8889

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

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

102-
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
103+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
103104

104105
adv_start();
105106
}

samples/bluetooth/direction_finding_central/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
197197
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
198198

199199
if (conn_err) {
200-
printk("Failed to connect to %s (%u)\n", addr, conn_err);
200+
printk("Failed to connect to %s, 0x%02x %s\n", addr, conn_err,
201+
bt_hci_err_to_str(conn_err));
201202

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

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

222-
printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
223+
printk("Disconnected: %s, reason 0x%02x %s\n", addr, reason, bt_hci_err_to_str(reason));
223224

224225
if (default_conn != conn) {
225226
return;

0 commit comments

Comments
 (0)