Skip to contentSkip to conversation

client: Add support for GATT API

RFC New Luiz Augusto von Dentz 1 participant 10 messages Feb 4, 2015

Subsystems (2)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

cover client: Add support for GATT API

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

This adds basic support for GATT API, the main idea is that one can navigate into the attributes and read and write values to them.

Im sending as RFC to figure if this is the way forward and if the command are good enough, there are a couple of things missing and it might be a good idea to have command categories/subparses.

Luiz Augusto von Dentz (9):
client: Add support for GattService1
client: Add support for GattCharacteristic1
client: Add support for GattDescriptor1
client: Make commands relative to device
client: Add command list-attributes
client: Add command select-attribute
client/display: Add rl_hexdump
client: Add read command
client: Add write command

 Makefile.tools                           |   1 +
 client/display.c                         |  41 +++
 client/display.h                         |   1 +
 client/gatt.c                            | 460 +++++++++++++++++++++++++++++++
 tools/parser/ericsson.c => client/gatt.h |  39 +--
 client/main.c                            | 286 ++++++++++++++-----
 6 files changed, 735 insertions(+), 93 deletions(-)
 create mode 100644 client/gatt.c
 copy tools/parser/ericsson.c => client/gatt.h (56%)
Signature · 1 line
2.1.0
author maintainer
#1 lore raw

RFC 1/9 client: Add support for GattService1

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

This add support for GattService1 interface detection and prints when they are added or removed

---
 Makefile.tools                           |  1 +
 client/gatt.c                            | 90 ++++++++++++++++++++++++++++++++
 profiles/input/server.h => client/gatt.h |  6 +--
 client/main.c                            | 30 +++++++++++
 4 files changed, 124 insertions(+), 3 deletions(-)
 create mode 100644 client/gatt.c
 copy profiles/input/server.h => client/gatt.h (84%)
Makefile.tools +1 -0
index e42e42d..64da54f 100644
@@ -5,6 +5,7 @@ bin_PROGRAMS += client/bluetoothctl
 client_bluetoothctl_SOURCES = client/main.c \
 					client/display.h client/display.c \
 					client/agent.h client/agent.c \
+					client/gatt.h client/gatt.c \
 					monitor/uuid.h monitor/uuid.c
 client_bluetoothctl_LDADD = gdbus/libgdbus-internal.la @GLIB_LIBS@ @DBUS_LIBS@ \
 				-lreadline
client/gatt.c new file +90 -0

90 lines not on this page · raw message

client/gatt.h renamed from profiles/input/server.h +3 -3
similarity index 84%
copy from profiles/input/server.h
copy to client/gatt.h
index 74159bb..f049039 100644
@@ -2,7 +2,7 @@
  *
  *  BlueZ - Bluetooth protocol stack for Linux
  *
- *  Copyright (C) 2004-2010  Marcel Holtmann <[email protected]>
+ *  Copyright (C) 2014  Intel Corporation. All rights reserved.
  *
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -21,5 +21,5 @@
  *
  */
 
-int server_start(const bdaddr_t *src);
-void server_stop(const bdaddr_t *src);
+void gatt_add_service(GDBusProxy *proxy);
+void gatt_remove_service(GDBusProxy *proxy);
client/main.c +30 -0
index ea80ee7..ab644ba 100644
@@ -41,6 +41,7 @@
 #include "monitor/uuid.h"
 #include "agent.h"
 #include "display.h"
+#include "gatt.h"
 
 /* String display constants */
 #define COLORED_NEW	COLOR_GREEN "NEW" COLOR_OFF
@@ -284,6 +285,29 @@ static gboolean device_is_child(GDBusProxy *device, GDBusProxy *master)
 	return FALSE;
 }
 
+static gboolean service_is_child(GDBusProxy *service)
+{
+	GList *l;
+	DBusMessageIter iter;
+	const char *device, *path;
+
+	if (g_dbus_proxy_get_property(service, "Device", &iter) == FALSE)
+		return FALSE;
+
+	dbus_message_iter_get_basic(&iter, &device);
+
+	for (l = dev_list; l; l = g_list_next(l)) {
+		GDBusProxy *proxy = l->data;
+
+		path = g_dbus_proxy_get_path(proxy);
+
+		if (!strcmp(path, device))
+			return TRUE;
+	}
+
+	return FALSE;
+}
+
 static void proxy_added(GDBusProxy *proxy, void *user_data)
 {
 	const char *interface;
@@ -311,6 +335,9 @@ static void proxy_added(GDBusProxy *proxy, void *user_data)
 				agent_register(dbus_conn, agent_manager,
 							auto_register_agent);
 		}
+	} else if (!strcmp(interface, "org.bluez.GattService1")) {
+		if (service_is_child(proxy))
+			gatt_add_service(proxy);
 	}
 }
 
@@ -343,6 +370,9 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data)
 			if (auto_register_agent)
 				agent_unregister(dbus_conn, NULL);
 		}
