[Adapt] Implement rfcomm client connection APIs 10/80210/1
authorAtul Rai <a.rai@samsung.com>
Fri, 15 Jul 2016 07:33:19 +0000 (16:33 +0900)
committerAtul Rai <a.rai@samsung.com>
Fri, 15 Jul 2016 07:33:19 +0000 (16:33 +0900)
In this patch we implement bluetooth_rfcomm_connect API with
RFCOMM_DIRECT flag disabled. We have added logic to receive
socket fd from bt-service over dbus and then perform I/O
operations on the received socket fd in application
context.

Change-Id: I5b2c885b2b19267c9ed1dec0a46dabd634cdb5b9
Signed-off-by: Atul Rai <a.rai@samsung.com>
bt-api/bt-request-sender.c
bt-api/bt-rfcomm-client.c
bt-api/include/bt-request-sender.h
bt-service-adaptation/services/bt-request-handler.c
bt-service-adaptation/services/rfcomm/bt-service-rfcomm.c

index b240fb8..516f854 100644 (file)
@@ -81,7 +81,7 @@ void _bt_gdbus_deinit_proxys(void)
        }
 }
 
-static void __bt_get_event_info(int service_function, GArray *output,
+void _bt_get_event_info(int service_function, GArray *output,
                        int *event, int *event_type, void **param_data)
 {
        ret_if(event == NULL);
@@ -277,7 +277,7 @@ static void __send_request_cb(GDBusProxy *proxy,
 
                ret_if(cb_data == NULL);
 
-               __bt_get_event_info(cb_data->service_function, NULL,
+               _bt_get_event_info(cb_data->service_function, NULL,
                                &bt_event.event, &event_type,
                                &bt_event.param_data);
        } else {
@@ -302,7 +302,7 @@ static void __send_request_cb(GDBusProxy *proxy,
 
                ret_if(cb_data == NULL);
 
-               __bt_get_event_info(cb_data->service_function, out_param1,
+               _bt_get_event_info(cb_data->service_function, out_param1,
                                &bt_event.event, &event_type,
                                &bt_event.param_data);
 
@@ -554,3 +554,82 @@ int _bt_async_send_request(int service_type, int service_function,
        return BLUETOOTH_ERROR_NONE;
 }
 
+int _bt_async_send_request_with_unix_fd_list(int service_type, int service_function,
+                       GArray *in_param1, GArray *in_param2,
+                       GArray *in_param3, GArray *in_param4,
+                       void *callback, void *user_data,
+                       GUnixFDList *fd_list, GAsyncReadyCallback __async_req_cb)
+{
+       GArray* in_param5 = NULL;
+       bt_req_info_t *cb_data;
+
+       GDBusProxy *proxy;
+       int timeout;
+       GVariant *param1;
+       GVariant *param2;
+       GVariant *param3;
+       GVariant *param4;
+       GVariant *param5;
+
+       BT_DBG("service_function : %d", service_function);
+
+       cb_data = g_new0(bt_req_info_t, 1);
+       cb_data->service_function = service_function;
+       cb_data->cb = callback;
+       cb_data->user_data = user_data;
+
+       switch (service_type) {
+       case BT_BLUEZ_SERVICE:
+       case BT_OBEX_SERVICE:
+               proxy =  __bt_gdbus_get_service_proxy();
+               if (!proxy) {
+                       g_free(cb_data);
+                       return BLUETOOTH_ERROR_INTERNAL;
+               }
+
+               /* Do not timeout the request in certain cases. Sometime the
+                * request may take undeterministic time to reponse.
+                * (for ex: pairing retry) */
+               if (service_function == BT_BOND_DEVICE ||
+                               service_function == BT_BOND_DEVICE_BY_TYPE)
+                       timeout = INT_MAX;
+               else
+                       timeout = BT_DBUS_TIMEOUT_MAX;
+
+               in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
+
+               param1 = g_variant_new_from_data((const GVariantType *)"ay",
+                               in_param1->data, in_param1->len,
+                               TRUE, NULL, NULL);
+               param2 = g_variant_new_from_data((const GVariantType *)"ay",
+                               in_param2->data, in_param2->len,
+                               TRUE, NULL, NULL);
+               param3 = g_variant_new_from_data((const GVariantType *)"ay",
+                               in_param3->data, in_param3->len,
+                               TRUE, NULL, NULL);
+               param4 = g_variant_new_from_data((const GVariantType *)"ay",
+                               in_param4->data, in_param4->len,
+                               TRUE, NULL, NULL);
+               param5 = g_variant_new_from_data((const GVariantType *)"ay",
+                               in_param5->data, in_param5->len,
+                               TRUE, NULL, NULL);
+
+               g_dbus_proxy_call_with_unix_fd_list(proxy, "service_request",
+                               g_variant_new("(iii@ay@ay@ay@ay@ay)",
+                                       service_type, service_function,
+                                       BT_ASYNC_REQ, param1, param2,
+                                       param3, param4, param5),
+                               G_DBUS_CALL_FLAGS_NONE,
+                               timeout, fd_list, NULL,
+                               __async_req_cb, (gpointer)cb_data);
+               sending_requests = g_slist_append(sending_requests, cb_data);
+
+               g_array_free(in_param5, TRUE);
+               break;
+       default:
+               g_free(cb_data);
+               break;
+       }
+
+       return BLUETOOTH_ERROR_NONE;
+}
index 59e6a59..549d5f3 100644 (file)
  */
 
 #include <string.h>
+#include <gio/gunixfdlist.h>
+
 #ifdef RFCOMM_DIRECT
 #include <errno.h>
-#include <gio/gunixfdlist.h>
 #endif
 
 #include "bluetooth-api.h"
@@ -33,7 +34,6 @@
 #endif
 
 #ifdef RFCOMM_DIRECT
-
 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
                        "the remote application did not send a reply, " \
                        "the message bus security policy blocked the reply, " \
@@ -550,6 +550,312 @@ done:
        if (err)
                g_clear_error(&err);
 }
+#else
+GSList *rfcomm_clients;
+
+typedef struct {
+       char *uuid;
+       char *remote_addr;
+       int sock_fd;
+       int watch_id;
+} rfcomm_client_conn_info_t;
+
+static gboolean __is_error_by_disconnect(GError *err)
+{
+       return !g_strcmp0(err->message, "Connection reset by peer") ||
+               !g_strcmp0(err->message, "Connection timed out") ||
+               !g_strcmp0(err->message, "Software caused connection abort");
+}
+
+static rfcomm_client_conn_info_t *__find_rfcomm_conn_info_with_fd(int fd)
+{
+       GSList *l;
+
+       BT_DBG("+");
+
+       for (l = rfcomm_clients; l != NULL; l = l->next) {
+               rfcomm_client_conn_info_t *info = l->data;
+
+               if (info && info->sock_fd == fd) {
+                       BT_INFO("Match found");
+                       return info;
+               }
+       }
+
+       BT_DBG("-");
+       return NULL;
+}
+
+static void __rfcomm_remove_client_conn_info_t(rfcomm_client_conn_info_t *info)
+{
+       ret_if(info == NULL);
+
+       rfcomm_clients = g_slist_remove(rfcomm_clients, info);
+       g_free(info->uuid);
+       g_free(info->remote_addr);
+}
+
+static void __bt_rfcomm_client_disconnected(rfcomm_client_conn_info_t *conn_info)
+{
+
+       bluetooth_rfcomm_disconnection_t disconn_info;
+       bt_event_info_t *event_info = NULL;
+
+       ret_if(conn_info == NULL);
+
+       event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
+       ret_if(event_info == NULL);
+
+       memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
+       disconn_info.device_role = RFCOMM_ROLE_CLIENT;
+       disconn_info.socket_fd = conn_info->sock_fd;
+       g_strlcpy(disconn_info.uuid, conn_info->uuid, BLUETOOTH_UUID_STRING_MAX);
+       _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
+                       conn_info->remote_addr);
+
+       BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
+                       BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
+                       conn_info->uuid, conn_info->sock_fd);
+       _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
+                       BLUETOOTH_ERROR_NONE, &disconn_info,
+                       event_info->cb, event_info->user_data);
+
+       BT_DBG("-");
+}
+
+static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
+{
+       bt_event_info_t *event_info;
+       bluetooth_rfcomm_received_data_t data_r;
+       rfcomm_client_conn_info_t *conn_info;
+
+       int fd;
+       gsize len = 0;
+       char *buffer;
+       GError *err = NULL;
+       GIOStatus status = G_IO_STATUS_NORMAL;
+
+       BT_DBG("+");
+
+       fd = g_io_channel_unix_get_fd(chan);
+       if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+               BT_ERR_C("RFComm Client  disconnected: %d", fd);
+               goto error;
+       }
+
+       buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
+       status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
+                       &len, &err);
+       if (status != G_IO_STATUS_NORMAL) {
+               BT_ERR("IO Channel read is failed with %d", status);
+               g_free(buffer);
+               if (err) {
+                       BT_ERR("IO Channel read error [%s]", err->message);
+                       if (status == G_IO_STATUS_ERROR &&
+                                       __is_error_by_disconnect(err)) {
+                               BT_ERR("cond : %d", cond);
+                               g_error_free(err);
+                               goto error;
+                       }
+                       g_error_free(err);
+               }
+
+               return TRUE;
+       }
+
+       BT_DBG("fd: %d, len: %d, buffer: %s", fd, len, buffer);
+
+       event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
+       if (event_info == NULL) {
+               BT_INFO("event_info == NULL");
+               g_free(buffer);
+               return TRUE;
+       }
+
+       data_r.socket_fd = fd;
+       data_r.buffer_size = len;
+       data_r.buffer = buffer;
+
+       _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
+                       BLUETOOTH_ERROR_NONE, &data_r,
+                       event_info->cb, event_info->user_data);
+
+       g_free(buffer);
+       return TRUE;
+
+error:
+       conn_info = __find_rfcomm_conn_info_with_fd(fd);
+       if (conn_info) {
+               __bt_rfcomm_client_disconnected(conn_info);
+               __rfcomm_remove_client_conn_info_t(conn_info);
+       } else {
+               BT_ERR("RFCOMM client conn_info not found");
+       }
+       return FALSE;
+}
+
+static void __rfcomm_client_connection_create_watch(rfcomm_client_conn_info_t *conn_info)
+{
+       GIOChannel *data_io;
+
+       ret_if(NULL == conn_info);
+
+       BT_DBG("+");
+
+       data_io = g_io_channel_unix_new(conn_info->sock_fd);
+       g_io_channel_set_encoding(data_io, NULL, NULL);
+       g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
+       conn_info->watch_id = g_io_add_watch(data_io,
+                       G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+                       __client_data_received_cb, NULL);
+       g_io_channel_unref(data_io);
+
+       BT_DBG("-");
+}
+
+static void __bt_rfcomm_handle_new_client_connection(bluetooth_rfcomm_connection_t *info)
+{
+       rfcomm_client_conn_info_t *conn_info;
+
+       ret_if(NULL == info);
+
+       BT_DBG("+");
+
+       conn_info = g_malloc0(sizeof(rfcomm_client_conn_info_t));
+       conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
+       _bt_convert_addr_type_to_string(
+                       conn_info->remote_addr, info->device_addr.addr);
+       conn_info->uuid = g_strdup(info->uuid);
+       conn_info->sock_fd = info->socket_fd;
+
+       BT_DBG("Address:%s, UUID:%s Socket: %d",
+                       conn_info->remote_addr, conn_info->uuid, conn_info->sock_fd);
+
+       rfcomm_clients = g_slist_append(rfcomm_clients, conn_info);
+       __rfcomm_client_connection_create_watch(conn_info);
+
+       BT_DBG("-");
+}
+
+static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
+{
+       char *data;
+       int size;
+
+       size = g_variant_get_size(var);
+       if (size > 0) {
+               data = (char *)g_variant_get_data(var);
+               if (data)
+                       param = g_array_append_vals(param, data, size);
+
+       }
+}
+
+static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
+{
+       int result = BLUETOOTH_ERROR_NONE;
+       int event_type = BT_ADAPTER_EVENT;
+       bt_req_info_t *cb_data = user_data;
+       bluetooth_event_param_t bt_event;
+
+       GError *error = NULL;
+       GVariant *value;
+       GVariant *param1;
+       GArray *out_param1 = NULL;
+       GUnixFDList *out_fd_list = NULL;
+
+       BT_DBG("+");
+
+       memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
+
+       value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
+       if (value == NULL) {
+               if (error) {
+                       /* dBUS gives error cause */
+                       BT_ERR("D-Bus API failure: message[%s]",
+                                       error->message);
+                       g_clear_error(&error);
+               }
+               result = BLUETOOTH_ERROR_TIMEOUT;
+
+               ret_if(cb_data == NULL);
+
+               _bt_get_event_info(cb_data->service_function, NULL,
+                               &bt_event.event, &event_type,
+                               &bt_event.param_data);
+               goto failed;
+       }
+
+       g_variant_get(value, "(iv)", &result, &param1);
+       g_variant_unref(value);
+
+       if (param1) {
+               out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
+               __bt_fill_garray_from_variant(param1, out_param1);
+               g_variant_unref(param1);
+       }
+
+       if (!cb_data)
+               goto done;
+
+       _bt_get_event_info(cb_data->service_function, out_param1,
+                       &bt_event.event, &event_type,
+                       &bt_event.param_data);
+
+       if (result == BLUETOOTH_ERROR_NONE && out_param1) {
+               if (BT_RFCOMM_CLIENT_CONNECT == cb_data->service_function) {
+                       int *fd_list_array;
+                       int len = 0;
+                       bluetooth_rfcomm_connection_t *conn_info;
+
+                       conn_info = (bluetooth_rfcomm_connection_t *)bt_event.param_data;
+                       if (!out_fd_list) {
+                               BT_ERR("out_fd_list is NULL");
+                               goto failed;
+                       }
+
+                       fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
+                       BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
+                       conn_info->socket_fd = fd_list_array[0];
+
+                       BT_DBG("conn_info->socket_fd: %d", conn_info->socket_fd);
+                       __bt_rfcomm_handle_new_client_connection(conn_info);
+
+                       if (cb_data->cb != NULL) {
+                               /* Send client connected event */
+                               bt_event.result = result;
+                               BT_INFO("event_type[%d], result=[%d]", event_type, result);
+                               ((bluetooth_cb_func_ptr)cb_data->cb)(
+                                       bt_event.event, &bt_event, cb_data->user_data);
+                       }
+
+                       g_free(fd_list_array);
+                       g_object_unref(out_fd_list);
+               }
+               goto done;
+       }
+
+failed:
+       if (cb_data->cb == NULL)
+               goto done;
+
+       /* Only if fail case, call the callback function*/
+       bt_event.result = result;
+
+       BT_INFO("event_type[%d], result=[%d]", event_type, result);
+       if (event_type == BT_RFCOMM_CLIENT_EVENT) {
+               ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
+                       &bt_event, cb_data->user_data);
+       } else {
+               BT_INFO("Not handled event type : %d", event_type);
+       }
+done:
+       if (out_param1)
+               g_array_free(out_param1, TRUE);
+
+       g_free(cb_data);
+       BT_DBG("-");
+}
 #endif
 
 BT_EXPORT_API int bluetooth_rfcomm_connect(
@@ -659,12 +965,6 @@ BT_EXPORT_API int bluetooth_rfcomm_connect(
        /* In now, we only support to connecty using UUID */
        connect_type = BT_RFCOMM_UUID;
 
-       if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
-            == BLUETOOTH_ERROR_PERMISSION_DEINED) {
-               BT_ERR("Don't have a privilege to use this API");
-               return BLUETOOTH_ERROR_PERMISSION_DEINED;
-       }
-
        BT_INIT_PARAMS();
        BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
@@ -676,11 +976,12 @@ BT_EXPORT_API int bluetooth_rfcomm_connect(
 
        g_array_append_vals(in_param3, &connect_type, sizeof(int));
 
-       result = _bt_send_request_async(BT_BLUEZ_SERVICE,
+       result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
                                BT_RFCOMM_CLIENT_CONNECT,
                                in_param1, in_param2,
                                in_param3, in_param4,
-                               user_info->cb, user_info->user_data);
+                               user_info->cb, user_info->user_data,
+                               NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
 
        BT_DBG("result: %x", result);
 
index 150c919..cbd8e95 100755 (executable)
@@ -58,6 +58,21 @@ int _bt_async_send_request(int service_type, int service_function,
        } \
        )
 
+int _bt_async_send_request_with_unix_fd_list(int service_type, int service_function,
+                       GArray *in_param1, GArray *in_param2,
+                       GArray *in_param3, GArray *in_param4,
+                       void *callback, void *user_data,
+                       GUnixFDList *fd_list, GAsyncReadyCallback __async_req_cb);
+#define _bt_send_request_async_with_unix_fd_list(a, b, format ...) ( \
+       { \
+       BT_INFO_C("Async Request => type=%s, fn=%s(0x%x)", #a, #b, b); \
+       _bt_async_send_request_with_unix_fd_list(a, b, format); \
+       } \
+       )
+
+void _bt_get_event_info(int service_function, GArray *output,
+                       int *event, int *event_type, void **param_data);
+
 #ifdef __cplusplus
 }
 #endif
