All of lore.kernel.org
 help / color / mirror / Atom feed
* [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning
@ 2020-09-17 21:25 Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 2/8] adv_monitor: Implement unit tests for RSSI Filter Miao-chen Chou
                   ` (7 more replies)
  0 siblings, 8 replies; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17 21:25 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Manish Mandlik, Alain Michaud, chromeos-bluetooth-upstreaming,
	Luiz Augusto von Dentz, Marcel Holtmann, Howard Chung,
	Manish Mandlik, Abhishek Pandit-Subedi, Miao-chen Chou

From: Manish Mandlik <mmandlik@google.com>

This patch implements the RSSI Filter logic for background scanning.

This was unit-tested by running tests in unit/test-adv-monitor.c

Reviewed-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
Reviewed-by: Alain Michaud <alainm@chromium.org>
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
Reviewed-by: Howard Chung <howardchung@google.com>
---

(no changes since v3)

Changes in v3:
- Fix commit message

 doc/advertisement-monitor-api.txt |   5 +
 src/adapter.c                     |   1 +
 src/adv_monitor.c                 | 286 +++++++++++++++++++++++++++++-
 src/adv_monitor.h                 |   4 +
 4 files changed, 292 insertions(+), 4 deletions(-)

diff --git a/doc/advertisement-monitor-api.txt b/doc/advertisement-monitor-api.txt
index e09b6fd25..92c8ffc38 100644
--- a/doc/advertisement-monitor-api.txt
+++ b/doc/advertisement-monitor-api.txt
@@ -70,6 +70,11 @@ Properties	string Type [read-only]
 			dBm indicates unset. The valid range of a timer is 1 to
 			300 seconds while 0 indicates unset.
 
+			If the peer device advertising interval is greater than the
+			HighRSSIThresholdTimer, the device will never be found. Similarly,
+			if it is greater than LowRSSIThresholdTimer, the device will be
+			considered as lost. Consider configuring these values accordingly.
+
 		array{(uint8, uint8, array{byte})} Patterns [read-only, optional]
 
 			If Type is set to 0x01, this must exist and has at least
diff --git a/src/adapter.c b/src/adapter.c
index b2bd8b3f1..415d6e06b 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1227,6 +1227,7 @@ void btd_adapter_remove_device(struct btd_adapter *adapter,
 	adapter->connect_list = g_slist_remove(adapter->connect_list, dev);
 
 	adapter->devices = g_slist_remove(adapter->devices, dev);
+	btd_adv_monitor_device_remove(adapter->adv_monitor_manager, dev);
 
 	adapter->discovery_found = g_slist_remove(adapter->discovery_found,
 									dev);
diff --git a/src/adv_monitor.c b/src/adv_monitor.c
index 737da1c90..7baa5317f 100644
--- a/src/adv_monitor.c
+++ b/src/adv_monitor.c
@@ -35,6 +35,7 @@
 
 #include "adapter.h"
 #include "dbus-common.h"
+#include "device.h"
 #include "log.h"
 #include "src/error.h"
 #include "src/shared/ad.h"
@@ -44,6 +45,8 @@
 
 #include "adv_monitor.h"
 
+static void monitor_device_free(void *data);
+
 #define ADV_MONITOR_INTERFACE		"org.bluez.AdvertisementMonitor1"
 #define ADV_MONITOR_MGR_INTERFACE	"org.bluez.AdvertisementMonitorManager1"
 
@@ -104,15 +107,36 @@ struct adv_monitor {
 
 	enum monitor_state state;	/* MONITOR_STATE_* */
 
-	int8_t high_rssi;		/* high RSSI threshold */
-	uint16_t high_rssi_timeout;	/* high RSSI threshold timeout */
-	int8_t low_rssi;		/* low RSSI threshold */
-	uint16_t low_rssi_timeout;	/* low RSSI threshold timeout */
+	int8_t high_rssi;		/* High RSSI threshold */
+	uint16_t high_rssi_timeout;	/* High RSSI threshold timeout */
+	int8_t low_rssi;		/* Low RSSI threshold */
+	uint16_t low_rssi_timeout;	/* Low RSSI threshold timeout */
+	struct queue *devices;		/* List of adv_monitor_device objects */
 
 	enum monitor_type type;		/* MONITOR_TYPE_* */
 	struct queue *patterns;
 };
 
+/* Some data like last_seen, timer/timeout values need to be maintained
+ * per device. struct adv_monitor_device maintains such data.
+ */
+struct adv_monitor_device {
+	struct adv_monitor *monitor;
+	struct btd_device *device;
+
+	time_t high_rssi_first_seen;	/* Start time when RSSI climbs above
+					 * the high RSSI threshold
+					 */
+	time_t low_rssi_first_seen;	/* Start time when RSSI drops below
+					 * the low RSSI threshold
+					 */
+	time_t last_seen;		/* Time when last Adv was received */
+	bool device_found;		/* State of the device - lost/found */
+	guint device_lost_timer;	/* Timer to track if the device goes
+					 * offline/out-of-range
+					 */
+};
+
 struct app_match_data {
 	const char *owner;
 	const char *path;
@@ -159,6 +183,9 @@ static void monitor_free(void *data)
 	g_dbus_proxy_unref(monitor->proxy);
 	g_free(monitor->path);
 
+	queue_destroy(monitor->devices, monitor_device_free);
+	monitor->devices = NULL;
+
 	queue_destroy(monitor->patterns, pattern_free);
 
 	free(monitor);
@@ -257,6 +284,7 @@ static struct adv_monitor *monitor_new(struct adv_monitor_app *app,
 	monitor->high_rssi_timeout = ADV_MONITOR_UNSET_TIMER;
 	monitor->low_rssi = ADV_MONITOR_UNSET_RSSI;
 	monitor->low_rssi_timeout = ADV_MONITOR_UNSET_TIMER;
+	monitor->devices = queue_new();
 
 	monitor->type = MONITOR_TYPE_NONE;
 	monitor->patterns = NULL;
@@ -932,3 +960,253 @@ void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager)
 
 	manager_destroy(manager);
 }
+
+/* Matches a device based on btd_device object */
+static bool monitor_device_match(const void *a, const void *b)
+{
+	const struct adv_monitor_device *dev = a;
+	const struct btd_device *device = b;
+
+	if (!dev)
+		return false;
+
+	if (dev->device != device)
+		return false;
+
+	return true;
+}
+
+/* Frees a monitor device object */
+static void monitor_device_free(void *data)
+{
+	struct adv_monitor_device *dev = data;
+
+	if (!dev)
+		return;
+
+	if (dev->device_lost_timer) {
+		g_source_remove(dev->device_lost_timer);
+		dev->device_lost_timer = 0;
+	}
+
+	dev->monitor = NULL;
+	dev->device = NULL;
+
+	g_free(dev);
+}
+
+/* Removes a device from monitor->devices list */
+static void remove_device_from_monitor(void *data, void *user_data)
+{
+	struct adv_monitor *monitor = data;
+	struct btd_device *device = user_data;
+	struct adv_monitor_device *dev = NULL;
+
+	if (!monitor)
+		return;
+
+	dev = queue_remove_if(monitor->devices, monitor_device_match, device);
+	if (dev) {
+		DBG("Device removed from the Adv Monitor at path %s",
+		    monitor->path);
+		monitor_device_free(dev);
+	}
+}
+
+/* Removes a device from every monitor in an app */
+static void remove_device_from_app(void *data, void *user_data)
+{
+	struct adv_monitor_app *app = data;
+	struct btd_device *device = user_data;
+
+	if (!app)
+		return;
+
+	queue_foreach(app->monitors, remove_device_from_monitor, device);
+}
+
+/* Removes a device from every monitor in all apps */
+void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager,
+				   struct btd_device *device)
+{
+	if (!manager || !device)
+		return;
+
+	queue_foreach(manager->apps, remove_device_from_app, device);
+}
+
+/* Creates a device object to track the per-device information */
+static struct adv_monitor_device *monitor_device_create(
+			struct adv_monitor *monitor,
+			struct btd_device *device)
+{
+	struct adv_monitor_device *dev = NULL;
+
+	dev = g_try_malloc0(sizeof(struct adv_monitor_device));
+	if (!dev)
+		return NULL;
+
+	dev->monitor = monitor;
+	dev->device = device;
+
+	queue_push_tail(monitor->devices, dev);
+
+	return dev;
+}
+
+/* Includes found/lost device's object path into the dbus message */
+static void report_device_state_setup(DBusMessageIter *iter, void *user_data)
+{
+	const char *path = device_get_path(user_data);
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
+}
+
+/* Handles a situation where the device goes offline/out-of-range */
+static gboolean handle_device_lost_timeout(gpointer user_data)
+{
+	struct adv_monitor_device *dev = user_data;
+	struct adv_monitor *monitor = dev->monitor;
+	time_t curr_time = time(NULL);
+
+	DBG("Device Lost timeout triggered for device %p "
+	    "for the Adv Monitor at path %s", dev->device, monitor->path);
+
+	dev->device_lost_timer = 0;
+
+	if (dev->device_found && dev->last_seen) {
+		/* We were tracking for the Low RSSI filter. Check if there is
+		 * any Adv received after the timeout function is invoked.
+		 * If not, report the Device Lost event.
+		 */
+		if (difftime(curr_time, dev->last_seen) >=
+		    monitor->low_rssi_timeout) {
+			dev->device_found = false;
+
+			DBG("Calling DeviceLost() on Adv Monitor of owner %s "
+			    "at path %s", monitor->app->owner, monitor->path);
+
+			g_dbus_proxy_method_call(monitor->proxy, "DeviceLost",
+						 report_device_state_setup,
+						 NULL, dev->device, NULL);
+		}
+	}
+
+	return FALSE;
+}
+
+/* Filters an Adv based on its RSSI value */
+static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
+				    struct btd_device *device, int8_t rssi)
+{
+	struct adv_monitor_device *dev = NULL;
+	time_t curr_time = time(NULL);
+	uint16_t adapter_id = monitor->app->manager->adapter_id;
+
+	/* If the RSSI thresholds and timeouts are not specified, report the
+	 * DeviceFound() event without tracking for the RSSI as the Adv has
+	 * already matched the pattern filter.
+	 */
+	if (monitor->high_rssi == ADV_MONITOR_UNSET_RSSI &&
+		monitor->low_rssi == ADV_MONITOR_UNSET_RSSI &&
+		monitor->high_rssi_timeout == ADV_MONITOR_UNSET_TIMER &&
+		monitor->low_rssi_timeout == ADV_MONITOR_UNSET_TIMER) {
+		DBG("Calling DeviceFound() on Adv Monitor of owner %s "
+		    "at path %s", monitor->app->owner, monitor->path);
+
+		g_dbus_proxy_method_call(monitor->proxy, "DeviceFound",
+					 report_device_state_setup, NULL,
+					 device, NULL);
+
+		return;
+	}
+
+	dev = queue_find(monitor->devices, monitor_device_match, device);
+	if (!dev)
+		dev = monitor_device_create(monitor, device);
+	if (!dev) {
+		btd_error(adapter_id, "Failed to create Adv Monitor "
+				      "device object.");
+		return;
+	}
+
+	if (dev->device_lost_timer) {
+		g_source_remove(dev->device_lost_timer);
+		dev->device_lost_timer = 0;
+	}
+
+	/* Reset the timings of found/lost if a device has been offline for
+	 * longer than the high/low timeouts.
+	 */
+	if (dev->last_seen) {
+		if (difftime(curr_time, dev->last_seen) >
+		    monitor->high_rssi_timeout) {
+			dev->high_rssi_first_seen = 0;
+		}
+
+		if (difftime(curr_time, dev->last_seen) >
+		    monitor->low_rssi_timeout) {
+			dev->low_rssi_first_seen = 0;
+		}
+	}
+	dev->last_seen = curr_time;
+
+	/* Check for the found devices (if the device is not already found) */
+	if (!dev->device_found && rssi > monitor->high_rssi) {
+		if (dev->high_rssi_first_seen) {
+			if (difftime(curr_time, dev->high_rssi_first_seen) >=
+			    monitor->high_rssi_timeout) {
+				dev->device_found = true;
+
+				DBG("Calling DeviceFound() on Adv Monitor "
+				    "of owner %s at path %s",
+				    monitor->app->owner, monitor->path);
+
+				g_dbus_proxy_method_call(
+					monitor->proxy, "DeviceFound",
+					report_device_state_setup, NULL,
+					dev->device, NULL);
+			}
+		} else {
+			dev->high_rssi_first_seen = curr_time;
+		}
+	} else {
+		dev->high_rssi_first_seen = 0;
+	}
+
+	/* Check for the lost devices (only if the device is already found, as
+	 * it doesn't make any sense to report the Device Lost event if the
+	 * device is not found yet)
+	 */
+	if (dev->device_found && rssi < monitor->low_rssi) {
+		if (dev->low_rssi_first_seen) {
+			if (difftime(curr_time, dev->low_rssi_first_seen) >=
+			    monitor->low_rssi_timeout) {
+				dev->device_found = false;
+
+				DBG("Calling DeviceLost() on Adv Monitor "
+				    "of owner %s at path %s",
+				    monitor->app->owner, monitor->path);
+
+				g_dbus_proxy_method_call(
+					monitor->proxy, "DeviceLost",
+					report_device_state_setup, NULL,
+					dev->device, NULL);
+			}
+		} else {
+			dev->low_rssi_first_seen = curr_time;
+		}
+	} else {
+		dev->low_rssi_first_seen = 0;
+	}
+
+	/* Setup a timer to track if the device goes offline/out-of-range, only
+	 * if we are tracking for the Low RSSI Threshold. If we are tracking
+	 * the High RSSI Threshold, nothing needs to be done.
+	 */
+	if (dev->device_found) {
+		dev->device_lost_timer =
+			g_timeout_add_seconds(monitor->low_rssi_timeout,
+					      handle_device_lost_timeout, dev);
+	}
+}
diff --git a/src/adv_monitor.h b/src/adv_monitor.h
index 69ea348f8..14508e7d1 100644
--- a/src/adv_monitor.h
+++ b/src/adv_monitor.h
@@ -21,6 +21,7 @@
 #define __ADV_MONITOR_H
 
 struct mgmt;
+struct btd_device;
 struct btd_adapter;
 struct btd_adv_monitor_manager;
 
@@ -29,4 +30,7 @@ struct btd_adv_monitor_manager *btd_adv_monitor_manager_create(
 						struct mgmt *mgmt);
 void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager);
 
+void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager,
+				   struct btd_device *device);
+
 #endif /* __ADV_MONITOR_H */
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [BlueZ PATCH v4 2/8] adv_monitor: Implement unit tests for RSSI Filter
  2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
@ 2020-09-17 21:25 ` Miao-chen Chou
  2020-09-17 21:49   ` [BlueZ,v4,2/8] " bluez.test.bot
  2020-09-17 21:25 ` [BlueZ PATCH v4 3/8] adv_monitor: Implement Adv matching based on stored monitors Miao-chen Chou
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17 21:25 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Manish Mandlik, Alain Michaud, chromeos-bluetooth-upstreaming,
	Luiz Augusto von Dentz, Marcel Holtmann, Howard Chung,
	Manish Mandlik, Miao-chen Chou

From: Manish Mandlik <mmandlik@google.com>

This patch implements unit tests for the background scanning RSSI
Filtering logic.

Verified all tests PASS by running tests in unit/test-adv-monitor.c

Reviewed-by: Alain Michaud <alainm@chromium.org>
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
---

(no changes since v3)

Changes in v3:
- Fix commit message

Changes in v2:
- Cast test data to void *

 Makefile.am             |   9 +
 doc/test-coverage.txt   |   3 +-
 src/adv_monitor.c       |  79 ++++++++
 src/adv_monitor.h       |  10 +
 unit/test-adv-monitor.c | 391 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 491 insertions(+), 1 deletion(-)
 create mode 100644 unit/test-adv-monitor.c

diff --git a/Makefile.am b/Makefile.am
index 22b4fa30c..6918f02b0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -527,6 +527,15 @@ unit_test_gattrib_LDADD = lib/libbluetooth-internal.la \
 			src/libshared-glib.la \
 			$(GLIB_LIBS) $(DBUS_LIBS) -ldl -lrt
 
+unit_tests += unit/test-adv-monitor
+
+unit_test_adv_monitor_SOURCES = unit/test-adv-monitor.c \
+				src/adv_monitor.h src/adv_monitor.c \
+				src/device.h src/device.c \
+				src/log.h src/log.c
+unit_test_adv_monitor_LDADD = gdbus/libgdbus-internal.la \
+				src/libshared-glib.la $(GLIB_LIBS) $(DBUS_LIBS)
+
 if MIDI
 unit_tests += unit/test-midi
 unit_test_midi_CPPFLAGS = $(AM_CPPFLAGS) $(ALSA_CFLAGS) -DMIDI_TEST
diff --git a/doc/test-coverage.txt b/doc/test-coverage.txt
index 741492a3e..5296983e6 100644
--- a/doc/test-coverage.txt
+++ b/doc/test-coverage.txt
@@ -30,8 +30,9 @@ test-gobex-transfer	  36	OBEX transfer handling
 test-gdbus-client	  13	D-Bus client handling
 test-gatt		 180	GATT qualification test cases
 test-hog		   6	HID Over GATT qualification test cases
+test-adv-monitor	   5	Advertisement Monitor test cases
 			-----
-			 761
+			 766
 
 
 Automated end-to-end testing
