get connected fd by socket accept 50/23650/1
authorGu Chaojie <chao.jie.gu@intel.com>
Tue, 1 Jul 2014 06:02:42 +0000 (14:02 +0800)
committerGu Chaojie <chao.jie.gu@intel.com>
Tue, 1 Jul 2014 06:03:40 +0000 (14:03 +0800)
Change-Id: Ib9ea0aea2799b28ca6a15d5ceb0955b56c9b1ea5
Signed-off-by: Gu Chaojie <chao.jie.gu@intel.com>
capi/bluetooth.c
src/pairing.c
test/bluez-capi-test.c
test/bluez-lib-test.c

index 0f3dce0..8b60880 100644 (file)
@@ -3170,6 +3170,7 @@ static const gchar introspection_xml[] =
        "    <method name='AuthorizeService'>"
        "      <arg type='o' name='device' direction='in'/>"
        "      <arg type='s' name='uuid' direction='in'/>"
+       "      <arg type='h' name='fd' direction='in'/>"
        "    </method>"
        "    <method name='Cancel'>"
        "    </method>"
@@ -3296,12 +3297,23 @@ static void handle_spp_authorize_request(bluez_device_t *device,
                                        GDBusMethodInvocation *invocation)
 {
        char *device_name, *device_address;
+       GDBusMessage *msg;
+       GUnixFDList *fd_list;
+       gint fd;
 
        if (spp_ctx->max_pending == 0) {
                bt_spp_reject(invocation);
                return;
        }
 
+       msg = g_dbus_method_invocation_get_message(invocation);
+
+       fd_list = g_dbus_message_get_unix_fd_list(msg);
+
+       fd = g_unix_fd_list_get(fd_list, (gint)0, NULL);
+
+       spp_ctx->channel = g_io_channel_unix_new(fd);
+
        device_name = bluez_device_get_property_alias(device);
        device_address = bluez_device_get_property_address(device);
 
@@ -3466,7 +3478,10 @@ static void handle_method_call(GDBusConnection *connection,
 
        if (g_strcmp0(method_name, "AuthorizeService") == 0) {
                gchar *device_path, *uuid;
-               g_variant_get(parameters, "(os)", &device_path, &uuid);
+               gint32 fd_index;
+
+               g_variant_get(parameters, "(osh)", &device_path,
+                                               &uuid, &fd_index);
 
                request_authorize_service_handler(device_path,
                                                uuid, invocation);
@@ -3855,14 +3870,17 @@ static void handle_new_connection(gchar *device_path, gint fd,
                return;
        }
 
-       channel = g_io_channel_unix_new(fd);
-       if (channel == NULL) {
-               ERROR("Create connection channel error");
-               g_dbus_method_invocation_return_value(invocation, NULL);
-               goto done;
-       }
+       if (!(spp_ctx->channel)) {
+               channel = g_io_channel_unix_new(fd);
+               if (channel == NULL) {
+                       ERROR("Create connection channel error");
+                       g_dbus_method_invocation_return_value(invocation, NULL);
+                       goto done;
+               }
 
-       spp_ctx->channel = channel;
+               spp_ctx->channel = channel;
+       } else
+               channel = spp_ctx->channel;
 
        g_io_channel_set_close_on_unref(channel, TRUE);
        g_io_channel_set_encoding(channel, NULL, NULL);
index 142de23..e113512 100644 (file)
@@ -17,6 +17,7 @@
 *
 */
 
+#include <gio/gunixfdlist.h>
 #include "common.h"
 #include "gdbus.h"
 #include "comms_error.h"
@@ -268,6 +269,7 @@ static const gchar introspection_xml[] =
        "    <method name='AuthorizeService'>"
        "      <arg type='o' name='device' direction='in'/>"
        "      <arg type='s' name='uuid' direction='in'/>"
+       "      <arg type='h' name='fd' direction='in'/>"
        "    </method>"
        "    <method name='Cancel'>"
        "    </method>"
@@ -469,25 +471,48 @@ static void handle_authorize_service(GDBusConnection *connection,
                                gpointer user_data)
 {
        struct agent_reply_data *reply_data;
+       GDBusMessage *msg;
+       GUnixFDList *in_fd_list, *out_fd_list;
+       GError *error = NULL;
+       gchar *device_path, *uuid;
+       gint32 fd_index;
+       gint fd;
 
        DBG("");
 
+       g_variant_get(parameters, "(osh)", &device_path,
+                                               &uuid, &fd_index);
+
+       msg = g_dbus_method_invocation_get_message(invocation);
+
+       in_fd_list = g_dbus_message_get_unix_fd_list(msg);
+
+       fd = g_unix_fd_list_get(in_fd_list, fd_index, NULL);
+
+       out_fd_list = g_unix_fd_list_new();
+
+       g_unix_fd_list_append(out_fd_list, fd, &error);
+
+       g_assert_no_error(error);
+
        reply_data = g_new0(struct agent_reply_data, 1);
 
        reply_data->connection = connection;
        reply_data->invocation = invocation;
 
-       g_dbus_connection_call(connection,
-                               relay_agent->owner,
-                               relay_agent->object_path,
-                               AGENT_INTERFACE,
-                               "AuthorizeService",
-                               parameters,
-                               NULL,
-                               G_DBUS_CALL_FLAGS_NONE,
-                               -1, NULL,
-                               relay_agent_reply,
-                               reply_data);
+       g_dbus_connection_call_with_unix_fd_list(connection,
+                                               relay_agent->owner,
+                                               relay_agent->object_path,
+                                               AGENT_INTERFACE,
+                                               "AuthorizeService",
+                                               parameters,
+                                               NULL,
+                                               G_DBUS_CALL_FLAGS_NONE,
+                                               -1,
+                                               out_fd_list,
+                                               NULL,
+                                               relay_agent_reply,
+                                               reply_data);
 }
 
 static void handle_cancel(GDBusConnection *connection,
index 30bbace..101715b 100644 (file)
@@ -1694,17 +1694,19 @@ void bt_socket_connection_requested_cb_test(int fd,
                                const char *remote_address,
                                void *user_data)
 {
-
+       int connected_socket_fd;
        DBG("socket_fd = %d", socket_fd);
        DBG("remote_address = %s", remote_address);
 
+       bt_socket_accept(fd, &connected_socket_fd);
+
+       DBG("Connected socket_fd = %d", connected_socket_fd);
        socket_fd = fd;
 }
 
 int socket_set_connection_requested_cb(const char *p1, const char *p2)
 {
        int ret;
-
        DBG("");
 
        ret = bt_socket_set_connection_requested_cb(
index 46e55c5..5d84429 100644 (file)
@@ -357,7 +357,9 @@ static void handle_method_call(GDBusConnection *connection,
 
        if (g_strcmp0(method_name, "AuthorizeService") == 0) {
                gchar *device_path, *uuid;
-               g_variant_get(parameters, "(os)", &device_path, &uuid);
+               gint32 fd_index;
+               g_variant_get(parameters, "(osh)", &device_path,
+                                       &uuid, &fd_index);
 
                printf("\n\tdevice %s uuid %s", device_path, uuid);
                printf("\n\tAuthorize connection (yes/no):\n");