NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
ble_ll_scan.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef H_BLE_LL_SCAN_
21#define H_BLE_LL_SCAN_
22
23#include "ble_ll_sched.h"
24#include "ble_ll_tmr.h"
25#include "nimble/porting/nimble/include/syscfg/syscfg.h"
26#include "nimble/nimble/include/nimble/nimble_npl.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/*
33 * SCAN_REQ
34 * -> ScanA (6 bytes)
35 * -> AdvA (6 bytes)
36 *
37 * ScanA is the scanners public (TxAdd=0) or random (TxAdd = 1) address
38 * AdvaA is the advertisers public (RxAdd=0) or random (RxAdd=1) address.
39 *
40 * Sent by the LL in the Scanning state; received by the LL in the advertising
41 * state. The advertising address is the intended recipient of this frame.
42 */
43#define BLE_SCAN_REQ_LEN (12)
44
45/*
46 * SCAN_RSP
47 * -> AdvA (6 bytes)
48 * -> ScanRspData (0 - 31 bytes)
49 *
50 * AdvaA is the advertisers public (TxAdd=0) or random (TxAdd=1) address.
51 * ScanRspData may contain any data from the advertisers host.
52 *
53 * Sent by the LL in the advertising state; received by the LL in the
54 * scanning state.
55 */
56#define BLE_SCAN_RSP_LEGACY_DATA_MAX_LEN (31)
57#define BLE_SCAN_LEGACY_MAX_PKT_LEN (37)
58
59#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
60#define BLE_SCAN_RSP_DATA_MAX_LEN MYNEWT_VAL(BLE_EXT_ADV_MAX_SIZE)
61
62/* For Bluetooth 5.0 we need state machine for two PHYs*/
63#define BLE_LL_SCAN_PHY_NUMBER (2)
64#else
65#define BLE_LL_SCAN_PHY_NUMBER (1)
66#define BLE_SCAN_RSP_DATA_MAX_LEN BLE_SCAN_RSP_LEGACY_DATA_MAX_LEN
67#endif
68
69#define PHY_UNCODED (0)
70#define PHY_CODED (1)
71
72#define BLE_LL_EXT_ADV_MODE_NON_CONN (0x00)
73#define BLE_LL_EXT_ADV_MODE_CONN (0x01)
74#define BLE_LL_EXT_ADV_MODE_SCAN (0x02)
75
76/* All values are stored as ticks */
77struct ble_ll_scan_timing {
78 uint32_t interval;
79 uint32_t window;
80 uint32_t start_time;
81};
82
83struct ble_ll_scan_phy
84{
85 uint8_t phy;
86 uint8_t configured;
87 uint8_t scan_type;
88 uint8_t scan_chan;
89 struct ble_ll_scan_timing timing;
90};
91
92struct ble_ll_scan_pdu_data {
93 uint8_t hdr_byte;
94 /* ScanA for SCAN_REQ and InitA for CONNECT_IND */
95 union {
96 uint8_t scana[BLE_DEV_ADDR_LEN];
97 uint8_t inita[BLE_DEV_ADDR_LEN];
98 };
99 uint8_t adva[BLE_DEV_ADDR_LEN];
100};
101
102struct ble_ll_scan_addr_data {
103 uint8_t *adva;
104 uint8_t *targeta;
105 uint8_t *adv_addr;
106 uint8_t adva_type : 1;
107 uint8_t targeta_type : 1;
108 uint8_t adv_addr_type : 1;
109#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
110 uint8_t adva_resolved : 1;
111 uint8_t targeta_resolved : 1;
112 int8_t rpa_index;
113#endif
114};
115
116struct ble_ll_scan_sm
117{
118 uint8_t scan_enabled;
119
120 uint8_t own_addr_type;
121 uint8_t scan_filt_policy;
122 uint8_t scan_filt_dups;
123 uint8_t scan_rsp_pending;
124 uint8_t scan_rsp_cons_fails;
125 uint8_t scan_rsp_cons_ok;
126 uint8_t scan_peer_rpa[BLE_DEV_ADDR_LEN];
127#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
128 ble_npl_time_t scan_nrpa_timer;
129 uint8_t scan_nrpa[BLE_DEV_ADDR_LEN];
130#endif
131 struct ble_ll_scan_pdu_data pdu_data;
132
133 /* XXX: Shall we count backoff per phy? */
134 uint16_t upper_limit;
135 uint16_t backoff_count;
136 uint32_t scan_win_start_time;
137 struct ble_npl_event scan_sched_ev;
138 struct ble_ll_tmr scan_timer;
139 struct ble_npl_event scan_interrupted_ev;
140
141#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
142 struct ble_npl_callout duration_timer;
143 struct ble_npl_callout period_timer;
144 ble_npl_time_t duration_ticks;
145 ble_npl_time_t period_ticks;
146 uint8_t ext_scanning;
147#endif
148
149 uint8_t restart_timer_needed;
150
151 struct ble_ll_scan_phy *scanp;
152 struct ble_ll_scan_phy *scanp_next;
153 struct ble_ll_scan_phy scan_phys[BLE_LL_SCAN_PHY_NUMBER];
154
155#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
156 /* Connection sm for initiator scan */
157 struct ble_ll_conn_sm *connsm;
158#endif
159};
160
161/* Scan types */
162#define BLE_SCAN_TYPE_PASSIVE (BLE_HCI_SCAN_TYPE_PASSIVE)
163#define BLE_SCAN_TYPE_ACTIVE (BLE_HCI_SCAN_TYPE_ACTIVE)
164#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
165#define BLE_SCAN_TYPE_INITIATE (2)
166#endif
167
168/*---- HCI ----*/
169/* Set scanning parameters */
170int ble_ll_scan_hci_set_params(const uint8_t *cmdbuf, uint8_t len);
171
172/* Turn scanning on/off */
173int ble_ll_scan_hci_set_enable(const uint8_t *cmdbuf, uint8_t len);
174int ble_ll_scan_hci_set_ext_enable(const uint8_t *cmdbuf, uint8_t len);
175
176#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
177int ble_ll_scan_hci_set_ext_params(const uint8_t *cmdbuf, uint8_t len);
178#endif
179
180/*--- Controller Internal API ---*/
181/* Initialize the scanner */
182void ble_ll_scan_init(void);
183
184/* Reset the scanner */
185void ble_ll_scan_reset(void);
186
187/* Called when Link Layer starts to receive a PDU and is in scanning state */
188int ble_ll_scan_rx_isr_start(uint8_t pdu_type, uint16_t *rxflags);
189
190/* Called when Link Layer has finished receiving a PDU while scanning */
191int ble_ll_scan_rx_isr_end(struct os_mbuf *rxpdu, uint8_t crcok);
192
193/* Process a scan response PDU */
194void ble_ll_scan_rx_pkt_in(uint8_t pdu_type, struct os_mbuf *om,
195 struct ble_mbuf_hdr *hdr);
196
197/* Boolean function denoting whether or not the whitelist can be changed */
198int ble_ll_scan_can_chg_whitelist(void);
199
200/* Boolean function returning true if scanning enabled */
201int ble_ll_scan_enabled(void);
202
203/* Initialize the scanner when we start initiating */
204struct ble_ll_conn_create_scan;
205struct ble_ll_conn_create_params;
206int
207ble_ll_scan_initiator_start(struct ble_ll_conn_sm *connsm, uint8_t ext,
208 struct ble_ll_conn_create_scan *cc_scan);
209
210/* Returns storage for PDU data (for SCAN_REQ or CONNECT_IND) */
211struct ble_ll_scan_pdu_data *ble_ll_scan_get_pdu_data(void);
212
213/* Called to set the resolvable private address of the last connected peer */
214void ble_ll_scan_set_peer_rpa(uint8_t *rpa);
215
216/* Returns peer RPA of last connection made */
217uint8_t *ble_ll_scan_get_peer_rpa(void);
218
219/* Returns the local RPA used by the scanner/initiator */
220uint8_t *ble_ll_scan_get_local_rpa(void);
221
222/* Stop the scanning state machine */
223void ble_ll_scan_sm_stop(int chk_disable);
224
225/* Resume scanning */
226#if MYNEWT_VAL(BLE_LL_ROLE_OBSERVER)
227void ble_ll_scan_chk_resume(void);
228#else
229static inline void ble_ll_scan_chk_resume(void) { };
230#endif
231
232/* Called when wait for response timer expires in scanning mode */
233void ble_ll_scan_wfr_timer_exp(void);
234
235/* Called when scan could be interrupted */
236void ble_ll_scan_interrupted(struct ble_ll_scan_sm *scansm);
237
238#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
239/* Called to parse extended advertising*/
240void ble_ll_scan_end_adv_evt(struct ble_ll_aux_data *aux_data);
241#endif
242
243/* Called to halt currently running scan */
244void ble_ll_scan_halt(void);
245
246uint8_t *ble_ll_get_scan_nrpa(void);
247uint8_t ble_ll_scan_get_own_addr_type(void);
248uint8_t ble_ll_scan_get_filt_policy(void);
249uint8_t ble_ll_scan_get_filt_dups(void);
250uint8_t ble_ll_scan_backoff_kick(void);
251void ble_ll_scan_backoff_update(int success);
252
253int ble_ll_scan_dup_check_ext(uint8_t addr_type, uint8_t *addr, bool has_aux,
254 uint16_t adi);
255int ble_ll_scan_dup_update_ext(uint8_t addr_type, uint8_t *addr, bool has_aux,
256 uint16_t adi);
257int ble_ll_scan_have_rxd_scan_rsp(uint8_t *addr, uint8_t txadd, uint8_t ext_adv,
258 uint16_t adi);
259void ble_ll_scan_add_scan_rsp_adv(uint8_t *addr, uint8_t txadd, uint8_t ext_adv,
260 uint16_t adi);
261
262int
263ble_ll_scan_rx_filter(uint8_t own_addr_type, uint8_t scan_filt_policy,
264 struct ble_ll_scan_addr_data *addrd, uint8_t *scan_ok);
265int ble_ll_scan_rx_check_init(struct ble_ll_scan_addr_data *addrd);
266
267#ifdef __cplusplus
268}
269#endif
270
271#endif /* H_BLE_LL_SCAN_ */
Definition os_mbuf.h:86