From patchwork Thu Nov 30 19:57:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Forslund X-Patchwork-Id: 13474975 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dbs3U1fx" Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B474FC4 for ; Thu, 30 Nov 2023 11:57:59 -0800 (PST) Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-50bc36725bbso1942008e87.2 for ; Thu, 30 Nov 2023 11:57:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701374278; x=1701979078; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=mRHn7roAGlLrELW/CgS9/3XIZ2n0xJCNo3eMlBEE+x4=; b=dbs3U1fxdYsNhG0cdk3cUWCveohpnSf/vbzQ9ebFOVJHeW/kf/+wE4/b16I+ONUz2V K1aC0lAEGWk93t5tEc5MV0QQRwiYoM6yKFgIbJRqd1/98GPZyjUdilp0lAWzxbJMScCz FRR/5AAVbn2fb/370yRfGHR8lqrUClsoV4ig4J6UZxG4VCJcEIIw2+3u3cxHv034Tlu3 oq5UGV4YySskJiUQaiFpSqP0oQI/CDqFZ7H1bqapCFVuQzcmvNM36OXvznhQkm3ZTZvh k/xLhJ6QtdBgoGVnTOGq77U5++BP45DdE96bMWb1+nPaAZNCGiZgzKmdz2wTMMZZQzgd VzpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701374278; x=1701979078; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mRHn7roAGlLrELW/CgS9/3XIZ2n0xJCNo3eMlBEE+x4=; b=Hz17CfwpwXZLFbMepNfPJSc5idmtJJP1q9Xr8JXc2XDf0YTs0G6CEzTEhntPiDICHX 2kGtJGGAq7r49h/Ts1MNyF6NvHSqIJEpr/Q++ld7nAilwi9sBqqMAxqnoTkMuvGLYmE5 YQvlZ6Gk7GSwftRZgSINj+ugbkeQ2a2XH4ZAj0td/muNpNYsjlcM+MG+th30weLFQS0r tf7b7H4yuo0cKTl1d6PKJ2QCnOZnHe7nn7MkKjPP7pCQ/SdfoQi/luMts7xckNojlkWV EDcbQ6vyA9lslIOOJelBWFsf54spXcd2o0B3G48D+whX8bJaMooCyzRuU5w1qjNNfCYS NLQQ== X-Gm-Message-State: AOJu0Yzx7hYX9EG1zZXIx7CTj0gz0nJRoEM5YAlwMc9mId9sRS7Ykkd7 PqLg004azeIZegcAKjRvIk0= X-Google-Smtp-Source: AGHT+IETEexIfME3Mp7y6FigpLUWktq9o/2300eU5xX0NYB1hwwZ2rvwTAfD3Uf6SQrXQrGIkuAhfQ== X-Received: by 2002:a2e:8716:0:b0:2c9:be1d:5e96 with SMTP id m22-20020a2e8716000000b002c9be1d5e96mr50847lji.5.1701374277499; Thu, 30 Nov 2023 11:57:57 -0800 (PST) Received: from localhost.localdomain (212-181-210-217-no600.tbcn.telia.com. [212.181.210.217]) by smtp.gmail.com with ESMTPSA id lb7-20020a170907784700b00a178b965899sm1026888ejc.100.2023.11.30.11.57.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Nov 2023 11:57:56 -0800 (PST) From: "sebastif@axis.com" To: sebastian.david.forslund@gmail.com, linux-bluetooth@vger.kernel.org Cc: Sebastian Forslund Subject: [PATCH BlueZ 1/1] Pattern match on service- and manufacturer data Date: Thu, 30 Nov 2023 20:57:50 +0100 Message-Id: <20231130195750.1732-1-sebastif@axis.com> X-Mailer: git-send-email 2.24.1.windows.2 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Sebastian Forslund 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); + 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,