diff --git a/src/adv_monitor.c b/src/adv_monitor.c
index 7baa5317f..046f5953f 100644
--- a/src/adv_monitor.c
+++ b/src/adv_monitor.c
@@ -1210,3 +1210,82 @@ static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
 					      handle_device_lost_timeout, dev);
 	}
 }
+
+/* Creates the dummy adv_monitor object for unit tests */
+void *btd_adv_monitor_rssi_test_setup(int8_t high_rssi, uint16_t high_timeout,
+				      int8_t low_rssi, uint16_t low_timeout)
+{
+	struct adv_monitor *test_monitor = NULL;
+
+	test_monitor = g_new0(struct adv_monitor, 1);
+	if (!test_monitor)
+		return NULL;
+
+	test_monitor->app = g_new0(struct adv_monitor_app, 1);
+	if (!test_monitor->app)
+		goto app_failed;
+
+	test_monitor->app->manager = g_new0(struct btd_adv_monitor_manager, 1);
+	if (!test_monitor->app->manager)
+		goto manager_failed;
+
+	test_monitor->high_rssi = high_rssi;
+	test_monitor->high_rssi_timeout = high_timeout;
+	test_monitor->low_rssi = low_rssi;
+	test_monitor->low_rssi_timeout = low_timeout;
+	test_monitor->devices = queue_new();
+
+	return test_monitor;
+
+manager_failed:
+	g_free(test_monitor->app);
+
+app_failed:
+	g_free(test_monitor);
+
+	return NULL;
+}
+
+/* Cleanup after unit test is done */
+void btd_adv_monitor_rssi_test_teardown(void *monitor_obj)
+{
+	struct adv_monitor *monitor = monitor_obj;
+
+	if (!monitor)
+		return;
+
+	queue_destroy(monitor->devices, monitor_device_free);
+	g_free(monitor);
+}
+
+/* Returns the current state of device - found/lost, used in unit tests */
+bool btd_adv_monitor_test_device_state(void *monitor_obj, void *device_obj)
+{
+	struct adv_monitor *monitor = monitor_obj;
+	struct btd_device *device = device_obj;
+	struct adv_monitor_device *dev = NULL;
+
+	if (!monitor || !device)
+		return false;
+
+	dev = queue_find(monitor->devices, monitor_device_match, device);
+	if (!dev)
+		return false;
+
+	return dev->device_found;
+}
+
+/* Helper function for the RSSI Filter unit tests */
+bool btd_adv_monitor_test_rssi(void *monitor_obj, void *device_obj,
+			       int8_t adv_rssi)
+{
+	struct adv_monitor *monitor = monitor_obj;
+	struct btd_device *device = device_obj;
+
+	if (!monitor || !device)
+		return false;
+
+	adv_monitor_filter_rssi(monitor, device, adv_rssi);
+
+	return btd_adv_monitor_test_device_state(monitor, device);
+}
diff --git a/src/adv_monitor.h b/src/adv_monitor.h
index 14508e7d1..351e7f9aa 100644
--- a/src/adv_monitor.h
+++ b/src/adv_monitor.h
@@ -33,4 +33,14 @@ void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager);
 void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager,
 				   struct btd_device *device);
 
+/* Following functions are the helper functions used for RSSI Filter unit tests
+ * defined in unit/test-adv-monitor.c
+ */
+void *btd_adv_monitor_rssi_test_setup(int8_t high_rssi, uint16_t high_timeout,
+				      int8_t low_rssi, uint16_t low_timeout);
+void btd_adv_monitor_rssi_test_teardown(void *monitor_obj);
+bool btd_adv_monitor_test_device_state(void *monitor_obj, void *device_obj);
+bool btd_adv_monitor_test_rssi(void *monitor_obj, void *device_obj,
+			       int8_t adv_rssi);
+
 #endif /* __ADV_MONITOR_H */
diff --git a/unit/test-adv-monitor.c b/unit/test-adv-monitor.c
new file mode 100644
index 000000000..970be84b0
--- /dev/null
+++ b/unit/test-adv-monitor.c
@@ -0,0 +1,391 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2020 Google LLC
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <glib.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "src/log.h"
+#include "src/shared/tester.h"
+
+#include "src/adv_monitor.h"
+
+#define define_test(name, type, data, setup_fn, test_fn, teardown_fn)	\
+	do {								\
+		static struct test_data test;				\
+		test.test_type = type;					\
+		test.test_name = g_strdup(name);			\
+		if (type == TEST_RSSI_FILTER) {				\
+			test.rssi_filter_test_data = (void *)&data;	\
+			test.rssi_filter_test_data->test_info = &test;	\
+		}							\
+		tester_add(name, &test, setup_fn, test_fn, teardown_fn);\
+	} while (0)
+
+#define ADV_INTERVAL		1	/* Advertisement interval in seconds */
+#define OUT_OF_RANGE		-128
+#define END_OF_RSSI_TEST	{0}
+
+#define RSSI_TEST_DONE(test_step)	\
+	(!test_step.adv_rssi && !test_step.duration && !test_step.result)
+
+#define DUMMY_BTD_DEVICE_OBJ	((void *) 0xF00)
+
+enum test_type {
+	TEST_RSSI_FILTER = 0,
+	TEST_CONTENT_FILTER,
+};
+
+enum result {
+	RESULT_DEVICE_NOT_FOUND = false,	/* Initial state of a device */
+	RESULT_DEVICE_FOUND = true,		/* Device state when the
+						 * Content/RSSI Filter match
+						 */
+	RESULT_DEVICE_LOST = false,		/* Device state when the Low
+						 * RSSI Filter match or if it
+						 * goes offline/out-of-range
+						 */
+};
+
+struct rssi_filter_test {
+	void *adv_monitor_obj;			/* struct adv_monitor object */
+	void *btd_device_obj;			/* struct btd_device object */
+	struct test_data *test_info;
+
+	const struct {
+		int8_t high_rssi_threshold;	/* High RSSI threshold */
+		uint16_t high_rssi_timeout;	/* High RSSI threshold timeout*/
+		int8_t low_rssi_threshold;	/* Low RSSI threshold */
+		uint16_t low_rssi_timeout;	/* Low RSSI threshold timeout */
+	} rssi_filter;
+
+	time_t start_time;		/* Start time of the test */
+	uint16_t resume_step;		/* Store the current sub-step of the
+					 * test before suspending that test
+					 */
+	guint out_of_range_timer;	/* Timer to simulate device offline */
+
+	const struct {
+		int8_t adv_rssi;	/* Advertisement RSSI */
+		uint16_t duration;	/* Advertisement duration in seconds */
+		enum result result;	/* Device state after every step */
+	} test_steps[];
+};
+
+/* Parent data structure to hold the test data and information,
+ * used by tester_* functions and callbacks.
+ */
+struct test_data {
+	enum test_type test_type;
+	char *test_name;
+
+	union {
+		struct rssi_filter_test *rssi_filter_test_data;
+	};
+};
+
+/* RSSI Filter Test 1:
+ * - The Device Lost event should NOT get triggered even if the Adv RSSI is
+ *   lower than LowRSSIThresh for more than LowRSSITimeout before finding
+ *   the device first.
+ * - Similarly, the Device Found event should NOT get triggered if the Adv RSSI
+ *   is greater than LowRSSIThresh but lower than HighRSSIThresh.
+ */
+static struct rssi_filter_test rssi_data_1 = {
+	.rssi_filter = {-40, 5, -60, 5},
+	.test_steps = {
+		{-70, 6, RESULT_DEVICE_NOT_FOUND},
+		{-50, 6, RESULT_DEVICE_NOT_FOUND},
+		END_OF_RSSI_TEST,
+	},
+};
+
+/* RSSI Filter Test 2:
+ * - The Device Found event should get triggered when the Adv RSSI is higher
+ *   than HighRSSIThresh for more than HighRSSITimeout.
+ * - Once the device is found, the Device Lost event should NOT get triggered
+ *   if the Adv RSSI drops below HighRSSIThresh but it is not lower than
+ *   LowRSSIThresh.
+ * - When the Adv RSSI drops below LowRSSIThresh for more than LowRSSITimeout,
+ *   the Device Lost event should get triggered.
+ */
+static struct rssi_filter_test rssi_data_2 = {
+	.rssi_filter = {-40, 5, -60, 5},
+	.test_steps = {
+		{-30, 6, RESULT_DEVICE_FOUND},
+		{-50, 6, RESULT_DEVICE_FOUND},
+		{-70, 6, RESULT_DEVICE_LOST},
+		END_OF_RSSI_TEST,
+	},
+};
+
+/* RSSI Filter Test 3:
+ * - The Device Found event should get triggered only when the Adv RSSI is
+ *   higher than HighRSSIThresh for more than HighRSSITimeout.
+ * - If the Adv RSSI drops below HighRSSIThresh, timer should reset and start
+ *   counting once the Adv RSSI is above HighRSSIThresh.
+ * - Similarly, when tracking the Low RSSI, timer should reset when the Adv RSSI
+ *   goes above LowRSSIThresh. The Device Lost event should get triggered only
+ *   when the Adv RSSI is lower than LowRSSIThresh for more than LowRSSITimeout.
+ */
+static struct rssi_filter_test rssi_data_3 = {
+	.rssi_filter = {-40, 5, -60, 5},
+	.test_steps = {
+		{-30, 2, RESULT_DEVICE_NOT_FOUND},
+		{-50, 6, RESULT_DEVICE_NOT_FOUND},
+		{-30, 4, RESULT_DEVICE_NOT_FOUND},
+		{-30, 2, RESULT_DEVICE_FOUND},
+		{-70, 2, RESULT_DEVICE_FOUND},
+		{-50, 6, RESULT_DEVICE_FOUND},
+		{-70, 4, RESULT_DEVICE_FOUND},
+		{-70, 2, RESULT_DEVICE_LOST},
+		END_OF_RSSI_TEST,
+	},
+};
+
+/* RSSI Filter Test 4:
+ * - While tracking the High RSSI, timer should reset if the device goes
+ *   offline/out-of-range for more than HighRSSITimeout.
+ * - Once the device is found, if the device goes offline/out-of-range for
+ *   more than LowRSSITimeout, the Device Lost event should get triggered.
+ */
+static struct rssi_filter_test rssi_data_4 = {
+	.rssi_filter = {-40, 5, -60, 5},
+	.test_steps = {
+		{         -30, 2, RESULT_DEVICE_NOT_FOUND},
+		{OUT_OF_RANGE, 6, RESULT_DEVICE_NOT_FOUND},
+		{         -30, 4, RESULT_DEVICE_NOT_FOUND},
+		{         -30, 2, RESULT_DEVICE_FOUND},
+		{         -70, 2, RESULT_DEVICE_FOUND},
+		{OUT_OF_RANGE, 6, RESULT_DEVICE_LOST},
+		END_OF_RSSI_TEST,
+	},
+};
+
+/* RSSI Filter Test 5:
+ * - The Device Found event should get triggered only once even if the Adv RSSI
+ *   stays higher than HighRSSIThresh for a longer period of time.
+ * - Once the device is found, while tracking the Low RSSI, timer should reset
+ *   when the Adv RSSI goes above LowRSSIThresh.
+ * - The timer should NOT reset if the device goes offline/out-of-range for
+ *   a very short period of time and comes back online/in-range before
+ *   the timeouts.
+ */
+static struct rssi_filter_test rssi_data_5 = {
+	.rssi_filter = {-40, 5, -60, 5},
+	.test_steps = {
+		{         -30, 2, RESULT_DEVICE_NOT_FOUND},
+		{OUT_OF_RANGE, 2, RESULT_DEVICE_NOT_FOUND},
+		{         -30, 2, RESULT_DEVICE_FOUND},
+		{         -30, 3, RESULT_DEVICE_FOUND},
+		{         -30, 3, RESULT_DEVICE_FOUND},
+		{         -70, 2, RESULT_DEVICE_FOUND},
+		{OUT_OF_RANGE, 2, RESULT_DEVICE_FOUND},
+		{         -50, 6, RESULT_DEVICE_FOUND},
+		{         -70, 2, RESULT_DEVICE_FOUND},
+		{OUT_OF_RANGE, 2, RESULT_DEVICE_FOUND},
+		{         -70, 2, RESULT_DEVICE_LOST},
+		END_OF_RSSI_TEST,
+	},
+};
+
+/* Initialize the data required for RSSI Filter test */
+static void setup_rssi_filter_test(gpointer data)
+{
+	struct rssi_filter_test *test = data;
+
+	test->adv_monitor_obj = btd_adv_monitor_rssi_test_setup(
+					test->rssi_filter.high_rssi_threshold,
+					test->rssi_filter.high_rssi_timeout,
+					test->rssi_filter.low_rssi_threshold,
+					test->rssi_filter.low_rssi_timeout);
+
+	/* The RSSI Filter logic uses btd_device object only as a key in the
+	 * adv_monitor->devices list, it is never dereferenced nor used to
+	 * perform any operations related to btd_device. So we can use any
+	 * dummy address for unit testing.
+	 */
+	test->btd_device_obj = DUMMY_BTD_DEVICE_OBJ;
+
+	tester_setup_complete();
+}
+
+/* Cleanup after the RSSI Filter test is done */
+static void teardown_rssi_filter_test(gpointer data)
+{
+	struct rssi_filter_test *test = data;
+
+	btd_adv_monitor_rssi_test_teardown(test->adv_monitor_obj);
+
+	tester_teardown_complete();
+}
+
+/* Execute the sub-steps of RSSI Filter test */
+static gboolean test_rssi_filter(gpointer data)
+{
+	struct rssi_filter_test *test = data;
+	time_t start_time = time(NULL);
+	bool ret = false;
+
+	uint16_t i = 0;
+	uint16_t j = 0;
+
+	/* If this is not the beginning of test, return to the sub-step
+	 * before that test was suspended
+	 */
+	if (test->resume_step) {
+		start_time = test->start_time;
+		i = test->resume_step;
+
+		/* Clear the test resume timer */
+		g_source_remove(test->out_of_range_timer);
+		test->out_of_range_timer = 0;
+
+		/* Check state of the device - found/lost, while device was
+		 * offline/out-of-range
+		 */
+		ret = btd_adv_monitor_test_device_state(test->adv_monitor_obj,
+							test->btd_device_obj);
+		tester_debug("%s: [t=%.0lf, step=%d] Test resume, "
+			     "device_found = %s",
+			     test->test_info->test_name,
+			     difftime(time(NULL), start_time), i,
+			     ret ? "true" : "false");
+		g_assert(ret == test->test_steps[i].result);
+
+		i++;
+	}
+
+	while (!RSSI_TEST_DONE(test->test_steps[i])) {
+		if (test->test_steps[i].adv_rssi == OUT_OF_RANGE) {
+			/* Simulate device offline/out-of-range by suspending
+			 * the test.
+			 *
+			 * Note: All tester_* functions run sequentially by
+			 * adding a next function to the main loop using
+			 * g_idle_add(). If a timeout function is added using
+			 * g_timeout_add_*(), it doesn't really get invoked as
+			 * soon as the timer expires. Instead, it is invoked
+			 * once the current function returns and the timer has
+			 * expired. So, to give handle_device_lost_timeout()
+			 * function a chance to run at the correct time, we
+			 * must save the current state and exit from this
+			 * function while we simulate the device offline. We can
+			 * come back later to continue with the remaining steps.
+			 */
+			test->resume_step = i;
+			test->start_time = start_time;
+			test->out_of_range_timer = g_timeout_add_seconds(
+						   test->test_steps[i].duration,
+						   test_rssi_filter, data);
+
+			/* Check the device state before suspending the test */
+			ret = btd_adv_monitor_test_device_state(
+							test->adv_monitor_obj,
+							test->btd_device_obj);
+			tester_debug("%s: [t=%.0lf, step=%d] Test suspend, "
+				     "device_found = %s",
+				     test->test_info->test_name,
+				     difftime(time(NULL), start_time), i,
+				     ret ? "true" : "false");
+			return FALSE;
+		}
+
+		for (j = 0; j < test->test_steps[i].duration; j++) {
+			ret = btd_adv_monitor_test_rssi(
+						test->adv_monitor_obj,
+						test->btd_device_obj,
+						test->test_steps[i].adv_rssi);
+			tester_debug("%s: [t=%.0lf, step=%d] Test "
+				     "advertisement RSSI %d, device_found = %s",
+				     test->test_info->test_name,
+				     difftime(time(NULL), start_time), i,
+				     test->test_steps[i].adv_rssi,
+				     ret ? "true" : "false");
+
+			/* Sleep for a second to simulate receiving
+			 * advertisement once every second
+			 */
+			sleep(ADV_INTERVAL);
+		}
+		g_assert(ret == test->test_steps[i].result);
+
+		i++;
+	}
+
+	tester_debug("%s: [t=%.0lf] Test done", test->test_info->test_name,
+		     difftime(time(NULL), start_time));
+
+	tester_test_passed();
+
+	return FALSE;
+}
+
+/* Handler function to prepare for a test */
+static void setup_handler(gconstpointer data)
+{
+	const struct test_data *test = data;
+
+	if (test->test_type == TEST_RSSI_FILTER)
+		setup_rssi_filter_test(test->rssi_filter_test_data);
+}
+
+/* Handler function to cleanup after the test is done */
+static void teardown_handler(gconstpointer data)
+{
+	const struct test_data *test = data;
+
+	if (test->test_type == TEST_RSSI_FILTER)
+		teardown_rssi_filter_test(test->rssi_filter_test_data);
+}
+
+/* Handler function to execute a test with the given data set */
+static void test_handler(gconstpointer data)
+{
+	const struct test_data *test = data;
+
+	if (test->test_type == TEST_RSSI_FILTER)
+		test_rssi_filter(test->rssi_filter_test_data);
+}
+
+int main(int argc, char *argv[])
+{
+	tester_init(&argc, &argv);
+
+	__btd_log_init("*", 0);
+
+	define_test("/advmon/rssi/1", TEST_RSSI_FILTER, rssi_data_1,
+		    setup_handler, test_handler, teardown_handler);
+	define_test("/advmon/rssi/2", TEST_RSSI_FILTER, rssi_data_2,
+		    setup_handler, test_handler, teardown_handler);
+	define_test("/advmon/rssi/3", TEST_RSSI_FILTER, rssi_data_3,
+		    setup_handler, test_handler, teardown_handler);
+	define_test("/advmon/rssi/4", TEST_RSSI_FILTER, rssi_data_4,
+		    setup_handler, test_handler, teardown_handler);
+	define_test("/advmon/rssi/5", TEST_RSSI_FILTER, rssi_data_5,
+		    setup_handler, test_handler, teardown_handler);
+
+	return tester_run();
+}
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [BlueZ PATCH v4 3/8] adv_monitor: Implement Adv matching based on stored monitors
  2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 2/8] adv_monitor: Implement unit tests for RSSI Filter Miao-chen Chou
