Skip to content

Commit

Permalink
bluetooth: Add CONFIG_BLUETOOTH_DEBUG_MSG config
Browse files Browse the repository at this point in the history
Add CONFIG_BLUETOOTH_DEBUG_MSG config to switch debug log on/off by it.
  • Loading branch information
SPRESENSE committed Jul 12, 2023
1 parent eb522b1 commit 7fb5467
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 56 deletions.
6 changes: 6 additions & 0 deletions sdk/modules/bluetooth/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ config BLUETOOTH_LE_GATT
This option enabling LE GATT feature.
endif

config BLUETOOTH_DEBUG_MSG
bool "Enable debug messages output"
default n
---help---
This option enables the bluetooth debug messages output

source "$APPSDIR/../modules/bluetooth/hal/Kconfig"
endif
endmenu
Expand Down
36 changes: 9 additions & 27 deletions sdk/modules/bluetooth/hal/nrf52/ble_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <ble/ble_gattc.h>
#include "ble_storage_operations.h"
#include "ble_comm_internal.h"
#include "ble_debug.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include <arch/board/board.h>
Expand All @@ -64,17 +65,6 @@ extern void board_nrf52_reset(bool en);
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
// #define BLE_DBGPRT_ENABLE
#ifdef BLE_DBGPRT_ENABLE
#include <stdio.h>
#define BLE_PRT printf
#define BLE_PRT2(...)
#define BLE_ERR printf
#else
#define BLE_PRT(...)
#define BLE_PRT2(...)
#define BLE_ERR(...)
#endif

#define NRF52_SYS_ATTR_DATA_OFFSET_HANDLE (0)
#define NRF52_SYS_ATTR_DATA_OFFSET_LEN (2)
Expand Down Expand Up @@ -173,7 +163,6 @@ struct bt_common_context_s bt_common_context = {0};
#endif

#define WAIT_TIME (100*1000)
#define LOG_OUT printf
#define AUTH_KEY_SIZE 6

static struct ble_hal_common_ops_s ble_hal_common_ops =
Expand Down Expand Up @@ -241,12 +230,10 @@ static void bleInitBondInfoKey(void)
bleKey.info_key[5] = BSO_GenerateRegistryKey(BOND_INFO_6_KEY_NAME, 0);
bleKey.info_key[6] = BSO_GenerateRegistryKey(BOND_INFO_7_KEY_NAME, 0);
bleKey.info_key[7] = BSO_GenerateRegistryKey(BOND_INFO_8_KEY_NAME, 0);
#ifdef BLE_DBGPRT_ENABLE
BLE_PRT("bleInitBondInfoKey: enable_key 0x%lx\n", bleKey.enable_key);
for (int i=0; i<BLE_SAVE_BOND_DEVICE_MAX_NUM; i++) {
BLE_PRT("bleInitBondInfoKey: info_key[%d] 0x%lx\n", i, bleKey.info_key[i]);
}
#endif
}

