Skip to contentSkip to conversation

gatt-api: Add error code ReadValue/WriteValue

PATCH Applied Luiz Augusto von Dentz 3 participants 4 messages Aug 25, 2022 – Aug 29, 2022 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/2 gatt-api: Add error code ReadValue/WriteValue

From: Luiz Augusto von Dentz <[email protected]>

This allows application to return error codes in the range allowed by the spec:

'GATT - Section 4.9.5:

Application Error 0x80 – 0x9F Application error code defined by a higher layer specification.'

---
 doc/gatt-api.txt | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
doc/gatt-api.txt +6 -2
index af0aa761d0a2..5042c54704e4 100644
@@ -79,13 +79,15 @@ Methods		array{byte} ReadValue(dict options)
 					  "mtu": Exchanged MTU (Server only)
 					  "device": Object Device (Server only)
 
-			Possible Errors: org.bluez.Error.Failed
+			Possible Errors: org.bluez.Error.Failed(string ecode)
 					 org.bluez.Error.InProgress
 					 org.bluez.Error.NotPermitted
 					 org.bluez.Error.NotAuthorized
 					 org.bluez.Error.InvalidOffset
 					 org.bluez.Error.NotSupported
 
+			Possible Error Code: string 0x80 - 0x9f
+
 		void WriteValue(array{byte} value, dict options)
 
 			Issues a request to write the value of the
@@ -105,13 +107,15 @@ Methods		array{byte} ReadValue(dict options)
 							       authorization
 							       request
 
-			Possible Errors: org.bluez.Error.Failed
+			Possible Errors: org.bluez.Error.Failed(string ecode)
 					 org.bluez.Error.InProgress
 					 org.bluez.Error.NotPermitted
 					 org.bluez.Error.InvalidValueLength
 					 org.bluez.Error.NotAuthorized
 					 org.bluez.Error.NotSupported
 
+			Possible Error Code: string 0x80 - 0x9f
+
 		fd, uint16 AcquireWrite(dict options) [optional]
 
 			Acquire file descriptor and MTU for writing. Only
Signature · 1 line
2.37.2
author maintainer
#1 lore raw

PATCH 2/2 gatt: Parse error message

From: Luiz Augusto von Dentz <[email protected]>

Application can now encode an error code into the D-Bus reply error message (0x80-0x9f).

---
 src/gatt-database.c | 40 ++++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)
src/gatt-database.c +28 -12
index db8432c6bf77..89a3dc47560b 100644
@@ -2192,27 +2192,43 @@ static bool parse_handle(GDBusProxy *proxy, uint16_t *handle)
 	return true;
 }
 
-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;
@@ -2236,7 +2252,7 @@ static void read_reply_cb(DBusMessage *message, void *user_data)
 
 	if (dbus_set_error_from_message(&err, message) == TRUE) {
 		DBG("Failed to read value: %s: %s", err.name, err.message);
-		ecode = dbus_error_to_att_ecode(err.name,
+		ecode = dbus_error_to_att_ecode(err.name, err.message,
 					BT_ATT_ERROR_READ_NOT_PERMITTED);
 		dbus_error_free(&err);
 		goto done;
@@ -2415,7 +2431,7 @@ static void write_reply_cb(DBusMessage *message, void *user_data)
 
 	if (dbus_set_error_from_message(&err, message) == TRUE) {
 		DBG("Failed to write value: %s: %s", err.name, err.message);
-		ecode = dbus_error_to_att_ecode(err.name,
+		ecode = dbus_error_to_att_ecode(err.name, err.message,
 					BT_ATT_ERROR_WRITE_NOT_PERMITTED);
 		dbus_error_free(&err);
 		goto done;
@@ -2610,7 +2626,7 @@ static void acquire_write_reply(DBusMessage *message, void *user_data)
 
 		error("Failed to acquire write: %s\n", err.name);
 
-		ecode = dbus_error_to_att_ecode(err.name,
+		ecode = dbus_error_to_att_ecode(err.name, err.message,
 					BT_ATT_ERROR_WRITE_NOT_PERMITTED);
 		dbus_error_free(&err);
 
Signature · 1 line
2.37.2
#2 lore raw
In reply to Luiz Augusto von Dentz

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=671211

---Test result---

Test Summary:

CheckPatch                    PASS      2.27 seconds
GitLint                       PASS      1.61 seconds
Prep - Setup ELL              PASS      26.73 seconds
Build - Prep                  PASS      0.73 seconds
Build - Configure             PASS      8.39 seconds
Build - Make                  PASS      969.49 seconds
Make Check                    PASS      11.87 seconds
Make Check w/Valgrind         PASS      286.82 seconds
Make Distcheck                PASS      235.16 seconds
Build w/ext ELL - Configure   PASS      8.64 seconds
Build w/ext ELL - Make        PASS      82.00 seconds
Incremental Build w/ patches  PASS      194.44 seconds
Scan Build                    WARNING   491.96 seconds

Details
##############################
Test: Scan Build - WARNING
Desc: Run Scan Build with patches
Output:
*****************************************************************************
The bugs reported by the scan-build may or may not be caused by your patches.
Please check the list and fix the bugs if they are caused by your patch.
*****************************************************************************
src/gatt-database.c:1138:10: warning: Value stored to 'bits' during its initialization is never read

        uint8_t bits[] = { BT_GATT_CHRC_CLI_FEAT_ROBUST_CACHING,
                ^~~~     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/gatt-database.c:3535:14: warning: Value stored to 'iface' during its initialization is never read
        const char *iface = g_dbus_proxy_get_interface(proxy);
                    ^~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/gatt-database.c:3536:14: warning: Value stored to 'path' during its initialization is never read
        const char *path = g_dbus_proxy_get_path(proxy);
                    ^~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3 warnings generated.

---
Regards,
Linux Bluetooth

#3 lore raw
In reply to Luiz Augusto von Dentz
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 8 lines · On Thu, 25 Aug 2022 13:07:57 -0700 you wrote:

From: Luiz Augusto von Dentz <[email protected]>

This allows application to return error codes in the range allowed by the spec:

'GATT - Section 4.9.5:

[...]

Here is the summary with links:
- [BlueZ,1/2] gatt-api: Add error code ReadValue/WriteValue

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=ea903d120680
  - [BlueZ,2/2] gatt: Parse error message
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=6b8f9fbd5bb8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html