From c8ba11d9805e54e66c6cf3e62a2b2f4cd4c521ba Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Thu, 25 Aug 2022 13:05:23 -0700 Subject: [PATCH] gatt: Parse error message Application can now encode an error code into the D-Bus reply error message (0x80-0x9f). Fixes: https://github.com/bluez/bluez/issues/380 Signed-off-by: Manika Shrivastava Signed-off-by: Ayush Garg --- src/gatt-database.c | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/gatt-database.c b/src/gatt-database.c index ba2d2d5..f0f74c1 100644 --- a/src/gatt-database.c +++ b/src/gatt-database.c @@ -2613,28 +2613,44 @@ static uint8_t dbus_error_to_att_ecode(const char *error_message, uint8_t perm_e return att_ecode; } #else -static uint8_t dbus_error_to_att_ecode(const char *error_name, uint8_t perm_err) +static uint8_t dbus_error_to_att_ecode(const char *name, const char *msg, + uint8_t perm_err) { - if (strcmp(error_name, ERROR_INTERFACE ".Failed") == 0) - return 0x80; /* For now return this "application error" */ + if (strcmp(name, ERROR_INTERFACE ".Failed") == 0) { + char *endptr = NULL; + uint32_t ecode; - if (strcmp(error_name, ERROR_INTERFACE ".NotSupported") == 0 + ecode = strtol(msg, &endptr, 0); + + /* If message doesn't set an error code just use 0x80 */ + if (!endptr || *endptr != '\0') + return 0x80; + + if (ecode < 0x80 || ecode > 0x9f) { + error("Invalid error code: %s", msg); + return BT_ATT_ERROR_UNLIKELY; + } + + return ecode; + } + + if (strcmp(name, ERROR_INTERFACE ".NotSupported") == 0) return BT_ATT_ERROR_REQUEST_NOT_SUPPORTED; - if (strcmp(error_name, ERROR_INTERFACE ".NotAuthorized") == 0) + if (strcmp(name, ERROR_INTERFACE ".NotAuthorized") == 0) return BT_ATT_ERROR_AUTHORIZATION; - if (strcmp(error_name, ERROR_INTERFACE ".InvalidValueLength") == 0) + if (strcmp(name, ERROR_INTERFACE ".InvalidValueLength") == 0) return BT_ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LEN; - if (strcmp(error_name, ERROR_INTERFACE ".InvalidOffset") == 0) + if (strcmp(name, ERROR_INTERFACE ".InvalidOffset") == 0) return BT_ATT_ERROR_INVALID_OFFSET; - if (strcmp(error_name, ERROR_INTERFACE ".InProgress") == 0) + if (strcmp(name, ERROR_INTERFACE ".InProgress") == 0) return BT_ERROR_ALREADY_IN_PROGRESS; - if (strcmp(error_name, ERROR_INTERFACE ".NotPermitted") == 0) + if (strcmp(name, ERROR_INTERFACE ".NotPermitted") == 0) return perm_err; return BT_ATT_ERROR_UNLIKELY; @@ -2663,7 +2679,7 @@ static void read_reply_cb(DBusMessage *message, void *user_data) ecode = dbus_error_to_att_ecode(err.message, BT_ATT_ERROR_WRITE_NOT_PERMITTED); #else - ecode = dbus_error_to_att_ecode(err.name, + ecode = dbus_error_to_att_ecode(err.name, err.message, BT_ATT_ERROR_WRITE_NOT_PERMITTED); #endif dbus_error_free(&err); @@ -2890,7 +2906,7 @@ static void write_reply_cb(DBusMessage *message, void *user_data) ecode = dbus_error_to_att_ecode(err.message, BT_ATT_ERROR_WRITE_NOT_PERMITTED); #else - ecode = dbus_error_to_att_ecode(err.name, + ecode = dbus_error_to_att_ecode(err.name, err.message, BT_ATT_ERROR_WRITE_NOT_PERMITTED); #endif dbus_error_free(&err); @@ -3102,8 +3118,13 @@ static void acquire_write_reply(DBusMessage *message, void *user_data) error("Failed to acquire write: %s\n", err.name); +#ifdef TIZEN_FEATURE_BLUEZ_MODIFY ecode = dbus_error_to_att_ecode(err.name, + BT_ATT_ERROR_WRITE_NOT_PERMITTED); +#else + ecode = dbus_error_to_att_ecode(err.name, err.message, BT_ATT_ERROR_WRITE_NOT_PERMITTED); +#endif dbus_error_free(&err); if (ecode != BT_ATT_ERROR_UNLIKELY) { -- 2.7.4