Skip to contentSkip to conversation

client: Add client-side error decoding

PATCH In review Bastien Nocera 3 participants 8 messages May 20, 2025 – May 21, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/4 client: Add client-side error decoding

The D-Bus errors returned in a number of cases aren't in human-readable form, but instead exist as "codes" (listed in src/error.h).

This new function will allow us to split a specifically formatted string into a human-readable message and an error code.

---
 Makefile.tools       |  2 +
 client/error-parse.c | 89 ++++++++++++++++++++++++++++++++++++++++++++
 client/error-parse.h | 12 ++++++
 3 files changed, 103 insertions(+)
 create mode 100644 client/error-parse.c
 create mode 100644 client/error-parse.h
Makefile.tools +2 -0
index e60c31b1d907..27346f1368a2 100644
@@ -10,6 +10,8 @@ client_bluetoothctl_SOURCES = client/main.c \
 					client/advertising.c \
 					client/adv_monitor.h \
 					client/adv_monitor.c \
+					client/error-parse.h \
+					client/error-parse.c \
 					client/gatt.h client/gatt.c \
 					client/admin.h client/admin.c \
 					client/player.h client/player.c \
client/error-parse.c new file +89 -0

89 lines not on this page · raw message

client/error-parse.h new file +12 -0
new file mode 100644
index 000000000000..c983982232f1
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2025 Bastien Nocera <[email protected]>
+ *
+ *
+ */
+
+const char *detailed_error_parse(const char  *error_msg,
+				 const char **error_code);
Signature · 1 line
2.49.0
#1 lore raw

PATCH v3 2/4 client: Print a human readable error message

When the D-Bus error message string contains an encoded error code and user-readable message, parse this message to only show the user-readable message.

---
 client/main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
client/main.c +4 -2
index 5d53a7be11e4..122d975a08da 100644
@@ -30,6 +30,7 @@
 #include "gdbus/gdbus.h"
 #include "print.h"
 #include "agent.h"
+#include "error-parse.h"
 #include "gatt.h"
 #include "advertising.h"
 #include "adv_monitor.h"
@@ -2013,8 +2014,9 @@ static void connect_reply(DBusMessage *message, void *user_data)
 	g_free(data);
 
 	if (dbus_set_error_from_message(&error, message) == TRUE) {
-		bt_shell_printf("Failed to connect: %s %s\n", error.name,
-				error.message);
+		const char *msg = detailed_error_parse(error.message, NULL);
+		bt_shell_printf("Failed to connect: %s: %s\n", error.name,
+				msg);
 		dbus_error_free(&error);
 		return bt_shell_noninteractive_quit(EXIT_FAILURE);
 	}
Signature · 1 line
2.49.0
#2 lore raw

PATCH v3 3/4 device: Better "Connect" debug

In reply to Bastien Nocera

Output clearer debug information so that it's possible to follow the decisions made by the bluetoothd daemon when a client such as bluetoothctl or the GNOME Bluetooth settings ask it to connect to a device.

---
 src/device.c | 41 +++++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)
