Skip to content

Commit

Permalink
modules/bluetooth: Add API to set BLE public address
Browse files Browse the repository at this point in the history
Add ble_set_public_address to set BLE public address.
Fix typo.
  • Loading branch information
SPRESENSE committed Mar 19, 2024
1 parent f892422 commit 49e6c8e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 28 deletions.
54 changes: 47 additions & 7 deletions sdk/modules/bluetooth/bluetooth_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ static struct bt_common_state_s g_bt_common_state =
.bt_name = CONFIG_BLUETOOTH_NAME,
.ble_name = CONFIG_BLUETOOTH_LE_NAME,
.bt_addr = {{0x20, 0x70, 0x3A, 0x10, 0x00, 0x01}},
.ble_addr = {{0x20, 0x70, 0x3A, 0x10, 0x00, 0x01}}
.ble_addr = {{0x20, 0x70, 0x3A, 0x10, 0x00, 0x01}},
.ble_addr_type = BLE_ADDRTYPE_RAND_STATIC
};

static struct bt_acl_state_s g_bt_acl_state =
Expand Down Expand Up @@ -870,8 +871,8 @@ int bt_common_event_handler(struct bt_event_t *bt_event)
* Name: ble_set_address
*
* Description:
* Set Bluetooth LE module address
* This is Spresense side address and should be call before bt_enable.
* Set Bluetooth LE module address of the random static address type.
* This is Spresense side address and should be called before bt_enable.
*
****************************************************************************/

Expand All @@ -881,14 +882,52 @@ int ble_set_address(BT_ADDR *addr)

if (!addr)
{
_err("%s [BLE][Common] Set local BT address failed(addr not set).\n", __func__);
_err("%s [BLE][Common] Set own BLE address failed.\n", __func__);
return BT_FAIL;
}

g_bt_common_state.ble_addr_type = BLE_ADDRTYPE_RAND_STATIC;
memcpy(&g_bt_common_state.ble_addr, addr, sizeof(BT_ADDR));
return ret;
}

/****************************************************************************
* Name: ble_set_public_address
*
* Description:
* Set Bluetooth LE module address of the public address type.
* This is Spresense side address and should be called before bt_enable.
*
****************************************************************************/

int ble_set_public_address(BT_ADDR *addr)
{
int ret = BT_SUCCESS;

if (!addr)
{
_err("%s [BLE][Common] Set own BLE public address failed.\n", __func__);
return BT_FAIL;
}

g_bt_common_state.ble_addr_type = BLE_ADDRTYPE_PUBLIC;
memcpy(&g_bt_common_state.ble_addr, addr, sizeof(BT_ADDR));
return ret;
}

/****************************************************************************
* Name: ble_get_address_type
*
* Description:
* Get Bluetooth LE module address type
*
****************************************************************************/

uint8_t ble_get_address_type(void)
{
return g_bt_common_state.ble_addr_type;
}

/****************************************************************************
* Name: ble_get_address
*
Expand Down Expand Up @@ -916,7 +955,7 @@ int ble_get_address(BT_ADDR *addr)
*
* Description:
* Set Bluetooth LE module name
* This name visible for other devices and should be call before bt_enable.
* This name visible for other devices and should be called before bt_enable.
*
****************************************************************************/

Expand Down Expand Up @@ -990,7 +1029,8 @@ int ble_enable(void)
return ret;
}

ret = ble_hal_common_ops->setDevAddr(&g_bt_common_state.ble_addr);
ret = ble_hal_common_ops->setDevAddr(&g_bt_common_state.ble_addr,
g_bt_common_state.ble_addr_type);

