gatt: Parse error message
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Thu, 25 Aug 2022 20:05:23 +0000 (13:05 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:54 +0000 (14:55 +0530)
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 <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/gatt-database.c

index ba2d2d5..f0f74c1 100644 (file)
@@ -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) {