src/device.c +33 -8
index d230af0a8aaa..0797e5ff5bb8 100644
@@ -2560,6 +2560,7 @@ static DBusMessage *connect_profiles(struct btd_device *dev, uint8_t bdaddr_type
 							"Connect") &&
 				find_service_with_state(dev->services,
 						BTD_SERVICE_STATE_CONNECTED)) {
+				DBG("Already connected to services");
 				return dbus_message_new_method_return(msg);
 			} else {
 				return btd_error_not_available_str(msg,
@@ -2572,8 +2573,10 @@ static DBusMessage *connect_profiles(struct btd_device *dev, uint8_t bdaddr_type
 
 	err = connect_next(dev);
 	if (err < 0) {
-		if (err == -EALREADY)
+		if (err == -EALREADY) {
+			DBG("Already connected");
 			return dbus_message_new_method_return(msg);
+		}
 		return btd_error_failed(msg,
 					btd_error_bredr_conn_from_errno(err));
 	}
@@ -2648,14 +2651,24 @@ static uint8_t select_conn_bearer(struct btd_device *dev)
 	return dev->bdaddr_type;
 }
 
+static const char *bdaddr_type_strs[] = {
+	"BR/EDR",
+	"LE public",
+	"LE random"
+};
+
 static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,
 							void *user_data)
 {
 	struct btd_device *dev = user_data;
 	uint8_t bdaddr_type;
 
-	if (dev->bonding)
+	DBG("Calling \"Connect\" for device %s", dev->path);
+
+	if (dev->bonding) {
+		DBG("Bonding in progress");
 		return btd_error_in_progress(msg);
+	}
 
 	if (dev->bredr_state.connected) {
 		/*
@@ -2664,23 +2677,35 @@ static DBusMessage *dev_connect(DBusConnection *conn, DBusMessage *msg,
 		 */
 		if (dev->bredr_state.svc_resolved &&
 			find_service_with_state(dev->services,
-						BTD_SERVICE_STATE_CONNECTED))
+						BTD_SERVICE_STATE_CONNECTED)) {
 			bdaddr_type = dev->bdaddr_type;
-		else
+			DBG("Selecting address type %s, as BR/EDR services are resolved "
+			    " and connected", bdaddr_type_strs[dev->bdaddr_type]);
+		} else {
 			bdaddr_type = BDADDR_BREDR;
-	} else if (dev->le_state.connected && dev->bredr)
+			DBG("Selecting address type BR/EDR, as services not resolved "
+			    "or not connected");
+		}
+	} else if (dev->le_state.connected && dev->bredr) {
 		bdaddr_type = BDADDR_BREDR;
-	else
+		DBG("Selecting address type BR/EDR, as LE already connected");
+	} else {
 		bdaddr_type = select_conn_bearer(dev);
+		DBG("Selecting address type %s", bdaddr_type_strs[dev->bdaddr_type]);
+	}
 
 	if (bdaddr_type != BDADDR_BREDR) {
 		int err;
 
-		if (dev->connect)
+		if (dev->connect) {
+			DBG("Device already connecting");
 			return btd_error_in_progress(msg);
+		}
 
-		if (dev->le_state.connected)
+		if (dev->le_state.connected) {
+			DBG("Device already connected through LE");
 			return dbus_message_new_method_return(msg);
+		}
 
 		btd_device_set_temporary(dev, false);
 
Signature · 1 line
2.49.0
#3 lore raw

PATCH v3 4/4 device: Add user-readable messages for a number of errors

In reply to Bastien Nocera
---
 src/device.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/device.c +6 -4
index 0797e5ff5bb8..d1023f762474 100644
@@ -1922,9 +1922,9 @@ void device_request_disconnect(struct btd_device *device, DBusMessage *msg)
 		DBusMessage *reply;
 
 		if (device->bonding_status == MGMT_STATUS_AUTH_FAILED)
-			err_str = ERR_BREDR_CONN_KEY_MISSING;
+			err_str = ":" ERR_BREDR_CONN_KEY_MISSING ":Link key missing";
 		else
-			err_str = ERR_BREDR_CONN_CANCELED;
+			err_str = ":" ERR_BREDR_CONN_CANCELED ":Connection canceled";
 		reply = btd_error_failed(device->connect, err_str);
 		g_dbus_send_message(dbus_conn, reply);
 		dbus_message_unref(device->connect);
@@ -2545,7 +2545,8 @@ static DBusMessage *connect_profiles(struct btd_device *dev, uint8_t bdaddr_type
 
 	if (!btd_adapter_get_powered(dev->adapter)) {
 		return btd_error_not_ready_str(msg,
-					ERR_BREDR_CONN_ADAPTER_NOT_POWERED);
+					":" ERR_BREDR_CONN_ADAPTER_NOT_POWERED
+					":Adapter not powered");
 	}
 
 	btd_device_set_temporary(dev, false);
@@ -2564,7 +2565,8 @@ static DBusMessage *connect_profiles(struct btd_device *dev, uint8_t bdaddr_type
 				return dbus_message_new_method_return(msg);
 			} else {
 				return btd_error_not_available_str(msg,
-					ERR_BREDR_CONN_PROFILE_UNAVAILABLE);
+					":" ERR_BREDR_CONN_PROFILE_UNAVAILABLE ":"
+					"Exhausted the list of BR/EDR profiles to connect to");
 			}
 		}
 
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH BlueZ v3 4/4] device: Add user-readable messages for a number of errors

