Skip to content

Commit c495bd2

Browse files
eivindj-nordicsondrepasilz
committed
lib: scan: add scan library
Add scan library. Co-authored-by: Sondre Pettersen <[email protected]> Co-authored-by: Asil Zogby <[email protected]> Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent 168cf77 commit c495bd2

File tree

16 files changed

+2933
-0
lines changed

16 files changed

+2933
-0
lines changed

CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
/lib/bluetooth/ble_qwr/ @nrfconnect/ncs-bm
5454
/lib/bluetooth/ble_racp/ @nrfconnect/ncs-bm
5555
/lib/bluetooth/ble_radio_notification/ @nrfconnect/ncs-bm
56+
/lib/bluetooth/ble_scan/ @nrfconnect/ncs-bm
5657
/lib/bluetooth/peer_manager/ @nrfconnect/ncs-bm
5758
/lib/bm_buttons/ @nrfconnect/ncs-bm
5859
/lib/bm_timer/ @nrfconnect/ncs-bm
@@ -101,6 +102,7 @@
101102
/tests/lib/bluetooth/ble_qwr/ @nrfconnect/ncs-bm
102103
/tests/lib/bluetooth/ble_racp/ @nrfconnect/ncs-bm
103104
/tests/lib/bluetooth/ble_radio_notif/ @nrfconnect/ncs-bm
105+
/tests/lib/bluetooth/ble_scan/ @nrfconnect/ncs-bm
104106
/tests/subsys/bluetooth/services/ @nrfconnect/ncs-bm @nrfconnect/ncs-bm-test
105107
/tests/subsys/fs/bm_zms/ @nrfconnect/ncs-bm @rghaddab
106108
/tests/subsys/kmu/ @nrfconnect/ncs-eris

doc/nrf-bm/api/api.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ Bluetooth LE Radio Notification library
5757
:inner:
5858
:members:
5959

60+
.. _api_ble_scan:
61+
62+
Bluetooth LE Scan library
63+
=========================
64+
65+
.. doxygengroup:: ble_scan
66+
:inner:
67+
:members:
68+
6069
.. _api_bm_buttons:
6170

6271
Bare Metal Buttons library

doc/nrf-bm/doxygen/nrf-bm.doxyfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,7 @@ INCLUDE_FILE_PATTERNS =
24022402

24032403
PREDEFINED = __DOXYGEN__ \
24042404
CONFIG_BLE_QWR_MAX_ATTR=1 \
2405+
CONFIG_BLE_SCAN_FILTER=1 \
24052406