@ 2020-09-17 21:25 ` Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 4/8] adv_monitor: Implement unit tests for content filter Miao-chen Chou
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17 21:25 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Manish Mandlik, Alain Michaud, chromeos-bluetooth-upstreaming,
	Luiz Augusto von Dentz, Marcel Holtmann, Howard Chung,
	Miao-chen Chou, Abhishek Pandit-Subedi

This implements create an entry point in adapter to start the matching of
Adv based on all monitors and invoke the RSSI tracking for Adv reporting.

Reviewed-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
Reviewed-by: Alain Michaud <alainm@chromium.org>
Reviewed-by: Manish Mandlik <mmandlik@chromium.org>
---

(no changes since v3)

Changes in v3:
- Remove unused variables
- Fix signature of queue_find()

 src/adapter.c     |  35 +++++--
 src/adv_monitor.c | 237 +++++++++++++++++++++++++++++++++++++++++-----
 src/adv_monitor.h |  19 ++++
 3 files changed, 259 insertions(+), 32 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 415d6e06b..d33ce7124 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -6614,6 +6614,15 @@ static void update_found_devices(struct btd_adapter *adapter,
 	bool name_known, discoverable;
 	char addr[18];
 	bool duplicate = false;
+	GSList *matched_monitors;
+
+	/* During the background scanning, update the device only when the data
+	 * match at least one Adv monitor
+	 */
+	matched_monitors = btd_adv_monitor_content_filter(
+				adapter->adv_monitor_manager, data, data_len);
+	if (!adapter->discovering && !matched_monitors)
+		return;
 
 	memset(&eir_data, 0, sizeof(eir_data));
 	eir_parse(&eir_data, data, data_len);
@@ -6659,18 +6668,22 @@ static void update_found_devices(struct btd_adapter *adapter,
 		device_store_cached_name(dev, eir_data.name);
 
 	/*
-	 * Only skip devices that are not connected, are temporary and there
-	 * is no active discovery session ongoing.
+	 * Only skip devices that are not connected, are temporary, and there
+	 * is no active discovery session ongoing and no matched Adv monitors
 	 */
-	if (!btd_device_is_connected(dev) && (device_is_temporary(dev) &&
-						 !adapter->discovery_list)) {
+	if (!btd_device_is_connected(dev) &&
+		(device_is_temporary(dev) && !adapter->discovery_list) &&
+		!matched_monitors) {
 		eir_data_free(&eir_data);
 		return;
 	}
 
-	/* Don't continue if not discoverable or if filter don't match */
-	if (!discoverable || (adapter->filtered_discovery &&
-	    !is_filter_match(adapter->discovery_list, &eir_data, rssi))) {
+	/* If there is no matched Adv monitors, don't continue if not
+	 * discoverable or if active discovery filter don't match.
+	 */
+	if (!matched_monitors && (!discoverable ||
+		(adapter->filtered_discovery && !is_filter_match(
+				adapter->discovery_list, &eir_data, rssi)))) {
 		eir_data_free(&eir_data);
 		return;
 	}
@@ -6727,6 +6740,14 @@ static void update_found_devices(struct btd_adapter *adapter,
 
 	eir_data_free(&eir_data);
 
+	/* After the device is updated, notify the matched Adv monitors */
+	if (matched_monitors) {
+		btd_adv_monitor_notify_monitors(adapter->adv_monitor_manager,
+						dev, rssi, matched_monitors);
+		g_slist_free(matched_monitors);
+		matched_monitors = NULL;
+	}
+
 	/*
 	 * Only if at least one client has requested discovery, maintain
 	 * list of found devices and name confirming for legacy devices.
diff --git a/src/adv_monitor.c b/src/adv_monitor.c
index 046f5953f..211094c89 100644
--- a/src/adv_monitor.c
+++ b/src/adv_monitor.c
@@ -38,15 +38,12 @@
 #include "device.h"
 #include "log.h"
 #include "src/error.h"
-#include "src/shared/ad.h"
 #include "src/shared/mgmt.h"
 #include "src/shared/queue.h"
 #include "src/shared/util.h"
 
 #include "adv_monitor.h"
 
-static void monitor_device_free(void *data);
-
 #define ADV_MONITOR_INTERFACE		"org.bluez.AdvertisementMonitor1"
 #define ADV_MONITOR_MGR_INTERFACE	"org.bluez.AdvertisementMonitorManager1"
 
@@ -93,7 +90,7 @@ enum monitor_state {
 	MONITOR_STATE_HONORED,	/* Accepted by kernel */
 };
 
-struct pattern {
+struct btd_adv_monitor_pattern {
 	uint8_t ad_type;
 	uint8_t offset;
 	uint8_t length;
@@ -142,6 +139,23 @@ struct app_match_data {
 	const char *path;
 };
 
+struct adv_content_filter_info {
+	uint8_t eir_len;
+	const uint8_t *eir;
+
+	bool matched;			/* Intermediate state per monitor */
+	GSList *matched_monitors;	/* List of matched monitors */
+};
+
+struct adv_rssi_filter_info {
+	struct btd_device *device;
+	int8_t rssi;
+};
+
+static void monitor_device_free(void *data);
+static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
+					struct btd_device *device, int8_t rssi);
+
 const struct adv_monitor_type {
 	enum monitor_type type;
 	const char *name;
@@ -164,7 +178,7 @@ static void app_reply_msg(struct adv_monitor_app *app, DBusMessage *reply)
 /* Frees a pattern */
 static void pattern_free(void *data)
 {
-	struct pattern *pattern = data;
+	struct btd_adv_monitor_pattern *pattern = data;
 
 	if (!pattern)
 		return;
@@ -172,6 +186,12 @@ static void pattern_free(void *data)
 	free(pattern);
 }
 
+void btd_adv_monitor_test_pattern_destroy(
+					struct btd_adv_monitor_pattern *pattern)
+{
+	pattern_free(pattern);
+}
+
 /* Frees a monitor object */
 static void monitor_free(void *data)
 {
@@ -444,6 +464,42 @@ failed:
 	return false;
 }
 
+/* Allocates and initiates a pattern with the given content */
+static struct btd_adv_monitor_pattern *pattern_create(
+	uint8_t ad_type, uint8_t offset, uint8_t length, const uint8_t *value)
+{
+	struct btd_adv_monitor_pattern *pattern;
+
+	if (offset > BT_AD_MAX_DATA_LEN - 1)
+		return NULL;
+
+	if ((ad_type > BT_AD_3D_INFO_DATA &&
+		ad_type != BT_AD_MANUFACTURER_DATA) ||
+		ad_type < BT_AD_FLAGS) {
+		return NULL;
+	}
+
+	if (!value || !length || offset + length > BT_AD_MAX_DATA_LEN)
+		return NULL;
+
+	pattern = new0(struct btd_adv_monitor_pattern, 1);
+	if (!pattern)
+		return NULL;
+
+	pattern->ad_type = ad_type;
+	pattern->offset = offset;
+	pattern->length = length;
+	memcpy(pattern->value, value, pattern->length);
+
+	return pattern;
+}
+
+struct btd_adv_monitor_pattern *btd_adv_monitor_test_pattern_create(
+	uint8_t ad_type, uint8_t offset, uint8_t length, const uint8_t *value)
+{
+	return pattern_create(ad_type, offset, length, value);
+}
+
 /* Retrieves Patterns from the remote Adv Monitor object, verifies the values
  * and update the local Adv Monitor
  */
@@ -473,7 +529,7 @@ static bool parse_patterns(struct adv_monitor *monitor, const char *path)
 		int value_len;
 		uint8_t *value;
 		uint8_t offset, ad_type;
-		struct pattern *pattern;
+		struct btd_adv_monitor_pattern *pattern;
 		DBusMessageIter struct_iter, value_iter;
 
 		dbus_message_iter_recurse(&array_iter, &struct_iter);
@@ -505,28 +561,10 @@ static bool parse_patterns(struct adv_monitor *monitor, const char *path)
 		dbus_message_iter_get_fixed_array(&value_iter, &value,
 							&value_len);
 
-		// Verify the values
-		if (offset > BT_AD_MAX_DATA_LEN - 1)
-			goto failed;
-
-		if ((ad_type > BT_AD_3D_INFO_DATA &&
-			ad_type != BT_AD_MANUFACTURER_DATA) ||
-			ad_type < BT_AD_FLAGS) {
-			goto failed;
-		}
-
-		if (!value || value_len <= 0 || value_len > BT_AD_MAX_DATA_LEN)
-			goto failed;
-
-		pattern = new0(struct pattern, 1);
+		pattern = pattern_create(ad_type, offset, value_len, value);
 		if (!pattern)
 			goto failed;
 
-		pattern->ad_type = ad_type;
-		pattern->offset = offset;
-		pattern->length = value_len;
-		memcpy(pattern->value, value, pattern->length);
-
 		queue_push_tail(monitor->patterns, pattern);
 
 		dbus_message_iter_next(&array_iter);
@@ -961,6 +999,155 @@ void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager)
 	manager_destroy(manager);
 }
 
+/* Matches the content based on the given pattern */
+bool btd_adv_monitor_pattern_match(
+	const uint8_t *eir, uint8_t eir_len,
+	const struct btd_adv_monitor_pattern *pattern)
+{
+	const uint8_t *data;
+	uint8_t idx = 0;
+	uint8_t field_len, data_len, data_type;
+
+	while (idx < eir_len - 1) {
+		field_len = eir[0];
+
+		/* Check for the end of EIR */
+		if (field_len == 0)
+			break;
+
+		idx += field_len + 1;
+
+		/* Do not continue filtering if got incorrect length */
+		if (idx >= eir_len)
+			break;
+
+		data = &eir[2];
+		data_type = eir[1];
+		data_len = field_len - 1;
+
+		eir += field_len + 1;
+
+		if (data_type != pattern->ad_type)
+			continue;
+
+		if (data_len < pattern->offset + pattern->length)
+			continue;
+
+		if (pattern->offset + pattern->length > BT_AD_MAX_DATA_LEN)
+			continue;
+
+		if (!memcmp(data + pattern->offset, pattern->value,
+				pattern->length))
+			return true;
+	}
+
+	return false;
+}
+
+/* Processes the content matching based on a pattern */
+static void adv_match_per_pattern(void *data, void *user_data)
+{
+	struct btd_adv_monitor_pattern *pattern = data;
+	struct adv_content_filter_info *info = user_data;
+
+	if (!pattern || info->matched)
+		return;
+
+	info->matched = btd_adv_monitor_pattern_match(info->eir, info->eir_len,
+							pattern);
+}
+
+/* Processes the content matching based pattern(s) of a monitor */
+static void adv_match_per_monitor(void *data, void *user_data)
+{
+	struct adv_monitor *monitor = data;
+	struct adv_content_filter_info *info = user_data;
+
+	if (!monitor && monitor->state != MONITOR_STATE_HONORED)
+		return;
+
+	/* Reset the intermediate matched status */
+	info->matched = false;
+
+	if (monitor->type == MONITOR_TYPE_OR_PATTERNS) {
+		queue_foreach(monitor->patterns, adv_match_per_pattern, info);
+		if (info->matched)
+			goto matched;
+	}
+
+	return;
+
+matched:
+	info->matched_monitors = g_slist_prepend(info->matched_monitors,
+							monitor);
+}
+
+/* Processes the content matching for the monitor(s) of an app */
+static void adv_match_per_app(void *data, void *user_data)
+{
+	struct adv_monitor_app *app = data;
+
+	if (!app)
+		return;
+
+	queue_foreach(app->monitors, adv_match_per_monitor, user_data);
+}
+
+/* Processes the content matching for every app without RSSI filtering and
+ * notifying monitors. The caller is responsible of releasing the memory of the
+ * list but not the data.
+ * Returns the list of monitors whose content match eir.
+ */
+GSList *btd_adv_monitor_content_filter(struct btd_adv_monitor_manager *manager,
+					const uint8_t *eir, uint8_t eir_len)
+{
+	struct adv_content_filter_info info;
+
+	if (!manager || !eir || !eir_len)
+		return NULL;
+
+	info.eir_len = eir_len;
+	info.eir = eir;
+	info.matched_monitors = NULL;
+
+	queue_foreach(manager->apps, adv_match_per_app, &info);
+
+	return info.matched_monitors;
+}
+
+/* Wraps adv_monitor_filter_rssi() to processes the content-matched monitor with
+ * RSSI filtering and notifies it on device found/lost event
+ */
+static void monitor_filter_rssi(gpointer a, gpointer b)
+{
+	struct adv_monitor *monitor = a;
+	struct adv_rssi_filter_info *info = b;
+
+	if (!monitor || !info)
+		return;
+
+	adv_monitor_filter_rssi(monitor, info->device, info->rssi);
+}
+
+/* Processes every content-matched monitor with RSSI filtering and notifies on
+ * device found/lost event. The caller is responsible of releasing the memory
+ * of matched_monitors list but not its data.
+ */
+void btd_adv_monitor_notify_monitors(struct btd_adv_monitor_manager *manager,
+					struct btd_device *device, int8_t rssi,
+					GSList *matched_monitors)
+{
+	struct adv_rssi_filter_info info;
+
+	if (!manager || !device || !matched_monitors)
+		return;
+
+	info.device = device;
+	info.rssi = rssi;
+
+	g_slist_foreach(matched_monitors, monitor_filter_rssi, &info);
+}
+
 /* Matches a device based on btd_device object */
 static bool monitor_device_match(const void *a, const void *b)
 {
diff --git a/src/adv_monitor.h b/src/adv_monitor.h
index 351e7f9aa..b660f5941 100644
--- a/src/adv_monitor.h
+++ b/src/adv_monitor.h
@@ -20,16 +20,28 @@
 #ifndef __ADV_MONITOR_H
 #define __ADV_MONITOR_H
 
+#include <glib.h>
+
+#include "src/shared/ad.h"
+
 struct mgmt;
 struct btd_device;
 struct btd_adapter;
 struct btd_adv_monitor_manager;
+struct btd_adv_monitor_pattern;
 
 struct btd_adv_monitor_manager *btd_adv_monitor_manager_create(
 						struct btd_adapter *adapter,
 						struct mgmt *mgmt);
 void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager);
 
+GSList *btd_adv_monitor_content_filter(struct btd_adv_monitor_manager *manager,
+					const uint8_t *eir, uint8_t eir_len);
+
+void btd_adv_monitor_notify_monitors(struct btd_adv_monitor_manager *manager,
+					struct btd_device *device, int8_t rssi,
+					GSList *matched_monitors);
+
 void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager,
 				   struct btd_device *device);
 
@@ -42,5 +54,12 @@ void btd_adv_monitor_rssi_test_teardown(void *monitor_obj);
 bool btd_adv_monitor_test_device_state(void *monitor_obj, void *device_obj);
 bool btd_adv_monitor_test_rssi(void *monitor_obj, void *device_obj,
 			       int8_t adv_rssi);
+struct btd_adv_monitor_pattern *btd_adv_monitor_test_pattern_create(
+	uint8_t ad_type, uint8_t offset, uint8_t length, const uint8_t *value);
+void btd_adv_monitor_test_pattern_destroy(
+				struct btd_adv_monitor_pattern *pattern);
+bool btd_adv_monitor_pattern_match(
+	const uint8_t *eir, uint8_t eir_len,
+	const struct btd_adv_monitor_pattern *pattern);
 
 #endif /* __ADV_MONITOR_H */
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [BlueZ PATCH v4 4/8] adv_monitor: Implement unit tests for content filter
  2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 2/8] adv_monitor: Implement unit tests for RSSI Filter Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 3/8] adv_monitor: Implement Adv matching based on stored monitors Miao-chen Chou
@ 2020-09-17 21:25 ` Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 5/8] adapter: Clear all Adv monitors upon bring-up Miao-chen Chou
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17 21:25 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Manish Mandlik, Alain Michaud, chromeos-bluetooth-upstreaming,
	Luiz Augusto von Dentz, Marcel Holtmann, Howard Chung,
	Miao-chen Chou, Abhishek Pandit-Subedi

This implements the unit tests for verifying the correctness of
advertisement data fields matching against a pattern.

Reviewed-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
Reviewed-by: Alain Michaud <alainm@chromium.org>
Reviewed-by: Manish Mandlik <mmandlik@chromium.org>
---

(no changes since v3)

Changes in v3:
- Fix mixed declarations and assignments

