2023-11-27 19:57:11

by Sebastian Forslund

[permalink] [raw]
Subject: [PATCH BlueZ 1/1] Pattern match on service- and manufacturer data

From: Sebastian Forslund <[email protected]>


When advertisement monitoring, manufacturer data and service data was

not being matched against. This made it so that advertisement monitoring

with or_patterns did not work that type of data.



We must start matching against the data in the manufacturer_data and

service_data queues. Run a different match-function depending on the

type of monitor that is being matched against.



Fixes: https://github.com/bluez/bluez/issues/652

---

src/shared/ad.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-

1 file changed, 77 insertions(+), 1 deletion(-)



diff --git a/src/shared/ad.c b/src/shared/ad.c

index 951c56c60..7cfe47291 100644

--- a/src/shared/ad.c

+++ b/src/shared/ad.c

@@ -1324,6 +1324,70 @@ struct bt_ad_pattern *bt_ad_pattern_new(uint8_t type, size_t offset, size_t len,

return pattern;

}



+static void pattern_manufacturer_data_match(void *data, void *user_data) {

+ struct bt_ad_manufacturer_data *manufacturer_data = data;

+ struct pattern_match_info *info = user_data;

+ struct bt_ad_pattern *pattern;

+ uint8_t all_data[BT_AD_MAX_DATA_LEN];

+

+ if (!manufacturer_data || !info)

+ return;

+

+ if (info->matched_pattern)

+ return;

+

+ pattern = info->current_pattern;

+

+ if (!pattern || pattern->type != BT_AD_MANUFACTURER_DATA)

+ return;

+

+ /* Take the manufacturer ID into account */

+ if (manufacturer_data->len + 2 < pattern->offset + pattern->len)

+ return;

+

+ memcpy(&all_data[0], &manufacturer_data->manufacturer_id, 2);

+ memcpy(&all_data[2], manufacturer_data->data, manufacturer_data->len);

+

+ if (!memcmp(all_data + pattern->offset, pattern->data,

+ pattern->len)) {

+ info->matched_pattern = pattern;

+ }

+}

+

+static void pattern_service_data_match(void *data, void *user_data) {

+ struct bt_ad_service_data *service_data = data;

+ struct pattern_match_info *info = user_data;

+ struct bt_ad_pattern *pattern;

+

+ if (!service_data || !info)

+ return;

+

+ if (info->matched_pattern)

+ return;

+

+ pattern = info->current_pattern;

+

+ if (!pattern)

+ return;

+

+ switch (pattern->type) {

+ case BT_AD_SERVICE_DATA16:

+ case BT_AD_SERVICE_DATA32:

+ case BT_AD_SERVICE_DATA128:

+ break;

+ default:

+ return;

+ }

+

+ if (service_data->len < pattern->offset + pattern->len)

+ return;

+

+ if (!memcmp(service_data->data + pattern->offset, pattern->data,

+ pattern->len)) {

+ info->matched_pattern = pattern;

+ }

+}

+

static void pattern_ad_data_match(void *data, void *user_data)

{

struct bt_ad_data *ad_data = data;

@@ -1363,7 +1427,19 @@ static void pattern_match(void *data, void *user_data)



info->current_pattern = pattern;



- bt_ad_foreach_data(info->ad, pattern_ad_data_match, info);

+ switch (pattern->type) {

+ case BT_AD_MANUFACTURER_DATA:

+ queue_foreach(info->ad->manufacturer_data, pattern_manufacturer_data_match, info);

+ break;

+ case BT_AD_SERVICE_DATA16:

+ case BT_AD_SERVICE_DATA32:

+ case BT_AD_SERVICE_DATA128:

+ queue_foreach(info->ad->service_data, pattern_service_data_match, info);

+ break;

+ default:

+ bt_ad_foreach_data(info->ad, pattern_ad_data_match, info);

+ break;

+ }

}