static void set_EncKey_for_save_event(struct ble_idkey_s *apps,
Expand Down Expand Up @@ -1109,10 +1096,9 @@ static void on_timeout(BLE_EvtTimeout *timeout)
static int input_passkey(uint8_t *key)
{
LOG_OUT("Please enter passkey:\n");
const char* name = "passkey_input";
int len = 0;

LOG_OUT("%s> ", name);
LOG_OUT("passkey_input> ");
fflush(stdout);
len = readline((char*)key, AUTH_KEY_SIZE, stdin, stdout);
if (len < 1)
Expand Down Expand Up @@ -1249,11 +1235,11 @@ void bleEvtDispatch(ble_evt_t *pBleNrfEvt)
static
void onAdvSetTerminate(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
{
#ifdef BLE_DBGPRT_ENABLE
#ifdef CONFIG_BLUETOOTH_DEBUG_MSG
ble_gap_evt_adv_set_terminated_t *set = &pBleNrfEvt->evt.gap_evt.params.adv_set_terminated;
#endif
BLE_PRT("onAdvSetTerminate reason=%d handle=%d num=%d\n",
set->reason, set->adv_handle, set->num_completed_adv_events);
#endif
pBleNrfEvt->evt.gap_evt.params.timeout.src = BLE_GAP_TIMEOUT_ADVERTISING;
onTimeout(pBleEvent, pBleNrfEvt);
}
Expand Down Expand Up @@ -1299,13 +1285,13 @@ static
void onDataLengthUpdateRequest(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
{
int ret = 0;
#ifdef BLE_DBGPRT_ENABLE
#ifdef CONFIG_BLUETOOTH_DEBUG_MSG
ble_gap_data_length_params_t *req = &pBleNrfEvt->evt.gap_evt.params.data_length_update_request.peer_params;
#endif
BLE_PRT("onLenUpReq: max_tx=%d\n", req->max_tx_octets);
BLE_PRT("onLenUpReq: max_rx=%d\n", req->max_rx_octets);
BLE_PRT("onLenUpReq: tx_time=%d\n", req->max_tx_time_us);
BLE_PRT("onLenUpReq: rx_time=%d\n", req->max_rx_time_us);
#endif
ret = sd_ble_gap_data_length_update(pBleNrfEvt->evt.gap_evt.conn_handle, NULL, NULL);
if (ret)
{
Expand Down Expand Up @@ -1347,7 +1333,7 @@ void onExchangeMtuResponse(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
ble_gattc_evt_exchange_mtu_rsp_t *rsp;

rsp = &pBleNrfEvt->evt.gattc_evt.params.exchange_mtu_rsp;
BLE_PRT("onMtuReq: mtu=%d\n", req->client_rx_mtu);
BLE_PRT("onMtuRsp: mtu=%d\n", rsp->server_rx_mtu);
if (commMem.requested_mtu > rsp->server_rx_mtu)
{
commMem.client_rx_mtu = rsp->server_rx_mtu;
Expand Down Expand Up @@ -1453,7 +1439,6 @@ void onConnect(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
commMem.connectData.role = BLE_ROLE_CENTRAL;
}
commMem.gapMem->connParams = *(ble_gap_conn_params_t *)&connected->conn_params;
#ifdef BLE_DBGPRT_ENABLE
BLE_PRT("onConnect: handle=%d\n", commMem.connectData.handle);
BLE_PRT("onConnect: role=%d\n", commMem.connectData.role);
BLE_PRT("onConnect: type=%d\n", commMem.connectData.addr.type);
Expand All @@ -1466,7 +1451,6 @@ void onConnect(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
BLE_PRT("onConnect: maxConnInterval=%d\n", commMem.gapMem->connParams.max_conn_interval);
BLE_PRT("onConnect: slaveLatency=%d\n", commMem.gapMem->connParams.slave_latency);
BLE_PRT("onConnect: connSupTimeout=%d\n", commMem.gapMem->connParams.conn_sup_timeout);
#endif
commMem.gapMem->wrapperBondInfo.connHandle = pBleNrfEvt->evt.gap_evt.conn_handle;
commMem.gapMem->wrapperBondInfo.bondInfo.addrType = connected->peer_addr.addr_type;
memcpy(commMem.gapMem->wrapperBondInfo.bondInfo.addr, connected->peer_addr.addr, BLE_GAP_ADDR_LENGTH);
Expand Down Expand Up @@ -1578,7 +1562,6 @@ void onAuthStatus(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
pBleEvent->evtHeader = BLE_GAP_EVENT_AUTH_STATUS;
ble_gap_evt_auth_status_t *authStatus = &pBleNrfEvt->evt.gap_evt.params.auth_status;
commMem.authStatusData.handle = pBleNrfEvt->evt.gap_evt.conn_handle;
#ifdef BLE_DBGPRT_ENABLE
BLE_PRT("onAuthStatus: handle=%d\n", pBleNrfEvt->evt.gap_evt.conn_handle);
BLE_PRT("onAuthStatus: auth_status=%d\n", authStatus->auth_status);
BLE_PRT("onAuthStatus: error_src=%d\n", authStatus->error_src);
Expand Down Expand Up @@ -1664,7 +1647,7 @@ void onAuthStatus(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
{
BLE_PRT("onAuthStatus: peer enc_info_ltk=%d\n", commMem.gapMem->wrapperBondInfo.peerEncKey.enc_info.ltk_len);
}
#endif

//Auth status code.
if ( authStatus->auth_status < BLE_GAP_SEC_STATUS_PDU_INVALID )
{
Expand Down Expand Up @@ -1869,7 +1852,7 @@ void onSecInfoRequest(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
ble_gap_evt_sec_info_request_t *secinfo = &pBleNrfEvt->evt.gap_evt.params.sec_info_request;
uint8_t *sys_attr_data = NULL;
int index;
#ifdef BLE_DBGPRT_ENABLE

BLE_PRT("onSecInfoRequest: handle=%d\n", pBleNrfEvt->evt.gap_evt.conn_handle);
BLE_PRT("onSecInfoRequest: addr_type=%d\n", secinfo->peer_addr.addr_type);
BLE_PRT("onSecInfoRequest: peer_addr=0x");
Expand Down Expand Up @@ -1953,7 +1936,6 @@ void onSecInfoRequest(BLE_Evt *pBleEvent, ble_evt_t *pBleNrfEvt)
{
BLE_PRT("onSecInfoRequest: peer enc_info_ltk=%d\n", commMem.gapMem->wrapperBondInfo.peerEncKey.enc_info.ltk_len);
}
#endif

index = searchBondInfoIndex(&secinfo->master_id);

Expand Down
63 changes: 63 additions & 0 deletions sdk/modules/bluetooth/hal/nrf52/ble_debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/****************************************************************************
* modules/bluetooth/hal/nrf52/ble_debug.h
*
* Copyright 2023 Sony Semiconductor Solutions Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of Sony Semiconductor Solutions Corporation nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

#ifndef __MODULES_BLUETOOTH_HAL_NRF52_BLE_DEBUG_H
#define __MODULES_BLUETOOTH_HAL_NRF52_BLE_DEBUG_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>
#include <stdio.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#ifdef CONFIG_BLUETOOTH_DEBUG_MSG
#define LOG_OUT printf
#define BLE_PRT printf
#define BLE_PRT2 printf
#define BLE_ERR printf
#else
#define LOG_OUT(...)
#define BLE_PRT(...)
#define BLE_PRT2(...)
#define BLE_ERR(...)
#endif /* CONFIG_BLUETOOTH_DEBUG_MSG */

#endif /* __MODULES_BLUETOOTH_HAL_NRF52_BLE_DEBUG_H */

16 changes: 4 additions & 12 deletions sdk/modules/bluetooth/hal/nrf52/ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <ble/ble_comm.h>
#include <ble/ble_gap.h>
#include "ble_comm_internal.h"
#include "ble_debug.h"
#include "ble_storage_operations.h"

/******************************************************************************
Expand Down Expand Up @@ -124,14 +125,6 @@ extern int bleConvertErrorCode(uint32_t errCode);
#define BLE_GAP_ADDR_LENGTH_5 5
#define BLE_KEY_MASK 0x7F

// #define BLE_DBGPRT_ENABLE
#ifdef BLE_DBGPRT_ENABLE
#include <stdio.h>
#define BLE_PRT printf
#else
#define BLE_PRT(...)
#endif

/******************************************************************************
* Structre define
*****************************************************************************/
Expand Down Expand Up @@ -496,8 +489,7 @@ int BLE_GapExchangePairingFeature(BLE_GapConnHandle connHandle, BLE_GapPairingFe
errCode = sd_ble_gap_sec_params_reply(connHandle, BLE_GAP_SEC_STATUS_SUCCESS, p_sec, &keysExchanged);
ret = bleConvertErrorCode((uint32_t)errCode);
memcpy(&gapMem.keySet, &keysExchanged, sizeof(ble_gap_sec_keyset_t));
#ifdef BLE_DBGPRT_ENABLE
#if 0

// own
BLE_PRT("ExchangePairing: own id_irk=0x");
for (int i=0; i<16; i++) {
Expand Down Expand Up @@ -526,7 +518,7 @@ int BLE_GapExchangePairingFeature(BLE_GapConnHandle connHandle, BLE_GapPairingFe
} else {
BLE_PRT("ExchangePairing: peer enc_info_ltk=%d\n", gapMem.wrapperBondInfo.peerEncKey.enc_info.ltk_len);
}
#endif

// peer
BLE_PRT("ExchangePairing: peer id_irk=0x");
for (int i=0; i<16; i++) {
Expand Down Expand Up @@ -555,7 +547,7 @@ int BLE_GapExchangePairingFeature(BLE_GapConnHandle connHandle, BLE_GapPairingFe
} else {
BLE_PRT("ExchangePairing: peer enc_info_ltk=%d\n", gapMem.wrapperBondInfo.peerEncKey.enc_info.ltk_len);
}
#endif

return ret;
}

Expand Down
23 changes: 14 additions & 9 deletions sdk/modules/bluetooth/hal/nrf52/ble_gattc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,7 @@
#include "ble.h"
#include "ble_err.h"
#include "ble_comm_internal.h"

// #define BLE_DBGPRT_ENABLE
#ifdef BLE_DBGPRT_ENABLE
#include <stdio.h>
#define BLE_PRT printf
#else
#define BLE_PRT(...)
#endif
#include "ble_debug.h"

/******************************************************************************
* externs
Expand Down Expand Up @@ -291,12 +284,24 @@ static int nrf52_ble_notify(struct ble_gatt_char_s *ble_gatt_char, uint16_t hand
int ret = BT_SUCCESS;
BLE_GattsHandleValueNfyIndParams param = {0};
int32_t size = (int32_t) ble_gatt_char->value.length;
int i;

if (BLE_MAX_CHAR_SIZE >= size)
{
BLE_PRT("size=%ld\n",size);
BLE_PRT("ble_gatt_char->handle=%d\n", ble_gatt_char->handle);
BLE_PRT("ble_gatt_char->value.data=%.*s\n", size, ble_gatt_char->value.data);
BLE_PRT("ble_gatt_char->value.data:");
for (i = 0; i < size; i++)
{
if (i % 16 == 0)
{
BLE_PRT("\n");
}

BLE_PRT("%02x ", ble_gatt_char->value.data[i]);
}

BLE_PRT("\n");
param.type = BLE_GATT_NOTIFICATION;
param.attrHandle = ble_gatt_char->handle;
param.attrValData = ble_gatt_char->value.data;
Expand Down
9 changes: 1 addition & 8 deletions sdk/modules/bluetooth/hal/nrf52/ble_gatts.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,9 @@
#include <ble/ble_gatts.h>
#include "ble.h"
#include "ble_comm_internal.h"
#include "ble_debug.h"
#include <bluetooth/ble_gatt.h>

// #define BLE_DBGPRT_ENABLE
#ifdef BLE_DBGPRT_ENABLE
#include <stdio.h>
#define BLE_PRT printf
#else
#define BLE_PRT(...)
#endif

/******************************************************************************
* externs
*****************************************************************************/
Expand Down

0 comments on commit 7fb5467

Please sign in to comment.