Changes in v2:
- Cast test data to void *

 doc/test-coverage.txt   |   4 +-
 unit/test-adv-monitor.c | 144 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 146 insertions(+), 2 deletions(-)

diff --git a/doc/test-coverage.txt b/doc/test-coverage.txt
index 5296983e6..e15474a44 100644
--- a/doc/test-coverage.txt
+++ b/doc/test-coverage.txt
@@ -30,9 +30,9 @@ test-gobex-transfer	  36	OBEX transfer handling
 test-gdbus-client	  13	D-Bus client handling
 test-gatt		 180	GATT qualification test cases
 test-hog		   6	HID Over GATT qualification test cases
-test-adv-monitor	   5	Advertisement Monitor test cases
+test-adv-monitor	   9	Advertisement Monitor test cases
 			-----
-			 766
+			 770
 
 
 Automated end-to-end testing
diff --git a/unit/test-adv-monitor.c b/unit/test-adv-monitor.c
index 970be84b0..a19a3092b 100644
--- a/unit/test-adv-monitor.c
+++ b/unit/test-adv-monitor.c
@@ -40,6 +40,8 @@
 		if (type == TEST_RSSI_FILTER) {				\
 			test.rssi_filter_test_data = (void *)&data;	\
 			test.rssi_filter_test_data->test_info = &test;	\
+		} else if (type == TEST_CONTENT_FILTER) {		\
+			test.content_filter_test_data = (void *)&data;	\
 		}							\
 		tester_add(name, &test, setup_fn, test_fn, teardown_fn);\
 	} while (0)
@@ -94,6 +96,22 @@ struct rssi_filter_test {
 	} test_steps[];
 };
 
+struct content_filter_test {
+	void *advmon_pattern;		/* btd_adv_monitor_pattern */
+
+	bool expected_match;
+
+	const struct {
+		uint8_t ad_type;
+		uint8_t offset;
+		uint8_t length;
+		uint8_t value[BT_AD_MAX_DATA_LEN];
+	} pattern;
+
+	uint8_t eir_len;
+	uint8_t eir[];
+};
+
 /* Parent data structure to hold the test data and information,
  * used by tester_* functions and callbacks.
  */
@@ -103,6 +121,7 @@ struct test_data {
 
 	union {
 		struct rssi_filter_test *rssi_filter_test_data;
+		struct content_filter_test *content_filter_test_data;
 	};
 };
 
@@ -211,6 +230,62 @@ static struct rssi_filter_test rssi_data_5 = {
 	},
 };
 
+/* Content Filter Test 1:
+ * The valid EIR data contains the given pattern whose content is a UUID16 AD
+ * data.
+ */
+static struct content_filter_test content_data_1 = {
+	.expected_match = true,
+	.pattern = {0x03, 0x02, 0x02, {0x09, 0x18} },
+	.eir_len = 20,
+	.eir = {0x02, 0x01, 0x02,				// flags
+		0x06, 0xff, 0x96, 0xfd, 0xab, 0xcd, 0xef,	// Mfr. Data
+		0x05, 0x03, 0x0d, 0x18, 0x09, 0x18,		// 16-bit UUIDs
+		0x05, 0x16, 0x0d, 0x18, 0x12, 0x34},		// Service Data
+};
+
+/* Content Filter Test 2:
+ * The valid EIR data does not match the given pattern whose content is a UUID16
+ * AD data.
+ */
+static struct content_filter_test content_data_2 = {
+	.expected_match = false,
+	.pattern = {0x03, 0x02, 0x02, {0x0d, 0x18} },
+	.eir_len = 20,
+	.eir = {0x02, 0x01, 0x02,				// flags
+		0x06, 0xff, 0x96, 0xfd, 0xab, 0xcd, 0xef,	// Mfr. Data
+		0x05, 0x03, 0x0d, 0x18, 0x09, 0x18,		// 16-bit UUIDs
+		0x05, 0x16, 0x0d, 0x18, 0x12, 0x34},		// Service Data
+};
+
+/* Content Filter Test 3:
+ * The valid EIR data does not have the given pattern whose content is a UUID32
+ * AD data.
+ */
+static struct content_filter_test content_data_3 = {
+	.expected_match = false,
+	.pattern = {0x05, 0x00, 0x04, {0x09, 0x18, 0x00, 0x00} },
+	.eir_len = 20,
+	.eir = {0x02, 0x01, 0x02,				// flags
+		0x06, 0xff, 0x96, 0xfd, 0xab, 0xcd, 0xef,	// Mfr. Data
+		0x05, 0x03, 0x0d, 0x18, 0x09, 0x18,		// 16-bit UUIDs
+		0x05, 0x16, 0x0d, 0x18, 0x12, 0x34},		// Service Data
+};
+
+/* Content Filter Test 4:
+ * The valid EIR data does not match the given pattern whose content is a
+ * UUID16 AD data due to invalid starting position of matching.
+ */
+static struct content_filter_test content_data_4 = {
+	.expected_match = false,
+	.pattern = {0x03, 0x02, 0x02, {0x09, 0x18} },
+	.eir_len = 20,
+	.eir = {0x02, 0x01, 0x02,				// flags
+		0x06, 0xff, 0x96, 0xfd, 0xab, 0xcd, 0xef,	// Mfr. Data
+		0x03, 0x03, 0x09, 0x18,				// 16-bit UUIDs
+		0x05, 0x16, 0x0d, 0x18, 0x12, 0x34},		// Service Data
+};
+
 /* Initialize the data required for RSSI Filter test */
 static void setup_rssi_filter_test(gpointer data)
 {
@@ -343,6 +418,60 @@ static gboolean test_rssi_filter(gpointer data)
 	return FALSE;
 }
 
+/* Initialize the data required for Content Filter test */
+static void setup_content_filter_test(gpointer data)
+{
+	struct content_filter_test *test = data;
+	struct btd_adv_monitor_pattern *pattern = NULL;
+
+	pattern = btd_adv_monitor_test_pattern_create(test->pattern.ad_type,
+							test->pattern.offset,
+							test->pattern.length,
+							test->pattern.value);
+	if (!pattern) {
+		tester_setup_failed();
+		return;
+	}
+
+	test->advmon_pattern = pattern;
+	tester_setup_complete();
+}
+
+/* Cleanup after the Content Filter test is done */
+static void teardown_content_filter_test(gpointer data)
+{
+	struct content_filter_test *test = data;
+
+	if (!test)
+		tester_teardown_complete();
+
+	btd_adv_monitor_test_pattern_destroy(test->advmon_pattern);
+	test->advmon_pattern = NULL;
+
+	tester_teardown_complete();
+}
+
+/* Execute the sub-steps of Content Filter test */
+static void test_content_filter(gpointer data)
+{
+	struct content_filter_test *test = data;
+	struct btd_adv_monitor_pattern *pattern = test->advmon_pattern;
+
+	if (!pattern) {
+		tester_test_abort();
+		return;
+	}
+
+	if (btd_adv_monitor_pattern_match(test->eir, test->eir_len,
+						test->advmon_pattern) ==
+		test->expected_match) {
+		tester_test_passed();
+		return;
+	}
+
+	tester_test_failed();
+}
+
 /* Handler function to prepare for a test */
 static void setup_handler(gconstpointer data)
 {
@@ -350,6 +479,8 @@ static void setup_handler(gconstpointer data)
 
 	if (test->test_type == TEST_RSSI_FILTER)
 		setup_rssi_filter_test(test->rssi_filter_test_data);
+	else if (test->test_type == TEST_CONTENT_FILTER)
+		setup_content_filter_test(test->content_filter_test_data);
 }
 
 /* Handler function to cleanup after the test is done */
@@ -359,6 +490,8 @@ static void teardown_handler(gconstpointer data)
 
 	if (test->test_type == TEST_RSSI_FILTER)
 		teardown_rssi_filter_test(test->rssi_filter_test_data);
+	else if (test->test_type == TEST_CONTENT_FILTER)
+		teardown_content_filter_test(test->content_filter_test_data);
 }
 
 /* Handler function to execute a test with the given data set */
@@ -368,6 +501,8 @@ static void test_handler(gconstpointer data)
 
 	if (test->test_type == TEST_RSSI_FILTER)
 		test_rssi_filter(test->rssi_filter_test_data);
+	else if (test->test_type == TEST_CONTENT_FILTER)
+		test_content_filter(test->content_filter_test_data);
 }
 
 int main(int argc, char *argv[])
@@ -387,5 +522,14 @@ int main(int argc, char *argv[])
 	define_test("/advmon/rssi/5", TEST_RSSI_FILTER, rssi_data_5,
 		    setup_handler, test_handler, teardown_handler);
 
+	define_test("/advmon/content/1", TEST_CONTENT_FILTER, content_data_1,
+		    setup_handler, test_handler, teardown_handler);
+	define_test("/advmon/content/2", TEST_CONTENT_FILTER, content_data_2,
+		    setup_handler, test_handler, teardown_handler);
+	define_test("/advmon/content/3", TEST_CONTENT_FILTER, content_data_3,
+		    setup_handler, test_handler, teardown_handler);
+	define_test("/advmon/content/4", TEST_CONTENT_FILTER, content_data_4,
+		    setup_handler, test_handler, teardown_handler);
+
 	return tester_run();
 }
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [BlueZ PATCH v4 5/8] adapter: Clear all Adv monitors upon bring-up
  2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
                   ` (2 preceding siblings ...)
  2020-09-17 21:25 ` [BlueZ PATCH v4 4/8] adv_monitor: Implement unit tests for content filter Miao-chen Chou
@ 2020-09-17 21:25 ` Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 6/8] adv_monitor: Implement Add Adv Patterns Monitor cmd handler Miao-chen Chou
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17 21:25 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Manish Mandlik, Alain Michaud, chromeos-bluetooth-upstreaming,
	Luiz Augusto von Dentz, Marcel Holtmann, Howard Chung,
	Miao-chen Chou

This clears all Adv monitors upon daemon bring-up by issuing
MGMT_OP_REMOVE_ADV_MONITOR command with monitor_handle 0.

The following test was performed:
- Add an Adv Monitor using btmgmt, restart bluetoothd and observe the
monitor got removed.

Reviewed-by: Alain Michaud <alainm@chromium.org>
Reviewed-by: Manish Mandlik <mmandlik@chromium.org>
Reviewed-by: Howard Chung <howardchung@google.com>
---

(no changes since v1)

 src/adapter.c | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/adapter.c b/src/adapter.c
index d33ce7124..191467048 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -9513,6 +9513,43 @@ failed:
 	btd_adapter_unref(adapter);
 }
 
+static void reset_adv_monitors_complete(uint8_t status, uint16_t length,
+					const void *param, void *user_data)
+{
+	struct mgmt_rp_remove_adv_monitor *rp = param;
+
+	if (status != MGMT_STATUS_SUCCESS) {
+		error("Failed to reset Adv Monitors: %s (0x%02x)",
+			mgmt_errstr(status), status);
+		return;
+	}
+
+	if (length < sizeof(*rp)) {
+		error("Wrong size of remove Adv Monitor response for reset "
+			"all Adv Monitors");
+		return;
+	}
+
+	DBG("Removed all Adv Monitors");
+}
+
+static void reset_adv_monitors(uint16_t index)
+{
+	struct mgmt_cp_remove_adv_monitor cp;
+
+	DBG("sending remove Adv Monitor command with handle 0");
+
+	/* Handle 0 indicates to remove all */
+	cp.monitor_handle = 0;
+	if (mgmt_send(mgmt_master, MGMT_OP_REMOVE_ADV_MONITOR, index,
+			sizeof(cp), &cp, reset_adv_monitors_complete, NULL,
+			NULL) > 0) {
+		return;
+	}
+
+	error("Failed to reset Adv Monitors");
+}
+
 static void index_added(uint16_t index, uint16_t length, const void *param,
 							void *user_data)
 {
@@ -9527,6 +9564,8 @@ static void index_added(uint16_t index, uint16_t length, const void *param,
 		return;
 	}
 
+	reset_adv_monitors(index);
+
 	adapter = btd_adapter_new(index);
 	if (!adapter) {
 		btd_error(index,
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [BlueZ PATCH v4 6/8] adv_monitor: Implement Add Adv Patterns Monitor cmd handler
  2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
                   ` (3 preceding siblings ...)
  2020-09-17 21:25 ` [BlueZ PATCH v4 5/8] adapter: Clear all Adv monitors upon bring-up Miao-chen Chou
@ 2020-09-17 21:25 ` Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 7/8] adv_monitor: Fix return type of RegisterMonitor() method Miao-chen Chou
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17 21:25 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Manish Mandlik, Alain Michaud, chromeos-bluetooth-upstreaming,
	Luiz Augusto von Dentz, Marcel Holtmann, Howard Chung,
	Miao-chen Chou

From: Howard Chung <howardchung@google.com>

- Send the MGMT_OP command to kernel upon registration of a Adv patterns
monitor.
- Call Activate() or Release() to client depending on the reply from
  kernel

Reviewed-by: Alain Michaud <alainm@chromium.org>
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
Reviewed-by: Manish Mandlik <mmandlik@chromium.org>
---

(no changes since v1)

 src/adv_monitor.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)

diff --git a/src/adv_monitor.c b/src/adv_monitor.c
index 211094c89..deaa1894a 100644
--- a/src/adv_monitor.c
+++ b/src/adv_monitor.c
@@ -609,11 +609,59 @@ done:
 	return monitor->state != MONITOR_STATE_FAILED;
 }
 
+/* Handles the callback of Add Adv Patterns Monitor command */
+static void add_adv_patterns_monitor_cb(uint8_t status, uint16_t length,
+					const void *param, void *user_data)
+{
+	const struct mgmt_rp_add_adv_patterns_monitor *rp = param;
+	struct adv_monitor *monitor = user_data;
+	uint16_t adapter_id = monitor->app->manager->adapter_id;
+
+	if (status != MGMT_STATUS_SUCCESS || !param) {
+		btd_error(adapter_id, "Failed to Add Adv Patterns Monitor "
+				"with status 0x%02x", status);
+		monitor_release(monitor, NULL);
+		return;
+	}
+
+	if (length < sizeof(*rp)) {
+		btd_error(adapter_id, "Wrong size of Add Adv Patterns Monitor "
+				"response");
+		monitor_release(monitor, NULL);
+		return;
+	}
+
+	monitor->state = MONITOR_STATE_HONORED;
+
+	DBG("Calling Activate() on Adv Monitor of owner %s at path %s",
+		monitor->app->owner, monitor->path);
+
+	g_dbus_proxy_method_call(monitor->proxy, "Activate", NULL, NULL, NULL,
+					NULL);
+
+	DBG("Adv Monitor with handle:0x%04x added",
+					le16_to_cpu(rp->monitor_handle));
+}
+
+static void monitor_copy_patterns(void *data, void *user_data)
+{
+	struct btd_adv_monitor_pattern *pattern = data;
+	struct mgmt_cp_add_adv_monitor *cp = user_data;
+
+	if (!pattern)
+		return;
+
+	memcpy(cp->patterns + cp->pattern_count, pattern, sizeof(*pattern));
+	cp->pattern_count++;
+}
+
 /* Handles an Adv Monitor D-Bus proxy added event */
 static void monitor_proxy_added_cb(GDBusProxy *proxy, void *user_data)
 {
 	struct adv_monitor *monitor;
 	struct adv_monitor_app *app = user_data;
+	struct mgmt_cp_add_adv_monitor *cp = NULL;
+	uint8_t pattern_count, cp_len;
 	uint16_t adapter_id = app->manager->adapter_id;
 	const char *path = g_dbus_proxy_get_path(proxy);
 	const char *iface = g_dbus_proxy_get_interface(proxy);
@@ -646,7 +694,24 @@ static void monitor_proxy_added_cb(GDBusProxy *proxy, void *user_data)
 
 	queue_push_tail(app->monitors, monitor);
 
+	pattern_count = queue_length(monitor->patterns);
+	cp_len = sizeof(struct mgmt_cp_add_adv_monitor) +
+			pattern_count * sizeof(struct mgmt_adv_pattern);
+
+	cp = malloc0(cp_len);
+	queue_foreach(monitor->patterns, monitor_copy_patterns, cp);
+
+	if (!mgmt_send(app->manager->mgmt, MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
+			adapter_id, cp_len, cp, add_adv_patterns_monitor_cb,
+			monitor, NULL)) {
+		error("Unable to send Add Adv Patterns Monitor command");
+		goto done;
+	}
+
 	DBG("Adv Monitor allocated for the object at path %s", path);
+
+done:
+	free(cp);
 }
 
 /* Handles the removal of an Adv Monitor D-Bus proxy */
@@ -1063,7 +1128,7 @@ static void adv_match_per_monitor(void *data, void *user_data)
 	struct adv_monitor *monitor = data;
 	struct adv_content_filter_info *info = user_data;
 
-	if (!monitor && monitor->state != MONITOR_STATE_HONORED)
+	if (!monitor || monitor->state != MONITOR_STATE_HONORED)
 		return;
 
 	/* Reset the intermediate matched status */
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [BlueZ PATCH v4 7/8] adv_monitor: Fix return type of RegisterMonitor() method
  2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
                   ` (4 preceding siblings ...)
  2020-09-17 21:25 ` [BlueZ PATCH v4 6/8] adv_monitor: Implement Add Adv Patterns Monitor cmd handler Miao-chen Chou
