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 5, 2024
1 parent 1d809b1 commit f41bcb5
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 78 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
27 changes: 16 additions & 11 deletions porting/examples/linux/include/syscfg/syscfg.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This file was generated by Apache newt version: 1.12.0-dev
* This file was generated by Apache newt version: 1.12.0
*/

#ifndef H_MYNEWT_SYSCFG_
Expand All @@ -15,6 +15,7 @@
#define MYNEWT_VAL(_name) MYNEWT_VAL_ ## _name
#define MYNEWT_VAL_CHOICE(_name, _val) MYNEWT_VAL_ ## _name ## __ ## _val

/*** @apache-mynewt-core/crypto/tinycrypt */
#ifndef MYNEWT_VAL_TINYCRYPT_SYSINIT_STAGE
#define MYNEWT_VAL_TINYCRYPT_SYSINIT_STAGE (200)
#endif
Expand Down Expand Up @@ -599,8 +600,8 @@
#define MYNEWT_VAL_BLE_ATT_SVR_WRITE_NO_RSP (1)
#endif

#ifndef MYNEWT_VAL_BLE_AUDIO_MAX_CODEC_RECORDS
#define MYNEWT_VAL_BLE_AUDIO_MAX_CODEC_RECORDS (0)
#ifndef MYNEWT_VAL_BLE_AUDIO
#define MYNEWT_VAL_BLE_AUDIO (0)
#endif

#ifndef MYNEWT_VAL_BLE_EATT_CHAN_NUM
Expand Down Expand Up @@ -727,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 Expand Up @@ -775,6 +780,14 @@
#define MYNEWT_VAL_BLE_HS_SYSINIT_STAGE (200)
#endif

#ifndef MYNEWT_VAL_BLE_ISO_MAX_BIGS
#define MYNEWT_VAL_BLE_ISO_MAX_BIGS (MYNEWT_VAL_BLE_MULTI_ADV_INSTANCES)
#endif

#ifndef MYNEWT_VAL_BLE_ISO_MAX_BISES
#define MYNEWT_VAL_BLE_ISO_MAX_BISES (4)
#endif

#ifndef MYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM
#define MYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM (0)
#endif
Expand Down Expand Up @@ -807,14 +820,6 @@
#define MYNEWT_VAL_BLE_L2CAP_SIG_MAX_PROCS (1)
#endif

#ifndef MYNEWT_VAL_BLE_ISO_MAX_BIGS
#define MYNEWT_VAL_BLE_ISO_MAX_BIGS (MYNEWT_VAL_BLE_MULTI_ADV_INSTANCES)
#endif

#ifndef MYNEWT_VAL_BLE_ISO_MAX_BISES
#define MYNEWT_VAL_BLE_ISO_MAX_BISES (4)
#endif

#ifndef MYNEWT_VAL_BLE_MESH
#define MYNEWT_VAL_BLE_MESH (0)
#endif
Expand Down
9 changes: 6 additions & 3 deletions porting/examples/linux/include/sysflash/sysflash.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
/**
* This file was generated by Apache newt version: 1.12.0-dev
* This file was generated by Apache newt version: 1.12.0
*/

#ifndef H_MYNEWT_SYSFLASH_
#define H_MYNEWT_SYSFLASH_

#include "flash_map/flash_map.h"

#define FLASH_AREA_COUNT 6

/* MYNEWT_SYSFLASH_ONLY_CONST is defined when this header is used for linker script generation */
#ifndef MYNEWT_SYSFLASH_ONLY_CONST
#include "flash_map/flash_map.h"

/**
* This flash map definition is used for two purposes:
* 1. To locate the meta area, which contains the true flash map definition.
* 2. As a fallback in case the meta area cannot be read from flash.
*/
extern const struct flash_area sysflash_map_dflt[FLASH_AREA_COUNT];
#endif

/* Flash map was defined in @apache-mynewt-core/hw/bsp/native */

Expand Down
27 changes: 16 additions & 11 deletions porting/examples/linux_blemesh/include/syscfg/syscfg.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This file was generated by Apache newt version: 1.12.0-dev
* This file was generated by Apache newt version: 1.12.0
*/

#ifndef H_MYNEWT_SYSCFG_
Expand All @@ -15,6 +15,7 @@
#define MYNEWT_VAL(_name) MYNEWT_VAL_ ## _name
#define MYNEWT_VAL_CHOICE(_name, _val) MYNEWT_VAL_ ## _name ## __ ## _val

/*** @apache-mynewt-core/crypto/tinycrypt */
#ifndef MYNEWT_VAL_TINYCRYPT_SYSINIT_STAGE
#define MYNEWT_VAL_TINYCRYPT_SYSINIT_STAGE (200)
#endif
Expand Down Expand Up @@ -600,8 +601,8 @@
#define MYNEWT_VAL_BLE_ATT_SVR_WRITE_NO_RSP (1)
#endif

#ifndef MYNEWT_VAL_BLE_AUDIO_MAX_CODEC_RECORDS
#define MYNEWT_VAL_BLE_AUDIO_MAX_CODEC_RECORDS (0)
#ifndef MYNEWT_VAL_BLE_AUDIO
#define MYNEWT_VAL_BLE_AUDIO (0)
#endif

#ifndef MYNEWT_VAL_BLE_EATT_CHAN_NUM
Expand Down Expand Up @@ -728,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 @@ -776,6 +781,14 @@
#define MYNEWT_VAL_BLE_HS_SYSINIT_STAGE (200)
#endif

#ifndef MYNEWT_VAL_BLE_ISO_MAX_BIGS
#define MYNEWT_VAL_BLE_ISO_MAX_BIGS (MYNEWT_VAL_BLE_MULTI_ADV_INSTANCES)
#endif

#ifndef MYNEWT_VAL_BLE_ISO_MAX_BISES
#define MYNEWT_VAL_BLE_ISO_MAX_BISES (4)
#endif

#ifndef MYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM
#define MYNEWT_VAL_BLE_L2CAP_COC_MAX_NUM (0)
#endif
Expand Down Expand Up @@ -808,14 +821,6 @@
#define MYNEWT_VAL_BLE_L2CAP_SIG_MAX_PROCS (1)
#endif

#ifndef MYNEWT_VAL_BLE_ISO_MAX_BIGS
#define MYNEWT_VAL_BLE_ISO_MAX_BIGS (MYNEWT_VAL_BLE_MULTI_ADV_INSTANCES)
#endif

#ifndef MYNEWT_VAL_BLE_ISO_MAX_BISES
#define MYNEWT_VAL_BLE_ISO_MAX_BISES (4)
#endif

/* Overridden by @apache-mynewt-nimble/porting/targets/linux_blemesh (defined by @apache-mynewt-nimble/nimble/host) */
#ifndef MYNEWT_VAL_BLE_MESH
#define MYNEWT_VAL_BLE_MESH (1)
Expand Down
Loading

0 comments on commit f41bcb5

Please sign in to comment.