+	} else if (!strcmp(interface, "org.bluez.GattService1")) {
+		if (service_is_child(proxy))
+			gatt_remove_service(proxy);
 	}
 }
 
Signature · 1 line
2.1.0
author maintainer
#2 lore raw

RFC 2/9 client: Add support for GattCharacteristic1

In reply to Luiz Augusto von Dentz

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

This add support for GattCharacteristici1 interface detection and prints when they are added or removed

---
 client/gatt.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 client/gatt.h |  3 +++
 client/main.c |  4 ++++
 3 files changed, 73 insertions(+)
client/gatt.c +66 -0
index 7de5c7b..8fd113f 100644
@@ -46,6 +46,7 @@
 #define COLORED_DEL	COLOR_RED "DEL" COLOR_OFF
 
 static GList *services;
+static GList *characteristics;
 
 static void print_service(GDBusProxy *proxy, const char *description)
 {
@@ -88,3 +89,68 @@ void gatt_remove_service(GDBusProxy *proxy)
 
 	print_service(proxy, COLORED_DEL);
 }
+
+static void print_characteristic(GDBusProxy *proxy, const char *description)
+{
+	DBusMessageIter iter;
+	const char *uuid, *text;
+
+	if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
+		return;
+
+	dbus_message_iter_get_basic(&iter, &uuid);
+
+	text = uuidstr_to_str(uuid);
+	if (!text)
+		text = uuid;
+
+	rl_printf("%s%s%sCharacteristic %s %s\n",
+				description ? "[" : "",
+				description ? : "",
+				description ? "] " : "",
+				g_dbus_proxy_get_path(proxy),
+				text);
+}
+
+static gboolean characteristic_is_child(GDBusProxy *characteristic)
+{
+	GList *l;
+	DBusMessageIter iter;
+	const char *service, *path;
+
+	if (!g_dbus_proxy_get_property(characteristic, "Service", &iter))
+		return FALSE;
+
+	dbus_message_iter_get_basic(&iter, &service);
+
+	for (l = services; l; l = g_list_next(l)) {
+		GDBusProxy *proxy = l->data;
+
+		path = g_dbus_proxy_get_path(proxy);
+
+		if (!strcmp(path, service))
+			return TRUE;
+	}
+
+	return FALSE;
+}
+
+void gatt_add_characteristic(GDBusProxy *proxy)
+{
+	if (!characteristic_is_child(proxy))
+		return;
+
+	characteristics = g_list_append(characteristics, proxy);
+
+	print_characteristic(proxy, COLORED_NEW);
+}
+
+void gatt_remove_characteristic(GDBusProxy *proxy)
+{
+	if (!characteristic_is_child(proxy))
+		return;
+
+	characteristics = g_list_remove(characteristics, proxy);
+
+	print_characteristic(proxy, COLORED_DEL);
+}
client/gatt.h +3 -0
index f049039..924c4d9 100644
@@ -23,3 +23,6 @@
 
 void gatt_add_service(GDBusProxy *proxy);
 void gatt_remove_service(GDBusProxy *proxy);
+
+void gatt_add_characteristic(GDBusProxy *proxy);
+void gatt_remove_characteristic(GDBusProxy *proxy);
client/main.c +4 -0
index ab644ba..3db18af 100644
@@ -338,6 +338,8 @@ static void proxy_added(GDBusProxy *proxy, void *user_data)
 	} else if (!strcmp(interface, "org.bluez.GattService1")) {
 		if (service_is_child(proxy))
 			gatt_add_service(proxy);
+	} else if (!strcmp(interface, "org.bluez.GattCharacteristic1")) {
+		gatt_add_characteristic(proxy);
 	}
 }
 
@@ -373,6 +375,8 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data)
 	} else if (!strcmp(interface, "org.bluez.GattService1")) {
 		if (service_is_child(proxy))
 			gatt_remove_service(proxy);
+	} else if (!strcmp(interface, "org.bluez.GattCharacteristic1")) {
+		gatt_remove_characteristic(proxy);
 	}
 }
 
Signature · 1 line
2.1.0
author maintainer
#3 lore raw

RFC 3/9 client: Add support for GattDescriptor1

In reply to Luiz Augusto von Dentz

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

This add support for GattDescriptor1 interface detection and prints when they are added or removed.

---
 client/gatt.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 client/gatt.h |  3 +++
 client/main.c |  4 ++++
 3 files changed, 73 insertions(+)
client/gatt.c +66 -0
index 8fd113f..47785a8 100644
@@ -47,6 +47,7 @@
 
 static GList *services;
 static GList *characteristics;
+static GList *descriptors;
 
 static void print_service(GDBusProxy *proxy, const char *description)
 {
@@ -154,3 +155,68 @@ void gatt_remove_characteristic(GDBusProxy *proxy)
 
 	print_characteristic(proxy, COLORED_DEL);
 }