struct bt_ad_pattern *bt_ad_pattern_match(struct bt_ad *ad,

--

2.39.2





2023-11-27 21:21:15

by bluez.test.bot

[permalink] [raw]
Subject: RE: [BlueZ,1/1] Pattern match on service- and manufacturer data

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=804640

---Test result---

Test Summary:
CheckPatch FAIL 0.67 seconds
GitLint PASS 0.31 seconds
BuildEll PASS 24.12 seconds
BluezMake PASS 551.41 seconds
MakeCheck PASS 10.52 seconds
MakeDistcheck PASS 150.82 seconds
CheckValgrind PASS 210.28 seconds
CheckSmatch PASS 315.47 seconds
bluezmakeextell PASS 96.19 seconds
IncrementalBuild PASS 510.76 seconds
ScanBuild PASS 906.21 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[BlueZ,1/1] Pattern match on service- and manufacturer data
ERROR:OPEN_BRACE: open brace '{' following function definitions go on the next line
#93: FILE: src/shared/ad.c:1327:
+static void pattern_manufacturer_data_match(void *data, void *user_data) {

ERROR:OPEN_BRACE: open brace '{' following function definitions go on the next line
#123: FILE: src/shared/ad.c:1357:
+static void pattern_service_data_match(void *data, void *user_data) {

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#124: FILE: src/shared/ad.c:1358:
+ struct bt_ad_service_data *service_data = data;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#125: FILE: src/shared/ad.c:1359:
+ struct pattern_match_info *info = user_data;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#126: FILE: src/shared/ad.c:1360:
+ struct bt_ad_pattern *pattern;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#128: FILE: src/shared/ad.c:1362:
+ if (!service_data || !info)$

WARNING:SUSPECT_CODE_INDENT: suspect code indent for conditional statements (2, 4)
#128: FILE: src/shared/ad.c:1362:
+ if (!service_data || !info)
+ return;

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#129: FILE: src/shared/ad.c:1363:
+ return;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#131: FILE: src/shared/ad.c:1365:
+ if (info->matched_pattern)$

WARNING:SUSPECT_CODE_INDENT: suspect code indent for conditional statements (2, 4)
#131: FILE: src/shared/ad.c:1365:
+ if (info->matched_pattern)
+ return;

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#132: FILE: src/shared/ad.c:1366:
+ return;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#134: FILE: src/shared/ad.c:1368:
+ pattern = info->current_pattern;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#136: FILE: src/shared/ad.c:1370:
+ if (!pattern)$

WARNING:SUSPECT_CODE_INDENT: suspect code indent for conditional statements (2, 4)
#136: FILE: src/shared/ad.c:1370:
+ if (!pattern)
+ return;

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#137: FILE: src/shared/ad.c:1371:
+ return;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#139: FILE: src/shared/ad.c:1373:
+ switch (pattern->type) {$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#140: FILE: src/shared/ad.c:1374:
+ case BT_AD_SERVICE_DATA16:$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#141: FILE: src/shared/ad.c:1375:
+ case BT_AD_SERVICE_DATA32:$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#142: FILE: src/shared/ad.c:1376:
+ case BT_AD_SERVICE_DATA128:$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#143: FILE: src/shared/ad.c:1377:
+ break;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#145: FILE: src/shared/ad.c:1379:
+ return;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#146: FILE: src/shared/ad.c:1380:
+ }$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#148: FILE: src/shared/ad.c:1382:
+ if (service_data->len < pattern->offset + pattern->len)$

WARNING:SUSPECT_CODE_INDENT: suspect code indent for conditional statements (2, 4)
#148: FILE: src/shared/ad.c:1382:
+ if (service_data->len < pattern->offset + pattern->len)
+ return;

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#149: FILE: src/shared/ad.c:1383:
+ return;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#151: FILE: src/shared/ad.c:1385:
+ if (!memcmp(service_data->data + pattern->offset, pattern->data,$

WARNING:SUSPECT_CODE_INDENT: suspect code indent for conditional statements (2, 4)
#151: FILE: src/shared/ad.c:1385:
+ if (!memcmp(service_data->data + pattern->offset, pattern->data,
[...]
+ info->matched_pattern = pattern;

ERROR:CODE_INDENT: code indent should use tabs where possible
#152: FILE: src/shared/ad.c:1386:
+ pattern->len)) {$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#152: FILE: src/shared/ad.c:1386:
+ pattern->len)) {$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#153: FILE: src/shared/ad.c:1387:
+ info->matched_pattern = pattern;$

WARNING:LEADING_SPACE: please, no spaces at the start of a line
#154: FILE: src/shared/ad.c:1388:
+ }$

WARNING:LONG_LINE: line length of 98 exceeds 80 columns
#167: FILE: src/shared/ad.c:1432:
+ queue_foreach(info->ad->manufacturer_data, pattern_manufacturer_data_match, info);

WARNING:LONG_LINE: line length of 88 exceeds 80 columns
#172: FILE: src/shared/ad.c:1437:
+ queue_foreach(info->ad->service_data, pattern_service_data_match, info);

/github/workspace/src/src/13470254.patch total: 3 errors, 30 warnings, 90 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

NOTE: Whitespace errors detected.
You may wish to use scripts/cleanpatch or scripts/cleanfile

/github/workspace/src/src/13470254.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.




---
Regards,
Linux Bluetooth

2023-12-04 22:12:34

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH BlueZ 1/1] Pattern match on service- and manufacturer data

Hi Sebastian,

On Thu, Nov 30, 2023 at 2:58 PM [email protected]
<[email protected]> wrote:
>
> From: Sebastian Forslund <[email protected]>
>
> When advertisement monitoring, manufacturer data and service data was
> not being matched against. This made it so that advertisement monitoring
> with or_patterns did not work that type of data.
>
> We must start matching against the data in the manufacturer_data and
> service_data queues. Run a different match-function depending on the
> type of monitor that is being matched against.
>
> Fixes: https://github.com/bluez/bluez/issues/652
> ---
> src/shared/ad.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 80 insertions(+), 1 deletion(-)
> mode change 100644 => 100755 src/shared/ad.c
>
> diff --git a/src/shared/ad.c b/src/shared/ad.c
> old mode 100644
> new mode 100755
> index 951c56c60..a95079dc1
> --- a/src/shared/ad.c
> +++ b/src/shared/ad.c
> @@ -1324,6 +1324,72 @@ struct bt_ad_pattern *bt_ad_pattern_new(uint8_t type, size_t offset, size_t len,
> return pattern;
> }
>
> +static void pattern_manufacturer_data_match(void *data, void *user_data)
> +{
> + struct bt_ad_manufacturer_data *manufacturer_data = data;
> + struct pattern_match_info *info = user_data;
> + struct bt_ad_pattern *pattern;
> + uint8_t all_data[BT_AD_MAX_DATA_LEN];
> +
> + if (!manufacturer_data || !info)
> + return;
> +
> + if (info->matched_pattern)
> + return;
> +
> + pattern = info->current_pattern;
> +
> + if (!pattern || pattern->type != BT_AD_MANUFACTURER_DATA)
> + return;
> +
> + /* Take the manufacturer ID into account */
> + if (manufacturer_data->len + 2 < pattern->offset + pattern->len)
> + return;
> +
> + memcpy(&all_data[0], &manufacturer_data->manufacturer_id, 2);
> + memcpy(&all_data[2], manufacturer_data->data, manufacturer_data->len);
> +
> + if (!memcmp(all_data + pattern->offset, pattern->data,
> + pattern->len)) {
> + info->matched_pattern = pattern;
> + }
> +}
> +
> +static void pattern_service_data_match(void *data, void *user_data)
> +{
> + struct bt_ad_service_data *service_data = data;
> + struct pattern_match_info *info = user_data;
> + struct bt_ad_pattern *pattern;
> +
> + if (!service_data || !info)
> + return;
> +
> + if (info->matched_pattern)
> + return;
> +
> + pattern = info->current_pattern;
> +
> + if (!pattern)
> + return;
> +
> + switch (pattern->type) {
> + case BT_AD_SERVICE_DATA16:
> + case BT_AD_SERVICE_DATA32:
> + case BT_AD_SERVICE_DATA128:
> + break;
> + default:
> + return;
> + }
> +
> + if (service_data->len < pattern->offset + pattern->len)
> + return;
> +
> + if (!memcmp(service_data->data + pattern->offset, pattern->data,
> + pattern->len)) {
> + info->matched_pattern = pattern;
> + }
> +}
> +
> static void pattern_ad_data_match(void *data, void *user_data)
> {
> struct bt_ad_data *ad_data = data;
> @@ -1363,7 +1429,20 @@ static void pattern_match(void *data, void *user_data)
>
> info->current_pattern = pattern;
>
> - bt_ad_foreach_data(info->ad, pattern_ad_data_match, info);
> + switch (pattern->type) {
> + case BT_AD_MANUFACTURER_DATA:
> + queue_foreach(info->ad->manufacturer_data, pattern_manufacturer_data_match,
> + info);

I'd use queue_find instead so you can stop the lookup earlier if there
is a match, also for the match function you can use something like
match_manufacturer.

> + break;
> + case BT_AD_SERVICE_DATA16:
> + case BT_AD_SERVICE_DATA32:
> + case BT_AD_SERVICE_DATA128:
> + queue_foreach(info->ad->service_data, pattern_service_data_match, info);

Ditto, Id also suggest to use something like match_service.

> + break;
> + default:
> + bt_ad_foreach_data(info->ad, pattern_ad_data_match, info);
> + break;
> + }
> }
>
> struct bt_ad_pattern *bt_ad_pattern_match(struct bt_ad *ad,
> --
> 2.39.2
>
>


--
Luiz Augusto von Dentz