rymanluk commented on a change in pull request #283: Adding Periodic 
Advertising Feature
URL: https://github.com/apache/mynewt-nimble/pull/283#discussion_r257622012
 
 

 ##########
 File path: nimble/host/src/ble_gap.c
 ##########
 @@ -2919,13 +3049,533 @@ ble_gap_ext_adv_remove(uint8_t instance)
     return 0;
 }
 
+#if MYNEWT_VAL(BLE_PERIODIC_ADV)
+static int
+ble_gap_periodic_adv_params_tx(uint8_t instance,
+        const struct ble_gap_periodic_adv_params *params)
+
+{
+    struct hci_periodic_adv_params hci_adv_params;
+    uint8_t buf[BLE_HCI_LE_SET_PERIODIC_ADV_PARAMS_LEN];
+    int rc;
+
+    memset(&hci_adv_params, 0, sizeof(hci_adv_params));
+
+    if (params->include_tx_power) {
+        hci_adv_params.properties |= 
BLE_HCI_LE_SET_PERIODIC_ADV_PROP_INC_TX_PWR;
+    }
+
+    /* Fill optional fields if application did not specify them. */
+    if (params->itvl_min == 0 && params->itvl_max == 0) {
+        hci_adv_params.min_interval = 30 / 1.25;   //30 ms
+        hci_adv_params.max_interval = 60 / 1.25;   //150 ms
+
+    } else {
+        hci_adv_params.min_interval = params->itvl_min;
+        hci_adv_params.max_interval = params->itvl_max;
+    }
+
+    rc = ble_hs_hci_cmd_build_le_periodic_adv_params(instance, 
&hci_adv_params, buf,
+            sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(
+            BLE_HCI_OP(BLE_HCI_OGF_LE, 
BLE_HCI_OCF_LE_SET_PERIODIC_ADV_PARAMS), buf,
+            sizeof(buf));
+
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+static int
+ble_gap_periodic_adv_params_validate(
+        const struct ble_gap_periodic_adv_params *params)
+{
+    if (!params) {
+        return BLE_HS_EINVAL;
+    }
+
+    if (params->itvl_min > 0xffff || params->itvl_min < 6){
+        return BLE_HS_EINVAL;
+    }
+    if (params->itvl_max > 0xffff || params->itvl_max < 6){
+            return BLE_HS_EINVAL;
+    }
+    return 0;
+
+}
+
+int
+ble_gap_periodic_adv_configure(uint8_t instance,
+        const struct ble_gap_periodic_adv_params *params)
+{
+    int rc;
+
+    if (instance >= BLE_ADV_INSTANCES) {
+        return BLE_HS_EINVAL;
+    }
+
+    rc = ble_gap_periodic_adv_params_validate(params);
+    if (rc) {
+        return rc;
+    }
+
+    ble_hs_lock();
+
+    /* The corresponding extended advertising instance should be configured */
+    if (!ble_gap_slave[instance].configured){
+        ble_hs_unlock();
+        return ENOMEM;
+    }
+
+    /* Periodic advertising shall not be configured while it is already
+     * running.
+     * Bluetooth Core Specification, Section 7.8.61
+     */
+    if (ble_gap_slave[instance].per_op == BLE_GAP_OP_S_PERIODIC_ADV){
+        ble_hs_unlock();
+        return BLE_HS_EINVAL;
+    }
+
+    rc = ble_gap_periodic_adv_params_tx(instance, params);
+    if (rc) {
+        ble_hs_unlock();
+        return rc;
+    }
+
+    ble_gap_slave[instance].per_configured = 1;
+
+    ble_hs_unlock();
+
+    return 0;
+}
+
+int
+ble_gap_periodic_adv_start(uint8_t instance)
+{
+    uint8_t buf[BLE_HCI_LE_SET_PERIODIC_ADV_ENABLE_LEN];
+    uint16_t opcode;
+    int rc;
+
+    if (instance >= BLE_ADV_INSTANCES) {
+        return BLE_HS_EINVAL;
+    }
+
+    ble_hs_lock();
+
+    /* Periodic advertising cannot start unless it is configured before */
+    if (!ble_gap_slave[instance].per_configured) {
+        ble_hs_unlock();
+        return BLE_HS_EINVAL;
+    }
+
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, 
BLE_HCI_OCF_LE_SET_PERIODIC_ADV_ENABLE);
+
+    rc = ble_hs_hci_cmd_build_le_periodic_adv_enable(1, instance, buf, 
sizeof(buf));
+    if (rc != 0) {
+        ble_hs_unlock();
+        return rc;
+    }
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(opcode, buf, sizeof(buf));
+    if (rc != 0) {
+        ble_hs_unlock();
+        return rc;
+    }
+
+    ble_gap_slave[instance].per_op = BLE_GAP_OP_S_PERIODIC_ADV;
+
+    ble_hs_unlock();
+    return 0;
+}
+
+static int
+ble_gap_periodic_adv_set(uint8_t instance, uint16_t opcode,
+        struct os_mbuf **data)
+{
+
+    /* In that case we always fit all data in single HCI command */
+#if MYNEWT_VAL(BLE_EXT_ADV_MAX_SIZE) <= BLE_HCI_MAX_PERIODIC_ADV_DATA_LEN
+
+    static uint8_t buf[BLE_HCI_SET_PERIODIC_ADV_DATA_HDR_LEN +
+                       MYNEWT_VAL(BLE_EXT_ADV_MAX_SIZE)];
+    uint16_t len = OS_MBUF_PKTLEN(*data);
+    int rc;
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, opcode);
+
+    rc = ble_hs_hci_cmd_build_le_periodic_adv_data(
+            instance,
+            BLE_HCI_LE_SET_DATA_OPER_COMPLETE,
+            *data, len,
+            buf, sizeof(buf));
+
+    if (rc) {
+        return rc;
+    }
+
+    os_mbuf_adj(*data, MYNEWT_VAL(BLE_EXT_ADV_MAX_SIZE));
+    *data = os_mbuf_trim_front(*data);
+
+    return ble_hs_hci_cmd_tx_empty_ack(opcode, buf,
+            BLE_HCI_SET_PERIODIC_ADV_DATA_HDR_LEN + len);
+#else
+    static uint8_t buf[BLE_HCI_SET_PERIODIC_ADV_DATA_HDR_LEN +
+                       BLE_HCI_MAX_PERIODIC_ADV_DATA_LEN];
+    uint16_t len = OS_MBUF_PKTLEN(*data);
+    uint8_t op;
+    int rc;
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, opcode);
+
+    /* Complete data */
+    if (len <= BLE_HCI_MAX_PERIODIC_ADV_DATA_LEN) {
+        rc = ble_hs_hci_cmd_build_le_periodic_adv_data(instance,
+                BLE_HCI_LE_SET_DATA_OPER_COMPLETE, *data, len, buf,
+                sizeof(buf));
+        if (rc) {
+            return rc;
+        }
+
+        os_mbuf_adj(*data, len);
+        *data = os_mbuf_trim_front(*data);
+
+        return ble_hs_hci_cmd_tx_empty_ack(opcode, buf,
+                BLE_HCI_SET_PERIODIC_ADV_DATA_HDR_LEN + len);
+    }
+
+    /* If the periodic advertising is already enabled, the periodic advertising
+     * the op code shall be nothing but 0x03
+     * Bluetooth Core Specification, section 7.8.62
+     */
+    if (ble_gap_slave[instance].per_op == BLE_GAP_OP_S_PERIODIC_ADV){
+        return BLE_HS_EINVAL;
+    }
+
+    /* First fragment  */
+    op = BLE_HCI_LE_SET_DATA_OPER_FIRST;
+
+    do {
+        rc = ble_hs_hci_cmd_build_le_periodic_adv_data(instance, op, *data,
+                BLE_HCI_MAX_PERIODIC_ADV_DATA_LEN, buf, sizeof(buf));
+        if (rc) {
+            return rc;
+        }
+
+        os_mbuf_adj(*data, BLE_HCI_MAX_PERIODIC_ADV_DATA_LEN);
+        *data = os_mbuf_trim_front(*data);
+
+        rc = ble_hs_hci_cmd_tx_empty_ack(opcode, buf, sizeof(buf));
+        if (rc) {
+            return rc;
+        }
+
+        len -= BLE_HCI_MAX_PERIODIC_ADV_DATA_LEN;
+        op = BLE_HCI_LE_SET_DATA_OPER_INT;
+    } while (len > BLE_HCI_MAX_PERIODIC_ADV_DATA_LEN);
+
+    /* Last fragment */
+    rc = ble_hs_hci_cmd_build_le_periodic_adv_data(
+            instance,
+            BLE_HCI_LE_SET_DATA_OPER_LAST,
+            *data, len, buf, sizeof(buf));
+    if (rc) {
+        return rc;
+    }
+
+    os_mbuf_adj(*data, len);
+    *data = os_mbuf_trim_front(*data);
+
+    return ble_hs_hci_cmd_tx_empty_ack(opcode, buf,
+            BLE_HCI_SET_PERIODIC_ADV_DATA_HDR_LEN + len);
+#endif
+}
+
+static int
+ble_gap_periodic_adv_set_data_validate(uint8_t instance,
+        struct os_mbuf *data)
+{
+    /* The corresponding extended advertising instance should be configured */
+    if (!ble_gap_slave[instance].configured){
+        return BLE_HS_EINVAL;
+    }
+
+    if (ble_gap_slave[instance].legacy_pdu) {
+        return BLE_HS_EINVAL;
+    }
+
+    /* One more check states that if the periodic advertising is already
+     * enabled, the operation shall be 0x03 (Complete).
+     * This check is hanled during sending the data to the controller, as the
+     * length checks are already checked there, so this saves duplicate code
+     */
+
+    return 0;
+}
+
+int
+ble_gap_periodic_adv_set_data(uint8_t instance, struct os_mbuf *data)
+{
+    int rc;
+
+    if (instance >= BLE_ADV_INSTANCES) {
+        rc = BLE_HS_EINVAL;
+        goto done;
+    }
+
+    ble_hs_lock();
+
+    rc = ble_gap_periodic_adv_set_data_validate(instance, data);
+    if (rc != 0) {
+        ble_hs_unlock();
+        goto done;
+    }
+
+    rc = ble_gap_periodic_adv_set(
+            instance,
+            BLE_HCI_OCF_LE_SET_PERIODIC_ADV_DATA,
+            &data);
+
+    ble_hs_unlock();
+
+    done: os_mbuf_free_chain(data);
+    return rc;
+}
+
+static int
+ble_gap_periodic_adv_stop_no_lock(uint8_t instance)
+{
+    uint8_t buf[BLE_HCI_LE_SET_PERIODIC_ADV_ENABLE_LEN];
+    uint16_t opcode;
+    int rc;
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, 
BLE_HCI_OCF_LE_SET_PERIODIC_ADV_ENABLE);
+
+    rc = ble_hs_hci_cmd_build_le_periodic_adv_enable(0, instance, buf, 
sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(opcode, buf, sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    ble_gap_slave[instance].per_op = BLE_GAP_OP_NULL;
+
+    return 0;
+}
+
+int
+ble_gap_periodic_adv_stop(uint8_t instance)
+{
+    int rc;
+
+    if (instance >= BLE_ADV_INSTANCES) {
+        return BLE_HS_EINVAL;
+    }
+
+    ble_hs_lock();
+    rc = ble_gap_periodic_adv_stop_no_lock(instance);
+    ble_hs_unlock();
+
+    return rc;
+}
+
+int
+ble_gap_periodic_adv_create_sync(uint8_t filter_policy, uint8_t adv_sid,
+        uint8_t adv_addr_type, const uint8_t *adv_addr, uint16_t skip,
+        uint16_t sync_timeout, uint8_t unused)
+
+{
+    uint8_t buf[BLE_HCI_LE_PERIODIC_ADV_CREATE_SYNC_LEN];
+    uint16_t opcode;
+    int rc = 0;
+
+    if (!ble_hs_periodic_sync_can_alloc()){
+        return BLE_HS_ENOMEM;
+    }
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_PERIODIC_ADV_CREATE_SYNC);
+
+    /* No sync can be created if another sync is still pending */
+    if (ble_gap_master.pending_create_sync){
+        return BLE_HS_EINVAL;
+    }
+
+    rc = ble_hs_hci_cmd_build_le_periodic_adv_create_sync(filter_policy,
+                                                          adv_sid,
+                                                          adv_addr_type,
+                                                          adv_addr,
+                                                          skip, sync_timeout,
+                                                          unused, buf,
+                                                          sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    /* This shall be decremented upon receiving sync_established event,
+     * or if the sync is cancelled before receiving that event.
+     */
+    ble_gap_master.pending_create_sync = 1;
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(opcode, buf, sizeof(buf));
+
+    return rc;
+}
+
+int
+ble_gap_periodic_adv_create_sync_cancel(void)
+{
+    uint16_t opcode;
+    int rc = 0;
+
+    if (!ble_gap_master.pending_create_sync){
+        return BLE_HS_EINVAL;
+    }
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE,
+            BLE_HCI_OCF_LE_PERIODIC_ADV_CREATE_SYNC_CANCEL);
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(opcode, NULL, 0);
+
+    if (!rc){
+        ble_gap_master.pending_create_sync = 0;
+    }
+    return rc;
+
+}
+
+int
+ble_gap_periodic_adv_terminate_sync(uint16_t sync_handle)
+{
+    uint8_t buf[BLE_HCI_LE_PERIODIC_ADV_TERM_SYNC_LEN];
+    uint16_t opcode;
+    struct ble_hs_periodic_sync *psync;
+    int rc = 0;
+
+    if (ble_gap_master.pending_create_sync){
+        return BLE_HS_EINVAL;
+    }
+
+    ble_hs_lock();
+    /* The handle must be in the list */
+    psync = ble_hs_periodic_sync_find_assert(sync_handle);
+
+    /* Remove the handle from the list */
+    ble_hs_periodic_sync_remove(psync);
+
+    /* Free the memory occupied by psync as it is no longer needed */
+    ble_hs_periodic_sync_free(psync);
+    ble_hs_unlock();
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_PERIODIC_ADV_TERM_SYNC);
+
+    rc = ble_hs_hci_cmd_build_le_periodic_adv_terminate_sync(sync_handle,
+                                                            buf, sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(opcode, buf, sizeof(buf));
+
+    return rc;
+
+}
+
+int
+ble_gap_add_dev_to_periodic_adv_list(uint8_t adv_addr_type,
+        const uint8_t *adv_addr, uint8_t adv_sid)
+{
+    uint8_t buf[BLE_HCI_LE_ADD_DEV_TO_PERIODIC_ADV_LIST_LEN];
+    uint16_t opcode;
+    int rc = 0;
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, 
BLE_HCI_OCF_LE_ADD_DEV_TO_PERIODIC_ADV_LIST);
+
+    rc = ble_hs_hci_cmd_build_le_add_dev_to_periodic_adv_list(adv_addr_type,
+            adv_addr, adv_sid, buf, sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(opcode, buf, sizeof(buf));
+
+    return rc;
+}
+
+int
+ble_gap_rem_dev_from_periodic_adv_list(uint8_t adv_addr_type,
+        const uint8_t *adv_addr, uint8_t adv_sid)
+{
+    uint8_t buf[BLE_HCI_LE_REM_DEV_FROM_PERIODIC_ADV_LIST_LEN];
+    uint16_t opcode;
+    int rc = 0;
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE,
+            BLE_HCI_OCF_LE_REM_DEV_FROM_PERIODIC_ADV_LIST);
+
+    rc = ble_hs_hci_cmd_build_le_rem_dev_from_periodic_adv_list(adv_addr_type,
+            adv_addr, adv_sid, buf, sizeof(buf));
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(opcode, buf, sizeof(buf));
+
+    return rc;
+}
+
+int
+ble_gap_clear_periodic_adv_list(void)
+{
+    uint16_t opcode;
+    int rc = 0;
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, 
BLE_HCI_OCF_LE_CLEAR_PERIODIC_ADV_LIST);
+
+    rc = ble_hs_hci_cmd_tx_empty_ack(opcode, NULL, 0);
+
+    return rc;
+}
+
+int
+ble_gap_read_periodic_adv_list_size(uint8_t *per_adv_list_size)
+{
+    uint8_t rspbuf[1];
+    uint8_t rsplen;
+    uint16_t opcode;
+    int rc = 0;
+
+    opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, 
BLE_HCI_OCF_LE_RD_PERIODIC_ADV_LIST_SIZE);
+
+    rc = ble_hs_hci_cmd_tx(opcode, NULL, 0, rspbuf, sizeof(rspbuf), &rsplen);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (rsplen != sizeof(rspbuf)) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    *per_adv_list_size = rspbuf[0];
+
+    return 0;
+}
+
 #endif
 
 /*****************************************************************************
  * $discovery procedures                                                     *
  *****************************************************************************/
 
-#if MYNEWT_VAL(BLE_EXT_ADV) && NIMBLE_BLE_SCAN
 
 Review comment:
   Not relevant, please remove from this PR

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to