+
+static void print_descriptor(GDBusProxy *proxy, const char *description)
+{
+	DBusMessageIter iter;
+	const char *uuid, *text;
+
+	if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
+		return;
+
+	dbus_message_iter_get_basic(&iter, &uuid);
+
+	text = uuidstr_to_str(uuid);
+	if (!text)
+		text = uuid;
+
+	rl_printf("%s%s%sDescriptor %s %s\n",
+				description ? "[" : "",
+				description ? : "",
+				description ? "] " : "",
+				g_dbus_proxy_get_path(proxy),
+				text);
+}
+
+static gboolean descriptor_is_child(GDBusProxy *characteristic)
+{
+	GList *l;
+	DBusMessageIter iter;
+	const char *service, *path;
+
+	if (!g_dbus_proxy_get_property(characteristic, "Characteristic", &iter))
+		return FALSE;
+
+	dbus_message_iter_get_basic(&iter, &service);
+
+	for (l = characteristics; l; l = g_list_next(l)) {
+		GDBusProxy *proxy = l->data;
+
+		path = g_dbus_proxy_get_path(proxy);
+
+		if (!strcmp(path, service))
+			return TRUE;
+	}
+
+	return FALSE;
+}
+
+void gatt_add_descriptor(GDBusProxy *proxy)
+{
+	if (!descriptor_is_child(proxy))
+		return;
+
+	descriptors = g_list_append(descriptors, proxy);
+
+	print_descriptor(proxy, COLORED_NEW);
+}
+
+void gatt_remove_descriptor(GDBusProxy *proxy)
+{
+	if (!descriptor_is_child(proxy))
+		return;
+
+	descriptors = g_list_remove(descriptors, proxy);
+
+	print_descriptor(proxy, COLORED_DEL);
+}
client/gatt.h +3 -0
index 924c4d9..8b30668 100644
@@ -26,3 +26,6 @@ void gatt_remove_service(GDBusProxy *proxy);
 
 void gatt_add_characteristic(GDBusProxy *proxy);
 void gatt_remove_characteristic(GDBusProxy *proxy);
+
+void gatt_add_descriptor(GDBusProxy *proxy);
+void gatt_remove_descriptor(GDBusProxy *proxy);
client/main.c +4 -0
index 3db18af..0590266 100644
@@ -340,6 +340,8 @@ static void proxy_added(GDBusProxy *proxy, void *user_data)
 			gatt_add_service(proxy);
 	} else if (!strcmp(interface, "org.bluez.GattCharacteristic1")) {
 		gatt_add_characteristic(proxy);
+	} else if (!strcmp(interface, "org.bluez.GattDescriptor1")) {
+		gatt_add_descriptor(proxy);
 	}
 }
 
@@ -377,6 +379,8 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data)
 			gatt_remove_service(proxy);
 	} else if (!strcmp(interface, "org.bluez.GattCharacteristic1")) {
 		gatt_remove_characteristic(proxy);
+	} else if (!strcmp(interface, "org.bluez.GattDescriptor1")) {
+		gatt_remove_descriptor(proxy);
 	}
 }
 
Signature · 1 line
2.1.0
author maintainer
#4 lore raw

RFC 4/9 client: Make commands relative to device

In reply to Luiz Augusto von Dentz

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

In case a device is connected make it the default and print to the prompt, so the user don't have to type in its address.

---
 client/main.c | 153 ++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 85 insertions(+), 68 deletions(-)
client/main.c +85 -68
index 0590266..72223d5 100644
@@ -58,6 +58,7 @@ static GDBusProxy *agent_manager;
 static char *auto_register_agent = NULL;
 
 static GDBusProxy *default_ctrl;
+static GDBusProxy *default_dev;
 static GList *ctrl_list;
 static GList *dev_list;
 
@@ -345,6 +346,27 @@ static void proxy_added(GDBusProxy *proxy, void *user_data)
 	}
 }
 
+static void set_default_device(GDBusProxy *proxy)
+{
+	char *desc = NULL;
+	DBusMessageIter iter;
+
+	default_dev = proxy;
+
+	if (!g_dbus_proxy_get_property(proxy, "Alias", &iter)) {
+		if (!g_dbus_proxy_get_property(proxy, "Address", &iter))
+			goto done;
+	}
+
+	dbus_message_iter_get_basic(&iter, &desc);
+	desc = g_strdup_printf(COLOR_BLUE "[%s]" COLOR_OFF "# ", desc);
+
+done:
+	rl_set_prompt(desc ? desc : PROMPT_ON);
+	rl_redisplay();
+	g_free(desc);
+}
+
 static void proxy_removed(GDBusProxy *proxy, void *user_data)
 {
 	const char *interface;
@@ -356,6 +378,9 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data)
 			dev_list = g_list_remove(dev_list, proxy);
 
 			print_device(proxy, COLORED_DEL);
