Check enabled before sending rfcomm details
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-rfcomm-client.c
index a0f5aff..c362fd0 100644 (file)
 #include "bt-common.h"
 #include "bt-request-sender.h"
 #include "bt-event-handler.h"
-
-#ifdef TIZEN_FEATURE_BT_DPM
 #include "bt-dpm.h"
-#endif
 
 /* Variable for privilege, only for write API,
   before we should reduce time to bt-service dbus calling
@@ -78,40 +75,38 @@ static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
 
 static bool __rfcomm_record_tx_data(void)
 {
-       if (bluetooth_get_battery_monitor_state()) {
-               if (tx_data > 0) {
-                       int ret = _bt_common_send_rfcomm_tx_details(tx_data);
-                       if (ret == BLUETOOTH_ERROR_NONE) {
-                               tx_data = 0;
-                               return TRUE;
-                       } else {
-                               BT_ERR("RFCOMM tx data could not be registered");
-                       }
+       if (tx_data > 0) {
+               int ret = _bt_common_send_rfcomm_tx_details(tx_data);
+               if (ret == BLUETOOTH_ERROR_NONE) {
+                       tx_data = 0;
+                       return TRUE;
+               } else {
+                       BT_ERR("RFCOMM tx data could not be registered");
                }
        }
 
        tx_data = 0;
        tx_tag = 0;
+
        return FALSE;
 }
 
 static bool __rfcomm_record_rx_data(void)
 {
-       if (bluetooth_get_battery_monitor_state()) {
-               if (rx_data) {
-                       int ret = _bt_common_send_rfcomm_rx_details(rx_data);
-                       if (ret == BLUETOOTH_ERROR_NONE) {
-                               rx_data = 0;
-                               return TRUE;
-                       } else {
-                               BT_ERR("RFCOMM rx data could not be registered");
-                       }
+       if (rx_data) {
+               int ret = _bt_common_send_rfcomm_rx_details(rx_data);
+               if (ret == BLUETOOTH_ERROR_NONE) {
+                       rx_data = 0;
+                       return TRUE;
+               } else {
+                       BT_ERR("RFCOMM rx data could not be registered");
                }
        }
 
        rx_data = 0;
        rx_tag = 0;
-        return FALSE;
+
+       return FALSE;
 }
 
 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
@@ -322,6 +317,10 @@ static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
        if (info->rfcomm_conns == NULL)
                rfcomm_cb_data_remove(info);
 
+       if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_CLIENT,
+                       FALSE, disconn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to send the connection info");
+
        BT_DBG("-");
 }
 
@@ -366,7 +365,7 @@ static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
        GIOStatus status = G_IO_STATUS_NORMAL;
        GError *err = NULL;
        int fd;
-       BT_DBG("+");
+       static int resource_unavailable_cnt = 0;
 
        retv_if(info == NULL, FALSE);
        fd = g_io_channel_unix_get_fd(chan);
@@ -395,35 +394,31 @@ static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
                BT_ERR("IO Channel read is failed with %d", status);
 
                g_free(buffer);
-               if (err || status == G_IO_STATUS_EOF) {
-                       if (err)
-                               BT_ERR("IO Channel read error [%s]", err->message);
-                       if ((status == G_IO_STATUS_EOF) ||
-                                       (status == G_IO_STATUS_ERROR &&
-                                       __is_error_by_disconnect(err))) {
+
+               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);
-                               if (err)
-                                       g_error_free(err);
-
-                               conn_info = __get_conn_info_from_fd(info, fd);
-                               if (conn_info == NULL) {
-                                       BT_ERR("No Connection info found with FD [%d]", fd);
-                                       return FALSE;
-                               }
-
-                               if (conn_info->disconnected == FALSE) {
-                                       close(conn_info->fd);
-                                       conn_info->disconnected = TRUE;
-                               }
-                               __rfcomm_client_disconnect(info);
-                               return FALSE;
-                       }
-                       if (err)
                                g_error_free(err);
+                               goto fail;
+                       }
+                       g_error_free(err);
                }
+
+               if (status == G_IO_STATUS_ERROR ||
+                       status == G_IO_STATUS_EOF) {
+                       goto fail;
+               } else if (status == G_IO_STATUS_AGAIN) {
+                       resource_unavailable_cnt++;
+                       if (resource_unavailable_cnt > 10)
+                               goto fail;
+               }
+
                return TRUE;
        }
+       resource_unavailable_cnt = 0;
 
        event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
        if (event_info == NULL) {
@@ -441,15 +436,29 @@ static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
 
        if (bluetooth_get_battery_monitor_state()) {
                if (rx_tag == 0) {
-                       BT_INFO("Adding rfcomm rx timeout function for battery monitor");
+                       BT_DBG("Adding rfcomm rx timeout function for battery monitor");
                        rx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_rx_data, NULL);
                }
                rx_data += len;
        }
 
        g_free(buffer);
-       BT_DBG("-");
        return TRUE;
+
+fail:
+       conn_info = __get_conn_info_from_fd(info, fd);
+       if (conn_info == NULL) {
+               BT_ERR("No Connection info found with FD [%d]", fd);
+               return FALSE;
+       }
+
+       if (conn_info->disconnected == FALSE) {
+               close(conn_info->fd);
+               conn_info->disconnected = TRUE;
+       }
+       __rfcomm_client_disconnect(info);
+
+       return FALSE;
 }
 
 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
@@ -479,6 +488,10 @@ static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
        conn_info.socket_fd = conn_list_info->fd;
        conn_info.server_id = -1;
 
+       if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_CLIENT,
+                       TRUE, conn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to send the connection info");
+
        BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
                        result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
        _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
@@ -520,6 +533,22 @@ void _bt_rfcomm_client_disconnect_all(void)
        return;
 }
 
+void _bt_rfcomm_client_reset_timer(void)
+{
+       if (rx_tag > 0) {
+               rx_tag = 0;
+               g_source_remove(rx_tag);
+       }
+
+       if (tx_tag > 0) {
+               g_source_remove(tx_tag);
+               tx_tag = 0;
+       }
+
+       rx_data = 0;
+       tx_data = 0;
+}
+
 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
 {
        rfcomm_cb_data_t *info;
@@ -1037,7 +1066,6 @@ BT_EXPORT_API int bluetooth_rfcomm_connect(
        BT_CHECK_PARAMETER(remote_uuid, return);
        BT_CHECK_ENABLED(return);
 
-#ifdef TIZEN_FEATURE_BT_DPM
        if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
                BT_ERR("Blacklist device");
                return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
@@ -1061,7 +1089,6 @@ BT_EXPORT_API int bluetooth_rfcomm_connect(
                        return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
                }
        }
-#endif
 
 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
        BT_INFO_C("### Connect RFCOMM");
@@ -1423,20 +1450,20 @@ BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
                        length, written, length - written); */
        if (written > 0 && bluetooth_get_battery_monitor_state()) {
                 if (tx_tag == 0) {
-                        BT_INFO("Adding rfcomm tx timeout function for battery monitor");
+                        BT_DBG("Adding rfcomm tx timeout function for battery monitor");
                         tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);
                 }
-                tx_data += length;
+                tx_data += written;
         }
        return written;
 #else
        result = __write_all(fd, buf, length);
        if (result > 0 && bluetooth_get_battery_monitor_state()) {
                 if (tx_tag == 0) {
-                        BT_INFO("Adding rfcomm tx timeout function for battery monitor");
+                        BT_DBG("Adding rfcomm tx timeout function for battery monitor");
                         tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);
                 }
-                tx_data += length;
+                tx_data += written;
         }
        return result;
 #endif