Use the local state for battery monitor state
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-rfcomm-server.c
index 05c311d..1365077 100644 (file)
@@ -34,6 +34,7 @@
 #endif
 
 #define BLUETOOTH_SOCK_CONNECT_INFO_LEN 16
+#define BATTERY_MONITOR_RFCOMM_INTERVAL 5
 
 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
 
@@ -55,6 +56,27 @@ typedef struct {
        guint disconnect_idle_id;
 } rfcomm_info_t;
 
+static unsigned int rx_data = 0;
+static guint rx_tag = 0;
+
+static bool __rfcomm_record_rx_data(void)
+{
+       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;
+}
+
 static rfcomm_info_t *__find_rfcomm_info_with_id(int id)
 {
        GSList *l;
@@ -95,7 +117,7 @@ static rfcomm_info_t *__find_rfcomm_info_with_path(const gchar *path)
        for (l = rfcomm_nodes; l != NULL; l = l->next) {
                rfcomm_info_t *info = l->data;
 
-               if (g_strcmp0(info->path, path) == 0)
+               if (g_ascii_strcasecmp(info->path, path) == 0)
                        return info;
        }
 
@@ -109,7 +131,7 @@ static rfcomm_info_t *__find_rfcomm_info_with_uuid(const char *uuid)
        for (l = rfcomm_nodes; l != NULL; l = l->next) {
                rfcomm_info_t *info = l->data;
 
-               if (g_strcmp0(info->uuid, uuid) == 0)
+               if (g_ascii_strcasecmp(info->uuid, uuid) == 0)
                        return info;
        }
 
@@ -154,7 +176,7 @@ gboolean _check_uuid_path(char *path, char *uuid)
        if (!info)
                return FALSE;
 
-       if (strcmp(info->uuid, uuid) == 0)
+       if (g_ascii_strcasecmp(info->uuid, uuid) == 0)
                return TRUE;
 
        return FALSE;
@@ -174,6 +196,7 @@ static void __connected_cb(rfcomm_info_t *info, rfcomm_conn_t *conn,
        conn_info.server_id = info->id;
 
        BT_INFO_C("### Connected [RFCOMM Server]");
+
        _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
                        BLUETOOTH_ERROR_NONE, &conn_info,
                        event_info->cb, event_info->user_data);
@@ -341,6 +364,14 @@ static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
                        result, &data_r,
                        event_info->cb, event_info->user_data);
 
+       if (bluetooth_get_battery_monitor_state()) {
+               if (rx_tag == 0) {
+                       BT_INFO("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);
 
        return TRUE;
@@ -488,7 +519,7 @@ void _bt_rfcomm_server_disconnect_all(void)
        GSList *conn;
        char addr[20];
 
-       BT_INFO("### Disconnect all RFCOMM server connections");
+       BT_INFO(" ### Disconnect all RFCOMM server connections");
 
        for (server = rfcomm_nodes; server; ) {
                rfcomm_info_t *info = server->data;
@@ -516,6 +547,16 @@ void _bt_rfcomm_server_disconnect_all(void)
 
        return;
 }
+
+void _bt_rfcomm_server_reset_timer(void)
+{
+       if (rx_tag > 0) {
+               g_source_remove(rx_tag);
+               rx_tag = 0;
+       }
+
+       rx_data = 0;
+}
 #else
 
 #define BT_RFCOMM_SERVER_ID_MAX 254
@@ -1050,7 +1091,7 @@ err:
        return FALSE;
 }
 
-static int __rfcomm_listen(rfcomm_server_info_t *server_info)
+static int __rfcomm_listen(rfcomm_server_info_t *server_info, bool accept)
 {
        int result;
        GUnixFDList *out_fd_list = NULL;
@@ -1061,9 +1102,15 @@ static int __rfcomm_listen(rfcomm_server_info_t *server_info)
        BT_INIT_PARAMS();
        BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
-       g_array_append_vals(in_param1, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
-       result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
-                       in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
+       if (accept == false) {
+               g_array_append_vals(in_param1, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
+               result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
+                               in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
+       } else {
+               g_array_append_vals(in_param1, server_info->uuid, BLUETOOTH_UUID_STRING_MAX);
+               result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN_AND_ACCEPT,
+                               in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
+       }
 
        BT_DBG("result: %x", result);
        if (result != BLUETOOTH_ERROR_NONE) {
@@ -1278,7 +1325,7 @@ BT_EXPORT_API int bluetooth_rfcomm_server_disconnect(int socket_fd)
 
        char address[20];
 
-       BT_INFO("### Disconnect RFCOMM server");
+       BT_INFO(" ### Disconnect RFCOMM server");
        if (socket_fd < 0) {
                BT_ERR("Invalid FD");
                return BLUETOOTH_ERROR_INVALID_PARAM;
@@ -1473,7 +1520,7 @@ BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept(int id, int max_pending_con
        server_info->max_pending_conn = max_pending_connection;
        server_info->auto_accept = TRUE;
 
-       return __rfcomm_listen(server_info);
+       return __rfcomm_listen(server_info, true);
 #endif
 }
 
@@ -1580,7 +1627,7 @@ BT_EXPORT_API int bluetooth_rfcomm_listen(int id, int max_pending_connection)
 
        server_info->max_pending_conn = max_pending_connection;
        server_info->auto_accept = FALSE;
-       return __rfcomm_listen(server_info);
+       return __rfcomm_listen(server_info, false);
 #endif
 }