+
+			if (default_dev == proxy)
+				set_default_device(NULL);
 		}
 	} else if (!strcmp(interface, "org.bluez.Adapter1")) {
 		ctrl_list = g_list_remove(ctrl_list, proxy);
@@ -364,6 +389,7 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data)
 
 		if (default_ctrl == proxy) {
 			default_ctrl = NULL;
+			set_default_device(NULL);
 
 			g_list_free(dev_list);
 			dev_list = NULL;
@@ -407,6 +433,17 @@ static void property_changed(GDBusProxy *proxy, const char *name,
 			} else
 				str = g_strdup("");
 
+			if (strcmp(name, "Connected") == 0) {
+				dbus_bool_t connected;
+
+				dbus_message_iter_get_basic(iter, &connected);
+
+				if (connected && default_dev == NULL)
+					set_default_device(proxy);
+				else if (!connected && default_dev == proxy)
+					set_default_device(NULL);
+			}
+
 			print_iter(str, name, iter);
 			g_free(str);
 		}
@@ -810,23 +847,36 @@ static void cmd_scan(const char *arg)
 	}
 }
 
-static void cmd_info(const char *arg)
+static struct GDBusProxy *find_device(const char *arg)
 {
 	GDBusProxy *proxy;
-	DBusMessageIter iter;
-	const char *address;
 
 	if (!arg || !strlen(arg)) {
+		if (default_dev)
+			return default_dev;
 		rl_printf("Missing device address argument\n");
-		return;
+		return NULL;
 	}
 
 	proxy = find_proxy_by_address(dev_list, arg);
 	if (!proxy) {
 		rl_printf("Device %s not available\n", arg);
-		return;
+		return NULL;
 	}
 
+	return proxy;
+}
+
+static void cmd_info(const char *arg)
+{
+	GDBusProxy *proxy;
+	DBusMessageIter iter;
+	const char *address;
+
+	proxy = find_device(arg);
+	if (!proxy)
+		return;
+
 	if (g_dbus_proxy_get_property(proxy, "Address", &iter) == FALSE)
 		return;
 
@@ -866,16 +916,9 @@ static void cmd_pair(const char *arg)
 {
 	GDBusProxy *proxy;
 
-	if (!arg || !strlen(arg)) {
-		rl_printf("Missing device address argument\n");
+	proxy = find_device(arg);
+	if (!proxy)
 		return;
-	}
-
-	proxy = find_proxy_by_address(dev_list, arg);
-	if (!proxy) {
-		rl_printf("Device %s not available\n", arg);
-		return;
-	}
 
 	if (g_dbus_proxy_method_call(proxy, "Pair", NULL, pair_reply,
 							NULL, NULL) == FALSE) {
@@ -892,16 +935,9 @@ static void cmd_trust(const char *arg)
 	dbus_bool_t trusted;
 	char *str;
 
-	if (!arg || !strlen(arg)) {
-		rl_printf("Missing device address argument\n");
-		return;
-	}
-
-	proxy = find_proxy_by_address(dev_list, arg);
-	if (!proxy) {
-		rl_printf("Device %s not available\n", arg);
+	proxy = find_device(arg);
+	if (!proxy)
 		return;
-	}
 
 	trusted = TRUE;
 
@@ -921,16 +957,9 @@ static void cmd_untrust(const char *arg)
 	dbus_bool_t trusted;
 	char *str;
 
-	if (!arg || !strlen(arg)) {
-		rl_printf("Missing device address argument\n");
-		return;
-	}
-
-	proxy = find_proxy_by_address(dev_list, arg);
-	if (!proxy) {
-		rl_printf("Device %s not available\n", arg);
+	proxy = find_device(arg);
+	if (!proxy)
 		return;
-	}
 
 	trusted = FALSE;
 
@@ -950,16 +979,9 @@ static void cmd_block(const char *arg)
 	dbus_bool_t blocked;
 	char *str;
 
-	if (!arg || !strlen(arg)) {
-		rl_printf("Missing device address argument\n");
-		return;
-	}
-
-	proxy = find_proxy_by_address(dev_list, arg);
-	if (!proxy) {
-		rl_printf("Device %s not available\n", arg);
+	proxy = find_device(arg);
+	if (!proxy)
 		return;
-	}
 
 	blocked = TRUE;
 
@@ -979,16 +1001,9 @@ static void cmd_unblock(const char *arg)
 	dbus_bool_t blocked;
 	char *str;
 
-	if (!arg || !strlen(arg)) {
-		rl_printf("Missing device address argument\n");
-		return;
-	}
-
-	proxy = find_proxy_by_address(dev_list, arg);
-	if (!proxy) {
-		rl_printf("Device %s not available\n", arg);
+	proxy = find_device(arg);
+	if (!proxy)
 		return;
-	}
 
 	blocked = FALSE;
 
@@ -1057,6 +1072,7 @@ static void cmd_remove(const char *arg)
 
 static void connect_reply(DBusMessage *message, void *user_data)
 {
+	GDBusProxy *proxy = user_data;
 	DBusError error;
 
 	dbus_error_init(&error);
@@ -1068,6 +1084,8 @@ static void connect_reply(DBusMessage *message, void *user_data)
 	}
 
 	rl_printf("Connection successful\n");
+
+	set_default_device(proxy);
 }
 
 static void cmd_connect(const char *arg)
@@ -1086,7 +1104,7 @@ static void cmd_connect(const char *arg)
 	}
 
 	if (g_dbus_proxy_method_call(proxy, "Connect", NULL, connect_reply,
-							NULL, NULL) == FALSE) {
+							proxy, NULL) == FALSE) {
 		rl_printf("Failed to connect\n");
 		return;
 	}