Hi Bastien,

Quoted 42 lines · Bastien Nocera
---
 src/device.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/device.c +6 -5
index 0797e5ff5bb8..d1023f762474 100644
@@ -1922,9 +1922,9 @@ void device_request_disconnect(struct btd_device *device, DBusMessage *msg)
                DBusMessage *reply;
 
                if (device->bonding_status == MGMT_STATUS_AUTH_FAILED)
-                       err_str = ERR_BREDR_CONN_KEY_MISSING;
+                       err_str = ":" ERR_BREDR_CONN_KEY_MISSING ":Link key missing";
                else
-                       err_str = ERR_BREDR_CONN_CANCELED;
+                       err_str = ":" ERR_BREDR_CONN_CANCELED ":Connection canceled";
                reply = btd_error_failed(device->connect, err_str);
                g_dbus_send_message(dbus_conn, reply);
                dbus_message_unref(device->connect);
@@ -2545,7 +2545,8 @@ static DBusMessage *connect_profiles(struct btd_device *dev, uint8_t bdaddr_type
 
        if (!btd_adapter_get_powered(dev->adapter)) {
                return btd_error_not_ready_str(msg,
-                                       ERR_BREDR_CONN_ADAPTER_NOT_POWERED);
+                                       ":" ERR_BREDR_CONN_ADAPTER_NOT_POWERED
+                                       ":Adapter not powered");
        }
 
        btd_device_set_temporary(dev, false);
@@ -2564,7 +2565,8 @@ static DBusMessage *connect_profiles(struct btd_device *dev, uint8_t bdaddr_type
                                return dbus_message_new_method_return(msg);
                        } else {
                                return btd_error_not_available_str(msg,
-                                       ERR_BREDR_CONN_PROFILE_UNAVAILABLE);
+                                       ":" ERR_BREDR_CONN_PROFILE_UNAVAILABLE ":"
+                                       "Exhausted the list of BR/EDR profiles to connect to");
                        }
                }
 
--
Quoted 1 line

2.49.0

I was afraid we would need to change these string again, also if we need to encode more than human readable errors on on the message something is already quite wrong, so I wonder if we shall instead expand the error code itself e.g.:

DBusMessage *btd_error_profile_unavailable_str(DBusMessage *msg, const char *str)

{
    return g_dbus_create_error(msg, ERROR_INTERFACE ".ProfileUnavailable", str);
}

That in my opinion uses the error reply better since we can isolate what is considered a fixed string, and therefore part of the stable API, and what could be considered a free format which is the error message.

Quoted 1 line
Signature · 1 line
Luiz Augusto von Dentz
#5 lore raw

Re: [PATCH BlueZ v3 4/4] device: Add user-readable messages for a number of errors

Quoted 74 lines · Luiz Augusto von Dentz

Hi Bastien,

Quoted 54 lines · Bastien Nocera

---
 src/device.c | 10 ++++++----

 1 file changed, 6 insertions(+), 4 deletions(-)
src/device.c +0 -0
index 0797e5ff5bb8..d1023f762474 100644
@@ -1922,9 +1922,9 @@ void device_request_disconnect(struct
 btd_device *device, DBusMessage *msg)
                 DBusMessage *reply;
 

                if (device->bonding_status == MGMT_STATUS_AUTH_FAILED)
