Skip to content

Commit 1fdb1c1

Browse files
authored
Merge pull request #14198 from ARMmbed/feature-bluetooth-traces
Feature bluetooth traces
2 parents f00ce59 + d43d4a0 commit 1fdb1c1

34 files changed

+4347
-535
lines changed

.github/issue_template.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@
4747

4848
#### How is this defect reproduced ?
4949

50+
<!--
51+
For connectivity issues it is recommended to enable mbed trace and attach the traces
52+
generated by the logger to the issue.
53+
-->
5054

connectivity/FEATURE_BLE/libraries/cordio_stack/wsf/include/wsf_trace.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
#include <stdarg.h>
2828

29+
#if MBED_CONF_CORDIO_TRACE_HCI_PACKETS
30+
#include "mbed_wsf_trace.h"
31+
#endif
32+
2933
#ifdef __cplusplus
3034
extern "C" {
3135
#endif
@@ -289,6 +293,7 @@ bool_t WsfTokenService(void);
289293
/*! \brief 3 argument HCI error trace. */
290294
#define HCI_TRACE_ERR3(msg, var1, var2, var3) WSF_TRACE3("HCI", "ERR", msg, var1, var2, var3)
291295

296+
#if !MBED_CONF_CORDIO_TRACE_HCI_PACKETS
292297
/*! \brief HCI PDUMP on command. */
293298
#define HCI_PDUMP_CMD(len, pBuf)
294299
/*! \brief HCI PDUMP on event. */
@@ -301,6 +306,7 @@ bool_t WsfTokenService(void);
301306
#define HCI_PDUMP_TX_ISO(len, pBuf)
302307
/*! \brief HCI PDUMP on Received ISO message. */
303308
#define HCI_PDUMP_RX_ISO(len, pBuf)
309+
#endif // !MBED_CONF_CORDIO_TRACE_HCI_PACKETS
304310

305311
/*! \brief 0 argument DM info trace. */
306312
#define DM_TRACE_INFO0(msg) WSF_TRACE0("DM", "INFO", msg)

connectivity/FEATURE_BLE/mbed_lib.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,15 @@
108108
"value": 16,
109109
"macro_name": "BLE_GAP_HOST_PRIVACY_RESOLVED_CACHE_SIZE"
110110
},
111+
111112
"ble-passkey-display-reversed-digits-deprecation": {
112113
"help": "This option is part of the deprecation process. Set this to false to remove the deprecation notice. When set to true, the digits in the SecurityManager passkeyDiplay event are reversed. When set to false the digits are in the correct order.",
113114
"value": true,
114115
"macro_name": "BLE_PASSKEY_DISPLAY_REVERSED_DIGITS_DEPRECATION"
116+
},
117+
"trace-human-readable-enums": {
118+
"help": "More hexadecimal values in traces will be printed as human readable strings.",
119+
"value": false
115120
}
116121
},
117122
"target_overrides": {

connectivity/FEATURE_BLE/source/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ add_subdirectory(gatt)
77
add_subdirectory(generic)
88
add_subdirectory(pal)
99

10+
target_include_directories(mbed-ble
11+
INTERFACE
12+
.
13+
)
14+
1015
target_sources(mbed-ble
1116
INTERFACE
1217
BLE.cpp
1318
Gap.cpp
1419
GattClient.cpp
1520
GattServer.cpp
1621
SecurityManager.cpp
22+
common/ble_trace_helpers.cpp
1723
)
1824

1925
target_link_libraries(mbed-ble

connectivity/FEATURE_BLE/source/common/ble_trace_helpers.cpp

Lines changed: 1263 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2021 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef BLE_CLIAPP_BLE_TRACE_HELPERS_H
19+
#define BLE_CLIAPP_BLE_TRACE_HELPERS_H
20+
21+
#include "ble/BLE.h"
22+
#include "ble/SecurityManager.h"
23+
#include "mbed-trace/mbed_trace.h"
24+
#include "pal/GapTypes.h"
25+
#include "pal/GapEvents.h"
26+
#include "dm_api.h"
27+
#include "common/UUID.h"
28+
29+
namespace ble {
30+
#if MBED_CONF_MBED_TRACE_ENABLE
31+
void trace_le_supported_features(uint64_t feat);
32+
33+
const char* dm_callback_event_to_string(uint8_t event);
34+
const char* hci_opcode_to_string(uint16_t opcode);
35+
const char* attribute_error_code_to_string(uint8_t error);
36+
const char* attribute_opcode_to_string(uint8_t error);
37+
const char* ble_error_to_string(ble_error_t error);
38+
const char* att_client_callback_event_to_string(uint8_t code);
39+
const char* att_error_code_to_string(uint8_t code);
40+
#endif //MBED_CONF_MBED_TRACE_ENABLE
41+
42+
template<size_t size>
43+
const char* to_string(const ble::byte_array_t<size> &array)
44+
{
45+
return (mbed_trace_array)(array.data(), array.size());
46+
}
47+
48+
const char* to_string(const UUID& uuid);
49+
50+
const char *to_string(bool v);
51+
52+
const char *to_string(ble::SecurityManager::SecurityIOCapabilities_t capabilities);
53+
54+
const char *to_string(ble::io_capability_t capabilities);
55+
56+
const char *to_string(ble::SecurityManager::SecurityMode_t security_mode);
57+
58+
const char *passkey_str(const uint8_t* passkey);
59+
60+
const char *to_string(const ble::address_t& address);
61+
62+
const char *to_string(const ble::link_encryption_t encryption);
63+
64+
const char *to_string(Keypress_t keypress);
65+
66+
const char *to_string(ble::pairing_failure_t reason);
67+
68+
const char *to_string(target_peer_address_type_t type);
69+
70+
const char *to_string(privacy_mode_t mode);
71+
72+
const char *to_string(ble::advertising_type_t advType);
73+
74+
const char *to_string(ble::own_address_type_t addressType);
75+
76+
const char *to_string(ble::advertising_filter_policy_t policy);
77+
78+
const char *to_string(ble::phy_t phy);
79+
80+
const char *to_string(ble::scanning_filter_policy_t policy);
81+
82+
const char *to_string(ble::duplicates_filter_t filtering);
83+
84+
const char *to_string(ble::peer_address_type_t addressType);
85+
86+
const char *to_string(ble::initiator_filter_policy_t policy);
87+
88+
const char *to_string(ble::coded_symbol_per_bit_t sym);
89+
90+
const char *to_string(ble::local_disconnection_reason_t reason);
91+
92+
const char *to_string(ble::advertising_channel_map_t map);
93+
94+
const char *to_string(ble::connection_peer_address_type_t connection_peer_address_type);
95+
96+
const char *to_string(ble::whitelist_address_type_t whitelist_address_type);
97+
98+
const char *to_string(ble::hci_error_code_t error);
99+
100+
const char *to_string(ble::advertising_fragment_description_t fragment_description);
101+
102+
const char *to_string(ble::peripheral_privacy_configuration_t::resolution_strategy_t resolution_strategy);
103+
104+
const char *to_string(ble::GapEventType event_type);
105+
106+
const char *to_string(ble::received_advertising_type_t type);
107+
108+
const char *to_string(ble::connection_role_t role);
109+
110+
const char *to_string(ble::direct_address_type_t direct_address_type);
111+
112+
const char *to_string(ble::advertising_data_status_t data_status);
113+
114+
const char *to_string(ble::att_security_requirement_t security_requirement);
115+
116+
const char *to_string(GattAuthCallbackReply_t authorization_reply);
117+
118+
} // namespace ble
119+
120+
#endif //BLE_CLIAPP_BLE_TRACE_HELPERS_H

connectivity/FEATURE_BLE/source/cordio/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ target_sources(mbed-ble-cordio
2929
stack_adaptation/wsf_cs.c
3030
stack_adaptation/wsf_mbed_os_adaptation.c
3131
stack_adaptation/wsf_os.c
32+
stack_adaptation/mbed_wsf_trace.c
3233
)

connectivity/FEATURE_BLE/source/cordio/driver/CordioHCIDriver.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
#include "bstream.h"
2828
#include "hci_mbed_os_adaptation.h"
2929

30+
#include "mbed-trace/mbed_trace.h"
31+
#include "common/ble_trace_helpers.h"
32+
33+
#define TRACE_GROUP "BLHC"
34+
3035
#define HCI_RESET_RAND_CNT 4
3136

3237
namespace ble {
@@ -69,12 +74,14 @@ CordioHCIDriver::CordioHCIDriver(CordioHCITransportDriver& transport_driver) :
6974

7075
void CordioHCIDriver::initialize()
7176
{
77+
tr_info("CordioHCIDriver initializing");
7278
_transport_driver.initialize();
7379
do_initialize();
7480
}
7581

7682
void CordioHCIDriver::terminate()
7783
{
84+
tr_info("CordioHCIDriver terminating");
7885
do_terminate();
7986
_transport_driver.terminate();
8087
}
@@ -275,19 +282,34 @@ bool CordioHCIDriver::get_random_static_address(ble::address_t& address)
275282

276283
void CordioHCIDriver::signal_reset_sequence_done()
277284
{
285+
tr_debug("Signal cordio HCI reset sequence done");
278286
hci_mbed_os_signal_reset_sequence_done();
279287
}
280288

281289
uint16_t CordioHCIDriver::write(uint8_t type, uint16_t len, uint8_t *pData)
282290
{
291+
#if MBED_CONF_CORDIO_TRACE_HCI_PACKETS
292+
const char *type_prefix;
293+
switch(type) {
294+
case 0x01: type_prefix = "CMD"; break;
295+
case 0x02: type_prefix = "ACL"; break;
296+
/* illegal packet */
297+
default: type_prefix = "ERR"; break;
298+
}
299+
tr_debug("HOST->LL %s %s", type_prefix, trace_array(pData, len));
300+
#endif
283301
return _transport_driver.write(type, len, pData);
284302
}
285303

286304
void CordioHCIDriver::on_host_stack_inactivity()
287305
{
288306
}
307+
289308
void CordioHCIDriver::handle_test_end(bool success, uint16_t packets)
290309
{
310+
MBED_ASSERT(_test_end_handler);
311+
tr_info("LE receiver mode ended (success: %s)", to_string(success));
312+
291313
if (_test_end_handler) {
292314
_test_end_handler(success, packets);
293315
_test_end_handler = nullptr;
@@ -299,17 +321,21 @@ ble_error_t CordioHCIDriver::rf_test_start_le_receiver_test(
299321
)
300322
{
301323
if (_test_end_handler) {
324+
tr_warning("Cannot start LE %s test mode - already started", "receiver");
302325
return BLE_ERROR_INVALID_STATE;
303326
}
304327

305328
if (!test_end_handler) {
329+
tr_warning("Cannot start LE %s test mode - invalid handler", "receiver");
306330
return BLE_ERROR_INVALID_PARAM;
307331
}
308332

309333
_test_end_handler = test_end_handler;
310334
uint8_t *buf = hciCmdAlloc(HCI_OPCODE_LE_RECEIVER_TEST, HCI_LEN_LE_RECEIVER_TEST);
311335

312336
if (buf) {
337+
tr_info("LE %s mode starter on channel %hhd", "receiver", channel);
338+
313339
uint8_t* p = buf + HCI_CMD_HDR_LEN;
314340
UINT8_TO_BSTREAM(p, channel);
315341
hciCmdSend(buf);
@@ -325,17 +351,21 @@ ble_error_t CordioHCIDriver::rf_test_start_le_transmitter_test(
325351
)
326352
{
327353
if (_test_end_handler) {
354+
tr_warning("Cannot start LE %s test mode - already started", "transmitter");
328355
return BLE_ERROR_INVALID_STATE;
329356
}
330357

331358
if (!test_end_handler) {
359+
tr_warning("Cannot start LE %s test mode - invalid handler", "transmitter");
332360
return BLE_ERROR_INVALID_PARAM;
333361
}
334362

335363
_test_end_handler = test_end_handler;
336364
uint8_t *buf = hciCmdAlloc(HCI_OPCODE_LE_TRANSMITTER_TEST, HCI_LEN_LE_TRANSMITTER_TEST);
337365

338366
if (buf) {
367+
tr_info("LE %s mode starter on channel %hhd", "transmitter", channel);
368+
339369
uint8_t* p = buf + HCI_CMD_HDR_LEN;
340370
UINT8_TO_BSTREAM(p, channel);
341371
UINT8_TO_BSTREAM(p, length);
@@ -350,14 +380,18 @@ ble_error_t CordioHCIDriver::rf_test_start_le_transmitter_test(
350380

351381
ble_error_t CordioHCIDriver::rf_test_end()
352382
{
383+
MBED_ASSERT(_test_end_handler);
384+
353385
if (!_test_end_handler) {
386+
tr_info("Cannot end LE transmitter mode test - missing handler (test never started?)");
354387
return BLE_ERROR_INVALID_STATE;
355388
}
356389

357390
uint8_t *buf = hciCmdAlloc(HCI_OPCODE_LE_TEST_END, HCI_LEN_LE_TEST_END);
358391

359392
if (buf) {
360393
hciCmdSend(buf);
394+
tr_info("LE transmitter mode ended");
361395

362396
return BLE_ERROR_NONE;
363397
}

connectivity/FEATURE_BLE/source/cordio/driver/CordioHCITransportDriver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#include "ble/driver/CordioHCITransportDriver.h"
2222
#include "ble/driver/CordioHCIDriver.h"
2323

24+
#include "mbed-trace/mbed_trace.h"
25+
#include "common/ble_trace_helpers.h"
26+
27+
#define TRACE_GROUP "BLHC"
28+
2429
extern "C" void hciTrSerialRxIncoming(uint8_t *pBuf, uint8_t len);
2530

2631
namespace ble {
@@ -40,6 +45,7 @@ void CordioHCITransportDriver::on_data_received(uint8_t* data, uint16_t len)
4045

4146
void CordioHCITransportDriver::set_data_received_handler(data_received_handler_t handler)
4247
{
48+
MBED_ASSERT(handler);
4349
data_received_handler = handler;
4450
}
4551

connectivity/FEATURE_BLE/source/cordio/mbed_lib.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@
8181
"help": "Check connection complete event if it needs the addresses swapped due to bug in controller",
8282
"value": null,
8383
"macro_name": "CORDIO_RPA_SWAP_WORKAROUND"
84+
},
85+
"trace-pal-echoes": {
86+
"help": "Include 'echo' traces in PAL, i.e. function signature and argument prints",
87+
"value": 0
88+
},
89+
"trace-hci-packets": {
90+
"help": "If tracing is enabled at level DEBUG traces will include HCI packet traces.",
91+
"value": false
92+
},
93+
"trace-cordio-wsf-traces": {
94+
"help": "If tracing is enabled at level DEBUG traces will include WSF traces from Cordio packet traces.",
95+
"value": false,
96+
"macro_name": "WSF_TRACE_ENABLED"
8497
}
8598
},
8699
"target_overrides": {

0 commit comments

Comments
 (0)