zephyr/tests
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
..
application_development hwmv2: Introduce Hardware model version 2 and convert devices 2024-03-02 16:56:33 -05:00
arch tests: arch: common: stack_unwind: fix symtab test 2024-07-09 19:44:08 -04:00
benchmarks tests: latency_measure: Restructure configurations 2024-06-12 14:33:47 +03:00
bluetooth Bluetooth: Conditionally print out HCI error codes as strings 2024-07-30 18:25:56 +01:00
boards tests: boards: nrf: qdec: add tests for qdec for nrf 2024-07-28 07:31:15 +03:00
boot tests: boot: Add nrf54l15pdk to platform_allow list in with_mcumgr tests 2024-06-04 13:41:29 +02:00
bsim Bluetooth: BAP: Scan delegator add src without PA sync 2024-07-28 07:31:06 +03:00
cmake tests: cmake: fix test identifier 2024-07-18 06:47:57 -04:00
crypto modules: mbedtls: fix MBEDTLS_PSA_P256M_DRIVER_RAW Kconfig option 2024-07-15 10:36:58 -04:00
drivers drivers: sensor: add driver for lsm9ds1 sensor 2024-07-29 14:21:24 +02:00
integration/kernel Kernel: Integration test 2024-06-19 13:41:06 +02:00
kconfig/configdefault
kernel tests: device: test CONFIG_DEVICE_DT_METADATA=y 2024-07-27 15:09:10 +03:00
lib json: test: Add test for array elements alignment 2024-07-09 19:02:25 +02:00
misc tests: do not get ZEPHYR_BASE from ENV 2024-07-11 13:09:53 -04:00
modules posix: move ioctl() from fdtable to posix 2024-06-14 14:01:05 -04:00
net tests: net: lib: http_server: core: Add test for RST_STREAM frame 2024-07-27 15:08:40 +03:00
posix tests: posix: unistd_h: mark fsync() & ftruncate() as supported 2024-07-29 14:14:33 +02:00
robot resc: cmake: robot: Rename Renode variable 2024-05-28 12:54:52 +02:00
subsys tests: modem: cmux: add missing timeout param 2024-07-30 11:44:04 +02:00
unit tests: unit: util: Add unit test of utf8_trunc and utf8_lcpy 2024-06-27 08:48:31 -04:00
ztest lib: cpp: remove deprecated Kconfigs 2024-06-18 14:33:58 -04:00
test_config.yaml