@@ -1096,6 +1114,7 @@ static void cmd_connect(const char *arg)
 
 static void disconn_reply(DBusMessage *message, void *user_data)
 {
+	GDBusProxy *proxy = user_data;
 	DBusError error;
 
 	dbus_error_init(&error);
@@ -1107,25 +1126,23 @@ static void disconn_reply(DBusMessage *message, void *user_data)
 	}
 
 	rl_printf("Successful disconnected\n");
+
+	if (proxy != default_dev)
+		return;
+
+	set_default_device(NULL);
 }
 
 static void cmd_disconn(const char *arg)
 {
 	GDBusProxy *proxy;
 
-	if (!arg || !strlen(arg)) {
-		rl_printf("Missing device address argument\n");
+	proxy = find_device(arg);
+	if (!proxy)
 		return;
-	}
-
-	proxy = find_proxy_by_address(dev_list, arg);
-	if (!proxy) {
-		rl_printf("Device %s not available\n", arg);
-		return;
-	}
 
 	if (g_dbus_proxy_method_call(proxy, "Disconnect", NULL, disconn_reply,
-							NULL, NULL) == FALSE) {
+							proxy, NULL) == FALSE) {
 		rl_printf("Failed to disconnect\n");
 		return;
 	}
@@ -1233,23 +1250,23 @@ static const struct {
 	{ "default-agent",NULL,       cmd_default_agent,
 				"Set agent as the default one" },
 	{ "scan",         "<on/off>", cmd_scan, "Scan for devices" },
-	{ "info",         "<dev>",    cmd_info, "Device information",
+	{ "info",         "[dev]",    cmd_info, "Device information",
 							dev_generator },
-	{ "pair",         "<dev>",    cmd_pair, "Pair with device",
+	{ "pair",         "[dev]",    cmd_pair, "Pair with device",
 							dev_generator },
-	{ "trust",        "<dev>",    cmd_trust, "Trust device",
+	{ "trust",        "[dev]",    cmd_trust, "Trust device",
 							dev_generator },
-	{ "untrust",      "<dev>",    cmd_untrust, "Untrust device",
+	{ "untrust",      "[dev]",    cmd_untrust, "Untrust device",
 							dev_generator },
-	{ "block",        "<dev>",    cmd_block, "Block device",
+	{ "block",        "[dev]",    cmd_block, "Block device",
 								dev_generator },
-	{ "unblock",      "<dev>",    cmd_unblock, "Unblock device",
+	{ "unblock",      "[dev]",    cmd_unblock, "Unblock device",
 								dev_generator },
 	{ "remove",       "<dev>",    cmd_remove, "Remove device",
 							dev_generator },
 	{ "connect",      "<dev>",    cmd_connect, "Connect device",
 							dev_generator },
-	{ "disconnect",   "<dev>",    cmd_disconn, "Disconnect device",
+	{ "disconnect",   "[dev]",    cmd_disconn, "Disconnect device",
 							dev_generator },
 	{ "version",      NULL,       cmd_version, "Display version" },
 	{ "quit",         NULL,       cmd_quit, "Quit program" },
Signature · 1 line
2.1.0
author maintainer
#5 lore raw

RFC 5/9 client: Add command list-attributes

In reply to Luiz Augusto von Dentz

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

This command can be used to list service attributes of a device.

---
 client/gatt.c | 30 ++++++++++++++++++++++++++++++
 client/gatt.h |  2 ++
 client/main.c | 13 +++++++++++++
 3 files changed, 45 insertions(+)
client/gatt.c +30 -0
index 47785a8..723b6d6 100644
@@ -220,3 +220,33 @@ void gatt_remove_descriptor(GDBusProxy *proxy)
 
 	print_descriptor(proxy, COLORED_DEL);
 }
+
+static void list_attributes(const char *path, GList *source)
+{
+	GList *l;
+
+	for (l = source; l; l = g_list_next(l)) {
+		GDBusProxy *proxy = l->data;
+		const char *proxy_path;
+
+		proxy_path = g_dbus_proxy_get_path(proxy);
+
+		if (!g_str_has_prefix(proxy_path, path))
+			continue;
+
+		if (source == services)
+			print_service(proxy, NULL);
+		else if (source == characteristics)
+			print_characteristic(proxy, NULL);
+		else if (source == descriptors)
+			print_descriptor(proxy, NULL);
+
+		list_attributes(proxy_path, source == services ? characteristics
+								: descriptors);
+	}
+}
+
+void gatt_list_attributes(const char *path)
+{
+	list_attributes(path, services);
+}
client/gatt.h +2 -0
index 8b30668..5785073 100644
@@ -29,3 +29,5 @@ void gatt_remove_characteristic(GDBusProxy *proxy);
 
 void gatt_add_descriptor(GDBusProxy *proxy);
 void gatt_remove_descriptor(GDBusProxy *proxy);
+
+void gatt_list_attributes(const char *device);
client/main.c +13 -0
index 72223d5..7cdb19a 100644
@@ -1150,6 +1150,17 @@ static void cmd_disconn(const char *arg)
 	rl_printf("Attempting to disconnect from %s\n", arg);
 }
 
+static void cmd_list_attributes(const char *arg)
+{
+	GDBusProxy *proxy;
+
+	proxy = find_device(arg);
+	if (!proxy)
+		return;
+
+	gatt_list_attributes(g_dbus_proxy_get_path(proxy));
+}
+
 static void cmd_version(const char *arg)
 {
 	rl_printf("Version %s\n", VERSION);
@@ -1268,6 +1279,8 @@ static const struct {
 							dev_generator },
 	{ "disconnect",   "[dev]",    cmd_disconn, "Disconnect device",
 							dev_generator },
+	{ "list-attributes", "[dev]", cmd_list_attributes, "List attributes",
+							dev_generator },
 	{ "version",      NULL,       cmd_version, "Display version" },
 	{ "quit",         NULL,       cmd_quit, "Quit program" },
 	{ "exit",         NULL,       cmd_quit },
Signature · 1 line
2.1.0
author maintainer
#6 lore raw

RFC 6/9 client: Add command select-attribute

In reply to Luiz Augusto von Dentz

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

Command select-attribute can be used to select a service attribute.

---
 client/gatt.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 client/gatt.h |  2 ++
 client/main.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 144 insertions(+), 15 deletions(-)
client/gatt.c +75 -6
index 723b6d6..f2830a3 100644
@@ -234,15 +234,14 @@ static void list_attributes(const char *path, GList *source)
 		if (!g_str_has_prefix(proxy_path, path))
 			continue;
 
-		if (source == services)
+		if (source == services) {
 			print_service(proxy, NULL);
-		else if (source == characteristics)
+			list_attributes(proxy_path, characteristics);
+		} else if (source == characteristics) {
 			print_characteristic(proxy, NULL);
-		else if (source == descriptors)
+			list_attributes(proxy_path, descriptors);
+		} else if (source == descriptors)
 			print_descriptor(proxy, NULL);
-
-		list_attributes(proxy_path, source == services ? characteristics
-								: descriptors);
 	}
 }
 
@@ -250,3 +249,73 @@ void gatt_list_attributes(const char *path)
 {
 	list_attributes(path, services);
 }
+
+static GDBusProxy *select_proxy(const char *path, GList *source)
+{
+	GList *l;
+
+	for (l = source; l; l = g_list_next(l)) {
+		GDBusProxy *proxy = l->data;
+
+		if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
+			return proxy;
+	}
+
+	return NULL;
+}
+
+GDBusProxy *gatt_select_attribute(const char *path)
+{
+	GDBusProxy *proxy;
+
+	proxy = select_proxy(path, services);
+	if (proxy)
+		return proxy;
+
+	proxy = select_proxy(path, characteristics);
+	if (proxy)
+		return proxy;
+
+	return select_proxy(path, descriptors);
+}
+
+static char *attribute_generator(const char *text, int state, GList *source)
+{
+	static int index, len;
+	GList *list;
+
+	if (!state) {
+		index = 0;
+		len = strlen(text);
+	}
+
+	for (list = g_list_nth(source, index); list;
+						list = g_list_next(list)) {
+		GDBusProxy *proxy = list->data;
+		const char *path;
+
+		index++;
+
+		path = g_dbus_proxy_get_path(proxy);
+
+		if (!strncmp(path, text, len))
+			return strdup(path);
+        }
+
+	return NULL;
+}
+
+char *gatt_attribute_generator(const char *text, int state)
+{
+	char *ret;
+
+	ret = attribute_generator(text, state, services);
+	if (ret)
+		return ret;
+
+	ret = attribute_generator(text, state, characteristics);
+	if (ret)
+		return ret;
+
+	return attribute_generator(text, state, descriptors);
+}
client/gatt.h +2 -0
index 5785073..bb7cb1c 100644
@@ -31,3 +31,5 @@ void gatt_add_descriptor(GDBusProxy *proxy);
 void gatt_remove_descriptor(GDBusProxy *proxy);
 
 void gatt_list_attributes(const char *device);
+GDBusProxy *gatt_select_attribute(const char *path);
+char *gatt_attribute_generator(const char *text, int state);
client/main.c +67 -9

166 lines not on this page · raw message

Signature · 1 line
2.1.0
author maintainer
#7 lore raw

RFC 7/9 client/display: Add rl_hexdump

In reply to Luiz Augusto von Dentz

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

---
 client/display.c | 41 +++++++++++++++++++++++++++++++++++++++++
 client/display.h |  1 +
 2 files changed, 42 insertions(+)
client/display.c +41 -0
index 0f212b6..619973c 100644
@@ -62,3 +62,44 @@ void rl_printf(const char *fmt, ...)
 		free(saved_line);
 	}
 }
+
+void rl_hexdump(const unsigned char *buf, size_t len)
+{
+	static const char hexdigits[] = "0123456789abcdef";
+	char str[68];
+	size_t i;
+
+	if (!len)
+		return;
+
+	str[0] = ' ';
+
+	for (i = 0; i < len; i++) {
+		str[((i % 16) * 3) + 1] = ' ';
+		str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
+		str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
+		str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
+
+		if ((i + 1) % 16 == 0) {
+			str[49] = ' ';
+			str[50] = ' ';
+			str[67] = '\0';
+			rl_printf("%s\n", str);
+			str[0] = ' ';
+		}
+	}
+
+	if (i % 16 > 0) {
+		size_t j;
+		for (j = (i % 16); j < 16; j++) {
+			str[(j * 3) + 1] = ' ';
+			str[(j * 3) + 2] = ' ';
+			str[(j * 3) + 3] = ' ';
+			str[j + 51] = ' ';
+		}
+		str[49] = ' ';
+		str[50] = ' ';
+		str[67] = '\0';
+		rl_printf("%s\n", str);
+	}
+}
client/display.h +1 -0
index 91a0be9..88dbbd0 100644
@@ -30,3 +30,4 @@
 #define COLOR_BOLDWHITE	"\x1B[1;37m"
 
 void rl_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
+void rl_hexdump(const unsigned char *buf, size_t len);
Signature · 1 line
2.1.0
author maintainer
#8 lore raw

RFC 8/9 client: Add read command

In reply to Luiz Augusto von Dentz

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

This command can be used to read attributes, it only works if an attribute has been selected with select-attribute.

---
 client/gatt.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 client/gatt.h |  2 ++
 client/main.c | 11 +++++++++++
 3 files changed, 72 insertions(+)
client/gatt.c +59 -0
index f2830a3..56a9241 100644
@@ -319,3 +319,62 @@ char *gatt_attribute_generator(const char *text, int state)
 
 	return attribute_generator(text, state, descriptors);
 }
+
+static void read_reply(DBusMessage *message, void *user_data)
+{
+	DBusError error;
+	DBusMessageIter iter, array;
+	uint8_t *value;
+	int len;
+
+	dbus_error_init(&error);
+
+	if (dbus_set_error_from_message(&error, message) == TRUE) {
+		rl_printf("Failed to read: %s\n", error.name);
+		dbus_error_free(&error);
+		return;
+	}
+
+	dbus_message_iter_init(message, &iter);
+
+	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
+		rl_printf("Invalid response to read\n");
+		return;
+	}
+
+	dbus_message_iter_recurse(&iter, &array);
+	dbus_message_iter_get_fixed_array(&array, &value, &len);
+
+	if (len < 0) {
+		rl_printf("Unable to parse value\n");
+		return;
+	}
+
+	rl_hexdump(value, len);
+}
+
+static void read_attribute(GDBusProxy *proxy)
+{
+	if (g_dbus_proxy_method_call(proxy, "ReadValue", NULL, read_reply,
+							NULL, NULL) == FALSE) {
+		rl_printf("Failed to read\n");
+		return;
+	}
+
+	rl_printf("Attempting to read %s\n", g_dbus_proxy_get_path(proxy));
+}
+
+void gatt_read_attribute(GDBusProxy *proxy)
+{
+	const char *iface;
+
+	iface = g_dbus_proxy_get_interface(proxy);
+	if (!strcmp(iface, "org.bluez.GattCharacteristic1") ||
+				!strcmp(iface, "org.bluez.GattDescriptor1")) {
+		read_attribute(proxy);
+		return;
+	}
+
+	rl_printf("Unable to read attribute %s\n",
+						g_dbus_proxy_get_path(proxy));
+}
client/gatt.h +2 -0
index bb7cb1c..ac18a2a 100644
@@ -33,3 +33,5 @@ void gatt_remove_descriptor(GDBusProxy *proxy);
 void gatt_list_attributes(const char *device);
 GDBusProxy *gatt_select_attribute(const char *path);
 char *gatt_attribute_generator(const char *text, int state);
+
+void gatt_read_attribute(GDBusProxy *proxy);
client/main.c +11 -0
index 1481694..e0f35e2 100644
@@ -1212,6 +1212,16 @@ static void cmd_select_attribute(const char *arg)
 		set_default_attribute(proxy);
 }
 
+static void cmd_read(const char *arg)
+{
+	if (!default_attr) {
+		rl_printf("No attribute selected\n");
+		return;
+	}
+
+	gatt_read_attribute(default_attr);
+}
+
 static void cmd_version(const char *arg)
 {
 	rl_printf("Version %s\n", VERSION);
@@ -1339,6 +1349,7 @@ static const struct {
 							dev_generator },
 	{ "select-attribute", "<attribute>",  cmd_select_attribute,
 				"Select attribute", attribute_generator },
+	{ "read",         NULL,       cmd_read, "Read attribute value" },
 	{ "version",      NULL,       cmd_version, "Display version" },
 	{ "quit",         NULL,       cmd_quit, "Quit program" },
 	{ "exit",         NULL,       cmd_quit },
Signature · 1 line
2.1.0
author maintainer
#9 lore raw

RFC 9/9 client: Add write command

In reply to Luiz Augusto von Dentz

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

This command can be used to write attributes, it only works if an attribute has been selected with select-attribute.

The data argument should be passed hexdump format, %02x separated by spaces, which is the same format used by read command.

---
 client/gatt.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 client/gatt.h |  1 +
 client/main.c | 17 +++++++++++++
 3 files changed, 98 insertions(+)
client/gatt.c +80 -0
index 56a9241..35150df 100644
@@ -30,6 +30,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <sys/uio.h>
 
 #include <readline/readline.h>
 #include <readline/history.h>
@@ -378,3 +379,82 @@ void gatt_read_attribute(GDBusProxy *proxy)
 	rl_printf("Unable to read attribute %s\n",
 						g_dbus_proxy_get_path(proxy));
 }
+
+static void write_reply(DBusMessage *message, void *user_data)
+{
+	DBusError error;
+
+	dbus_error_init(&error);
+
+	if (dbus_set_error_from_message(&error, message) == TRUE) {
+		rl_printf("Failed to write: %s\n", error.name);
+		dbus_error_free(&error);
+		return;
+	}
+}
+
+static void write_setup(DBusMessageIter *iter, void *user_data)
+{
+	struct iovec *iov = user_data;
+	DBusMessageIter array;
+
+	dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "y", &array);
+	dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
+						&iov->iov_base, iov->iov_len);
+	dbus_message_iter_close_container(iter, &array);
+}
+
+static void write_attribute(GDBusProxy *proxy, char *arg)
+{
+	struct iovec iov;
+	uint8_t value[512];
+	char *entry;
+	int i;
+
+	for (i = 0; (entry = strsep(&arg, " \t")) != NULL; i++) {
+		long int val;
+		char *endptr = NULL;
+
+		if (*entry == '\0')
+			continue;
+
+		if (i > 512) {
+			rl_printf("Too much data\n");
+			return;
+		}
+
+		val = strtol(entry, &endptr, 0);
+		if (!endptr || *endptr != '\0' || val > UINT8_MAX) {
+			rl_printf("Invalid value at index %d\n", i);
+			return;
+		}
+
+		value[i] = val;
+	}
+
+	iov.iov_base = value;
+	iov.iov_len = i;
+
+	if (g_dbus_proxy_method_call(proxy, "WriteValue", write_setup,
+					write_reply, &iov, NULL) == FALSE) {
+		rl_printf("Failed to write\n");
+		return;
+	}
+
+	rl_printf("Attempting to write %s\n", g_dbus_proxy_get_path(proxy));
+}
+
+void gatt_write_attribute(GDBusProxy *proxy, const char *arg)
+{
+	const char *iface;
+
+	iface = g_dbus_proxy_get_interface(proxy);
+	if (!strcmp(iface, "org.bluez.GattCharacteristic1") ||
+				!strcmp(iface, "org.bluez.GattDescriptor1")) {
+		write_attribute(proxy, (char *) arg);
+		return;
+	}
+
+	rl_printf("Unable to write attribute %s\n",
+						g_dbus_proxy_get_path(proxy));
+}
client/gatt.h +1 -0
index ac18a2a..b99f0f0 100644
@@ -35,3 +35,4 @@ GDBusProxy *gatt_select_attribute(const char *path);
 char *gatt_attribute_generator(const char *text, int state);
 
 void gatt_read_attribute(GDBusProxy *proxy);
+void gatt_write_attribute(GDBusProxy *proxy, const char *arg);
client/main.c +17 -0
index e0f35e2..7184bde 100644
@@ -1222,6 +1222,21 @@ static void cmd_read(const char *arg)
 	gatt_read_attribute(default_attr);
 }
 
+static void cmd_write(const char *arg)
+{
+	if (!arg || !strlen(arg)) {
+		rl_printf("Missing data argument\n");
+		return;
+	}
+
+	if (!default_attr) {
+		rl_printf("No attribute selected\n");
+		return;
+	}
+
+	gatt_write_attribute(default_attr, arg);
+}
+
 static void cmd_version(const char *arg)
 {
 	rl_printf("Version %s\n", VERSION);
@@ -1350,6 +1365,8 @@ static const struct {
 	{ "select-attribute", "<attribute>",  cmd_select_attribute,
 				"Select attribute", attribute_generator },
 	{ "read",         NULL,       cmd_read, "Read attribute value" },
+	{ "write",        "<data=[xx xx ...]>", cmd_write,
+						"Write attribute value" },
 	{ "version",      NULL,       cmd_version, "Display version" },
 	{ "quit",         NULL,       cmd_quit, "Quit program" },
 	{ "exit",         NULL,       cmd_quit },
Signature · 1 line
2.1.0