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

 ##########
 File path: nimble/host/src/ble_gap.c
 ##########
 @@ -2915,8 +2985,515 @@ ble_gap_ext_adv_remove(uint8_t instance)
 
     memset(&ble_gap_slave[instance], 0, sizeof(struct ble_gap_slave_state));
     ble_hs_unlock();
+}
+
+#if MYNEWT_VAL(BLE_PERIODIC_ADV)
+static int
+ble_gap_per_adv_params_tx(uint8_t instance,
+               const struct ble_gap_per_adv_params *params)
+
+{
+       struct hci_per_adv_params hci_adv_params;
+       uint8_t buf[BLE_HCI_LE_SET_PER_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_PER_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_per_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_PER_ADV_PARAMS), buf,
+                       sizeof(buf));
+
+       if (rc != 0) {
+               return rc;
+       }
+
+       return 0;
+}
+
+static int
+ble_gap_per_adv_params_validate(
+               const struct ble_gap_per_adv_params *params) {
+       if (!params) {
+               return BLE_HS_EINVAL;
+       }
+
+       // 1- if anonymous return BLE_HS_EINVAL
+
+       // 2- if periodic adv is enabled return BLE_HS_EINVAL
+
+       return 0;
 
-    return 0;
+}
+
+int
+ble_gap_per_adv_configure(uint8_t instance,
+               const struct ble_gap_per_adv_params *params) {
+       int rc;
+
+       if (instance >= BLE_ADV_INSTANCES) {
+               return BLE_HS_EINVAL;
+       }
+
+       rc = ble_gap_per_adv_params_validate(params);
+       if (rc) {
+               return rc;
+       }
+
+       rc = ble_gap_per_adv_params_tx(instance, params);
+       if (rc) {
+               ble_hs_unlock();
+               return rc;
+       }
+
+       return 0;
+}
+
+int
+ble_gap_per_adv_start(uint8_t instance) {
+       const uint8_t *rnd_addr;
+       uint8_t buf[BLE_HCI_LE_SET_PER_ADV_ENABLE_LEN];
+       uint16_t opcode;
+       int rc;
+
+       if (instance >= BLE_ADV_INSTANCES) {
+               return BLE_HS_EINVAL;
+       }
+
+       ble_hs_lock();
+
+       // 1- if periodic_adv_enable when periodic_adv is enabled => change the 
random address
+
+       //    if (!ble_gap_slave[instance].configured) {
+       //        ble_hs_unlock();
+       //        return BLE_HS_EINVAL;
+       //    }
+
+       //    if (ble_gap_slave[instance].op != BLE_GAP_OP_NULL) {
+       //        ble_hs_unlock();
+       //        return  BLE_HS_EALREADY;
+       //    }
+
+       /* verify own address type if random address for instance wasn't 
explicitly
+        * set
+        */
+       //    switch (ble_gap_slave[instance].our_addr_type) {
+       //    case BLE_OWN_ADDR_RANDOM:
+       //    case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT:
+       //        if (ble_gap_slave[instance].rnd_addr_set) {
+       //            break;
+       //        }
+       //        /* fall through */
+       //    case BLE_OWN_ADDR_PUBLIC:
+       //    case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT:
+       //    default:
+       //        rc = 
ble_hs_id_use_addr(ble_gap_slave[instance].our_addr_type);
+       //        if (rc) {
+       //            ble_hs_unlock();
+       //            return BLE_HS_EINVAL;
+       //        }
+       //        break;
+       //    }
+       /* fallback to ID static random address if using random address and 
instance
+        * wasn't configured with own address
+        */
+       //    if (!ble_gap_slave[instance].rnd_addr_set) {
+       //        switch (ble_gap_slave[instance].our_addr_type) {
+       //        case BLE_OWN_ADDR_RANDOM:
+       //        case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT:
+       //            rc = ble_hs_id_addr(BLE_ADDR_RANDOM, &rnd_addr, NULL);
+       //            if (rc != 0) {
+       //                ble_hs_unlock();
+       //                return rc;
+       //            }
+       //
+       //            rc = ble_gap_ext_adv_set_addr_no_lock(instance, rnd_addr);
+       //            if (rc != 0) {
+       //                ble_hs_unlock();
+       //                return rc;
+       //            }
+       //            break;
+       //        default:
+       //            break;
+       //        }
+       //    }
+       opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_PER_ADV_ENABLE);
+
+       rc = ble_hs_hci_cmd_build_le_per_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].op = BLE_GAP_OP_S_ADV;
+
+       ble_hs_unlock();
+       return 0;
+}
+
+static int
+ble_gap_per_adv_set(uint8_t instance, uint16_t opcode,
 
 Review comment:
   ok, let us leave it for now as it is.

----------------------------------------------------------------
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