-                       err_str = ERR_BREDR_CONN_KEY_MISSING; +                       err_str = ":" ERR_BREDR_CONN_KEY_MISSING ":Link key missing";
                else
-                       err_str = ERR_BREDR_CONN_CANCELED; +                       err_str = ":" ERR_BREDR_CONN_CANCELED ":Connection canceled";
                reply = btd_error_failed(device->connect, err_str); g_dbus_send_message(dbus_conn, reply); dbus_message_unref(device->connect);

src/device.c +0 -0
@@ -2545,7 +2545,8 @@ static DBusMessage *connect_profiles(struct

btd_device *dev, uint8_t bdaddr_type

        if (!btd_adapter_get_powered(dev->adapter)) { return btd_error_not_ready_str(msg,
-
ERR_BREDR_CONN_ADAPTER_NOT_POWERED);
+                                       ":" ERR_BREDR_CONN_ADAPTER_NOT_POWERED
+                                       ":Adapter not powered"); }

        btd_device_set_temporary(dev, false);

src/device.c +0 -0
@@ -2564,7 +2565,8 @@ static DBusMessage *connect_profiles(struct

btd_device *dev, uint8_t bdaddr_type
                                return dbus_message_new_method_return(msg);
                        } else {
                                return btd_error_not_available_str(msg,
-
ERR_BREDR_CONN_PROFILE_UNAVAILABLE);
+                                       ":" ERR_BREDR_CONN_PROFILE_UNAVAILABLE ":"
+                                       "Exhausted the list of BR/EDR profiles to connect to");
                        }
                }

Quoted 2 lines

--
2.49.0

I was afraid we would need to change these string again, also if we need to encode more than human readable errors on on the message something is already quite wrong, so I wonder if we shall instead expand the error code itself e.g.:

DBusMessage *btd_error_profile_unavailable_str(DBusMessage *msg, const
char *str)
{
    return g_dbus_create_error(msg, ERROR_INTERFACE ".ProfileUnavailable", str);
}

Quoted 4 lines

That in my opinion uses the error reply better since we can isolate what is considered a fixed string, and therefore part of the stable API, and what could be considered a free format which is the error message.

So, for this example, a new error code
org.bluez.Error.ProfileUnavailable to be added to
doc/org.bluez.Device.rst right?

Or would org.bluez.Error.Failed.ProfileUnavailable make it easier for front-ends to parse? gnome-bluetooth doesn't (currently) have any code to differentiate those, so it's not a problem on my side.

What about the human-readable strings, would you want bluetoothctl to translate the error code to something human-readable, or should we have a human-readable message right in src/device.c in this case.

In short, does this work for you?

DBusMessage *btd_error_failed_profile_unavailable(DBusMessage *msg) {
return g_dbus_create_error(msg, ERROR_INTERFACE
".Failed.ProfileUnavailable", "Exhausted the list of BR/EDR profiles to connect to");
}

Quoted 3 lines
Quoted 2 lines
#6 lore raw

Re: [PATCH BlueZ v3 4/4] device: Add user-readable messages for a number of errors

Hi Bastien,

Quoted 81 lines · Bastien Nocera
Quoted 74 lines · Luiz Augusto von Dentz

Hi Bastien,

Quoted 50 lines · Bastien Nocera
---
 src/device.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/device.c +2 -2
index 0797e5ff5bb8..d1023f762474 100644
@@ -1922,9 +1922,9 @@ void device_request_disconnect(struct
 btd_device *device, DBusMessage *msg)
                DBusMessage *reply;
 
                if (device->bonding_status ==
 MGMT_STATUS_AUTH_FAILED)
-                       err_str = ERR_BREDR_CONN_KEY_MISSING;
+                       err_str = ":" ERR_BREDR_CONN_KEY_MISSING
 ":Link key missing";
                else
