Linux bluetooth development
 help / color / mirror / Atom feed
From: Arthur Crepin-Leblond <arthur@marmottus.net>
To: linux-bluetooth@vger.kernel.org
Cc: Arthur Crepin-Leblond <arthur@marmottus.net>
Subject: [PATCH BlueZ 1/1] device: Expose the disconnect reason over D-Bus
Date: Mon, 12 Dec 2022 14:32:35 +0100	[thread overview]
Message-ID: <20221212133235.79657-2-arthur@marmottus.net> (raw)
In-Reply-To: <20221212133235.79657-1-arthur@marmottus.net>

Get the disconnect reason from the adapter disconnect notify callback
and set the D-Bus "DisconnectReason" property.
---
 src/device.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/src/device.c b/src/device.c
index 995d39f2c..395cbe3e5 100644
--- a/src/device.c
+++ b/src/device.c
@@ -183,6 +183,7 @@ struct btd_device {
 	bool		le;
 	bool		pending_paired;		/* "Paired" waiting for SDP */
 	bool		svc_refreshed;
+	uint8_t		disconnect_reason;
 	bool		refresh_discovery;
 
 	/* Manage whether this device can wake the system from suspend.
@@ -1134,6 +1135,18 @@ dev_property_get_svc_resolved(const GDBusPropertyTable *property,
 	return TRUE;
 }
 
+static gboolean
+dev_property_get_disconnect_reason(const GDBusPropertyTable *property,
+					DBusMessageIter *iter, void *data)
+{
+	struct btd_device *device = data;
+	guint8 val = device->disconnect_reason;
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_BYTE, &val);
+
+	return TRUE;
+}
+
 static gboolean dev_property_flags_exist(const GDBusPropertyTable *property,
 								void *data)
 {
@@ -2624,6 +2637,14 @@ static void device_set_svc_refreshed(struct btd_device *device, bool value)
 					DEVICE_INTERFACE, "ServicesResolved");
 }
 
+static void btd_device_disconnected_cb(struct btd_device *device, uint8_t reason)
+{
+	device->disconnect_reason = reason;
+
+	g_dbus_emit_property_changed(dbus_conn, device->path,
+					DEVICE_INTERFACE, "DisconnectReason");
+}
+
 static void device_svc_resolved(struct btd_device *dev, uint8_t browse_type,
 						uint8_t bdaddr_type, int err)
 {
@@ -2987,6 +3008,7 @@ static const GDBusPropertyTable device_properties[] = {
 	{ "TxPower", "n", dev_property_get_tx_power, NULL,
 					dev_property_exists_tx_power },
 	{ "ServicesResolved", "b", dev_property_get_svc_resolved, NULL, NULL },
+	{ "DisconnectReason", "y", dev_property_get_disconnect_reason, NULL, NULL },
 	{ "AdvertisingFlags", "ay", dev_property_get_flags, NULL,
 					dev_property_flags_exist,
 					G_DBUS_PROPERTY_FLAG_EXPERIMENTAL},
@@ -3910,6 +3932,8 @@ static struct btd_device *device_new(struct btd_adapter *adapter,
 		return NULL;
 	}
 
+	device->disconnect_reason = MGMT_DEV_DISCONN_UNKNOWN;
+
 	memset(device->ad_flags, INVALID_FLAGS, sizeof(device->ad_flags));
 
 	device->ad = bt_ad_new();
@@ -6910,11 +6934,13 @@ void btd_device_init(void)
 	dbus_conn = btd_get_dbus_connection();
 	service_state_cb_id = btd_service_add_state_cb(
 						service_state_changed, NULL);
+	btd_add_disconnect_cb(btd_device_disconnected_cb);
 }
 
 void btd_device_cleanup(void)
 {
 	btd_service_remove_state_cb(service_state_cb_id);
+	btd_remove_disconnect_cb(btd_device_disconnected_cb);
 }
 
 void btd_device_set_volume(struct btd_device *device, int8_t volume)
-- 
2.38.1


  reply	other threads:[~2022-12-12 13:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-12 13:32 [PATCH BlueZ 0/1] Expose the disconnect reason over D-Bus Arthur Crepin-Leblond
2022-12-12 13:32 ` Arthur Crepin-Leblond [this message]
2022-12-12 14:41   ` bluez.test.bot
2022-12-12 14:49 ` [PATCH BlueZ 0/1] " Bastien Nocera
2022-12-12 15:36   ` Arthur Crepin-Leblond
2022-12-13 20:45     ` Luiz Augusto von Dentz
2022-12-20 12:30       ` Arthur Crepin-Leblond

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=20221212133235.79657-2-arthur@marmottus.net \
    --to=arthur@marmottus.net \
    --cc=linux-bluetooth@vger.kernel.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