@ 2020-09-17 21:25 ` Miao-chen Chou
  2020-09-17 21:25 ` [BlueZ PATCH v4 8/8] adv_monitor: Issue Remove Adv Monitor mgmt call Miao-chen Chou
  2020-09-17 21:51 ` [BlueZ,v4,1/8] adv_monitor: Implement RSSI Filter logic for background scanning bluez.test.bot
  7 siblings, 0 replies; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17 21:25 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Manish Mandlik, Alain Michaud, chromeos-bluetooth-upstreaming,
	Luiz Augusto von Dentz, Marcel Holtmann, Howard Chung,
	Miao-chen Chou

This modifies the D-Bus call return type to be asynchronous for
RegisterMonitor() method call.

The following test was performed:
- Enter bluetoothctl, exit the console and re-enter the console without
AlreadyExist error for RegisterMonitor() upon bring-up of the console.

Reviewed-by: Howard Chung <howardchung@google.com>
Reviewed-by: Manish Mandlik <mmandlik@chromium.org>
---

(no changes since v1)

 src/adv_monitor.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/adv_monitor.c b/src/adv_monitor.c
index deaa1894a..b4fe39eff 100644
--- a/src/adv_monitor.c
+++ b/src/adv_monitor.c
@@ -758,6 +758,8 @@ static struct adv_monitor_app *app_create(DBusConnection *conn,
 
 	app->monitors = queue_new();
 
+	app->reg = dbus_message_ref(msg);
+
 	g_dbus_client_set_disconnect_watch(app->client, app_disconnect_cb, app);
 
 	/* Note that any property changes on a monitor object would not affect
@@ -769,8 +771,6 @@ static struct adv_monitor_app *app_create(DBusConnection *conn,
 
 	g_dbus_client_set_ready_watch(app->client, app_ready_cb, app);
 
-	app->reg = dbus_message_ref(msg);
-
 	return app;
 }
 
@@ -864,7 +864,7 @@ static DBusMessage *unregister_monitor(DBusConnection *conn,
 }
 
 static const GDBusMethodTable adv_monitor_methods[] = {
-	{ GDBUS_EXPERIMENTAL_METHOD("RegisterMonitor",
+	{ GDBUS_EXPERIMENTAL_ASYNC_METHOD("RegisterMonitor",
 					GDBUS_ARGS({ "application", "o" }),
 					NULL, register_monitor) },
 	{ GDBUS_EXPERIMENTAL_ASYNC_METHOD("UnregisterMonitor",
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [BlueZ PATCH v4 8/8] adv_monitor: Issue Remove Adv Monitor mgmt call
  2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
                   ` (5 preceding siblings ...)
  2020-09-17 21:25 ` [BlueZ PATCH v4 7/8] adv_monitor: Fix return type of RegisterMonitor() method Miao-chen Chou
@ 2020-09-17 21:25 ` Miao-chen Chou
  2020-09-17 21:51 ` [BlueZ,v4,1/8] adv_monitor: Implement RSSI Filter logic for background scanning bluez.test.bot
  7 siblings, 0 replies; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17 21:25 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Manish Mandlik, Alain Michaud, chromeos-bluetooth-upstreaming,
	Luiz Augusto von Dentz, Marcel Holtmann, Howard Chung

From: Alain Michaud <alainm@chromium.org>

This calls Remove Adv Monitor command to kernel and handles the callback
during a monitor removal initiated by a D-Bus client. This also
registers callback for getting notified on Adv Monitor Removed event, so
that the Adv monitor manager can invalidate the monitor by calling
Release() on its proxy.

The following tests were performed.
- In bluetoothctl console, add a monitor and remove the monitor by its
index and verify the removal in both the output of btmgmt and syslog.
- In bluetoothctl console, add a monitor, remove the monitor via
btmgmt and verify the removal in syslog.

Reviewed-by: Howard Chung <howardchung@google.com>
Reviewed-by: Alain Michaud <alainm@chromium.org>
---

Changes in v4:
- Fix build error

Changes in v3:
- Fix const qualifier of a pointer

 src/adv_monitor.c | 132 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 124 insertions(+), 8 deletions(-)

diff --git a/src/adv_monitor.c b/src/adv_monitor.c
index b4fe39eff..e2ad907cf 100644
--- a/src/adv_monitor.c
+++ b/src/adv_monitor.c
@@ -88,6 +88,7 @@ enum monitor_state {
 	MONITOR_STATE_FAILED,	/* Failed to be init'ed */
 	MONITOR_STATE_INITED,	/* Init'ed but not yet sent to kernel */
 	MONITOR_STATE_HONORED,	/* Accepted by kernel */
+	MONITOR_STATE_REMOVING, /* Removing from kernel */
 };
 
 struct btd_adv_monitor_pattern {
@@ -103,6 +104,7 @@ struct adv_monitor {
 	char *path;
 
 	enum monitor_state state;	/* MONITOR_STATE_* */
+	uint16_t monitor_handle;	/* Kernel Monitor Handle */
 
 	int8_t high_rssi;		/* High RSSI threshold */
 	uint16_t high_rssi_timeout;	/* High RSSI threshold timeout */
@@ -631,6 +633,7 @@ static void add_adv_patterns_monitor_cb(uint8_t status, uint16_t length,
 		return;
 	}
 
+	monitor->monitor_handle = le16_to_cpu(rp->monitor_handle);
 	monitor->state = MONITOR_STATE_HONORED;
 
 	DBG("Calling Activate() on Adv Monitor of owner %s at path %s",
@@ -639,8 +642,7 @@ static void add_adv_patterns_monitor_cb(uint8_t status, uint16_t length,
 	g_dbus_proxy_method_call(monitor->proxy, "Activate", NULL, NULL, NULL,
 					NULL);
 
-	DBG("Adv Monitor with handle:0x%04x added",
-					le16_to_cpu(rp->monitor_handle));
+	DBG("Adv monitor with handle:0x%04x added", monitor->monitor_handle);
 }
 
 static void monitor_copy_patterns(void *data, void *user_data)
@@ -714,20 +716,77 @@ done:
 	free(cp);
 }
 
+/* Handles the callback of Remove Adv Monitor command */
+static void remove_adv_monitor_cb(uint8_t status, uint16_t length,
+				const void *param, void *user_data)
+{
+	struct adv_monitor *monitor = user_data;
+	const struct mgmt_rp_remove_adv_monitor *rp = param;
+	uint16_t adapter_id = monitor->app->manager->adapter_id;
+
+	if (status != MGMT_STATUS_SUCCESS || !param) {
+		btd_error(adapter_id, "Failed to Remove Adv Monitor with "
+			"status 0x%02x", status);
+		goto done;
+	}
+
+	if (length < sizeof(*rp)) {
+		btd_error(adapter_id, "Wrong size of Remove Adv Monitor "
+				"response");
+		goto done;
+	}
+
+done:
+	queue_remove(monitor->app->monitors, monitor);
+
+	DBG("Adv Monitor removed with handle:0x%04x, path %s",
+		monitor->monitor_handle, monitor->path);
+
+	monitor_free(monitor);
+}
+
+
 /* Handles the removal of an Adv Monitor D-Bus proxy */
 static void monitor_proxy_removed_cb(GDBusProxy *proxy, void *user_data)
 {
 	struct adv_monitor *monitor;
+	struct mgmt_cp_remove_adv_monitor cp;
 	struct adv_monitor_app *app = user_data;
+	uint16_t adapter_id = app->manager->adapter_id;
 
-	monitor = queue_remove_if(app->monitors, monitor_match, proxy);
-	if (monitor) {
-		DBG("Adv Monitor removed for the object at path %s",
-			monitor->path);
+	monitor = queue_find(app->monitors, monitor_match, proxy);
 
-		/* The object was gone, so we don't need to call Release() */
-		monitor_free(monitor);
+	/* A monitor removed event from kernel can remove a monitor and notify
+	 * the app on Release() where this callback can be invoked, so we
+	 * simply skip here.
+	 */
+	if (!monitor)
+		return;
+
+	if (monitor->state != MONITOR_STATE_HONORED)
+		goto done;
+
+	monitor->state = MONITOR_STATE_REMOVING;
+
+	cp.monitor_handle = cpu_to_le16(monitor->monitor_handle);
+
+	if (!mgmt_send(app->manager->mgmt, MGMT_OP_REMOVE_ADV_MONITOR,
+			adapter_id, sizeof(cp), &cp, remove_adv_monitor_cb,
+			monitor, NULL)) {
+		btd_error(adapter_id, "Unable to send Remove Advt Monitor "
+				"command");
+		goto done;
 	}
+
+	return;
+
+done:
+	queue_remove(app->monitors, monitor);
+
+	DBG("Adv Monitor removed in state %02x with path %s", monitor->state,
+		monitor->path);
+
+	monitor_free(monitor);
 }
 
 /* Creates an app object, initiates it and sets D-Bus event handlers */
@@ -936,6 +995,59 @@ static const GDBusPropertyTable adv_monitor_properties[] = {
 	{ }
 };
 
+/* Matches a monitor based on its handle */
+static bool removed_monitor_match(const void *data, const void *user_data)
+{
+	const uint16_t *handle = user_data;
+	const struct adv_monitor *monitor = data;
+
+	if (!data || !handle)
+		return false;
+
+	return monitor->monitor_handle == *handle;
+}
+
+/* Remove the matched monitor and reports the removal to the app */
+static void app_remove_monitor(void *data, void *user_data)
+{
+	struct adv_monitor_app *app = data;
+	struct adv_monitor *monitor;
+
+	monitor = queue_find(app->monitors, removed_monitor_match, user_data);
+	if (monitor) {
+		if (monitor->state == MONITOR_STATE_HONORED)
+			monitor_release(monitor, NULL);
+
+		queue_remove(app->monitors, monitor);
+
+		DBG("Adv Monitor at path %s removed", monitor->path);
+
+		monitor_free(monitor);
+	}
+}
+
+/* Processes Adv Monitor removed event from kernel */
+static void adv_monitor_removed_callback(uint16_t index, uint16_t length,
+					const void *param, void *user_data)
+{
+	struct btd_adv_monitor_manager *manager = user_data;
+	const struct mgmt_ev_adv_monitor_removed *ev = param;
+	uint16_t handle = ev->monitor_handle;
+	const uint16_t adapter_id = manager->adapter_id;
+
+	if (length < sizeof(*ev)) {
+		btd_error(adapter_id, "Wrong size of Adv Monitor Removed "
+				"event");
+		return;
+	}
+
+	/* Traverse the apps to find the monitor */
+	queue_foreach(manager->apps, app_remove_monitor, &handle);
+
+	DBG("Adv Monitor removed event with handle 0x%04x processed",
+		ev->monitor_handle);
+}
+
 /* Allocates a manager object */
 static struct btd_adv_monitor_manager *manager_new(
 						struct btd_adapter *adapter,
@@ -955,6 +1067,10 @@ static struct btd_adv_monitor_manager *manager_new(
 	manager->adapter_id = btd_adapter_get_index(adapter);
 	manager->apps = queue_new();
 
+	mgmt_register(manager->mgmt, MGMT_EV_ADV_MONITOR_REMOVED,
+			manager->adapter_id, adv_monitor_removed_callback,
+			manager, NULL);
+
 	return manager;
 }
 
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* RE: [BlueZ,v4,2/8] adv_monitor: Implement unit tests for RSSI Filter
  2020-09-17 21:25 ` [BlueZ PATCH v4 2/8] adv_monitor: Implement unit tests for RSSI Filter Miao-chen Chou
@ 2020-09-17 21:49   ` bluez.test.bot
  0 siblings, 0 replies; 11+ messages in thread
From: bluez.test.bot @ 2020-09-17 21:49 UTC (permalink / raw)
  To: linux-bluetooth, mcchou

[-- Attachment #1: Type: text/plain, Size: 963 bytes --]


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.
While we are preparing for reviewing the patches, we found the following
issue/warning.

Test Result:
checkpatch Failed

Outputs:
WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1
#161: FILE: unit/test-adv-monitor.c:1:
+/*

- total: 0 errors, 1 warnings, 512 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.

Your patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED 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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: [BlueZ,v4,1/8] adv_monitor: Implement RSSI Filter logic for background scanning
  2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
                   ` (6 preceding siblings ...)
  2020-09-17 21:25 ` [BlueZ PATCH v4 8/8] adv_monitor: Issue Remove Adv Monitor mgmt call Miao-chen Chou
@ 2020-09-17 21:51 ` bluez.test.bot
  7 siblings, 0 replies; 11+ messages in thread
From: bluez.test.bot @ 2020-09-17 21:51 UTC (permalink / raw)
  To: linux-bluetooth, mcchou

