Skip to content

Commit

Permalink
nimble/host: Wrap extended advertising with legacy API
Browse files Browse the repository at this point in the history
This allows to use extended advertising along legacy GAP API. This
will make it easier for applications to port to using EXT ADV.
  • Loading branch information
sjanc committed Apr 9, 2024
1 parent 795264b commit bc1f354
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 9 deletions.
138 changes: 131 additions & 7 deletions nimble/host/src/ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2514,7 +2514,17 @@ ble_gap_adv_stop_no_lock(void)
int
ble_gap_adv_stop(void)
{
#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV)
#if NIMBLE_BLE_ADVERTISE
#if MYNEWT_VAL(BLE_EXT_ADV)
int rc;

rc = ble_gap_ext_adv_stop(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE));
if (rc) {
return rc;
}

return ble_gap_ext_adv_remove(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE));
#else
int rc;

if (!ble_hs_is_enabled()) {
Expand All @@ -2526,6 +2536,7 @@ ble_gap_adv_stop(void)
ble_hs_unlock();

return rc;
#endif
#else
return BLE_HS_ENOTSUP;
#endif
Expand Down Expand Up @@ -2712,7 +2723,73 @@ ble_gap_adv_start(uint8_t own_addr_type, const ble_addr_t *direct_addr,
const struct ble_gap_adv_params *adv_params,
ble_gap_event_fn *cb, void *cb_arg)
{
#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV)
#if NIMBLE_BLE_ADVERTISE
#if MYNEWT_VAL(BLE_EXT_ADV)
struct ble_gap_ext_adv_params ext_params;
int duration;
int rc;

STATS_INC(ble_gap_stats, adv_start);

if (!ble_hs_is_enabled()) {
return BLE_HS_EDISABLED;
}

memset(&ext_params, 0, sizeof(ext_params));

ext_params.own_addr_type = own_addr_type;
ext_params.primary_phy = BLE_HCI_LE_PHY_1M;
ext_params.secondary_phy = BLE_HCI_LE_PHY_1M;
ext_params.tx_power = 127;
ext_params.sid = 0;
ext_params.legacy_pdu = 1;

switch (adv_params->conn_mode) {
case BLE_GAP_CONN_MODE_NON:
if (adv_params->disc_mode != BLE_GAP_DISC_MODE_NON) {
ext_params.scannable = 1;
}
break;
case BLE_GAP_CONN_MODE_DIR:
if (!direct_addr) {
return BLE_HS_EINVAL;
}

ext_params.peer = *direct_addr;
ext_params.directed = 1;
if (adv_params->high_duty_cycle) {
ext_params.high_duty_directed = 1;
}
break;
case BLE_GAP_CONN_MODE_UND:
ext_params.connectable = 1;
ext_params.scannable = 1;
break;
default:
return BLE_HS_EINVAL;
}

ext_params.itvl_min = adv_params->itvl_min;
ext_params.itvl_max = adv_params->itvl_max;
ext_params.channel_map = adv_params->channel_map;
ext_params.filter_policy = adv_params->filter_policy;
ext_params.high_duty_directed = adv_params->high_duty_cycle;

/* configure instance 0 */
rc = ble_gap_ext_adv_configure(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE),
&ext_params, NULL, cb, cb_arg);
if (rc) {
return rc;
}

if (duration_ms == BLE_HS_FOREVER) {
duration = 0;
} else {
duration = duration_ms / 10;
}

return ble_gap_ext_adv_start(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE), duration, 0);
#else
uint32_t duration_ticks;
int rc;

Expand Down Expand Up @@ -2788,6 +2865,7 @@ ble_gap_adv_start(uint8_t own_addr_type, const ble_addr_t *direct_addr,
STATS_INC(ble_gap_stats, adv_start_fail);
}
return rc;
#endif
#else
return BLE_HS_ENOTSUP;
#endif
Expand All @@ -2796,7 +2874,29 @@ ble_gap_adv_start(uint8_t own_addr_type, const ble_addr_t *direct_addr,
int
ble_gap_adv_set_data(const uint8_t *data, int data_len)
{
#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV)
#if NIMBLE_BLE_ADVERTISE
#if MYNEWT_VAL(BLE_EXT_ADV)
struct os_mbuf *mbuf;
int rc;

if (((data == NULL) && (data_len != 0)) ||
(data_len > BLE_HCI_MAX_ADV_DATA_LEN)) {
return BLE_HS_EINVAL;
}

mbuf = os_msys_get_pkthdr(data_len, 0);
if (!mbuf) {
return BLE_HS_ENOMEM;
}

rc = os_mbuf_append(mbuf, data, data_len);
if (rc) {
os_mbuf_free_chain(mbuf);
return BLE_HS_ENOMEM;
}

return ble_gap_ext_adv_set_data(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE), mbuf);
#else
struct ble_hci_le_set_adv_data_cp cmd;
uint16_t opcode;

Expand All @@ -2818,6 +2918,7 @@ ble_gap_adv_set_data(const uint8_t *data, int data_len)
opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADV_DATA);

return ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0);
#endif
#else
return BLE_HS_ENOTSUP;
#endif
Expand All @@ -2826,7 +2927,29 @@ ble_gap_adv_set_data(const uint8_t *data, int data_len)
int
ble_gap_adv_rsp_set_data(const uint8_t *data, int data_len)
{
#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV)
#if NIMBLE_BLE_ADVERTISE
#if MYNEWT_VAL(BLE_EXT_ADV)
struct os_mbuf *mbuf;
int rc;

if (((data == NULL) && (data_len != 0)) ||
(data_len > BLE_HCI_MAX_ADV_DATA_LEN)) {
return BLE_HS_EINVAL;
}

mbuf = os_msys_get_pkthdr(data_len, 0);
if (!mbuf) {
return BLE_HS_ENOMEM;
}

rc = os_mbuf_append(mbuf, data, data_len);
if (rc) {
os_mbuf_free_chain(mbuf);
return BLE_HS_ENOMEM;
}

return ble_gap_ext_adv_rsp_set_data(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE), mbuf);
#else
struct ble_hci_le_set_scan_rsp_data_cp cmd;
uint16_t opcode;

Expand All @@ -2846,6 +2969,7 @@ ble_gap_adv_rsp_set_data(const uint8_t *data, int data_len)
opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA);

return ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0);
#endif
#else
return BLE_HS_ENOTSUP;
#endif
Expand All @@ -2854,7 +2978,7 @@ ble_gap_adv_rsp_set_data(const uint8_t *data, int data_len)
int
ble_gap_adv_set_fields(const struct ble_hs_adv_fields *adv_fields)
{
#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV)
#if NIMBLE_BLE_ADVERTISE
uint8_t buf[BLE_HS_ADV_MAX_SZ];
uint8_t buf_sz;
int rc;
Expand Down Expand Up @@ -2882,7 +3006,7 @@ ble_gap_adv_set_fields(const struct ble_hs_adv_fields *adv_fields)
int
ble_gap_adv_rsp_set_fields(const struct ble_hs_adv_fields *rsp_fields)
{
#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV)
#if NIMBLE_BLE_ADVERTISE
uint8_t buf[BLE_HS_ADV_MAX_SZ];
uint8_t buf_sz;
int rc;
Expand All @@ -2907,7 +3031,7 @@ int
ble_gap_adv_active(void)
{
#if NIMBLE_BLE_ADVERTISE
return ble_gap_adv_active_instance(0);
return ble_gap_adv_active_instance(MYNEWT_VAL(BLE_HS_EXT_ADV_LEGACY_INSTANCE));
#else
return 0;
#endif
Expand Down
7 changes: 7 additions & 0 deletions nimble/host/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ syscfg.defs:
initialization.
value: 1

BLE_HS_EXT_ADV_LEGACY_INSTANCE:
description: >
Advertising instance that is used when extended advertising
support is enabled but legacy GAP API is used.
range: 0..BLE_MULTI_ADV_INSTANCES
value: 0

# Debug settings.
BLE_HS_DEBUG:
description: 'Enables extra runtime assertions.'
Expand Down
4 changes: 4 additions & 0 deletions porting/examples/linux/include/syscfg/syscfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@
#define MYNEWT_VAL_BLE_HS_DEBUG (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE
#define MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL
#define MYNEWT_VAL_BLE_HS_FLOW_CTRL (0)
#endif
Expand Down
6 changes: 5 additions & 1 deletion porting/examples/linux_blemesh/include/syscfg/syscfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,10 @@
#define MYNEWT_VAL_BLE_HS_DEBUG (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE
#define MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL
#define MYNEWT_VAL_BLE_HS_FLOW_CTRL (0)
#endif
Expand Down Expand Up @@ -1329,7 +1333,7 @@

/* Overridden by @apache-mynewt-nimble/nimble/host/mesh (defined by @apache-mynewt-nimble/nimble/host/mesh) */
#ifndef MYNEWT_VAL_BLE_MESH_PROXY_MSG_LEN
#define MYNEWT_VAL_BLE_MESH_PROXY_MSG_LEN (66)
#define MYNEWT_VAL_BLE_MESH_PROXY_MSG_LEN (33)
#endif

#ifndef MYNEWT_VAL_BLE_MESH_PROXY_USE_DEVICE_NAME
Expand Down
4 changes: 4 additions & 0 deletions porting/examples/nuttx/include/syscfg/syscfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@
#define MYNEWT_VAL_BLE_HS_DEBUG (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE
#define MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL
#define MYNEWT_VAL_BLE_HS_FLOW_CTRL (0)
#endif
Expand Down
4 changes: 4 additions & 0 deletions porting/nimble/include/syscfg/syscfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,10 @@
#define MYNEWT_VAL_BLE_HS_DEBUG (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE
#define MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL
#define MYNEWT_VAL_BLE_HS_FLOW_CTRL (0)
#endif
Expand Down
7 changes: 6 additions & 1 deletion porting/npl/riot/include/syscfg/syscfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@
#define MYNEWT_VAL_CRYPTO (0)
#endif

/* Overridden by @apache-mynewt-core/hw/bsp/nordic_pca10056 (defined by @apache-mynewt-core/hw/mcu/nordic/nrf52xxx) */
#ifndef MYNEWT_VAL_GPIO_AS_PIN_RESET
#define MYNEWT_VAL_GPIO_AS_PIN_RESET (0)
#define MYNEWT_VAL_GPIO_AS_PIN_RESET (1)
#endif

#ifndef MYNEWT_VAL_I2C_0
Expand Down Expand Up @@ -1643,6 +1644,10 @@
#define MYNEWT_VAL_BLE_HS_DEBUG (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE
#define MYNEWT_VAL_BLE_HS_EXT_ADV_LEGACY_INSTANCE (0)
#endif

#ifndef MYNEWT_VAL_BLE_HS_FLOW_CTRL
#define MYNEWT_VAL_BLE_HS_FLOW_CTRL (0)
#endif
Expand Down

0 comments on commit bc1f354

Please sign in to comment.