linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Howard Chung <howardchung@google.com>
To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com
Cc: Yun-Hao Chung <howardchung@chromium.org>,
	Miao-chen Chou <mcchou@chromium.org>
Subject: [Bluez PATCH v1 05/14] core: add device state and state callbacks
Date: Thu,  8 Jul 2021 14:23:05 +0800	[thread overview]
Message-ID: <20210708142059.Bluez.v1.5.I699a3837a5a18d9f889635300f6c717879f19dd2@changeid> (raw)
In-Reply-To: <20210708062314.245754-1-howardchung@google.com>

From: Yun-Hao Chung <howardchung@chromium.org>

This implements functions to register/unregister state changed callback
functions, the functions will be called when a device's state changed.
Currently the state only shows initializing, available and removing,
which is enough for plugins to register dbus objects upon device
creation and unregister it upon device removal.

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

 src/device.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/device.h | 13 +++++++++++
 2 files changed, 77 insertions(+)

diff --git a/src/device.c b/src/device.c
index e1d82eab0988..0d7444706336 100644
--- a/src/device.c
+++ b/src/device.c
@@ -81,6 +81,13 @@
 
 static DBusConnection *dbus_conn = NULL;
 static unsigned service_state_cb_id;
+static GSList *device_state_callbacks;
+
+struct device_state_callback {
+	btd_device_state_cb	cb;
+	void			*user_data;
+	unsigned int		id;
+};
 
 struct btd_disconnect_data {
 	guint id;
@@ -272,6 +279,8 @@ struct btd_device {
 
 	GIOChannel	*att_io;
 	guint		store_id;
+
+	enum btd_device_state_t state;
 };
 
 static const uint16_t uuid_list[] = {
@@ -4095,6 +4104,23 @@ static void gatt_service_removed(struct gatt_db_attribute *attr,
 	gatt_services_changed(device);
 }
 
+static void device_change_state(struct btd_device *device,
+					enum btd_device_state_t new_state)
+{
+	GSList *l;
+	struct device_state_callback *cb_data;
+
+	if (device->state == new_state)
+		return;
+
+	for (l = device_state_callbacks; l != NULL; l = g_slist_next(l)) {
+		cb_data = l->data;
+		cb_data->cb(device, new_state, cb_data->user_data);
+	}
+
+	device->state = new_state;
+}
+
 static struct btd_device *device_new(struct btd_adapter *adapter,
 				const char *address)
 {
@@ -4158,6 +4184,8 @@ static struct btd_device *device_new(struct btd_adapter *adapter,
 
 	device->refresh_discovery = btd_opts.refresh_discovery;
 
+	device_change_state(device, BTD_DEVICE_STATE_AVAILABLE);
+
 	return btd_device_ref(device);
 }
 
@@ -6839,6 +6867,7 @@ void btd_device_unref(struct btd_device *device)
 
 	DBG("Freeing device %s", device->path);
 
+	device_change_state(device, BTD_DEVICE_STATE_REMOVING);
 	g_dbus_unregister_interface(dbus_conn, device->path, DEVICE_INTERFACE);
 }
 
@@ -6980,3 +7009,38 @@ void btd_device_cleanup(void)
 {
 	btd_service_remove_state_cb(service_state_cb_id);
 }
+
+unsigned int btd_device_add_state_cb(btd_device_state_cb cb, void *user_data)
+{
+	struct device_state_callback *cb_data;
+	static unsigned int id;
+
+	cb_data = g_new0(struct device_state_callback, 1);
+	cb_data->cb = cb;
+	cb_data->user_data = user_data;
+	cb_data->id = ++id;
+
+	device_state_callbacks = g_slist_append(device_state_callbacks,
+								cb_data);
+
+	return cb_data->id;
+}
+
+bool btd_device_remove_state_cb(unsigned int id)
+{
+	GSList *l;
+
+	for (l = device_state_callbacks; l != NULL; l = g_slist_next(l)) {
+		struct device_state_callback *cb_data = l->data;
+
+		if (cb_data && cb_data->id == id) {
+			device_state_callbacks = g_slist_remove(
+							device_state_callbacks,
+							cb_data);
+			g_free(cb_data);
+			return true;
+		}
+	}
+
+	return false;
+}
diff --git a/src/device.h b/src/device.h
index 5f615cb4b6b2..a8424aa4f098 100644
--- a/src/device.h
+++ b/src/device.h
@@ -11,8 +11,18 @@
 
 #define DEVICE_INTERFACE	"org.bluez.Device1"
 
+enum btd_device_state_t {
+	BTD_DEVICE_STATE_INITIALIZING,	/* Device object is creating */
+	BTD_DEVICE_STATE_AVAILABLE,	/* Device object is registered */
+	BTD_DEVICE_STATE_REMOVING,	/* Device object is being removed */
+};
+
 struct btd_device;
 
+typedef void (*btd_device_state_cb) (struct btd_device *device,
+					enum btd_device_state_t new_state,
+					void *user_data);
+
 struct btd_device *device_create(struct btd_adapter *adapter,
 				const bdaddr_t *address, uint8_t bdaddr_type);
 struct btd_device *device_create_from_storage(struct btd_adapter *adapter,
@@ -179,3 +189,6 @@ bool btd_device_all_services_allowed(struct btd_device *dev);
 void btd_device_update_allowed_services(struct btd_device *dev);
 void btd_device_init(void);
 void btd_device_cleanup(void);
+
+unsigned int btd_device_add_state_cb(btd_device_state_cb cb, void *user_data);
+bool btd_device_remove_state_cb(unsigned int id);
-- 
2.32.0.93.g670b81a890-goog


  parent reply	other threads:[~2021-07-08  6:23 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-08  6:23 [Bluez PATCH v1 00/14] Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 01/14] lib: add hash functions for bt_uuid_t Howard Chung
2021-07-08  6:36   ` [Bluez,v1,01/14] " bluez.test.bot
2021-07-09  5:21   ` [Bluez PATCH v1 01/14] " Luiz Augusto von Dentz
2021-07-12  3:20     ` Yun-hao Chung
2021-07-08  6:23 ` [Bluez PATCH v1 02/14] unit: add uuid unit tests Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 03/14] core: add is_allowed property in btd_service Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 04/14] core: add adapter and device allowed_uuid functions Howard Chung
2021-07-08  6:23 ` Howard Chung [this message]
2021-07-09  5:34   ` [Bluez PATCH v1 05/14] core: add device state and state callbacks Luiz Augusto von Dentz
2021-07-12  3:56     ` Yun-hao Chung
2021-07-08  6:23 ` [Bluez PATCH v1 06/14] audio: Remove Media1 interface when a2dp source disallowed Howard Chung
2021-07-09  5:49   ` Luiz Augusto von Dentz
2021-07-12  8:16     ` Yun-hao Chung
2021-07-12 16:37       ` Luiz Augusto von Dentz
2021-07-08  6:23 ` [Bluez PATCH v1 07/14] plugins: add a new plugin for admin_policy Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 08/14] plugins/admin_policy: add admin_policy adapter driver Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 09/14] plugins/admin_policy: add ServiceAllowList method Howard Chung
2021-07-09  6:01   ` Luiz Augusto von Dentz
2021-07-12  9:09     ` Yun-hao Chung
2021-07-12 16:41       ` Luiz Augusto von Dentz
2021-07-08  6:23 ` [Bluez PATCH v1 10/14] plugins/admin_policy: add ServiceAllowList property Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 11/14] plugins/admin_policy: add device state callback Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 12/14] plugins/admin_policy: add AffectedByPolicy property Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 13/14] plugins/admin_policy: persist policy settings Howard Chung
2021-07-08  6:23 ` [Bluez PATCH v1 14/14] core: fix a possible crash when removing devices Howard Chung

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210708142059.Bluez.v1.5.I699a3837a5a18d9f889635300f6c717879f19dd2@changeid \
    --to=howardchung@google.com \
    --cc=howardchung@chromium.org \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=mcchou@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).