[-- Attachment #1: Type: text/plain, Size: 47157 bytes --]


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.
While we are preparing for reviewing the patches, we found the following
issue/warning.

Test Result:
checkbuild Failed

Outputs:
ar: `u' modifier ignored since `D' is the default (see `U')
ar: `u' modifier ignored since `D' is the default (see `U')
ar: `u' modifier ignored since `D' is the default (see `U')
ar: `u' modifier ignored since `D' is the default (see `U')
ar: `u' modifier ignored since `D' is the default (see `U')
/usr/bin/ld: src/adv_monitor.o: in function `app_destroy':
/github/workspace/src/adv_monitor.c:245: undefined reference to `btd_error_failed'
/usr/bin/ld: src/adv_monitor.o: in function `app_reply_msg':
/github/workspace/src/adv_monitor.c:175: undefined reference to `btd_get_dbus_connection'
/usr/bin/ld: src/adv_monitor.o: in function `unregister_monitor':
/github/workspace/src/adv_monitor.c:908: undefined reference to `btd_error_invalid_args'
/usr/bin/ld: /github/workspace/src/adv_monitor.c:914: undefined reference to `btd_error_does_not_exist'
/usr/bin/ld: src/adv_monitor.o: in function `register_monitor':
/github/workspace/src/adv_monitor.c:869: undefined reference to `btd_error_invalid_args'
/usr/bin/ld: /github/workspace/src/adv_monitor.c:874: undefined reference to `btd_error_already_exists'
/usr/bin/ld: /github/workspace/src/adv_monitor.c:881: undefined reference to `btd_error_failed'
/usr/bin/ld: src/adv_monitor.o: in function `manager_destroy':
/github/workspace/src/adv_monitor.c:1093: undefined reference to `adapter_get_path'
/usr/bin/ld: /github/workspace/src/adv_monitor.c:1093: undefined reference to `btd_get_dbus_connection'
/usr/bin/ld: src/adv_monitor.o: in function `app_reply_msg':
/github/workspace/src/adv_monitor.c:175: undefined reference to `btd_get_dbus_connection'
/usr/bin/ld: src/adv_monitor.o: in function `manager_new':
/github/workspace/src/adv_monitor.c:1067: undefined reference to `btd_adapter_get_index'
/usr/bin/ld: src/adv_monitor.o: in function `btd_adv_monitor_manager_create':
/github/workspace/src/adv_monitor.c:1147: undefined reference to `adapter_get_path'
/usr/bin/ld: /github/workspace/src/adv_monitor.c:1147: undefined reference to `btd_get_dbus_connection'
/usr/bin/ld: src/device.o: in function `store_device_info_cb':
/github/workspace/src/device.c:403: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:404: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: /github/workspace/src/device.c:479: undefined reference to `create_file'
/usr/bin/ld: src/device.o: in function `browse_request_free':
/github/workspace/src/device.c:562: undefined reference to `sdp_record_free'
/usr/bin/ld: /github/workspace/src/device.c:562: undefined reference to `sdp_list_free'
/usr/bin/ld: src/device.o: in function `service_prio_cmp':
/github/workspace/src/device.c:1929: undefined reference to `btd_service_get_profile'
/usr/bin/ld: /github/workspace/src/device.c:1930: undefined reference to `btd_service_get_profile'
/usr/bin/ld: src/device.o: in function `find_service_with_uuid':
/github/workspace/src/device.c:343: undefined reference to `btd_service_get_profile'
/usr/bin/ld: /github/workspace/src/device.c:345: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `disconnect_gatt_service':
/github/workspace/src/device.c:5075: undefined reference to `btd_service_get_profile'
/usr/bin/ld: src/device.o: in function `device_disappeared':
/github/workspace/src/device.c:4281: undefined reference to `btd_adapter_remove_device'
/usr/bin/ld: src/device.o: in function `store_service':
/github/workspace/src/device.c:2447: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: src/device.o: in function `store_chrc':
/github/workspace/src/device.c:2365: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: /github/workspace/src/device.c:2368: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:2369: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/device.o: in function `store_desc':
/github/workspace/src/device.c:2337: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: /github/workspace/src/device.c:2339: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:2340: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/device.o: in function `store_incl':
/github/workspace/src/device.c:2422: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: src/device.o: in function `dev_property_get_adapter':
/github/workspace/src/device.c:1224: undefined reference to `adapter_get_path'
/usr/bin/ld: src/device.o: in function `dev_property_get_alias':
/github/workspace/src/device.c:812: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `dev_property_get_address':
/github/workspace/src/device.c:758: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `append_service_data':
/github/workspace/src/device.c:1278: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: /github/workspace/src/device.c:1280: undefined reference to `dict_append_array'
/usr/bin/ld: src/device.o: in function `bonding_request_free':
/github/workspace/src/device.c:2741: undefined reference to `agent_cancel'
/usr/bin/ld: /github/workspace/src/device.c:2742: undefined reference to `agent_unref'
/usr/bin/ld: src/device.o: in function `select_conn_bearer':
/github/workspace/src/device.c:2098: undefined reference to `btd_adapter_get_bredr'
/usr/bin/ld: src/device.o: in function `new_auth':
/github/workspace/src/device.c:6350: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:6359: undefined reference to `agent_ref'
/usr/bin/ld: /github/workspace/src/device.c:6361: undefined reference to `agent_get'
/usr/bin/ld: src/device.o: in function `store_services':
/github/workspace/src/device.c:2251: undefined reference to `sdp_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:2252: undefined reference to `bt_uuid2string'
/usr/bin/ld: /github/workspace/src/device.c:2256: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:2258: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: /github/workspace/src/device.c:2270: undefined reference to `bt_string2uuid'
/usr/bin/ld: /github/workspace/src/device.c:2271: undefined reference to `sdp_uuid128_to_uuid'
/usr/bin/ld: /github/workspace/src/device.c:2297: undefined reference to `create_file'
/usr/bin/ld: src/device.o: in function `device_set_auto_connect':
/github/workspace/src/device.c:1674: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:1691: undefined reference to `adapter_auto_connect_add'
/usr/bin/ld: /github/workspace/src/device.c:1698: undefined reference to `adapter_connect_list_add'
/usr/bin/ld: /github/workspace/src/device.c:1685: undefined reference to `adapter_connect_list_remove'
/usr/bin/ld: /github/workspace/src/device.c:1686: undefined reference to `adapter_auto_connect_remove'
/usr/bin/ld: src/device.o: in function `pincode_cb':
/github/workspace/src/device.c:6286: undefined reference to `btd_adapter_pincode_reply'
/usr/bin/ld: /github/workspace/src/device.c:6289: undefined reference to `agent_unref'
/usr/bin/ld: src/device.o: in function `passkey_cb':
/github/workspace/src/device.c:6323: undefined reference to `btd_adapter_passkey_reply'
/usr/bin/ld: /github/workspace/src/device.c:6326: undefined reference to `agent_unref'
/usr/bin/ld: src/device.o: in function `confirm_cb':
/github/workspace/src/device.c:6302: undefined reference to `btd_adapter_confirm_reply'
/usr/bin/ld: /github/workspace/src/device.c:6306: undefined reference to `agent_unref'
/usr/bin/ld: src/device.o: in function `read_device_records':
/github/workspace/src/device.c:6679: undefined reference to `btd_adapter_get_address'
/usr/bin/ld: /github/workspace/src/device.c:6679: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:6680: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:6694: undefined reference to `record_from_string'
/usr/bin/ld: /github/workspace/src/device.c:6695: undefined reference to `sdp_list_append'
/usr/bin/ld: src/device.o: in function `disconnect_all':
/github/workspace/src/device.c:1483: undefined reference to `btd_adapter_disconnect_device'
/usr/bin/ld: /github/workspace/src/device.c:1487: undefined reference to `btd_adapter_disconnect_device'
/usr/bin/ld: src/device.o: in function `gatt_server_cleanup':
/github/workspace/src/device.c:611: undefined reference to `btd_adapter_get_database'
/usr/bin/ld: /github/workspace/src/device.c:611: undefined reference to `btd_gatt_database_att_disconnected'
/usr/bin/ld: src/device.o: in function `add_service_data':
/github/workspace/src/device.c:1866: undefined reference to `bt_string_to_uuid'
/usr/bin/ld: src/device.o: in function `find_connectable_service':
/github/workspace/src/device.c:1915: undefined reference to `btd_service_get_profile'
/usr/bin/ld: src/device.o: in function `disconnect_profile':
/github/workspace/src/device.c:2201: undefined reference to `bt_name2string'
/usr/bin/ld: /github/workspace/src/device.c:2212: undefined reference to `btd_error_in_progress'
/usr/bin/ld: /github/workspace/src/device.c:2209: undefined reference to `btd_error_invalid_args'
/usr/bin/ld: /github/workspace/src/device.c:2214: undefined reference to `btd_service_get_state'
/usr/bin/ld: /github/workspace/src/device.c:2219: undefined reference to `btd_service_disconnect'
/usr/bin/ld: /github/workspace/src/device.c:2231: undefined reference to `btd_error_failed'
/usr/bin/ld: /github/workspace/src/device.c:2227: undefined reference to `btd_error_not_supported'
/usr/bin/ld: src/device.o: in function `create_pending_list':
/github/workspace/src/device.c:1951: undefined reference to `btd_service_get_profile'
/usr/bin/ld: /github/workspace/src/device.c:1959: undefined reference to `btd_service_get_state'
/usr/bin/ld: src/device.o: in function `connect_next':
/github/workspace/src/device.c:1728: undefined reference to `btd_service_connect'
/usr/bin/ld: src/device.o: in function `add_primary':
/github/workspace/src/device.c:3474: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: src/device.o: in function `device_auth_req_free':
/github/workspace/src/device.c:6018: undefined reference to `agent_unref'
/usr/bin/ld: src/device.o: in function `record_has_uuid':
/github/workspace/src/device.c:4581: undefined reference to `bt_uuid2string'
/usr/bin/ld: src/device.o: in function `load_services':
/github/workspace/src/device.c:3179: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `convert_info':
/github/workspace/src/device.c:3218: undefined reference to `btd_adapter_get_address'
/usr/bin/ld: /github/workspace/src/device.c:3218: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:3219: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `load_att_info':
/github/workspace/src/device.c:3377: undefined reference to `sdp_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:3378: undefined reference to `bt_uuid2string'
/usr/bin/ld: /github/workspace/src/device.c:3440: undefined reference to `bt_uuid2string'
/usr/bin/ld: src/device.o: in function `device_match_profile':
/github/workspace/src/device.c:3819: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `probe_service':
/github/workspace/src/device.c:4623: undefined reference to `service_create'
/usr/bin/ld: /github/workspace/src/device.c:4625: undefined reference to `service_probe'
/usr/bin/ld: src/device.o: in function `find_service_with_profile':
/github/workspace/src/device.c:315: undefined reference to `btd_service_get_profile'
/usr/bin/ld: src/device.o: in function `probe_service':
/github/workspace/src/device.c:4626: undefined reference to `btd_service_unref'
/usr/bin/ld: src/device.o: in function `device_store_cached_name':
/github/workspace/src/device.c:533: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:534: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: /github/workspace/src/device.c:536: undefined reference to `create_file'
/usr/bin/ld: src/device.o: in function `gatt_cache_is_enabled':
/github/workspace/src/device.c:569: undefined reference to `main_opts'
/usr/bin/ld: src/device.o: in function `store_gatt_db':
/github/workspace/src/device.c:2480: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:2482: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: /github/workspace/src/device.c:2485: undefined reference to `create_file'
/usr/bin/ld: src/device.o: in function `gatt_service_removed':
/github/workspace/src/device.c:4009: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: /github/workspace/src/device.c:4034: undefined reference to `btd_gatt_client_service_removed'
/usr/bin/ld: src/device.o: in function `device_remove_gatt_service':
/github/workspace/src/device.c:3899: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: /github/workspace/src/device.c:3908: undefined reference to `service_remove'
/usr/bin/ld: src/device.o: in function `gatt_cache_is_enabled':
/github/workspace/src/device.c:569: undefined reference to `main_opts'
/usr/bin/ld: src/device.o: in function `attio_cleanup':
/github/workspace/src/device.c:642: undefined reference to `g_attrib_cancel_all'
/usr/bin/ld: src/device.o: in function `browse_request_cancel':
/github/workspace/src/device.c:654: undefined reference to `btd_adapter_get_address'
/usr/bin/ld: /github/workspace/src/device.c:654: undefined reference to `bt_cancel_discovery'
/usr/bin/ld: src/device.o: in function `device_free':
/github/workspace/src/device.c:677: undefined reference to `btd_gatt_client_destroy'
/usr/bin/ld: /github/workspace/src/device.c:694: undefined reference to `sdp_record_free'
/usr/bin/ld: /github/workspace/src/device.c:694: undefined reference to `sdp_list_free'
/usr/bin/ld: /github/workspace/src/device.c:716: undefined reference to `agent_unref'
/usr/bin/ld: src/device.o: in function `att_disconnected_cb':
/github/workspace/src/device.c:5097: undefined reference to `btd_gatt_client_disconnected'
/usr/bin/ld: /github/workspace/src/device.c:5110: undefined reference to `adapter_connect_list_add'
/usr/bin/ld: src/device.o: in function `gatt_cache_is_enabled':
/github/workspace/src/device.c:569: undefined reference to `main_opts'
/usr/bin/ld: src/device.o: in function `load_service':
/github/workspace/src/device.c:3651: undefined reference to `bt_string_to_uuid'
/usr/bin/ld: src/device.o: in function `load_gatt_db_impl':
/github/workspace/src/device.c:3733: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: src/device.o: in function `load_chrc':
/github/workspace/src/device.c:3572: undefined reference to `bt_string_to_uuid'
/usr/bin/ld: src/device.o: in function `load_desc':
/github/workspace/src/device.c:3522: undefined reference to `bt_string_to_uuid'
/usr/bin/ld: /github/workspace/src/device.c:3523: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:3526: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/device.o: in function `load_chrc':
/github/workspace/src/device.c:3572: undefined reference to `bt_string_to_uuid'
/usr/bin/ld: /github/workspace/src/device.c:3572: undefined reference to `bt_string_to_uuid'
/usr/bin/ld: src/device.o: in function `device_add_eir_uuids':
/github/workspace/src/device.c:1825: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `btd_device_connect_services':
/github/workspace/src/device.c:1977: undefined reference to `btd_adapter_get_powered'
/usr/bin/ld: src/device.o: in function `bonding_request_cancel':
/github/workspace/src/device.c:1587: undefined reference to `adapter_cancel_bonding'
/usr/bin/ld: src/device.o: in function `device_request_disconnect':
/github/workspace/src/device.c:1610: undefined reference to `btd_error_failed'
/usr/bin/ld: src/device.o: in function `btd_device_get_storage_path':
/github/workspace/src/device.c:4169: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:4176: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: /github/workspace/src/device.c:4172: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: src/device.o: in function `device_browse_sdp':
/github/workspace/src/device.c:5614: undefined reference to `sdp_uuid16_create'
/usr/bin/ld: src/device.o: in function `get_sdp_flags':
/github/workspace/src/device.c:5592: undefined reference to `btd_adapter_ssp_enabled'
/usr/bin/ld: src/device.o: in function `device_browse_sdp':
/github/workspace/src/device.c:5618: undefined reference to `btd_adapter_get_address'
/usr/bin/ld: /github/workspace/src/device.c:5618: undefined reference to `bt_search'
/usr/bin/ld: src/device.o: in function `device_address_cmp':
/github/workspace/src/device.c:4516: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `find_service_with_profile':
/github/workspace/src/device.c:315: undefined reference to `btd_service_get_profile'
/usr/bin/ld: src/device.o: in function `device_probe_profiles':
/github/workspace/src/device.c:4694: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:4703: undefined reference to `btd_profile_foreach'
/usr/bin/ld: src/device.o: in function `device_add_uuids':
/github/workspace/src/device.c:3795: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `device_unblock':
/github/workspace/src/device.c:1548: undefined reference to `btd_adapter_unblock_address'
/usr/bin/ld: /github/workspace/src/device.c:1552: undefined reference to `btd_adapter_unblock_address'
/usr/bin/ld: src/device.o: in function `device_connect_le':
/github/workspace/src/device.c:5455: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:5472: undefined reference to `btd_adapter_get_address_type'
/usr/bin/ld: /github/workspace/src/device.c:5468: undefined reference to `btd_adapter_get_address'
/usr/bin/ld: /github/workspace/src/device.c:5468: undefined reference to `bt_io_connect'
/usr/bin/ld: /github/workspace/src/device.c:5481: undefined reference to `btd_error_failed'
/usr/bin/ld: src/device.o: in function `bonding_request_cancel':
/github/workspace/src/device.c:1587: undefined reference to `adapter_cancel_bonding'
/usr/bin/ld: src/device.o: in function `device_attach_att':
/github/workspace/src/device.c:5287: undefined reference to `bt_io_get'
/usr/bin/ld: /github/workspace/src/device.c:5299: undefined reference to `main_opts'
/usr/bin/ld: /github/workspace/src/device.c:5327: undefined reference to `main_opts'
/usr/bin/ld: /github/workspace/src/device.c:5328: undefined reference to `g_attrib_new'
/usr/bin/ld: /github/workspace/src/device.c:5337: undefined reference to `g_attrib_get_att'
/usr/bin/ld: /github/workspace/src/device.c:5353: undefined reference to `btd_adapter_get_database'
/usr/bin/ld: /github/workspace/src/device.c:5356: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `device_accept_gatt_profiles':
/github/workspace/src/device.c:3887: undefined reference to `service_accept'
/usr/bin/ld: src/device.o: in function `gatt_server_init':
/github/workspace/src/device.c:5226: undefined reference to `btd_gatt_database_get_db'
/usr/bin/ld: /github/workspace/src/device.c:5245: undefined reference to `btd_gatt_database_server_connected'
/usr/bin/ld: src/device.o: in function `device_attach_att':
/github/workspace/src/device.c:5370: undefined reference to `adapter_connect_list_remove'
/usr/bin/ld: /github/workspace/src/device.c:5318: undefined reference to `bt_io_set'
/usr/bin/ld: /github/workspace/src/device.c:5359: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: src/device.o: in function `gatt_client_init':
/github/workspace/src/device.c:5220: undefined reference to `btd_gatt_client_connected'
/usr/bin/ld: src/device.o: in function `device_update_last_seen':
/github/workspace/src/device.c:4300: undefined reference to `main_opts'
/usr/bin/ld: src/device.o: in function `device_add_connection':
/github/workspace/src/device.c:2977: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `device_remove_connection':
/github/workspace/src/device.c:3031: undefined reference to `btd_error_failed'
/usr/bin/ld: /github/workspace/src/device.c:3050: undefined reference to `btd_adapter_remove_bonding'
/usr/bin/ld: src/device.o: in function `btd_device_set_temporary':
/github/workspace/src/device.c:5699: undefined reference to `adapter_connect_list_remove'
/usr/bin/ld: /github/workspace/src/device.c:5700: undefined reference to `main_opts'
/usr/bin/ld: /github/workspace/src/device.c:5707: undefined reference to `adapter_whitelist_add'
/usr/bin/ld: /github/workspace/src/device.c:5698: undefined reference to `adapter_whitelist_remove'
/usr/bin/ld: src/device.o: in function `device_block':
/github/workspace/src/device.c:1509: undefined reference to `service_remove'
/usr/bin/ld: /github/workspace/src/device.c:1514: undefined reference to `btd_adapter_block_address'
/usr/bin/ld: /github/workspace/src/device.c:1518: undefined reference to `btd_adapter_block_address'
/usr/bin/ld: src/device.o: in function `pair_device':
/github/workspace/src/device.c:2786: undefined reference to `btd_error_already_exists'
/usr/bin/ld: /github/workspace/src/device.c:2790: undefined reference to `agent_get'
/usr/bin/ld: /github/workspace/src/device.c:2792: undefined reference to `agent_get_io_capability'
/usr/bin/ld: src/device.o: in function `bonding_request_new':
/github/workspace/src/device.c:2649: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:2657: undefined reference to `btd_adapter_pin_cb_iter_new'
/usr/bin/ld: /github/workspace/src/device.c:2665: undefined reference to `agent_ref'
/usr/bin/ld: src/device.o: in function `pair_device':
/github/workspace/src/device.c:2799: undefined reference to `agent_unref'
/usr/bin/ld: /github/workspace/src/device.c:2814: undefined reference to `btd_le_connect_before_pairing'
/usr/bin/ld: /github/workspace/src/device.c:2817: undefined reference to `adapter_create_bonding'
/usr/bin/ld: /github/workspace/src/device.c:2827: undefined reference to `btd_error_failed'
/usr/bin/ld: /github/workspace/src/device.c:2774: undefined reference to `btd_error_in_progress'
/usr/bin/ld: /github/workspace/src/device.c:2771: undefined reference to `btd_error_invalid_args'
/usr/bin/ld: /github/workspace/src/device.c:2821: undefined reference to `adapter_create_bonding'
/usr/bin/ld: src/device.o: in function `find_service_with_state':
/github/workspace/src/device.c:330: undefined reference to `btd_service_get_state'
/usr/bin/ld: src/device.o: in function `browse_request_complete':
/github/workspace/src/device.c:2544: undefined reference to `btd_error_failed'
/usr/bin/ld: /github/workspace/src/device.c:2521: undefined reference to `btd_error_failed'
/usr/bin/ld: src/device.o: in function `device_browse_gatt':
/github/workspace/src/device.c:5563: undefined reference to `btd_adapter_get_address_type'
/usr/bin/ld: /github/workspace/src/device.c:5558: undefined reference to `btd_adapter_get_address'
/usr/bin/ld: /github/workspace/src/device.c:5558: undefined reference to `bt_io_connect'
/usr/bin/ld: src/device.o: in function `connect_profiles':
/github/workspace/src/device.c:2008: undefined reference to `btd_adapter_get_powered'
/usr/bin/ld: src/device.o: in function `find_service_with_state':
/github/workspace/src/device.c:330: undefined reference to `btd_service_get_state'
/usr/bin/ld: src/device.o: in function `connect_profile':
/github/workspace/src/device.c:2163: undefined reference to `bt_name2string'
/usr/bin/ld: /github/workspace/src/device.c:2161: undefined reference to `btd_error_invalid_args'
/usr/bin/ld: src/device.o: in function `att_connect_cb':
/github/workspace/src/device.c:5388: undefined reference to `bt_io_error_quark'
/usr/bin/ld: /github/workspace/src/device.c:5421: undefined reference to `btd_error_failed'
/usr/bin/ld: src/device.o: in function `bonding_request_cancel':
/github/workspace/src/device.c:1587: undefined reference to `adapter_cancel_bonding'
/usr/bin/ld: src/device.o: in function `att_connect_cb':
/github/workspace/src/device.c:5432: undefined reference to `btd_error_failed'
/usr/bin/ld: /github/workspace/src/device.c:5393: undefined reference to `adapter_connect_list_add'
/usr/bin/ld: /github/workspace/src/device.c:5413: undefined reference to `agent_get_io_capability'
/usr/bin/ld: /github/workspace/src/device.c:5417: undefined reference to `adapter_create_bonding'
/usr/bin/ld: src/device.o: in function `device_remove_stored':
/github/workspace/src/device.c:4429: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:4431: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: /github/workspace/src/device.c:4436: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: /github/workspace/src/device.c:4446: undefined reference to `create_file'
/usr/bin/ld: src/device.o: in function `gatt_client_ready_cb':
/github/workspace/src/device.c:5160: undefined reference to `btd_gatt_client_ready'
/usr/bin/ld: src/device.o: in function `device_add_gatt_services':
/github/workspace/src/device.c:3872: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `service_state_changed':
/github/workspace/src/device.c:6870: undefined reference to `btd_service_get_profile'
/usr/bin/ld: /github/workspace/src/device.c:6871: undefined reference to `btd_service_get_device'
/usr/bin/ld: /github/workspace/src/device.c:6872: undefined reference to `btd_service_get_error'
/usr/bin/ld: src/device.o: in function `find_service_with_profile':
/github/workspace/src/device.c:315: undefined reference to `btd_service_get_profile'
/usr/bin/ld: src/device.o: in function `device_profile_connected':
/github/workspace/src/device.c:1771: undefined reference to `btd_service_get_profile'
/usr/bin/ld: src/device.o: in function `device_profile_disconnected':
/github/workspace/src/device.c:2177: undefined reference to `btd_error_failed'
/usr/bin/ld: src/device.o: in function `device_profile_connected':
/github/workspace/src/device.c:1804: undefined reference to `btd_error_failed'
/usr/bin/ld: src/device.o: in function `find_service_with_state':
/github/workspace/src/device.c:330: undefined reference to `btd_service_get_state'
/usr/bin/ld: src/device.o: in function `device_store_svc_chng_ccc':
/github/workspace/src/device.c:5777: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:5778: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: /github/workspace/src/device.c:5806: undefined reference to `create_file'
/usr/bin/ld: src/device.o: in function `device_load_svc_chng_ccc':
/github/workspace/src/device.c:5822: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:5823: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: src/device.o: in function `device_wait_for_svc_complete':
/github/workspace/src/device.c:6145: undefined reference to `main_opts'
/usr/bin/ld: src/device.o: in function `device_bonding_attempt_retry':
/github/workspace/src/device.c:6240: undefined reference to `btd_adapter_pin_cb_iter_end'
/usr/bin/ld: src/device.o: in function `device_request_pincode':
/github/workspace/src/device.c:6388: undefined reference to `agent_request_pincode'
/usr/bin/ld: src/device.o: in function `device_request_passkey':
/github/workspace/src/device.c:6407: undefined reference to `agent_request_passkey'
/usr/bin/ld: src/device.o: in function `device_confirm_passkey':
/github/workspace/src/device.c:6449: undefined reference to `btd_adapter_confirm_reply'
/usr/bin/ld: /github/workspace/src/device.c:6458: undefined reference to `agent_request_confirmation'
/usr/bin/ld: /github/workspace/src/device.c:6425: undefined reference to `main_opts'
/usr/bin/ld: /github/workspace/src/device.c:6455: undefined reference to `agent_request_authorization'
/usr/bin/ld: /github/workspace/src/device.c:6426: undefined reference to `btd_adapter_confirm_reply'
/usr/bin/ld: src/device.o: in function `device_notify_passkey':
/github/workspace/src/device.c:6492: undefined reference to `agent_display_passkey'
/usr/bin/ld: src/device.o: in function `device_notify_pincode':
/github/workspace/src/device.c:6513: undefined reference to `agent_display_pincode'
/usr/bin/ld: src/device.o: in function `device_cancel_authentication':
/github/workspace/src/device.c:6572: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:6576: undefined reference to `agent_cancel'
/usr/bin/ld: src/device.o: in function `device_cancel_bonding':
/github/workspace/src/device.c:2878: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `bonding_request_cancel':
/github/workspace/src/device.c:1587: undefined reference to `adapter_cancel_bonding'
/usr/bin/ld: src/device.o: in function `create_bond_req_exit':
/github/workspace/src/device.c:2714: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `device_bonding_complete':
/github/workspace/src/device.c:6043: undefined reference to `agent_cancel'
/usr/bin/ld: /github/workspace/src/device.c:6101: undefined reference to `main_opts'
/usr/bin/ld: src/device.o: in function `device_bonding_retry':
/github/workspace/src/device.c:6212: undefined reference to `agent_get_io_capability'
/usr/bin/ld: /github/workspace/src/device.c:6216: undefined reference to `adapter_bonding_attempt'
/usr/bin/ld: src/device.o: in function `btd_device_get_primary':
/github/workspace/src/device.c:6594: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `btd_device_add_uuid':
/github/workspace/src/device.c:6652: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `add_gatt_service':
/github/workspace/src/device.c:3836: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: /github/workspace/src/device.c:3856: undefined reference to `btd_service_get_profile'
/usr/bin/ld: /github/workspace/src/device.c:3865: undefined reference to `service_accept'
/usr/bin/ld: src/device.o: in function `gatt_service_added':
/github/workspace/src/device.c:3945: undefined reference to `btd_gatt_client_service_added'
/usr/bin/ld: src/device.o: in function `device_new':
/github/workspace/src/device.c:4044: undefined reference to `adapter_get_path'
/usr/bin/ld: /github/workspace/src/device.c:4073: undefined reference to `str2ba'
/usr/bin/ld: /github/workspace/src/device.c:4075: undefined reference to `btd_gatt_client_new'
/usr/bin/ld: /github/workspace/src/device.c:4100: undefined reference to `main_opts'
/usr/bin/ld: src/device.o: in function `device_create':
/github/workspace/src/device.c:4134: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:4148: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: src/device.o: in function `device_remove':
/github/workspace/src/device.c:4481: undefined reference to `service_remove'
/usr/bin/ld: src/device.o: in function `device_set_appearance':
/github/workspace/src/device.c:6786: undefined reference to `gap_appearance_to_icon'
/usr/bin/ld: src/device.o: in function `btd_device_set_pnpid':
/github/workspace/src/device.c:6815: undefined reference to `bt_modalias'
/usr/bin/ld: src/device.o: in function `update_bredr_services':
/github/workspace/src/device.c:4827: undefined reference to `btd_adapter_get_address'
/usr/bin/ld: /github/workspace/src/device.c:4827: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:4828: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:4852: undefined reference to `bt_uuid2string'
/usr/bin/ld: /github/workspace/src/device.c:4857: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `update_record':
/github/workspace/src/device.c:4799: undefined reference to `sdp_copy_record'
/usr/bin/ld: /github/workspace/src/device.c:4799: undefined reference to `sdp_list_append'
/usr/bin/ld: /github/workspace/src/device.c:4802: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `store_sdp_record':
/github/workspace/src/device.c:4718: undefined reference to `sdp_gen_record_pdu'
/usr/bin/ld: src/device.o: in function `store_primaries_from_sdp_record':
/github/workspace/src/device.c:4743: undefined reference to `sdp_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:4744: undefined reference to `bt_uuid2string'
/usr/bin/ld: /github/workspace/src/device.c:4746: undefined reference to `sdp_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:4747: undefined reference to `bt_uuid2string'
/usr/bin/ld: /github/workspace/src/device.c:4752: undefined reference to `gatt_parse_record'
/usr/bin/ld: src/device.o: in function `update_bredr_services':
/github/workspace/src/device.c:4861: undefined reference to `sdp_data_get'
/usr/bin/ld: /github/workspace/src/device.c:4864: undefined reference to `sdp_data_get'
/usr/bin/ld: /github/workspace/src/device.c:4867: undefined reference to `sdp_data_get'
/usr/bin/ld: /github/workspace/src/device.c:4870: undefined reference to `sdp_data_get'
/usr/bin/ld: src/device.o: in function `update_record':
/github/workspace/src/device.c:4804: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `update_bredr_services':
/github/workspace/src/device.c:4894: undefined reference to `create_file'
/usr/bin/ld: /github/workspace/src/device.c:4905: undefined reference to `create_file'
/usr/bin/ld: src/device.o: in function `search_cb':
/github/workspace/src/device.c:4988: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:4999: undefined reference to `sdp_record_free'
/usr/bin/ld: /github/workspace/src/device.c:4999: undefined reference to `sdp_list_free'
/usr/bin/ld: src/device.o: in function `device_services_from_record':
/github/workspace/src/device.c:4948: undefined reference to `sdp_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:4949: undefined reference to `bt_uuid2string'
/usr/bin/ld: /github/workspace/src/device.c:4965: undefined reference to `gatt_parse_record'
/usr/bin/ld: /github/workspace/src/device.c:4971: undefined reference to `sdp_uuid2strn'
/usr/bin/ld: src/device.o: in function `search_cb':
/github/workspace/src/device.c:4988: undefined reference to `ba2str'
/usr/bin/ld: /github/workspace/src/device.c:4988: undefined reference to `ba2str'
/usr/bin/ld: src/device.o: in function `browse_cb':
/github/workspace/src/device.c:5052: undefined reference to `sdp_uuid16_create'
/usr/bin/ld: /github/workspace/src/device.c:5053: undefined reference to `btd_adapter_get_address'
/usr/bin/ld: /github/workspace/src/device.c:5053: undefined reference to `bt_search_service'
/usr/bin/ld: src/device.o: in function `btd_device_set_record':
/github/workspace/src/device.c:6721: undefined reference to `record_from_string'
/usr/bin/ld: /github/workspace/src/device.c:6722: undefined reference to `sdp_list_append'
/usr/bin/ld: /github/workspace/src/device.c:6724: undefined reference to `sdp_list_free'
/usr/bin/ld: src/device.o: in function `device_create_from_storage':
/github/workspace/src/device.c:4119: undefined reference to `btd_adapter_get_storage_dir'
/usr/bin/ld: src/device.o: in function `btd_device_get_service':
/github/workspace/src/device.c:6891: undefined reference to `btd_service_get_profile'
/usr/bin/ld: src/device.o: in function `btd_device_init':
/github/workspace/src/device.c:6902: undefined reference to `btd_get_dbus_connection'
/usr/bin/ld: /github/workspace/src/device.c:6903: undefined reference to `btd_service_add_state_cb'
/usr/bin/ld: src/device.o: in function `dev_disconn_service':
/github/workspace/src/device.c:1592: undefined reference to `btd_service_disconnect'
/usr/bin/ld: src/device.o: in function `prim_uuid_cmp':
/github/workspace/src/device.c:3966: undefined reference to `bt_uuid_strcmp'
/usr/bin/ld: src/device.o: in function `disconnect_gatt_service':
/github/workspace/src/device.c:5081: undefined reference to `btd_service_disconnect'
/usr/bin/ld: src/device.o: in function `get_icon':
/github/workspace/src/device.c:930: undefined reference to `gap_appearance_to_icon'
/usr/bin/ld: /github/workspace/src/device.c:928: undefined reference to `class_to_icon'
/usr/bin/ld: src/device.o: in function `attio_cleanup':
/github/workspace/src/device.c:643: undefined reference to `g_attrib_unref'
/usr/bin/ld: src/device.o: in function `device_remove_profile':
/github/workspace/src/device.c:4686: undefined reference to `service_remove'
/usr/bin/ld: src/device.o: in function `device_set_wake_allowed':
/github/workspace/src/device.c:1399: undefined reference to `adapter_set_device_wakeable'
/usr/bin/ld: src/device.o: in function `device_remove_connection':
/github/workspace/src/device.c:3071: undefined reference to `btd_adapter_remove_device'
/usr/bin/ld: src/device.o: in function `dev_connect':
/github/workspace/src/device.c:2141: undefined reference to `btd_error_failed'
/usr/bin/ld: src/device.o: in function `connect_profiles':
/github/workspace/src/device.c:2006: undefined reference to `btd_error_in_progress'
/usr/bin/ld: /github/workspace/src/device.c:2050: undefined reference to `btd_error_failed'
/usr/bin/ld: /github/workspace/src/device.c:2009: undefined reference to `btd_error_not_ready'
/usr/bin/ld: /github/workspace/src/device.c:2025: undefined reference to `btd_error_not_available'
/usr/bin/ld: src/device.o: in function `device_remove_bonding':
/github/workspace/src/device.c:4405: undefined reference to `btd_adapter_remove_bonding'
/usr/bin/ld: src/device.o: in function `device_set_unpaired':
/github/workspace/src/device.c:6007: undefined reference to `btd_adapter_remove_device'
/usr/bin/ld: src/device.o: in function `cancel_pairing':
/github/workspace/src/device.c:2900: undefined reference to `btd_error_does_not_exist'
/usr/bin/ld: src/device.o: in function `btd_device_get_record':
/github/workspace/src/device.c:6748: undefined reference to `find_record_in_list'
/usr/bin/ld: src/device.o: in function `btd_device_cleanup':
/github/workspace/src/device.c:6909: undefined reference to `btd_service_remove_state_cb'
/usr/bin/ld: src/.libs/libshared-glib.a(ad.o): in function `serialize_uuids':
/github/workspace/src/shared/ad.c:322: undefined reference to `bt_uuid_to_le'
/usr/bin/ld: src/.libs/libshared-glib.a(ad.o): in function `uuid_match':
/github/workspace/src/shared/ad.c:527: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(ad.o): in function `service_uuid_match':
/github/workspace/src/shared/ad.c:713: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(ad.o): in function `uuid_data_match':
/github/workspace/src/shared/ad.c:84: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(ad.o): in function `service_data_match':
/github/workspace/src/shared/ad.c:764: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(ad.o): in function `serialize_service_data':
/github/workspace/src/shared/ad.c:404: undefined reference to `bt_uuid_to_le'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `find_ccc':
/github/workspace/src/shared/gatt-client.c:233: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:235: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `discovery_parse_services':
/github/workspace/src/shared/gatt-client.c:1121: undefined reference to `bt_uuid128_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:1124: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `discover_descs':
/github/workspace/src/shared/gatt-client.c:716: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `read_server_feat':
/github/workspace/src/shared/gatt-client.c:1511: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `read_db_hash':
/github/workspace/src/shared/gatt-client.c:1452: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `discover_incl_cb':
/github/workspace/src/shared/gatt-client.c:567: undefined reference to `bt_uuid128_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:570: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `discover_chrcs_cb':
/github/workspace/src/shared/gatt-client.c:970: undefined reference to `bt_uuid128_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:973: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `notify_chrc_create':
/github/workspace/src/shared/gatt-client.c:299: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:300: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `register_service_changed':
/github/workspace/src/shared/gatt-client.c:1786: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `discover_descs_cb':
/github/workspace/src/shared/gatt-client.c:875: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:878: undefined reference to `bt_uuid128_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:881: undefined reference to `bt_uuid_to_string'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:904: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:891: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `write_client_features':
/github/workspace/src/shared/gatt-client.c:1969: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-client.c:1980: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-client.o): in function `write_server_features':
/github/workspace/src/shared/gatt-client.c:1947: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-server.o): in function `find_by_type_val_cb':
/github/workspace/src/shared/gatt-server.c:757: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-server.o): in function `get_uuid_le':
/github/workspace/src/shared/gatt-server.c:179: undefined reference to `bt_uuid128_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-server.c:175: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-server.o): in function `encode_find_info_rsp':
/github/workspace/src/shared/gatt-server.c:612: undefined reference to `bt_uuid_to_le'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-server.o): in function `read_by_grp_type_cb':
/github/workspace/src/shared/gatt-server.c:306: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-server.c:307: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-server.c:308: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: /github/workspace/src/shared/gatt-server.c:308: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `uuid_to_le':
/github/workspace/src/shared/gatt-db.c:491: undefined reference to `bt_uuid_to_uuid128'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `gen_hash_m':
/github/workspace/src/shared/gatt-db.c:333: undefined reference to `bt_uuid_to_le'
/usr/bin/ld: /github/workspace/src/shared/gatt-db.c:321: undefined reference to `bt_uuid_to_le'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `le_to_uuid':
/github/workspace/src/shared/gatt-db.c:514: undefined reference to `bt_uuid128_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-db.c:501: undefined reference to `bt_uuid16_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-db.c:506: undefined reference to `bt_uuid32_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `gatt_db_service_foreach':
/github/workspace/src/shared/gatt-db.c:1452: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `gatt_db_service_foreach_desc':
/github/workspace/src/shared/gatt-db.c:1478: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: /github/workspace/src/shared/gatt-db.c:1497: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: /github/workspace/src/shared/gatt-db.c:1498: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `gatt_db_attribute_get_service_uuid':
/github/workspace/src/shared/gatt-db.c:1630: undefined reference to `bt_uuid128_create'
/usr/bin/ld: /github/workspace/src/shared/gatt-db.c:1621: undefined reference to `bt_uuid16_create'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `gatt_db_insert_service':
/github/workspace/src/shared/gatt-db.c:722: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: /github/workspace/src/shared/gatt-db.c:723: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `foreach_in_range':
/github/workspace/src/shared/gatt-db.c:1382: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o): in function `foreach_service_in_range':
/github/workspace/src/shared/gatt-db.c:1335: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: /github/workspace/src/shared/gatt-db.c:1339: undefined reference to `bt_uuid_cmp'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-db.o):/github/workspace/src/shared/gatt-db.c:1572: more undefined references to `bt_uuid_cmp' follow
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-helpers.o): in function `bt_gatt_iter_next_service':
/github/workspace/src/shared/gatt-helpers.c:349: undefined reference to `bt_uuid_to_uuid128'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-helpers.o): in function `discover_services':
/github/workspace/src/shared/gatt-helpers.c:856: undefined reference to `bt_uuid_to_le'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-helpers.o): in function `find_by_type_val_cb':
/github/workspace/src/shared/gatt-helpers.c:786: undefined reference to `bt_uuid_to_le'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-helpers.o): in function `read_by_type_cb':
/github/workspace/src/shared/gatt-helpers.c:1340: undefined reference to `bt_uuid_to_le'
/usr/bin/ld: src/.libs/libshared-glib.a(gatt-helpers.o): in function `bt_gatt_read_by_type':
/github/workspace/src/shared/gatt-helpers.c:1383: undefined reference to `bt_uuid_to_le'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:6060: unit/test-adv-monitor] Error 1
make: *** [Makefile:4056: all] Error 2



---
Regards,
Linux Bluetooth

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning
@ 2020-09-17  7:10 Miao-chen Chou
  0 siblings, 0 replies; 11+ messages in thread
From: Miao-chen Chou @ 2020-09-17  7:10 UTC (permalink / raw)
  To: Bluetooth Kernel Mailing List
  Cc: Marcel Holtmann, Alain Michaud, Howard Chung,
	chromeos-bluetooth-upstreaming, Luiz Augusto von Dentz,
	Manish Mandlik, Manish Mandlik, Abhishek Pandit-Subedi,
	Miao-chen Chou

From: Manish Mandlik <mmandlik@google.com>

This patch implements the RSSI Filter logic for background scanning.

This was unit-tested by running tests in unit/test-adv-monitor.c

Reviewed-by: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>
Reviewed-by: Alain Michaud <alainm@chromium.org>
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
Reviewed-by: Howard Chung <howardchung@google.com>
---

Changes in v4:
- Fix commit message

Changes in v3:
- Fix commit message

 doc/advertisement-monitor-api.txt |   5 +
 src/adapter.c                     |   1 +
 src/adv_monitor.c                 | 286 +++++++++++++++++++++++++++++-
 src/adv_monitor.h                 |   4 +
 4 files changed, 292 insertions(+), 4 deletions(-)

diff --git a/doc/advertisement-monitor-api.txt b/doc/advertisement-monitor-api.txt
index e09b6fd25..92c8ffc38 100644
--- a/doc/advertisement-monitor-api.txt
+++ b/doc/advertisement-monitor-api.txt
@@ -70,6 +70,11 @@ Properties	string Type [read-only]
 			dBm indicates unset. The valid range of a timer is 1 to
 			300 seconds while 0 indicates unset.
 
+			If the peer device advertising interval is greater than the
+			HighRSSIThresholdTimer, the device will never be found. Similarly,
+			if it is greater than LowRSSIThresholdTimer, the device will be
+			considered as lost. Consider configuring these values accordingly.
+
 		array{(uint8, uint8, array{byte})} Patterns [read-only, optional]
 
 			If Type is set to 0x01, this must exist and has at least
diff --git a/src/adapter.c b/src/adapter.c
index b2bd8b3f1..415d6e06b 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1227,6 +1227,7 @@ void btd_adapter_remove_device(struct btd_adapter *adapter,
 	adapter->connect_list = g_slist_remove(adapter->connect_list, dev);
 
 	adapter->devices = g_slist_remove(adapter->devices, dev);
+	btd_adv_monitor_device_remove(adapter->adv_monitor_manager, dev);
 
 	adapter->discovery_found = g_slist_remove(adapter->discovery_found,
 									dev);
diff --git a/src/adv_monitor.c b/src/adv_monitor.c
index 737da1c90..7baa5317f 100644
--- a/src/adv_monitor.c
+++ b/src/adv_monitor.c
@@ -35,6 +35,7 @@
 
 #include "adapter.h"
 #include "dbus-common.h"
+#include "device.h"
 #include "log.h"
 #include "src/error.h"
 #include "src/shared/ad.h"
@@ -44,6 +45,8 @@
 
 #include "adv_monitor.h"
 
+static void monitor_device_free(void *data);
+
 #define ADV_MONITOR_INTERFACE		"org.bluez.AdvertisementMonitor1"
 #define ADV_MONITOR_MGR_INTERFACE	"org.bluez.AdvertisementMonitorManager1"
 
@@ -104,15 +107,36 @@ struct adv_monitor {
 
 	enum monitor_state state;	/* MONITOR_STATE_* */
 
-	int8_t high_rssi;		/* high RSSI threshold */
-	uint16_t high_rssi_timeout;	/* high RSSI threshold timeout */
-	int8_t low_rssi;		/* low RSSI threshold */
-	uint16_t low_rssi_timeout;	/* low RSSI threshold timeout */
+	int8_t high_rssi;		/* High RSSI threshold */
+	uint16_t high_rssi_timeout;	/* High RSSI threshold timeout */
+	int8_t low_rssi;		/* Low RSSI threshold */
+	uint16_t low_rssi_timeout;	/* Low RSSI threshold timeout */
+	struct queue *devices;		/* List of adv_monitor_device objects */
 
 	enum monitor_type type;		/* MONITOR_TYPE_* */
 	struct queue *patterns;
 };
 
+/* Some data like last_seen, timer/timeout values need to be maintained
+ * per device. struct adv_monitor_device maintains such data.
+ */
+struct adv_monitor_device {
+	struct adv_monitor *monitor;
+	struct btd_device *device;
+
+	time_t high_rssi_first_seen;	/* Start time when RSSI climbs above
+					 * the high RSSI threshold
+					 */
+	time_t low_rssi_first_seen;	/* Start time when RSSI drops below
+					 * the low RSSI threshold
+					 */
+	time_t last_seen;		/* Time when last Adv was received */
+	bool device_found;		/* State of the device - lost/found */
+	guint device_lost_timer;	/* Timer to track if the device goes
+					 * offline/out-of-range
+					 */
+};
+
 struct app_match_data {
 	const char *owner;
 	const char *path;
@@ -159,6 +183,9 @@ static void monitor_free(void *data)
 	g_dbus_proxy_unref(monitor->proxy);
 	g_free(monitor->path);
 
+	queue_destroy(monitor->devices, monitor_device_free);
+	monitor->devices = NULL;
+
 	queue_destroy(monitor->patterns, pattern_free);
 
 	free(monitor);
@@ -257,6 +284,7 @@ static struct adv_monitor *monitor_new(struct adv_monitor_app *app,
 	monitor->high_rssi_timeout = ADV_MONITOR_UNSET_TIMER;
 	monitor->low_rssi = ADV_MONITOR_UNSET_RSSI;
 	monitor->low_rssi_timeout = ADV_MONITOR_UNSET_TIMER;
+	monitor->devices = queue_new();
 
 	monitor->type = MONITOR_TYPE_NONE;
 	monitor->patterns = NULL;
@@ -932,3 +960,253 @@ void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager)
 
 	manager_destroy(manager);
 }
+
+/* Matches a device based on btd_device object */
+static bool monitor_device_match(const void *a, const void *b)
+{
+	const struct adv_monitor_device *dev = a;
+	const struct btd_device *device = b;
+
+	if (!dev)
+		return false;
+
+	if (dev->device != device)
+		return false;
+
+	return true;
+}
+
+/* Frees a monitor device object */
+static void monitor_device_free(void *data)
+{
+	struct adv_monitor_device *dev = data;
+
+	if (!dev)
+		return;
+
+	if (dev->device_lost_timer) {
+		g_source_remove(dev->device_lost_timer);
+		dev->device_lost_timer = 0;
+	}
+
+	dev->monitor = NULL;
+	dev->device = NULL;
+
+	g_free(dev);
+}
+
+/* Removes a device from monitor->devices list */
+static void remove_device_from_monitor(void *data, void *user_data)
+{
+	struct adv_monitor *monitor = data;
+	struct btd_device *device = user_data;
+	struct adv_monitor_device *dev = NULL;
+
+	if (!monitor)
+		return;
+
+	dev = queue_remove_if(monitor->devices, monitor_device_match, device);
+	if (dev) {
+		DBG("Device removed from the Adv Monitor at path %s",
+		    monitor->path);
+		monitor_device_free(dev);
+	}
+}
+
+/* Removes a device from every monitor in an app */
+static void remove_device_from_app(void *data, void *user_data)
+{
+	struct adv_monitor_app *app = data;
+	struct btd_device *device = user_data;
+
+	if (!app)
+		return;
+
+	queue_foreach(app->monitors, remove_device_from_monitor, device);
+}
+
+/* Removes a device from every monitor in all apps */
+void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager,
+				   struct btd_device *device)
+{
+	if (!manager || !device)
+		return;
+
+	queue_foreach(manager->apps, remove_device_from_app, device);
+}
+
+/* Creates a device object to track the per-device information */
+static struct adv_monitor_device *monitor_device_create(
+			struct adv_monitor *monitor,
+			struct btd_device *device)
+{
+	struct adv_monitor_device *dev = NULL;
+
+	dev = g_try_malloc0(sizeof(struct adv_monitor_device));
+	if (!dev)
+		return NULL;
+
+	dev->monitor = monitor;
+	dev->device = device;
+
+	queue_push_tail(monitor->devices, dev);
+
+	return dev;
+}
+
+/* Includes found/lost device's object path into the dbus message */
+static void report_device_state_setup(DBusMessageIter *iter, void *user_data)
+{
+	const char *path = device_get_path(user_data);
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
+}
+
+/* Handles a situation where the device goes offline/out-of-range */
+static gboolean handle_device_lost_timeout(gpointer user_data)
+{
+	struct adv_monitor_device *dev = user_data;
+	struct adv_monitor *monitor = dev->monitor;
+	time_t curr_time = time(NULL);
+
+	DBG("Device Lost timeout triggered for device %p "
+	    "for the Adv Monitor at path %s", dev->device, monitor->path);
+
+	dev->device_lost_timer = 0;
+
+	if (dev->device_found && dev->last_seen) {
+		/* We were tracking for the Low RSSI filter. Check if there is
+		 * any Adv received after the timeout function is invoked.
+		 * If not, report the Device Lost event.
+		 */
+		if (difftime(curr_time, dev->last_seen) >=
+		    monitor->low_rssi_timeout) {
+			dev->device_found = false;
+
+			DBG("Calling DeviceLost() on Adv Monitor of owner %s "
+			    "at path %s", monitor->app->owner, monitor->path);
+
+			g_dbus_proxy_method_call(monitor->proxy, "DeviceLost",
+						 report_device_state_setup,
+						 NULL, dev->device, NULL);
+		}
+	}
+
+	return FALSE;
+}
+
+/* Filters an Adv based on its RSSI value */
+static void adv_monitor_filter_rssi(struct adv_monitor *monitor,
+				    struct btd_device *device, int8_t rssi)
+{
+	struct adv_monitor_device *dev = NULL;
+	time_t curr_time = time(NULL);
+	uint16_t adapter_id = monitor->app->manager->adapter_id;
+
+	/* If the RSSI thresholds and timeouts are not specified, report the
+	 * DeviceFound() event without tracking for the RSSI as the Adv has
+	 * already matched the pattern filter.
+	 */
+	if (monitor->high_rssi == ADV_MONITOR_UNSET_RSSI &&
+		monitor->low_rssi == ADV_MONITOR_UNSET_RSSI &&
+		monitor->high_rssi_timeout == ADV_MONITOR_UNSET_TIMER &&
+		monitor->low_rssi_timeout == ADV_MONITOR_UNSET_TIMER) {
+		DBG("Calling DeviceFound() on Adv Monitor of owner %s "
+		    "at path %s", monitor->app->owner, monitor->path);
+
+		g_dbus_proxy_method_call(monitor->proxy, "DeviceFound",
+					 report_device_state_setup, NULL,
+					 device, NULL);
+
+		return;
+	}
+
+	dev = queue_find(monitor->devices, monitor_device_match, device);
+	if (!dev)
+		dev = monitor_device_create(monitor, device);
+	if (!dev) {
+		btd_error(adapter_id, "Failed to create Adv Monitor "
+				      "device object.");
+		return;
+	}
+
+	if (dev->device_lost_timer) {
+		g_source_remove(dev->device_lost_timer);
+		dev->device_lost_timer = 0;
+	}
+
+	/* Reset the timings of found/lost if a device has been offline for
+	 * longer than the high/low timeouts.
+	 */
+	if (dev->last_seen) {
+		if (difftime(curr_time, dev->last_seen) >
+		    monitor->high_rssi_timeout) {
+			dev->high_rssi_first_seen = 0;
+		}
+
+		if (difftime(curr_time, dev->last_seen) >
+		    monitor->low_rssi_timeout) {
+			dev->low_rssi_first_seen = 0;
+		}
+	}
+	dev->last_seen = curr_time;
+
+	/* Check for the found devices (if the device is not already found) */
+	if (!dev->device_found && rssi > monitor->high_rssi) {
+		if (dev->high_rssi_first_seen) {
+			if (difftime(curr_time, dev->high_rssi_first_seen) >=
+			    monitor->high_rssi_timeout) {
+				dev->device_found = true;
+
+				DBG("Calling DeviceFound() on Adv Monitor "
+				    "of owner %s at path %s",
+				    monitor->app->owner, monitor->path);
+
+				g_dbus_proxy_method_call(
+					monitor->proxy, "DeviceFound",
+					report_device_state_setup, NULL,
+					dev->device, NULL);
+			}
+		} else {
+			dev->high_rssi_first_seen = curr_time;
+		}
+	} else {
+		dev->high_rssi_first_seen = 0;
+	}
+
+	/* Check for the lost devices (only if the device is already found, as
+	 * it doesn't make any sense to report the Device Lost event if the
+	 * device is not found yet)
+	 */
+	if (dev->device_found && rssi < monitor->low_rssi) {
+		if (dev->low_rssi_first_seen) {
+			if (difftime(curr_time, dev->low_rssi_first_seen) >=
+			    monitor->low_rssi_timeout) {
+				dev->device_found = false;
+
+				DBG("Calling DeviceLost() on Adv Monitor "
+				    "of owner %s at path %s",
+				    monitor->app->owner, monitor->path);
+
+				g_dbus_proxy_method_call(
+					monitor->proxy, "DeviceLost",
+					report_device_state_setup, NULL,
+					dev->device, NULL);
+			}
+		} else {
+			dev->low_rssi_first_seen = curr_time;
+		}
+	} else {
+		dev->low_rssi_first_seen = 0;
+	}
+
+	/* Setup a timer to track if the device goes offline/out-of-range, only
+	 * if we are tracking for the Low RSSI Threshold. If we are tracking
+	 * the High RSSI Threshold, nothing needs to be done.
+	 */
+	if (dev->device_found) {
+		dev->device_lost_timer =
+			g_timeout_add_seconds(monitor->low_rssi_timeout,
+					      handle_device_lost_timeout, dev);
+	}
+}
diff --git a/src/adv_monitor.h b/src/adv_monitor.h
index 69ea348f8..14508e7d1 100644
--- a/src/adv_monitor.h
+++ b/src/adv_monitor.h
@@ -21,6 +21,7 @@
 #define __ADV_MONITOR_H
 
 struct mgmt;
+struct btd_device;
 struct btd_adapter;
 struct btd_adv_monitor_manager;
 
@@ -29,4 +30,7 @@ struct btd_adv_monitor_manager *btd_adv_monitor_manager_create(
 						struct mgmt *mgmt);
 void btd_adv_monitor_manager_destroy(struct btd_adv_monitor_manager *manager);
 
+void btd_adv_monitor_device_remove(struct btd_adv_monitor_manager *manager,
+				   struct btd_device *device);
+
 #endif /* __ADV_MONITOR_H */
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-09-17 21:51 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17 21:25 [BlueZ PATCH v4 1/8] adv_monitor: Implement RSSI Filter logic for background scanning Miao-chen Chou
2020-09-17 21:25 ` [BlueZ PATCH v4 2/8] adv_monitor: Implement unit tests for RSSI Filter Miao-chen Chou
2020-09-17 21:49   ` [BlueZ,v4,2/8] " bluez.test.bot
2020-09-17 21:25 ` [BlueZ PATCH v4 3/8] adv_monitor: Implement Adv matching based on stored monitors Miao-chen Chou
2020-09-17 21:25 ` [BlueZ PATCH v4 4/8] adv_monitor: Implement unit tests for content filter Miao-chen Chou
2020-09-17 21:25 ` [BlueZ PATCH v4 5/8] adapter: Clear all Adv monitors upon bring-up Miao-chen Chou
2020-09-17 21:25 ` [BlueZ PATCH v4 6/8] adv_monitor: Implement Add Adv Patterns Monitor cmd handler Miao-chen Chou
2020-09-17 21:25 ` [BlueZ PATCH v4 7/8] adv_monitor: Fix return type of RegisterMonitor() method Miao-chen Chou
2020-09-17 21:25 ` [BlueZ PATCH v4 8/8] adv_monitor: Issue Remove Adv Monitor mgmt call Miao-chen Chou
2020-09-17 21:51 ` [BlueZ,v4,1/8] adv_monitor: Implement RSSI Filter logic for background scanning bluez.test.bot
  -- strict thread matches above, loose matches on Subject: below --
2020-09-17  7:10 [BlueZ PATCH v4 1/8] " Miao-chen Chou

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.