Skip to content

Commit

Permalink
added periodic adv feature updates in ble 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
RoshanESP committed Jun 28, 2023
1 parent eead9c1 commit ae529ed
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 6 deletions.
61 changes: 58 additions & 3 deletions nimble/host/include/host/ble_gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,30 @@ struct ble_gap_periodic_adv_params {
uint16_t itvl_max;
};

/** @brief Periodic advertising enable parameters */
struct ble_gap_periodic_adv_enable_params {
#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
/** If include adi in aux_sync_ind PDU */
unsigned int include_adi:1;
#endif
};

/** @brief Periodic advertising sync reporting parameters */
struct ble_gap_periodic_adv_sync_report_params {
#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
/** If filter duplicates */
unsigned int filter_duplicates:1;
#endif
};

/** @brief Periodic adv set data parameters */
struct ble_gap_periodic_adv_set_data_params {
#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
/** If include adi in aux_sync_ind PDU */
unsigned int update_did:1;
#endif
};

/** @brief Periodic sync parameters */
struct ble_gap_periodic_sync_params {
/** The maximum number of periodic advertising events that controller can
Expand All @@ -1537,6 +1561,12 @@ struct ble_gap_periodic_sync_params {

/** If reports should be initially disabled when sync is created */
unsigned int reports_disabled:1;

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
/** If duplicate filtering should be should be initially enabled when sync is
created */
unsigned int filter_duplicates:1;
#endif
};

/**
Expand All @@ -1558,10 +1588,15 @@ int ble_gap_periodic_adv_configure(uint8_t instance,
* Start periodic advertising for specified advertising instance.
*
* @param instance Instance ID
* @param params Additional arguments specifying the particulars
* of periodic advertising.
*
* @return 0 on success, error code on failure.
*/
int ble_gap_periodic_adv_start(uint8_t instance);
int
ble_gap_periodic_adv_start(uint8_t instance,
const struct ble_gap_periodic_adv_enable_params *params);


/**
* Stop periodic advertising for specified advertising instance.
Expand All @@ -1578,10 +1613,14 @@ int ble_gap_periodic_adv_stop(uint8_t instance);
*
* @param instance Instance ID
* @param data Chain containing the periodic advertising data.
* @param params Additional arguments specifying the particulars
of periodic advertising data.
*
* @return 0 on success or error code on failure.
*/
int ble_gap_periodic_adv_set_data(uint8_t instance, struct os_mbuf *data);
int ble_gap_periodic_adv_set_data(uint8_t instance,
struct os_mbuf *data,
struct ble_gap_periodic_adv_set_data_params *params);

/**
* Performs the Synchronization procedure with periodic advertiser.
Expand Down Expand Up @@ -1625,10 +1664,14 @@ int ble_gap_periodic_adv_sync_terminate(uint16_t sync_handle);
*
* @param sync_handle Handle identifying synchronization.
* @param enable If reports should be enabled.
* @param params Additional arguments specifying the particulars
* of periodic reports.
*
* @return 0 on success; nonzero on failure.
*/
int ble_gap_periodic_adv_sync_reporting(uint16_t sync_handle, bool enable);
int ble_gap_periodic_adv_sync_reporting(uint16_t sync_handle,
bool enable,
const struct ble_gap_periodic_adv_sync_report_params *params);

/**
* Initialize sync transfer procedure for specified handles.
Expand Down Expand Up @@ -1660,6 +1703,18 @@ int ble_gap_periodic_adv_sync_set_info(uint8_t instance,
uint16_t conn_handle,
uint16_t service_data);

/**
* Set the default periodic sync transfer params.
*
*
* @param conn_handle Handle identifying connection.
* @param params Default Parameters for periodic sync transfer.
*
* @return 0 on success; nonzero on failure.
*/
int periodic_adv_set_default_sync_params(uint16_t conn_handle,
const struct ble_gap_periodic_sync_params *params);

/**
* Enables or disables sync transfer reception on specified connection.
* When sync transfer arrives, BLE_GAP_EVENT_PERIODIC_TRANSFER is sent to the user.
Expand Down
110 changes: 107 additions & 3 deletions nimble/host/src/ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#define bssnz_t
#endif

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
#define SET_BIT(t, n) (t |= 1UL << (n))
#endif

/**
* GAP - Generic Access Profile.
*
Expand Down Expand Up @@ -3664,7 +3668,7 @@ ble_gap_periodic_adv_configure(uint8_t instance,
}

int
ble_gap_periodic_adv_start(uint8_t instance)
ble_gap_periodic_adv_start(uint8_t instance, const struct ble_gap_periodic_adv_enable_params *params)
{
struct ble_hci_le_set_periodic_adv_enable_cp cmd;
uint16_t opcode;
Expand All @@ -3689,6 +3693,12 @@ ble_gap_periodic_adv_start(uint8_t instance)
opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_PERIODIC_ADV_ENABLE);

cmd.enable = 0x01;

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
if (params && params->include_adi) {
SET_BIT(cmd.enable, 1);
}
#endif
cmd.adv_handle = instance;

rc = ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0);
Expand All @@ -3703,6 +3713,25 @@ ble_gap_periodic_adv_start(uint8_t instance)
return 0;
}

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
static int
ble_gap_periodic_adv_update_did(uint8_t instance)
{
static uint8_t buf[sizeof(struct ble_hci_le_set_periodic_adv_data_cp)];
struct ble_hci_le_set_periodic_adv_data_cp *cmd = (void *) buf;
uint16_t opcode;
memset(buf, 0, sizeof(buf));

opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_PERIODIC_ADV_DATA);

cmd->adv_handle = instance;
cmd->operation = BLE_HCI_LE_SET_DATA_OPER_UNCHANGED;
cmd->adv_data_len = 0;
return ble_hs_hci_cmd_tx(opcode, cmd, sizeof(*cmd) + cmd->adv_data_len,
NULL, 0);
}
#endif

static int
ble_gap_periodic_adv_set(uint8_t instance, struct os_mbuf **data)
{
Expand Down Expand Up @@ -3817,7 +3846,9 @@ ble_gap_periodic_adv_set_data_validate(uint8_t instance,
}

int
ble_gap_periodic_adv_set_data(uint8_t instance, struct os_mbuf *data)
ble_gap_periodic_adv_set_data(uint8_t instance,
struct os_mbuf *data,
struct ble_gap_periodic_adv_set_data_params *params)
{
int rc;
if (instance >= BLE_ADV_INSTANCES) {
Expand All @@ -3830,6 +3861,14 @@ ble_gap_periodic_adv_set_data(uint8_t instance, struct os_mbuf *data)
goto done;
}

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
/* update_did and data cannot be set at the same time */
if (params && params -> update_did && data) {
rc = BLE_HS_EINVAL;
goto done;
}
#endif

