You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2019/06/27 14:52:54 UTC

[mynewt-nimble] 01/03: nimble/host: Add event for scan request notification

This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 70d53304c072808c8b82ad89be4bf3a063748040
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Tue Jun 11 00:00:50 2019 +0200

    nimble/host: Add event for scan request notification
    
    We can enable scan request notifications for an advertising instance so
    would be good to have event that app can actually use.
---
 nimble/host/include/host/ble_gap.h | 16 ++++++++++++++++
 nimble/host/src/ble_gap.c          | 27 +++++++++++++++++++++++++++
 nimble/host/src/ble_gap_priv.h     |  1 +
 nimble/host/src/ble_hs_hci_evt.c   | 24 ++++++++++++++++++++++++
 nimble/include/nimble/hci_common.h |  9 +++++++++
 5 files changed, 77 insertions(+)

diff --git a/nimble/host/include/host/ble_gap.h b/nimble/host/include/host/ble_gap.h
index a6f92a0..a23cdd4 100644
--- a/nimble/host/include/host/ble_gap.h
+++ b/nimble/host/include/host/ble_gap.h
@@ -127,6 +127,7 @@ struct hci_conn_update;
 #define BLE_GAP_EVENT_PERIODIC_SYNC         20
 #define BLE_GAP_EVENT_PERIODIC_REPORT       21
 #define BLE_GAP_EVENT_PERIODIC_SYNC_LOST    22
+#define BLE_GAP_EVENT_SCAN_REQ_RCVD         23
 
 /*** Reason codes for the subscribe GAP event. */
 
@@ -909,6 +910,21 @@ struct ble_gap_event {
             int reason;
         } periodic_sync_lost;
 #endif
+
+#if MYNEWT_VAL(BLE_EXT_ADV)
+        /**
+         * Represents a scan request for an extended advertising instance where
+         * scan request notifications were enabled.
+         * Valid for the following event types:
+         *     o BLE_GAP_EVENT_SCAN_REQ_RCVD
+         */
+        struct {
+            /** Extended advertising instance */
+            uint8_t instance;
+            /** Address of scanner */
+            ble_addr_t scan_addr;
+        } scan_req_rcvd;
+#endif
     };
 };
 
diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c
index 3bc6bb1..45bc401 100644
--- a/nimble/host/src/ble_gap.c
+++ b/nimble/host/src/ble_gap.c
@@ -743,6 +743,27 @@ ble_gap_adv_finished(uint8_t instance, int reason, uint16_t conn_handle,
     }
 }
 
+#if MYNEWT_VAL(BLE_EXT_ADV)
+static void
+ble_gap_scan_req_rcvd(uint8_t instance, uint8_t scan_addr_type,
+                      uint8_t *scan_addr)
+{
+    struct ble_gap_event event;
+    ble_gap_event_fn *cb;
+    void *cb_arg;
+
+    ble_gap_slave_extract_cb(instance, &cb, &cb_arg);
+    if (cb != NULL) {
+        memset(&event, 0, sizeof event);
+        event.type = BLE_GAP_EVENT_SCAN_REQ_RCVD;
+        event.scan_req_rcvd.instance = instance;
+        event.scan_req_rcvd.scan_addr.type = scan_addr_type;
+        memcpy(event.scan_req_rcvd.scan_addr.val, scan_addr, 6);
+        cb(&event, cb_arg);
+    }
+}
+#endif
+
 static int
 ble_gap_master_connect_failure(int status)
 {
@@ -1302,6 +1323,12 @@ ble_gap_rx_adv_set_terminated(struct hci_le_adv_set_terminated *evt)
     ble_gap_adv_finished(evt->adv_handle, reason, conn_handle,
                          evt->completed_events);
 }
+
+void
+ble_gap_rx_scan_req_rcvd(struct hci_le_scan_req_rcvd *evt)
+{
+    ble_gap_scan_req_rcvd(evt->adv_handle, evt->scan_addr_type, evt->scan_addr);
+}
 #endif
 
 /* Periodic adv events */
