Code Sync [Tizen3.0]: Merged the tizen_2.4 Spin code to tizen.org
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-rfcomm-client.c
old mode 100644 (file)
new mode 100755 (executable)
index dfa9681..ebbf517
@@ -1,13 +1,17 @@
 /*
- * bluetooth-frwk
+ * Bluetooth-frwk
  *
- * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
+ *              Girishashok Joshi <girish.joshi@samsung.com>
+ *              Chanyeol Park <chanyeol.park@samsung.com>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *              http://www.apache.org/licenses/LICENSE-2.0
+ *             http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -69,7 +73,7 @@ static int __bt_rfcomm_open_socket(char *dev_node)
        socket_fd = open(dev_node, O_RDWR | O_NOCTTY);
 
        if (socket_fd < 0) {
-               BT_ERR("\nCan't open TTY : %s(%d)");
+               BT_ERR("Can't open TTY : %s(%d)", dev_node, socket_fd);
                return socket_fd;
        }
 
@@ -318,13 +322,22 @@ static void __bt_rfcomm_connected_cb(DBusGProxy *proxy, DBusGProxyCall *call,
                goto dbus_return;
        }
 
-       BT_DBG("Succss Connect REMOTE Device RFCOMM Node[%s]", rfcomm_device_node);
+       BT_INFO("Succss Connect REMOTE Device RFCOMM Node[%s]", rfcomm_device_node);
 
        socket_fd = __bt_rfcomm_open_socket(rfcomm_device_node);
 
        if (socket_fd < 0) {
-               BT_ERR("Fail to open socket: %d", socket_fd);
-               goto dbus_return;
+               int retry_count = 10;
+               do {
+                       BT_ERR("Fail to open socket[%d] retry_count[%d]", socket_fd, retry_count);
+                       usleep(10*1000);                /* 10 ms */
+                       socket_fd = __bt_rfcomm_open_socket(rfcomm_device_node);
+               } while (socket_fd < 0 && retry_count-- > 0);
+
+               if (socket_fd < 0) {
+                       BT_ERR("Fail to open socket: %d", socket_fd);
+                       goto dbus_return;
+               }
        }
 
        client_info = g_malloc0(sizeof(bt_rfcomm_info_t));
@@ -334,6 +347,7 @@ static void __bt_rfcomm_connected_cb(DBusGProxy *proxy, DBusGProxyCall *call,
        client_info->address = g_strdup(rfcomm_info->address);
        client_info->uuid = g_strdup(rfcomm_info->uuid);
        client_info->io_channel = g_io_channel_unix_new(socket_fd);
+       g_io_channel_set_encoding(client_info->io_channel, NULL, NULL);
        client_info->io_event = g_io_add_watch(client_info->io_channel,
                                G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
                                __bt_rfcomm_client_data_received_cb,
@@ -517,8 +531,8 @@ int _bt_rfcomm_connect_using_uuid(int request_id,
        gchar *device_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
 
-       BT_CHECK_PARAMETER(address);
-       BT_CHECK_PARAMETER(remote_uuid);
+       BT_CHECK_PARAMETER(address, return);
+       BT_CHECK_PARAMETER(remote_uuid, return);
        retv_if(rfcomm_info != NULL, BLUETOOTH_ERROR_DEVICE_BUSY);
 
        adapter_proxy = _bt_get_adapter_proxy();
@@ -573,7 +587,7 @@ int _bt_rfcomm_connect_using_channel(int request_id,
        gchar *device_path = NULL;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
 
-       BT_CHECK_PARAMETER(address);
+       BT_CHECK_PARAMETER(address, return);
        retv_if(rfcomm_info != NULL, BLUETOOTH_ERROR_DEVICE_BUSY);
 
        adapter_proxy = _bt_get_adapter_proxy();
@@ -635,27 +649,12 @@ int _bt_rfcomm_write(int socket_fd, char *buf, int length)
 {
        int wbytes = 0;
        int written;
-       int new_length;
-       char *ptr = NULL;
-
-       retv_if(buf == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
-
-       /* Check the utf8 validation & Fill the NULL in the invalid location*/
-       if (!g_utf8_validate(buf, -1, (const char **)&ptr))
-               *ptr = '\0';
 
-       /* After calling g_utf8_validate, it is possible to be NULL */
        retv_if(buf == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
 
-       new_length = strlen(buf);
-       if (new_length < length) {
-               length = new_length;
-       }
-
-       /*some times user may send huge data */
+       /* Sometimes user may send huge data */
        while (wbytes < length) {
-               written = write(socket_fd, buf + wbytes,
-                               length - wbytes);
+               written = write(socket_fd, buf + wbytes, length - wbytes);
                if (written <= 0) {
                        BT_ERR("write failed..\n");
                        return BLUETOOTH_ERROR_NOT_IN_OPERATION;
@@ -711,7 +710,7 @@ int _bt_rfcomm_cancel_connect(void)
 
 int _bt_rfcomm_is_connected(gboolean *connected)
 {
-       BT_CHECK_PARAMETER(connected);
+       BT_CHECK_PARAMETER(connected, return);
 
        *connected = (client_list == NULL || g_slist_length(client_list) == 0) ?
                                        FALSE : TRUE;
@@ -726,8 +725,8 @@ int _bt_rfcomm_is_device_connected(bluetooth_device_address_t *device_address,
        bt_rfcomm_info_t *client_info;
        char address[BT_ADDRESS_STRING_SIZE] = { 0 };
 
-       BT_CHECK_PARAMETER(device_address);
-       BT_CHECK_PARAMETER(connected);
+       BT_CHECK_PARAMETER(device_address, return);
+       BT_CHECK_PARAMETER(connected, return);
 
        _bt_convert_addr_type_to_string(address, device_address->addr);