Fix bt-service crash issue 36/229536/4 accepted/tizen/unified/20200403.034541 submit/tizen/20200402.035805
authorDoHyun Pyun <dh79.pyun@samsung.com>
Wed, 1 Apr 2020 23:01:10 +0000 (08:01 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Thu, 2 Apr 2020 02:26:48 +0000 (11:26 +0900)
There are some cases that current_session_data is
NULL. This patchset adds the prevent code for
crashing issue.

Change-Id: I7931b35f2202f54cba48ab2c826d0785987f18e7
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
bt-service-adaptation/services/bt-service-battery-monitor.c

index ce452ed..5133774 100644 (file)
@@ -25,6 +25,7 @@
 #include <dlog.h>
 #include <time.h>
 #include <sys/time.h>
+#include <vconf.h>
 
 #include <oal-event.h>
 
 #include "bt-service-event.h"
 #include "bt-service-core-adapter.h"
 
+/* Avoid the build error related to vconf.h's dependency */
+#ifndef VCONFKEY_BATTERY_MONITOR_STATUS
+#define VCONFKEY_BATTERY_MONITOR_STATUS "db/bluetooth/bmstatus"
+#endif
+
 static struct timeval scan_start;
 static struct timeval connect_start;
 static struct timeval app_scan_base;
@@ -50,6 +56,7 @@ typedef struct {
 _bt_battery_data_t *current_session_data = NULL;
 
 static void __bt_bm_add_prev_time(uint32_t scan_time);
+static int __bt_start_session_time(void);
 
 uint32_t static __bt_dm_time_diff_msec(struct timeval prev, struct timeval cur)
 {
@@ -59,6 +66,12 @@ uint32_t static __bt_dm_time_diff_msec(struct timeval prev, struct timeval cur)
 static void __bt_display_session_data()
 {
        BT_DBG("Displaying session data...");
+
+       if (current_session_data == NULL) {
+               BT_ERR("Session in progress but data structure is not initialized");
+               return;
+       }
+
        BT_DBG("session_start_time = %ld", current_session_data->session_start_time);
        BT_DBG("session_end_time = %ld", current_session_data->session_end_time);
        BT_DBG("session_scan_time = %d", current_session_data->session_scan_time);
@@ -89,6 +102,18 @@ int _bt_bm_read_data(_bt_battery_data_t *data)
                return BLUETOOTH_ERROR_NOT_SUPPORT;
        }
 
+       if (is_session_started == FALSE) {
+               if (__bt_start_session_time() != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Fail to start session time");
+                       return BLUETOOTH_ERROR_NOT_SUPPORT;
+               }
+       }
+
+       if (current_session_data == NULL) {
+               BT_ERR("Session in progress but data structure is not initialized");
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
        gettimeofday(&cur_time, 0);
 
        data->tx_time = tx_time;
@@ -156,8 +181,11 @@ static GSList* is_app_present(GSList *start, uid_t uid, pid_t pid)
 
 void _bt_bm_add_transaction_details(uid_t uid, pid_t pid, int value, data_transaction_type_e type)
 {
+       if (is_session_started == FALSE)
+               __bt_start_session_time();
+
        if (current_session_data == NULL) {
-               BT_ERR("Session in progress but data structure is not initialized"); //error handling
+               BT_ERR("Session in progress but data structure is not initialized");
                return;
        }
        GSList *t = is_app_present(current_session_data->atm_list, uid, pid);
@@ -188,33 +216,55 @@ void _bt_bm_add_transaction_details(uid_t uid, pid_t pid, int value, data_transa
        }
 }
 
-void _bt_start_session_time()
+static int __bt_start_session_time(void)
 {
-       if (is_session_started == FALSE) {
-               BT_DBG("Bt session starting...");
-               is_session_started = TRUE;
-               current_session_data = g_malloc0(sizeof(_bt_battery_data_t));
-               current_session_data->session_start_time = time(NULL);
-               current_session_data->session_end_time = 0;
-               current_session_data->session_connected_time = 0;
-               current_session_data->session_scan_time = 0;
-               current_session_data->atm_list = NULL;
-       } else {
-               if (current_session_data == NULL)
-                       BT_ERR("Session in progress but data structure is not initialized"); //error handling
-               else
-                       BT_DBG("Bt session already in progress... Returning");
+       int state = 0;
+
+       if (is_session_started == TRUE) {
+               BT_ERR("Session is already started");
+               return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
+       }
+
+       if (vconf_get_bool(VCONFKEY_BATTERY_MONITOR_STATUS, &state) != 0) {
+               BT_ERR("vconf_get_bool failed");
+               return BLUETOOTH_ERROR_INTERNAL;
        }
+
+       if (state == 0) {
+               BT_ERR("Battery is not monitoring in now");
+               return BLUETOOTH_ERROR_NOT_SUPPORT;
+       }
+
+       BT_DBG("Bt session starting...");
+       is_session_started = TRUE;
+
+       if (current_session_data == NULL)
+               current_session_data = g_malloc0(sizeof(_bt_battery_data_t));
+
+       current_session_data->session_start_time = time(NULL);
+       current_session_data->session_end_time = 0;
+       current_session_data->session_connected_time = 0;
+       current_session_data->session_scan_time = 0;
+       current_session_data->atm_list = NULL;
+
+       return BLUETOOTH_ERROR_NONE;
 }
 
-void _bt_stop_session_time()
+void _bt_stop_session_time(void)
 {
        if (is_session_started == FALSE) {
-               BT_DBG("BT session not in progress... Returning"); //error handling
+               BT_DBG("BT session not in progress... Returning");
                return;
        }
+
        BT_DBG("Bt session ending...");
        is_session_started = FALSE;
+
+       if (current_session_data == NULL) {
+               BT_ERR("Session in progress but data structure is not initialized");
+               return;
+       }
+
        current_session_data->session_end_time = time(NULL);
        __bt_display_session_data();
 }
@@ -360,6 +410,13 @@ void _bt_bm_remove_scan_app(bt_bm_scan_type_e type, uid_t uid, pid_t pid)
 
 void _bt_start_scan_time()
 {
+       if (is_session_started == FALSE) {
+               if (__bt_start_session_time() != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Fail to start session time");
+                       return;
+               }
+       }
+
        if (current_session_data != NULL) {
                if (scan_cnt == 0) {
                        BT_DBG("Starting scan time");
@@ -368,14 +425,21 @@ void _bt_start_scan_time()
                }
                scan_cnt++;
        } else {
-               BT_ERR("Data structure uninitialized"); //error handling
+               BT_ERR("Data structure uninitialized");
        }
 }
 
 void _bt_stop_scan_time()
 {
+       if (is_session_started == FALSE) {
+               if (__bt_start_session_time() != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Fail to start session time");
+                       return;
+               }
+       }
+
        if (scan_cnt == 0 || current_session_data == NULL)
-               BT_ERR("Error encountered, returning..."); //error handling
+               BT_ERR("Error encountered, returning...");
        else {
                scan_cnt--;
                if(scan_cnt == 0) {
@@ -391,6 +455,13 @@ void _bt_stop_scan_time()
 
 void _bt_start_connect_time()
 {
+       if (is_session_started == FALSE) {
+               if (__bt_start_session_time() != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Fail to start session time");
+                       return;
+               }
+       }
+
        if (current_session_data != NULL) {
                if (connect_cnt == 0) {
                        BT_DBG("Starting connect time");
@@ -399,16 +470,22 @@ void _bt_start_connect_time()
                connect_cnt++;
        }
        else {
-               BT_ERR("Data structure uninitialized"); //error handling
+               BT_ERR("Data structure uninitialized");
        }
 }
 
 void _bt_stop_connect_time()
 {
-       if(connect_cnt == 0 || current_session_data == NULL) {
-               BT_ERR("Error encountered, returning..."); //error handling
+       if (is_session_started == FALSE) {
+               if (__bt_start_session_time() != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Fail to start session time");
+                       return;
+               }
        }
-       else {
+
+       if(connect_cnt == 0 || current_session_data == NULL) {
+               BT_ERR("Error encountered, returning...");
+       } else {
                connect_cnt--;
                if(connect_cnt == 0) {
                        struct timeval cur_time;
@@ -419,7 +496,7 @@ void _bt_stop_connect_time()
        }
 }
 
-static void _bt_notify_battery_data(void)
+static void __bt_notify_battery_data(void)
 {
        BT_INFO("+");
        _bt_battery_data_t *data = NULL;
@@ -479,12 +556,15 @@ void _bt_bm_event_handler(gpointer data)
        switch(event_type) {
        case OAL_EVENT_ADAPTER_ENABLED:
                BT_DBG("Handling Adapter Enabled");
-               _bt_start_session_time();
+               if (__bt_start_session_time() != BLUETOOTH_ERROR_NONE)
+                       BT_ERR("Fail to start session time");
                break;
        case OAL_EVENT_ADAPTER_DISABLED:
                BT_DBG("Handling Adapter Disabled");
-               _bt_stop_session_time();
-               _bt_notify_battery_data();
+               if (is_session_started == TRUE) {
+                       _bt_stop_session_time();
+                       __bt_notify_battery_data();
+               }
                break;
        case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
        case OAL_EVENT_BLE_DISCOVERY_STARTED: