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 diff --git a/Makefile.tools b/Makefile.tools index e60c31b1d907..27346f1368a2 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -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 \ diff --git a/client/error-parse.c b/client/error-parse.c new file mode 100644 index 000000000000..782d63f625f9 --- /dev/null +++ b/client/error-parse.c @@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2025 Bastien Nocera <hadess@xxxxxxxxxx> + * + * + */ + +#include <stddef.h> +#include <glib.h> +#include "src/error.h" +#include "error-parse.h" + +const char *error_codes[] = { + ERR_BREDR_CONN_ALREADY_CONNECTED, + ERR_BREDR_CONN_PAGE_TIMEOUT, + ERR_BREDR_CONN_PROFILE_UNAVAILABLE, + ERR_BREDR_CONN_SDP_SEARCH, + ERR_BREDR_CONN_CREATE_SOCKET, + ERR_BREDR_CONN_INVALID_ARGUMENTS, + ERR_BREDR_CONN_ADAPTER_NOT_POWERED, + ERR_BREDR_CONN_NOT_SUPPORTED, + ERR_BREDR_CONN_BAD_SOCKET, + ERR_BREDR_CONN_MEMORY_ALLOC, + ERR_BREDR_CONN_BUSY, + ERR_BREDR_CONN_CNCR_CONNECT_LIMIT, + ERR_BREDR_CONN_TIMEOUT, + ERR_BREDR_CONN_REFUSED, + ERR_BREDR_CONN_ABORT_BY_REMOTE, + ERR_BREDR_CONN_ABORT_BY_LOCAL, + ERR_BREDR_CONN_LMP_PROTO_ERROR, + ERR_BREDR_CONN_CANCELED, + ERR_BREDR_CONN_KEY_MISSING, + ERR_BREDR_CONN_UNKNOWN, + ERR_LE_CONN_INVALID_ARGUMENTS, + ERR_LE_CONN_ADAPTER_NOT_POWERED, + ERR_LE_CONN_NOT_SUPPORTED, + ERR_LE_CONN_ALREADY_CONNECTED, + ERR_LE_CONN_BAD_SOCKET, + ERR_LE_CONN_MEMORY_ALLOC, + ERR_LE_CONN_BUSY, + ERR_LE_CONN_REFUSED, + ERR_LE_CONN_CREATE_SOCKET, + ERR_LE_CONN_TIMEOUT, + ERR_LE_CONN_SYNC_CONNECT_LIMIT, + ERR_LE_CONN_ABORT_BY_REMOTE, + ERR_LE_CONN_ABORT_BY_LOCAL, + ERR_LE_CONN_LL_PROTO_ERROR, + ERR_LE_CONN_GATT_BROWSE, + ERR_LE_CONN_KEY_MISSING, + ERR_LE_CONN_UNKNOWN +}; + +#define MIN_ERROR_MSG_LEN 4 + +/* Parse formatted combined error code + user-readable error + * string into its components. + * Format is ":code:message" */ +const char *detailed_error_parse(const char *error_msg, + const char **error_code) +{ + const char *second_colon; + unsigned int i; + + if (error_msg == NULL) + goto out; + + if (*error_msg != ':') + goto out; + if (strlen(error_msg) < MIN_ERROR_MSG_LEN) + goto out; + + second_colon = strchr(error_msg + 1, ':'); + if (second_colon == NULL) + goto out; + + for (i = 0; i < G_N_ELEMENTS(error_codes); i++) { + if (strncmp(error_codes[i], error_msg + 1, (size_t)(second_colon - 1 - error_msg)) == 0) { + if (error_code != NULL) + *error_code = error_codes[i]; + return second_colon + 1; + } + } + +out: + return error_msg; +} diff --git a/client/error-parse.h b/client/error-parse.h new file mode 100644 index 000000000000..c983982232f1 --- /dev/null +++ b/client/error-parse.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2025 Bastien Nocera <hadess@xxxxxxxxxx> + * + * + */ + +const char *detailed_error_parse(const char *error_msg, + const char **error_code); -- 2.49.0