zephyr/subsys/bluetooth/host
Rubin Gerritsen bfc0cdc905 Bluetooth: Conditionally print out HCI error codes as strings
When developing Bluetooth applications, you typically run into
some errors. If you are an experienced Bluetooth developer,
you would typically have an HCI error lookup table in your memory.
Others might not.

This commit utilizes defines CONFIG_BT_DEBUG_HCI_ERR_TO_STR
and utilizes bt_hci_err_to_str() to print out HCI error strings
when enabled to improve the user experience.

Several alternatives where considered. This approach was chosen
as it had the best balance between readability, code size, and
implementation complexity.

The alternatives are listed below as a reference.

1. Macro defined format specifier:

```c
  #define HCI_ERR_FMT "%s"
  #define BT_HCI_ERR_TO_STR(err) (err)
  #define HCI_ERR_FMT "%d"
  #define BT_HCI_ERR_TO_STR(err) bt_hci_err_to_str((err))

LOG_INF("The event contained " HCI_ERR_FMT " as status",
	BT_HCI_ERR_TO_STR(err));
```
Advantage: Space efficient: Code size does not increase
Disadvantage: Code becomes hard to read

2. Format specifier to always include both integer and string:

```c
static inline const char bt_hci_err_to_str(err)
{
	return "";
}

LOG_INF("The event contained %s(0x%02x) as status",
	bt_hci_err_to_str(err), err);
```

Advantage: Simple to use, implement, and read,
Disadvantage: Increases code size when CONFIG_BT_DEBUG_HCI_ERR_TO_STR
is disabled. The compiler seems unable to optimize away the unused
format specifier. Note: The size increase is only present when
logging is enabled.

3. Always print as string, allocate a stack variable when printing:

```c
const char *bt_hci_err_to_str(char *dst, size_t dst_size, uint8_t err)
{
  snprintf(dst, dst_size, 0x%02x, err);
  return dst;
}

LOG_INF("The event contained %s as status", BT_HCI_ERR_TO_STR(err));
```

Advantage: Very easy to read.
Disadvantage: Printing error codes becomes slow as it involves calling
snprint.

4. Implement a custom printf specifier, for example E.

   This requires a global CONFIG_ERR_AS_STR as I assume we cannot have
   one specifier for each type of error code.
   Also, I assume we cannot start adding specifiers for each subsystem.

```c
  #define BT_HCI_ERR_TO_STR(err) (err)
  #define BT_HCI_ERR_TO_STR(err) bt_hci_err_to_str((err))

LOG_INF("The event contained %E as status", BT_HCI_ERR_TO_STR(err));
```

Advantage: Both efficient code and readable code.
Disadvantage: This requires a global CONFIG_ERR_AS_STR as I assume
we cannot have one specifier for each type of error code.
Also, I assume we cannot start adding specifiers for each subsystem.
That is, this approach is hard to implement correctly in a scalable
way.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
2024-07-30 18:25:56 +01:00
..
classic kconfig: replace known integer constants with variables 2024-07-27 20:49:15 +03:00
addr_internal.h
addr.c
adv.c Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
adv.h
aes_ccm.c
att_internal.h
att.c Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
buf_view.h everywhere: replace double words 2024-06-22 05:40:22 -04:00
buf.c Bluetooth: host: Use correct user_data size for hci_rx_pool 2024-07-13 10:25:31 -04:00
CMakeLists.txt bt-host: add option to use PSA APIs instead of TinyCrypt 2024-06-14 15:41:34 +02:00
conn_internal.h Bluetooth: host: Send host num completes as early as possible 2024-07-27 15:18:51 +03:00
conn.c Bluetooth: Host: Remove work cancel before submit 2024-07-27 20:50:21 +03:00
crypto_psa.c bt-host: add option to use PSA APIs instead of TinyCrypt 2024-06-14 15:41:34 +02:00
crypto_tc.c bt-host: add option to use PSA APIs instead of TinyCrypt 2024-06-14 15:41:34 +02:00
crypto.h
data.c
direction_internal.h
direction.c Bluetooth: HCI: Expose bt_hci_per_adv_sync_lookup_handle() 2024-07-27 15:09:00 +03:00
ecc.c Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
ecc.h
gatt_internal.h
gatt.c Bluetooth: GATT: factor out notify callback 2024-07-05 18:38:17 +02:00
hci_common.c
hci_core.c Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
hci_core.h Bluetooth: host: Send host num completes as early as possible 2024-07-27 15:18:51 +03:00
hci_ecc.c Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
hci_ecc.h
hci_raw_internal.h Bluetooth: host: Add support for new-style HCI drivers 2024-06-11 19:42:49 -04:00
hci_raw.c Bluetooth: host: hci_raw: Use existing H4 defines from hci_types.h 2024-06-11 19:42:49 -04:00
id.c bluetooth: host: fix typo 2024-06-25 10:27:23 -04:00
id.h
iso_internal.h Bluetooth: host: Change TX pattern (push -> pull) 2024-06-12 18:51:34 +02:00
iso.c Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
Kconfig kconfig: replace known integer constants with variables 2024-07-27 20:49:15 +03:00
Kconfig.gatt kconfig: replace known integer constants with variables 2024-07-27 20:49:15 +03:00
Kconfig.l2cap kconfig: replace known integer constants with variables 2024-07-27 20:49:15 +03:00
keys.c Bluetooth: Host: Fix not clearing IDs and keys upon bt_disable() 2024-05-28 09:11:52 -07:00
keys.h Bluetooth: Host: Fix not clearing IDs and keys upon bt_disable() 2024-05-28 09:11:52 -07:00
l2cap_internal.h Bluetooth: L2CAP: Make bt_l2cap_send_pdu() 2024-06-12 18:51:34 +02:00
l2cap.c bluetooth: host: fix typo 2024-06-25 10:27:23 -04:00
long_wq.c
long_wq.h
monitor.c lib: os: add a header for printk hook functions 2024-07-27 10:41:01 +03:00
monitor.h
scan.c Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
scan.h Bluetooth: HCI: Expose bt_hci_per_adv_sync_lookup_handle() 2024-07-27 15:09:00 +03:00
settings.c
settings.h
smp_null.c Bluetooth: L2CAP: Make bt_l2cap_send_pdu() 2024-06-12 18:51:34 +02:00
smp.c Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
smp.h Bluetooth: Mark bt_<type>_err_to_str() APIs experimental 2024-07-11 13:11:59 -04:00
testing.c Bluetooth: Host: Callback registering functions to return status 2024-06-13 08:03:28 -04:00
testing.h
uuid.c