ble_hs_lock();

rc = ble_gap_periodic_adv_set_data_validate(instance, data);
Expand All @@ -3838,7 +3877,16 @@ ble_gap_periodic_adv_set_data(uint8_t instance, struct os_mbuf *data)
goto done;
}

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
if (params && params -> update_did) {
rc = ble_gap_periodic_adv_update_did(instance);
}
else {
rc = ble_gap_periodic_adv_set(instance, &data);
}
#else
rc = ble_gap_periodic_adv_set(instance, &data);
#endif

ble_hs_unlock();

Expand Down Expand Up @@ -3975,6 +4023,15 @@ ble_gap_periodic_adv_sync_create(const ble_addr_t *addr, uint8_t adv_sid,
memcpy(cmd.peer_addr, BLE_ADDR_ANY->val, BLE_DEV_ADDR_LEN);
}

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
/* LE Periodic Advertising Create Sync command */
if (params->reports_disabled) {
SET_BIT(cmd.options, 1);
}
if (params->filter_duplicates) {
SET_BIT(cmd.options, 2);
}
#endif
cmd.sid = adv_sid;
cmd.skip = params->skip;
cmd.sync_timeout = htole16(params->sync_timeout);
Expand Down Expand Up @@ -4072,8 +4129,11 @@ ble_gap_periodic_adv_sync_terminate(uint16_t sync_handle)
return rc;
}
#if MYNEWT_VAL(BLE_PERIODIC_ADV_SYNC_TRANSFER)
/* LE Set Periodic Advertising Receive Enable command */
int
ble_gap_periodic_adv_sync_reporting(uint16_t sync_handle, bool enable)
ble_gap_periodic_adv_sync_reporting(uint16_t sync_handle,
bool enable,
const struct ble_gap_periodic_adv_sync_report_params *params)
{
struct ble_hci_le_periodic_adv_receive_enable_cp cmd;
struct ble_hs_periodic_sync *psync;
Expand Down Expand Up @@ -4101,6 +4161,11 @@ ble_gap_periodic_adv_sync_reporting(uint16_t sync_handle, bool enable)

cmd.sync_handle = htole16(sync_handle);
cmd.enable = enable ? 0x01 : 0x00;
#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
if (params && params->filter_duplicates) {
SET_BIT(cmd.enable, 1);
}
#endif

rc = ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0);

Expand Down Expand Up @@ -4201,6 +4266,7 @@ ble_gap_periodic_adv_sync_set_info(uint8_t instance, uint16_t conn_handle,
return rc;
}

/* LE Set Periodic Advertising Sync Transfer Parameters command */
static int
periodic_adv_transfer_enable(uint16_t conn_handle,
const struct ble_gap_periodic_sync_params *params)
Expand All @@ -4215,6 +4281,44 @@ periodic_adv_transfer_enable(uint16_t conn_handle,
cmd.conn_handle = htole16(conn_handle);
cmd.sync_cte_type = 0x00;
cmd.mode = params->reports_disabled ? 0x01 : 0x02;

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
if (params->filter_duplicates)
cmd.mode = 0x03;
#endif

cmd.skip = htole16(params->skip);
cmd.sync_timeout = htole16(params->sync_timeout);

rc = ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), &rsp, sizeof(rsp));
if (!rc) {
BLE_HS_DBG_ASSERT(le16toh(rsp.conn_handle) == conn_handle);
}

return rc;
}

/* BLE_HCI_OCF_LE_SET_DEFAULT_SYNC_TRANSFER_PARAMS command api */
int
periodic_adv_set_default_sync_params(uint16_t conn_handle,
const struct ble_gap_periodic_sync_params *params)
{
struct ble_hci_le_periodic_adv_sync_transfer_params_cp cmd;
struct ble_hci_le_periodic_adv_sync_transfer_params_rp rsp;
uint16_t opcode;
int rc;

opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_DEFAULT_SYNC_TRANSFER_PARAMS);

cmd.conn_handle = htole16(conn_handle);
cmd.sync_cte_type = 0x00;
cmd.mode = params->reports_disabled ? 0x01 : 0x02;

#if MYNEWT_VAL(BLE_PERIODIC_ADV_ENH)
if (params->filter_duplicates)
cmd.mode = 0x03;
#endif

cmd.skip = htole16(params->skip);
cmd.sync_timeout = htole16(params->sync_timeout);

Expand Down

0 comments on commit ae529ed

Please sign in to comment.