[Adapt] Implement rfcomm client APIs 73/81773/1
authorAtul Rai <a.rai@samsung.com>
Thu, 28 Jul 2016 08:28:15 +0000 (13:58 +0530)
committerAtul Rai <a.rai@samsung.com>
Thu, 28 Jul 2016 08:32:47 +0000 (14:02 +0530)
In this patch we implement below RFCOMM client role APIs:
1/ bluetooth_rfcomm_write()
2/ bluetooth_rfcomm_client_is_connected()

Change-Id: I817f563d3c411eb35f78f3a399ab39c6f0e2c4dd
Signed-off-by: Atul Rai <a.rai@samsung.com>
bt-api/bt-rfcomm-client.c
bt-oal/bluez_hal/src/bt-hal-rfcomm-dbus-handler.c
bt-service-adaptation/services/bt-request-handler.c

index 545315c..62bb4ca 100644 (file)
 #include "bt-dpm.h"
 #endif
 
+/* Variable for privilege, only for write API,
+  before we should reduce time to bt-service dbus calling
+  -1 : Don't have a permission to access API
+  0 : Initial value, not yet check
+  1 : Have a permission to access API
+*/
+static int privilege_token;
+
+
 #ifdef RFCOMM_DIRECT
 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
                        "the remote application did not send a reply, " \
 
 static GSList *rfcomm_clients;
 
-/* Variable for privilege, only for write API,
-  before we should reduce time to bt-service dbus calling
-  -1 : Don't have a permission to access API
-  0 : Initial value, not yet check
-  1 : Have a permission to access API
-*/
-static int privilege_token;
-
 typedef struct {
        char bt_addr[BT_ADDRESS_STRING_SIZE];
        int fd;
@@ -1025,7 +1026,30 @@ BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_ad
 
        return BLUETOOTH_ERROR_NONE;
 #else
-       return BLUETOOTH_ERROR_NOT_SUPPORT;
+       GSList *l;
+       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+
+       BT_CHECK_PARAMETER(device_address, return);
+       BT_CHECK_PARAMETER(connected, return);
+
+       BT_DBG("+");
+
+       *connected = FALSE;
+       _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
+       BT_INFO("Client address: [%s]", address);
+
+       for (l = rfcomm_clients; l != NULL; l = l->next) {
+               rfcomm_client_conn_info_t *info = l->data;
+
+               if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
+                       BT_INFO("Match found");
+                       *connected = TRUE;
+                       return BLUETOOTH_ERROR_NONE;
+               }
+       }
+
+       BT_DBG("-");
+       return BLUETOOTH_ERROR_NONE;
 #endif
 }
 
@@ -1143,15 +1167,44 @@ BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
 #endif
 }
 
+static int __write_all(int fd, const char *buf, int len)
+{
+       int sent = 0;
+
+       BT_DBG("+");
+       while (len > 0) {
+               int written;
+
+               written = write(fd, buf, len);
+               BT_DBG("written: %d", written);
+               if (written < 0) {
+                       if (errno == EINTR || errno == EAGAIN)
+                               continue;
+                       return -1;
+               }
+
+               if (!written)
+                       return 0;
+
+               len -= written;
+               buf += written;
+               sent += written;
+       }
+
+       BT_DBG("-");
+       return sent;
+}
+
 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
 {
 #ifdef RFCOMM_DIRECT
        int written;
-#else
-       char *buffer;
 #endif
        int result;
 
+#ifndef RFCOMM_DIRECT
+       BT_CHECK_ENABLED(return);
+#endif
        BT_CHECK_PARAMETER(buf, return);
        if (fd < 0) {
                BT_ERR("Invalid FD");
@@ -1160,9 +1213,6 @@ BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
 
        BT_DBG("FD : %d", fd);
 
-#ifndef RFCOMM_DIRECT
-       BT_CHECK_ENABLED(return);
-#endif
        retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
 
 #ifdef TIZEN_DPM_ENABLE
@@ -1173,7 +1223,6 @@ BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
        }
 #endif
 
-#ifdef RFCOMM_DIRECT
        switch (privilege_token) {
        case 0:
                result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
@@ -1198,31 +1247,13 @@ BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
+#ifdef RFCOMM_DIRECT
        written = write(fd, buf, length);
        /*BT_DBG("Length %d, written = %d, balance(%d)",
-                        length, written, length - written); */
+                       length, written, length - written); */
        return written;
 #else
-       BT_INIT_PARAMS();
-       BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
-
-       buffer = g_malloc0(length + 1);
-
-       memcpy(buffer, buf, length);
-
-       g_array_append_vals(in_param1, &fd, sizeof(int));
-       g_array_append_vals(in_param2, &length, sizeof(int));
-       g_array_append_vals(in_param3, buffer, length);
-
-       result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
-               in_param1, in_param2, in_param3, in_param4, &out_param);
-
-       BT_DBG("result: %x", result);
-
-       BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
-
-       g_free(buffer);
-
+       result = __write_all(fd, buf, length);
        return result;
 #endif
 }
index d6eea7c..c486175 100644 (file)
@@ -456,12 +456,14 @@ static int __new_connection(const char *path, int fd, bt_bdaddr_t *addr)
        cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
        io = g_io_channel_unix_new(conn_info->hal_fd);
        conn_info->hal_watch = g_io_add_watch(io, cond, app_event_cb, info);
+       g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
        g_io_channel_unref(io);
 
        /* Handle rfcomm events from bluez */
        cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
        io = g_io_channel_unix_new(conn_info->stack_fd);
        conn_info->bt_watch = g_io_add_watch(io, cond, stack_event_cb, info);
+       g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
        g_io_channel_unref(io);
 
        return 0;
index 92bc5d6..677b909 100644 (file)
@@ -1006,6 +1006,14 @@ int __bt_bluez_request(int function_name,
                result = BLUETOOTH_ERROR_NONE;
                break;
        }
+       case BT_RFCOMM_SOCKET_WRITE: {
+               /*
+                * This call to bt-service is only used for privilege check, so return
+                * BLUETOOTH_ERROR_NONE from here.
+                */
+               result = BLUETOOTH_ERROR_NONE;
+               break;
+       }
        default:
                BT_INFO("UnSupported function [%d]", function_name);
                result = BLUETOOTH_ERROR_NOT_SUPPORT;