-                       err_str = ERR_BREDR_CONN_CANCELED;
+                       err_str = ":" ERR_BREDR_CONN_CANCELED
":Connection canceled";
                reply = btd_error_failed(device->connect, err_str);
                g_dbus_send_message(dbus_conn, reply);
                dbus_message_unref(device->connect);
src/device.c +0 -0
@@ -2545,7 +2545,8 @@ static DBusMessage *connect_profiles(struct

btd_device *dev, uint8_t bdaddr_type

        if (!btd_adapter_get_powered(dev->adapter)) {
                return btd_error_not_ready_str(msg,

-
ERR_BREDR_CONN_ADAPTER_NOT_POWERED);
+ ":"
ERR_BREDR_CONN_ADAPTER_NOT_POWERED

src/device.c +1 -0
+                                       ":Adapter not powered");
        }
 
        btd_device_set_temporary(dev, false);
@@ -2564,7 +2565,8 @@ static DBusMessage *connect_profiles(struct

btd_device *dev, uint8_t bdaddr_type
return

dbus_message_new_method_return(msg);
                        } else {
                                return

btd_error_not_available_str(msg,
-
ERR_BREDR_CONN_PROFILE_UNAVAILABLE);
+ ":"
ERR_BREDR_CONN_PROFILE_UNAVAILABLE ":"
+ "Exhausted the list of

Quoted 6 lines
BR/EDR profiles to connect to");
                        }
                }

--
2.49.0

I was afraid we would need to change these string again, also if we need to encode more than human readable errors on on the message something is already quite wrong, so I wonder if we shall instead expand the error code itself e.g.:

DBusMessage *btd_error_profile_unavailable_str(DBusMessage *msg, const
char *str)
{
return g_dbus_create_error(msg, ERROR_INTERFACE
".ProfileUnavailable", str);
}

Quoted 4 lines

That in my opinion uses the error reply better since we can isolate what is considered a fixed string, and therefore part of the stable API, and what could be considered a free format which is the error message.

Quoted 7 lines

So, for this example, a new error code
org.bluez.Error.ProfileUnavailable to be added to
doc/org.bluez.Device.rst right?

Or would org.bluez.Error.Failed.ProfileUnavailable make it easier for front-ends to parse? gnome-bluetooth doesn't (currently) have any code to differentiate those, so it's not a problem on my side.

Don't think it adds anything to include a subdomain as org.bluez.Error.Failed, except if you are saying that is going to break if application don't except errors other than what is currently documented, I would consider that a bug since otherwise we can't really add any new errors.

Quoted 4 lines

What about the human-readable strings, would you want bluetoothctl to translate the error code to something human-readable, or should we have a human-readable message right in src/device.c in this case.

Quoted 8 lines

In short, does this work for you?

DBusMessage *btd_error_failed_profile_unavailable(DBusMessage *msg) {
return g_dbus_create_error(msg, ERROR_INTERFACE
".Failed.ProfileUnavailable", "Exhausted the list of BR/EDR profiles to connect to");
}

Except for the error just being .ProfileUnavailable that sounds good to me.

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
Signature · 1 line
Luiz Augusto von Dentz
#7 lore raw
In reply to Bastien Nocera

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

Dear submitter,

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

---Test result---

Test Summary:

CheckPatch                    PENDING   0.31 seconds
GitLint                       PENDING   0.27 seconds
BuildEll                      PASS      21.01 seconds
BluezMake                     PASS      2949.70 seconds
MakeCheck                     PASS      20.88 seconds
MakeDistcheck                 PASS      206.14 seconds
CheckValgrind                 PASS      281.18 seconds
CheckSmatch                   PASS      305.40 seconds
bluezmakeextell               PASS      130.73 seconds
IncrementalBuild              PENDING   0.30 seconds
ScanBuild                     PASS      930.06 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:

---
Regards,
Linux Bluetooth