diff mbox series

[BlueZ,8/9] btgatt-client: Add 'bytes <value> <count>' option

Message ID 20230323103835.571037-9-simon.mikuda@streamunlimited.com
State New
Headers show
Series gatt-db fix + btgatt-client features | expand

Commit Message

Simon Mikuda March 23, 2023, 10:38 a.m. UTC
This command is available for write requests command and it can specify
bytes count with specific value.

e.g. command: write-value 0x0001 bytes 0 100
will write 100 zero bytes to handle 0x0001
---
 tools/btgatt-client.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
diff mbox series

Patch

diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c
index c4311d067..b7a23ac9b 100644
--- a/tools/btgatt-client.c
+++ b/tools/btgatt-client.c
@@ -669,6 +669,23 @@  static uint8_t *read_bytes(char **argv, int *length)
 	int i, byte;
 	uint8_t *value;
 	char *endptr = NULL;
+	bool use_bytes = false;
+
+	if (*length == 3 && !strcmp(argv[i], "bytes")) {
+		byte = strtol(argv[i+1], &endptr, 0);
+		if (endptr == argv[i+1] || *endptr != '\0'
+			|| errno == ERANGE || byte < 0 || byte > 255) {
+			error("Invalid bytes value: %s", argv[i+1]);
+			return NULL;
+		}
+		*length = strtol(argv[i+2], &endptr, 0);
+		if (endptr == argv[i+2] || *endptr != '\0'
+			|| errno == ERANGE) {
+			error("Invalid bytes count: %s", argv[i+2]);
+			return NULL;
+		}
+		use_bytes = true;
+	}
 
 	if (*length <= 0) {
 		error("Nothing to write");
@@ -685,6 +702,11 @@  static uint8_t *read_bytes(char **argv, int *length)
 		return NULL;
 	}
 
+	if (use_bytes) {
+		memset(value, byte, *length);
+		return value;
+	}
+
 	for (i = 0; i < *length; i++) {
 		byte = strtol(argv[i], &endptr, 0);
 		if (endptr == argv[i] || *endptr != '\0'
@@ -1194,22 +1216,28 @@  static const struct bt_shell_menu main_menu = {
 		"Options:\n"
 			"\t-w, --without-response\tWrite without response\n"
 			"\t-s, --signed-write\tSigned write command\n"
+			"\tbytes <value> <count>\tWrite specified number of bytes with value\n"
 		"e.g.:\n"
 			"\twrite-value 0x0001 00 01 00"
+			"\twrite-value 0x0001 bytes 0 100"
 	},
 	{ "write-long-value", "[-r] <value_handle> <offset>",
 		cmd_write_long_value, "Write long characteristic or descriptor value\n"
 		"Options:\n"
 			"\t-r, --reliable-write\tReliable write\n"
+			"\tbytes <value> <count>\tWrite specified number of bytes with value\n"
 		"e.g.:\n"
 			"\twrite-long-value 0x0001 0 00 01 00"
+			"\twrite-long-value 0x0001 0 bytes 0 100"
 	},
 	{ "write-prepare", " [options...] <value_handle> <value>",
 		cmd_write_prepare, "Write prepare characteristic or descriptor value\n"
 		"Options:\n"
 			"\t-s, --session-id\tSession id\n"
+			"\tbytes <value> <count>\tWrite specified number of bytes with value\n"
 		"e.g.:\n"
 			"\twrite-prepare -s 1 0x0001 00 01 00"
+			"\twrite-prepare -s 1 0x0001 bytes 0 100"
 	},
 	{ "write-execute", " <session_id> <execute>",
 		cmd_write_execute, "Execute already prepared write" },