Skip to content

bluetooth: ble_scan: add manufacturer data filter support#783

Open
PizzaAllTheWay wants to merge 3 commits intonrfconnect:mainfrom
PizzaAllTheWay:ble-scan-manufacturer-filter
Open

bluetooth: ble_scan: add manufacturer data filter support#783
PizzaAllTheWay wants to merge 3 commits intonrfconnect:mainfrom
PizzaAllTheWay:ble-scan-manufacturer-filter

Conversation

@PizzaAllTheWay
Copy link
Copy Markdown
Contributor

Port the manufacturer data scan filter from NCS to Bare Metal.

ble_adv_data: new ble_adv_data_manufacturer_data_find() helper.

ble_scan: new BLE_SCAN_MANUFACTURER_DATA_FILTER filter type with CONFIG_BLE_SCAN_MANUFACTURER_DATA_COUNT and CONFIG_BLE_SCAN_MANUFACTURER_DATA_MAX_LEN Kconfig options. Wired through filter add/enable/disable/remove and the adv report handler.

Unit tests added for the new find helper, filter-add paths and event handler, plus regression guards in existing tests.

Changelog entries added under lib_ble_adv and lib_ble_scan.

Clarified the comment around the match_all ADV/SCAN_RSP cache logic.

Feature verified end-to-end using locally modified ble_hrs and ble_hrs_central samples (peripheral advertising Nordic company ID 0x0059 and central configured with a manufacturer data filter). Sample changes are not included in this PR.

@github-actions github-actions Bot added the doc-required PR must not be merged without tech writer approval. label Apr 28, 2026
@PizzaAllTheWay PizzaAllTheWay force-pushed the ble-scan-manufacturer-filter branch from 71e578f to db6182f Compare April 28, 2026 15:32
@github-actions
Copy link
Copy Markdown

You can find the documentation preview for this PR here.

@PizzaAllTheWay PizzaAllTheWay force-pushed the ble-scan-manufacturer-filter branch from db6182f to 991a6e7 Compare April 28, 2026 15:39
@PizzaAllTheWay PizzaAllTheWay marked this pull request as ready for review April 28, 2026 15:47
@PizzaAllTheWay PizzaAllTheWay requested review from a team as code owners April 28, 2026 15:47
@PizzaAllTheWay PizzaAllTheWay self-assigned this Apr 28, 2026
Comment thread include/bm/bluetooth/ble_adv_data.h Outdated
* @p target_data_len bytes match @p target_data.
* @retval false No match, or the advertising data contains no manufacturer-specific data.
*/
bool ble_adv_data_manufacturer_data_find(const uint8_t *data, uint16_t data_len,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's align with the other _find functions. Use buf, len for encoded advertising data and length. Also align doxygen formatting (one space after variable etc.).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return false;
}

if (target_data_len > parsed_len) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't necessarily know the exact length, rather we should be able to tell the size of the buffer we have and return the amount of bytes copied.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate? I don't quite follow what you're asking for here.

This function is a bool returning find, it doesn't copy anything, it just reports whether a match exists in the advertising data. Is it something about the existing target_data/target_data_len parameters that I'm missing?

Comment thread lib/bluetooth/ble_adv/ble_adv_data.c Outdated
return false;
}

return memcmp(&data[data_offset], target_data, target_data_len) == 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do the memcpy first then return true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines +218 to +219


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not belong in 7c80e5d

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

TEST_ASSERT_EQUAL(NRF_SUCCESS, nrf_err);
}

static void encode_manufacturer_data_test_data(const uint8_t *payload, uint8_t payload_len)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encode_manufacturer_test_data is enough

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread lib/bluetooth/ble_scan/Kconfig Outdated
Maximum size of the short name to search for in the advertisement report.

config BLE_SCAN_MANUFACTURER_DATA_MAX_LEN
int "Maximum size for the manufacturer data to search in the advertisement report"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align with Kconfigs above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread lib/bluetooth/ble_scan/Kconfig Outdated
Maximum number of filters for UUIDs.

config BLE_SCAN_MANUFACTURER_DATA_COUNT
int "Number of manufacturer data filters"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align with other Kconfigs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


test_ble_scan_init();

nrf_err = ble_scan_filters_enable(&ble_scan, 0x20, true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put in the same commit as the changes to ble_scan!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


#define UUID 0x2312

/* Nordic Semiconductor company identifier (Bluetooth SIG assigned). */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Squash commit adding tests with ble_scan code changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

TEST_ASSERT_EQUAL(0, scan_event.filter_match.filter_match.short_name_filter_match);
TEST_ASSERT_EQUAL(0, scan_event.filter_match.filter_match.appearance_filter_match);
TEST_ASSERT_EQUAL(1, scan_event.filter_match.filter_match.uuid_filter_match);
TEST_ASSERT_EQUAL(0, scan_event.filter_match.filter_match.manufacturer_data_filter_match);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test case for manufacturer_data_filter_match equal to 1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_ble_scan_on_ble_evt_adv_report_device_manufacturer_data() already covers that case, it asserts scan_event.filter_match.filter_match.manufacturer_data_filter_match == 1 on a successful match.

Did you mean an additional test, like a combined filter case (similar to test_ble_scan_on_ble_evt_adv_report_device_name_and_appearance()) where manufacturer data matches together with another filter? Could you elaborate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I assume you meant an extra case with multiple manufacturer data filters, so I added test_ble_scan_on_ble_evt_adv_report_device_multiple_manufacturer_data() (two manuf filters registered). Also bumped CONFIG_BLE_SCAN_MANUFACTURER_DATA_COUNT to 2 and updated test_ble_scan_filter_add_manufacturer_data_error_no_mem() accordingly. Let me know if that's what you had in mind.

@PizzaAllTheWay PizzaAllTheWay force-pushed the ble-scan-manufacturer-filter branch from 991a6e7 to 4dcd5d0 Compare May 4, 2026 16:47
Comment on lines +79 to +81
* Added:

* The :c:func:`ble_adv_data_manufacturer_data_find` function to locate manufacturer-specific data in an advertising payload and prefix-match it against a target value.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Added:
* The :c:func:`ble_adv_data_manufacturer_data_find` function to locate manufacturer-specific data in an advertising payload and prefix-match it against a target value.
* Added the :c:func:`ble_adv_data_manufacturer_data_find` function to locate manufacturer-specific data in an advertising payload and prefix-match it against a target value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

* Added:

* The :c:struct:`ble_scan_filter_data` structure as input to the :c:func:`ble_scan_filter_add` function.
* Support for filtering by manufacturer-specific data via the :c:macro:`BLE_SCAN_MANUFACTURER_DATA_FILTER` filter type.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Support for filtering by manufacturer-specific data via the :c:macro:`BLE_SCAN_MANUFACTURER_DATA_FILTER` filter type.
* Support for filtering by manufacturer-specific data using the :c:macro:`BLE_SCAN_MANUFACTURER_DATA_FILTER` filter type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread include/bm/bluetooth/ble_adv_data.h Outdated
* vendor payload.
* @param[in] target_data_len Length of the target manufacturer data.
*
* @retval true If manufacturer-specific data whose first @p target_data_len bytes match
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @retval true If manufacturer-specific data whose first @p target_data_len bytes match
* @retval true If manufacturer-specific data in which the first @p target_data_len bytes matches

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Add ble_adv_data_manufacturer_data_find() to locate
manufacturer-specific data in an advertising payload and prefix-match
it against a target value. Follows the pattern of existing _find
helpers for name, uuid and appearance.

Modified unit tests to align with changes.
Added changelog.

Signed-off-by: Martynas Smilingis <martynas.smilingis@nordicsemi.no>
Expand the comment around the match_all cache logic to explain the
on-air ADV → SCAN_REQ → SCAN_RSP exchange and why the paired SCAN_RSP
is guaranteed to arrive before an ADV from another device. Also clarify
the purpose of the buffer swap. No functional changes.

Signed-off-by: Martynas Smilingis <martynas.smilingis@nordicsemi.no>
@PizzaAllTheWay PizzaAllTheWay force-pushed the ble-scan-manufacturer-filter branch from 4dcd5d0 to e3f6b95 Compare May 5, 2026 07:14
Port the manufacturer data scan filter from NCS to Bare Metal. Adds
BLE_SCAN_MANUFACTURER_DATA_FILTER type, manufacturer_data_filter_match
flag in ble_scan_filter_match, and plumbing through
ble_scan_filter_add, ble_scan_filters_enable, ble_scan_filters_disable,
ble_scan_all_filter_remove and the adv report event handler.

New Kconfig options:
  - CONFIG_BLE_SCAN_MANUFACTURER_DATA_COUNT
  - CONFIG_BLE_SCAN_MANUFACTURER_DATA_MAX_LEN

Modified unit tests to align with changes.
Added changelog.

Signed-off-by: Martynas Smilingis <martynas.smilingis@nordicsemi.no>
@PizzaAllTheWay PizzaAllTheWay force-pushed the ble-scan-manufacturer-filter branch from e3f6b95 to 356109f Compare May 5, 2026 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-required PR must not be merged without tech writer approval.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants