Skip to content

Commit

Permalink
bluetooth: Add BLE API to set connection parameter
Browse files Browse the repository at this point in the history
Add BLE API to set connection parameter.
  • Loading branch information
SPRESENSE committed Nov 29, 2024
1 parent 2d15714 commit 3f05f65
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
18 changes: 18 additions & 0 deletions sdk/modules/bluetooth/bluetooth_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,24 @@ int ble_set_scan_param(struct ble_scan_param_s *param)
return ret;
}

int ble_set_conn_param(struct ble_conn_param_s *param)
{
int ret = BT_SUCCESS;
struct ble_hal_common_ops_s *ops = g_bt_common_state.ble_hal_common_ops;

if (ops && ops->setConnParam)
{
ret = ops->setConnParam(param);
}
else
{
_err("%s [BLE][Common] Not supported.\n", __func__);
return BT_FAIL;
}

return ret;
}

/****************************************************************************
* Name: ble_register_common_cb
*
Expand Down
15 changes: 15 additions & 0 deletions sdk/modules/bluetooth/hal/nrf52/ble_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ static int nrf52_ble_get_negotiated_mtusize(uint16_t handle);
static int nrf52_ble_pairing(uint16_t handle);
static int nrf52_ble_set_txpower(int8_t tx_power);
static int nrf52_ble_set_scan_param(struct ble_scan_param_s *param);
static int nrf52_ble_set_conn_param(struct ble_conn_param_s *param);

static int nrf52_bt_init(void);
static int nrf52_bt_finalize(void);
Expand Down Expand Up @@ -193,6 +194,7 @@ static struct ble_hal_common_ops_s ble_hal_common_ops =
.pairing = nrf52_ble_pairing,
.setTxPower = nrf52_ble_set_txpower,
.setScanParam = nrf52_ble_set_scan_param,
.setConnParam = nrf52_ble_set_conn_param,
};

static struct bt_hal_common_ops_s bt_hal_common_ops =
Expand Down Expand Up @@ -3535,6 +3537,19 @@ static int nrf52_ble_set_scan_param(struct ble_scan_param_s *param)
return BLE_GapSetScanParam(param);
}

/****************************************************************************
* Name: nrf52_ble_set_conn_param
*
* Description:
* Bluetooth LE set connection parameter
*
****************************************************************************/

static int nrf52_ble_set_conn_param(struct ble_conn_param_s *param)
{
return BLE_GapSetConnectionParams((BLE_GapConnParams *)param);
}

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down
48 changes: 44 additions & 4 deletions sdk/modules/bluetooth/hal/nrf52/ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ extern int bleConvertErrorCode(uint32_t errCode);

#define CONNECTION_TIMEOUT 1000

#define IS_INVALID_CONN_PARAM(min, max, slave, timeout) \
(((min) < BLE_GAP_CP_MIN_CONN_INTVL_MIN) || \
((min) > BLE_GAP_CP_MIN_CONN_INTVL_MAX) || \
((max) < BLE_GAP_CP_MAX_CONN_INTVL_MIN) || \
((max) > BLE_GAP_CP_MAX_CONN_INTVL_MAX) || \
((slave) > BLE_GAP_CP_SLAVE_LATENCY_MAX) || \
((timeout) < BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN)|| \
((timeout) > BLE_GAP_CP_CONN_SUP_TIMEOUT_MAX))

/* Flash handle key name size */
#define FLASH_KEY_NAME_SIZE 4
#define FLASH_KEY_NAME_2 2
Expand Down Expand Up @@ -706,6 +715,29 @@ int BLE_GapStopScan(void)
return ret;
}

int BLE_GapSetConnectionParams(BLE_GapConnParams *connParams)
{
int ret = BLE_SUCCESS;

if (connParams == NULL) {
return -EINVAL;
}

if (IS_INVALID_CONN_PARAM(connParams->minConnInterval,
connParams->maxConnInterval,
connParams->slaveLatency,
connParams->connSupTimeout)) {
return -EINVAL;
}

gapMem.connParams.min_conn_interval = connParams->minConnInterval;
gapMem.connParams.max_conn_interval = connParams->maxConnInterval;
gapMem.connParams.slave_latency = connParams->slaveLatency;
gapMem.connParams.conn_sup_timeout = connParams->connSupTimeout;

return ret;
}

