Diff between 52392d84055ff8e95148254d816077d5bb26b426 and fcbf38a99c42e9209096b0e81567a45bd1d2dd21

Changed Files

File Additions Deletions Status
client/gatt.c +47 -0 modified
client/gatt.h +1 -0 modified
client/main.c +16 -0 modified

Full Patch

diff --git a/client/gatt.c b/client/gatt.c
index 207e049..3785452 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -465,3 +465,50 @@ void gatt_write_attribute(GDBusProxy *proxy, const char *arg)
 	rl_printf("Unable to write attribute %s\n",
 						g_dbus_proxy_get_path(proxy));
 }
+
+static void notify_reply(DBusMessage *message, void *user_data)
+{
+	bool enable = GPOINTER_TO_UINT(user_data);
+	DBusError error;
+
+	dbus_error_init(&error);
+
+	if (dbus_set_error_from_message(&error, message) == TRUE) {
+		rl_printf("Failed to %s notify: %s\n",
+				enable ? "start" : "stop", error.name);
+		dbus_error_free(&error);
+		return;
+	}
+
+	rl_printf("Notify %s\n", enable == TRUE ? "started" : "stopped");
+}
+
+static void notify_attribute(GDBusProxy *proxy, bool enable)
+{
+	const char *method;
+
+	if (enable == TRUE)
+		method = "StartNotify";
+	else
+		method = "StopNotify";
+
+	if (g_dbus_proxy_method_call(proxy, method, NULL, notify_reply,
+				GUINT_TO_POINTER(enable), NULL) == FALSE) {
+		rl_printf("Failed to %s notify\n", enable ? "start" : "stop");
+		return;
+	}
+}
+
+void gatt_notify_attribute(GDBusProxy *proxy, bool enable)
+{
+	const char *iface;
+
+	iface = g_dbus_proxy_get_interface(proxy);
+	if (!strcmp(iface, "org.bluez.GattCharacteristic1")) {
+		notify_attribute(proxy, enable);
+		return;
+	}
+
+	rl_printf("Unable to notify attribute %s\n",
+						g_dbus_proxy_get_path(proxy));
+}
diff --git a/client/gatt.h b/client/gatt.h
index b99f0f0..effee5e 100644
--- a/client/gatt.h
+++ b/client/gatt.h
@@ -36,3 +36,4 @@ char *gatt_attribute_generator(const char *text, int state);
 
 void gatt_read_attribute(GDBusProxy *proxy);
 void gatt_write_attribute(GDBusProxy *proxy, const char *arg);
+void gatt_notify_attribute(GDBusProxy *proxy, bool enable);
diff --git a/client/main.c b/client/main.c
index 56d4750..57b1201 100644
--- a/client/main.c
+++ b/client/main.c
@@ -1301,6 +1301,21 @@ static void cmd_write(const char *arg)
 	gatt_write_attribute(default_attr, arg);
 }
 
+static void cmd_notify(const char *arg)
+{
+	dbus_bool_t enable;
+
+	if (parse_argument_on_off(arg, &enable) == FALSE)
+		return;
+
+	if (!default_attr) {
+		rl_printf("No attribute selected\n");
+		return;
+	}
+
+	gatt_notify_attribute(default_attr, enable ? true : false);
+}
+
 static void cmd_version(const char *arg)
 {
 	rl_printf("Version %s\n", VERSION);
@@ -1433,6 +1448,7 @@ static const struct {
 	{ "read",         NULL,       cmd_read, "Read attribute value" },
 	{ "write",        "<data=[xx xx ...]>", cmd_write,
 						"Write attribute value" },
+	{ "notify",       "<on/off>", cmd_notify, "Notify attribute value" },
 	{ "version",      NULL,       cmd_version, "Display version" },
 	{ "quit",         NULL,       cmd_quit, "Quit program" },
 	{ "exit",         NULL,       cmd_quit },