Use the local state for battery monitor state
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-rfcomm-server.c
index 923b14f..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;
@@ -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);
@@ -342,9 +365,11 @@ static gboolean __data_received_cb(GIOChannel *chan, GIOCondition cond,
                        event_info->cb, event_info->user_data);
 
        if (bluetooth_get_battery_monitor_state()) {
-               int ret = _bt_common_send_rfcomm_rx_details(&data_r);
-               if (ret != BLUETOOTH_ERROR_NONE)
-                       BT_ERR("RFCOMM received data details not sent to battery monitor frwk");
+               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);
@@ -522,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