Bluetooth: Host: Define bt_smp_err_to_str()

This API converts a SMP error code to a string.
This can be useful if application developers want
to print them in the applications.

BT_SMP_ERR_SUCCESS was added for completeness.

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-14 10:43:50 +02:00 committed by Alberto Escolar
commit b25985ad6a
7 changed files with 124 additions and 0 deletions

View file

@ -500,6 +500,38 @@ static uint8_t smp_err_get(enum bt_security_err auth_err)
}
}
const char *bt_smp_err_to_str(uint8_t smp_err)
{
#define SMP_ERR(err) [err] = #err
const char * const mapping_table[] = {
SMP_ERR(BT_SMP_ERR_SUCCESS),
SMP_ERR(BT_SMP_ERR_PASSKEY_ENTRY_FAILED),
SMP_ERR(BT_SMP_ERR_OOB_NOT_AVAIL),
SMP_ERR(BT_SMP_ERR_AUTH_REQUIREMENTS),
SMP_ERR(BT_SMP_ERR_CONFIRM_FAILED),
SMP_ERR(BT_SMP_ERR_PAIRING_NOTSUPP),
SMP_ERR(BT_SMP_ERR_ENC_KEY_SIZE),
SMP_ERR(BT_SMP_ERR_CMD_NOTSUPP),
SMP_ERR(BT_SMP_ERR_UNSPECIFIED),
SMP_ERR(BT_SMP_ERR_REPEATED_ATTEMPTS),
SMP_ERR(BT_SMP_ERR_INVALID_PARAMS),
SMP_ERR(BT_SMP_ERR_DHKEY_CHECK_FAILED),
SMP_ERR(BT_SMP_ERR_NUMERIC_COMP_FAILED),
SMP_ERR(BT_SMP_ERR_BREDR_PAIRING_IN_PROGRESS),
SMP_ERR(BT_SMP_ERR_CROSS_TRANSP_NOT_ALLOWED),
SMP_ERR(BT_SMP_ERR_KEY_REJECTED),
};
if (smp_err < ARRAY_SIZE(mapping_table) && mapping_table[smp_err]) {
return mapping_table[smp_err];
} else {
return "(unknown)";
}
#undef SMP_ERR
}
static struct net_buf *smp_create_pdu(struct bt_smp *smp, uint8_t op, size_t len)
{
struct bt_smp_hdr *hdr;

View file

@ -13,6 +13,7 @@ struct bt_smp_hdr {
uint8_t code;
} __packed;
#define BT_SMP_ERR_SUCCESS 0x00
#define BT_SMP_ERR_PASSKEY_ENTRY_FAILED 0x01
#define BT_SMP_ERR_OOB_NOT_AVAIL 0x02
#define BT_SMP_ERR_AUTH_REQUIREMENTS 0x03
@ -178,3 +179,17 @@ int bt_smp_sign(struct bt_conn *conn, struct net_buf *buf);
/** Generate IRK from Identity Root (IR) */
int bt_smp_irk_get(uint8_t *ir, uint8_t *irk);
/** Converts a SMP error to string.
*
* The error codes are described in the Bluetooth Core specification,
* Vol 3, Part H, Section 3.5.5.
*
* The Security Manager Protocol documentation found in Vol 4, Part H,
* describes when the different error codes are used.
*
* See also the defined BT_SMP_ERR_* macros.
*
* @return The string representation of the SMP error code.
*/
const char *bt_smp_err_to_str(uint8_t smp_err);

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(bluetooth_gatt)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,10 @@
CONFIG_TEST=y
CONFIG_ZTEST=y
CONFIG_BT=y
CONFIG_BT_SMP=y
CONFIG_BT_CTLR=n
CONFIG_BT_H4=n
CONFIG_LOG=y
CONFIG_BT_PERIPHERAL=y

View file

@ -0,0 +1,38 @@
/* main.c - Application main entry point */
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <stddef.h>
#include <zephyr/ztest.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <../subsys/bluetooth/host/smp.h>
ZTEST_SUITE(test_smp, NULL, NULL, NULL, NULL, NULL);
ZTEST(test_smp, test_bt_smp_err_to_str)
{
/* Test a couple of entries */
zassert_str_equal(bt_smp_err_to_str(0x00),
"BT_SMP_ERR_SUCCESS");
zassert_str_equal(bt_smp_err_to_str(0x0a),
"BT_SMP_ERR_INVALID_PARAMS");
zassert_str_equal(bt_smp_err_to_str(0x0F),
"BT_SMP_ERR_KEY_REJECTED");
/* Test entries that are not used */
zassert_mem_equal(bt_smp_err_to_str(0x10),
"(unknown)", strlen("(unknown)"));
zassert_mem_equal(bt_smp_err_to_str(0xFF),
"(unknown)", strlen("(unknown)"));
for (uint16_t i = 0; i <= UINT8_MAX; i++) {
zassert_not_null(bt_smp_err_to_str(i), ": %d", i);
}
}

View file

@ -0,0 +1,5 @@
/ {
chosen {
/delete-property/ zephyr,bt-hci;
};
};

View file

@ -0,0 +1,16 @@
tests:
bluetooth.smp:
extra_args:
- EXTRA_DTC_OVERLAY_FILE="test.overlay"
platform_allow:
- native_posix
- native_posix/native/64
- native_sim
- native_sim/native/64
- qemu_x86
- qemu_cortex_m3
integration_platforms:
- native_sim
tags:
- bluetooth
- smp