int BLE_GapConnect(BLE_GapAddr *addr)
{
int ret = BLE_SUCCESS;
Expand All @@ -729,10 +761,18 @@ int BLE_GapConnect(BLE_GapAddr *addr)
memcpy(&scanParams, &gapMem.scanParams, sizeof(scanParams));
scanParams.timeout = CONNECTION_TIMEOUT;

gapMem.connParams.min_conn_interval = MIN_CONNECTION_INTERVAL;
gapMem.connParams.max_conn_interval = MAX_CONNECTION_INTERVAL;
gapMem.connParams.slave_latency = SLAVE_LATENCY;
gapMem.connParams.conn_sup_timeout = SUPERVISION_TIMEOUT;
if (IS_INVALID_CONN_PARAM(gapMem.connParams.min_conn_interval,
gapMem.connParams.max_conn_interval,
gapMem.connParams.slave_latency,
gapMem.connParams.conn_sup_timeout)) {
/* If connection parameter is not set, use the default value. */

gapMem.connParams.min_conn_interval = MIN_CONNECTION_INTERVAL;
gapMem.connParams.max_conn_interval = MAX_CONNECTION_INTERVAL;
gapMem.connParams.slave_latency = SLAVE_LATENCY;
gapMem.connParams.conn_sup_timeout = SUPERVISION_TIMEOUT;
}

peerAddr.addr_type = addr->type;
memcpy(peerAddr.addr, addr->addr, BLE_GAP_ADDR_LENGTH);
errCode = sd_ble_gap_connect(&peerAddr,
Expand Down
15 changes: 15 additions & 0 deletions sdk/modules/bluetooth/hal/nrf52/include/ble/ble_gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,21 @@ int BLE_GapStartScanExt(BLE_GapScanParams *scanparams);
*/
int BLE_GapStopScan(void);

/**@brief Set connection paramter
* @details This call allows to set connection parameter before connecting.
* @param[in] connParam: Pointer to connection parameters
* @return 0: success
*
* @par Blocking
* Yes
* @par Context
* Task
* @par Reentrant
* No
*
*/
int BLE_GapSetConnectionParams(BLE_GapConnParams *connParams);

/**@brief Create a connection
* @details This call allows the application to create a connection. The following events may be triggered: @ref BLE_GAP_EVENT_CONNECTED.
* @param[in] addr: Pointer to peer address
Expand Down
23 changes: 23 additions & 0 deletions sdk/modules/include/bluetooth/bt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
#define BLE_SCAN_PARAM_WINDOW_MSEC(t) ((t) * 1000 / 625)
#define BLE_SCAN_PARAM_TIMEOUT_MSEC(t) ((t) * 1000 / 10000)

/* Macro to convert time in msec for connection parameter */

#define BLE_CONN_PARAM_INTERVAL_MSEC(t) ((t) * 1000 / 1250)
#define BLE_CONN_PARAM_TIMEOUT_MSEC(t) ((t) * 1000 / 10000)

/** BLE status code */

/** Success */
Expand Down Expand Up @@ -230,6 +235,14 @@ struct ble_scan_param_s
uint16_t timeout; /**< Scan timeout in 10 ms units. 0: no timeout */
};

struct ble_conn_param_s
{
uint16_t min_interval; /**< Minimum Connection Interval in 1.25 ms units. (7.5 - 4,000 ms) */
uint16_t max_interval; /**< Maximum Connection Interval in 1.25 ms units. (7.5 - 4,000 ms) */
uint16_t slave_latency; /**< Slave Latency in number of connection events. (max 499) */
uint16_t sup_timeout; /**< Connection Supervision Timeout in 10 ms unit. (100 - 32,000 ms) */
};

/**
* @struct bt_common_ops_s
* @brief Bluetooth Common application callbacks
Expand Down Expand Up @@ -673,6 +686,16 @@ int ble_set_tx_power(int8_t tx_power);

int ble_set_scan_param(struct ble_scan_param_s *param);

/**
* @brief Set connection parameter
*
* @param[in] param: connection parameter
*
* @retval error code
*/

int ble_set_conn_param(struct ble_conn_param_s *param);

/**
* @brief Execute pairing
*
Expand Down
1 change: 1 addition & 0 deletions sdk/modules/include/bluetooth/hal/bt_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ struct ble_hal_common_ops_s
int (*getNegotiatedMtuSize)(uint16_t handle); /**< Get negotiated MTU size */
int (*setTxPower)(int8_t tx_power); /**< Set Tx Power */
int (*setScanParam)(struct ble_scan_param_s *param); /**< Set scan parameter */
int (*setConnParam)(struct ble_conn_param_s *param); /**< Set connection parameter */
};

/**
Expand Down

0 comments on commit 3f05f65

Please sign in to comment.