if (ret != BT_SUCCESS)
{
Expand Down Expand Up @@ -1318,7 +1358,7 @@ int ble_register_common_cb(struct ble_common_ops_s *ble_common_ops)
*
* Description:
* Bluetooth LE common function HAL register
* This is Spresense side address and should be call before bt_enable.
* This is Spresense side address and should be called before bt_enable.
*
****************************************************************************/

Expand Down
10 changes: 5 additions & 5 deletions sdk/modules/bluetooth/hal/bcm20706/bcm20706_ble_common.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* modules/bluetooth/hal/bcm20706/manager/bt_uart_manager.c
* modules/bluetooth/hal/bcm20706/bcm20706_ble_common.c
*
* Copyright 2018 Sony Semiconductor Solutions Corporation
* Copyright 2018, 2024 Sony Semiconductor Solutions Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -86,7 +86,7 @@ extern bleGapMem *bleGetGapMem(void);
* Private Function Prototypes
****************************************************************************/

static int bcm20706_ble_set_dev_addr(BT_ADDR *addr);
static int bcm20706_ble_set_dev_addr(BT_ADDR *addr, uint8_t type);
static int bcm20706_ble_set_dev_name(char *name);
static int bcm20706_ble_set_appearance(BLE_APPEARANCE appearance);
static int bcm20706_ble_set_ppcp(BLE_CONN_PARAMS ppcp);
Expand Down Expand Up @@ -197,7 +197,7 @@ static int32_t set_adv_data(void)
*
****************************************************************************/

static int bcm20706_ble_set_dev_addr(BT_ADDR *addr)
static int bcm20706_ble_set_dev_addr(BT_ADDR *addr, uint8_t type)
{
int ret = BT_SUCCESS;

Expand Down Expand Up @@ -226,7 +226,7 @@ static int bcm20706_ble_set_dev_name(char *name)

nameSize = strlen(name);

/* If invalid size, retrun NG */
/* If invalid size, return error */

if (!name || nameSize > BT_MAX_NAME_LEN)
{
Expand Down
12 changes: 6 additions & 6 deletions sdk/modules/bluetooth/hal/nrf52/ble_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static int nrf52_ble_stop_scan(void);
static int nrf52_ble_connect(uint8_t addr_type, const BT_ADDR *addr);
static int nrf52_ble_disconnect(const uint16_t conn_handle);
static int nrf52_ble_advertise(bool enable);
static int nrf52_ble_set_dev_addr(BT_ADDR *addr);
static int nrf52_ble_set_dev_addr(BT_ADDR *addr, uint8_t type);
static int nrf52_ble_set_dev_name(char *name);
static int nrf52_ble_set_appearance(BLE_APPEARANCE appearance);
static int nrf52_ble_set_ppcp(BLE_CONN_PARAMS ppcp);
Expand Down Expand Up @@ -2829,7 +2829,7 @@ void onSrvDiscCompletion(BLE_Evt *pBleEvent, bleGattcDb *gattcDbDiscovery)
srvBeingDiscovered = &(gattcDbDiscovery->dbDiscovery.services[gattcDbDiscovery->currSrvInd]);
srvBeingDiscovered->charCount = 0;

/* Discover characterisitics of next service. */
/* Discover characteristics of next service. */

(void)characteristicsDiscover(pBleEvent, gattcDbDiscovery);
}
Expand Down Expand Up @@ -3030,7 +3030,7 @@ int descriptorsDiscover(BLE_Evt *pBleEvent, bleGattcDb *const gattcDbDiscovery,
if (!isDiscoveryReqd)
{
// No more descriptor discovery required.
// Preceed to the characteristics discovery about next service.
// Proceed to the characteristics discovery about next service.

gattcDbDiscovery->currSrvInd++;
gattcDbDiscovery->currCharInd = 0;
Expand Down Expand Up @@ -3285,12 +3285,12 @@ static int nrf52_ble_advertise(bool enable)
return ret;
}

static int nrf52_ble_set_dev_addr(BT_ADDR *addr)
static int nrf52_ble_set_dev_addr(BT_ADDR *addr, uint8_t type)
{
int ret;
ble_gap_addr_t nrf52_addr;

nrf52_addr.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC;
nrf52_addr.addr_type = type;
memcpy(nrf52_addr.addr, addr->address, sizeof(nrf52_addr.addr));

ret = sd_ble_gap_addr_set(&nrf52_addr);
Expand All @@ -3306,7 +3306,7 @@ static int nrf52_ble_set_dev_name(char *name)

nameSize = strlen(name);

/* If invalid size, retrun NG */
/* If invalid size, return error */

if (!name || nameSize > BT_MAX_NAME_LEN)
{
Expand Down
34 changes: 27 additions & 7 deletions sdk/modules/include/bluetooth/bt_common.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
* modules/include/bluetooth/bt_common.h
*
* Copyright 2018 Sony Semiconductor Solutions Corporation
* Copyright 2018, 2024 Sony Semiconductor Solutions Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -140,6 +140,7 @@ struct bt_common_state_s
struct ble_common_ops_s *ble_common_ops; /**< BLE status callbacks @ref ble_common_ops_s */
BT_ADDR bt_addr; /**< BT local device address @ref BT_ADDR */
BT_ADDR ble_addr; /**< BLE local device address @ref BT_ADDR */
uint8_t ble_addr_type; /**< BLE local device address_type @ref BLE_GAP_ADDR_TYPES */
char bt_name[BT_NAME_LEN + 1]; /**< BT local device name */
char ble_name[BT_NAME_LEN + 1]; /**< BLE local device name */
};
Expand Down Expand Up @@ -224,7 +225,7 @@ struct bt_common_ops_s
void (*command_status)(BT_CMD_STATUS status); /**< Command status */
void (*pairing_complete)(BT_ADDR addr, BT_PAIR_STATUS status); /**< Pairing complete */
void (*inquiry_result)(BT_ADDR addr, char *name); /**< Inquiry data result */
void (*inquiry_complete)(void); /**< Coplete inquiry */
void (*inquiry_complete)(void); /**< Complete inquiry */
void (*connect_status_changed)(struct bt_acl_state_s *bt_acl_state, bool connected, int status); /**< Connection status change */
void (*connected_device_name)(const char *name); /**< Device name change */
void (*bond_info)(BT_ADDR addr); /**< Bonding information */
Expand Down Expand Up @@ -311,7 +312,7 @@ int bt_finalize(void);

/**
* @brief Set Bluetooth module address
* This is Spresense side address and should be call before bt_enable.
* This is Spresense side address and should be called before bt_enable.
*
* @param[in] addr: Bluetooth device address @ref BT_ADDR
*
Expand All @@ -332,7 +333,7 @@ int bt_get_address(BT_ADDR *addr);

/**
* @brief Set Bluetooth module name
* This name visible for other devices and should be call before bt_enable.
* This name visible for other devices and should be called before bt_enable.
*
* @param[in] name: Bluetooth device name
*
Expand Down Expand Up @@ -451,8 +452,8 @@ int bt_cancel_inquiry(void);
int bt_register_common_cb(struct bt_common_ops_s *bt_common_ops);

/**
* @brief Set Bluetooth LE module address
* This is Spresense side address and should be call before bt_enable.
* @brief Set Bluetooth LE module address of the random static address type.
* This is Spresense side address and should be called before bt_enable.
*
* @param[in] addr: Bluetooth LE device address @ref BT_ADDR
*
Expand All @@ -471,9 +472,28 @@ int ble_set_address(BT_ADDR *addr);

int ble_get_address(BT_ADDR *addr);

/**
* @brief Set Bluetooth LE module address of the public address type.
* This is Spresense side address and should be called before bt_enable.
*
* @param[in] addr: Bluetooth LE device address @ref BT_ADDR
*
* @retval error code
*/

int ble_set_public_address(BT_ADDR *addr);

/**
* @brief Get Bluetooth LE module address type
*
* @retval Bluetooth LE device address type
*/

uint8_t ble_get_address_type(void);

/**
* @brief Set Bluetooth LE module name
* This name visible for other devices and should be call before bt_enable.
* This name visible for other devices and should be called before bt_enable.
*
* @param[in] name: Bluetooth LE device name
*
Expand Down
6 changes: 3 additions & 3 deletions sdk/modules/include/bluetooth/hal/bt_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ struct bt_hal_hfp_ops_s
int (*connect)(BT_ADDR *addr, uint16_t handle, bool connect); /**< Connect/Disconnect HFP by BT_ADDR */
int (*audio_connect)(BT_ADDR *addr, uint16_t handle, bool connect); /**< Connect/Disconnect HFP audio by BT_ADDR */
int (*set_hf_feature)(BT_HFP_HF_FEATURE_FLAG hf_heature); /**< Setup HFP HF feature @ref BT_HFP_HF_FEATURE_FLAG */
int (*send_at_command)(BT_ADDR *addr, char *at_str, uint16_t handle); /**< Send AT comand */
int (*press_button)(BT_ADDR *addr, uint16_t handle); /**< Send pressing button comand */
int (*send_at_command)(BT_ADDR *addr, char *at_str, uint16_t handle); /**< Send AT command */
int (*press_button)(BT_ADDR *addr, uint16_t handle); /**< Send pressing button command */
};

/**
Expand All @@ -137,7 +137,7 @@ struct bt_hal_spp_ops_s
*/
struct ble_hal_common_ops_s
{
int (*setDevAddr)(BT_ADDR *addr); /**< Set BLE device address */
int (*setDevAddr)(BT_ADDR *addr, uint8_t type); /**< Set BLE device address and type */
int (*setDevName)(char *name); /**< Set BLE device name */
int (*setAppearance)(BLE_APPEARANCE appearance); /**< Set BLE appearance ID @ref BLE_APPEARANCE */
int (*setPPCP)(BLE_CONN_PARAMS ppcp); /**< Set PPCP connection parameter */
Expand Down

0 comments on commit 49e6c8e

Please sign in to comment.