1 /* Copyright (c) 2022 Nordic Semiconductor ASA
2 * SPDX-License-Identifier: Apache-2.0
3 */
4
5 /* Helper for printk parameters to convert from binary to hex.
6 * We declare multiple buffers so the helper can be used multiple times
7 * in a single printk call.
8 */
9
10 #include <stddef.h>
11
12 #include <zephyr/bluetooth/bluetooth.h>
13 #include <zephyr/bluetooth/hci.h>
14 #include <zephyr/bluetooth/uuid.h>
15 #include <zephyr/kernel.h>
16 #include <zephyr/sys/util.h>
17 #include <zephyr/types.h>
18
bt_hex(const void * buf,size_t len)19 const char *bt_hex(const void *buf, size_t len)
20 {
21 static const char hex[] = "0123456789abcdef";
22 static char str[129];
23 const uint8_t *b = buf;
24 size_t i;
25
26 len = MIN(len, (sizeof(str) - 1) / 2);
27
28 for (i = 0; i < len; i++) {
29 str[i * 2] = hex[b[i] >> 4];
30 str[i * 2 + 1] = hex[b[i] & 0xf];
31 }
32
33 str[i * 2] = '\0';
34
35 return str;
36 }
37
bt_addr_str(const bt_addr_t * addr)38 const char *bt_addr_str(const bt_addr_t *addr)
39 {
40 static char str[BT_ADDR_STR_LEN];
41
42 bt_addr_to_str(addr, str, sizeof(str));
43
44 return str;
45 }
46
bt_addr_le_str(const bt_addr_le_t * addr)47 const char *bt_addr_le_str(const bt_addr_le_t *addr)
48 {
49 static char str[BT_ADDR_LE_STR_LEN];
50
51 bt_addr_le_to_str(addr, str, sizeof(str));
52
53 return str;
54 }
55
bt_uuid_str(const struct bt_uuid * uuid)56 const char *bt_uuid_str(const struct bt_uuid *uuid)
57 {
58 static char str[BT_UUID_STR_LEN];
59
60 bt_uuid_to_str(uuid, str, sizeof(str));
61
62 return str;
63 }
64
65 #if defined(CONFIG_BT_HCI_ERR_TO_STR)
bt_hci_err_to_str(uint8_t hci_err)66 const char *bt_hci_err_to_str(uint8_t hci_err)
67 {
68 #define HCI_ERR(err) [err] = #err
69
70 const char * const mapping_table[] = {
71 HCI_ERR(BT_HCI_ERR_SUCCESS),
72 HCI_ERR(BT_HCI_ERR_UNKNOWN_CMD),
73 HCI_ERR(BT_HCI_ERR_UNKNOWN_CONN_ID),
74 HCI_ERR(BT_HCI_ERR_HW_FAILURE),
75 HCI_ERR(BT_HCI_ERR_PAGE_TIMEOUT),
76 HCI_ERR(BT_HCI_ERR_AUTH_FAIL),
77 HCI_ERR(BT_HCI_ERR_PIN_OR_KEY_MISSING),
78 HCI_ERR(BT_HCI_ERR_MEM_CAPACITY_EXCEEDED),
79 HCI_ERR(BT_HCI_ERR_CONN_TIMEOUT),
80 HCI_ERR(BT_HCI_ERR_CONN_LIMIT_EXCEEDED),
81 HCI_ERR(BT_HCI_ERR_SYNC_CONN_LIMIT_EXCEEDED),
82 HCI_ERR(BT_HCI_ERR_CONN_ALREADY_EXISTS),
83 HCI_ERR(BT_HCI_ERR_CMD_DISALLOWED),
84 HCI_ERR(BT_HCI_ERR_INSUFFICIENT_RESOURCES),
85 HCI_ERR(BT_HCI_ERR_INSUFFICIENT_SECURITY),
86 HCI_ERR(BT_HCI_ERR_BD_ADDR_UNACCEPTABLE),
87 HCI_ERR(BT_HCI_ERR_CONN_ACCEPT_TIMEOUT),
88 HCI_ERR(BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL),
89 HCI_ERR(BT_HCI_ERR_INVALID_PARAM),
90 HCI_ERR(BT_HCI_ERR_REMOTE_USER_TERM_CONN),
91 HCI_ERR(BT_HCI_ERR_REMOTE_LOW_RESOURCES),
92 HCI_ERR(BT_HCI_ERR_REMOTE_POWER_OFF),
93 HCI_ERR(BT_HCI_ERR_LOCALHOST_TERM_CONN),
94 HCI_ERR(BT_HCI_ERR_REPEATED_ATTEMPTS),
95 HCI_ERR(BT_HCI_ERR_PAIRING_NOT_ALLOWED),
96 HCI_ERR(BT_HCI_ERR_UNKNOWN_LMP_PDU),
97 HCI_ERR(BT_HCI_ERR_UNSUPP_REMOTE_FEATURE),
98 HCI_ERR(BT_HCI_ERR_SCO_OFFSET_REJECTED),
99 HCI_ERR(BT_HCI_ERR_SCO_INTERVAL_REJECTED),
100 HCI_ERR(BT_HCI_ERR_SCO_AIR_MODE_REJECTED),
101 HCI_ERR(BT_HCI_ERR_INVALID_LL_PARAM),
102 HCI_ERR(BT_HCI_ERR_UNSPECIFIED),
103 HCI_ERR(BT_HCI_ERR_UNSUPP_LL_PARAM_VAL),
104 HCI_ERR(BT_HCI_ERR_ROLE_CHANGE_NOT_ALLOWED),
105 HCI_ERR(BT_HCI_ERR_LL_RESP_TIMEOUT),
106 HCI_ERR(BT_HCI_ERR_LL_PROC_COLLISION),
107 HCI_ERR(BT_HCI_ERR_LMP_PDU_NOT_ALLOWED),
108 HCI_ERR(BT_HCI_ERR_ENC_MODE_NOT_ACCEPTABLE),
109 HCI_ERR(BT_HCI_ERR_LINK_KEY_CANNOT_BE_CHANGED),
110 HCI_ERR(BT_HCI_ERR_REQUESTED_QOS_NOT_SUPPORTED),
111 HCI_ERR(BT_HCI_ERR_INSTANT_PASSED),
112 HCI_ERR(BT_HCI_ERR_PAIRING_NOT_SUPPORTED),
113 HCI_ERR(BT_HCI_ERR_DIFF_TRANS_COLLISION),
114 HCI_ERR(BT_HCI_ERR_QOS_UNACCEPTABLE_PARAM),
115 HCI_ERR(BT_HCI_ERR_QOS_REJECTED),
116 HCI_ERR(BT_HCI_ERR_CHAN_ASSESS_NOT_SUPPORTED),
117 HCI_ERR(BT_HCI_ERR_INSUFF_SECURITY),
118 HCI_ERR(BT_HCI_ERR_PARAM_OUT_OF_MANDATORY_RANGE),
119 HCI_ERR(BT_HCI_ERR_ROLE_SWITCH_PENDING),
120 HCI_ERR(BT_HCI_ERR_RESERVED_SLOT_VIOLATION),
121 HCI_ERR(BT_HCI_ERR_ROLE_SWITCH_FAILED),
122 HCI_ERR(BT_HCI_ERR_EXT_INQ_RESP_TOO_LARGE),
123 HCI_ERR(BT_HCI_ERR_SIMPLE_PAIR_NOT_SUPP_BY_HOST),
124 HCI_ERR(BT_HCI_ERR_HOST_BUSY_PAIRING),
125 HCI_ERR(BT_HCI_ERR_CONN_REJECTED_DUE_TO_NO_CHAN),
126 HCI_ERR(BT_HCI_ERR_CONTROLLER_BUSY),
127 HCI_ERR(BT_HCI_ERR_UNACCEPT_CONN_PARAM),
128 HCI_ERR(BT_HCI_ERR_ADV_TIMEOUT),
129 HCI_ERR(BT_HCI_ERR_TERM_DUE_TO_MIC_FAIL),
130 HCI_ERR(BT_HCI_ERR_CONN_FAIL_TO_ESTAB),
131 HCI_ERR(BT_HCI_ERR_MAC_CONN_FAILED),
132 HCI_ERR(BT_HCI_ERR_CLOCK_ADJUST_REJECTED),
133 HCI_ERR(BT_HCI_ERR_SUBMAP_NOT_DEFINED),
134 HCI_ERR(BT_HCI_ERR_UNKNOWN_ADV_IDENTIFIER),
135 HCI_ERR(BT_HCI_ERR_LIMIT_REACHED),
136 HCI_ERR(BT_HCI_ERR_OP_CANCELLED_BY_HOST),
137 HCI_ERR(BT_HCI_ERR_PACKET_TOO_LONG),
138 HCI_ERR(BT_HCI_ERR_TOO_LATE),
139 HCI_ERR(BT_HCI_ERR_TOO_EARLY),
140 };
141
142 if (hci_err < ARRAY_SIZE(mapping_table) && mapping_table[hci_err]) {
143 return mapping_table[hci_err];
144 } else {
145 return "(unknown)";
146 }
147
148 #undef HCI_ERR
149 }
150 #endif /* CONFIG_BT_HCI_ERR_TO_STR */
151