From e5a65afa9465a4a4879019205088bfd0762dded3 Mon Sep 17 00:00:00 2001 From: Sondre Pettersen Date: Tue, 7 Oct 2025 13:03:38 +0200 Subject: [PATCH 1/6] lib: scan: add scan library Signed-off-by: Sondre Pettersen Co-authored-by: Asil Zogby --- CODEOWNERS | 1 + include/bm/bluetooth/ble_scan.h | 671 ++++++++++++++ lib/bluetooth/CMakeLists.txt | 1 + lib/bluetooth/Kconfig | 1 + lib/bluetooth/ble_scan/CMakeLists.txt | 9 + lib/bluetooth/ble_scan/Kconfig | 12 + lib/bluetooth/ble_scan/ble_scan.c | 1169 +++++++++++++++++++++++++ 7 files changed, 1864 insertions(+) create mode 100644 include/bm/bluetooth/ble_scan.h create mode 100644 lib/bluetooth/ble_scan/CMakeLists.txt create mode 100644 lib/bluetooth/ble_scan/Kconfig create mode 100644 lib/bluetooth/ble_scan/ble_scan.c diff --git a/CODEOWNERS b/CODEOWNERS index b06f30e3a0..1a5031a01a 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -53,6 +53,7 @@ /lib/bluetooth/ble_qwr/ @nrfconnect/ncs-bm /lib/bluetooth/ble_racp/ @nrfconnect/ncs-bm /lib/bluetooth/ble_radio_notification/ @nrfconnect/ncs-bm +/lib/bluetooth/ble_scan/ @nrfconnect/ncs-bm /lib/bluetooth/peer_manager/ @nrfconnect/ncs-bm /lib/bm_buttons/ @nrfconnect/ncs-bm /lib/bm_timer/ @nrfconnect/ncs-bm diff --git a/include/bm/bluetooth/ble_scan.h b/include/bm/bluetooth/ble_scan.h new file mode 100644 index 0000000000..d4e61433fb --- /dev/null +++ b/include/bm/bluetooth/ble_scan.h @@ -0,0 +1,671 @@ +/* + * Copyright (c) 2018 - 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +/** + * @file + * + * @defgroup nrf_ble_scan Scanning Module + * @{ + * @ingroup ble_sdk_lib + * @brief Module for handling the BLE scanning. + * + * @details The Scanning Module handles the BLE scanning for your application. + * The module offers several criteria for filtering the devices available for connection, + * and it can also work in the simple mode without using the filtering. + * If an event handler is provided, your main application can react to a filter match or to + * the need of setting the whitelist. The module can also be configured to automatically connect + * after it matches a filter or a device from the whitelist. + * + * @note The Scanning Module also supports applications with a multicentral link. + */ + +#ifndef NRF_BLE_SCAN_H__ +#define NRF_BLE_SCAN_H__ + +#include +#include +#include +#include "ble.h" +#include "ble_gap.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// NRF_BLE_SCAN_ENABLED - nrf_ble_scan - Scanning Module +//========================================================== +#ifndef NRF_BLE_SCAN_ENABLED +#define NRF_BLE_SCAN_ENABLED 1 +#endif +// NRF_BLE_SCAN_BUFFER - Data length for an advertising set. +#ifndef NRF_BLE_SCAN_BUFFER +#define NRF_BLE_SCAN_BUFFER 31 +#endif + +// NRF_BLE_SCAN_NAME_MAX_LEN - Maximum size for the name to search in the advertisement report. +#ifndef NRF_BLE_SCAN_NAME_MAX_LEN +#define NRF_BLE_SCAN_NAME_MAX_LEN 32 +#endif + +// NRF_BLE_SCAN_SHORT_NAME_MAX_LEN - Maximum size of the short name to search for in the +// advertisement report. +#ifndef NRF_BLE_SCAN_SHORT_NAME_MAX_LEN +#define NRF_BLE_SCAN_SHORT_NAME_MAX_LEN 32 +#endif + +// NRF_BLE_SCAN_SCAN_INTERVAL - Scanning interval. Determines the scan interval in units of +// 0.625 millisecond. +#ifndef NRF_BLE_SCAN_SCAN_INTERVAL +#define NRF_BLE_SCAN_SCAN_INTERVAL 160 +#endif + +// NRF_BLE_SCAN_SCAN_DURATION - Duration of a scanning session in units of 10 ms. Range: 0x0001 +// - 0xFFFF (10 ms to 10.9225 ms). If set to 0x0000, the scanning continues until it is explicitly +// disabled. +#ifndef NRF_BLE_SCAN_SCAN_DURATION +#define NRF_BLE_SCAN_SCAN_DURATION 0 +#endif + +// NRF_BLE_SCAN_SCAN_WINDOW - Scanning window. Determines the scanning window in units of 0.625 +// millisecond. +#ifndef NRF_BLE_SCAN_SCAN_WINDOW +#define NRF_BLE_SCAN_SCAN_WINDOW 80 +#endif + +// NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL - Determines minimum connection interval in +// milliseconds. +#ifndef NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL +#define NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL 7.5 +#endif + +// NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL - Determines maximum connection interval in +// milliseconds. +#ifndef NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL +#define NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL 30 +#endif + +// NRF_BLE_SCAN_SLAVE_LATENCY - Determines the slave latency in counts of connection events. +#ifndef NRF_BLE_SCAN_SLAVE_LATENCY +#define NRF_BLE_SCAN_SLAVE_LATENCY 0 +#endif + +// NRF_BLE_SCAN_SUPERVISION_TIMEOUT - Determines the supervision time-out in units of 10 +// millisecond. +#ifndef NRF_BLE_SCAN_SUPERVISION_TIMEOUT +#define NRF_BLE_SCAN_SUPERVISION_TIMEOUT 4000 +#endif + +#ifndef NRF_BLE_SCAN_SCAN_PHY +#define NRF_BLE_SCAN_SCAN_PHY 0 +#endif + +// NRF_BLE_SCAN_FILTER_ENABLE - Enabling filters for the Scanning Module. +//========================================================== +#ifndef NRF_BLE_SCAN_FILTER_ENABLE +#define NRF_BLE_SCAN_FILTER_ENABLE 1 +#endif +// NRF_BLE_SCAN_UUID_CNT - Number of filters for UUIDs. +#ifndef NRF_BLE_SCAN_UUID_CNT +#define NRF_BLE_SCAN_UUID_CNT 0 +#endif + +// NRF_BLE_SCAN_NAME_CNT - Number of name filters. +#ifndef NRF_BLE_SCAN_NAME_CNT +#define NRF_BLE_SCAN_NAME_CNT 1 +#endif + +// NRF_BLE_SCAN_SHORT_NAME_CNT - Number of short name filters. +#ifndef NRF_BLE_SCAN_SHORT_NAME_CNT +#define NRF_BLE_SCAN_SHORT_NAME_CNT 0 +#endif + +// NRF_BLE_SCAN_ADDRESS_CNT - Number of address filters. +#ifndef NRF_BLE_SCAN_ADDRESS_CNT +#define NRF_BLE_SCAN_ADDRESS_CNT 0 +#endif + +// NRF_BLE_SCAN_APPEARANCE_CNT - Number of appearance filters. +#ifndef NRF_BLE_SCAN_APPEARANCE_CNT +#define NRF_BLE_SCAN_APPEARANCE_CNT 0 +#endif + +/** + * @defgroup NRF_BLE_SCAN_FILTER_MODE Filter modes + * @{ */ +#define NRF_BLE_SCAN_NAME_FILTER (0x01) /** Filters the device name. */ +#define NRF_BLE_SCAN_ADDR_FILTER (0x02) /** Filters the device address. */ +#define NRF_BLE_SCAN_UUID_FILTER (0x04) /** Filters the UUID. */ +#define NRF_BLE_SCAN_APPEARANCE_FILTER (0x08) /** Filters the appearance. */ +#define NRF_BLE_SCAN_SHORT_NAME_FILTER (0x10) /** Filters the device short name. */ +#define NRF_BLE_SCAN_ALL_FILTER (0x1F) /** Uses the combination of all filters. */ +/* @} */ + +/** + * @brief Macro for defining a nrf_ble_scan instance. + * + * @param _name Name of the instance. + * @hideinitializer + */ +#define NRF_BLE_SCAN_DEF(_name) \ + static struct nrf_ble_scan _name; \ + NRF_SDH_BLE_OBSERVER(_name##_ble_obs, nrf_ble_scan_on_ble_evt, &_name, \ + NRF_BLE_SCAN_OBSERVER_PRIO); + +/** + * @brief Enumeration for scanning events. + * + * @details These events are propagated to the main application if a handler is provided during + * the initialization of the Scanning Module. @ref NRF_BLE_SCAN_EVT_WHITELIST_REQUEST + * cannot be ignored if whitelist is used. + */ +enum nrf_ble_scan_evt { + /** A filter is matched or all filters are matched in the multifilter mode. */ + NRF_BLE_SCAN_EVT_FILTER_MATCH, + /** Request the whitelist from the main application. + * For whitelist scanning to work, the whitelist must be + * set when this event occurs. + */ + NRF_BLE_SCAN_EVT_WHITELIST_REQUEST, + /** Send notification to the main application when a + *device from the whitelist is found. + */ + NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT, + /** The filter was not matched for the scan data. */ + NRF_BLE_SCAN_EVT_NOT_FOUND, + /** Scan timeout. */ + NRF_BLE_SCAN_EVT_SCAN_TIMEOUT, + /** Error occurred when establishing the connection. In + *this event, an error is passed from the function call + *@ref sd_ble_gap_connect. + */ + NRF_BLE_SCAN_EVT_CONNECTING_ERROR, + /** Connected to device. */ + NRF_BLE_SCAN_EVT_CONNECTED, + NRF_BLE_SCAN_EVT_ERROR, +}; + +/** + *@brief Types of filters. + */ +enum nrf_ble_scan_filter_type { + /** Filter for names. */ + SCAN_NAME_FILTER, + /** Filter for short names. */ + SCAN_SHORT_NAME_FILTER, + /** Filter for addresses. */ + SCAN_ADDR_FILTER, + /** Filter for UUIDs. */ + SCAN_UUID_FILTER, + /** Filter for appearances. */ + SCAN_APPEARANCE_FILTER, +}; + +struct nrf_ble_scan_short_name { + /** Pointer to the short name. */ + char const *p_short_name; + /** Minimum length of the short name. */ + uint8_t short_name_min_len; +}; + +/** + * @brief Structure for Scanning Module initialization. + */ +struct nrf_ble_scan_init { + /** BLE GAP scan parameters required to initialize the module. Can + * be initialized as NULL. If NULL, the parameters required to + * initialize the module are loaded from the static configuration. + */ + ble_gap_scan_params_t const *p_scan_param; + /** If set to true, the module automatically connects after a filter + * match or successful identification of a device from the whitelist. + */ + bool connect_if_match; + /** Connection parameters. Can be initialized as NULL. If NULL, the + * default static configuration is used. + */ + ble_gap_conn_params_t const *p_conn_param; + /** Variable to keep track of what connection settings will be used + * if a filer match or a whitelist match results in a connection. + */ + uint8_t conn_cfg_tag; +}; + +/** + * @brief Structure for setting the filter status. + * + * @details This structure is used for sending filter status to the main application. + */ +struct nrf_ble_scan_filter_match { + /** Set to 1 if name filter is matched. */ + uint8_t name_filter_match: 1; + /** Set to 1 if address filter is matched. */ + uint8_t address_filter_match: 1; + /** Set to 1 if uuid filter is matched. */ + uint8_t uuid_filter_match: 1; + /** Set to 1 if appearance filter is matched. */ + uint8_t appearance_filter_match: 1; + /** Set to 1 if short name filter is matched. */ + uint8_t short_name_filter_match: 1; +}; + +/** + * @brief Event structure for @ref NRF_BLE_SCAN_EVT_FILTER_MATCH. + */ +struct nrf_ble_scan_evt_filter_match { + /** Event structure for @ref BLE_GAP_EVT_ADV_REPORT. This data + * allows the main application to establish connection. + */ + ble_gap_evt_adv_report_t const *p_adv_report; + /** Matching filters. Information about matched filters. */ + struct nrf_ble_scan_filter_match filter_match; +}; + +/** + * @brief Event structure for @ref NRF_BLE_SCAN_EVT_CONNECTING_ERROR. + */ +struct nrf_ble_scan_evt_connecting_err { + /** Indicates success or failure of an API procedure. In case of + * failure, a comprehensive error code indicating the cause or reason + * for failure is provided. + */ + int err_code; +}; + +/** + * @brief Event structure for @ref NRF_BLE_SCAN_EVT_CONNECTED. + */ +struct nrf_ble_scan_evt_connected { + /** Connected event parameters. */ + ble_gap_evt_connected_t const *p_connected; + /** Connection handle of the device on which the event occurred. */ + uint16_t conn_handle; +}; + +/** + * @brief Structure for Scanning Module event data. + * + * @details This structure is used to send module event data to the main application when an event + * occurs. + */ +struct scan_evt { + /** Type of event propagated to the main application. */ + enum nrf_ble_scan_evt scan_evt_id; + union { + /** Scan filter match. */ + struct nrf_ble_scan_evt_filter_match filter_match; + /** Timeout event parameters. */ + ble_gap_evt_timeout_t timeout; + /** Advertising report event parameters for whitelist. */ + ble_gap_evt_adv_report_t const *p_whitelist_adv_report; + /** Advertising report event parameters when filter is not found. */ + ble_gap_evt_adv_report_t const *p_not_found; + /** Connected event parameters. */ + struct nrf_ble_scan_evt_connected connected; + /** Error event when connecting. Propagates the error code returned by the + SoftDevice API @ref sd_ble_gap_scan_start. */ + struct nrf_ble_scan_evt_connecting_err connecting_err; + struct { + uint32_t reason; + } error; + } params; + /** GAP scanning parameters. These parameters + * are needed to establish connection. + */ + ble_gap_scan_params_t const *p_scan_params; +}; + +/** + * @brief BLE scanning event handler type. + */ +typedef void (*nrf_ble_scan_evt_handler_t)(struct scan_evt const *p_scan_evt); + +#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 + +#if (NRF_BLE_SCAN_NAME_CNT > 0) +struct nrf_ble_scan_name_filter { + /** Names that the main application will scan + * for, and that will be advertised by the + * peripherals. + */ + char target_name[NRF_BLE_SCAN_NAME_CNT][NRF_BLE_SCAN_NAME_MAX_LEN]; + /** Name filter counter. */ + uint8_t name_cnt; + /** Flag to inform about enabling or disabling this filter. */ + bool name_filter_enabled; +}; +#endif + +#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) +typedef struct { + struct { + /** Short names that the + * main application will + * scan for, and that will + * be advertised by the + * peripherals. + */ + char short_target_name[NRF_BLE_SCAN_SHORT_NAME_MAX_LEN]; + /** Minimum length of the short name. */ + uint8_t short_name_min_len; + } short_name[NRF_BLE_SCAN_SHORT_NAME_CNT]; + /** Short name filter counter. */ + uint8_t name_cnt; + /** Flag to inform about enabling or disabling this filter. */ + bool short_name_filter_enabled; +} nrf_ble_scan_short_name_filter_t; +#endif + +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) +struct nrf_ble_scan_addr_filter { + struct ble_gap_addr + /** Addresses in the same format as the + format used by the SoftDevice that the + main application will scan for, and that + will be advertised by the peripherals. */ + target_addr[NRF_BLE_SCAN_ADDRESS_CNT]; + /** Address filter counter. */ + uint8_t addr_cnt; + /** Flag to inform about enabling or disabling this filter. */ + bool addr_filter_enabled; +}; +#endif + +#if (NRF_BLE_SCAN_UUID_CNT > 0) +struct nrf_ble_scan_uuid_filter { + struct ble_uuid + /** UUIDs that the main application will scan for, and + that will be advertised by the peripherals. */ + uuid[NRF_BLE_SCAN_UUID_CNT]; + /** UUID filter counter. */ + uint8_t uuid_cnt; + /** Flag to inform about enabling or disabling this filter. */ + bool uuid_filter_enabled; +}; +#endif + +#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) +typedef struct { + /** Apperances that the main application + will scan for, and that will be + advertised by the peripherals. */ + uint16_t appearance[NRF_BLE_SCAN_APPEARANCE_CNT]; + /** Appearance filter counter. */ + uint8_t appearance_cnt; + /** Flag to inform about enabling or disabling this filter.*/ + bool appearance_filter_enabled; +} nrf_ble_scan_appearance_filter_t; +#endif + +/** + * @brief Filters data. + * + * @details This structure contains all filter data and the information about enabling and disabling + * any type of filters. Flag all_filter_mode informs about the filter mode. If this flag is set, + * then all types of enabled filters must be matched for the module to send a notification to the + * main application. Otherwise, it is enough to match one of filters to send notification. + */ +struct nrf_ble_scan_filters { +#if (NRF_BLE_SCAN_NAME_CNT > 0) + /** Name filter data. */ + struct nrf_ble_scan_name_filter name_filter; +#endif +#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) + /** Short name filter data. */ + struct nrf_ble_scan_short_name_filter short_name_filter; +#endif +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) + /** Address filter data. */ + struct nrf_ble_scan_addr_filter addr_filter; +#endif +#if (NRF_BLE_SCAN_UUID_CNT > 0) + /** UUID filter data. */ + struct nrf_ble_scan_uuid_filter uuid_filter; +#endif +#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) + /** Appearance filter data. */ + nrf_ble_scan_appearance_filter_t appearance_filter; +#endif + /** Filter mode. If true, all set filters must be matched to + generate an event.*/ + bool all_filters_mode; +}; + +#endif // NRF_BLE_SCAN_FILTER_ENABLE + +#ifndef NRF_BLE_SCAN_BUFFER +#define NRF_BLE_SCAN_BUFFER 31 +#endif + +/** + * @brief Scan module instance. Options for the different scanning modes. + * + * @details This structure stores all module settings. It is used to enable or disable scanning + * modes and to configure filters. + */ +struct nrf_ble_scan { +#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 + /** Filter data. */ + struct nrf_ble_scan_filters scan_filters; +#endif + /** If set to true, the module automatically connects after a filter + match or successful identification of a device from the whitelist.*/ + bool connect_if_match; + /** Connection parameters. */ + ble_gap_conn_params_t conn_params; + /** Variable to keep track of what connection settings will be used + if a filer match or a whitelist match results in a connection. */ + uint8_t conn_cfg_tag; + /** GAP scanning parameters. */ + ble_gap_scan_params_t scan_params; + /** Handler for the scanning events. Can be initialized as NULL if no + handling is implemented in the main application. */ + nrf_ble_scan_evt_handler_t evt_handler; + /** Buffer where advertising reports will be + stored by the SoftDevice. */ + uint8_t scan_buffer_data[NRF_BLE_SCAN_BUFFER]; + /** Structure-stored pointer to the buffer where advertising + reports will be stored by the SoftDevice. */ + ble_data_t scan_buffer; +}; + +/** + * @brief Function for indicating that the Scanning Module is using the whitelist. + * + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @return Whether the whitelist is used. + */ +bool is_whitelist_used(struct nrf_ble_scan const *const p_scan_ctx); + +/** + * @brief Function for initializing the Scanning Module. + * + * @param[out] p_scan_ctx Pointer to the Scanning Module instance. This structure must be supplied + * by the application. It is initialized by this function and is later used to identify this + * particular module instance. + * + * @param[in] p_init Can be initialized as NULL. If NULL, the parameters required to + * initialize the module are loaded from static configuration. If module is to establish the + * connection automatically, this must be initialized with the relevant data. + * @param[in] evt_handler Handler for the scanning events. + * Can be initialized as NULL if no handling is implemented in the main + * application. + * + * @retval NRF_SUCCESS If initialization was successful. + * @retval NRF_ERROR_NULL When the NULL pointer is passed as input. + */ +int nrf_ble_scan_init(struct nrf_ble_scan *const p_scan_ctx, + struct nrf_ble_scan_init const *const p_init, + nrf_ble_scan_evt_handler_t evt_handler); + +/** + * @brief Function for starting scanning. + * + * @details This function starts the scanning according to the configuration set during the + * initialization. + * + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If scanning started. Otherwise, an error code is returned. + * @retval NRF_ERROR_NULL If NULL pointer is passed as input. + * + * @return This API propagates the error code returned by the + * SoftDevice API @ref sd_ble_gap_scan_start. + */ +int nrf_ble_scan_start(struct nrf_ble_scan const *const p_scan_ctx); + +/** + * @brief Function for stopping scanning. + */ +void nrf_ble_scan_stop(void); + +#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 + +/** + * @brief Function for enabling filtering. + * + * @details The filters can be combined with each other. For example, you can enable one filter or + * several filters. For example, (NRF_BLE_SCAN_NAME_FILTER | NRF_BLE_SCAN_UUID_FILTER) enables UUID + * and name filters. + * + * @param[in] mode Filter mode: @ref NRF_BLE_SCAN_FILTER_MODE. + * @param[in] match_all If this flag is set, all types of enabled filters must be + * matched before generating @ref NRF_BLE_SCAN_EVT_FILTER_MATCH to the main application. Otherwise, + * it is enough to match one filter to trigger the filter match event. + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If the filters are enabled successfully. + * @retval NRF_ERROR_INVALID_PARAM If the filter mode is incorrect. Available filter modes: @ref + * NRF_BLE_SCAN_FILTER_MODE. + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + */ +int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const p_scan_ctx, uint8_t mode, + bool match_all); + +/** + * @brief Function for disabling filtering. + * + * @details This function disables all filters. + * Even if the automatic connection establishing is enabled, + * the connection will not be established with the first + device found after this function is called. + * + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If filters are disabled successfully. + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + */ +int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const p_scan_ctx); + +/** + * @brief Function for getting filter status. + * + * @details This function returns the filter setting and whether it is enabled or disabled. + + * @param[out] p_status Filter status. + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If filter status is returned. + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + */ +int nrf_ble_scan_filter_get(struct nrf_ble_scan *const p_scan_ctx, + struct nrf_ble_scan_filters *p_status); + +/** + * @brief Function for adding any type of filter to the scanning. + * + * @details This function adds a new filter by type @ref nrf_ble_scan_filter_type_t. + * The filter will be added if the number of filters of a given type does not exceed @ref + * NRF_BLE_SCAN_UUID_CNT, + * @ref NRF_BLE_SCAN_NAME_CNT, @ref NRF_BLE_SCAN_ADDRESS_CNT, or @ref + * NRF_BLE_SCAN_APPEARANCE_CNT, depending on the filter type, and if the same filter has not already + * been set. + * + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] type Filter type. + * @param[in] p_data The filter data to add. + * + * @retval NRF_SUCCESS If the filter is added successfully. + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + * @retval NRF_ERROR_DATA_SIZE If the name filter length is too long. Maximum name filter + * length corresponds to @ref NRF_BLE_SCAN_NAME_MAX_LEN. + * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. + * @retval NRF_ERROR_INVALID_PARAM If the filter type is incorrect. Available filter types: + * @ref nrf_ble_scan_filter_type_t. + * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address type is invalid. + */ +int nrf_ble_scan_filter_set(struct nrf_ble_scan *const p_scan_ctx, + enum nrf_ble_scan_filter_type type, void const *p_data); + +/** + * @brief Function for removing all set filters. + * + * @details The function removes all previously set filters. + * + * @note After using this function the filters are still enabled. + * + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If all filters are removed successfully. + */ +int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const p_scan_ctx); + +#endif // CONFIG_BT_SCAN_FILTER_ENABLE + +/** + * @brief Function for changing the scanning parameters. + * + **@details Use this function to change scanning parameters. During the parameter change + * the scan is stopped. To resume scanning, use @ref nrf_ble_scan_start. + * Scanning parameters can be set to NULL. If so, the default static configuration + * is used. For example, use this function when the @ref + *NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT event is generated. The generation of this event means that + *there is a risk that the whitelist is empty. In such case, this function can change the scanning + *parameters, so that the whitelist is not used, and you avoid the error caused by scanning with the + *whitelist when there are no devices on the whitelist. + * + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] p_scan_param GAP scanning parameters. Can be initialized as NULL. + * + * @retval NRF_SUCCESS If parameters are changed successfully. + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + */ +int nrf_ble_scan_params_set(struct nrf_ble_scan *const p_scan_ctx, + ble_gap_scan_params_t const *p_scan_param); + +/** + * @brief Function for handling the BLE stack events of the application. + * + * @param[in] p_ble_evt Pointer to the BLE event received. + * @param[in,out] p_scan Pointer to the Scanning Module instance. + */ +void nrf_ble_scan_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_scan); + +/** + * @brief Function for converting the raw address to the SoftDevice GAP address. + * + * @details This function inverts the byte order in the address. If you enter the address as it is + * displayed (for example, on a phone screen from left to right), you must use this function to + * convert the address to the SoftDevice address type. + * + * @note This function does not decode an address type. + * + * @param[out] p_gap_addr The Bluetooth Low Energy address. + * @param[in] addr Address to be converted to the SoftDevice address. + * + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + * @retval NRF_SUCCESS If the address is copied and converted successfully. + */ +int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *p_gap_addr, + uint8_t const addr[BLE_GAP_ADDR_LEN]); + +#ifdef __cplusplus +} +#endif + +#endif // NRF_BLE_SCAN_H__ + +/** @} */ \ No newline at end of file diff --git a/lib/bluetooth/CMakeLists.txt b/lib/bluetooth/CMakeLists.txt index a29e625f37..37fd22b8fc 100644 --- a/lib/bluetooth/CMakeLists.txt +++ b/lib/bluetooth/CMakeLists.txt @@ -10,5 +10,6 @@ add_subdirectory_ifdef(CONFIG_BLE_CONN_STATE ble_conn_state) add_subdirectory_ifdef(CONFIG_BLE_GATT_QUEUE ble_gq) add_subdirectory_ifdef(CONFIG_BLE_RACP ble_racp) add_subdirectory_ifdef(CONFIG_BLE_RADIO_NOTIFICATION ble_radio_notification) +add_subdirectory_ifdef(CONFIG_BLE_SCAN ble_scan) add_subdirectory_ifdef(CONFIG_BLE_QWR ble_qwr) add_subdirectory_ifdef(CONFIG_PEER_MANAGER peer_manager) diff --git a/lib/bluetooth/Kconfig b/lib/bluetooth/Kconfig index 4e7e7007f6..7976ac8988 100644 --- a/lib/bluetooth/Kconfig +++ b/lib/bluetooth/Kconfig @@ -11,6 +11,7 @@ rsource "ble_conn_state/Kconfig" rsource "ble_gq/Kconfig" rsource "ble_racp/Kconfig" rsource "ble_radio_notification/Kconfig" +rsource "ble_scan/Kconfig" rsource "ble_qwr/Kconfig" rsource "peer_manager/Kconfig" diff --git a/lib/bluetooth/ble_scan/CMakeLists.txt b/lib/bluetooth/ble_scan/CMakeLists.txt new file mode 100644 index 0000000000..a61bfd550e --- /dev/null +++ b/lib/bluetooth/ble_scan/CMakeLists.txt @@ -0,0 +1,9 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +zephyr_library() +zephyr_library_sources( + ble_scan.c +) diff --git a/lib/bluetooth/ble_scan/Kconfig b/lib/bluetooth/ble_scan/Kconfig new file mode 100644 index 0000000000..65338596fe --- /dev/null +++ b/lib/bluetooth/ble_scan/Kconfig @@ -0,0 +1,12 @@ +# +# Copyright (c) 2025 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +menuconfig BLE_SCAN + bool "BLE scan" + depends on SOFTDEVICE + select EXPERIMENTAL + help + scan. diff --git a/lib/bluetooth/ble_scan/ble_scan.c b/lib/bluetooth/ble_scan/ble_scan.c new file mode 100644 index 0000000000..59d2f25144 --- /dev/null +++ b/lib/bluetooth/ble_scan/ble_scan.c @@ -0,0 +1,1169 @@ +/** + * Copyright (c) 2018 - 2021, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +// #include "sdk_common.h" + +#include +#include + +// #include "nrf_ble_scan.h" + +#include + +#include +// #include "app_error.h" +// #include "nrf_assert.h" +// #include "sdk_macros.h" +// #include "ble_advdata.h" + +#include +LOG_MODULE_REGISTER(ble_scan, 3); + +// NRF_BLE_SCAN_ENABLED - nrf_ble_scan - Scanning Module +//========================================================== +#ifndef NRF_BLE_SCAN_ENABLED +#define NRF_BLE_SCAN_ENABLED 1 +#endif +// NRF_BLE_SCAN_BUFFER - Data length for an advertising set. +#ifndef NRF_BLE_SCAN_BUFFER +#define NRF_BLE_SCAN_BUFFER 31 +#endif + +// NRF_BLE_SCAN_NAME_MAX_LEN - Maximum size for the name to search in the advertisement report. +#ifndef NRF_BLE_SCAN_NAME_MAX_LEN +#define NRF_BLE_SCAN_NAME_MAX_LEN 32 +#endif + +// NRF_BLE_SCAN_SHORT_NAME_MAX_LEN - Maximum size of the short name to search for in the +// advertisement report. +#ifndef NRF_BLE_SCAN_SHORT_NAME_MAX_LEN +#define NRF_BLE_SCAN_SHORT_NAME_MAX_LEN 32 +#endif + +// NRF_BLE_SCAN_SCAN_INTERVAL - Scanning interval. Determines the scan interval in units of +// 0.625 millisecond. +#ifndef NRF_BLE_SCAN_SCAN_INTERVAL +#define NRF_BLE_SCAN_SCAN_INTERVAL 160 +#endif + +// NRF_BLE_SCAN_SCAN_DURATION - Duration of a scanning session in units of 10 ms. Range: 0x0001 +// - 0xFFFF (10 ms to 10.9225 ms). If set to 0x0000, the scanning continues until it is explicitly +// disabled. +#ifndef NRF_BLE_SCAN_SCAN_DURATION +#define NRF_BLE_SCAN_SCAN_DURATION 0 +#endif + +// NRF_BLE_SCAN_SCAN_WINDOW - Scanning window. Determines the scanning window in units of 0.625 +// millisecond. +#ifndef NRF_BLE_SCAN_SCAN_WINDOW +#define NRF_BLE_SCAN_SCAN_WINDOW 80 +#endif + +// NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL - Determines minimum connection interval in +// milliseconds. +#ifndef NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL +#define NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL 100 +#endif + +// NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL - Determines maximum connection interval in +// milliseconds. +#ifndef NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL +#define NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL 500 +#endif + +// NRF_BLE_SCAN_SLAVE_LATENCY - Determines the slave latency in counts of connection events. +#ifndef NRF_BLE_SCAN_SLAVE_LATENCY +#define NRF_BLE_SCAN_SLAVE_LATENCY 5 +#endif + +// NRF_BLE_SCAN_SUPERVISION_TIMEOUT - Determines the supervision time-out in units of 10 +// millisecond. +#ifndef NRF_BLE_SCAN_SUPERVISION_TIMEOUT +#define NRF_BLE_SCAN_SUPERVISION_TIMEOUT 6200 +#endif + +/**@brief Function for establishing the connection with a device. + * + * @details Connection is established if @ref NRF_BLE_SCAN_EVT_FILTER_MATCH + * or @ref NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT occurs and the module was + * initialized in the automatic connection mode. This function can generate an event + * to the main application when @ref sd_ble_gap_connect is used inside the function and it + * returns value that is different than @ref NRF_SUCCESS. + * + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] p_adv_report Advertising data. + */ +static void nrf_ble_scan_connect_with_target(struct nrf_ble_scan const *const p_scan_ctx, + ble_gap_evt_adv_report_t const *const p_adv_report) +{ + int err_code; + struct scan_evt scan_evt; + + // For readability. + ble_gap_addr_t const *p_addr = &p_adv_report->peer_addr; + ble_gap_scan_params_t const *p_scan_params = &p_scan_ctx->scan_params; + ble_gap_conn_params_t const *p_conn_params = &p_scan_ctx->conn_params; + uint8_t con_cfg_tag = p_scan_ctx->conn_cfg_tag; + + // Return if the automatic connection is disabled. + if (!p_scan_ctx->connect_if_match) { + return; + } + + // Stop scanning. + nrf_ble_scan_stop(); + + memset(&scan_evt, 0, sizeof(scan_evt)); + + // Establish connection. + err_code = sd_ble_gap_connect(p_addr, p_scan_params, p_conn_params, con_cfg_tag); + if (err_code != NRF_SUCCESS) { + // TODO: Trigger event handler with error + } + + LOG_DBG("Connecting"); + + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTING_ERROR; + scan_evt.params.connecting_err.err_code = err_code; + + LOG_DBG("Connection status: %d", err_code); + + // If an error occurred, send an event to the event handler. + if ((err_code != NRF_SUCCESS) && (p_scan_ctx->evt_handler != NULL)) { + p_scan_ctx->evt_handler(&scan_evt); + } +} + +#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) + +/**@brief Function for searching for the provided address in the advertisement packets. + * + * @details Use this function to parse the received advertising data for the provided address. + * + * + * @param[in] p_adv_report Advertising data to parse. + * @param[in] p_addr Address to search for. The address length must correspond to @ref + * BLE_GAP_ADDR_LEN. + * + * @return True if the provided address was found, false otherwise. + */ +static bool find_peer_addr(ble_gap_evt_adv_report_t const *const p_adv_report, + ble_gap_addr_t const *p_addr) +{ + // Compare addresses. + if (memcmp(p_addr->addr, p_adv_report->peer_addr.addr, + sizeof(p_adv_report->peer_addr.addr)) == 0) { + return true; + } + + return false; +} + +/** @brief Function for comparing the provided address with the addresses of the advertising + * devices. + * + * @param[in] p_adv_report Advertising data to parse. + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval True when the address matches with the addresses of the advertising devices. False + * otherwise. + */ +static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const p_adv_report, + struct nrf_ble_scan const *const p_scan_ctx) +{ + ble_gap_addr_t const *p_addr = p_scan_ctx->scan_filters.addr_filter.target_addr; + uint8_t counter = p_scan_ctx->scan_filters.addr_filter.addr_cnt; + + for (uint8_t index = 0; index < counter; index++) { + /* Search for address. */ + if (find_peer_addr(p_adv_report, &p_addr[index])) { + return true; + } + } + + return false; +} + +/**@brief Function for adding target address to the scanning filter. + * + * @param[in] p_addr Target address in the format required by the SoftDevice. If you + * need to convert the address, use @ref nrf_ble_scan_copy_addr_to_sd_gap_addr. The address length + * must correspond to @ref BLE_GAP_ADDR_LEN. + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a + * filter that was already added before. + * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. + * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address type is invalid. + */ +static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const p_scan_ctx, + uint8_t const *p_addr) +{ + ble_gap_addr_t *p_addr_filter = p_scan_ctx->scan_filters.addr_filter.target_addr; + uint8_t *p_counter = &p_scan_ctx->scan_filters.addr_filter.addr_cnt; + uint8_t index; + + /* If no memory for filter. */ + if (*p_counter >= NRF_BLE_SCAN_ADDRESS_CNT) { + return NRF_ERROR_NO_MEM; + } + + // Check for duplicated filter. + for (index = 0; index < NRF_BLE_SCAN_ADDRESS_CNT; index++) { + if (!memcmp(p_addr_filter[index].addr, p_addr, BLE_GAP_ADDR_LEN)) { + return NRF_SUCCESS; + } + } + + for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; i++) { + p_addr_filter[*p_counter].addr[i] = p_addr[i]; + } + + // Address type is not used so set it to 0. + p_addr_filter[*p_counter].addr_type = 0; + + LOG_DBG("Filter set on address 0x"); + NRF_LOG_HEXDUMP_DEBUG(p_addr_filter[*p_counter].addr, BLE_GAP_ADDR_LEN); + + // Increase the address filter counter. + *p_counter += 1; + + return NRF_SUCCESS; +} + +#endif // NRF_BLE_SCAN_ADDRESS_CNT + +#if (NRF_BLE_SCAN_NAME_CNT > 0) +/** @brief Function for comparing the provided name with the advertised name. + * + * @param[in] p_adv_report Advertising data to parse. + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval True when the names match. False otherwise. + */ +static bool adv_name_compare(ble_gap_evt_adv_report_t const *p_adv_report, + struct nrf_ble_scan const *const p_scan_ctx) +{ + struct nrf_ble_scan_name_filter const *p_name_filter = + &p_scan_ctx->scan_filters.name_filter; + uint8_t counter = p_scan_ctx->scan_filters.name_filter.name_cnt; + uint8_t index; + uint16_t data_len; + + data_len = p_adv_report->data.len; + + // Compare the name found with the name filter. + for (index = 0; index < counter; index++) { + if (ble_advdata_name_find(p_adv_report->data.p_data, data_len, + p_name_filter->target_name[index])) { + return true; + } + } + + return false; +} + +/**@brief Function for adding name of the peripheral to the scanning filter. + * + * @param[in] p_name Peripheral name. + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a + * filter that was already added before. + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + * @retval NRF_ERROR_DATA_SIZE If the name filter length is too long. The maximum filter name + * length corresponds to @ref NRF_BLE_SCAN_NAME_MAX_LEN. + * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. + */ +static int nrf_ble_scan_name_filter_add(struct nrf_ble_scan *const p_scan_ctx, char const *p_name) +{ + uint8_t index; + uint8_t *counter = &p_scan_ctx->scan_filters.name_filter.name_cnt; + uint8_t name_len = strlen(p_name); + + // Check the name length. + if ((name_len == 0) || (name_len > NRF_BLE_SCAN_NAME_MAX_LEN)) { + return NRF_ERROR_DATA_SIZE; + } + + // If no memory for filter. + if (*counter >= NRF_BLE_SCAN_NAME_CNT) { + return NRF_ERROR_NO_MEM; + } + + // Check for duplicated filter. + for (index = 0; index < NRF_BLE_SCAN_NAME_CNT; index++) { + if (!strcmp(p_scan_ctx->scan_filters.name_filter.target_name[index], p_name)) { + return NRF_SUCCESS; + } + } + + // Add name to filter. + memcpy(p_scan_ctx->scan_filters.name_filter.target_name[(*counter)++], p_name, + strlen(p_name)); + + LOG_DBG("Adding filter on %s name", p_name); + + return NRF_SUCCESS; +} + +#endif // NRF_BLE_SCAN_NAME_CNT + +#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) +/** @brief Function for comparing the provided short name with the advertised short name. + * + * @param[in] p_adv_report Advertising data to parse. + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval True when the names match. False otherwise. + */ +static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const p_adv_report, + struct nrf_ble_scan const *const p_scan_ctx) +{ + nrf_ble_scan_short_name_filter_t const *p_name_filter = + &p_scan_ctx->scan_filters.short_name_filter; + uint8_t counter = p_scan_ctx->scan_filters.short_name_filter.name_cnt; + uint8_t index; + uint16_t data_len; + + data_len = p_adv_report->data.len; + + // Compare the name found with the name filters. + for (index = 0; index < counter; index++) { + if (ble_advdata_short_name_find( + p_adv_report->data.p_data, data_len, + p_name_filter->short_name[index].short_target_name, + p_name_filter->short_name[index].short_name_min_len)) { + return true; + } + } + + return false; +} + +/**@brief Function for adding the short name of the peripheral to the scanning filter. + * + * @param[in] p_short_name Short name of the peripheral. + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a + * filter that was already added before. + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + * @retval NRF_ERROR_DATA_SIZE If the name filter length is too long. The maximum filter name + * length corresponds to @ref NRF_BLE_SCAN_SHORT_NAME_MAX_LEN. + * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. + */ +static int nrf_ble_scan_short_name_filter_add(struct nrf_ble_scan *const p_scan_ctx, + nrf_ble_scan_short_name_t const *p_short_name) +{ + uint8_t index; + uint8_t *p_counter = &p_scan_ctx->scan_filters.short_name_filter.name_cnt; + nrf_ble_scan_short_name_filter_t *p_short_name_filter = + &p_scan_ctx->scan_filters.short_name_filter; + uint8_t name_len = strlen(p_short_name->p_short_name); + + // Check the name length. + if ((name_len == 0) || (name_len > NRF_BLE_SCAN_SHORT_NAME_MAX_LEN)) { + return NRF_ERROR_DATA_SIZE; + } + + // If no memory for filter. + if (*p_counter >= NRF_BLE_SCAN_SHORT_NAME_CNT) { + return NRF_ERROR_NO_MEM; + } + + // Check for duplicated filter. + for (index = 0; index < NRF_BLE_SCAN_SHORT_NAME_CNT; index++) { + if (!strcmp(p_short_name_filter->short_name[index].short_target_name, + p_short_name->p_short_name)) { + return NRF_SUCCESS; + } + } + + // Add name to the filter. + p_short_name_filter->short_name[(*p_counter)].short_name_min_len = + p_short_name->short_name_min_len; + memcpy(p_short_name_filter->short_name[(*p_counter)++].short_target_name, + p_short_name->p_short_name, strlen(p_short_name->p_short_name)); + + LOG_DBG("Adding filter on %s name", p_short_name->p_short_name); + + return NRF_SUCCESS; +} + +#endif + +#if (NRF_BLE_SCAN_UUID_CNT > 0) +/**@brief Function for comparing the provided UUID with the UUID in the advertisement packets. + * + * @param[in] p_adv_report Advertising data to parse. + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * + * @return True if the UUIDs match. False otherwise. + */ +static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const p_adv_report, + struct nrf_ble_scan const *const p_scan_ctx) +{ + nrf_ble_scan_uuid_filter_t const *p_uuid_filter = &p_scan_ctx->scan_filters.uuid_filter; + bool const all_filters_mode = p_scan_ctx->scan_filters.all_filters_mode; + uint8_t const counter = p_scan_ctx->scan_filters.uuid_filter.uuid_cnt; + uint8_t index; + uint16_t data_len; + uint8_t uuid_match_cnt = 0; + + data_len = p_adv_report->data.len; + + for (index = 0; index < counter; index++) { + + if (ble_advdata_uuid_find(p_adv_report->data.p_data, data_len, + &p_uuid_filter->uuid[index])) { + uuid_match_cnt++; + + // In the normal filter mode, only one UUID is needed to match. + if (!all_filters_mode) { + break; + } + } else if (all_filters_mode) { + break; + } else { + // Do nothing. + } + } + + // In the multifilter mode, all UUIDs must be found in the advertisement packets. + if ((all_filters_mode && (uuid_match_cnt == counter)) || + ((!all_filters_mode) && (uuid_match_cnt > 0))) { + return true; + } + + return false; +} + +/**@brief Function for adding UUID to the scanning filter. + * + * @param[in] uuid UUID, 16-bit size. + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If the scanning started. Otherwise, an error code is returned, + * also if you tried to add a filter that was already added before. + * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. + */ +static int nrf_ble_scan_uuid_filter_add(struct nrf_ble_scan *const p_scan_ctx, + ble_uuid_t const *p_uuid) +{ + ble_uuid_t *p_uuid_filter = p_scan_ctx->scan_filters.uuid_filter.uuid; + uint8_t *p_counter = &p_scan_ctx->scan_filters.uuid_filter.uuid_cnt; + uint8_t index; + + // If no memory. + if (*p_counter >= NRF_BLE_SCAN_UUID_CNT) { + return NRF_ERROR_NO_MEM; + } + + // Check for duplicated filter. + for (index = 0; index < NRF_BLE_SCAN_UUID_CNT; index++) { + if (p_uuid_filter[index].uuid == p_uuid->uuid) { + return NRF_SUCCESS; + } + } + + // Add UUID to the filter. + p_uuid_filter[(*p_counter)++] = *p_uuid; + LOG_DBG("Added filter on UUID %x", p_uuid->uuid); + + return NRF_SUCCESS; +} + +#endif // NRF_BLE_SCAN_UUID_CNT + +#if (NRF_BLE_SCAN_APPEARANCE_CNT) +/**@brief Function for comparing the provided appearance with the appearance in the advertisement + * packets. + * + * @param[in] p_adv_report Advertising data to parse. + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * + * @return True if the appearances match. False otherwise. + */ +static bool adv_appearance_compare(ble_gap_evt_adv_report_t const *const p_adv_report, + struct nrf_ble_scan const *const p_scan_ctx) +{ + nrf_ble_scan_appearance_filter_t const *p_appearance_filter = + &p_scan_ctx->scan_filters.appearance_filter; + uint8_t const counter = p_scan_ctx->scan_filters.appearance_filter.appearance_cnt; + uint8_t index; + uint16_t data_len; + + data_len = p_adv_report->data.len; + + // Verify if the advertised appearance matches the provided appearance. + for (index = 0; index < counter; index++) { + if (ble_advdata_appearance_find(p_adv_report->data.p_data, data_len, + &p_appearance_filter->appearance[index])) { + return true; + } + } + return false; +} + +/**@brief Function for adding appearance to the scanning filter. + * + * @param[in] appearance Appearance to be added. + * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * + * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter + * that was already added before. + * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. + * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. + */ +static int nrf_ble_scan_appearance_filter_add(struct nrf_ble_scan *const p_scan_ctx, + uint16_t appearance) +{ + uint16_t *p_appearance_filter = p_scan_ctx->scan_filters.appearance_filter.appearance; + uint8_t *p_counter = &p_scan_ctx->scan_filters.appearance_filter.appearance_cnt; + uint8_t index; + + // If no memory. + if (*p_counter >= NRF_BLE_SCAN_APPEARANCE_CNT) { + return NRF_ERROR_NO_MEM; + } + + // Check for duplicated filter. + for (index = 0; index < NRF_BLE_SCAN_APPEARANCE_CNT; index++) { + if (p_appearance_filter[index] == appearance) { + return NRF_SUCCESS; + } + } + + // Add appearance to the filter. + p_appearance_filter[(*p_counter)++] = appearance; + LOG_DBG("Added filter on appearance %x", appearance); + return NRF_SUCCESS; +} + +#endif // NRF_BLE_SCAN_APPEARANCE_CNT + +int nrf_ble_scan_filter_set(struct nrf_ble_scan *const p_scan_ctx, + enum nrf_ble_scan_filter_type type, void const *p_data) +{ + // VERIFY_PARAM_NOT_NULL(p_scan_ctx); + // VERIFY_PARAM_NOT_NULL(p_data); + + switch (type) { +#if (NRF_BLE_SCAN_NAME_CNT > 0) + case SCAN_NAME_FILTER: { + char *p_name = (char *)p_data; + return nrf_ble_scan_name_filter_add(p_scan_ctx, p_name); + } +#endif + +#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) + case SCAN_SHORT_NAME_FILTER: { + nrf_ble_scan_short_name_t *p_short_name = (nrf_ble_scan_short_name_t *)p_data; + return nrf_ble_scan_short_name_filter_add(p_scan_ctx, p_short_name); + } +#endif + +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) + case SCAN_ADDR_FILTER: { + uint8_t *p_addr = (uint8_t *)p_data; + return nrf_ble_scan_addr_filter_add(p_scan_ctx, p_addr); + } +#endif + +#if (NRF_BLE_SCAN_UUID_CNT > 0) + case SCAN_UUID_FILTER: { + ble_uuid_t *p_uuid = (ble_uuid_t *)p_data; + return nrf_ble_scan_uuid_filter_add(p_scan_ctx, p_uuid); + } +#endif + +#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) + case SCAN_APPEARANCE_FILTER: { + uint16_t appearance = *((uint16_t *)p_data); + return nrf_ble_scan_appearance_filter_add(p_scan_ctx, appearance); + } +#endif + + default: + return NRF_ERROR_INVALID_PARAM; + } +} + +int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const p_scan_ctx) +{ +#if (NRF_BLE_SCAN_NAME_CNT > 0) + struct nrf_ble_scan_name_filter *p_name_filter = &p_scan_ctx->scan_filters.name_filter; + memset(p_name_filter->target_name, 0, sizeof(p_name_filter->target_name)); + p_name_filter->name_cnt = 0; +#endif + +#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) + nrf_ble_scan_short_name_filter_t *p_short_name_filter = + &p_scan_ctx->scan_filters.short_name_filter; + memset(p_short_name_filter->short_name, 0, sizeof(p_short_name_filter->short_name)); + p_short_name_filter->name_cnt = 0; +#endif + +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) + nrf_ble_scan_addr_filter_t *p_addr_filter = &p_scan_ctx->scan_filters.addr_filter; + memset(p_addr_filter->target_addr, 0, sizeof(p_addr_filter->target_addr)); + p_addr_filter->addr_cnt = 0; +#endif + +#if (NRF_BLE_SCAN_UUID_CNT > 0) + nrf_ble_scan_uuid_filter_t *p_uuid_filter = &p_scan_ctx->scan_filters.uuid_filter; + memset(p_uuid_filter->uuid, 0, sizeof(p_uuid_filter->uuid)); + p_uuid_filter->uuid_cnt = 0; +#endif + +#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) + nrf_ble_scan_appearance_filter_t *p_appearance_filter = + &p_scan_ctx->scan_filters.appearance_filter; + memset(p_appearance_filter->appearance, 0, sizeof(p_appearance_filter->appearance)); + p_appearance_filter->appearance_cnt = 0; +#endif + + return NRF_SUCCESS; +} + +int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const scan_ctx, uint8_t mode, bool match_all) +{ + // VERIFY_PARAM_NOT_NULL(p_scan_ctx); + if (scan_ctx == NULL) { + return NRF_ERROR_NULL; + } + + // Check if the mode is correct. + if ((!(mode & NRF_BLE_SCAN_ADDR_FILTER)) && (!(mode & NRF_BLE_SCAN_NAME_FILTER)) && + (!(mode & NRF_BLE_SCAN_UUID_FILTER)) && (!(mode & NRF_BLE_SCAN_SHORT_NAME_FILTER)) && + (!(mode & NRF_BLE_SCAN_APPEARANCE_FILTER))) { + return NRF_ERROR_INVALID_PARAM; + } + + int err_code; + + // Disable filters. + err_code = nrf_ble_scan_filters_disable(scan_ctx); + // ASSERT(err_code == NRF_SUCCESS); + + struct nrf_ble_scan_filters *filters = &scan_ctx->scan_filters; + + // Turn on the filters of your choice. +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) + if (mode & NRF_BLE_SCAN_ADDR_FILTER) { + p_filters->addr_filter.addr_filter_enabled = true; + } +#endif + +#if (NRF_BLE_SCAN_NAME_CNT > 0) + if (mode & NRF_BLE_SCAN_NAME_FILTER) { + filters->name_filter.name_filter_enabled = true; + } +#endif + +#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) + if (mode & NRF_BLE_SCAN_SHORT_NAME_FILTER) { + p_filters->short_name_filter.short_name_filter_enabled = true; + } +#endif + +#if (NRF_BLE_SCAN_UUID_CNT > 0) + if (mode & NRF_BLE_SCAN_UUID_FILTER) { + p_filters->uuid_filter.uuid_filter_enabled = true; + } +#endif + +#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) + if (mode & NRF_BLE_SCAN_APPEARANCE_FILTER) { + p_filters->appearance_filter.appearance_filter_enabled = true; + } +#endif + + // Select the filter mode. + filters->all_filters_mode = match_all; + + return NRF_SUCCESS; +} + +int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const p_scan_ctx) +{ + if (!p_scan_ctx) { + return NRF_ERROR_NULL; + } + + // Disable all filters. +#if (NRF_BLE_SCAN_NAME_CNT > 0) + bool *p_name_filter_enabled = &p_scan_ctx->scan_filters.name_filter.name_filter_enabled; + *p_name_filter_enabled = false; +#endif + +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) + bool *p_addr_filter_enabled = &p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled; + *p_addr_filter_enabled = false; +#endif + +#if (NRF_BLE_SCAN_UUID_CNT > 0) + bool *p_uuid_filter_enabled = &p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled; + *p_uuid_filter_enabled = false; +#endif + +#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) + bool *p_appearance_filter_enabled = + &p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled; + *p_appearance_filter_enabled = false; +#endif + + return NRF_SUCCESS; +} + +int nrf_ble_scan_filter_get(struct nrf_ble_scan *const scan_ctx, + struct nrf_ble_scan_filters *status) +{ + if (scan_ctx == NULL) { + return NRF_ERROR_NULL; + } + if (status == NULL) { + return NRF_ERROR_NULL; + } + + *status = scan_ctx->scan_filters; + + return NRF_SUCCESS; +} + +#endif // CONFIG_BT_SCAN_FILTER_ENABLE + +/**@brief Function for calling the BLE_GAP_EVT_ADV_REPORT event to check whether the received + * scanning data matches the scan configuration. + * + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] p_adv_report Advertising report. + */ +static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const p_scan_ctx, + ble_gap_evt_adv_report_t const *const p_adv_report) +{ + struct scan_evt scan_evt; + +#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 + uint8_t filter_cnt = 0; + uint8_t filter_match_cnt = 0; +#endif + + memset(&scan_evt, 0, sizeof(scan_evt)); + + scan_evt.p_scan_params = &p_scan_ctx->scan_params; + + // If the whitelist is used, do not check the filters and return. + if (is_whitelist_used(p_scan_ctx)) { + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT; + scan_evt.params.p_not_found = p_adv_report; + p_scan_ctx->evt_handler(&scan_evt); + + sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer); + nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report); + + return; + } + +#if BT_SCAN_FILTER_ENABLE + bool const all_filter_mode = p_scan_ctx->scan_filters.all_filters_mode; + bool is_filter_matched = false; + +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) + bool const addr_filter_enabled = p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled; +#endif + +#if (NRF_BLE_SCAN_NAME_CNT > 0) + bool const name_filter_enabled = p_scan_ctx->scan_filters.name_filter.name_filter_enabled; +#endif + +#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) + bool const short_name_filter_enabled = + p_scan_ctx->scan_filters.short_name_filter.short_name_filter_enabled; +#endif + +#if (NRF_BLE_SCAN_UUID_CNT > 0) + bool const uuid_filter_enabled = p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled; +#endif + +#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) + bool const appearance_filter_enabled = + p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled; +#endif + +#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) + // Check the address filter. + if (addr_filter_enabled) { + // Number of active filters. + filter_cnt++; + if (adv_addr_compare(p_adv_report, p_scan_ctx)) { + // Number of filters matched. + filter_match_cnt++; + // Information about the filters matched. + scan_evt.params.filter_match.filter_match.address_filter_match = true; + is_filter_matched = true; + } + } +#endif + +#if (NRF_BLE_SCAN_NAME_CNT > 0) + // Check the name filter. + if (name_filter_enabled) { + filter_cnt++; + if (adv_name_compare(p_adv_report, p_scan_ctx)) { + filter_match_cnt++; + + // Information about the filters matched. + scan_evt.params.filter_match.filter_match.name_filter_match = true; + is_filter_matched = true; + } + } +#endif + +#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) + if (short_name_filter_enabled) { + filter_cnt++; + if (adv_short_name_compare(p_adv_report, p_scan_ctx)) { + filter_match_cnt++; + + // Information about the filters matched. + scan_evt.params.filter_match.filter_match.short_name_filter_match = true; + is_filter_matched = true; + } + } +#endif + +#if (NRF_BLE_SCAN_UUID_CNT > 0) + // Check the UUID filter. + if (uuid_filter_enabled) { + filter_cnt++; + if (adv_uuid_compare(p_adv_report, p_scan_ctx)) { + filter_match_cnt++; + // Information about the filters matched. + scan_evt.params.filter_match.filter_match.uuid_filter_match = true; + is_filter_matched = true; + } + } +#endif + +#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) + // Check the appearance filter. + if (appearance_filter_enabled) { + filter_cnt++; + if (adv_appearance_compare(p_adv_report, p_scan_ctx)) { + filter_match_cnt++; + // Information about the filters matched. + scan_evt.params.filter_match.filter_match.appearance_filter_match = true; + is_filter_matched = true; + } + } + + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND; +#endif + + scan_evt.params.filter_match.p_adv_report = p_adv_report; + + // In the multifilter mode, the number of the active filters must equal the number of the + // filters matched to generate the notification. + if (all_filter_mode && (filter_match_cnt == filter_cnt)) { + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH; + nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report); + } + // In the normal filter mode, only one filter match is needed to generate the notification + // to the main application. + else if ((!all_filter_mode) && is_filter_matched) { + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH; + nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report); + } else { + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND; + scan_evt.params.p_not_found = p_adv_report; + } + + // If the event handler is not NULL, notify the main application. + if (p_scan_ctx->evt_handler != NULL) { + p_scan_ctx->evt_handler(&scan_evt); + } + +#endif // BT_SCAN_FILTER_ENABLE + + // Resume the scanning. + (void)sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer); +} + +/**@brief Function for checking whether the whitelist is used. + * + * @param[in] p_scan_ctx Scanning Module instance. + */ +bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx) +{ + if (scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST || + scan_ctx->scan_params.filter_policy == + BLE_GAP_SCAN_FP_WHITELIST_NOT_RESOLVED_DIRECTED) { + return true; + } + + return false; +} + +/**@brief Function for restoring the default scanning parameters. + * + * @param[out] p_scan_ctx Pointer to the Scanning Module instance. + */ +static void nrf_ble_scan_default_param_set(struct nrf_ble_scan *const scan_ctx) +{ + // Set the default parameters. + scan_ctx->scan_params.active = 1; +#if (NRF_SD_BLE_API_VERSION > 7) + p_scan_ctx->scan_params.interval_us = NRF_BLE_SCAN_SCAN_INTERVAL * UNIT_0_625_MS; + p_scan_ctx->scan_params.window_us = NRF_BLE_SCAN_SCAN_WINDOW * UNIT_0_625_MS; +#else + scan_ctx->scan_params.interval = NRF_BLE_SCAN_SCAN_INTERVAL; + scan_ctx->scan_params.window = NRF_BLE_SCAN_SCAN_WINDOW; +#endif // #if (NRF_SD_BLE_API_VERSION > 7) + scan_ctx->scan_params.timeout = NRF_BLE_SCAN_SCAN_DURATION; + scan_ctx->scan_params.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL; + scan_ctx->scan_params.scan_phys = BLE_GAP_PHY_1MBPS; +} + +/**@brief Function for setting the default connection parameters. + * + * @param[out] p_scan_ctx Pointer to the Scanning Module instance. + */ +static void nrf_ble_scan_default_conn_param_set(struct nrf_ble_scan *const scan_ctx) +{ + scan_ctx->conn_params.conn_sup_timeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN; + scan_ctx->conn_params.min_conn_interval = BLE_GAP_CONN_INTVL_MS_TO_UNITS(25); + scan_ctx->conn_params.max_conn_interval = BLE_GAP_CONN_INTVL_MS_TO_UNITS(25); + scan_ctx->conn_params.slave_latency = (uint16_t)NRF_BLE_SCAN_SLAVE_LATENCY; +} + +/**@brief Function for calling the BLE_GAP_EVT_TIMEOUT event. + * + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] p_gap GAP event structure. + */ +static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, + ble_gap_evt_t const *const gap) +{ + ble_gap_evt_timeout_t const *timeout = &gap->params.timeout; + struct scan_evt scan_evt = {0}; + + if (timeout->src == BLE_GAP_TIMEOUT_SRC_SCAN) { + LOG_DBG("BLE_GAP_SCAN_TIMEOUT"); + if (scan_ctx->evt_handler != NULL) { + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_SCAN_TIMEOUT; + scan_evt.p_scan_params = &scan_ctx->scan_params; + scan_evt.params.timeout.src = timeout->src; + + scan_ctx->evt_handler(&scan_evt); + } + } +} + +/**@brief Function for stopping the scanning. + */ +void nrf_ble_scan_stop(void) +{ + // It is ok to ignore the function return value here, because this function can return + // NRF_SUCCESS or NRF_ERROR_INVALID_STATE, when app is not in the scanning state. + (void)sd_ble_gap_scan_stop(); +} + +int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, + struct nrf_ble_scan_init const *const init, + nrf_ble_scan_evt_handler_t evt_handler) +{ + if (scan_ctx == NULL) { + return NRF_ERROR_NULL; + } + + scan_ctx->evt_handler = evt_handler; + +#if BT_SCAN_FILTER_ENABLE + // Disable all scanning filters. + memset(&p_scan_ctx->scan_filters, 0, sizeof(p_scan_ctx->scan_filters)); +#endif + + // If the pointer to the initialization structure exist, use it to scan the configuration. + if (init != NULL) { + scan_ctx->connect_if_match = init->connect_if_match; + scan_ctx->conn_cfg_tag = init->conn_cfg_tag; + + if (init->p_scan_param != NULL) { + scan_ctx->scan_params = *init->p_scan_param; + } else { + // Use the default static configuration. + nrf_ble_scan_default_param_set(scan_ctx); + } + + if (init->p_conn_param != NULL) { + scan_ctx->conn_params = *init->p_conn_param; + } else { + // Use the default static configuration. + nrf_ble_scan_default_conn_param_set(scan_ctx); + } + } + // If pointer is NULL, use the static default configuration. + else { + nrf_ble_scan_default_param_set(scan_ctx); + nrf_ble_scan_default_conn_param_set(scan_ctx); + + scan_ctx->connect_if_match = false; + } + + // Assign a buffer where the advertising reports are to be stored by the SoftDevice. + scan_ctx->scan_buffer.p_data = scan_ctx->scan_buffer_data; + scan_ctx->scan_buffer.len = NRF_BLE_SCAN_BUFFER; + + return NRF_SUCCESS; +} + +int nrf_ble_scan_start(struct nrf_ble_scan const *const p_scan_ctx) +{ + // VERIFY_PARAM_NOT_NULL(p_scan_ctx); + + if (p_scan_ctx == NULL) { + LOG_ERR("Scan ctx is NULL"); + } + + int err_code; + struct scan_evt scan_evt = {0}; + + nrf_ble_scan_stop(); + + // If the whitelist is used and the event handler is not NULL, send the whitelist request to + // the main application. + if (is_whitelist_used(p_scan_ctx)) { + if (p_scan_ctx->evt_handler != NULL) { + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_REQUEST; + p_scan_ctx->evt_handler(&scan_evt); + } + } + + // Start the scanning. + err_code = sd_ble_gap_scan_start(&p_scan_ctx->scan_params, &p_scan_ctx->scan_buffer); + + // It is okay to ignore this error, because the scan stopped earlier. + if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_SUCCESS)) { + LOG_ERR("sd_ble_gap_scan_start returned 0x%x", err_code); + return (err_code); + } + LOG_DBG("Scanning"); + + return NRF_SUCCESS; +} + +int nrf_ble_scan_params_set(struct nrf_ble_scan *const p_scan_ctx, + ble_gap_scan_params_t const *const p_scan_param) +{ + // VERIFY_PARAM_NOT_NULL(p_scan_ctx); + nrf_ble_scan_stop(); + + if (p_scan_param != NULL) { + // Assign new scanning parameters. + p_scan_ctx->scan_params = *p_scan_param; + } else { + // If NULL, use the default static configuration. + nrf_ble_scan_default_param_set(p_scan_ctx); + } + + LOG_DBG("Scanning parameters have been changed successfully"); + + return NRF_SUCCESS; +} + +/**@brief Function for calling the BLE_GAP_EVT_CONNECTED event. + * + * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] p_gap_evt GAP event structure. + */ +static void nrf_ble_scan_on_connected_evt(struct nrf_ble_scan const *const p_scan_ctx, + ble_gap_evt_t const *const p_gap_evt) +{ + struct scan_evt scan_evt; + + memset(&scan_evt, 0, sizeof(scan_evt)); + scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTED; + scan_evt.params.connected.p_connected = &p_gap_evt->params.connected; + scan_evt.params.connected.conn_handle = p_gap_evt->conn_handle; + scan_evt.p_scan_params = &p_scan_ctx->scan_params; + + if (p_scan_ctx->evt_handler != NULL) { + p_scan_ctx->evt_handler(&scan_evt); + } +} + +int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *p_gap_addr, + const uint8_t addr[BLE_GAP_ADDR_LEN]) +{ + // VERIFY_PARAM_NOT_NULL(p_gap_addr); + + for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; ++i) { + p_gap_addr->addr[i] = addr[BLE_GAP_ADDR_LEN - (i + 1)]; + } + + return NRF_SUCCESS; +} + +void nrf_ble_scan_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_contex) +{ + struct nrf_ble_scan *p_scan_data = (struct nrf_ble_scan *)p_contex; + ble_gap_evt_adv_report_t const *p_adv_report = &p_ble_evt->evt.gap_evt.params.adv_report; + ble_gap_evt_t const *p_gap_evt = &p_ble_evt->evt.gap_evt; + + switch (p_ble_evt->header.evt_id) { + case BLE_GAP_EVT_ADV_REPORT: + nrf_ble_scan_on_adv_report(p_scan_data, p_adv_report); + break; + + case BLE_GAP_EVT_TIMEOUT: + nrf_ble_scan_on_timeout(p_scan_data, p_gap_evt); + break; + + case BLE_GAP_EVT_CONNECTED: + nrf_ble_scan_on_connected_evt(p_scan_data, p_gap_evt); + break; + + default: + break; + } +} From 2d5f467cbaf390036fcaae11286470ccfb7d7160 Mon Sep 17 00:00:00 2001 From: Sondre Pettersen Date: Tue, 7 Oct 2025 14:46:49 +0200 Subject: [PATCH 2/6] lib: scan: fix copyright statement Signed-off-by: Sondre Pettersen Co-authored-by: Asil Zogby --- lib/bluetooth/ble_scan/ble_scan.c | 221 ++++-------------------------- 1 file changed, 24 insertions(+), 197 deletions(-) diff --git a/lib/bluetooth/ble_scan/ble_scan.c b/lib/bluetooth/ble_scan/ble_scan.c index 59d2f25144..d9a51961eb 100644 --- a/lib/bluetooth/ble_scan/ble_scan.c +++ b/lib/bluetooth/ble_scan/ble_scan.c @@ -1,66 +1,19 @@ -/** - * Copyright (c) 2018 - 2021, Nordic Semiconductor ASA - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form, except as embedded into a Nordic - * Semiconductor ASA integrated circuit in a product or a software update for - * such product, must reproduce the above copyright notice, this list of - * conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * 3. Neither the name of Nordic Semiconductor ASA nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * 4. This software, with or without modification, must only be used with a - * Nordic Semiconductor ASA integrated circuit. - * - * 5. Any software provided in binary form under this license must not be reverse - * engineered, decompiled, modified and/or disassembled. - * - * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* + * Copyright (c) 2015 - 2025 Nordic Semiconductor ASA * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ // #include "sdk_common.h" #include #include - -// #include "nrf_ble_scan.h" - #include #include -// #include "app_error.h" -// #include "nrf_assert.h" -// #include "sdk_macros.h" -// #include "ble_advdata.h" - #include LOG_MODULE_REGISTER(ble_scan, 3); -// NRF_BLE_SCAN_ENABLED - nrf_ble_scan - Scanning Module -//========================================================== -#ifndef NRF_BLE_SCAN_ENABLED -#define NRF_BLE_SCAN_ENABLED 1 -#endif // NRF_BLE_SCAN_BUFFER - Data length for an advertising set. #ifndef NRF_BLE_SCAN_BUFFER #define NRF_BLE_SCAN_BUFFER 31 @@ -96,40 +49,11 @@ LOG_MODULE_REGISTER(ble_scan, 3); #define NRF_BLE_SCAN_SCAN_WINDOW 80 #endif -// NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL - Determines minimum connection interval in -// milliseconds. -#ifndef NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL -#define NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL 100 -#endif - -// NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL - Determines maximum connection interval in -// milliseconds. -#ifndef NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL -#define NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL 500 -#endif - // NRF_BLE_SCAN_SLAVE_LATENCY - Determines the slave latency in counts of connection events. #ifndef NRF_BLE_SCAN_SLAVE_LATENCY #define NRF_BLE_SCAN_SLAVE_LATENCY 5 #endif -// NRF_BLE_SCAN_SUPERVISION_TIMEOUT - Determines the supervision time-out in units of 10 -// millisecond. -#ifndef NRF_BLE_SCAN_SUPERVISION_TIMEOUT -#define NRF_BLE_SCAN_SUPERVISION_TIMEOUT 6200 -#endif - -/**@brief Function for establishing the connection with a device. - * - * @details Connection is established if @ref NRF_BLE_SCAN_EVT_FILTER_MATCH - * or @ref NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT occurs and the module was - * initialized in the automatic connection mode. This function can generate an event - * to the main application when @ref sd_ble_gap_connect is used inside the function and it - * returns value that is different than @ref NRF_SUCCESS. - * - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. - * @param[in] p_adv_report Advertising data. - */ static void nrf_ble_scan_connect_with_target(struct nrf_ble_scan const *const p_scan_ctx, ble_gap_evt_adv_report_t const *const p_adv_report) { @@ -174,17 +98,6 @@ static void nrf_ble_scan_connect_with_target(struct nrf_ble_scan const *const p_ #if CONFIG_BT_SCAN_FILTER_ENABLE || 1 #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) -/**@brief Function for searching for the provided address in the advertisement packets. - * - * @details Use this function to parse the received advertising data for the provided address. - * - * - * @param[in] p_adv_report Advertising data to parse. - * @param[in] p_addr Address to search for. The address length must correspond to @ref - * BLE_GAP_ADDR_LEN. - * - * @return True if the provided address was found, false otherwise. - */ static bool find_peer_addr(ble_gap_evt_adv_report_t const *const p_adv_report, ble_gap_addr_t const *p_addr) { @@ -197,15 +110,6 @@ static bool find_peer_addr(ble_gap_evt_adv_report_t const *const p_adv_report, return false; } -/** @brief Function for comparing the provided address with the addresses of the advertising - * devices. - * - * @param[in] p_adv_report Advertising data to parse. - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. - * - * @retval True when the address matches with the addresses of the advertising devices. False - * otherwise. - */ static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const p_adv_report, struct nrf_ble_scan const *const p_scan_ctx) { @@ -222,18 +126,6 @@ static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const p_adv_report, return false; } -/**@brief Function for adding target address to the scanning filter. - * - * @param[in] p_addr Target address in the format required by the SoftDevice. If you - * need to convert the address, use @ref nrf_ble_scan_copy_addr_to_sd_gap_addr. The address length - * must correspond to @ref BLE_GAP_ADDR_LEN. - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. - * - * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a - * filter that was already added before. - * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. - * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address type is invalid. - */ static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const p_scan_ctx, uint8_t const *p_addr) { @@ -272,13 +164,7 @@ static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const p_scan_ctx, #endif // NRF_BLE_SCAN_ADDRESS_CNT #if (NRF_BLE_SCAN_NAME_CNT > 0) -/** @brief Function for comparing the provided name with the advertised name. - * - * @param[in] p_adv_report Advertising data to parse. - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. - * - * @retval True when the names match. False otherwise. - */ + static bool adv_name_compare(ble_gap_evt_adv_report_t const *p_adv_report, struct nrf_ble_scan const *const p_scan_ctx) { @@ -301,18 +187,6 @@ static bool adv_name_compare(ble_gap_evt_adv_report_t const *p_adv_report, return false; } -/**@brief Function for adding name of the peripheral to the scanning filter. - * - * @param[in] p_name Peripheral name. - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. - * - * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a - * filter that was already added before. - * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. - * @retval NRF_ERROR_DATA_SIZE If the name filter length is too long. The maximum filter name - * length corresponds to @ref NRF_BLE_SCAN_NAME_MAX_LEN. - * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. - */ static int nrf_ble_scan_name_filter_add(struct nrf_ble_scan *const p_scan_ctx, char const *p_name) { uint8_t index; @@ -348,13 +222,6 @@ static int nrf_ble_scan_name_filter_add(struct nrf_ble_scan *const p_scan_ctx, c #endif // NRF_BLE_SCAN_NAME_CNT #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) -/** @brief Function for comparing the provided short name with the advertised short name. - * - * @param[in] p_adv_report Advertising data to parse. - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. - * - * @retval True when the names match. False otherwise. - */ static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const p_adv_report, struct nrf_ble_scan const *const p_scan_ctx) { @@ -379,18 +246,6 @@ static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const p_adv_r return false; } -/**@brief Function for adding the short name of the peripheral to the scanning filter. - * - * @param[in] p_short_name Short name of the peripheral. - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. - * - * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a - * filter that was already added before. - * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. - * @retval NRF_ERROR_DATA_SIZE If the name filter length is too long. The maximum filter name - * length corresponds to @ref NRF_BLE_SCAN_SHORT_NAME_MAX_LEN. - * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. - */ static int nrf_ble_scan_short_name_filter_add(struct nrf_ble_scan *const p_scan_ctx, nrf_ble_scan_short_name_t const *p_short_name) { @@ -432,13 +287,7 @@ static int nrf_ble_scan_short_name_filter_add(struct nrf_ble_scan *const p_scan_ #endif #if (NRF_BLE_SCAN_UUID_CNT > 0) -/**@brief Function for comparing the provided UUID with the UUID in the advertisement packets. - * - * @param[in] p_adv_report Advertising data to parse. - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. - * - * @return True if the UUIDs match. False otherwise. - */ + static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const p_adv_report, struct nrf_ble_scan const *const p_scan_ctx) { @@ -477,15 +326,6 @@ static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const p_adv_report, return false; } -/**@brief Function for adding UUID to the scanning filter. - * - * @param[in] uuid UUID, 16-bit size. - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. - * - * @retval NRF_SUCCESS If the scanning started. Otherwise, an error code is returned, - * also if you tried to add a filter that was already added before. - * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. - */ static int nrf_ble_scan_uuid_filter_add(struct nrf_ble_scan *const p_scan_ctx, ble_uuid_t const *p_uuid) { @@ -515,14 +355,7 @@ static int nrf_ble_scan_uuid_filter_add(struct nrf_ble_scan *const p_scan_ctx, #endif // NRF_BLE_SCAN_UUID_CNT #if (NRF_BLE_SCAN_APPEARANCE_CNT) -/**@brief Function for comparing the provided appearance with the appearance in the advertisement - * packets. - * - * @param[in] p_adv_report Advertising data to parse. - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. - * - * @return True if the appearances match. False otherwise. - */ + static bool adv_appearance_compare(ble_gap_evt_adv_report_t const *const p_adv_report, struct nrf_ble_scan const *const p_scan_ctx) { @@ -544,16 +377,6 @@ static bool adv_appearance_compare(ble_gap_evt_adv_report_t const *const p_adv_r return false; } -/**@brief Function for adding appearance to the scanning filter. - * - * @param[in] appearance Appearance to be added. - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. - * - * @retval NRF_SUCCESS If the filter is added successfully or if you try to add a filter - * that was already added before. - * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. - * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. - */ static int nrf_ble_scan_appearance_filter_add(struct nrf_ble_scan *const p_scan_ctx, uint16_t appearance) { @@ -581,45 +404,49 @@ static int nrf_ble_scan_appearance_filter_add(struct nrf_ble_scan *const p_scan_ #endif // NRF_BLE_SCAN_APPEARANCE_CNT -int nrf_ble_scan_filter_set(struct nrf_ble_scan *const p_scan_ctx, - enum nrf_ble_scan_filter_type type, void const *p_data) +int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_scan_filter_type type, + void const *data) { - // VERIFY_PARAM_NOT_NULL(p_scan_ctx); - // VERIFY_PARAM_NOT_NULL(p_data); + if (scan_ctx == NULL) { + return NRF_ERROR_NULL; + } + if (data == NULL) { + return NRF_ERROR_NULL; + } switch (type) { #if (NRF_BLE_SCAN_NAME_CNT > 0) case SCAN_NAME_FILTER: { - char *p_name = (char *)p_data; - return nrf_ble_scan_name_filter_add(p_scan_ctx, p_name); + char *name = (char *)data; + return nrf_ble_scan_name_filter_add(scan_ctx, name); } #endif #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) case SCAN_SHORT_NAME_FILTER: { - nrf_ble_scan_short_name_t *p_short_name = (nrf_ble_scan_short_name_t *)p_data; - return nrf_ble_scan_short_name_filter_add(p_scan_ctx, p_short_name); + nrf_ble_scan_short_name_t *short_name = (nrf_ble_scan_short_name_t *)data; + return nrf_ble_scan_short_name_filter_add(scan_ctx, short_name); } #endif #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) case SCAN_ADDR_FILTER: { - uint8_t *p_addr = (uint8_t *)p_data; - return nrf_ble_scan_addr_filter_add(p_scan_ctx, p_addr); + uint8_t *addr = (uint8_t *)data; + return nrf_ble_scan_addr_filter_add(scan_ctx, addr); } #endif #if (NRF_BLE_SCAN_UUID_CNT > 0) case SCAN_UUID_FILTER: { - ble_uuid_t *p_uuid = (ble_uuid_t *)p_data; - return nrf_ble_scan_uuid_filter_add(p_scan_ctx, p_uuid); + ble_uuid_t *uuid = (ble_uuid_t *)data; + return nrf_ble_scan_uuid_filter_add(scan_ctx, uuid); } #endif #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) case SCAN_APPEARANCE_FILTER: { - uint16_t appearance = *((uint16_t *)p_data); - return nrf_ble_scan_appearance_filter_add(p_scan_ctx, appearance); + uint16_t appearance = *((uint16_t *)data); + return nrf_ble_scan_appearance_filter_add(scan_ctx, appearance); } #endif From 57ddf4786b70d40007af0d865847894a716971d9 Mon Sep 17 00:00:00 2001 From: Sondre Pettersen Date: Tue, 14 Oct 2025 15:57:47 +0200 Subject: [PATCH 3/6] lib: scan: update coding standard - Remove Hungarian notation - Standardise comments - WIP Signed-off-by: Sondre Pettersen Co-authored-by: Asil Zogby --- include/bm/bluetooth/ble_scan.h | 267 ++++++------- lib/bluetooth/ble_scan/ble_scan.c | 607 +++++++++++++++--------------- 2 files changed, 435 insertions(+), 439 deletions(-) diff --git a/include/bm/bluetooth/ble_scan.h b/include/bm/bluetooth/ble_scan.h index d4e61433fb..d5e95e6fd2 100644 --- a/include/bm/bluetooth/ble_scan.h +++ b/include/bm/bluetooth/ble_scan.h @@ -4,7 +4,7 @@ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ -/** +/* * @file * * @defgroup nrf_ble_scan Scanning Module @@ -132,18 +132,18 @@ extern "C" { #define NRF_BLE_SCAN_APPEARANCE_CNT 0 #endif -/** +/* * @defgroup NRF_BLE_SCAN_FILTER_MODE Filter modes * @{ */ -#define NRF_BLE_SCAN_NAME_FILTER (0x01) /** Filters the device name. */ -#define NRF_BLE_SCAN_ADDR_FILTER (0x02) /** Filters the device address. */ -#define NRF_BLE_SCAN_UUID_FILTER (0x04) /** Filters the UUID. */ -#define NRF_BLE_SCAN_APPEARANCE_FILTER (0x08) /** Filters the appearance. */ -#define NRF_BLE_SCAN_SHORT_NAME_FILTER (0x10) /** Filters the device short name. */ -#define NRF_BLE_SCAN_ALL_FILTER (0x1F) /** Uses the combination of all filters. */ +#define NRF_BLE_SCAN_NAME_FILTER (0x01) /* Filters the device name. */ +#define NRF_BLE_SCAN_ADDR_FILTER (0x02) /* Filters the device address. */ +#define NRF_BLE_SCAN_UUID_FILTER (0x04) /* Filters the UUID. */ +#define NRF_BLE_SCAN_APPEARANCE_FILTER (0x08) /* Filters the appearance. */ +#define NRF_BLE_SCAN_SHORT_NAME_FILTER (0x10) /* Filters the device short name. */ +#define NRF_BLE_SCAN_ALL_FILTER (0x1F) /* Uses the combination of all filters. */ /* @} */ -/** +/* * @brief Macro for defining a nrf_ble_scan instance. * * @param _name Name of the instance. @@ -162,72 +162,72 @@ extern "C" { * cannot be ignored if whitelist is used. */ enum nrf_ble_scan_evt { - /** A filter is matched or all filters are matched in the multifilter mode. */ + /* A filter is matched or all filters are matched in the multifilter mode. */ NRF_BLE_SCAN_EVT_FILTER_MATCH, - /** Request the whitelist from the main application. + /* Request the whitelist from the main application. * For whitelist scanning to work, the whitelist must be * set when this event occurs. */ NRF_BLE_SCAN_EVT_WHITELIST_REQUEST, - /** Send notification to the main application when a - *device from the whitelist is found. + /* Send notification to the main application when a + * device from the whitelist is found. */ NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT, - /** The filter was not matched for the scan data. */ + /* The filter was not matched for the scan data. */ NRF_BLE_SCAN_EVT_NOT_FOUND, - /** Scan timeout. */ + /* Scan timeout. */ NRF_BLE_SCAN_EVT_SCAN_TIMEOUT, - /** Error occurred when establishing the connection. In + /* Error occurred when establishing the connection. In *this event, an error is passed from the function call *@ref sd_ble_gap_connect. */ NRF_BLE_SCAN_EVT_CONNECTING_ERROR, - /** Connected to device. */ + /* Connected to device. */ NRF_BLE_SCAN_EVT_CONNECTED, NRF_BLE_SCAN_EVT_ERROR, }; /** - *@brief Types of filters. + * @brief Types of filters. */ enum nrf_ble_scan_filter_type { - /** Filter for names. */ + /* Filter for names. */ SCAN_NAME_FILTER, - /** Filter for short names. */ + /* Filter for short names. */ SCAN_SHORT_NAME_FILTER, - /** Filter for addresses. */ + /* Filter for addresses. */ SCAN_ADDR_FILTER, - /** Filter for UUIDs. */ + /* Filter for UUIDs. */ SCAN_UUID_FILTER, - /** Filter for appearances. */ + /* Filter for appearances. */ SCAN_APPEARANCE_FILTER, }; struct nrf_ble_scan_short_name { - /** Pointer to the short name. */ - char const *p_short_name; - /** Minimum length of the short name. */ + /* Pointer to the short name. */ + char const *short_name; + /* Minimum length of the short name. */ uint8_t short_name_min_len; }; /** - * @brief Structure for Scanning Module initialization. + * @brief Structure for Scanning Module initialization. */ struct nrf_ble_scan_init { - /** BLE GAP scan parameters required to initialize the module. Can + /* BLE GAP scan parameters required to initialize the module. Can * be initialized as NULL. If NULL, the parameters required to * initialize the module are loaded from the static configuration. */ - ble_gap_scan_params_t const *p_scan_param; - /** If set to true, the module automatically connects after a filter + ble_gap_scan_params_t const *scan_param; + /* If set to true, the module automatically connects after a filter * match or successful identification of a device from the whitelist. */ bool connect_if_match; - /** Connection parameters. Can be initialized as NULL. If NULL, the + /* Connection parameters. Can be initialized as NULL. If NULL, the * default static configuration is used. */ - ble_gap_conn_params_t const *p_conn_param; - /** Variable to keep track of what connection settings will be used + ble_gap_conn_params_t const *conn_param; + /* Variable to keep track of what connection settings will be used * if a filer match or a whitelist match results in a connection. */ uint8_t conn_cfg_tag; @@ -239,15 +239,15 @@ struct nrf_ble_scan_init { * @details This structure is used for sending filter status to the main application. */ struct nrf_ble_scan_filter_match { - /** Set to 1 if name filter is matched. */ + /* Set to 1 if name filter is matched. */ uint8_t name_filter_match: 1; - /** Set to 1 if address filter is matched. */ + /* Set to 1 if address filter is matched. */ uint8_t address_filter_match: 1; - /** Set to 1 if uuid filter is matched. */ + /* Set to 1 if uuid filter is matched. */ uint8_t uuid_filter_match: 1; - /** Set to 1 if appearance filter is matched. */ + /* Set to 1 if appearance filter is matched. */ uint8_t appearance_filter_match: 1; - /** Set to 1 if short name filter is matched. */ + /* Set to 1 if short name filter is matched. */ uint8_t short_name_filter_match: 1; }; @@ -255,11 +255,11 @@ struct nrf_ble_scan_filter_match { * @brief Event structure for @ref NRF_BLE_SCAN_EVT_FILTER_MATCH. */ struct nrf_ble_scan_evt_filter_match { - /** Event structure for @ref BLE_GAP_EVT_ADV_REPORT. This data - * allows the main application to establish connection. + /* Event structure for @ref BLE_GAP_EVT_ADV_REPORT. This data + * allows the main application to establish connection. */ - ble_gap_evt_adv_report_t const *p_adv_report; - /** Matching filters. Information about matched filters. */ + ble_gap_evt_adv_report_t const *adv_report; + /* Matching filters. Information about matched filters. */ struct nrf_ble_scan_filter_match filter_match; }; @@ -267,7 +267,7 @@ struct nrf_ble_scan_evt_filter_match { * @brief Event structure for @ref NRF_BLE_SCAN_EVT_CONNECTING_ERROR. */ struct nrf_ble_scan_evt_connecting_err { - /** Indicates success or failure of an API procedure. In case of + /* Indicates success or failure of an API procedure. In case of * failure, a comprehensive error code indicating the cause or reason * for failure is provided. */ @@ -278,9 +278,9 @@ struct nrf_ble_scan_evt_connecting_err { * @brief Event structure for @ref NRF_BLE_SCAN_EVT_CONNECTED. */ struct nrf_ble_scan_evt_connected { - /** Connected event parameters. */ - ble_gap_evt_connected_t const *p_connected; - /** Connection handle of the device on which the event occurred. */ + /* Connected event parameters. */ + ble_gap_evt_connected_t const *connected; + /* Connection handle of the device on which the event occurred. */ uint16_t conn_handle; }; @@ -291,49 +291,49 @@ struct nrf_ble_scan_evt_connected { * occurs. */ struct scan_evt { - /** Type of event propagated to the main application. */ + /* Type of event propagated to the main application. */ enum nrf_ble_scan_evt scan_evt_id; union { - /** Scan filter match. */ + /* Scan filter match. */ struct nrf_ble_scan_evt_filter_match filter_match; - /** Timeout event parameters. */ + /* Timeout event parameters. */ ble_gap_evt_timeout_t timeout; - /** Advertising report event parameters for whitelist. */ - ble_gap_evt_adv_report_t const *p_whitelist_adv_report; - /** Advertising report event parameters when filter is not found. */ - ble_gap_evt_adv_report_t const *p_not_found; - /** Connected event parameters. */ + /* Advertising report event parameters for whitelist. */ + ble_gap_evt_adv_report_t const *whitelist_adv_report; + /* Advertising report event parameters when filter is not found. */ + ble_gap_evt_adv_report_t const *not_found; + /* Connected event parameters. */ struct nrf_ble_scan_evt_connected connected; - /** Error event when connecting. Propagates the error code returned by the + /* Error event when connecting. Propagates the error code returned by the SoftDevice API @ref sd_ble_gap_scan_start. */ struct nrf_ble_scan_evt_connecting_err connecting_err; struct { uint32_t reason; } error; } params; - /** GAP scanning parameters. These parameters + /* GAP scanning parameters. These parameters * are needed to establish connection. */ - ble_gap_scan_params_t const *p_scan_params; + ble_gap_scan_params_t const *scan_params; }; /** * @brief BLE scanning event handler type. */ -typedef void (*nrf_ble_scan_evt_handler_t)(struct scan_evt const *p_scan_evt); +typedef void (*nrf_ble_scan_evt_handler_t)(struct scan_evt const *scan_evt); #if CONFIG_BT_SCAN_FILTER_ENABLE || 1 #if (NRF_BLE_SCAN_NAME_CNT > 0) struct nrf_ble_scan_name_filter { - /** Names that the main application will scan + /* Names that the main application will scan * for, and that will be advertised by the * peripherals. */ char target_name[NRF_BLE_SCAN_NAME_CNT][NRF_BLE_SCAN_NAME_MAX_LEN]; - /** Name filter counter. */ + /* Name filter counter. */ uint8_t name_cnt; - /** Flag to inform about enabling or disabling this filter. */ + /* Flag to inform about enabling or disabling this filter. */ bool name_filter_enabled; }; #endif @@ -341,19 +341,19 @@ struct nrf_ble_scan_name_filter { #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) typedef struct { struct { - /** Short names that the + /* Short names that the * main application will * scan for, and that will * be advertised by the * peripherals. */ char short_target_name[NRF_BLE_SCAN_SHORT_NAME_MAX_LEN]; - /** Minimum length of the short name. */ + /* Minimum length of the short name. */ uint8_t short_name_min_len; } short_name[NRF_BLE_SCAN_SHORT_NAME_CNT]; - /** Short name filter counter. */ + /* Short name filter counter. */ uint8_t name_cnt; - /** Flag to inform about enabling or disabling this filter. */ + /* Flag to inform about enabling or disabling this filter. */ bool short_name_filter_enabled; } nrf_ble_scan_short_name_filter_t; #endif @@ -361,14 +361,15 @@ typedef struct { #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) struct nrf_ble_scan_addr_filter { struct ble_gap_addr - /** Addresses in the same format as the - format used by the SoftDevice that the - main application will scan for, and that - will be advertised by the peripherals. */ + /* Addresses in the same format as the + * format used by the SoftDevice that the + * main application will scan for, and that + * will be advertised by the peripherals. + */ target_addr[NRF_BLE_SCAN_ADDRESS_CNT]; - /** Address filter counter. */ + /* Address filter counter. */ uint8_t addr_cnt; - /** Flag to inform about enabling or disabling this filter. */ + /* Flag to inform about enabling or disabling this filter. */ bool addr_filter_enabled; }; #endif @@ -376,25 +377,25 @@ struct nrf_ble_scan_addr_filter { #if (NRF_BLE_SCAN_UUID_CNT > 0) struct nrf_ble_scan_uuid_filter { struct ble_uuid - /** UUIDs that the main application will scan for, and + /* UUIDs that the main application will scan for, and that will be advertised by the peripherals. */ uuid[NRF_BLE_SCAN_UUID_CNT]; - /** UUID filter counter. */ + /* UUID filter counter. */ uint8_t uuid_cnt; - /** Flag to inform about enabling or disabling this filter. */ + /* Flag to inform about enabling or disabling this filter. */ bool uuid_filter_enabled; }; #endif #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) typedef struct { - /** Apperances that the main application + /* Apperances that the main application will scan for, and that will be advertised by the peripherals. */ uint16_t appearance[NRF_BLE_SCAN_APPEARANCE_CNT]; - /** Appearance filter counter. */ + /* Appearance filter counter. */ uint8_t appearance_cnt; - /** Flag to inform about enabling or disabling this filter.*/ + /* Flag to inform about enabling or disabling this filter.*/ bool appearance_filter_enabled; } nrf_ble_scan_appearance_filter_t; #endif @@ -409,26 +410,26 @@ typedef struct { */ struct nrf_ble_scan_filters { #if (NRF_BLE_SCAN_NAME_CNT > 0) - /** Name filter data. */ + /* Name filter data. */ struct nrf_ble_scan_name_filter name_filter; #endif #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) - /** Short name filter data. */ + /* Short name filter data. */ struct nrf_ble_scan_short_name_filter short_name_filter; #endif #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) - /** Address filter data. */ + /* Address filter data. */ struct nrf_ble_scan_addr_filter addr_filter; #endif #if (NRF_BLE_SCAN_UUID_CNT > 0) - /** UUID filter data. */ + /* UUID filter data. */ struct nrf_ble_scan_uuid_filter uuid_filter; #endif #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) - /** Appearance filter data. */ + /* Appearance filter data. */ nrf_ble_scan_appearance_filter_t appearance_filter; #endif - /** Filter mode. If true, all set filters must be matched to + /* Filter mode. If true, all set filters must be matched to generate an event.*/ bool all_filters_mode; }; @@ -447,47 +448,52 @@ struct nrf_ble_scan_filters { */ struct nrf_ble_scan { #if CONFIG_BT_SCAN_FILTER_ENABLE || 1 - /** Filter data. */ + /* Filter data. */ struct nrf_ble_scan_filters scan_filters; #endif - /** If set to true, the module automatically connects after a filter - match or successful identification of a device from the whitelist.*/ + /* If set to true, the module automatically connects after a filter + * match or successful identification of a device from the whitelist. + */ bool connect_if_match; - /** Connection parameters. */ + /* Connection parameters. */ ble_gap_conn_params_t conn_params; - /** Variable to keep track of what connection settings will be used - if a filer match or a whitelist match results in a connection. */ + /* Variable to keep track of what connection settings will be used + * if a filer match or a whitelist match results in a connection. + */ uint8_t conn_cfg_tag; - /** GAP scanning parameters. */ + /* GAP scanning parameters. */ ble_gap_scan_params_t scan_params; - /** Handler for the scanning events. Can be initialized as NULL if no - handling is implemented in the main application. */ + /* Handler for the scanning events. Can be initialized as NULL if no + * handling is implemented in the main application. + */ nrf_ble_scan_evt_handler_t evt_handler; - /** Buffer where advertising reports will be - stored by the SoftDevice. */ + /* Buffer where advertising reports will be + * stored by the SoftDevice. + */ uint8_t scan_buffer_data[NRF_BLE_SCAN_BUFFER]; - /** Structure-stored pointer to the buffer where advertising - reports will be stored by the SoftDevice. */ + /* Structure-stored pointer to the buffer where advertising + * reports will be stored by the SoftDevice. + */ ble_data_t scan_buffer; }; /** * @brief Function for indicating that the Scanning Module is using the whitelist. * - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] scan_ctx Pointer to the Scanning Module instance. * * @return Whether the whitelist is used. */ -bool is_whitelist_used(struct nrf_ble_scan const *const p_scan_ctx); +bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx); /** * @brief Function for initializing the Scanning Module. * - * @param[out] p_scan_ctx Pointer to the Scanning Module instance. This structure must be supplied + * @param[out] scan_ctx Pointer to the Scanning Module instance. This structure must be supplied * by the application. It is initialized by this function and is later used to identify this * particular module instance. * - * @param[in] p_init Can be initialized as NULL. If NULL, the parameters required to + * @param[in] init Can be initialized as NULL. If NULL, the parameters required to * initialize the module are loaded from static configuration. If module is to establish the * connection automatically, this must be initialized with the relevant data. * @param[in] evt_handler Handler for the scanning events. @@ -497,8 +503,8 @@ bool is_whitelist_used(struct nrf_ble_scan const *const p_scan_ctx); * @retval NRF_SUCCESS If initialization was successful. * @retval NRF_ERROR_NULL When the NULL pointer is passed as input. */ -int nrf_ble_scan_init(struct nrf_ble_scan *const p_scan_ctx, - struct nrf_ble_scan_init const *const p_init, +int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, + struct nrf_ble_scan_init const *const init, nrf_ble_scan_evt_handler_t evt_handler); /** @@ -507,7 +513,7 @@ int nrf_ble_scan_init(struct nrf_ble_scan *const p_scan_ctx, * @details This function starts the scanning according to the configuration set during the * initialization. * - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] scan_ctx Pointer to the Scanning Module instance. * * @retval NRF_SUCCESS If scanning started. Otherwise, an error code is returned. * @retval NRF_ERROR_NULL If NULL pointer is passed as input. @@ -515,7 +521,7 @@ int nrf_ble_scan_init(struct nrf_ble_scan *const p_scan_ctx, * @return This API propagates the error code returned by the * SoftDevice API @ref sd_ble_gap_scan_start. */ -int nrf_ble_scan_start(struct nrf_ble_scan const *const p_scan_ctx); +int nrf_ble_scan_start(struct nrf_ble_scan const *const scan_ctx); /** * @brief Function for stopping scanning. @@ -535,15 +541,14 @@ void nrf_ble_scan_stop(void); * @param[in] match_all If this flag is set, all types of enabled filters must be * matched before generating @ref NRF_BLE_SCAN_EVT_FILTER_MATCH to the main application. Otherwise, * it is enough to match one filter to trigger the filter match event. - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] scan_ctx Pointer to the Scanning Module instance. * * @retval NRF_SUCCESS If the filters are enabled successfully. * @retval NRF_ERROR_INVALID_PARAM If the filter mode is incorrect. Available filter modes: @ref * NRF_BLE_SCAN_FILTER_MODE. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. */ -int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const p_scan_ctx, uint8_t mode, - bool match_all); +int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const scan_ctx, uint8_t mode, bool match_all); /** * @brief Function for disabling filtering. @@ -553,40 +558,40 @@ int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const p_scan_ctx, uint8_t m * the connection will not be established with the first device found after this function is called. * - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in] scan_ctx Pointer to the Scanning Module instance. * * @retval NRF_SUCCESS If filters are disabled successfully. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. */ -int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const p_scan_ctx); +int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const scan_ctx); /** * @brief Function for getting filter status. * * @details This function returns the filter setting and whether it is enabled or disabled. - * @param[out] p_status Filter status. - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. + * @param[out] status Filter status. + * @param[in] scan_ctx Pointer to the Scanning Module instance. * * @retval NRF_SUCCESS If filter status is returned. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. */ -int nrf_ble_scan_filter_get(struct nrf_ble_scan *const p_scan_ctx, - struct nrf_ble_scan_filters *p_status); +int nrf_ble_scan_filter_get(struct nrf_ble_scan *const scan_ctx, + struct nrf_ble_scan_filters *status); /** * @brief Function for adding any type of filter to the scanning. * * @details This function adds a new filter by type @ref nrf_ble_scan_filter_type_t. - * The filter will be added if the number of filters of a given type does not exceed @ref - * NRF_BLE_SCAN_UUID_CNT, + * The filter will be added if the number of filters of a given type does not + * exceed @ref NRF_BLE_SCAN_UUID_CNT, * @ref NRF_BLE_SCAN_NAME_CNT, @ref NRF_BLE_SCAN_ADDRESS_CNT, or @ref * NRF_BLE_SCAN_APPEARANCE_CNT, depending on the filter type, and if the same filter has not already * been set. * - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in,out] scan_ctx Pointer to the Scanning Module instance. * @param[in] type Filter type. - * @param[in] p_data The filter data to add. + * @param[in] data The filter data to add. * * @retval NRF_SUCCESS If the filter is added successfully. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. @@ -597,8 +602,8 @@ int nrf_ble_scan_filter_get(struct nrf_ble_scan *const p_scan_ctx, * @ref nrf_ble_scan_filter_type_t. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address type is invalid. */ -int nrf_ble_scan_filter_set(struct nrf_ble_scan *const p_scan_ctx, - enum nrf_ble_scan_filter_type type, void const *p_data); +int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_scan_filter_type type, + void const *data); /** * @brief Function for removing all set filters. @@ -607,11 +612,11 @@ int nrf_ble_scan_filter_set(struct nrf_ble_scan *const p_scan_ctx, * * @note After using this function the filters are still enabled. * - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. + * @param[in,out] scan_ctx Pointer to the Scanning Module instance. * * @retval NRF_SUCCESS If all filters are removed successfully. */ -int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const p_scan_ctx); +int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const scan_ctx); #endif // CONFIG_BT_SCAN_FILTER_ENABLE @@ -627,22 +632,22 @@ int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const p_scan_ctx); *parameters, so that the whitelist is not used, and you avoid the error caused by scanning with the *whitelist when there are no devices on the whitelist. * - * @param[in,out] p_scan_ctx Pointer to the Scanning Module instance. - * @param[in] p_scan_param GAP scanning parameters. Can be initialized as NULL. + * @param[in,out] scan_ctx Pointer to the Scanning Module instance. + * @param[in] scan_param GAP scanning parameters. Can be initialized as NULL. * * @retval NRF_SUCCESS If parameters are changed successfully. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. */ -int nrf_ble_scan_params_set(struct nrf_ble_scan *const p_scan_ctx, - ble_gap_scan_params_t const *p_scan_param); +int nrf_ble_scan_params_set(struct nrf_ble_scan *const scan_ctx, + ble_gap_scan_params_t const *scan_param); /** * @brief Function for handling the BLE stack events of the application. * - * @param[in] p_ble_evt Pointer to the BLE event received. - * @param[in,out] p_scan Pointer to the Scanning Module instance. + * @param[in] ble_evt Pointer to the BLE event received. + * @param[in,out] scan Pointer to the Scanning Module instance. */ -void nrf_ble_scan_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_scan); +void nrf_ble_scan_on_ble_evt(ble_evt_t const *ble_evt, void *scan); /** * @brief Function for converting the raw address to the SoftDevice GAP address. @@ -653,13 +658,13 @@ void nrf_ble_scan_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_scan); * * @note This function does not decode an address type. * - * @param[out] p_gap_addr The Bluetooth Low Energy address. + * @param[out] gap_addr The Bluetooth Low Energy address. * @param[in] addr Address to be converted to the SoftDevice address. * * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. * @retval NRF_SUCCESS If the address is copied and converted successfully. */ -int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *p_gap_addr, +int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *gap_addr, uint8_t const addr[BLE_GAP_ADDR_LEN]); #ifdef __cplusplus @@ -668,4 +673,4 @@ int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *p_gap_addr, #endif // NRF_BLE_SCAN_H__ -/** @} */ \ No newline at end of file +/* @} */ diff --git a/lib/bluetooth/ble_scan/ble_scan.c b/lib/bluetooth/ble_scan/ble_scan.c index d9a51961eb..74b6c1508d 100644 --- a/lib/bluetooth/ble_scan/ble_scan.c +++ b/lib/bluetooth/ble_scan/ble_scan.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ -// #include "sdk_common.h" +/* #include "sdk_common.h"*/ #include #include @@ -14,72 +14,72 @@ #include LOG_MODULE_REGISTER(ble_scan, 3); -// NRF_BLE_SCAN_BUFFER - Data length for an advertising set. +/* NRF_BLE_SCAN_BUFFER - Data length for an advertising set.*/ #ifndef NRF_BLE_SCAN_BUFFER #define NRF_BLE_SCAN_BUFFER 31 #endif -// NRF_BLE_SCAN_NAME_MAX_LEN - Maximum size for the name to search in the advertisement report. +/* NRF_BLE_SCAN_NAME_MAX_LEN - Maximum size for the name to search in the advertisement report.*/ #ifndef NRF_BLE_SCAN_NAME_MAX_LEN #define NRF_BLE_SCAN_NAME_MAX_LEN 32 #endif -// NRF_BLE_SCAN_SHORT_NAME_MAX_LEN - Maximum size of the short name to search for in the -// advertisement report. +/* NRF_BLE_SCAN_SHORT_NAME_MAX_LEN - Maximum size of the short name to search for in the*/ +/* advertisement report.*/ #ifndef NRF_BLE_SCAN_SHORT_NAME_MAX_LEN #define NRF_BLE_SCAN_SHORT_NAME_MAX_LEN 32 #endif -// NRF_BLE_SCAN_SCAN_INTERVAL - Scanning interval. Determines the scan interval in units of -// 0.625 millisecond. +/* NRF_BLE_SCAN_SCAN_INTERVAL - Scanning interval. Determines the scan interval in units of*/ +/* 0.625 millisecond.*/ #ifndef NRF_BLE_SCAN_SCAN_INTERVAL #define NRF_BLE_SCAN_SCAN_INTERVAL 160 #endif -// NRF_BLE_SCAN_SCAN_DURATION - Duration of a scanning session in units of 10 ms. Range: 0x0001 -// - 0xFFFF (10 ms to 10.9225 ms). If set to 0x0000, the scanning continues until it is explicitly -// disabled. +/* NRF_BLE_SCAN_SCAN_DURATION - Duration of a scanning session in units of 10 ms. Range: 0x0001*/ +/* - 0xFFFF (10 ms to 10.9225 ms). If set to 0x0000, the scanning continues until it is explicitly*/ +/* disabled.*/ #ifndef NRF_BLE_SCAN_SCAN_DURATION #define NRF_BLE_SCAN_SCAN_DURATION 0 #endif -// NRF_BLE_SCAN_SCAN_WINDOW - Scanning window. Determines the scanning window in units of 0.625 -// millisecond. +/* NRF_BLE_SCAN_SCAN_WINDOW - Scanning window. Determines the scanning window in units of 0.625*/ +/* millisecond.*/ #ifndef NRF_BLE_SCAN_SCAN_WINDOW #define NRF_BLE_SCAN_SCAN_WINDOW 80 #endif -// NRF_BLE_SCAN_SLAVE_LATENCY - Determines the slave latency in counts of connection events. +/* NRF_BLE_SCAN_SLAVE_LATENCY - Determines the slave latency in counts of connection events.*/ #ifndef NRF_BLE_SCAN_SLAVE_LATENCY #define NRF_BLE_SCAN_SLAVE_LATENCY 5 #endif -static void nrf_ble_scan_connect_with_target(struct nrf_ble_scan const *const p_scan_ctx, - ble_gap_evt_adv_report_t const *const p_adv_report) +static void nrf_ble_scan_connect_with_target(struct nrf_ble_scan const *const scan_ctx, + ble_gap_evt_adv_report_t const *const adv_report) { int err_code; struct scan_evt scan_evt; - // For readability. - ble_gap_addr_t const *p_addr = &p_adv_report->peer_addr; - ble_gap_scan_params_t const *p_scan_params = &p_scan_ctx->scan_params; - ble_gap_conn_params_t const *p_conn_params = &p_scan_ctx->conn_params; - uint8_t con_cfg_tag = p_scan_ctx->conn_cfg_tag; + /* For readability.*/ + ble_gap_addr_t const *addr = &adv_report->peer_addr; + ble_gap_scan_params_t const *scan_params = &scan_ctx->scan_params; + ble_gap_conn_params_t const *conn_params = &scan_ctx->conn_params; + uint8_t con_cfg_tag = scan_ctx->conn_cfg_tag; - // Return if the automatic connection is disabled. - if (!p_scan_ctx->connect_if_match) { + /* Return if the automatic connection is disabled.*/ + if (!scan_ctx->connect_if_match) { return; } - // Stop scanning. + /* Stop scanning.*/ nrf_ble_scan_stop(); memset(&scan_evt, 0, sizeof(scan_evt)); - // Establish connection. - err_code = sd_ble_gap_connect(p_addr, p_scan_params, p_conn_params, con_cfg_tag); + /* Establish connection.*/ + err_code = sd_ble_gap_connect(addr, scan_params, conn_params, con_cfg_tag); if (err_code != NRF_SUCCESS) { - // TODO: Trigger event handler with error + /* TODO: Trigger event handler with error*/ } LOG_DBG("Connecting"); @@ -89,36 +89,36 @@ static void nrf_ble_scan_connect_with_target(struct nrf_ble_scan const *const p_ LOG_DBG("Connection status: %d", err_code); - // If an error occurred, send an event to the event handler. - if ((err_code != NRF_SUCCESS) && (p_scan_ctx->evt_handler != NULL)) { - p_scan_ctx->evt_handler(&scan_evt); + /* If an error occurred, send an event to the event handler.*/ + if ((err_code != NRF_SUCCESS) && (scan_ctx->evt_handler != NULL)) { + scan_ctx->evt_handler(&scan_evt); } } #if CONFIG_BT_SCAN_FILTER_ENABLE || 1 #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) -static bool find_peer_addr(ble_gap_evt_adv_report_t const *const p_adv_report, - ble_gap_addr_t const *p_addr) +static bool find_peer_addr(ble_gap_evt_adv_report_t const *const adv_report, + ble_gap_addr_t const *addr) { - // Compare addresses. - if (memcmp(p_addr->addr, p_adv_report->peer_addr.addr, - sizeof(p_adv_report->peer_addr.addr)) == 0) { + /* Compare addresses.*/ + if (memcmp(addr->addr, adv_report->peer_addr.addr, + sizeof(adv_report->peer_addr.addr)) == 0) { return true; } return false; } -static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const p_adv_report, - struct nrf_ble_scan const *const p_scan_ctx) +static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const adv_report, + struct nrf_ble_scan const *const an_ctx) { - ble_gap_addr_t const *p_addr = p_scan_ctx->scan_filters.addr_filter.target_addr; - uint8_t counter = p_scan_ctx->scan_filters.addr_filter.addr_cnt; + ble_gap_addr_t const *addr = scan_ctx->scan_filters.addr_filter.target_addr; + uint8_t counter = scan_ctx->scan_filters.addr_filter.addr_cnt; for (uint8_t index = 0; index < counter; index++) { /* Search for address. */ - if (find_peer_addr(p_adv_report, &p_addr[index])) { + if (find_peer_addr(adv_report, &addr[index])) { return true; } } @@ -126,60 +126,60 @@ static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const p_adv_report, return false; } -static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const p_scan_ctx, - uint8_t const *p_addr) +static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const scan_ctx, + uint8_t const *addr) { - ble_gap_addr_t *p_addr_filter = p_scan_ctx->scan_filters.addr_filter.target_addr; - uint8_t *p_counter = &p_scan_ctx->scan_filters.addr_filter.addr_cnt; + ble_gap_addr_t *addr_filter = scan_ctx->scan_filters.addr_filter.target_addr; + uint8_t *counter = &scan_ctx->scan_filters.addr_filter.addr_cnt; uint8_t index; /* If no memory for filter. */ - if (*p_counter >= NRF_BLE_SCAN_ADDRESS_CNT) { + if (*counter >= NRF_BLE_SCAN_ADDRESS_CNT) { return NRF_ERROR_NO_MEM; } - // Check for duplicated filter. + /* Check for duplicated filter.*/ for (index = 0; index < NRF_BLE_SCAN_ADDRESS_CNT; index++) { - if (!memcmp(p_addr_filter[index].addr, p_addr, BLE_GAP_ADDR_LEN)) { + if (!memcmp(addr_filter[index].addr, addr, BLE_GAP_ADDR_LEN)) { return NRF_SUCCESS; } } for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; i++) { - p_addr_filter[*p_counter].addr[i] = p_addr[i]; + addr_filter[*counter].addr[i] = addr[i]; } - // Address type is not used so set it to 0. - p_addr_filter[*p_counter].addr_type = 0; + /* Address type is not used so set it to 0.*/ + addr_filter[*counter].addr_type = 0; LOG_DBG("Filter set on address 0x"); - NRF_LOG_HEXDUMP_DEBUG(p_addr_filter[*p_counter].addr, BLE_GAP_ADDR_LEN); + NRF_LOG_HEXDUMP_DEBUG(addr_filter[*counter].addr, BLE_GAP_ADDR_LEN); - // Increase the address filter counter. - *p_counter += 1; + /* Increase the address filter counter.*/ + *counter += 1; return NRF_SUCCESS; } -#endif // NRF_BLE_SCAN_ADDRESS_CNT +#endif /* NRF_BLE_SCAN_ADDRESS_CNT*/ #if (NRF_BLE_SCAN_NAME_CNT > 0) -static bool adv_name_compare(ble_gap_evt_adv_report_t const *p_adv_report, - struct nrf_ble_scan const *const p_scan_ctx) +static bool adv_name_compare(ble_gap_evt_adv_report_t const *adv_report, + struct nrf_ble_scan const *const scan_ctx) { - struct nrf_ble_scan_name_filter const *p_name_filter = - &p_scan_ctx->scan_filters.name_filter; - uint8_t counter = p_scan_ctx->scan_filters.name_filter.name_cnt; + struct nrf_ble_scan_name_filter const *name_filter = + &scan_ctx->scan_filters.name_filter; + uint8_t counter = scan_ctx->scan_filters.name_filter.name_cnt; uint8_t index; uint16_t data_len; - data_len = p_adv_report->data.len; + data_len = adv_report->data.len; - // Compare the name found with the name filter. + /* Compare the name found with the name filter.*/ for (index = 0; index < counter; index++) { - if (ble_advdata_name_find(p_adv_report->data.p_data, data_len, - p_name_filter->target_name[index])) { + if (ble_advdata_name_find(adv_report->data.data, data_len, + name_filter->target_name[index])) { return true; } } @@ -187,58 +187,58 @@ static bool adv_name_compare(ble_gap_evt_adv_report_t const *p_adv_report, return false; } -static int nrf_ble_scan_name_filter_add(struct nrf_ble_scan *const p_scan_ctx, char const *p_name) +static int nrf_ble_scan_name_filter_add(struct nrf_ble_scan *const scan_ctx, char const *name) { uint8_t index; - uint8_t *counter = &p_scan_ctx->scan_filters.name_filter.name_cnt; - uint8_t name_len = strlen(p_name); + uint8_t *counter = &scan_ctx->scan_filters.name_filter.name_cnt; + uint8_t name_len = strlen(name); - // Check the name length. + /* Check the name length.*/ if ((name_len == 0) || (name_len > NRF_BLE_SCAN_NAME_MAX_LEN)) { return NRF_ERROR_DATA_SIZE; } - // If no memory for filter. + /* If no memory for filter.*/ if (*counter >= NRF_BLE_SCAN_NAME_CNT) { return NRF_ERROR_NO_MEM; } - // Check for duplicated filter. + /* Check for duplicated filter.*/ for (index = 0; index < NRF_BLE_SCAN_NAME_CNT; index++) { - if (!strcmp(p_scan_ctx->scan_filters.name_filter.target_name[index], p_name)) { + if (!strcmp(scan_ctx->scan_filters.name_filter.target_name[index], name)) { return NRF_SUCCESS; } } - // Add name to filter. - memcpy(p_scan_ctx->scan_filters.name_filter.target_name[(*counter)++], p_name, - strlen(p_name)); + /* Add name to filter.*/ + memcpy(scan_ctx->scan_filters.name_filter.target_name[(*counter)++], name, + strlen(name)); - LOG_DBG("Adding filter on %s name", p_name); + LOG_DBG("Adding filter on %s name", name); return NRF_SUCCESS; } -#endif // NRF_BLE_SCAN_NAME_CNT +#endif /* NRF_BLE_SCAN_NAME_CNT*/ #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) -static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const p_adv_report, - struct nrf_ble_scan const *const p_scan_ctx) +static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const adv_report, + struct nrf_ble_scan const *const scan_ctx) { - nrf_ble_scan_short_name_filter_t const *p_name_filter = - &p_scan_ctx->scan_filters.short_name_filter; - uint8_t counter = p_scan_ctx->scan_filters.short_name_filter.name_cnt; + nrf_ble_scan_short_name_filter_t const *name_filter = + &scan_ctx->scan_filters.short_name_filter; + uint8_t counter = scan_ctx->scan_filters.short_name_filter.name_cnt; uint8_t index; uint16_t data_len; - data_len = p_adv_report->data.len; + data_len = adv_report->data.len; - // Compare the name found with the name filters. + /* Compare the name found with the name filters.*/ for (index = 0; index < counter; index++) { if (ble_advdata_short_name_find( - p_adv_report->data.p_data, data_len, - p_name_filter->short_name[index].short_target_name, - p_name_filter->short_name[index].short_name_min_len)) { + adv_report->data.data, data_len, + name_filter->short_name[index].short_target_name, + name_filter->short_name[index].short_name_min_len)) { return true; } } @@ -246,40 +246,40 @@ static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const p_adv_r return false; } -static int nrf_ble_scan_short_name_filter_add(struct nrf_ble_scan *const p_scan_ctx, - nrf_ble_scan_short_name_t const *p_short_name) +static int nrf_ble_scan_short_name_filter_add(struct nrf_ble_scan *const scan_ctx, + nrf_ble_scan_short_name_t const *short_name) { uint8_t index; - uint8_t *p_counter = &p_scan_ctx->scan_filters.short_name_filter.name_cnt; - nrf_ble_scan_short_name_filter_t *p_short_name_filter = - &p_scan_ctx->scan_filters.short_name_filter; - uint8_t name_len = strlen(p_short_name->p_short_name); + uint8_t *counter = &scan_ctx->scan_filters.short_name_filter.name_cnt; + nrf_ble_scan_short_name_filter_t *short_name_filter = + &scan_ctx->scan_filters.short_name_filter; + uint8_t name_len = strlen(short_name->short_name); - // Check the name length. + /* Check the name length.*/ if ((name_len == 0) || (name_len > NRF_BLE_SCAN_SHORT_NAME_MAX_LEN)) { return NRF_ERROR_DATA_SIZE; } - // If no memory for filter. - if (*p_counter >= NRF_BLE_SCAN_SHORT_NAME_CNT) { + /* If no memory for filter.*/ + if (*counter >= NRF_BLE_SCAN_SHORT_NAME_CNT) { return NRF_ERROR_NO_MEM; } - // Check for duplicated filter. + /* Check for duplicated filter.*/ for (index = 0; index < NRF_BLE_SCAN_SHORT_NAME_CNT; index++) { - if (!strcmp(p_short_name_filter->short_name[index].short_target_name, - p_short_name->p_short_name)) { + if (!strcmp(short_name_filter->short_name[index].short_target_name, + short_name->short_name)) { return NRF_SUCCESS; } } - // Add name to the filter. - p_short_name_filter->short_name[(*p_counter)].short_name_min_len = - p_short_name->short_name_min_len; - memcpy(p_short_name_filter->short_name[(*p_counter)++].short_target_name, - p_short_name->p_short_name, strlen(p_short_name->p_short_name)); + /* Add name to the filter.*/ + short_name_filter->short_name[(*counter)].short_name_min_len = + short_name->short_name_min_len; + memcpy(short_name_filter->short_name[(*counter)++].short_target_name, + short_name->short_name, strlen(short_name->short_name)); - LOG_DBG("Adding filter on %s name", p_short_name->p_short_name); + LOG_DBG("Adding filter on %s name", short_name->short_name); return NRF_SUCCESS; } @@ -288,36 +288,36 @@ static int nrf_ble_scan_short_name_filter_add(struct nrf_ble_scan *const p_scan_ #if (NRF_BLE_SCAN_UUID_CNT > 0) -static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const p_adv_report, - struct nrf_ble_scan const *const p_scan_ctx) +static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const adv_report, + struct nrf_ble_scan const *const scan_ctx) { - nrf_ble_scan_uuid_filter_t const *p_uuid_filter = &p_scan_ctx->scan_filters.uuid_filter; - bool const all_filters_mode = p_scan_ctx->scan_filters.all_filters_mode; - uint8_t const counter = p_scan_ctx->scan_filters.uuid_filter.uuid_cnt; + nrf_ble_scan_uuid_filter_t const *uuid_filter = &scan_ctx->scan_filters.uuid_filter; + bool const all_filters_mode = scan_ctx->scan_filters.all_filters_mode; + uint8_t const counter = scan_ctx->scan_filters.uuid_filter.uuid_cnt; uint8_t index; uint16_t data_len; uint8_t uuid_match_cnt = 0; - data_len = p_adv_report->data.len; + data_len = adv_report->data.len; for (index = 0; index < counter; index++) { - if (ble_advdata_uuid_find(p_adv_report->data.p_data, data_len, - &p_uuid_filter->uuid[index])) { + if (ble_advdata_uuid_find(adv_report->data.data, data_len, + &uuid_filter->uuid[index])) { uuid_match_cnt++; - // In the normal filter mode, only one UUID is needed to match. + /* In the normal filter mode, only one UUID is needed to match.*/ if (!all_filters_mode) { break; } } else if (all_filters_mode) { break; } else { - // Do nothing. + /* Do nothing.*/ } } - // In the multifilter mode, all UUIDs must be found in the advertisement packets. + /* In the multifilter mode, all UUIDs must be found in the advertisement packets.*/ if ((all_filters_mode && (uuid_match_cnt == counter)) || ((!all_filters_mode) && (uuid_match_cnt > 0))) { return true; @@ -326,83 +326,83 @@ static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const p_adv_report, return false; } -static int nrf_ble_scan_uuid_filter_add(struct nrf_ble_scan *const p_scan_ctx, - ble_uuid_t const *p_uuid) +static int nrf_ble_scan_uuid_filter_add(struct nrf_ble_scan *const scan_ctx, + ble_uuid_t const *uuid) { - ble_uuid_t *p_uuid_filter = p_scan_ctx->scan_filters.uuid_filter.uuid; - uint8_t *p_counter = &p_scan_ctx->scan_filters.uuid_filter.uuid_cnt; + ble_uuid_t *uuid_filter = scan_ctx->scan_filters.uuid_filter.uuid; + uint8_t *counter = &scan_ctx->scan_filters.uuid_filter.uuid_cnt; uint8_t index; - // If no memory. - if (*p_counter >= NRF_BLE_SCAN_UUID_CNT) { + /* If no memory.*/ + if (*counter >= NRF_BLE_SCAN_UUID_CNT) { return NRF_ERROR_NO_MEM; } - // Check for duplicated filter. + /* Check for duplicated filter.*/ for (index = 0; index < NRF_BLE_SCAN_UUID_CNT; index++) { - if (p_uuid_filter[index].uuid == p_uuid->uuid) { + if (uuid_filter[index].uuid == uuid->uuid) { return NRF_SUCCESS; } } - // Add UUID to the filter. - p_uuid_filter[(*p_counter)++] = *p_uuid; - LOG_DBG("Added filter on UUID %x", p_uuid->uuid); + /* Add UUID to the filter.*/ + uuid_filter[(*counter)++] = *uuid; + LOG_DBG("Added filter on UUID %x", uuid->uuid); return NRF_SUCCESS; } -#endif // NRF_BLE_SCAN_UUID_CNT +#endif /* NRF_BLE_SCAN_UUID_CNT*/ #if (NRF_BLE_SCAN_APPEARANCE_CNT) -static bool adv_appearance_compare(ble_gap_evt_adv_report_t const *const p_adv_report, - struct nrf_ble_scan const *const p_scan_ctx) +static bool adv_appearance_compare(ble_gap_evt_adv_report_t const *const adv_report, + struct nrf_ble_scan const *const scan_ctx) { - nrf_ble_scan_appearance_filter_t const *p_appearance_filter = - &p_scan_ctx->scan_filters.appearance_filter; - uint8_t const counter = p_scan_ctx->scan_filters.appearance_filter.appearance_cnt; + nrf_ble_scan_appearance_filter_t const *appearance_filter = + &scan_ctx->scan_filters.appearance_filter; + uint8_t const counter = scan_ctx->scan_filters.appearance_filter.appearance_cnt; uint8_t index; uint16_t data_len; - data_len = p_adv_report->data.len; + data_len = adv_report->data.len; - // Verify if the advertised appearance matches the provided appearance. + /* Verify if the advertised appearance matches the provided appearance.*/ for (index = 0; index < counter; index++) { - if (ble_advdata_appearance_find(p_adv_report->data.p_data, data_len, - &p_appearance_filter->appearance[index])) { + if (ble_advdata_appearance_find(adv_report->data.data, data_len, + &appearance_filter->appearance[index])) { return true; } } return false; } -static int nrf_ble_scan_appearance_filter_add(struct nrf_ble_scan *const p_scan_ctx, +static int nrf_ble_scan_appearance_filter_add(struct nrf_ble_scan *const scan_ctx, uint16_t appearance) { - uint16_t *p_appearance_filter = p_scan_ctx->scan_filters.appearance_filter.appearance; - uint8_t *p_counter = &p_scan_ctx->scan_filters.appearance_filter.appearance_cnt; + uint16_t *appearance_filter = scan_ctx->scan_filters.appearance_filter.appearance; + uint8_t *counter = &scan_ctx->scan_filters.appearance_filter.appearance_cnt; uint8_t index; - // If no memory. - if (*p_counter >= NRF_BLE_SCAN_APPEARANCE_CNT) { + /* If no memory.*/ + if (*counter >= NRF_BLE_SCAN_APPEARANCE_CNT) { return NRF_ERROR_NO_MEM; } - // Check for duplicated filter. + /* Check for duplicated filter.*/ for (index = 0; index < NRF_BLE_SCAN_APPEARANCE_CNT; index++) { - if (p_appearance_filter[index] == appearance) { + if (appearance_filter[index] == appearance) { return NRF_SUCCESS; } } - // Add appearance to the filter. - p_appearance_filter[(*p_counter)++] = appearance; + /* Add appearance to the filter.*/ + appearance_filter[(*counter)++] = appearance; LOG_DBG("Added filter on appearance %x", appearance); return NRF_SUCCESS; } -#endif // NRF_BLE_SCAN_APPEARANCE_CNT +#endif /* NRF_BLE_SCAN_APPEARANCE_CNT*/ int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_scan_filter_type type, void const *data) @@ -455,38 +455,38 @@ int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_sc } } -int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const p_scan_ctx) +int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const scan_ctx) { #if (NRF_BLE_SCAN_NAME_CNT > 0) - struct nrf_ble_scan_name_filter *p_name_filter = &p_scan_ctx->scan_filters.name_filter; - memset(p_name_filter->target_name, 0, sizeof(p_name_filter->target_name)); - p_name_filter->name_cnt = 0; + struct nrf_ble_scan_name_filter *name_filter = &scan_ctx->scan_filters.name_filter; + memset(name_filter->target_name, 0, sizeof(name_filter->target_name)); + name_filter->name_cnt = 0; #endif #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) - nrf_ble_scan_short_name_filter_t *p_short_name_filter = - &p_scan_ctx->scan_filters.short_name_filter; - memset(p_short_name_filter->short_name, 0, sizeof(p_short_name_filter->short_name)); - p_short_name_filter->name_cnt = 0; + nrf_ble_scan_short_name_filter_t *short_name_filter = + &scan_ctx->scan_filters.short_name_filter; + memset(short_name_filter->short_name, 0, sizeof(short_name_filter->short_name)); + short_name_filter->name_cnt = 0; #endif #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) - nrf_ble_scan_addr_filter_t *p_addr_filter = &p_scan_ctx->scan_filters.addr_filter; - memset(p_addr_filter->target_addr, 0, sizeof(p_addr_filter->target_addr)); - p_addr_filter->addr_cnt = 0; + nrf_ble_scan_addr_filter_t *addr_filter = &scan_ctx->scan_filters.addr_filter; + memset(addr_filter->target_addr, 0, sizeof(addr_filter->target_addr)); + addr_filter->addr_cnt = 0; #endif #if (NRF_BLE_SCAN_UUID_CNT > 0) - nrf_ble_scan_uuid_filter_t *p_uuid_filter = &p_scan_ctx->scan_filters.uuid_filter; - memset(p_uuid_filter->uuid, 0, sizeof(p_uuid_filter->uuid)); - p_uuid_filter->uuid_cnt = 0; + nrf_ble_scan_uuid_filter_t *uuid_filter = &scan_ctx->scan_filters.uuid_filter; + memset(uuid_filter->uuid, 0, sizeof(uuid_filter->uuid)); + uuid_filter->uuid_cnt = 0; #endif #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) - nrf_ble_scan_appearance_filter_t *p_appearance_filter = - &p_scan_ctx->scan_filters.appearance_filter; - memset(p_appearance_filter->appearance, 0, sizeof(p_appearance_filter->appearance)); - p_appearance_filter->appearance_cnt = 0; + nrf_ble_scan_appearance_filter_t *appearance_filter = + &scan_ctx->scan_filters.appearance_filter; + memset(appearance_filter->appearance, 0, sizeof(appearance_filter->appearance)); + appearance_filter->appearance_cnt = 0; #endif return NRF_SUCCESS; @@ -494,12 +494,12 @@ int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const p_scan_ctx) int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const scan_ctx, uint8_t mode, bool match_all) { - // VERIFY_PARAM_NOT_NULL(p_scan_ctx); + /* VERIFY_PARAM_NOT_NULL(scan_ctx);*/ if (scan_ctx == NULL) { return NRF_ERROR_NULL; } - // Check if the mode is correct. + /* Check if the mode is correct.*/ if ((!(mode & NRF_BLE_SCAN_ADDR_FILTER)) && (!(mode & NRF_BLE_SCAN_NAME_FILTER)) && (!(mode & NRF_BLE_SCAN_UUID_FILTER)) && (!(mode & NRF_BLE_SCAN_SHORT_NAME_FILTER)) && (!(mode & NRF_BLE_SCAN_APPEARANCE_FILTER))) { @@ -508,16 +508,16 @@ int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const scan_ctx, uint8_t mod int err_code; - // Disable filters. + /* Disable filters.*/ err_code = nrf_ble_scan_filters_disable(scan_ctx); - // ASSERT(err_code == NRF_SUCCESS); + /* ASSERT(err_code == NRF_SUCCESS);*/ struct nrf_ble_scan_filters *filters = &scan_ctx->scan_filters; - // Turn on the filters of your choice. + /* Turn on the filters of your choice.*/ #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) if (mode & NRF_BLE_SCAN_ADDR_FILTER) { - p_filters->addr_filter.addr_filter_enabled = true; + filters->addr_filter.addr_filter_enabled = true; } #endif @@ -529,54 +529,54 @@ int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const scan_ctx, uint8_t mod #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) if (mode & NRF_BLE_SCAN_SHORT_NAME_FILTER) { - p_filters->short_name_filter.short_name_filter_enabled = true; + filters->short_name_filter.short_name_filter_enabled = true; } #endif #if (NRF_BLE_SCAN_UUID_CNT > 0) if (mode & NRF_BLE_SCAN_UUID_FILTER) { - p_filters->uuid_filter.uuid_filter_enabled = true; + filters->uuid_filter.uuid_filter_enabled = true; } #endif #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) if (mode & NRF_BLE_SCAN_APPEARANCE_FILTER) { - p_filters->appearance_filter.appearance_filter_enabled = true; + filters->appearance_filter.appearance_filter_enabled = true; } #endif - // Select the filter mode. + /* Select the filter mode.*/ filters->all_filters_mode = match_all; return NRF_SUCCESS; } -int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const p_scan_ctx) +int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const scan_ctx) { - if (!p_scan_ctx) { + if (!scan_ctx) { return NRF_ERROR_NULL; } - // Disable all filters. + /* Disable all filters.*/ #if (NRF_BLE_SCAN_NAME_CNT > 0) - bool *p_name_filter_enabled = &p_scan_ctx->scan_filters.name_filter.name_filter_enabled; - *p_name_filter_enabled = false; + bool *name_filter_enabled = &scan_ctx->scan_filters.name_filter.name_filter_enabled; + *name_filter_enabled = false; #endif #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) - bool *p_addr_filter_enabled = &p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled; - *p_addr_filter_enabled = false; + bool *addr_filter_enabled = &scan_ctx->scan_filters.addr_filter.addr_filter_enabled; + *addr_filter_enabled = false; #endif #if (NRF_BLE_SCAN_UUID_CNT > 0) - bool *p_uuid_filter_enabled = &p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled; - *p_uuid_filter_enabled = false; + bool *uuid_filter_enabled = &scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled; + *uuid_filter_enabled = false; #endif #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) - bool *p_appearance_filter_enabled = - &p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled; - *p_appearance_filter_enabled = false; + bool *appearance_filter_enabled = + &scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled; + *appearance_filter_enabled = false; #endif return NRF_SUCCESS; @@ -597,75 +597,67 @@ int nrf_ble_scan_filter_get(struct nrf_ble_scan *const scan_ctx, return NRF_SUCCESS; } -#endif // CONFIG_BT_SCAN_FILTER_ENABLE +#endif /* CONFIG_BT_SCAN_FILTER_ENABLE*/ -/**@brief Function for calling the BLE_GAP_EVT_ADV_REPORT event to check whether the received - * scanning data matches the scan configuration. - * - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. - * @param[in] p_adv_report Advertising report. - */ -static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const p_scan_ctx, - ble_gap_evt_adv_report_t const *const p_adv_report) +static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const scan_ctx, + ble_gap_evt_adv_report_t const *const adv_report) { - struct scan_evt scan_evt; + struct scan_evt scan_evt = {0}; #if CONFIG_BT_SCAN_FILTER_ENABLE || 1 uint8_t filter_cnt = 0; uint8_t filter_match_cnt = 0; #endif - memset(&scan_evt, 0, sizeof(scan_evt)); - - scan_evt.p_scan_params = &p_scan_ctx->scan_params; + scan_evt.scan_params = &scan_ctx->scan_params; - // If the whitelist is used, do not check the filters and return. - if (is_whitelist_used(p_scan_ctx)) { + /* If the whitelist is used, do not check the filters and return.*/ + if (is_whitelist_used(scan_ctx)) { scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT; - scan_evt.params.p_not_found = p_adv_report; - p_scan_ctx->evt_handler(&scan_evt); + scan_evt.params.not_found = adv_report; + scan_ctx->evt_handler(&scan_evt); - sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer); - nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report); + sd_ble_gap_scan_start(NULL, &scan_ctx->scan_buffer); + nrf_ble_scan_connect_with_target(scan_ctx, adv_report); return; } #if BT_SCAN_FILTER_ENABLE - bool const all_filter_mode = p_scan_ctx->scan_filters.all_filters_mode; + bool const all_filter_mode = scan_ctx->scan_filters.all_filters_mode; bool is_filter_matched = false; #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) - bool const addr_filter_enabled = p_scan_ctx->scan_filters.addr_filter.addr_filter_enabled; + bool const addr_filter_enabled = scan_ctx->scan_filters.addr_filter.addr_filter_enabled; #endif #if (NRF_BLE_SCAN_NAME_CNT > 0) - bool const name_filter_enabled = p_scan_ctx->scan_filters.name_filter.name_filter_enabled; + bool const name_filter_enabled = scan_ctx->scan_filters.name_filter.name_filter_enabled; #endif #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) bool const short_name_filter_enabled = - p_scan_ctx->scan_filters.short_name_filter.short_name_filter_enabled; + scan_ctx->scan_filters.short_name_filter.short_name_filter_enabled; #endif #if (NRF_BLE_SCAN_UUID_CNT > 0) - bool const uuid_filter_enabled = p_scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled; + bool const uuid_filter_enabled = scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled; #endif #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) bool const appearance_filter_enabled = - p_scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled; + scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled; #endif #if (NRF_BLE_SCAN_ADDRESS_CNT > 0) - // Check the address filter. + /* Check the address filter.*/ if (addr_filter_enabled) { - // Number of active filters. + /* Number of active filters.*/ filter_cnt++; - if (adv_addr_compare(p_adv_report, p_scan_ctx)) { - // Number of filters matched. + if (adv_addr_compare(adv_report, scan_ctx)) { + /* Number of filters matched.*/ filter_match_cnt++; - // Information about the filters matched. + /* Information about the filters matched.*/ scan_evt.params.filter_match.filter_match.address_filter_match = true; is_filter_matched = true; } @@ -673,13 +665,13 @@ static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const p_scan_c #endif #if (NRF_BLE_SCAN_NAME_CNT > 0) - // Check the name filter. + /* Check the name filter.*/ if (name_filter_enabled) { filter_cnt++; - if (adv_name_compare(p_adv_report, p_scan_ctx)) { + if (adv_name_compare(adv_report, scan_ctx)) { filter_match_cnt++; - // Information about the filters matched. + /* Information about the filters matched.*/ scan_evt.params.filter_match.filter_match.name_filter_match = true; is_filter_matched = true; } @@ -689,10 +681,10 @@ static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const p_scan_c #if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) if (short_name_filter_enabled) { filter_cnt++; - if (adv_short_name_compare(p_adv_report, p_scan_ctx)) { + if (adv_short_name_compare(adv_report, scan_ctx)) { filter_match_cnt++; - // Information about the filters matched. + /* Information about the filters matched.*/ scan_evt.params.filter_match.filter_match.short_name_filter_match = true; is_filter_matched = true; } @@ -700,12 +692,12 @@ static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const p_scan_c #endif #if (NRF_BLE_SCAN_UUID_CNT > 0) - // Check the UUID filter. + /* Check the UUID filter.*/ if (uuid_filter_enabled) { filter_cnt++; - if (adv_uuid_compare(p_adv_report, p_scan_ctx)) { + if (adv_uuid_compare(adv_report, scan_ctx)) { filter_match_cnt++; - // Information about the filters matched. + /* Information about the filters matched.*/ scan_evt.params.filter_match.filter_match.uuid_filter_match = true; is_filter_matched = true; } @@ -713,12 +705,12 @@ static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const p_scan_c #endif #if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) - // Check the appearance filter. + /* Check the appearance filter.*/ if (appearance_filter_enabled) { filter_cnt++; - if (adv_appearance_compare(p_adv_report, p_scan_ctx)) { + if (adv_appearance_compare(adv_report, scan_ctx)) { filter_match_cnt++; - // Information about the filters matched. + /* Information about the filters matched.*/ scan_evt.params.filter_match.filter_match.appearance_filter_match = true; is_filter_matched = true; } @@ -727,39 +719,35 @@ static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const p_scan_c scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND; #endif - scan_evt.params.filter_match.p_adv_report = p_adv_report; + scan_evt.params.filter_match.adv_report = adv_report; - // In the multifilter mode, the number of the active filters must equal the number of the - // filters matched to generate the notification. + /* In the multifilter mode, the number of the active filters must equal the number of the*/ + /* filters matched to generate the notification.*/ if (all_filter_mode && (filter_match_cnt == filter_cnt)) { scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH; - nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report); + nrf_ble_scan_connect_with_target(scan_ctx, adv_report); } - // In the normal filter mode, only one filter match is needed to generate the notification - // to the main application. + /* In the normal filter mode, only one filter match is needed to generate the notification*/ + /* to the main application.*/ else if ((!all_filter_mode) && is_filter_matched) { scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH; - nrf_ble_scan_connect_with_target(p_scan_ctx, p_adv_report); + nrf_ble_scan_connect_with_target(scan_ctx, adv_report); } else { scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND; - scan_evt.params.p_not_found = p_adv_report; + scan_evt.params.not_found = adv_report; } - // If the event handler is not NULL, notify the main application. - if (p_scan_ctx->evt_handler != NULL) { - p_scan_ctx->evt_handler(&scan_evt); + /* If the event handler is not NULL, notify the main application.*/ + if (scan_ctx->evt_handler != NULL) { + scan_ctx->evt_handler(&scan_evt); } -#endif // BT_SCAN_FILTER_ENABLE +#endif /* BT_SCAN_FILTER_ENABLE*/ - // Resume the scanning. - (void)sd_ble_gap_scan_start(NULL, &p_scan_ctx->scan_buffer); + /* Resume the scanning.*/ + (void)sd_ble_gap_scan_start(NULL, &scan_ctx->scan_buffer); } -/**@brief Function for checking whether the whitelist is used. - * - * @param[in] p_scan_ctx Scanning Module instance. - */ bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx) { if (scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST || @@ -773,19 +761,19 @@ bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx) /**@brief Function for restoring the default scanning parameters. * - * @param[out] p_scan_ctx Pointer to the Scanning Module instance. + * @param[out] scan_ctx Pointer to the Scanning Module instance. */ static void nrf_ble_scan_default_param_set(struct nrf_ble_scan *const scan_ctx) { - // Set the default parameters. + /* Set the default parameters.*/ scan_ctx->scan_params.active = 1; #if (NRF_SD_BLE_API_VERSION > 7) - p_scan_ctx->scan_params.interval_us = NRF_BLE_SCAN_SCAN_INTERVAL * UNIT_0_625_MS; - p_scan_ctx->scan_params.window_us = NRF_BLE_SCAN_SCAN_WINDOW * UNIT_0_625_MS; + scan_ctx->scan_params.interval_us = NRF_BLE_SCAN_SCAN_INTERVAL * UNIT_0_625_MS; + scan_ctx->scan_params.window_us = NRF_BLE_SCAN_SCAN_WINDOW * UNIT_0_625_MS; #else scan_ctx->scan_params.interval = NRF_BLE_SCAN_SCAN_INTERVAL; scan_ctx->scan_params.window = NRF_BLE_SCAN_SCAN_WINDOW; -#endif // #if (NRF_SD_BLE_API_VERSION > 7) +#endif /* #if (NRF_SD_BLE_API_VERSION > 7)*/ scan_ctx->scan_params.timeout = NRF_BLE_SCAN_SCAN_DURATION; scan_ctx->scan_params.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL; scan_ctx->scan_params.scan_phys = BLE_GAP_PHY_1MBPS; @@ -793,7 +781,7 @@ static void nrf_ble_scan_default_param_set(struct nrf_ble_scan *const scan_ctx) /**@brief Function for setting the default connection parameters. * - * @param[out] p_scan_ctx Pointer to the Scanning Module instance. + * @param[out] scan_ctx Pointer to the Scanning Module instance. */ static void nrf_ble_scan_default_conn_param_set(struct nrf_ble_scan *const scan_ctx) { @@ -805,8 +793,8 @@ static void nrf_ble_scan_default_conn_param_set(struct nrf_ble_scan *const scan_ /**@brief Function for calling the BLE_GAP_EVT_TIMEOUT event. * - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. - * @param[in] p_gap GAP event structure. + * @param[in] scan_ctx Pointer to the Scanning Module instance. + * @param[in] gap GAP event structure. */ static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, ble_gap_evt_t const *const gap) @@ -818,7 +806,7 @@ static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, LOG_DBG("BLE_GAP_SCAN_TIMEOUT"); if (scan_ctx->evt_handler != NULL) { scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_SCAN_TIMEOUT; - scan_evt.p_scan_params = &scan_ctx->scan_params; + scan_evt.scan_params = &scan_ctx->scan_params; scan_evt.params.timeout.src = timeout->src; scan_ctx->evt_handler(&scan_evt); @@ -830,8 +818,8 @@ static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, */ void nrf_ble_scan_stop(void) { - // It is ok to ignore the function return value here, because this function can return - // NRF_SUCCESS or NRF_ERROR_INVALID_STATE, when app is not in the scanning state. + /* It is ok to ignore the function return value here, because this function can return*/ + /* NRF_SUCCESS or NRF_ERROR_INVALID_STATE, when app is not in the scanning state.*/ (void)sd_ble_gap_scan_stop(); } @@ -846,30 +834,30 @@ int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, scan_ctx->evt_handler = evt_handler; #if BT_SCAN_FILTER_ENABLE - // Disable all scanning filters. - memset(&p_scan_ctx->scan_filters, 0, sizeof(p_scan_ctx->scan_filters)); + /* Disable all scanning filters.*/ + memset(&scan_ctx->scan_filters, 0, sizeof(scan_ctx->scan_filters)); #endif - // If the pointer to the initialization structure exist, use it to scan the configuration. + /* If the pointer to the initialization structure exist, use it to scan the configuration.*/ if (init != NULL) { scan_ctx->connect_if_match = init->connect_if_match; scan_ctx->conn_cfg_tag = init->conn_cfg_tag; - if (init->p_scan_param != NULL) { - scan_ctx->scan_params = *init->p_scan_param; + if (init->scan_param != NULL) { + scan_ctx->scan_params = *init->scan_param; } else { - // Use the default static configuration. + /* Use the default static configuration.*/ nrf_ble_scan_default_param_set(scan_ctx); } - if (init->p_conn_param != NULL) { - scan_ctx->conn_params = *init->p_conn_param; + if (init->conn_param != NULL) { + scan_ctx->conn_params = *init->conn_param; } else { - // Use the default static configuration. + /* Use the default static configuration.*/ nrf_ble_scan_default_conn_param_set(scan_ctx); } } - // If pointer is NULL, use the static default configuration. + /* If pointer is NULL, use the static default configuration.*/ else { nrf_ble_scan_default_param_set(scan_ctx); nrf_ble_scan_default_conn_param_set(scan_ctx); @@ -877,19 +865,18 @@ int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, scan_ctx->connect_if_match = false; } - // Assign a buffer where the advertising reports are to be stored by the SoftDevice. - scan_ctx->scan_buffer.p_data = scan_ctx->scan_buffer_data; + /* Assign a buffer where the advertising reports are to be stored by the SoftDevice.*/ + scan_ctx->scan_buffer.data = scan_ctx->scan_buffer_data; scan_ctx->scan_buffer.len = NRF_BLE_SCAN_BUFFER; return NRF_SUCCESS; } -int nrf_ble_scan_start(struct nrf_ble_scan const *const p_scan_ctx) +int nrf_ble_scan_start(struct nrf_ble_scan const *const scan_ctx) { - // VERIFY_PARAM_NOT_NULL(p_scan_ctx); - if (p_scan_ctx == NULL) { - LOG_ERR("Scan ctx is NULL"); + if (!scan_ctx) { + return NRF_ERROR_NULL; } int err_code; @@ -897,19 +884,19 @@ int nrf_ble_scan_start(struct nrf_ble_scan const *const p_scan_ctx) nrf_ble_scan_stop(); - // If the whitelist is used and the event handler is not NULL, send the whitelist request to - // the main application. - if (is_whitelist_used(p_scan_ctx)) { - if (p_scan_ctx->evt_handler != NULL) { + /* If the whitelist is used and the event handler is not NULL, send the whitelist request to*/ + /* the main application.*/ + if (is_whitelist_used(scan_ctx)) { + if (scan_ctx->evt_handler != NULL) { scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_REQUEST; - p_scan_ctx->evt_handler(&scan_evt); + scan_ctx->evt_handler(&scan_evt); } } - // Start the scanning. - err_code = sd_ble_gap_scan_start(&p_scan_ctx->scan_params, &p_scan_ctx->scan_buffer); + /* Start the scanning.*/ + err_code = sd_ble_gap_scan_start(&scan_ctx->scan_params, &scan_ctx->scan_buffer); - // It is okay to ignore this error, because the scan stopped earlier. + /* It is okay to ignore this error, because the scan stopped earlier.*/ if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_SUCCESS)) { LOG_ERR("sd_ble_gap_scan_start returned 0x%x", err_code); return (err_code); @@ -919,18 +906,21 @@ int nrf_ble_scan_start(struct nrf_ble_scan const *const p_scan_ctx) return NRF_SUCCESS; } -int nrf_ble_scan_params_set(struct nrf_ble_scan *const p_scan_ctx, - ble_gap_scan_params_t const *const p_scan_param) +int nrf_ble_scan_params_set(struct nrf_ble_scan *const scan_ctx, + ble_gap_scan_params_t const *const scan_param) { - // VERIFY_PARAM_NOT_NULL(p_scan_ctx); + /* VERIFY_PARAM_NOT_NULL(scan_ctx);*/ + if (!scan_ctx) { + return NRF_ERROR_NULL; + } nrf_ble_scan_stop(); - if (p_scan_param != NULL) { - // Assign new scanning parameters. - p_scan_ctx->scan_params = *p_scan_param; + if (scan_param != NULL) { + /* Assign new scanning parameters.*/ + scan_ctx->scan_params = *scan_param; } else { - // If NULL, use the default static configuration. - nrf_ble_scan_default_param_set(p_scan_ctx); + /* If NULL, use the default static configuration.*/ + nrf_ble_scan_default_param_set(scan_ctx); } LOG_DBG("Scanning parameters have been changed successfully"); @@ -940,54 +930,55 @@ int nrf_ble_scan_params_set(struct nrf_ble_scan *const p_scan_ctx, /**@brief Function for calling the BLE_GAP_EVT_CONNECTED event. * - * @param[in] p_scan_ctx Pointer to the Scanning Module instance. - * @param[in] p_gap_evt GAP event structure. + * @param[in] scan_ctx Pointer to the Scanning Module instance. + * @param[in] gap_evt GAP event structure. */ -static void nrf_ble_scan_on_connected_evt(struct nrf_ble_scan const *const p_scan_ctx, - ble_gap_evt_t const *const p_gap_evt) +static void nrf_ble_scan_on_connected_evt(struct nrf_ble_scan const *const scan_ctx, + ble_gap_evt_t const *const gap_evt) { - struct scan_evt scan_evt; + struct scan_evt scan_evt = {0}; - memset(&scan_evt, 0, sizeof(scan_evt)); scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTED; - scan_evt.params.connected.p_connected = &p_gap_evt->params.connected; - scan_evt.params.connected.conn_handle = p_gap_evt->conn_handle; - scan_evt.p_scan_params = &p_scan_ctx->scan_params; + scan_evt.params.connected.connected = &gap_evt->params.connected; + scan_evt.params.connected.conn_handle = gap_evt->conn_handle; + scan_evt.scan_params = &scan_ctx->scan_params; - if (p_scan_ctx->evt_handler != NULL) { - p_scan_ctx->evt_handler(&scan_evt); + if (scan_ctx->evt_handler != NULL) { + scan_ctx->evt_handler(&scan_evt); } } -int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *p_gap_addr, +int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *gap_addr, const uint8_t addr[BLE_GAP_ADDR_LEN]) { - // VERIFY_PARAM_NOT_NULL(p_gap_addr); + if (!gap_addr) { + return NRF_ERROR_NULL; + } for (uint8_t i = 0; i < BLE_GAP_ADDR_LEN; ++i) { - p_gap_addr->addr[i] = addr[BLE_GAP_ADDR_LEN - (i + 1)]; + gap_addr->addr[i] = addr[BLE_GAP_ADDR_LEN - (i + 1)]; } return NRF_SUCCESS; } -void nrf_ble_scan_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_contex) +void nrf_ble_scan_on_ble_evt(ble_evt_t const *ble_evt, void *contex) { - struct nrf_ble_scan *p_scan_data = (struct nrf_ble_scan *)p_contex; - ble_gap_evt_adv_report_t const *p_adv_report = &p_ble_evt->evt.gap_evt.params.adv_report; - ble_gap_evt_t const *p_gap_evt = &p_ble_evt->evt.gap_evt; + struct nrf_ble_scan *scan_data = (struct nrf_ble_scan *)contex; + ble_gap_evt_adv_report_t const *adv_report = &ble_evt->evt.gap_evt.params.adv_report; + ble_gap_evt_t const *gap_evt = &ble_evt->evt.gap_evt; - switch (p_ble_evt->header.evt_id) { + switch (ble_evt->header.evt_id) { case BLE_GAP_EVT_ADV_REPORT: - nrf_ble_scan_on_adv_report(p_scan_data, p_adv_report); + nrf_ble_scan_on_adv_report(scan_data, adv_report); break; case BLE_GAP_EVT_TIMEOUT: - nrf_ble_scan_on_timeout(p_scan_data, p_gap_evt); + nrf_ble_scan_on_timeout(scan_data, gap_evt); break; case BLE_GAP_EVT_CONNECTED: - nrf_ble_scan_on_connected_evt(p_scan_data, p_gap_evt); + nrf_ble_scan_on_connected_evt(scan_data, gap_evt); break; default: From 3d0ab11b028cc01b48840dc0ad197fbaa0589254 Mon Sep 17 00:00:00 2001 From: Asil Zogby Date: Tue, 14 Oct 2025 16:26:33 +0200 Subject: [PATCH 4/6] lib: scan: remove unnecessary comments and doxygen Signed-off-by: Asil Zogby --- include/bm/bluetooth/ble_scan.h | 1 - lib/bluetooth/ble_scan/ble_scan.c | 43 +++++++------------------------ 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/include/bm/bluetooth/ble_scan.h b/include/bm/bluetooth/ble_scan.h index d5e95e6fd2..d8f2219cd0 100644 --- a/include/bm/bluetooth/ble_scan.h +++ b/include/bm/bluetooth/ble_scan.h @@ -9,7 +9,6 @@ * * @defgroup nrf_ble_scan Scanning Module * @{ - * @ingroup ble_sdk_lib * @brief Module for handling the BLE scanning. * * @details The Scanning Module handles the BLE scanning for your application. diff --git a/lib/bluetooth/ble_scan/ble_scan.c b/lib/bluetooth/ble_scan/ble_scan.c index 74b6c1508d..0a03aecd59 100644 --- a/lib/bluetooth/ble_scan/ble_scan.c +++ b/lib/bluetooth/ble_scan/ble_scan.c @@ -102,8 +102,8 @@ static bool find_peer_addr(ble_gap_evt_adv_report_t const *const adv_report, ble_gap_addr_t const *addr) { /* Compare addresses.*/ - if (memcmp(addr->addr, adv_report->peer_addr.addr, - sizeof(adv_report->peer_addr.addr)) == 0) { + if (memcmp(addr->addr, adv_report->peer_addr.addr, sizeof(adv_report->peer_addr.addr)) == + 0) { return true; } @@ -126,8 +126,7 @@ static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const adv_report, return false; } -static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const scan_ctx, - uint8_t const *addr) +static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const scan_ctx, uint8_t const *addr) { ble_gap_addr_t *addr_filter = scan_ctx->scan_filters.addr_filter.target_addr; uint8_t *counter = &scan_ctx->scan_filters.addr_filter.addr_cnt; @@ -168,8 +167,7 @@ static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const scan_ctx, static bool adv_name_compare(ble_gap_evt_adv_report_t const *adv_report, struct nrf_ble_scan const *const scan_ctx) { - struct nrf_ble_scan_name_filter const *name_filter = - &scan_ctx->scan_filters.name_filter; + struct nrf_ble_scan_name_filter const *name_filter = &scan_ctx->scan_filters.name_filter; uint8_t counter = scan_ctx->scan_filters.name_filter.name_cnt; uint8_t index; uint16_t data_len; @@ -211,8 +209,7 @@ static int nrf_ble_scan_name_filter_add(struct nrf_ble_scan *const scan_ctx, cha } /* Add name to filter.*/ - memcpy(scan_ctx->scan_filters.name_filter.target_name[(*counter)++], name, - strlen(name)); + memcpy(scan_ctx->scan_filters.name_filter.target_name[(*counter)++], name, strlen(name)); LOG_DBG("Adding filter on %s name", name); @@ -326,8 +323,7 @@ static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const adv_report, return false; } -static int nrf_ble_scan_uuid_filter_add(struct nrf_ble_scan *const scan_ctx, - ble_uuid_t const *uuid) +static int nrf_ble_scan_uuid_filter_add(struct nrf_ble_scan *const scan_ctx, ble_uuid_t const *uuid) { ble_uuid_t *uuid_filter = scan_ctx->scan_filters.uuid_filter.uuid; uint8_t *counter = &scan_ctx->scan_filters.uuid_filter.uuid_cnt; @@ -494,8 +490,7 @@ int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const scan_ctx) int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const scan_ctx, uint8_t mode, bool match_all) { - /* VERIFY_PARAM_NOT_NULL(scan_ctx);*/ - if (scan_ctx == NULL) { + if (!scan_ctx) { return NRF_ERROR_NULL; } @@ -759,10 +754,6 @@ bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx) return false; } -/**@brief Function for restoring the default scanning parameters. - * - * @param[out] scan_ctx Pointer to the Scanning Module instance. - */ static void nrf_ble_scan_default_param_set(struct nrf_ble_scan *const scan_ctx) { /* Set the default parameters.*/ @@ -779,10 +770,6 @@ static void nrf_ble_scan_default_param_set(struct nrf_ble_scan *const scan_ctx) scan_ctx->scan_params.scan_phys = BLE_GAP_PHY_1MBPS; } -/**@brief Function for setting the default connection parameters. - * - * @param[out] scan_ctx Pointer to the Scanning Module instance. - */ static void nrf_ble_scan_default_conn_param_set(struct nrf_ble_scan *const scan_ctx) { scan_ctx->conn_params.conn_sup_timeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN; @@ -791,11 +778,6 @@ static void nrf_ble_scan_default_conn_param_set(struct nrf_ble_scan *const scan_ scan_ctx->conn_params.slave_latency = (uint16_t)NRF_BLE_SCAN_SLAVE_LATENCY; } -/**@brief Function for calling the BLE_GAP_EVT_TIMEOUT event. - * - * @param[in] scan_ctx Pointer to the Scanning Module instance. - * @param[in] gap GAP event structure. - */ static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, ble_gap_evt_t const *const gap) { @@ -814,8 +796,6 @@ static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, } } -/**@brief Function for stopping the scanning. - */ void nrf_ble_scan_stop(void) { /* It is ok to ignore the function return value here, because this function can return*/ @@ -884,7 +864,8 @@ int nrf_ble_scan_start(struct nrf_ble_scan const *const scan_ctx) nrf_ble_scan_stop(); - /* If the whitelist is used and the event handler is not NULL, send the whitelist request to*/ + /* If the whitelist is used and the event handler is not NULL, send the whitelist request + * to*/ /* the main application.*/ if (is_whitelist_used(scan_ctx)) { if (scan_ctx->evt_handler != NULL) { @@ -909,7 +890,6 @@ int nrf_ble_scan_start(struct nrf_ble_scan const *const scan_ctx) int nrf_ble_scan_params_set(struct nrf_ble_scan *const scan_ctx, ble_gap_scan_params_t const *const scan_param) { - /* VERIFY_PARAM_NOT_NULL(scan_ctx);*/ if (!scan_ctx) { return NRF_ERROR_NULL; } @@ -928,11 +908,6 @@ int nrf_ble_scan_params_set(struct nrf_ble_scan *const scan_ctx, return NRF_SUCCESS; } -/**@brief Function for calling the BLE_GAP_EVT_CONNECTED event. - * - * @param[in] scan_ctx Pointer to the Scanning Module instance. - * @param[in] gap_evt GAP event structure. - */ static void nrf_ble_scan_on_connected_evt(struct nrf_ble_scan const *const scan_ctx, ble_gap_evt_t const *const gap_evt) { From 271ea290079b31285c6b5847d32fd6759aa72eec Mon Sep 17 00:00:00 2001 From: Sondre Pettersen Date: Tue, 21 Oct 2025 16:01:45 +0200 Subject: [PATCH 5/6] lib: scan: Standardize code - Fix comment format - Move configurations to kconfig - Fix undefined function warning - Remove struct typedefs - Add missing includes - Remove hungarian notation Signed-off-by: Sondre Pettersen Co-authored-by: Asil Zogby --- include/bm/bluetooth/ble_scan.h | 447 ++++++++++------------- lib/bluetooth/ble_scan/Kconfig | 108 ++++++ lib/bluetooth/ble_scan/ble_scan.c | 575 ++++++++++++++++-------------- 3 files changed, 595 insertions(+), 535 deletions(-) diff --git a/include/bm/bluetooth/ble_scan.h b/include/bm/bluetooth/ble_scan.h index d8f2219cd0..ae9ad01aad 100644 --- a/include/bm/bluetooth/ble_scan.h +++ b/include/bm/bluetooth/ble_scan.h @@ -4,10 +4,10 @@ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ -/* +/** * @file * - * @defgroup nrf_ble_scan Scanning Module + * @defgroup ble_scan Scanning Module * @{ * @brief Module for handling the BLE scanning. * @@ -21,8 +21,8 @@ * @note The Scanning Module also supports applications with a multicentral link. */ -#ifndef NRF_BLE_SCAN_H__ -#define NRF_BLE_SCAN_H__ +#ifndef BLE_SCAN_H__ +#define BLE_SCAN_H__ #include #include @@ -34,185 +34,118 @@ extern "C" { #endif -// NRF_BLE_SCAN_ENABLED - nrf_ble_scan - Scanning Module -//========================================================== -#ifndef NRF_BLE_SCAN_ENABLED -#define NRF_BLE_SCAN_ENABLED 1 -#endif -// NRF_BLE_SCAN_BUFFER - Data length for an advertising set. -#ifndef NRF_BLE_SCAN_BUFFER -#define NRF_BLE_SCAN_BUFFER 31 -#endif - -// NRF_BLE_SCAN_NAME_MAX_LEN - Maximum size for the name to search in the advertisement report. -#ifndef NRF_BLE_SCAN_NAME_MAX_LEN -#define NRF_BLE_SCAN_NAME_MAX_LEN 32 -#endif - -// NRF_BLE_SCAN_SHORT_NAME_MAX_LEN - Maximum size of the short name to search for in the -// advertisement report. -#ifndef NRF_BLE_SCAN_SHORT_NAME_MAX_LEN -#define NRF_BLE_SCAN_SHORT_NAME_MAX_LEN 32 -#endif - -// NRF_BLE_SCAN_SCAN_INTERVAL - Scanning interval. Determines the scan interval in units of -// 0.625 millisecond. -#ifndef NRF_BLE_SCAN_SCAN_INTERVAL -#define NRF_BLE_SCAN_SCAN_INTERVAL 160 -#endif - -// NRF_BLE_SCAN_SCAN_DURATION - Duration of a scanning session in units of 10 ms. Range: 0x0001 -// - 0xFFFF (10 ms to 10.9225 ms). If set to 0x0000, the scanning continues until it is explicitly -// disabled. -#ifndef NRF_BLE_SCAN_SCAN_DURATION -#define NRF_BLE_SCAN_SCAN_DURATION 0 -#endif - -// NRF_BLE_SCAN_SCAN_WINDOW - Scanning window. Determines the scanning window in units of 0.625 -// millisecond. -#ifndef NRF_BLE_SCAN_SCAN_WINDOW -#define NRF_BLE_SCAN_SCAN_WINDOW 80 -#endif - -// NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL - Determines minimum connection interval in -// milliseconds. -#ifndef NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL -#define NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL 7.5 -#endif - -// NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL - Determines maximum connection interval in -// milliseconds. -#ifndef NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL -#define NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL 30 -#endif - -// NRF_BLE_SCAN_SLAVE_LATENCY - Determines the slave latency in counts of connection events. -#ifndef NRF_BLE_SCAN_SLAVE_LATENCY -#define NRF_BLE_SCAN_SLAVE_LATENCY 0 -#endif - -// NRF_BLE_SCAN_SUPERVISION_TIMEOUT - Determines the supervision time-out in units of 10 -// millisecond. -#ifndef NRF_BLE_SCAN_SUPERVISION_TIMEOUT -#define NRF_BLE_SCAN_SUPERVISION_TIMEOUT 4000 -#endif - -#ifndef NRF_BLE_SCAN_SCAN_PHY -#define NRF_BLE_SCAN_SCAN_PHY 0 -#endif - -// NRF_BLE_SCAN_FILTER_ENABLE - Enabling filters for the Scanning Module. -//========================================================== -#ifndef NRF_BLE_SCAN_FILTER_ENABLE -#define NRF_BLE_SCAN_FILTER_ENABLE 1 -#endif -// NRF_BLE_SCAN_UUID_CNT - Number of filters for UUIDs. -#ifndef NRF_BLE_SCAN_UUID_CNT -#define NRF_BLE_SCAN_UUID_CNT 0 -#endif - -// NRF_BLE_SCAN_NAME_CNT - Number of name filters. -#ifndef NRF_BLE_SCAN_NAME_CNT -#define NRF_BLE_SCAN_NAME_CNT 1 -#endif - -// NRF_BLE_SCAN_SHORT_NAME_CNT - Number of short name filters. -#ifndef NRF_BLE_SCAN_SHORT_NAME_CNT -#define NRF_BLE_SCAN_SHORT_NAME_CNT 0 -#endif - -// NRF_BLE_SCAN_ADDRESS_CNT - Number of address filters. -#ifndef NRF_BLE_SCAN_ADDRESS_CNT -#define NRF_BLE_SCAN_ADDRESS_CNT 0 -#endif - -// NRF_BLE_SCAN_APPEARANCE_CNT - Number of appearance filters. -#ifndef NRF_BLE_SCAN_APPEARANCE_CNT -#define NRF_BLE_SCAN_APPEARANCE_CNT 0 -#endif - -/* - * @defgroup NRF_BLE_SCAN_FILTER_MODE Filter modes - * @{ */ -#define NRF_BLE_SCAN_NAME_FILTER (0x01) /* Filters the device name. */ -#define NRF_BLE_SCAN_ADDR_FILTER (0x02) /* Filters the device address. */ -#define NRF_BLE_SCAN_UUID_FILTER (0x04) /* Filters the UUID. */ -#define NRF_BLE_SCAN_APPEARANCE_FILTER (0x08) /* Filters the appearance. */ -#define NRF_BLE_SCAN_SHORT_NAME_FILTER (0x10) /* Filters the device short name. */ -#define NRF_BLE_SCAN_ALL_FILTER (0x1F) /* Uses the combination of all filters. */ -/* @} */ +/** + * @defgroup BLE_SCAN_FILTER_MODE Filter modes + * @{ + */ +#define BLE_SCAN_NAME_FILTER (0x01) /* Filters the device name. */ +#define BLE_SCAN_ADDR_FILTER (0x02) /* Filters the device address. */ +#define BLE_SCAN_UUID_FILTER (0x04) /* Filters the UUID. */ +#define BLE_SCAN_APPEARANCE_FILTER (0x08) /* Filters the appearance. */ +#define BLE_SCAN_SHORT_NAME_FILTER (0x10) /* Filters the device short name. */ +#define BLE_SCAN_ALL_FILTER (0x1F) /* Uses the combination of all filters. */ +/* @} + */ -/* - * @brief Macro for defining a nrf_ble_scan instance. +/** + * @brief Macro for defining a ble_scan instance. * * @param _name Name of the instance. * @hideinitializer */ -#define NRF_BLE_SCAN_DEF(_name) \ - static struct nrf_ble_scan _name; \ - NRF_SDH_BLE_OBSERVER(_name##_ble_obs, nrf_ble_scan_on_ble_evt, &_name, \ +#define BLE_SCAN_DEF(_name) \ + static struct ble_scan _name; \ + NRF_SDH_BLE_OBSERVER(_name##_ble_obs, ble_scan_on_ble_evt, &_name, \ NRF_BLE_SCAN_OBSERVER_PRIO); /** * @brief Enumeration for scanning events. * - * @details These events are propagated to the main application if a handler is provided during - * the initialization of the Scanning Module. @ref NRF_BLE_SCAN_EVT_WHITELIST_REQUEST - * cannot be ignored if whitelist is used. + * @details These events are propagated to the main application if a handler is provided during the + * initialization of the Scanning Module. @ref BLE_SCAN_EVT_WHITELIST_REQUEST cannot be ignored if + * whitelist is used. */ -enum nrf_ble_scan_evt { - /* A filter is matched or all filters are matched in the multifilter mode. */ - NRF_BLE_SCAN_EVT_FILTER_MATCH, - /* Request the whitelist from the main application. - * For whitelist scanning to work, the whitelist must be - * set when this event occurs. +enum ble_scan_evt { + /** + * @brief A filter is matched or all filters are matched in the multifilter mode. + */ + BLE_SCAN_EVT_FILTER_MATCH, + /** + * @brief Request the whitelist from the main application. + * + * For whitelist scanning to work, the whitelist must be set when this event occurs. */ - NRF_BLE_SCAN_EVT_WHITELIST_REQUEST, - /* Send notification to the main application when a - * device from the whitelist is found. + BLE_SCAN_EVT_WHITELIST_REQUEST, + /** + * @brief Send notification to the main application when a device from the whitelist is + * found. */ - NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT, - /* The filter was not matched for the scan data. */ - NRF_BLE_SCAN_EVT_NOT_FOUND, - /* Scan timeout. */ - NRF_BLE_SCAN_EVT_SCAN_TIMEOUT, - /* Error occurred when establishing the connection. In - *this event, an error is passed from the function call - *@ref sd_ble_gap_connect. + BLE_SCAN_EVT_WHITELIST_ADV_REPORT, + /** + * @brief The filter was not matched for the scan data. */ - NRF_BLE_SCAN_EVT_CONNECTING_ERROR, - /* Connected to device. */ - NRF_BLE_SCAN_EVT_CONNECTED, - NRF_BLE_SCAN_EVT_ERROR, + BLE_SCAN_EVT_NOT_FOUND, + /** + * @brief Scan timeout. + */ + BLE_SCAN_EVT_SCAN_TIMEOUT, + /** + * @brief Error occurred when establishing the connection. + * + * In this event, an error is passed from the function call @ref sd_ble_gap_connect. + */ + BLE_SCAN_EVT_CONNECTING_ERROR, + /** + * @brief Connected to device. + */ + BLE_SCAN_EVT_CONNECTED, + BLE_SCAN_EVT_ERROR, }; /** * @brief Types of filters. */ -enum nrf_ble_scan_filter_type { - /* Filter for names. */ +enum ble_scan_filter_type { + /** + * Filter for names. + */ SCAN_NAME_FILTER, - /* Filter for short names. */ + /** + * @brief Filter for short names. + */ SCAN_SHORT_NAME_FILTER, - /* Filter for addresses. */ + /** + * @brief Filter for addresses. + */ SCAN_ADDR_FILTER, - /* Filter for UUIDs. */ + /** + * @brief Filter for UUIDs. + */ SCAN_UUID_FILTER, - /* Filter for appearances. */ + /** + * @brief Filter for appearances. + */ SCAN_APPEARANCE_FILTER, }; -struct nrf_ble_scan_short_name { - /* Pointer to the short name. */ +/** + * @brief Scan short name. + */ +struct ble_scan_short_name { + /** + * @brief Pointer to the short name. + */ char const *short_name; - /* Minimum length of the short name. */ + /** + * @brief Minimum length of the short name. + */ uint8_t short_name_min_len; }; /** * @brief Structure for Scanning Module initialization. */ -struct nrf_ble_scan_init { +struct ble_scan_init { /* BLE GAP scan parameters required to initialize the module. Can * be initialized as NULL. If NULL, the parameters required to * initialize the module are loaded from the static configuration. @@ -237,7 +170,7 @@ struct nrf_ble_scan_init { * * @details This structure is used for sending filter status to the main application. */ -struct nrf_ble_scan_filter_match { +struct ble_scan_filter_match { /* Set to 1 if name filter is matched. */ uint8_t name_filter_match: 1; /* Set to 1 if address filter is matched. */ @@ -251,21 +184,21 @@ struct nrf_ble_scan_filter_match { }; /** - * @brief Event structure for @ref NRF_BLE_SCAN_EVT_FILTER_MATCH. + * @brief Event structure for @ref BLE_SCAN_EVT_FILTER_MATCH. */ -struct nrf_ble_scan_evt_filter_match { +struct ble_scan_evt_filter_match { /* Event structure for @ref BLE_GAP_EVT_ADV_REPORT. This data * allows the main application to establish connection. */ ble_gap_evt_adv_report_t const *adv_report; /* Matching filters. Information about matched filters. */ - struct nrf_ble_scan_filter_match filter_match; + struct ble_scan_filter_match filter_match; }; /** - * @brief Event structure for @ref NRF_BLE_SCAN_EVT_CONNECTING_ERROR. + * @brief Event structure for @ref BLE_SCAN_EVT_CONNECTING_ERROR. */ -struct nrf_ble_scan_evt_connecting_err { +struct ble_scan_evt_connecting_err { /* Indicates success or failure of an API procedure. In case of * failure, a comprehensive error code indicating the cause or reason * for failure is provided. @@ -274,9 +207,9 @@ struct nrf_ble_scan_evt_connecting_err { }; /** - * @brief Event structure for @ref NRF_BLE_SCAN_EVT_CONNECTED. + * @brief Event structure for @ref BLE_SCAN_EVT_CONNECTED. */ -struct nrf_ble_scan_evt_connected { +struct ble_scan_evt_connected { /* Connected event parameters. */ ble_gap_evt_connected_t const *connected; /* Connection handle of the device on which the event occurred. */ @@ -291,10 +224,10 @@ struct nrf_ble_scan_evt_connected { */ struct scan_evt { /* Type of event propagated to the main application. */ - enum nrf_ble_scan_evt scan_evt_id; + enum ble_scan_evt scan_evt_id; union { /* Scan filter match. */ - struct nrf_ble_scan_evt_filter_match filter_match; + struct ble_scan_evt_filter_match filter_match; /* Timeout event parameters. */ ble_gap_evt_timeout_t timeout; /* Advertising report event parameters for whitelist. */ @@ -302,10 +235,11 @@ struct scan_evt { /* Advertising report event parameters when filter is not found. */ ble_gap_evt_adv_report_t const *not_found; /* Connected event parameters. */ - struct nrf_ble_scan_evt_connected connected; - /* Error event when connecting. Propagates the error code returned by the - SoftDevice API @ref sd_ble_gap_scan_start. */ - struct nrf_ble_scan_evt_connecting_err connecting_err; + struct ble_scan_evt_connected connected; + /* Error event when connecting. Propagates the error code returned by the SoftDevice + * API @ref sd_ble_gap_scan_start. + */ + struct ble_scan_evt_connecting_err connecting_err; struct { uint32_t reason; } error; @@ -319,17 +253,17 @@ struct scan_evt { /** * @brief BLE scanning event handler type. */ -typedef void (*nrf_ble_scan_evt_handler_t)(struct scan_evt const *scan_evt); +typedef void (*ble_scan_evt_handler_t)(struct scan_evt const *scan_evt); -#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 +#if CONFIG_BLE_SCAN_FILTER_ENABLE -#if (NRF_BLE_SCAN_NAME_CNT > 0) -struct nrf_ble_scan_name_filter { +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) +struct ble_scan_name_filter { /* Names that the main application will scan * for, and that will be advertised by the * peripherals. */ - char target_name[NRF_BLE_SCAN_NAME_CNT][NRF_BLE_SCAN_NAME_MAX_LEN]; + char target_name[CONFIG_BLE_SCAN_NAME_CNT][CONFIG_BLE_SCAN_NAME_MAX_LEN]; /* Name filter counter. */ uint8_t name_cnt; /* Flag to inform about enabling or disabling this filter. */ @@ -337,8 +271,8 @@ struct nrf_ble_scan_name_filter { }; #endif -#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) -typedef struct { +#if (CONFIG_BLE_SCAN_SHORT_NAME_CNT > 0) +struct ble_scan_short_name_filter { struct { /* Short names that the * main application will @@ -346,26 +280,26 @@ typedef struct { * be advertised by the * peripherals. */ - char short_target_name[NRF_BLE_SCAN_SHORT_NAME_MAX_LEN]; + char short_target_name[CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN]; /* Minimum length of the short name. */ uint8_t short_name_min_len; - } short_name[NRF_BLE_SCAN_SHORT_NAME_CNT]; + } short_name[CONFIG_BLE_SCAN_SHORT_NAME_CNT]; /* Short name filter counter. */ uint8_t name_cnt; /* Flag to inform about enabling or disabling this filter. */ bool short_name_filter_enabled; -} nrf_ble_scan_short_name_filter_t; +}; #endif -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) -struct nrf_ble_scan_addr_filter { - struct ble_gap_addr +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) +struct ble_scan_addr_filter { + ble_gap_addr_t /* Addresses in the same format as the * format used by the SoftDevice that the * main application will scan for, and that * will be advertised by the peripherals. */ - target_addr[NRF_BLE_SCAN_ADDRESS_CNT]; + target_addr[CONFIG_BLE_SCAN_ADDRESS_CNT]; /* Address filter counter. */ uint8_t addr_cnt; /* Flag to inform about enabling or disabling this filter. */ @@ -373,12 +307,13 @@ struct nrf_ble_scan_addr_filter { }; #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) -struct nrf_ble_scan_uuid_filter { - struct ble_uuid - /* UUIDs that the main application will scan for, and - that will be advertised by the peripherals. */ - uuid[NRF_BLE_SCAN_UUID_CNT]; +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) +struct ble_scan_uuid_filter { + ble_uuid_t + /* UUIDs that the main application will scan for, and that will be advertised by the + * peripherals. + */ + uuid[CONFIG_BLE_SCAN_UUID_CNT]; /* UUID filter counter. */ uint8_t uuid_cnt; /* Flag to inform about enabling or disabling this filter. */ @@ -386,17 +321,17 @@ struct nrf_ble_scan_uuid_filter { }; #endif -#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) -typedef struct { - /* Apperances that the main application - will scan for, and that will be - advertised by the peripherals. */ - uint16_t appearance[NRF_BLE_SCAN_APPEARANCE_CNT]; +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT > 0) +struct ble_scan_appearance_filter { + /* Apperances that the main application will scan for, and that will be advertised by the + * peripherals. + */ + uint16_t appearance[CONFIG_BLE_SCAN_APPEARANCE_CNT]; /* Appearance filter counter. */ uint8_t appearance_cnt; /* Flag to inform about enabling or disabling this filter.*/ bool appearance_filter_enabled; -} nrf_ble_scan_appearance_filter_t; +}; #endif /** @@ -407,37 +342,34 @@ typedef struct { * then all types of enabled filters must be matched for the module to send a notification to the * main application. Otherwise, it is enough to match one of filters to send notification. */ -struct nrf_ble_scan_filters { -#if (NRF_BLE_SCAN_NAME_CNT > 0) +struct ble_scan_filters { +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) /* Name filter data. */ - struct nrf_ble_scan_name_filter name_filter; + struct ble_scan_name_filter name_filter; #endif -#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_SHORT_NAME_CNT > 0) /* Short name filter data. */ - struct nrf_ble_scan_short_name_filter short_name_filter; + struct ble_scan_short_name_filter short_name_filter; #endif -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) /* Address filter data. */ - struct nrf_ble_scan_addr_filter addr_filter; + struct ble_scan_addr_filter addr_filter; #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) /* UUID filter data. */ - struct nrf_ble_scan_uuid_filter uuid_filter; + struct ble_scan_uuid_filter uuid_filter; #endif -#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT > 0) /* Appearance filter data. */ - nrf_ble_scan_appearance_filter_t appearance_filter; + struct ble_scan_appearance_filter appearance_filter; #endif /* Filter mode. If true, all set filters must be matched to - generate an event.*/ + * generate an event. + */ bool all_filters_mode; }; -#endif // NRF_BLE_SCAN_FILTER_ENABLE - -#ifndef NRF_BLE_SCAN_BUFFER -#define NRF_BLE_SCAN_BUFFER 31 -#endif +#endif /* CONFIG_BLE_SCAN_FILTER_ENABLE */ /** * @brief Scan module instance. Options for the different scanning modes. @@ -445,10 +377,10 @@ struct nrf_ble_scan_filters { * @details This structure stores all module settings. It is used to enable or disable scanning * modes and to configure filters. */ -struct nrf_ble_scan { -#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 +struct ble_scan { +#if CONFIG_BLE_SCAN_FILTER_ENABLE /* Filter data. */ - struct nrf_ble_scan_filters scan_filters; + struct ble_scan_filters scan_filters; #endif /* If set to true, the module automatically connects after a filter * match or successful identification of a device from the whitelist. @@ -465,11 +397,11 @@ struct nrf_ble_scan { /* Handler for the scanning events. Can be initialized as NULL if no * handling is implemented in the main application. */ - nrf_ble_scan_evt_handler_t evt_handler; + ble_scan_evt_handler_t evt_handler; /* Buffer where advertising reports will be * stored by the SoftDevice. */ - uint8_t scan_buffer_data[NRF_BLE_SCAN_BUFFER]; + uint8_t scan_buffer_data[CONFIG_BLE_SCAN_BUFFER]; /* Structure-stored pointer to the buffer where advertising * reports will be stored by the SoftDevice. */ @@ -483,7 +415,7 @@ struct nrf_ble_scan { * * @return Whether the whitelist is used. */ -bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx); +bool is_whitelist_used(struct ble_scan const *const scan_ctx); /** * @brief Function for initializing the Scanning Module. @@ -502,9 +434,8 @@ bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx); * @retval NRF_SUCCESS If initialization was successful. * @retval NRF_ERROR_NULL When the NULL pointer is passed as input. */ -int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, - struct nrf_ble_scan_init const *const init, - nrf_ble_scan_evt_handler_t evt_handler); +int ble_scan_init(struct ble_scan *const scan_ctx, struct ble_scan_init const *const init, + ble_scan_evt_handler_t evt_handler); /** * @brief Function for starting scanning. @@ -520,49 +451,49 @@ int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, * @return This API propagates the error code returned by the * SoftDevice API @ref sd_ble_gap_scan_start. */ -int nrf_ble_scan_start(struct nrf_ble_scan const *const scan_ctx); +int ble_scan_start(struct ble_scan const *const scan_ctx); /** * @brief Function for stopping scanning. */ -void nrf_ble_scan_stop(void); +void ble_scan_stop(void); -#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 +#if CONFIG_BLE_SCAN_FILTER_ENABLE /** * @brief Function for enabling filtering. * * @details The filters can be combined with each other. For example, you can enable one filter or - * several filters. For example, (NRF_BLE_SCAN_NAME_FILTER | NRF_BLE_SCAN_UUID_FILTER) enables UUID - * and name filters. + * several filters. For example, (BLE_SCAN_NAME_FILTER | BLE_SCAN_UUID_FILTER) + * enables UUID and name filters. * - * @param[in] mode Filter mode: @ref NRF_BLE_SCAN_FILTER_MODE. + * @param[in] mode Filter mode: @ref BLE_SCAN_FILTER_MODE. * @param[in] match_all If this flag is set, all types of enabled filters must be - * matched before generating @ref NRF_BLE_SCAN_EVT_FILTER_MATCH to the main application. Otherwise, + * matched before generating @ref BLE_SCAN_EVT_FILTER_MATCH to the main application. Otherwise, * it is enough to match one filter to trigger the filter match event. * @param[in] scan_ctx Pointer to the Scanning Module instance. * * @retval NRF_SUCCESS If the filters are enabled successfully. * @retval NRF_ERROR_INVALID_PARAM If the filter mode is incorrect. Available filter modes: @ref - * NRF_BLE_SCAN_FILTER_MODE. + * BLE_SCAN_FILTER_MODE. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. */ -int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const scan_ctx, uint8_t mode, bool match_all); +int ble_scan_filters_enable(struct ble_scan *const scan_ctx, uint8_t mode, bool match_all); /** * @brief Function for disabling filtering. * * @details This function disables all filters. - * Even if the automatic connection establishing is enabled, - * the connection will not be established with the first - device found after this function is called. + * Even if the automatic connection establishing is enabled, + * the connection will not be established with the first device found after + * this function is called. * * @param[in] scan_ctx Pointer to the Scanning Module instance. * * @retval NRF_SUCCESS If filters are disabled successfully. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. */ -int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const scan_ctx); +int ble_scan_filters_disable(struct ble_scan *const scan_ctx); /** * @brief Function for getting filter status. @@ -575,18 +506,16 @@ int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const scan_ctx); * @retval NRF_SUCCESS If filter status is returned. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. */ -int nrf_ble_scan_filter_get(struct nrf_ble_scan *const scan_ctx, - struct nrf_ble_scan_filters *status); +int ble_scan_filter_get(struct ble_scan *const scan_ctx, struct ble_scan_filters *status); /** * @brief Function for adding any type of filter to the scanning. * - * @details This function adds a new filter by type @ref nrf_ble_scan_filter_type_t. - * The filter will be added if the number of filters of a given type does not - * exceed @ref NRF_BLE_SCAN_UUID_CNT, - * @ref NRF_BLE_SCAN_NAME_CNT, @ref NRF_BLE_SCAN_ADDRESS_CNT, or @ref - * NRF_BLE_SCAN_APPEARANCE_CNT, depending on the filter type, and if the same filter has not already - * been set. + * @details This function adds a new filter by type @ref ble_scan_filter_type_t. + * The filter will be added if the number of filters of a given type does not exceed @ref + * CONFIG_BLE_SCAN_UUID_CNT, @ref CONFIG_BLE_SCAN_NAME_CNT, @ref + * CONFIG_BLE_SCAN_ADDRESS_CNT, or @ref CONFIG_BLE_SCAN_APPEARANCE_CNT, depending on the + * filter type, and if the same filter has not already been set. * * @param[in,out] scan_ctx Pointer to the Scanning Module instance. * @param[in] type Filter type. @@ -598,11 +527,11 @@ int nrf_ble_scan_filter_get(struct nrf_ble_scan *const scan_ctx, * length corresponds to @ref NRF_BLE_SCAN_NAME_MAX_LEN. * @retval NRF_ERROR_NO_MEMORY If the number of available filters is exceeded. * @retval NRF_ERROR_INVALID_PARAM If the filter type is incorrect. Available filter types: - * @ref nrf_ble_scan_filter_type_t. + * @ref ble_scan_filter_type_t. * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address type is invalid. */ -int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_scan_filter_type type, - void const *data); +int ble_scan_filter_set(struct ble_scan *const scan_ctx, enum ble_scan_filter_type type, + void const *data); /** * @brief Function for removing all set filters. @@ -615,21 +544,22 @@ int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_sc * * @retval NRF_SUCCESS If all filters are removed successfully. */ -int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const scan_ctx); +int ble_scan_all_filter_remove(struct ble_scan *const scan_ctx); -#endif // CONFIG_BT_SCAN_FILTER_ENABLE +#endif /* CONFIG_BLE_SCAN_FILTER_ENABLE */ /** * @brief Function for changing the scanning parameters. * - **@details Use this function to change scanning parameters. During the parameter change - * the scan is stopped. To resume scanning, use @ref nrf_ble_scan_start. - * Scanning parameters can be set to NULL. If so, the default static configuration - * is used. For example, use this function when the @ref - *NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT event is generated. The generation of this event means that - *there is a risk that the whitelist is empty. In such case, this function can change the scanning - *parameters, so that the whitelist is not used, and you avoid the error caused by scanning with the - *whitelist when there are no devices on the whitelist. + * @details Use this function to change scanning parameters. During the parameter change + * the scan is stopped. To resume scanning, use @ref ble_scan_start. + * Scanning parameters can be set to NULL. If so, the default static + * configuration is used. For example, use this function when the @ref + * BLE_SCAN_EVT_WHITELIST_ADV_REPORT event is generated. The generation of this + * event means that there is a risk that the whitelist is empty. In such case, + * this function can change the scanning parameters, so that the whitelist is + * not used, and you avoid the error caused by scanning with the whitelist when + * there are no devices on the whitelist. * * @param[in,out] scan_ctx Pointer to the Scanning Module instance. * @param[in] scan_param GAP scanning parameters. Can be initialized as NULL. @@ -637,8 +567,7 @@ int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const scan_ctx); * @retval NRF_SUCCESS If parameters are changed successfully. * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. */ -int nrf_ble_scan_params_set(struct nrf_ble_scan *const scan_ctx, - ble_gap_scan_params_t const *scan_param); +int ble_scan_params_set(struct ble_scan *const scan_ctx, ble_gap_scan_params_t const *scan_param); /** * @brief Function for handling the BLE stack events of the application. @@ -646,14 +575,14 @@ int nrf_ble_scan_params_set(struct nrf_ble_scan *const scan_ctx, * @param[in] ble_evt Pointer to the BLE event received. * @param[in,out] scan Pointer to the Scanning Module instance. */ -void nrf_ble_scan_on_ble_evt(ble_evt_t const *ble_evt, void *scan); +void ble_scan_on_ble_evt(ble_evt_t const *ble_evt, void *scan); /** * @brief Function for converting the raw address to the SoftDevice GAP address. * * @details This function inverts the byte order in the address. If you enter the address as it is - * displayed (for example, on a phone screen from left to right), you must use this function to - * convert the address to the SoftDevice address type. + * displayed (for example, on a phone screen from left to right), you must use + * this function to convert the address to the SoftDevice address type. * * @note This function does not decode an address type. * @@ -663,13 +592,13 @@ void nrf_ble_scan_on_ble_evt(ble_evt_t const *ble_evt, void *scan); * @retval NRF_ERROR_NULL If a NULL pointer is passed as input. * @retval NRF_SUCCESS If the address is copied and converted successfully. */ -int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *gap_addr, - uint8_t const addr[BLE_GAP_ADDR_LEN]); +int ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *gap_addr, + uint8_t const addr[BLE_GAP_ADDR_LEN]); #ifdef __cplusplus } #endif -#endif // NRF_BLE_SCAN_H__ +#endif /* BLE_SCAN_H__ */ /* @} */ diff --git a/lib/bluetooth/ble_scan/Kconfig b/lib/bluetooth/ble_scan/Kconfig index 65338596fe..166aed8cf6 100644 --- a/lib/bluetooth/ble_scan/Kconfig +++ b/lib/bluetooth/ble_scan/Kconfig @@ -10,3 +10,111 @@ menuconfig BLE_SCAN select EXPERIMENTAL help scan. + +if BLE_SCAN + +config BLE_SCAN_BUFFER + int "Scan buffer length" + default 31 + help + Data length for an advertising set. + +config BLE_SCAN_NAME_MAX_LEN + int "Scan name maximum length" + default 32 + help + Maximum size for the name to search in the advertisement report. + +config BLE_SCAN_SHORT_NAME_MAX_LEN + int "Scan short name maximum length" + default 32 + help + Maximum size of the short name to search for in the advertisement report + +config BLE_SCAN_NAME_CNT + int "Scan name count" + default 1 + help + Number of name filters. + +config BLE_SCAN_APPEARANCE_CNT + int "Scan appearance count" + default 0 + help + Number of appearance filters. + +config BLE_SCAN_ADDRESS_CNT + int "Scan address count" + default 0 + help + Number of address filters. + +config BLE_SCAN_SHORT_NAME_CNT + int "Scan short name count" + default 0 + help + Number of short name filters. + +config BLE_SCAN_UUID_CNT + int "Scan UUID count" + default 0 + help + Number of filters for UUIDs. + +config BLE_SCAN_SCAN_INTERVAL + int "Scanning interval" + default 160 + help + Determines the scan interval in units of 0.625 millisecond. + +config BLE_SCAN_SCAN_DURATION + hex "Scan duration" + default 0x0000 + range 0x0000 0xFFFF + help + Duration of a scanning session in units of 10 ms. If set to 0x0000, the scanning continues until it is explicitly disabled. + +config BLE_SCAN_SCAN_WINDOW + int "Scanning window" + default 80 + help + Determines the scanning window in units of 0.625 milliseconds. + +config BLE_SCAN_SLAVE_LATENCY + int "Scan slave latency" + default 0 + help + Determines the slave latency in counts of connection events + +config BLE_SCAN_MIN_CONNECTION_INTERVAL + hex "Minimum connection interval" + default 0x0006 + range 0x0006 0x0C80 + help + Determines the minimum connection interval in units of 1.25 milliseconds + +config BLE_SCAN_MAX_CONNECTION_INTERVAL + hex "Maximum connection interval" + default 0x0018 + range 0x0006 0x0C80 + help + Determines the maximum connection interval in units of 1.25 milliseconds + +config BLE_SCAN_SUPERVISION_TIMEOUT + hex "Scan supervision timeout" + default 0x0C80 + range 0x000A 0x0C80 + help + Determines the supervision time-out in units of 10 millisecond + +config BLE_SCAN_FILTER_ENABLE + bool "Scan filter enable" + default y + help + Enabling filters for the Scanning Module. + +module=BLE_SCAN +module-str=BLE Scan +source "$(ZEPHYR_BASE)/subsys/logging/Kconfig.template.log_config" + +endif # BLE_SCAN diff --git a/lib/bluetooth/ble_scan/ble_scan.c b/lib/bluetooth/ble_scan/ble_scan.c index 0a03aecd59..f02d261f9f 100644 --- a/lib/bluetooth/ble_scan/ble_scan.c +++ b/lib/bluetooth/ble_scan/ble_scan.c @@ -4,99 +4,59 @@ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ -/* #include "sdk_common.h"*/ - #include #include #include #include #include -LOG_MODULE_REGISTER(ble_scan, 3); - -/* NRF_BLE_SCAN_BUFFER - Data length for an advertising set.*/ -#ifndef NRF_BLE_SCAN_BUFFER -#define NRF_BLE_SCAN_BUFFER 31 -#endif - -/* NRF_BLE_SCAN_NAME_MAX_LEN - Maximum size for the name to search in the advertisement report.*/ -#ifndef NRF_BLE_SCAN_NAME_MAX_LEN -#define NRF_BLE_SCAN_NAME_MAX_LEN 32 -#endif - -/* NRF_BLE_SCAN_SHORT_NAME_MAX_LEN - Maximum size of the short name to search for in the*/ -/* advertisement report.*/ -#ifndef NRF_BLE_SCAN_SHORT_NAME_MAX_LEN -#define NRF_BLE_SCAN_SHORT_NAME_MAX_LEN 32 -#endif +#include -/* NRF_BLE_SCAN_SCAN_INTERVAL - Scanning interval. Determines the scan interval in units of*/ -/* 0.625 millisecond.*/ -#ifndef NRF_BLE_SCAN_SCAN_INTERVAL -#define NRF_BLE_SCAN_SCAN_INTERVAL 160 -#endif - -/* NRF_BLE_SCAN_SCAN_DURATION - Duration of a scanning session in units of 10 ms. Range: 0x0001*/ -/* - 0xFFFF (10 ms to 10.9225 ms). If set to 0x0000, the scanning continues until it is explicitly*/ -/* disabled.*/ -#ifndef NRF_BLE_SCAN_SCAN_DURATION -#define NRF_BLE_SCAN_SCAN_DURATION 0 -#endif - -/* NRF_BLE_SCAN_SCAN_WINDOW - Scanning window. Determines the scanning window in units of 0.625*/ -/* millisecond.*/ -#ifndef NRF_BLE_SCAN_SCAN_WINDOW -#define NRF_BLE_SCAN_SCAN_WINDOW 80 -#endif - -/* NRF_BLE_SCAN_SLAVE_LATENCY - Determines the slave latency in counts of connection events.*/ -#ifndef NRF_BLE_SCAN_SLAVE_LATENCY -#define NRF_BLE_SCAN_SLAVE_LATENCY 5 -#endif +LOG_MODULE_REGISTER(ble_scan, CONFIG_BLE_SCAN_LOG_LEVEL); -static void nrf_ble_scan_connect_with_target(struct nrf_ble_scan const *const scan_ctx, - ble_gap_evt_adv_report_t const *const adv_report) +static void ble_scan_connect_with_target(struct ble_scan const *const scan_ctx, + ble_gap_evt_adv_report_t const *const adv_report) { int err_code; struct scan_evt scan_evt; - /* For readability.*/ + /* For readability. */ ble_gap_addr_t const *addr = &adv_report->peer_addr; ble_gap_scan_params_t const *scan_params = &scan_ctx->scan_params; ble_gap_conn_params_t const *conn_params = &scan_ctx->conn_params; uint8_t con_cfg_tag = scan_ctx->conn_cfg_tag; - /* Return if the automatic connection is disabled.*/ + /* Return if the automatic connection is disabled. */ if (!scan_ctx->connect_if_match) { return; } - /* Stop scanning.*/ - nrf_ble_scan_stop(); + /* Stop scanning. */ + ble_scan_stop(); memset(&scan_evt, 0, sizeof(scan_evt)); - /* Establish connection.*/ + /* Establish connection. */ err_code = sd_ble_gap_connect(addr, scan_params, conn_params, con_cfg_tag); if (err_code != NRF_SUCCESS) { - /* TODO: Trigger event handler with error*/ + /* TODO: Trigger event handler with error */ } LOG_DBG("Connecting"); - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTING_ERROR; + scan_evt.scan_evt_id = BLE_SCAN_EVT_CONNECTING_ERROR; scan_evt.params.connecting_err.err_code = err_code; LOG_DBG("Connection status: %d", err_code); - /* If an error occurred, send an event to the event handler.*/ + /* If an error occurred, send an event to the event handler. */ if ((err_code != NRF_SUCCESS) && (scan_ctx->evt_handler != NULL)) { scan_ctx->evt_handler(&scan_evt); } } -#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) +#if CONFIG_BLE_SCAN_FILTER_ENABLE +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) static bool find_peer_addr(ble_gap_evt_adv_report_t const *const adv_report, ble_gap_addr_t const *addr) @@ -111,7 +71,7 @@ static bool find_peer_addr(ble_gap_evt_adv_report_t const *const adv_report, } static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const adv_report, - struct nrf_ble_scan const *const an_ctx) + struct ble_scan const *const scan_ctx) { ble_gap_addr_t const *addr = scan_ctx->scan_filters.addr_filter.target_addr; uint8_t counter = scan_ctx->scan_filters.addr_filter.addr_cnt; @@ -126,19 +86,19 @@ static bool adv_addr_compare(ble_gap_evt_adv_report_t const *const adv_report, return false; } -static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const scan_ctx, uint8_t const *addr) +static int ble_scan_addr_filter_add(struct ble_scan *const scan_ctx, uint8_t const *addr) { ble_gap_addr_t *addr_filter = scan_ctx->scan_filters.addr_filter.target_addr; uint8_t *counter = &scan_ctx->scan_filters.addr_filter.addr_cnt; uint8_t index; /* If no memory for filter. */ - if (*counter >= NRF_BLE_SCAN_ADDRESS_CNT) { + if (*counter >= CONFIG_BLE_SCAN_ADDRESS_CNT) { return NRF_ERROR_NO_MEM; } /* Check for duplicated filter.*/ - for (index = 0; index < NRF_BLE_SCAN_ADDRESS_CNT; index++) { + for (index = 0; index < CONFIG_BLE_SCAN_ADDRESS_CNT; index++) { if (!memcmp(addr_filter[index].addr, addr, BLE_GAP_ADDR_LEN)) { return NRF_SUCCESS; } @@ -148,36 +108,88 @@ static int nrf_ble_scan_addr_filter_add(struct nrf_ble_scan *const scan_ctx, uin addr_filter[*counter].addr[i] = addr[i]; } - /* Address type is not used so set it to 0.*/ + /* Address type is not used so set it to 0. */ addr_filter[*counter].addr_type = 0; - LOG_DBG("Filter set on address 0x"); - NRF_LOG_HEXDUMP_DEBUG(addr_filter[*counter].addr, BLE_GAP_ADDR_LEN); + LOG_HEXDUMP_DBG(addr_filter[*counter].addr, BLE_GAP_ADDR_LEN, "Filter set on address"); - /* Increase the address filter counter.*/ + /* Increase the address filter counter. */ *counter += 1; return NRF_SUCCESS; } -#endif /* NRF_BLE_SCAN_ADDRESS_CNT*/ +#endif /* CONFIG_BLE_SCAN_ADDRESS_CNT */ -#if (NRF_BLE_SCAN_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) +static uint16_t advdata_search(uint8_t const *encoded_data, uint16_t data_len, uint16_t *offset, + uint8_t ad_type) +{ + if ((encoded_data == NULL) || (offset == NULL)) { + return 0; + } + + uint16_t i = 0; + + while ((i + 1 < data_len) && ((i < *offset) || (encoded_data[i + 1] != ad_type))) { + /* Jump to next data. */ + i += (encoded_data[i] + 1); + } + + if (i >= data_len) { + return 0; + } + uint16_t new_offset = i + 2; + + uint16_t len = encoded_data[i] ? (encoded_data[i] - 1) : 0; + + if (!len || ((new_offset + len) > data_len)) { + /* Malformed. Zero length or extends beyond provided data. */ + return 0; + } + *offset = new_offset; + return len; +} + +static bool advdata_name_find(uint8_t const *encoded_data, uint16_t data_len, + char const *target_name) +{ + uint16_t parsed_name_len; + uint8_t const *parsed_name; + uint16_t data_offset = 0; + + if (target_name == NULL) { + return false; + } + + parsed_name_len = advdata_search(encoded_data, data_len, &data_offset, + BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME); + + parsed_name = &encoded_data[data_offset]; + + if ((data_offset != 0) && (parsed_name_len != 0) && + (strlen(target_name) == parsed_name_len) && + (memcmp(target_name, parsed_name, parsed_name_len) == 0)) { + return true; + } + + return false; +} static bool adv_name_compare(ble_gap_evt_adv_report_t const *adv_report, - struct nrf_ble_scan const *const scan_ctx) + struct ble_scan const *const scan_ctx) { - struct nrf_ble_scan_name_filter const *name_filter = &scan_ctx->scan_filters.name_filter; + struct ble_scan_name_filter const *name_filter = &scan_ctx->scan_filters.name_filter; uint8_t counter = scan_ctx->scan_filters.name_filter.name_cnt; uint8_t index; uint16_t data_len; data_len = adv_report->data.len; - /* Compare the name found with the name filter.*/ + /* Compare the name found with the name filter. */ for (index = 0; index < counter; index++) { - if (ble_advdata_name_find(adv_report->data.data, data_len, - name_filter->target_name[index])) { + if (advdata_name_find(adv_report->data.p_data, data_len, + name_filter->target_name[index])) { return true; } } @@ -185,30 +197,30 @@ static bool adv_name_compare(ble_gap_evt_adv_report_t const *adv_report, return false; } -static int nrf_ble_scan_name_filter_add(struct nrf_ble_scan *const scan_ctx, char const *name) +static int ble_scan_name_filter_add(struct ble_scan *const scan_ctx, char const *name) { uint8_t index; uint8_t *counter = &scan_ctx->scan_filters.name_filter.name_cnt; uint8_t name_len = strlen(name); - /* Check the name length.*/ - if ((name_len == 0) || (name_len > NRF_BLE_SCAN_NAME_MAX_LEN)) { + /* Check the name length. */ + if ((name_len == 0) || (name_len > CONFIG_BLE_SCAN_NAME_MAX_LEN)) { return NRF_ERROR_DATA_SIZE; } - /* If no memory for filter.*/ - if (*counter >= NRF_BLE_SCAN_NAME_CNT) { + /* If no memory for filter. */ + if (*counter >= CONFIG_BLE_SCAN_NAME_CNT) { return NRF_ERROR_NO_MEM; } - /* Check for duplicated filter.*/ - for (index = 0; index < NRF_BLE_SCAN_NAME_CNT; index++) { + /* Check for duplicated filter. */ + for (index = 0; index < CONFIG_BLE_SCAN_NAME_CNT; index++) { if (!strcmp(scan_ctx->scan_filters.name_filter.target_name[index], name)) { return NRF_SUCCESS; } } - /* Add name to filter.*/ + /* Add name to filter. */ memcpy(scan_ctx->scan_filters.name_filter.target_name[(*counter)++], name, strlen(name)); LOG_DBG("Adding filter on %s name", name); @@ -216,13 +228,13 @@ static int nrf_ble_scan_name_filter_add(struct nrf_ble_scan *const scan_ctx, cha return NRF_SUCCESS; } -#endif /* NRF_BLE_SCAN_NAME_CNT*/ +#endif /* CONFIG_BLE_SCAN_NAME_CNT */ -#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_SHORT_NAME_CNT > 0) static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const adv_report, - struct nrf_ble_scan const *const scan_ctx) + struct ble_scan const *const scan_ctx) { - nrf_ble_scan_short_name_filter_t const *name_filter = + struct ble_scan_short_name_filter const *name_filter = &scan_ctx->scan_filters.short_name_filter; uint8_t counter = scan_ctx->scan_filters.short_name_filter.name_cnt; uint8_t index; @@ -230,10 +242,10 @@ static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const adv_rep data_len = adv_report->data.len; - /* Compare the name found with the name filters.*/ + /* Compare the name found with the name filters. */ for (index = 0; index < counter; index++) { - if (ble_advdata_short_name_find( - adv_report->data.data, data_len, + if (ble_adv_data_short_name_find( + adv_report->data.p_data, data_len, name_filter->short_name[index].short_target_name, name_filter->short_name[index].short_name_min_len)) { return true; @@ -243,34 +255,34 @@ static bool adv_short_name_compare(ble_gap_evt_adv_report_t const *const adv_rep return false; } -static int nrf_ble_scan_short_name_filter_add(struct nrf_ble_scan *const scan_ctx, - nrf_ble_scan_short_name_t const *short_name) +static int ble_scan_short_name_filter_add(struct ble_scan *const scan_ctx, + struct ble_scan_short_name const *short_name) { uint8_t index; uint8_t *counter = &scan_ctx->scan_filters.short_name_filter.name_cnt; - nrf_ble_scan_short_name_filter_t *short_name_filter = + struct ble_scan_short_name_filter *short_name_filter = &scan_ctx->scan_filters.short_name_filter; uint8_t name_len = strlen(short_name->short_name); - /* Check the name length.*/ - if ((name_len == 0) || (name_len > NRF_BLE_SCAN_SHORT_NAME_MAX_LEN)) { + /* Check the name length. */ + if ((name_len == 0) || (name_len > CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN)) { return NRF_ERROR_DATA_SIZE; } - /* If no memory for filter.*/ - if (*counter >= NRF_BLE_SCAN_SHORT_NAME_CNT) { + /* If no memory for filter. */ + if (*counter >= CONFIG_BLE_SCAN_SHORT_NAME_CNT) { return NRF_ERROR_NO_MEM; } - /* Check for duplicated filter.*/ - for (index = 0; index < NRF_BLE_SCAN_SHORT_NAME_CNT; index++) { + /* Check for duplicated filter. */ + for (index = 0; index < CONFIG_BLE_SCAN_SHORT_NAME_CNT; index++) { if (!strcmp(short_name_filter->short_name[index].short_target_name, short_name->short_name)) { return NRF_SUCCESS; } } - /* Add name to the filter.*/ + /* Add name to the filter. */ short_name_filter->short_name[(*counter)].short_name_min_len = short_name->short_name_min_len; memcpy(short_name_filter->short_name[(*counter)++].short_target_name, @@ -283,12 +295,12 @@ static int nrf_ble_scan_short_name_filter_add(struct nrf_ble_scan *const scan_ct #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const adv_report, - struct nrf_ble_scan const *const scan_ctx) + struct ble_scan const *const scan_ctx) { - nrf_ble_scan_uuid_filter_t const *uuid_filter = &scan_ctx->scan_filters.uuid_filter; + struct ble_scan_uuid_filter const *uuid_filter = &scan_ctx->scan_filters.uuid_filter; bool const all_filters_mode = scan_ctx->scan_filters.all_filters_mode; uint8_t const counter = scan_ctx->scan_filters.uuid_filter.uuid_cnt; uint8_t index; @@ -299,22 +311,20 @@ static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const adv_report, for (index = 0; index < counter; index++) { - if (ble_advdata_uuid_find(adv_report->data.data, data_len, - &uuid_filter->uuid[index])) { + if (ble_adv_data_uuid_find(adv_report->data.p_data, data_len, + &uuid_filter->uuid[index])) { uuid_match_cnt++; - /* In the normal filter mode, only one UUID is needed to match.*/ + /* In the normal filter mode, only one UUID is needed to match. */ if (!all_filters_mode) { break; } } else if (all_filters_mode) { break; - } else { - /* Do nothing.*/ } } - /* In the multifilter mode, all UUIDs must be found in the advertisement packets.*/ + /* In the multifilter mode, all UUIDs must be found in the advertisement packets. */ if ((all_filters_mode && (uuid_match_cnt == counter)) || ((!all_filters_mode) && (uuid_match_cnt > 0))) { return true; @@ -323,39 +333,39 @@ static bool adv_uuid_compare(ble_gap_evt_adv_report_t const *const adv_report, return false; } -static int nrf_ble_scan_uuid_filter_add(struct nrf_ble_scan *const scan_ctx, ble_uuid_t const *uuid) +static int ble_scan_uuid_filter_add(struct ble_scan *const scan_ctx, ble_uuid_t const *uuid) { ble_uuid_t *uuid_filter = scan_ctx->scan_filters.uuid_filter.uuid; uint8_t *counter = &scan_ctx->scan_filters.uuid_filter.uuid_cnt; uint8_t index; - /* If no memory.*/ - if (*counter >= NRF_BLE_SCAN_UUID_CNT) { + /* If no memory. */ + if (*counter >= CONFIG_BLE_SCAN_UUID_CNT) { return NRF_ERROR_NO_MEM; } /* Check for duplicated filter.*/ - for (index = 0; index < NRF_BLE_SCAN_UUID_CNT; index++) { + for (index = 0; index < CONFIG_BLE_SCAN_UUID_CNT; index++) { if (uuid_filter[index].uuid == uuid->uuid) { return NRF_SUCCESS; } } - /* Add UUID to the filter.*/ + /* Add UUID to the filter. */ uuid_filter[(*counter)++] = *uuid; LOG_DBG("Added filter on UUID %x", uuid->uuid); return NRF_SUCCESS; } -#endif /* NRF_BLE_SCAN_UUID_CNT*/ +#endif /* CONFIG_BLE_SCAN_UUID_CNT */ -#if (NRF_BLE_SCAN_APPEARANCE_CNT) +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT) static bool adv_appearance_compare(ble_gap_evt_adv_report_t const *const adv_report, - struct nrf_ble_scan const *const scan_ctx) + struct ble_scan const *const scan_ctx) { - nrf_ble_scan_appearance_filter_t const *appearance_filter = + struct ble_scan_appearance_filter const *appearance_filter = &scan_ctx->scan_filters.appearance_filter; uint8_t const counter = scan_ctx->scan_filters.appearance_filter.appearance_cnt; uint8_t index; @@ -363,45 +373,44 @@ static bool adv_appearance_compare(ble_gap_evt_adv_report_t const *const adv_rep data_len = adv_report->data.len; - /* Verify if the advertised appearance matches the provided appearance.*/ + /* Verify if the advertised appearance matches the provided appearance. */ for (index = 0; index < counter; index++) { - if (ble_advdata_appearance_find(adv_report->data.data, data_len, - &appearance_filter->appearance[index])) { + if (ble_adv_data_appearance_find(adv_report->data.p_data, data_len, + &appearance_filter->appearance[index])) { return true; } } return false; } -static int nrf_ble_scan_appearance_filter_add(struct nrf_ble_scan *const scan_ctx, - uint16_t appearance) +static int ble_scan_appearance_filter_add(struct ble_scan *const scan_ctx, uint16_t appearance) { uint16_t *appearance_filter = scan_ctx->scan_filters.appearance_filter.appearance; uint8_t *counter = &scan_ctx->scan_filters.appearance_filter.appearance_cnt; uint8_t index; - /* If no memory.*/ - if (*counter >= NRF_BLE_SCAN_APPEARANCE_CNT) { + /* If no memory. */ + if (*counter >= CONFIG_BLE_SCAN_APPEARANCE_CNT) { return NRF_ERROR_NO_MEM; } - /* Check for duplicated filter.*/ - for (index = 0; index < NRF_BLE_SCAN_APPEARANCE_CNT; index++) { + /* Check for duplicated filter. */ + for (index = 0; index < CONFIG_BLE_SCAN_APPEARANCE_CNT; index++) { if (appearance_filter[index] == appearance) { return NRF_SUCCESS; } } - /* Add appearance to the filter.*/ + /* Add appearance to the filter. */ appearance_filter[(*counter)++] = appearance; LOG_DBG("Added filter on appearance %x", appearance); return NRF_SUCCESS; } -#endif /* NRF_BLE_SCAN_APPEARANCE_CNT*/ +#endif /* CONFIG_BLE_SCAN_APPEARANCE_CNT */ -int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_scan_filter_type type, - void const *data) +int ble_scan_filter_set(struct ble_scan *const scan_ctx, enum ble_scan_filter_type type, + void const *data) { if (scan_ctx == NULL) { return NRF_ERROR_NULL; @@ -411,38 +420,43 @@ int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_sc } switch (type) { -#if (NRF_BLE_SCAN_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) case SCAN_NAME_FILTER: { char *name = (char *)data; - return nrf_ble_scan_name_filter_add(scan_ctx, name); + + return ble_scan_name_filter_add(scan_ctx, name); } #endif -#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_SHORT_NAME_CNT > 0) case SCAN_SHORT_NAME_FILTER: { - nrf_ble_scan_short_name_t *short_name = (nrf_ble_scan_short_name_t *)data; - return nrf_ble_scan_short_name_filter_add(scan_ctx, short_name); + struct ble_scan_short_name *short_name = (struct ble_scan_short_name *)data; + + return ble_scan_short_name_filter_add(scan_ctx, short_name); } #endif -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) case SCAN_ADDR_FILTER: { uint8_t *addr = (uint8_t *)data; - return nrf_ble_scan_addr_filter_add(scan_ctx, addr); + + return ble_scan_addr_filter_add(scan_ctx, addr); } #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) case SCAN_UUID_FILTER: { ble_uuid_t *uuid = (ble_uuid_t *)data; - return nrf_ble_scan_uuid_filter_add(scan_ctx, uuid); + + return ble_scan_uuid_filter_add(scan_ctx, uuid); } #endif -#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT > 0) case SCAN_APPEARANCE_FILTER: { uint16_t appearance = *((uint16_t *)data); - return nrf_ble_scan_appearance_filter_add(scan_ctx, appearance); + + return ble_scan_appearance_filter_add(scan_ctx, appearance); } #endif @@ -451,36 +465,41 @@ int nrf_ble_scan_filter_set(struct nrf_ble_scan *const scan_ctx, enum nrf_ble_sc } } -int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const scan_ctx) +int ble_scan_all_filter_remove(struct ble_scan *const scan_ctx) { -#if (NRF_BLE_SCAN_NAME_CNT > 0) - struct nrf_ble_scan_name_filter *name_filter = &scan_ctx->scan_filters.name_filter; +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) + struct ble_scan_name_filter *name_filter = &scan_ctx->scan_filters.name_filter; + memset(name_filter->target_name, 0, sizeof(name_filter->target_name)); name_filter->name_cnt = 0; #endif -#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) - nrf_ble_scan_short_name_filter_t *short_name_filter = +#if (CONFIG_BLE_SCAN_SHORT_NAME_CNT > 0) + struct ble_scan_short_name_filter *short_name_filter = &scan_ctx->scan_filters.short_name_filter; + memset(short_name_filter->short_name, 0, sizeof(short_name_filter->short_name)); short_name_filter->name_cnt = 0; #endif -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) - nrf_ble_scan_addr_filter_t *addr_filter = &scan_ctx->scan_filters.addr_filter; +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) + struct ble_scan_addr_filter *addr_filter = &scan_ctx->scan_filters.addr_filter; + memset(addr_filter->target_addr, 0, sizeof(addr_filter->target_addr)); addr_filter->addr_cnt = 0; #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) - nrf_ble_scan_uuid_filter_t *uuid_filter = &scan_ctx->scan_filters.uuid_filter; +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) + struct ble_scan_uuid_filter *uuid_filter = &scan_ctx->scan_filters.uuid_filter; + memset(uuid_filter->uuid, 0, sizeof(uuid_filter->uuid)); uuid_filter->uuid_cnt = 0; #endif -#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) - nrf_ble_scan_appearance_filter_t *appearance_filter = +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT > 0) + struct ble_scan_appearance_filter *appearance_filter = &scan_ctx->scan_filters.appearance_filter; + memset(appearance_filter->appearance, 0, sizeof(appearance_filter->appearance)); appearance_filter->appearance_cnt = 0; #endif @@ -488,87 +507,89 @@ int nrf_ble_scan_all_filter_remove(struct nrf_ble_scan *const scan_ctx) return NRF_SUCCESS; } -int nrf_ble_scan_filters_enable(struct nrf_ble_scan *const scan_ctx, uint8_t mode, bool match_all) +int ble_scan_filters_enable(struct ble_scan *const scan_ctx, uint8_t mode, bool match_all) { if (!scan_ctx) { return NRF_ERROR_NULL; } - /* Check if the mode is correct.*/ - if ((!(mode & NRF_BLE_SCAN_ADDR_FILTER)) && (!(mode & NRF_BLE_SCAN_NAME_FILTER)) && - (!(mode & NRF_BLE_SCAN_UUID_FILTER)) && (!(mode & NRF_BLE_SCAN_SHORT_NAME_FILTER)) && - (!(mode & NRF_BLE_SCAN_APPEARANCE_FILTER))) { + /* Check if the mode is correct. */ + if ((!(mode & BLE_SCAN_ADDR_FILTER)) && (!(mode & BLE_SCAN_NAME_FILTER)) && + (!(mode & BLE_SCAN_UUID_FILTER)) && (!(mode & BLE_SCAN_SHORT_NAME_FILTER)) && + (!(mode & BLE_SCAN_APPEARANCE_FILTER))) { return NRF_ERROR_INVALID_PARAM; } int err_code; /* Disable filters.*/ - err_code = nrf_ble_scan_filters_disable(scan_ctx); - /* ASSERT(err_code == NRF_SUCCESS);*/ + err_code = ble_scan_filters_disable(scan_ctx); + if (err_code != NRF_SUCCESS) { + return err_code; + } - struct nrf_ble_scan_filters *filters = &scan_ctx->scan_filters; + struct ble_scan_filters *filters = &scan_ctx->scan_filters; - /* Turn on the filters of your choice.*/ -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) - if (mode & NRF_BLE_SCAN_ADDR_FILTER) { + /* Turn on the filters of your choice. */ +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) + if (mode & BLE_SCAN_ADDR_FILTER) { filters->addr_filter.addr_filter_enabled = true; } #endif -#if (NRF_BLE_SCAN_NAME_CNT > 0) - if (mode & NRF_BLE_SCAN_NAME_FILTER) { +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) + if (mode & BLE_SCAN_NAME_FILTER) { filters->name_filter.name_filter_enabled = true; } #endif -#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) - if (mode & NRF_BLE_SCAN_SHORT_NAME_FILTER) { +#if (CONFIG_BLE_SCAN_SHORT_NAME_CNT > 0) + if (mode & BLE_SCAN_SHORT_NAME_FILTER) { filters->short_name_filter.short_name_filter_enabled = true; } #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) - if (mode & NRF_BLE_SCAN_UUID_FILTER) { +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) + if (mode & BLE_SCAN_UUID_FILTER) { filters->uuid_filter.uuid_filter_enabled = true; } #endif -#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) - if (mode & NRF_BLE_SCAN_APPEARANCE_FILTER) { +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT > 0) + if (mode & BLE_SCAN_APPEARANCE_FILTER) { filters->appearance_filter.appearance_filter_enabled = true; } #endif - /* Select the filter mode.*/ + /* Select the filter mode. */ filters->all_filters_mode = match_all; return NRF_SUCCESS; } -int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const scan_ctx) +int ble_scan_filters_disable(struct ble_scan *const scan_ctx) { if (!scan_ctx) { return NRF_ERROR_NULL; } /* Disable all filters.*/ -#if (NRF_BLE_SCAN_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) bool *name_filter_enabled = &scan_ctx->scan_filters.name_filter.name_filter_enabled; *name_filter_enabled = false; #endif -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) bool *addr_filter_enabled = &scan_ctx->scan_filters.addr_filter.addr_filter_enabled; *addr_filter_enabled = false; #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) bool *uuid_filter_enabled = &scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled; *uuid_filter_enabled = false; #endif -#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT > 0) bool *appearance_filter_enabled = &scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled; *appearance_filter_enabled = false; @@ -577,8 +598,7 @@ int nrf_ble_scan_filters_disable(struct nrf_ble_scan *const scan_ctx) return NRF_SUCCESS; } -int nrf_ble_scan_filter_get(struct nrf_ble_scan *const scan_ctx, - struct nrf_ble_scan_filters *status) +int ble_scan_filter_get(struct ble_scan *const scan_ctx, struct ble_scan_filters *status) { if (scan_ctx == NULL) { return NRF_ERROR_NULL; @@ -592,158 +612,160 @@ int nrf_ble_scan_filter_get(struct nrf_ble_scan *const scan_ctx, return NRF_SUCCESS; } -#endif /* CONFIG_BT_SCAN_FILTER_ENABLE*/ +#endif /* CONFIG_BLE_SCAN_FILTER_ENABLE */ -static void nrf_ble_scan_on_adv_report(struct nrf_ble_scan const *const scan_ctx, - ble_gap_evt_adv_report_t const *const adv_report) +static void ble_scan_on_adv_report(struct ble_scan const *const scan_ctx, + ble_gap_evt_adv_report_t const *const adv_report) { struct scan_evt scan_evt = {0}; -#if CONFIG_BT_SCAN_FILTER_ENABLE || 1 +#if CONFIG_BLE_SCAN_FILTER_ENABLE uint8_t filter_cnt = 0; uint8_t filter_match_cnt = 0; #endif scan_evt.scan_params = &scan_ctx->scan_params; - /* If the whitelist is used, do not check the filters and return.*/ + /* If the whitelist is used, do not check the filters and return. */ if (is_whitelist_used(scan_ctx)) { - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT; + scan_evt.scan_evt_id = BLE_SCAN_EVT_WHITELIST_ADV_REPORT; scan_evt.params.not_found = adv_report; scan_ctx->evt_handler(&scan_evt); sd_ble_gap_scan_start(NULL, &scan_ctx->scan_buffer); - nrf_ble_scan_connect_with_target(scan_ctx, adv_report); + ble_scan_connect_with_target(scan_ctx, adv_report); return; } -#if BT_SCAN_FILTER_ENABLE +#if CONFIG_BLE_SCAN_FILTER_ENABLE bool const all_filter_mode = scan_ctx->scan_filters.all_filters_mode; bool is_filter_matched = false; -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) bool const addr_filter_enabled = scan_ctx->scan_filters.addr_filter.addr_filter_enabled; #endif -#if (NRF_BLE_SCAN_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) bool const name_filter_enabled = scan_ctx->scan_filters.name_filter.name_filter_enabled; #endif -#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_SHORT_NAME_CNT > 0) bool const short_name_filter_enabled = scan_ctx->scan_filters.short_name_filter.short_name_filter_enabled; #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) bool const uuid_filter_enabled = scan_ctx->scan_filters.uuid_filter.uuid_filter_enabled; #endif -#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT > 0) bool const appearance_filter_enabled = scan_ctx->scan_filters.appearance_filter.appearance_filter_enabled; #endif -#if (NRF_BLE_SCAN_ADDRESS_CNT > 0) - /* Check the address filter.*/ +#if (CONFIG_BLE_SCAN_ADDRESS_CNT > 0) + /* Check the address filter. */ if (addr_filter_enabled) { - /* Number of active filters.*/ + /* Number of active filters. */ filter_cnt++; if (adv_addr_compare(adv_report, scan_ctx)) { - /* Number of filters matched.*/ + /* Number of filters matched. */ filter_match_cnt++; - /* Information about the filters matched.*/ + /* Information about the filters matched. */ scan_evt.params.filter_match.filter_match.address_filter_match = true; is_filter_matched = true; } } #endif -#if (NRF_BLE_SCAN_NAME_CNT > 0) - /* Check the name filter.*/ +#if (CONFIG_BLE_SCAN_NAME_CNT > 0) + /* Check the name filter. */ if (name_filter_enabled) { filter_cnt++; if (adv_name_compare(adv_report, scan_ctx)) { filter_match_cnt++; - /* Information about the filters matched.*/ + /* Information about the filters matched. */ scan_evt.params.filter_match.filter_match.name_filter_match = true; is_filter_matched = true; } } #endif -#if (NRF_BLE_SCAN_SHORT_NAME_CNT > 0) +#if (CONFIG_BLE_SCAN_SHORT_NAME_CNT > 0) if (short_name_filter_enabled) { filter_cnt++; if (adv_short_name_compare(adv_report, scan_ctx)) { filter_match_cnt++; - /* Information about the filters matched.*/ + /* Information about the filters matched. */ scan_evt.params.filter_match.filter_match.short_name_filter_match = true; is_filter_matched = true; } } #endif -#if (NRF_BLE_SCAN_UUID_CNT > 0) - /* Check the UUID filter.*/ +#if (CONFIG_BLE_SCAN_UUID_CNT > 0) + /* Check the UUID filter. */ if (uuid_filter_enabled) { filter_cnt++; if (adv_uuid_compare(adv_report, scan_ctx)) { filter_match_cnt++; - /* Information about the filters matched.*/ + /* Information about the filters matched. */ scan_evt.params.filter_match.filter_match.uuid_filter_match = true; is_filter_matched = true; } } #endif -#if (NRF_BLE_SCAN_APPEARANCE_CNT > 0) - /* Check the appearance filter.*/ +#if (CONFIG_BLE_SCAN_APPEARANCE_CNT > 0) + /* Check the appearance filter. */ if (appearance_filter_enabled) { filter_cnt++; if (adv_appearance_compare(adv_report, scan_ctx)) { filter_match_cnt++; - /* Information about the filters matched.*/ + /* Information about the filters matched. */ scan_evt.params.filter_match.filter_match.appearance_filter_match = true; is_filter_matched = true; } } - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND; + scan_evt.scan_evt_id = BLE_SCAN_EVT_NOT_FOUND; #endif scan_evt.params.filter_match.adv_report = adv_report; - /* In the multifilter mode, the number of the active filters must equal the number of the*/ - /* filters matched to generate the notification.*/ + /** In the multifilter mode, the number of the active filters must equal the number of the + * filters matched to generate the notification. + */ if (all_filter_mode && (filter_match_cnt == filter_cnt)) { - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH; - nrf_ble_scan_connect_with_target(scan_ctx, adv_report); + scan_evt.scan_evt_id = BLE_SCAN_EVT_FILTER_MATCH; + ble_scan_connect_with_target(scan_ctx, adv_report); } - /* In the normal filter mode, only one filter match is needed to generate the notification*/ - /* to the main application.*/ + /** In the normal filter mode, only one filter match is needed to generate the notification + * to the main application. + */ else if ((!all_filter_mode) && is_filter_matched) { - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_FILTER_MATCH; - nrf_ble_scan_connect_with_target(scan_ctx, adv_report); + scan_evt.scan_evt_id = BLE_SCAN_EVT_FILTER_MATCH; + ble_scan_connect_with_target(scan_ctx, adv_report); } else { - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_NOT_FOUND; + scan_evt.scan_evt_id = BLE_SCAN_EVT_NOT_FOUND; scan_evt.params.not_found = adv_report; } - /* If the event handler is not NULL, notify the main application.*/ + /* If the event handler is not NULL, notify the main application. */ if (scan_ctx->evt_handler != NULL) { scan_ctx->evt_handler(&scan_evt); } -#endif /* BT_SCAN_FILTER_ENABLE*/ +#endif /* CONFIG_BLE_SCAN_FILTER_ENABLE*/ - /* Resume the scanning.*/ + /* Resume the scanning. */ (void)sd_ble_gap_scan_start(NULL, &scan_ctx->scan_buffer); } -bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx) +bool is_whitelist_used(struct ble_scan const *const scan_ctx) { if (scan_ctx->scan_params.filter_policy == BLE_GAP_SCAN_FP_WHITELIST || scan_ctx->scan_params.filter_policy == @@ -754,32 +776,32 @@ bool is_whitelist_used(struct nrf_ble_scan const *const scan_ctx) return false; } -static void nrf_ble_scan_default_param_set(struct nrf_ble_scan *const scan_ctx) +static void ble_scan_default_param_set(struct ble_scan *const scan_ctx) { - /* Set the default parameters.*/ + /* Set the default parameters. */ scan_ctx->scan_params.active = 1; #if (NRF_SD_BLE_API_VERSION > 7) - scan_ctx->scan_params.interval_us = NRF_BLE_SCAN_SCAN_INTERVAL * UNIT_0_625_MS; - scan_ctx->scan_params.window_us = NRF_BLE_SCAN_SCAN_WINDOW * UNIT_0_625_MS; + scan_ctx->scan_params.interval_us = CONFG_BLE_SCAN_SCAN_INTERVAL * UNIT_0_625_MS; + scan_ctx->scan_params.window_us = CONFIG_BLE_SCAN_SCAN_WINDOW * UNIT_0_625_MS; #else - scan_ctx->scan_params.interval = NRF_BLE_SCAN_SCAN_INTERVAL; - scan_ctx->scan_params.window = NRF_BLE_SCAN_SCAN_WINDOW; -#endif /* #if (NRF_SD_BLE_API_VERSION > 7)*/ - scan_ctx->scan_params.timeout = NRF_BLE_SCAN_SCAN_DURATION; + scan_ctx->scan_params.interval = CONFIG_BLE_SCAN_SCAN_INTERVAL; + scan_ctx->scan_params.window = CONFIG_BLE_SCAN_SCAN_WINDOW; +#endif /* #if (NRF_SD_BLE_API_VERSION > 7) */ + scan_ctx->scan_params.timeout = CONFIG_BLE_SCAN_SCAN_DURATION; scan_ctx->scan_params.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL; scan_ctx->scan_params.scan_phys = BLE_GAP_PHY_1MBPS; } -static void nrf_ble_scan_default_conn_param_set(struct nrf_ble_scan *const scan_ctx) +static void ble_scan_default_conn_param_set(struct ble_scan *const scan_ctx) { scan_ctx->conn_params.conn_sup_timeout = BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN; - scan_ctx->conn_params.min_conn_interval = BLE_GAP_CONN_INTVL_MS_TO_UNITS(25); - scan_ctx->conn_params.max_conn_interval = BLE_GAP_CONN_INTVL_MS_TO_UNITS(25); - scan_ctx->conn_params.slave_latency = (uint16_t)NRF_BLE_SCAN_SLAVE_LATENCY; + scan_ctx->conn_params.min_conn_interval = CONFIG_BLE_SCAN_MIN_CONNECTION_INTERVAL; + scan_ctx->conn_params.max_conn_interval = CONFIG_BLE_SCAN_MAX_CONNECTION_INTERVAL; + scan_ctx->conn_params.slave_latency = (uint16_t)CONFIG_BLE_SCAN_SLAVE_LATENCY; } -static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, - ble_gap_evt_t const *const gap) +static void ble_scan_on_timeout(struct ble_scan const *const scan_ctx, + ble_gap_evt_t const *const gap) { ble_gap_evt_timeout_t const *timeout = &gap->params.timeout; struct scan_evt scan_evt = {0}; @@ -787,7 +809,7 @@ static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, if (timeout->src == BLE_GAP_TIMEOUT_SRC_SCAN) { LOG_DBG("BLE_GAP_SCAN_TIMEOUT"); if (scan_ctx->evt_handler != NULL) { - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_SCAN_TIMEOUT; + scan_evt.scan_evt_id = BLE_SCAN_EVT_SCAN_TIMEOUT; scan_evt.scan_params = &scan_ctx->scan_params; scan_evt.params.timeout.src = timeout->src; @@ -796,16 +818,16 @@ static void nrf_ble_scan_on_timeout(struct nrf_ble_scan const *const scan_ctx, } } -void nrf_ble_scan_stop(void) +void ble_scan_stop(void) { - /* It is ok to ignore the function return value here, because this function can return*/ - /* NRF_SUCCESS or NRF_ERROR_INVALID_STATE, when app is not in the scanning state.*/ + /** It is ok to ignore the function return value here, because this function can return + * NRF_SUCCESS or NRF_ERROR_INVALID_STATE, when app is not in the scanning state. + */ (void)sd_ble_gap_scan_stop(); } -int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, - struct nrf_ble_scan_init const *const init, - nrf_ble_scan_evt_handler_t evt_handler) +int ble_scan_init(struct ble_scan *const scan_ctx, struct ble_scan_init const *const init, + ble_scan_evt_handler_t evt_handler) { if (scan_ctx == NULL) { return NRF_ERROR_NULL; @@ -813,12 +835,13 @@ int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, scan_ctx->evt_handler = evt_handler; -#if BT_SCAN_FILTER_ENABLE - /* Disable all scanning filters.*/ +#if CONFIG_BLE_SCAN_FILTER_ENABLE + /* Disable all scanning filters. */ memset(&scan_ctx->scan_filters, 0, sizeof(scan_ctx->scan_filters)); #endif - /* If the pointer to the initialization structure exist, use it to scan the configuration.*/ + /** If the pointer to the initialization structure exist, use it to scan the configuration. + */ if (init != NULL) { scan_ctx->connect_if_match = init->connect_if_match; scan_ctx->conn_cfg_tag = init->conn_cfg_tag; @@ -826,33 +849,33 @@ int nrf_ble_scan_init(struct nrf_ble_scan *const scan_ctx, if (init->scan_param != NULL) { scan_ctx->scan_params = *init->scan_param; } else { - /* Use the default static configuration.*/ - nrf_ble_scan_default_param_set(scan_ctx); + /* Use the default static configuration. */ + ble_scan_default_param_set(scan_ctx); } if (init->conn_param != NULL) { scan_ctx->conn_params = *init->conn_param; } else { - /* Use the default static configuration.*/ - nrf_ble_scan_default_conn_param_set(scan_ctx); + /* Use the default static configuration. */ + ble_scan_default_conn_param_set(scan_ctx); } } - /* If pointer is NULL, use the static default configuration.*/ + /* If pointer is NULL, use the static default configuration. */ else { - nrf_ble_scan_default_param_set(scan_ctx); - nrf_ble_scan_default_conn_param_set(scan_ctx); + ble_scan_default_param_set(scan_ctx); + ble_scan_default_conn_param_set(scan_ctx); scan_ctx->connect_if_match = false; } - /* Assign a buffer where the advertising reports are to be stored by the SoftDevice.*/ - scan_ctx->scan_buffer.data = scan_ctx->scan_buffer_data; - scan_ctx->scan_buffer.len = NRF_BLE_SCAN_BUFFER; + /* Assign a buffer where the advertising reports are to be stored by the SoftDevice. */ + scan_ctx->scan_buffer.p_data = scan_ctx->scan_buffer_data; + scan_ctx->scan_buffer.len = CONFIG_BLE_SCAN_BUFFER; return NRF_SUCCESS; } -int nrf_ble_scan_start(struct nrf_ble_scan const *const scan_ctx) +int ble_scan_start(struct ble_scan const *const scan_ctx) { if (!scan_ctx) { @@ -862,45 +885,45 @@ int nrf_ble_scan_start(struct nrf_ble_scan const *const scan_ctx) int err_code; struct scan_evt scan_evt = {0}; - nrf_ble_scan_stop(); + ble_scan_stop(); - /* If the whitelist is used and the event handler is not NULL, send the whitelist request - * to*/ - /* the main application.*/ + /** If the whitelist is used and the event handler is not NULL, send the whitelist request + * to the main application. + */ if (is_whitelist_used(scan_ctx)) { if (scan_ctx->evt_handler != NULL) { - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_WHITELIST_REQUEST; + scan_evt.scan_evt_id = BLE_SCAN_EVT_WHITELIST_REQUEST; scan_ctx->evt_handler(&scan_evt); } } - /* Start the scanning.*/ + /* Start the scanning. */ err_code = sd_ble_gap_scan_start(&scan_ctx->scan_params, &scan_ctx->scan_buffer); - /* It is okay to ignore this error, because the scan stopped earlier.*/ + /* It is okay to ignore this error, because the scan stopped earlier. */ if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_SUCCESS)) { LOG_ERR("sd_ble_gap_scan_start returned 0x%x", err_code); - return (err_code); + return err_code; } LOG_DBG("Scanning"); return NRF_SUCCESS; } -int nrf_ble_scan_params_set(struct nrf_ble_scan *const scan_ctx, - ble_gap_scan_params_t const *const scan_param) +int ble_scan_params_set(struct ble_scan *const scan_ctx, + ble_gap_scan_params_t const *const scan_param) { if (!scan_ctx) { return NRF_ERROR_NULL; } - nrf_ble_scan_stop(); + ble_scan_stop(); if (scan_param != NULL) { - /* Assign new scanning parameters.*/ + /* Assign new scanning parameters. */ scan_ctx->scan_params = *scan_param; } else { - /* If NULL, use the default static configuration.*/ - nrf_ble_scan_default_param_set(scan_ctx); + /* If NULL, use the default static configuration. */ + ble_scan_default_param_set(scan_ctx); } LOG_DBG("Scanning parameters have been changed successfully"); @@ -908,12 +931,12 @@ int nrf_ble_scan_params_set(struct nrf_ble_scan *const scan_ctx, return NRF_SUCCESS; } -static void nrf_ble_scan_on_connected_evt(struct nrf_ble_scan const *const scan_ctx, - ble_gap_evt_t const *const gap_evt) +static void ble_scan_on_connected_evt(struct ble_scan const *const scan_ctx, + ble_gap_evt_t const *const gap_evt) { struct scan_evt scan_evt = {0}; - scan_evt.scan_evt_id = NRF_BLE_SCAN_EVT_CONNECTED; + scan_evt.scan_evt_id = BLE_SCAN_EVT_CONNECTED; scan_evt.params.connected.connected = &gap_evt->params.connected; scan_evt.params.connected.conn_handle = gap_evt->conn_handle; scan_evt.scan_params = &scan_ctx->scan_params; @@ -923,8 +946,8 @@ static void nrf_ble_scan_on_connected_evt(struct nrf_ble_scan const *const scan_ } } -int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *gap_addr, - const uint8_t addr[BLE_GAP_ADDR_LEN]) +int ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *gap_addr, + const uint8_t addr[BLE_GAP_ADDR_LEN]) { if (!gap_addr) { return NRF_ERROR_NULL; @@ -937,23 +960,23 @@ int nrf_ble_scan_copy_addr_to_sd_gap_addr(ble_gap_addr_t *gap_addr, return NRF_SUCCESS; } -void nrf_ble_scan_on_ble_evt(ble_evt_t const *ble_evt, void *contex) +void ble_scan_on_ble_evt(ble_evt_t const *ble_evt, void *contex) { - struct nrf_ble_scan *scan_data = (struct nrf_ble_scan *)contex; + struct ble_scan *scan_data = (struct ble_scan *)contex; ble_gap_evt_adv_report_t const *adv_report = &ble_evt->evt.gap_evt.params.adv_report; ble_gap_evt_t const *gap_evt = &ble_evt->evt.gap_evt; switch (ble_evt->header.evt_id) { case BLE_GAP_EVT_ADV_REPORT: - nrf_ble_scan_on_adv_report(scan_data, adv_report); + ble_scan_on_adv_report(scan_data, adv_report); break; case BLE_GAP_EVT_TIMEOUT: - nrf_ble_scan_on_timeout(scan_data, gap_evt); + ble_scan_on_timeout(scan_data, gap_evt); break; case BLE_GAP_EVT_CONNECTED: - nrf_ble_scan_on_connected_evt(scan_data, gap_evt); + ble_scan_on_connected_evt(scan_data, gap_evt); break; default: From fdf32ad5ae11850dbb0ac2f01172c59cd5cb210a Mon Sep 17 00:00:00 2001 From: Sondre Pettersen Date: Tue, 18 Nov 2025 15:58:35 +0100 Subject: [PATCH 6/6] lib: scan: Add unit test and documentation Signed-off-by: Sondre Pettersen --- doc/nrf-bm/api/api.rst | 9 + tests/lib/bluetooth/ble_scan/CMakeLists.txt | 61 ++++ tests/lib/bluetooth/ble_scan/prj.conf | 6 + tests/lib/bluetooth/ble_scan/src/unity_test.c | 327 ++++++++++++++++++ tests/lib/bluetooth/ble_scan/testcase.yaml | 4 + 5 files changed, 407 insertions(+) create mode 100644 tests/lib/bluetooth/ble_scan/CMakeLists.txt create mode 100644 tests/lib/bluetooth/ble_scan/prj.conf create mode 100644 tests/lib/bluetooth/ble_scan/src/unity_test.c create mode 100644 tests/lib/bluetooth/ble_scan/testcase.yaml diff --git a/doc/nrf-bm/api/api.rst b/doc/nrf-bm/api/api.rst index fc79992291..068e69ab96 100644 --- a/doc/nrf-bm/api/api.rst +++ b/doc/nrf-bm/api/api.rst @@ -154,6 +154,15 @@ Record Access Control Point .. _api_sensorsim: +Scan library +============ + +.. doxygengroup:: ble_scan + :inner: + :members: + +.. _api_ble_scan: + Sensor data simulator library ============================= diff --git a/tests/lib/bluetooth/ble_scan/CMakeLists.txt b/tests/lib/bluetooth/ble_scan/CMakeLists.txt new file mode 100644 index 0000000000..612fa6a8a1 --- /dev/null +++ b/tests/lib/bluetooth/ble_scan/CMakeLists.txt @@ -0,0 +1,61 @@ +# +# Copyright (c) 2025 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + + +project(unit_test_ble_scan) + +add_compile_definitions( + SVCALL_AS_NORMAL_FUNCTION + SUPPRESS_INLINE_IMPLEMENTATION + NRF54L15_XXAA + CONFIG_BLE_SCAN_BUFFER=31 + CONFIG_BLE_SCAN_NAME_MAX_LEN=32 + CONFIG_BLE_SCAN_SHORT_NAME_MAX_LEN=32 + CONFIG_BLE_SCAN_NAME_CNT=1 + CONFIG_BLE_SCAN_APPEARANCE_CNT=1 + CONFIG_BLE_SCAN_ADDRESS_CNT=1 + CONFIG_BLE_SCAN_SHORT_NAME_CNT=1 + CONFIG_BLE_SCAN_UUID_CNT=1 + CONFIG_BLE_SCAN_SCAN_INTERVAL=160 + CONFIG_BLE_SCAN_SCAN_DURATION=0x0000 + CONFIG_BLE_SCAN_SCAN_WINDOW=80 + CONFIG_BLE_SCAN_SLAVE_LATENCY=0 + CONFIG_BLE_SCAN_MIN_CONNECTION_INTERVAL=0x0006 + CONFIG_BLE_SCAN_MAX_CONNECTION_INTERVAL=0x0018 + CONFIG_BLE_SCAN_SUPERVISION_TIMEOUT=0x0C80 + CONFIG_BLE_SCAN_FILTER_ENABLE=1 +) + +set(SOFTDEVICE_VARIANT "s145") +set(SOFTDEVICE_INCLUDE_DIR "${ZEPHYR_NRF_BM_MODULE_DIR}/components/softdevice/${SOFTDEVICE_VARIANT}/${SOFTDEVICE_VARIANT}_API/include") + +cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble.h) +cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gap.h + WORD_EXCLUDE + "__STATIC_INLINE") +cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gatts.h + WORD_EXCLUDE + "__STATIC_INLINE") +cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gattc.h + WORD_EXCLUDE + "__STATIC_INLINE") + +target_include_directories(app PRIVATE ${ZEPHYR_NRF_BM_MODULE_DIR}/include) +target_include_directories(app PRIVATE ${SOFTDEVICE_INCLUDE_DIR}) +target_include_directories(app PRIVATE ${ZEPHYR_HAL_NORDIC_MODULE_DIR}/nrfx/mdk) +target_include_directories(app PRIVATE ${ZEPHYR_CMSIS_MODULE_DIR}/CMSIS/Core/Include) + +test_runner_generate(src/unity_test.c) +target_sources(app PRIVATE src/unity_test.c) + +target_sources(app + PRIVATE + ${ZEPHYR_NRF_BM_MODULE_DIR}/lib/bluetooth/ble_scan/ble_scan.c +) diff --git a/tests/lib/bluetooth/ble_scan/prj.conf b/tests/lib/bluetooth/ble_scan/prj.conf new file mode 100644 index 0000000000..a8788847da --- /dev/null +++ b/tests/lib/bluetooth/ble_scan/prj.conf @@ -0,0 +1,6 @@ +# +# Copyright (c) 2025 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +CONFIG_UNITY=y diff --git a/tests/lib/bluetooth/ble_scan/src/unity_test.c b/tests/lib/bluetooth/ble_scan/src/unity_test.c new file mode 100644 index 0000000000..3d523a4846 --- /dev/null +++ b/tests/lib/bluetooth/ble_scan/src/unity_test.c @@ -0,0 +1,327 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include +#include +#include +#include + +#include "cmock_ble.h" +#include "cmock_ble_gap.h" +#include "cmock_ble_gatts.h" +#include "cmock_ble_gattc.h" + +void scan_event_handler_func(struct scan_evt const *scan_evt) +{ +} + +void test_ble_scan_init(void) +{ + int err; + + err = ble_scan_init(NULL, NULL, NULL); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, err); + + ble_scan_evt_handler_t scan_event = scan_event_handler_func; + + struct ble_scan ble_scan_module = {0}; + + struct ble_scan_init scan_init = {.scan_param = NULL, .conn_param = NULL}; + + err = ble_scan_init(&ble_scan_module, NULL, scan_event); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); + + err = ble_scan_init(&ble_scan_module, &scan_init, scan_event); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); + + ble_gap_scan_params_t dummy_scan_params = {.extended = 1, + .report_incomplete_evts = 1, + .active = 1, + .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, + .scan_phys = BLE_GAP_PHY_1MBPS, + .interval = CONFIG_BLE_SCAN_SCAN_INTERVAL, + .window = CONFIG_BLE_SCAN_SCAN_WINDOW, + .timeout = CONFIG_BLE_SCAN_SCAN_DURATION, + .channel_mask = {1, 1, 1, 1, 1}}; + + ble_gap_conn_params_t dummy_conn_params = {.min_conn_interval = 1, + .max_conn_interval = 10, + .slave_latency = CONFIG_BLE_SCAN_SLAVE_LATENCY, + .conn_sup_timeout = + BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN}; + + scan_init.scan_param = &dummy_scan_params; + scan_init.conn_param = &dummy_conn_params; + err = ble_scan_init(&ble_scan_module, &scan_init, scan_event); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); +} + +void test_ble_scan_start(void) +{ + int err; + + ble_scan_evt_handler_t scan_event = scan_event_handler_func; + + ble_gap_scan_params_t dummy_scan_params = {.extended = 1, + .report_incomplete_evts = 1, + .active = 1, + .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, + .scan_phys = BLE_GAP_PHY_1MBPS, + .interval = CONFIG_BLE_SCAN_SCAN_INTERVAL, + .window = CONFIG_BLE_SCAN_SCAN_WINDOW, + .timeout = CONFIG_BLE_SCAN_SCAN_DURATION, + .channel_mask = {1, 1, 1, 1, 1}}; + + ble_gap_conn_params_t dummy_conn_params = {.min_conn_interval = 1, + .max_conn_interval = 10, + .slave_latency = CONFIG_BLE_SCAN_SLAVE_LATENCY, + .conn_sup_timeout = + BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN}; + + struct ble_scan ble_scan_module = {0}; + + struct ble_scan_init scan_init = {.scan_param = &dummy_scan_params, + .conn_param = &dummy_conn_params}; + + err = ble_scan_init(&ble_scan_module, &scan_init, scan_event); + + err = ble_scan_start(NULL); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, err); + + __cmock_sd_ble_gap_scan_stop_IgnoreAndReturn(NRF_SUCCESS); + __cmock_sd_ble_gap_scan_start_ExpectAndReturn(&ble_scan_module.scan_params, + &ble_scan_module.scan_buffer, NRF_SUCCESS); + err = ble_scan_start(&ble_scan_module); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); +} + +void test_ble_scan_filters_enable(void) +{ + int err; + + err = ble_scan_filters_enable(NULL, BLE_SCAN_ADDR_FILTER, true); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, err); + + ble_scan_evt_handler_t scan_event = scan_event_handler_func; + + ble_gap_scan_params_t dummy_scan_params = {.extended = 1, + .report_incomplete_evts = 1, + .active = 1, + .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, + .scan_phys = BLE_GAP_PHY_1MBPS, + .interval = CONFIG_BLE_SCAN_SCAN_INTERVAL, + .window = CONFIG_BLE_SCAN_SCAN_WINDOW, + .timeout = CONFIG_BLE_SCAN_SCAN_DURATION, + .channel_mask = {1, 1, 1, 1, 1}}; + + ble_gap_conn_params_t dummy_conn_params = {.min_conn_interval = 1, + .max_conn_interval = 10, + .slave_latency = CONFIG_BLE_SCAN_SLAVE_LATENCY, + .conn_sup_timeout = + BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN}; + + struct ble_scan ble_scan_module = {0}; + + struct ble_scan_init scan_init = {.scan_param = &dummy_scan_params, + .conn_param = &dummy_conn_params}; + + err = ble_scan_init(&ble_scan_module, &scan_init, scan_event); + + err = ble_scan_filters_enable(&ble_scan_module, 0x20, true); + TEST_ASSERT_EQUAL(NRF_ERROR_INVALID_PARAM, err); + + err = ble_scan_filters_enable(&ble_scan_module, BLE_SCAN_ALL_FILTER, true); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); +} + +void test_ble_scan_filters_disable(void) +{ + int err; + + err = ble_scan_filters_disable(NULL); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, err); + + ble_scan_evt_handler_t scan_event = scan_event_handler_func; + + ble_gap_scan_params_t dummy_scan_params = {.extended = 1, + .report_incomplete_evts = 1, + .active = 1, + .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, + .scan_phys = BLE_GAP_PHY_1MBPS, + .interval = CONFIG_BLE_SCAN_SCAN_INTERVAL, + .window = CONFIG_BLE_SCAN_SCAN_WINDOW, + .timeout = CONFIG_BLE_SCAN_SCAN_DURATION, + .channel_mask = {1, 1, 1, 1, 1}}; + + ble_gap_conn_params_t dummy_conn_params = {.min_conn_interval = 1, + .max_conn_interval = 10, + .slave_latency = CONFIG_BLE_SCAN_SLAVE_LATENCY, + .conn_sup_timeout = + BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN}; + + struct ble_scan ble_scan_module = {0}; + + struct ble_scan_init scan_init = {.scan_param = &dummy_scan_params, + .conn_param = &dummy_conn_params}; + + err = ble_scan_init(&ble_scan_module, &scan_init, scan_event); + + err = ble_scan_filters_disable(&ble_scan_module); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); +} + +void test_ble_scan_filter_get(void) +{ + int err; + + struct ble_scan ble_scan_module = {0}; + + struct ble_scan_filters ble_scan_filter_data = {0}; + + ble_scan_evt_handler_t scan_event = scan_event_handler_func; + + ble_gap_scan_params_t dummy_scan_params = {.extended = 1, + .report_incomplete_evts = 1, + .active = 1, + .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, + .scan_phys = BLE_GAP_PHY_1MBPS, + .interval = CONFIG_BLE_SCAN_SCAN_INTERVAL, + .window = CONFIG_BLE_SCAN_SCAN_WINDOW, + .timeout = CONFIG_BLE_SCAN_SCAN_DURATION, + .channel_mask = {1, 1, 1, 1, 1}}; + + ble_gap_conn_params_t dummy_conn_params = {.min_conn_interval = 1, + .max_conn_interval = 10, + .slave_latency = CONFIG_BLE_SCAN_SLAVE_LATENCY, + .conn_sup_timeout = + BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN}; + + struct ble_scan_init scan_init = {.scan_param = &dummy_scan_params, + .conn_param = &dummy_conn_params}; + + ble_scan_init(&ble_scan_module, &scan_init, scan_event); + + ble_scan_filters_enable(&ble_scan_module, BLE_SCAN_ALL_FILTER, true); + + char *device_name = "generic_device"; + + ble_scan_filter_set(&ble_scan_module, BLE_SCAN_ALL_FILTER, device_name); + + err = ble_scan_filter_get(&ble_scan_module, &ble_scan_filter_data); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); +} + +void test_ble_scan_filter_set(void) +{ + int err; + + struct ble_scan ble_scan_module = {0}; + + ble_scan_evt_handler_t scan_event = scan_event_handler_func; + + ble_gap_scan_params_t dummy_scan_params = {.extended = 1, + .report_incomplete_evts = 1, + .active = 1, + .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, + .scan_phys = BLE_GAP_PHY_1MBPS, + .interval = CONFIG_BLE_SCAN_SCAN_INTERVAL, + .window = CONFIG_BLE_SCAN_SCAN_WINDOW, + .timeout = CONFIG_BLE_SCAN_SCAN_DURATION, + .channel_mask = {1, 1, 1, 1, 1}}; + + ble_gap_conn_params_t dummy_conn_params = {.min_conn_interval = 1, + .max_conn_interval = 10, + .slave_latency = CONFIG_BLE_SCAN_SLAVE_LATENCY, + .conn_sup_timeout = + BLE_GAP_CP_CONN_SUP_TIMEOUT_MIN}; + + struct ble_scan_init scan_init = {.scan_param = &dummy_scan_params, + .conn_param = &dummy_conn_params}; + + ble_scan_init(&ble_scan_module, &scan_init, scan_event); + + ble_scan_filters_enable(&ble_scan_module, BLE_SCAN_ALL_FILTER, true); + + char *device_name = "generic_device"; + + err = ble_scan_filter_set(NULL, BLE_SCAN_ALL_FILTER, device_name); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, err); + + err = ble_scan_filter_set(&ble_scan_module, BLE_SCAN_ALL_FILTER, NULL); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, err); +} + +void test_is_whitelist_used(void) +{ + bool err; + + struct ble_scan ble_scan_module = {0}; + + err = is_whitelist_used(&ble_scan_module); + TEST_ASSERT_FALSE(err); + + ble_scan_module.scan_params.filter_policy = BLE_GAP_SCAN_FP_WHITELIST; + + err = is_whitelist_used(&ble_scan_module); + TEST_ASSERT_TRUE(err); +} + +void test_ble_scan_all_filter_remove(void) +{ + int err; + + struct ble_scan ble_scan_module = {0}; + + err = ble_scan_all_filter_remove(&ble_scan_module); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); +} + +void test_ble_scan_params_set(void) +{ + int err; + + struct ble_scan ble_scan_module = {0}; + + err = ble_scan_params_set(NULL, NULL); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, err); + + __cmock_sd_ble_gap_scan_stop_IgnoreAndReturn(NRF_SUCCESS); + err = ble_scan_params_set(&ble_scan_module, NULL); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); +} + +void test_ble_scan_copy_addr_to_sd_gap_addr(void) +{ + int err; + + err = ble_scan_copy_addr_to_sd_gap_addr(NULL, 0); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, err); + + uint8_t address[BLE_GAP_ADDR_LEN]; + ble_gap_addr_t gap_address = {0}; + err = ble_scan_copy_addr_to_sd_gap_addr(&gap_address, address); + TEST_ASSERT_EQUAL(NRF_SUCCESS, err); +} + +void test_ble_scan_on_ble_evt(void) +{ +} + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +extern int unity_main(void); + +int main(void) +{ + return unity_main(); +} diff --git a/tests/lib/bluetooth/ble_scan/testcase.yaml b/tests/lib/bluetooth/ble_scan/testcase.yaml new file mode 100644 index 0000000000..a5bc0ac0ca --- /dev/null +++ b/tests/lib/bluetooth/ble_scan/testcase.yaml @@ -0,0 +1,4 @@ +tests: + lib.ble_racp: + platform_allow: native_sim + tags: unittest