diff --git a/nimble/host/src/ble_gap_priv.h b/nimble/host/src/ble_gap_priv.h
index c2aba24..48a1253 100644
--- a/nimble/host/src/ble_gap_priv.h
+++ b/nimble/host/src/ble_gap_priv.h
@@ -87,6 +87,7 @@ void ble_gap_rx_peroidic_adv_sync_estab(struct hci_le_subev_periodic_adv_sync_es
 void ble_gap_rx_periodic_adv_rpt(struct hci_le_subev_periodic_adv_rpt *evt);
 void ble_gap_rx_periodic_adv_sync_lost(struct hci_le_subev_periodic_adv_sync_lost *evt);
 #endif
+void ble_gap_rx_scan_req_rcvd(struct hci_le_scan_req_rcvd *evt);
 #endif
 void ble_gap_rx_adv_report(struct ble_gap_disc_desc *desc);
 void ble_gap_rx_rd_rem_sup_feat_complete(struct hci_le_rd_rem_supp_feat_complete *evt);
diff --git a/nimble/host/src/ble_hs_hci_evt.c b/nimble/host/src/ble_hs_hci_evt.c
index 2ce0e03..c071593 100644
--- a/nimble/host/src/ble_hs_hci_evt.c
+++ b/nimble/host/src/ble_hs_hci_evt.c
@@ -54,6 +54,7 @@ static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_adv_set_terminated;
 static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_periodic_adv_sync_estab;
 static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_periodic_adv_rpt;
 static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_periodic_adv_sync_lost;
+static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_scan_req_rcvd;
 /* Statistics */
 struct host_hci_stats
 {
@@ -113,6 +114,8 @@ static const struct ble_hs_hci_evt_le_dispatch_entry
             ble_hs_hci_evt_le_scan_timeout },
     { BLE_HCI_LE_SUBEV_ADV_SET_TERMINATED,
             ble_hs_hci_evt_le_adv_set_terminated },
+    { BLE_HCI_LE_SUBEV_SCAN_REQ_RCVD,
+            ble_hs_hci_evt_le_scan_req_rcvd },
 };
 
 #define BLE_HS_HCI_EVT_LE_DISPATCH_SZ \
@@ -723,6 +726,27 @@ ble_hs_hci_evt_le_adv_set_terminated(uint8_t subevent, uint8_t *data, int len)
 }
 
 static int
+ble_hs_hci_evt_le_scan_req_rcvd(uint8_t subevent, uint8_t *data, int len)
+{
+#if MYNEWT_VAL(BLE_EXT_ADV)
+    struct hci_le_scan_req_rcvd evt;
+
+    if (len != BLE_HCI_LE_SUBEV_SCAN_REQ_RCVD_LEN) {
+        return BLE_HS_ECONTROLLER;
+    }
+
+    evt.subevent_code = data[0];
+    evt.adv_handle = data[1];
+    evt.scan_addr_type = data[2];
+    memcpy(evt.scan_addr, data + 3, 6);
+
+    ble_gap_rx_scan_req_rcvd(&evt);
+#endif
+
+    return 0;
+}
+
+static int
 ble_hs_hci_evt_le_conn_upd_complete(uint8_t subevent, uint8_t *data, int len)
 {
     struct hci_le_conn_upd_complete evt;
diff --git a/nimble/include/nimble/hci_common.h b/nimble/include/nimble/hci_common.h
index 964cd06..1b049c8 100644
--- a/nimble/include/nimble/hci_common.h
+++ b/nimble/include/nimble/hci_common.h
@@ -1212,6 +1212,15 @@ struct hci_le_adv_set_terminated
     uint8_t completed_events;
 };
 
+/* LE Scan Request Received subevent */
+struct hci_le_scan_req_rcvd
+{
+    uint8_t subevent_code;
+    uint8_t adv_handle;
+    uint8_t scan_addr_type;
+    uint8_t scan_addr[BLE_DEV_ADDR_LEN];
+};
+
 #define BLE_HCI_DATA_HDR_SZ                 4
 #define BLE_HCI_DATA_HANDLE(handle_pb_bc)   (((handle_pb_bc) & 0x0fff) >> 0)
 #define BLE_HCI_DATA_PB(handle_pb_bc)       (((handle_pb_bc) & 0x3000) >> 12)