24062407
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
24072408
# tag can be used to specify a list of macro names that should be expanded. The
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
.. _lib_ble_scan:
2+
3+
Bluetooth: Scan
4+
###############
5+
6+
.. contents::
7+
:local:
8+
:depth: 2
9+
10+
The Bluetooth® Low Energy Scan library handles scanning for advertising Bluetooth LE devices.
11+
You can use it to find an advertising device and establish a connection with it.
12+
You can narrow down the scan to a device of a specific type using scan filters or the whitelist.
13+
14+
Overview
15+
********
16+
17+
You can configure this library in one of the following modes:
18+
19+
* Simple mode without using filters or the whitelist.
20+
* Advanced mode that allows you to use advanced filters and the whitelist.
21+
22+
The library can automatically establish a connection on a filter match or when identifying a whitelisted device.
23+
24+
The library registers as a Bluetooth LE event observer using the :c:macro:`NRF_SDH_BLE_OBSERVER` macro and handles the relevant Bluetooth LE events from the SoftDevice.
25+
26+
Configuration
27+
*************
28+
29+
To enable the library, set the :kconfig:option:`CONFIG_BLE_SCAN` Kconfig option.
30+
31+
The library provides the following Kconfig configuration options:
32+
33+
* :kconfig:option:`CONFIG_BLE_SCAN_BUFFER_SIZE` - Maximum size of an advertising event.
34+
* :kconfig:option:`CONFIG_BLE_SCAN_NAME_MAX_LEN` - Maximum size of the name to search for in the advertisement report.
35+
* :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN` - Maximum size of the short name to search for in the advertisement report.
36+
* :kconfig:option:`CONFIG_BLE_SCAN_FILTER` - Enabling filters for the scan library.
37+
* :kconfig:option:`CONFIG_BLE_SCAN_NAME_COUNT` - Maximum number of name filters.
38+
* :kconfig:option:`CONFIG_BLE_SCAN_APPEARANCE_COUNT` - Maximum number of appearance filters.
39+
* :kconfig:option:`CONFIG_BLE_SCAN_ADDRESS_COUNT` - Maximum number of address filters.
40+
* :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_COUNT` - Maximum number of short name filters.
41+
* :kconfig:option:`CONFIG_BLE_SCAN_UUID_COUNT` - Maximum number of filters for UUIDs.
42+
* :kconfig:option:`CONFIG_BLE_SCAN_INTERVAL` - Determines the scan interval in units of 0.625 ms.
43+
* :kconfig:option:`CONFIG_BLE_SCAN_DURATION` - Duration of a scanning session in units of 10 ms, if set to 0, the scanning continues until it is explicitly disabled.
44+
* :kconfig:option:`CONFIG_BLE_SCAN_WINDOW` - Determines the scanning window in units of 0.625 ms.
45+
* :kconfig:option:`CONFIG_BLE_SCAN_SLAVE_LATENCY` - Determines the slave latency in counts of connection events.
46+
* :kconfig:option:`CONFIG_BLE_SCAN_MIN_CONNECTION_INTERVAL` - Determines the minimum connection interval in units of 1.25 ms.
47+
* :kconfig:option:`CONFIG_BLE_SCAN_MAX_CONNECTION_INTERVAL` - Determines the maximum connection interval in units of 1.25 ms.
48+
* :kconfig:option:`CONFIG_BLE_SCAN_SUPERVISION_TIMEOUT` - Determines the supervision time-out in units of 10 ms.
49+
50+
Initialization
51+
==============
52+
53+
The library is initialized by calling the :c:func:`ble_scan_init` function.
54+
The application can provide an event handler to receive events to inform about a filter or whitelist match, or about a connection error during the automatic connection.
55+
56+
Simple initialization
57+
=====================
58+
59+
You can use the simple initialization with the default scanning and connection parameters when you want the library to work in the simple mode.
60+
61+
62+
.. code-block:: c
63+
64+
BLE_SCAN_DEF(ble_scan);
65+
66+
void scan_event_handler_func(struct ble_scan_evt const *scan_evt);
67+
68+
uint32_t nrf_err;
69+
struct ble_scan_config scan_cfg = {
70+
.scan_params = BLE_SCAN_SCAN_PARAMS_DEFAULT,
71+
.conn_params = BLE_SCAN_CONN_PARAMS_DEFAULT,
72+
.evt_handler = scan_event_handler_func,
73+
};
74+
75+
nrf_err = ble_scan_init(&ble_scan, &scan_cfg);
76+
if (nrf_err) {
77+
LOG_ERR("Failed to initialie scan library, nrf_error %#x", nrf_err);
78+
}
79+
80+
Advanced initialization
81+
=======================
82+
83+
The advanced initialization provides a larger configuration set for the library.
84+
It is required when using scan filters or the whitelist.
85+
86+
.. code-block:: c
87+
88+
BLE_SCAN_DEF(ble_scan);
89+
90+
void scan_event_handler_func(struct ble_scan_evt const *scan_evt);
91+
92+
uint32_t nrf_err;
93+
struct ble_scan_config scan_cfg = {
94+
.scan_params = {
95+
.active = 0x01,
96+
.interval = NRF_BLE_SCAN_INTERVAL,
97+
.window = NRF_BLE_SCAN_WINDOW,
98+
.filter_policy = BLE_GAP_SCAN_FP_WHITELIST,
99+
.timeout = SCAN_DURATION_WHITELIST,
100+
.scan_phys = BLE_GAP_PHY_1MBPS,
101+
.extended = true,
102+
},
103+
.conn_params = BLE_SCAN_CONN_PARAMS_DEFAULT,
104+
.connect_if_match = true,
105+
.conn_cfg_tag = APP_BLE_CONN_CFG_TAG,
106+
.evt_handler = scan_event_handler_func,
107+
};
108+
109+
nrf_err = ble_scan_init(&ble_scan, &scan_cfg);
110+
if (nrf_err) {
111+
LOG_ERR("Failed to initialize scan library, nrf_error %#x", nrf_err);
112+
}
113+
114+
.. note::
115+
116+
When setting connection-specific configurations with the :c:func:`sd_ble_cfg_set` function, you must create a tag for each configuration.
117+
If your application uses the library with automatic connection, this tag must be provided when calling the :c:func:`sd_ble_gap_scan_start` or the :c:func:`sd_ble_gap_connect` function.
118+
119+
Usage
120+
*****
121+
122+
The application can start scanning by calling the :c:func:`ble_scan_start` function.
123+
124+
The scan parameters can be updated by calling the :c:func:`ble_scan_params_set` function.
125+
126+
To stop scanning, call the :c:func:`ble_scan_stop` function.
127+
128+
The library resumes scanning after receiving advertising reports.
129+
Scanning stops if the library establishes a connection automatically, or if the application calls the :c:func:`ble_scan_stop` or the :c:func:`sd_ble_gap_connect` function.
130+
131+
.. note::
132+
133+
When you use the :c:func:`ble_scan_params_set` function during the ongoing scan, scanning is stopped.
134+
To resume scanning, use the :c:func:`ble_scan_start` function.
135+
136+
Whitelist
137+
=========
138+
139+
The whitelist stores information about all the device connections and bonding.
140+
If you enable the whitelist, the application receives advertising packets only from the devices that are on the whitelist.
141+
An advertising package from a whitelisted device generates the :c:macro:`NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT` event.
142+
143+
.. note::
144+
145+
When using the whitelist, filters are inactive.
146+
147+
.. caution::
148+
149+
If you use the whitelist, you must pass the event handler during the library initialization.
150+
The initial scanning with whitelist generates a :c:macro:`NRF_BLE_SCAN_EVT_WHITELIST_REQUEST` event.
151+
The application must react to this event by either setting up the whitelist or switching off the whitelist scan.
152+
Otherwise, an error is reported when the scan starts.
153+
154+
Filters
155+
=======
156+
157+
The library can set scanning filters of different type and mode.
158+
When a filter is matched, it generates a :c:macro:`NRF_BLE_SCAN_EVT_FILTER_MATCH` event to the main application.
159+
If the filter matching is enabled and no filter is matched, a :c:macro:`NRF_BLE_SCAN_EVT_NOT_FOUND` event is generated.
160+
161+
The available filter types are:
162+
163+
* **Name** - Filter set to the target name.
164+
The maximum length of the name corresponds to :kconfig:option:`CONFIG_BLE_SCAN_NAME_MAX_LEN`.
165+
The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_NAME_COUNT`.
166+
* **Short name** - Filter set to the short target name.
167+
The maximum length of the name corresponds to :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN`.
168+
The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_SHORT_NAME_COUNT`.
169+
* **Address** - Filter set to the target address.
170+
The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_ADDRESS_COUNT`.
171+
* **UUID** - Filter set to the target UUID.
172+
The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_UUID_COUNT`.
173+
* **Appearance** - Filter set to the target appearance.
174+
The maximum number of filters of this type corresponds to :kconfig:option:`CONFIG_BLE_SCAN_APPEARANCE_COUNT`.
175+
176+
The following two filter modes are available:
177+
178+
* **Normal** - Only one of the filters set, regardless of the type, must be matched to generate an event.
179+
* **Multifilter** - At least one filter from each filter type you set must be matched to generate an event.
180+
For UUID filters, all specified UUIDs must match in this mode.
181+
Enable multifilter by setting the :c:macro:`match_all` argument to true when calling the :c:func:`ble_scan_filters_enable` function.
182+
183+
Multifilter example:
184+
185+
Several filters are set for name, address, UUID, and appearance.
186+
To generate the :c:macro:`NRF_BLE_SCAN_EVT_FILTER_MATCH` event, the following types must match:
187+
188+
* One of the address filters.
189+
* One of the name filters.
190+
* One of the appearance filters.
191+
* All UUID filters.
192+
193+
Otherwise, the :c:macro:`NRF_BLE_SCAN_EVT_NOT_FOUND` event is generated.
194+
195+
You can enable filters by calling the :c:func:`ble_scan_filters_enable` function after initialization.
196+
You can activate filters for one filter type, or for a combination of several filter types.
197+
198+
.. code-block:: c
199+
200+
BLE_SCAN_DEF(ble_scan);
201+
202+
uint8_t addr[BLE_GAP_ADDR_LEN] = {0xa, 0xd, 0xd, 0x4, 0xe, 0x5};
203+
char *device_name = "my_device";
204+
205+
/* See above code snippet for initialization */
206+
207+
/* Enable filter for name and address in normal mode */
208+
nrf_err = ble_scan_filters_enable(&ble_scan, BLE_SCAN_NAME_FILTER | BLE_SCAN_ADDR_FILTER, false);
209+
if (nrf_err) {
210+
LOG_ERR("Failed to enable scan filters, nrf_error %#x", nrf_err);
211+
}
212+
213+
/* Add address to scan filter */
214+
nrf_err = ble_scan_filter_set(&ble_scan, BLE_SCAN_ADDR_FILTER, addr);
215+
if (nrf_err) {
216+
LOG_ERR("Failed to add address scan filter, nrf_error %#x", nrf_err);
217+
}
218+
219+
/* Add name to scan filter */
220+
nrf_err = ble_scan_filter_set(&ble_scan, BLE_SCAN_NAME_FILTER, device_name);
221+
if (nrf_err) {
222+
LOG_ERR("Failed to add name scan filter, nrf_error %#x", nrf_err);
223+
}
224+
225+
/* Start scanning */
226+
nrf_err = ble_scan_start(&ble_scan);
227+
if (nrf_err) {
228+
LOG_ERR("Failed to start scan, nrf_error %#x", nrf_err);
229+
}
230+
231+
/* Start scanning */
232+
ble_scan_stop(&ble_scan);
233+
234+
/* Disable filters */
235+
nrf_err = ble_scan_filters_disable(&ble_scan);
236+
if (nrf_err) {
237+
LOG_ERR("Failed to disable scan filters, nrf_error %#x", nrf_err);
238+
}
239+
240+
/* Remove all scan filters */
241+
nrf_err = ble_scan_all_filter_remove(&ble_scan);
242+
if (nrf_err) {
243+
LOG_ERR("Failed to remove scan filters, nrf_error %#x", nrf_err);
244+
}
245+
246+
Dependencies
247+
************
248+
249+
This library uses the following |BMshort| libraries:
250+
251+
* SoftDevice - :kconfig:option:`CONFIG_SOFTDEVICE`
252+
* SoftDevice handler - :kconfig:option:`CONFIG_NRF_SDH`
253+
254+
API documentation
255+
*****************
256+
257+
| Header file: :file:`include/bm/bluetooth/ble_scan.h`
258+
| Source files: :file:`lib/bluetooth/ble_scan/`
259+
260+
:ref:`Bluetooth LE Scan library API reference <api_ble_scan>`

0 commit comments

Comments
 (0)