index 8319541..3881c0f 100644 (file)
@@ -982,6 +982,7 @@ int __bt_bluez_request(int function_name,
                        g_array_append_vals(*out_param1, &conn_info,
                                        sizeof(bluetooth_rfcomm_connection_t));
                } else {
+                       BT_ERR("BT_RFCOMM_CLIENT_CONNECT success, save context");
                        sender = (char*)g_dbus_method_invocation_get_sender(context);
                        _bt_save_invocation_context(context, result, sender, function_name, NULL);
                }
index 544d4c7..1105784 100644 (file)
@@ -26,7 +26,6 @@
 #include <dlog.h>
 #include <string.h>
 
-
 #include "bluetooth-api.h"
 #include "bt-internal-types.h"
 #include "bt-request-handler.h"
@@ -65,11 +64,11 @@ static void __bt_rfcomm_reply_pending_request(int result,
                                fd_list = g_unix_fd_list_new();
                                g_unix_fd_list_append(fd_list, ptr->socket_fd, &error);
                                g_assert_no_error (error);
+                               close(ptr->socket_fd);
                        }
 
                        _bt_service_method_return_with_unix_fd_list(
                                        req_info->context, out_param, result, fd_list);
-
                        g_object_unref(fd_list);
                        break;
                }
@@ -107,16 +106,6 @@ static void __bt_rfcomm_socket_conn_cb(int result, int sock_fd, char *address, c
                        result, BT_RFCOMM_CLIENT_CONNECT,
                        (void *)&conn_info, sizeof(bluetooth_rfcomm_connection_t));
 
-       /* Send BLUETOOTH_EVENT_RFCOMM_CONNECTED event to application */
-       if (result == BLUETOOTH_ERROR_NONE) {
-               GVariant *param;
-
-               param = g_variant_new("(issn)", BLUETOOTH_ERROR_NONE,
-                               address, conn_info.uuid, sock_fd);
-               _bt_send_event(BT_RFCOMM_CLIENT_EVENT,
-                               BLUETOOTH_EVENT_RFCOMM_CONNECTED, param);
-       }
-
        BT_DBG("-");
 }