Bluetooth: Host: Define bt_security_err_to_str()

This can be useful if application developers
want to print them in the applications.

Later we can also use them in
the host to improve debuggability.

Signed-off-by: Rubin Gerritsen <rubin.gerritsen@nordicsemi.no>
This commit is contained in:
Rubin Gerritsen 2024-06-19 10:44:03 +02:00 committed by Alberto Escolar
commit 69fb606579
3 changed files with 52 additions and 0 deletions

View file

@ -1286,6 +1286,12 @@ int bt_conn_cb_unregister(struct bt_conn_cb *cb);
_CONCAT(bt_conn_cb_, \
_name))
/** Converts a security error to string.
*
* @return The string representation of the security error code.
*/
const char *bt_security_err_to_str(enum bt_security_err err);
/** @brief Enable/disable bonding.
*
* Set/clear the Bonding flag in the Authentication Requirements of

View file

@ -475,6 +475,33 @@ static enum bt_security_err security_err_get(uint8_t smp_err)
}
}
const char *bt_security_err_to_str(enum bt_security_err err)
{
#define SEC_ERR(err) [err] = #err
const char * const mapping_table[] = {
SEC_ERR(BT_SECURITY_ERR_SUCCESS),
SEC_ERR(BT_SECURITY_ERR_AUTH_FAIL),
SEC_ERR(BT_SECURITY_ERR_PIN_OR_KEY_MISSING),
SEC_ERR(BT_SECURITY_ERR_OOB_NOT_AVAILABLE),
SEC_ERR(BT_SECURITY_ERR_AUTH_REQUIREMENT),
SEC_ERR(BT_SECURITY_ERR_PAIR_NOT_SUPPORTED),
SEC_ERR(BT_SECURITY_ERR_PAIR_NOT_ALLOWED),
SEC_ERR(BT_SECURITY_ERR_INVALID_PARAM),
SEC_ERR(BT_SECURITY_ERR_KEY_REJECTED),
SEC_ERR(BT_SECURITY_ERR_UNSPECIFIED),
};
if (err < ARRAY_SIZE(mapping_table) && mapping_table[err]) {
return mapping_table[err];
} else {
return "(unknown)";
}
#undef SEC_ERR
}
static uint8_t smp_err_get(enum bt_security_err auth_err)
{
switch (auth_err) {

View file

@ -36,3 +36,22 @@ ZTEST(test_smp, test_bt_smp_err_to_str)
zassert_not_null(bt_smp_err_to_str(i), ": %d", i);
}
}
ZTEST(test_smp, test_bt_security_err_to_str)
{
/* Test a couple of entries */
zassert_str_equal(bt_security_err_to_str(BT_SECURITY_ERR_AUTH_FAIL),
"BT_SECURITY_ERR_AUTH_FAIL");
zassert_str_equal(bt_security_err_to_str(BT_SECURITY_ERR_KEY_REJECTED),
"BT_SECURITY_ERR_KEY_REJECTED");
zassert_str_equal(bt_security_err_to_str(BT_SECURITY_ERR_UNSPECIFIED),
"BT_SECURITY_ERR_UNSPECIFIED");
/* Test outside range */
zassert_str_equal(bt_security_err_to_str(BT_SECURITY_ERR_UNSPECIFIED + 1),
"(unknown)");
for (uint16_t i = 0; i <= UINT8_MAX; i++) {
zassert_not_null(bt_security_err_to_str(i), ": %d", i);
}
}