CAPI: Implement NAP set connection callback 85/23185/2
authorGu Chaojie <chao.jie.gu@intel.com>
Thu, 19 Jun 2014 06:53:13 +0000 (14:53 +0800)
committerGu Chaojie <chao.jie.gu@intel.com>
Thu, 19 Jun 2014 06:53:50 +0000 (14:53 +0800)
Change-Id: Ic6fd9a7ee7733a15b373536c76e89a0739b8ca99
Signed-off-by: Gu Chaojie <chao.jie.gu@intel.com>
capi/bluetooth-obex.c
capi/bluetooth.c
include/bluez.h
lib/bluez.c
test/bluez-capi-test.c

index c79744c..2c25a6a 100644 (file)
@@ -22,7 +22,9 @@
 #include <unistd.h>
 #include <glib.h>
 #include <gio/gio.h>
+#include <dirent.h>
 #include <dbus/dbus.h>
+#include <gio/gunixfdlist.h>
 
 #include "bluetooth.h"
 #include "obex.h"
index 996613b..9ce8e0f 100644 (file)
@@ -194,6 +194,11 @@ struct hid_host_connection_state_changed_cb_node {
        void *user_data;
 };
 
+struct nap_connection_state_changed_cb_node {
+       bt_nap_connection_state_changed_cb cb;
+       void *user_data;
+};
+
 static struct device_connect_cb_node *device_connect_node;
 static struct device_disconnect_cb_node *device_disconnect_node;
 static struct avrcp_repeat_mode_changed_node *avrcp_repeat_node;
@@ -225,6 +230,8 @@ static struct socket_connection_requested_cb_node
 static struct socket_connection_state_changed_cb_node
                                        *socket_connection_state_node;
 static struct hid_host_connection_state_changed_cb_node *hid_host_state_node;
+static struct nap_connection_state_changed_cb_node
+                               *nap_connection_state_changed_node;
 
 static gboolean generic_device_removed_set;
 
@@ -491,6 +498,19 @@ static void bluez_avrcp_shuffle_changed(gboolean shuffle,
                data->cb(shuffle_mode, data->user_data);
 }
 
+static void bluez_nap_connection_changed(bool connected,
+                               const char *remote_address,
+                               const char *interface_name,
+                               void *user_data)
+{
+       struct nap_connection_state_changed_cb_node *data =
+                                               user_data;
+
+       if (data->cb)
+               data->cb(connected, remote_address,
+                       interface_name, data->user_data);
+}
+
 static void bluez_set_data_received_changed(
                                unsigned int channel,
                                const char *data,
@@ -964,6 +984,11 @@ static void _bt_update_bluetooth_callbacks(void)
                bluez_set_device_disconnect_changed_cb(
                                        bluez_device_disconnect_changed,
                                        device_disconnect_node);
+
+       if (nap_connection_state_changed_node)
+               bluez_set_nap_connection_state_cb(
+                               bluez_nap_connection_changed,
+                               nap_connection_state_changed_node);
 }
 
 static void setup_bluez_lib(void)
@@ -5114,6 +5139,59 @@ int bt_device_disconnect_le(bt_device_gatt_state_changed_cb callback,
        return BT_SUCCESS;
 }
 
+int bt_nap_set_connection_state_changed_cb(
+                               bt_nap_connection_state_changed_cb callback,
+                               void *user_data)
+{
+       struct nap_connection_state_changed_cb_node *node_data;
+
+       DBG("");
+
+       if (callback == NULL)
+               return BT_ERROR_INVALID_PARAMETER;
+
+       if (nap_connection_state_changed_node) {
+               DBG("Powered state changed callback already set.");
+               return BT_ERROR_ALREADY_DONE;
+       }
+
+       node_data = g_new0(struct nap_connection_state_changed_cb_node, 1);
+       if (node_data == NULL) {
+               ERROR("no memory");
+               return BT_ERROR_OUT_OF_MEMORY;
+       }
+
+       node_data->cb = callback;
+       node_data->user_data = user_data;
+
+       nap_connection_state_changed_node = node_data;
+
+       _bt_update_bluetooth_callbacks();
+
+       return BT_SUCCESS;
+}
+
+int bt_nap_unset_connection_state_changed_cb(void)
+{
+       DBG("");
+
+       if (initialized == false)
+               return BT_ERROR_NOT_INITIALIZED;
+
+       if (default_adapter == NULL)
+               return BT_ERROR_ADAPTER_NOT_FOUND;
+
+       if (!adapter_name_node)
+               return BT_SUCCESS;
+
+       bluez_unset_nap_connection_state_cb();
+
+       g_free(nap_connection_state_changed_node);
+       nap_connection_state_changed_node = NULL;
+
+       return BT_SUCCESS;
+}
+
 static gboolean spp_is_device_connected(const char *address)
 {
        struct spp_context *spp_ctx;
index 4f6cb4d..679062a 100644 (file)
@@ -350,6 +350,15 @@ void bluez_set_avrcp_target_cb(
                                gpointer user_data);
 void bluez_unset_avrcp_target_cb();
 
+typedef void (*bluez_nap_connection_state_cb_t)(bool connected,
+                               const char *remote_address,
+                               const char *interface_name,
+                               gpointer user_data);
+void bluez_set_nap_connection_state_cb(
+                               bluez_nap_connection_state_cb_t cb,
+                               gpointer user_data);
+void bluez_unset_nap_connection_state_cb(void);
+
 typedef void (*bluez_audio_state_cb_t)(int result,
                                gboolean connected,
                                const char *remote_address,
index e873f95..7a00a14 100644 (file)
@@ -31,6 +31,7 @@
 #define MEDIACONTROL_INTERFACE "org.bluez.MediaControl1"
 #define DEVICE_INTERFACE "org.bluez.Device1"
 #define NETWORK_INTERFACE "org.bluez.Network1"
+#define NETWORKSERVER_INTERFACES "org.bluez.NetworkServer1"
 #define INPUT_INTERFACE "org.bluez.Input1"
 #define AGENT_INTERFACE "org.bluez.AgentManager1"
 #define PROFILE_INTERFACE "org.bluez.ProfileManager1"
@@ -89,9 +90,11 @@ struct _bluez_adapter {
        char *object_path;
        GDBusInterface *interface;
        GDBusInterface *media_interface;
+       GDBusInterface *netserver_interface;
        guint avrcp_registration_id;
        GDBusProxy *proxy;
        GDBusProxy *media_proxy;
+       GDBusProxy *netserver_proxy;
        struct _bluez_object *parent;
        struct _device_head *device_head;
        bluez_adapter_powered_cb_t powered_cb;
@@ -142,6 +145,8 @@ static bluez_avrcp_target_cb_t avrcp_target_cb;
 static gpointer avrcp_target_cb_data;
 static bluez_audio_state_cb_t audio_state_cb;
 static gpointer audio_state_cb_data;
+static bluez_nap_connection_state_cb_t nap_connnection_state_cb;
+static gpointer nap_connnection_state_cb_data;
 static device_connect_cb_t dev_connect_cb;
 static gpointer dev_connect_data;
 static device_disconnect_cb_t dev_disconnect_cb;
@@ -264,6 +269,35 @@ static void handle_adapter_discoverable_timeout_changed(
                                adapter->discoverable_timeout_cb_data);
 }
 
+static void networkserver_on_signal(GDBusProxy *proxy,
+                                       gchar *sender_name,
+                                       gchar *signal_name,
+                                       GVariant *param,
+                                       gpointer user_data)
+{
+       gboolean connected;
+       gchar *device = NULL, *address =  NULL;
+
+       DBG("sender_name = %s, signal_name = %s",
+                               sender_name, signal_name);
+       if (strcasecmp(signal_name, "PeerConnected"))
+               connected = true;
+       else if (strcasecmp(signal_name, "PeerDisconnected"))
+               connected = false;
+       else
+               return;
+
+       g_variant_get(param, "(ss)", &device, &address);
+
+       if (nap_connnection_state_cb)
+               nap_connnection_state_cb(connected,
+                       address, device,
+                       nap_connnection_state_cb_data);
+
+       g_free(device);
+       g_free(address);
+}
+
 static void adapter_properties_changed(GDBusProxy *proxy,
                                        GVariant *changed_properties,
                                        GStrv *invalidated_properties,
@@ -320,7 +354,8 @@ static void parse_bluez_adapter_interfaces(gpointer data, gpointer user_data)
        struct _bluez_adapter *adapter = user_data;
        GDBusInterface *interface = data;
        GDBusProxy *proxy = G_DBUS_PROXY(interface);
-       const gchar *iface_name;
+       GDBusProxy *signal_proxy;
+       const gchar *iface_name, *adapter_path;
 
        if (!adapter) {
                WARN("no adapter");
@@ -341,7 +376,28 @@ static void parse_bluez_adapter_interfaces(gpointer data, gpointer user_data)
                DBG("adapter->media_proxy = proxy");
                adapter->media_interface = interface;
                adapter->media_proxy = proxy;
+       } else if (g_strcmp0(iface_name,
+                               NETWORKSERVER_INTERFACES) == 0) {
+               adapter_path = g_dbus_proxy_get_object_path(proxy);
+
+               signal_proxy = g_dbus_proxy_new_for_bus_sync(
+                                               G_BUS_TYPE_SYSTEM, 0,
+                                               NULL,
+                                               BLUEZ_NAME,
+                                               adapter_path,
+                                               iface_name,
+                                               NULL, NULL);
+
+               DBG("adapter->netserver_proxy = proxy");
+
+               adapter->netserver_interface = interface;
+               adapter->netserver_proxy = signal_proxy;
+
+               g_signal_connect(signal_proxy, "g-signal",
+                                       G_CALLBACK(networkserver_on_signal),
+                                                       NULL);
        }
+
 }
 
 static GList *bluez_object_list;
@@ -1048,6 +1104,20 @@ void bluez_unset_avrcp_target_cb()
        avrcp_target_cb_data = NULL;
 }
 
+void bluez_set_nap_connection_state_cb(
+                               bluez_nap_connection_state_cb_t cb,
+                               gpointer user_data)
+{
+       nap_connnection_state_cb = cb;
+       nap_connnection_state_cb_data = user_data;
+}
+
+void bluez_unset_nap_connection_state_cb(void)
+{
+       nap_connnection_state_cb = NULL;
+       nap_connnection_state_cb_data = NULL;
+}
+
 void bluez_set_audio_state_cb(bluez_audio_state_cb_t cb,
                                        gpointer user_data)
 {
@@ -1200,6 +1270,9 @@ static void destruct_bluez_adapter(gpointer data)
        g_free(adapter->object_path);
        g_object_unref(adapter->interface);
        g_object_unref(adapter->media_proxy);
+       g_object_unref(adapter->media_interface);
+       g_object_unref(adapter->netserver_proxy);
+       g_object_unref(adapter->netserver_interface);
        g_object_unref(adapter->proxy);
 
        g_free(adapter);
index 857c99c..cc054fd 100644 (file)
@@ -2085,6 +2085,44 @@ static int nap_deactivate(const char *p1, const char *p2)
        return 0;
 }
 
+void bt_nap_connection_state_changed_cb_test(bool connected,
+                                       const char *remote_address,
+                                       const char *interface_name,
+                                       void *user_data)
+{
+
+       DBG("");
+       DBG("connected = %d", connected);
+       DBG("remote_address = %s", remote_address);
+       DBG("interfaces_name = %s", interface_name);
+}
+
+static int nap_set_connection_state_changed_cb(const char *p1,
+                                               const char *p2)
+{
+       int ret;
+
+       ret = bt_nap_set_connection_state_changed_cb(
+                       bt_nap_connection_state_changed_cb_test,
+                       NULL);
+
+       DBG("ret = %d", ret);
+
+       return 0;
+}
+
+static int nap_unset_connection_state_changed_cb(const char *p1,
+                                               const char *p2)
+{
+       int ret;
+
+       ret = bt_nap_unset_connection_state_changed_cb();
+
+       DBG("ret = %d", ret);
+
+       return 0;
+}
+
 void bt_hdp_connected_cb_test(int result, const char *remote_address,
                        const char *app_id, bt_hdp_channel_type_e type,
                        unsigned int channel, void *user_data)
@@ -2450,6 +2488,12 @@ struct {
        {"nap_deactivate", nap_deactivate,
                "Usage: nap_deactivate\n\tdeactivate NAP"},
 
+       {"nap_set_connection_state_changed_cb", nap_set_connection_state_changed_cb,
+               "Usage: nap_set_connection_state_changed_cb\n\tset nap conn cb"},
+
+       {"nap_unset_connection_state_changed_cb", nap_unset_connection_state_changed_cb,
+               "Usage: nap_unset_connection_state_changed_cb\n\tunset nap conn cb"},
+
        {"hdo_set_connect_cb", hdp_set_connection_state_changed_cb,
                "Usage: hdp_set_connect_cb\n\tset hdp conn cb"},