client: Proxy calls to StartNotify/StopNotify
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Tue, 12 Feb 2019 11:23:06 +0000 (13:23 +0200)
committerhimanshu <h.himanshu@samsung.com>
Tue, 11 Feb 2020 08:57:47 +0000 (14:27 +0530)
This uses the proxies created by clone command to forward the
subscriptions to the cloned services.

Change-Id: I04ba4be9b9e6208afe2ea1cbcff7642e2fa3c053
Signed-off-by: himanshu <h.himanshu@samsung.com>
client/gatt.c

index 12c7519..f3524ed 100755 (executable)
@@ -1706,6 +1706,9 @@ static gboolean chrc_write_acquired_exists(const GDBusPropertyTable *property,
        struct chrc *chrc = data;
        int i;
 
+       if (chrc->proxy)
+               return FALSE;
+
        for (i = 0; chrc->flags[i]; i++) {
                if (!strcmp("write-without-response", chrc->flags[i]))
                        return TRUE;
@@ -1733,6 +1736,9 @@ static gboolean chrc_notify_acquired_exists(const GDBusPropertyTable *property,
        struct chrc *chrc = data;
        int i;
 
+       if (chrc->proxy)
+               return FALSE;
+
        for (i = 0; chrc->flags[i]; i++) {
                if (!strcmp("notify", chrc->flags[i]))
                        return TRUE;
@@ -2337,14 +2343,79 @@ static DBusMessage *chrc_acquire_notify(DBusConnection *conn, DBusMessage *msg,
        return reply;
 }
 
+struct notify_attribute_data {
+       struct chrc *chrc;
+       DBusMessage *msg;
+       bool enable;
+};
+
+static void proxy_notify_reply(DBusMessage *message, void *user_data)
+{
+       struct notify_attribute_data *data = user_data;
+       DBusConnection *conn = bt_shell_get_env("DBUS_CONNECTION");
+       DBusError error;
+
+       dbus_error_init(&error);
+
+       if (dbus_set_error_from_message(&error, message) == TRUE) {
+               bt_shell_printf("Failed to %s: %s\n",
+                               data->enable ? "StartNotify" : "StopNotify",
+                               error.name);
+               dbus_error_free(&error);
+               g_dbus_send_error(conn, data->msg, error.name, "%s",
+                                                       error.message);
+               goto done;
+       }
+
+       g_dbus_send_reply(conn, data->msg, DBUS_TYPE_INVALID);
+
+       data->chrc->notifying = data->enable;
+       bt_shell_printf("[" COLORED_CHG "] Attribute %s notifications %s\n",
+                               data->chrc->path,
+                               data->enable ? "enabled" : "disabled");
+       g_dbus_emit_property_changed(conn, data->chrc->path, CHRC_INTERFACE,
+                                                       "Notifying");
+
+done:
+       dbus_message_unref(data->msg);
+       free(data);
+}
+
+static DBusMessage *proxy_notify(struct chrc *chrc, DBusMessage *msg,
+                                                       bool enable)
+{
+       struct notify_attribute_data *data;
+       const char *method;
+
+       if (enable == TRUE)
+               method = "StartNotify";
+       else
+               method = "StopNotify";
+
+       data = new0(struct notify_attribute_data, 1);
+       data->chrc = chrc;
+       data->msg = dbus_message_ref(msg);
+       data->enable = enable;
+
+       if (g_dbus_proxy_method_call(chrc->proxy, method, NULL,
+                                       proxy_notify_reply, data, NULL))
+               return NULL;
+
+       return g_dbus_create_error(msg, "org.bluez.Error.InvalidArguments",
+                                                               NULL);
+}
+
 static DBusMessage *chrc_start_notify(DBusConnection *conn, DBusMessage *msg,
                                                        void *user_data)
 {
        struct chrc *chrc = user_data;
 
-       if (!chrc->notifying)
+       if (chrc->notifying)
                return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 
+       if (chrc->proxy)
+               return proxy_notify(chrc, msg, true);
+
        chrc->notifying = true;
        bt_shell_printf("[" COLORED_CHG "] Attribute %s notifications enabled",
                                                        chrc->path);
@@ -2359,9 +2430,12 @@ static DBusMessage *chrc_stop_notify(DBusConnection *conn, DBusMessage *msg,
 {
        struct chrc *chrc = user_data;
 
-       if (chrc->notifying)
+       if (!chrc->notifying)
                return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 
+       if (chrc->proxy)
+               return proxy_notify(chrc, msg, false);
+
        chrc->notifying = false;
        bt_shell_printf("[" COLORED_CHG "] Attribute %s notifications disabled",
                                                        chrc->path);
@@ -2832,6 +2906,15 @@ static GDBusProxy *select_service(GDBusProxy *proxy)
        return NULL;
 }
 
+static void proxy_property_changed(GDBusProxy *proxy, const char *name,
+                                       DBusMessageIter *iter, void *user_data)
+{
+       DBusConnection *conn = bt_shell_get_env("DBUS_CONNECTION");
+       struct chrc *chrc = user_data;
+
+       g_dbus_emit_property_changed(conn, chrc->path, CHRC_INTERFACE, name);
+}
+
 static void clone_chrc(struct GDBusProxy *proxy)
 {
        struct service *service;
@@ -2883,6 +2966,8 @@ static void clone_chrc(struct GDBusProxy *proxy)
                return bt_shell_noninteractive_quit(EXIT_FAILURE);
        }
 
+       g_dbus_proxy_set_property_watch(proxy, proxy_property_changed, chrc);
+
        service->chrcs = g_list_append(service->chrcs, chrc);
 
        print_chrc(chrc, COLORED_NEW);