Merge "[Non-ACR] improve SAM Score" into tizen
authorAbhishek Vijay <abhishek.v@samsung.com>
Thu, 17 Sep 2020 13:33:26 +0000 (13:33 +0000)
committerGerrit Code Review <gerrit@review>
Thu, 17 Sep 2020 13:33:26 +0000 (13:33 +0000)
1  2 
include/bm_server_db.h
src/bm_server_db.c

diff --combined include/bm_server_db.h
  #include <glib.h>
  #include <tzplatform_config.h>
  #include <db-util.h>
+ #include <pthread.h>
  #include "bm_private.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /* List of Resources
  BM_RESOURCE_ID_BLE ==> rId_ble,
  BM_RESOURCE_ID_WIFI,
@@@ -189,10 -190,14 +190,10 @@@ int initialize_database(void)
  
  int deinitialize_database(void);
  
 -appid_usage_s *bm_server_query_app_usage_by_appid(const char *app_id, int period_type, int *error_code);
 -
  appid_usage_s *bm_server_query_app_usage_by_appid_ci(const char *app_id, long long s_time, long long e_time, int *error_code);
  
  int bm_server_app_usage_insert_to_db(appid_usage_s *bm_app_type);
  
 -resourceid_usage_s *bm_server_query_resource_usage_resourceid(const char *resource_id, int period_type, int *error_code);
 -
  resourceid_usage_s *bm_server_query_resource_usage_resourceid_ci(const char *resource_id, long long s_time, long long e_time, int *error_code);
  
  resourceid_usage_s *bm_server_query_resource_base_usage_resourceid_ci(const char *resource_id, long long s_time, long long e_time, int *error_code);
@@@ -214,6 -219,7 +215,7 @@@ int bm_server_delete_table_by_time_inte
  int bm_server_appid_insert_to_db(const char* app_id);
  
  GSList* bm_server_query_appid_map(int *error_code);
  #ifdef __cplusplus
  }
  #endif
diff --combined src/bm_server_db.c
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
 -#include <fcntl.h>
 -#include <unistd.h>
  #include <sys/stat.h>
  #include <glib.h>
  #include <db-util.h>
- #include <pthread.h>
  #include <sqlite3.h>
  #include <time.h>
  #include <tzplatform_config.h>
@@@ -39,7 -40,7 +38,7 @@@
  static sqlite3* g_hBatteryMonitorDB = NULL;
  static sqlite3* g_hBatteryMonitorDB2 = NULL;
  
pthread_mutex_t battery_monitor_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t battery_monitor_mutex;
  
  static const char* bm_db_err_msg(sqlite3 *bm_db_handle)
  {
@@@ -344,6 -345,18 +343,6 @@@ static int bm_query_app_usage_convert_t
        return count;
  }
  
 -static int bm_query_app_usage_convert_to_sql(const char *str, long s_t, bm_stmt hstmt)
 -{
 -      ENTER;
 -
 -      int count = 1;
 -      bm_query_bind_text(hstmt, count++, str);
 -      bm_query_bind_int(hstmt, count++, s_t);
 -
 -      EXIT;
 -      return count;
 -}
 -
  static int bm_appid_map_usage_convert_to_sql(const char* app_id, bm_stmt hstmt, char *sql_value)
  {
        ENTER;
@@@ -1507,6 -1520,88 +1506,6 @@@ CATCH
        return NULL;
  }
  
 -static appid_usage_s* bm_query_usage_by_appid(sqlite3 *bm_db_handle, const char *app_id, int *error_code, int period_type)
 -{
 -      ENTER;
 -
 -      long int duration  = 0;
 -      duration = bm_get_time_bias(period_type);
 -      if (duration == 0) {
 -              _ERR("Invalid period_type [%d]", period_type);
 -              return NULL;
 -      }
 -      _DBG("Value for duration is [%ld]", duration);
 -
 -      bm_stmt hstmt = NULL;
 -      char query[BATTERY_MONITOR_SQL_LEN_MAX] = {0, };
 -      int rc = 0;
 -      *error_code = BATTERY_MONITOR_ERROR_NONE;
 -
 -      BM_RETURN_VAL((app_id != NULL), { *error_code = BATTERY_MONITOR_ERROR_INVALID_PARAMETER; },
 -                                              NULL, ("APP ID IS NULL"));
 -      BM_RETURN_VAL((bm_db_handle != NULL), { *error_code = BATTERY_MONITOR_ERROR_DB_NOT_OPENED; },
 -                                              NULL, ("The database isn't connected."));
 -      BM_MEMSET(query, 0x00, BATTERY_MONITOR_SQL_LEN_MAX);
 -
 -      BM_SNPRINTF(query, sizeof(query), "SELECT AppId, sum(rId_ble), sum(rId_wifi), sum(rId_cpu), \
 -              sum(rId_display), sum(rId_device_network), sum(rId_gps) FROM %s WHERE \
 -              AppId = ? AND log_time >= ?", BATTERY_MONITOR_APP_ID_USAGE_TABLE);
 -
 -      hstmt = bm_prepare_query(bm_db_handle, query);
 -      if (bm_db_err_code(bm_db_handle) == SQLITE_PERM) {
 -              _ERR("Access failed(%s)", bm_db_err_msg(bm_db_handle));
 -              *error_code = BATTERY_MONITOR_ERROR_PERMISSION_DENIED;
 -              return NULL;
 -      }
 -
 -      bm_query_app_usage_convert_to_sql(app_id, duration, hstmt);
 -      appid_usage_s *bm_app_type = NULL;
 -      rc = bm_query_step(hstmt);
 -      BM_CATCH_ERROR_P(rc == SQLITE_ROW, {}, BATTERY_MONITOR_ERROR_RECORD_NOT_FOUND,
 -                              ("The record isn't found.(%s)\n", app_id));
 -
 -      bm_app_type = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
 -      if (bm_app_type == NULL) {
 -              _ERR("memory allocation failed");
 -              if (hstmt != NULL) {
 -                      rc = bm_query_finalize(hstmt);
 -                      BM_RETURN_VAL((rc == BATTERY_MONITOR_ERROR_NONE), { *error_code = rc; },
 -                                              NULL, ("finalize error"));
 -                      hstmt = NULL;
 -              }
 -              *error_code = BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
 -              return NULL;
 -      }
 -
 -      bm_convert_column_to_appid_usage(hstmt, bm_app_type);
 -
 -      rc = bm_query_finalize(hstmt);
 -      BM_CATCH_ERROR_P((rc == BATTERY_MONITOR_ERROR_NONE), {}, rc, ("finalize error"));
 -      hstmt = NULL;
 -      *error_code = BATTERY_MONITOR_ERROR_NONE;
 -
 -CATCH:
 -      if (hstmt != NULL) {
 -              rc = bm_query_finalize(hstmt);
 -              if (rc != BATTERY_MONITOR_ERROR_NONE) {
 -                      *error_code = rc;
 -                      _ERR("finalize error");
 -              }
 -              hstmt = NULL;
 -      }
 -      if ((*error_code != BATTERY_MONITOR_ERROR_NONE) && bm_app_type) {
 -              BM_FREE(bm_app_type);
 -              bm_app_type = NULL;
 -      }
 -      if ((*error_code == BATTERY_MONITOR_ERROR_NONE) && bm_app_type != NULL) {
 -              _INFO("Returning appid usage");
 -              return bm_app_type;
 -      }
 -
 -      EXIT;
 -      return NULL;
 -}
 -
  static appid_usage_s* bm_query_usage_by_appid_ci(sqlite3 *bm_db_handle, const char *app_id, long long s_time, long long e_time, int *error_code)
  {
        ENTER;
@@@ -1729,6 -1824,86 +1728,6 @@@ CATCH
        return error_code;
  }
  
 -static resourceid_usage_s* bm_query_usage_by_resourceid(sqlite3 *bm_db_handle, const char *resource_id, int *error_code, int period_type)
 -{
 -      ENTER;
 -
 -      long int duration  = 0;
 -      duration = bm_get_time_bias(period_type);
 -      if (duration == 0) {
 -              _ERR("Invalid period_type [%d]", period_type);
 -              return NULL;
 -      }
 -
 -      _DBG("Value for duration is [%ld]", duration);
 -
 -      bm_stmt hstmt = NULL;
 -      char query[BATTERY_MONITOR_SQL_LEN_MAX] = {0, };
 -      int rc = 0;
 -      *error_code = BATTERY_MONITOR_ERROR_NONE;
 -
 -      BM_RETURN_VAL((resource_id != NULL), { *error_code = BATTERY_MONITOR_ERROR_INVALID_PARAMETER; },
 -                                      NULL, ("RESOURCE ID IS NULL"));
 -      BM_RETURN_VAL((bm_db_handle != NULL), { *error_code = BATTERY_MONITOR_ERROR_DB_NOT_OPENED; },
 -                                      NULL, ("The database isn't connected."));
 -      BM_MEMSET(query, 0x00, BATTERY_MONITOR_SQL_LEN_MAX);
 -      BM_SNPRINTF(query, sizeof(query), "SELECT ResourceId, sum(usage) FROM %s WHERE ResourceId = '%s' \
 -                      AND log_time >= %ld", BATTERY_MONITOR_RESOURCE_ID_USAGE_TABLE, resource_id, duration);
 -
 -      hstmt = bm_prepare_query(bm_db_handle, query);
 -      if (bm_db_err_code(bm_db_handle) == SQLITE_PERM) {
 -              _ERR("Access failed(%s)", bm_db_err_msg(bm_db_handle));
 -              *error_code = BATTERY_MONITOR_ERROR_PERMISSION_DENIED;
 -              return NULL;
 -      }
 -
 -      resourceid_usage_s *bm_resource_type = NULL;
 -
 -      rc = bm_query_step(hstmt);
 -      BM_CATCH_ERROR_P(rc == SQLITE_ROW, {}, BATTERY_MONITOR_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", resource_id));
 -
 -      bm_resource_type = (resourceid_usage_s *)calloc(1, sizeof(resourceid_usage_s));
 -      if (bm_resource_type == NULL) {
 -              _ERR("memory allocation failed");
 -              if (hstmt != NULL) {
 -                      rc = bm_query_finalize(hstmt);
 -                      BM_RETURN_VAL((rc == BATTERY_MONITOR_ERROR_NONE), { *error_code = rc; }, NULL, ("finalize error"));
 -                      hstmt = NULL;
 -              }
 -              *error_code = BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
 -              return NULL;
 -      }
 -
 -      bm_convert_column_to_resourceid_usage(hstmt, bm_resource_type);
 -
 -      rc = bm_query_finalize(hstmt);
 -      BM_CATCH_ERROR_P((rc == BATTERY_MONITOR_ERROR_NONE), {}, rc, ("finalize error"));
 -      hstmt = NULL;
 -
 -      *error_code = BATTERY_MONITOR_ERROR_NONE;
 -
 -CATCH:
 -      if (hstmt != NULL) {
 -              rc = bm_query_finalize(hstmt);
 -              if (rc != BATTERY_MONITOR_ERROR_NONE) {
 -                      *error_code = rc;
 -                      _ERR("finalize error");
 -              }
 -              hstmt = NULL;
 -      }
 -      if ((*error_code != BATTERY_MONITOR_ERROR_NONE) && bm_resource_type) {
 -              BM_FREE(bm_resource_type);
 -              bm_resource_type = NULL;
 -      }
 -      if ((*error_code == BATTERY_MONITOR_ERROR_NONE) && bm_resource_type != NULL) {
 -              _INFO("Returning appid usage");
 -              return bm_resource_type;
 -      }
 -
 -      EXIT;
 -      return NULL;
 -}
 -
  static resourceid_usage_s* bm_query_usage_by_resourceid_ci(sqlite3 *bm_db_handle, const char *resource_id, long long s_time, long long e_time, int *error_code)
  {
        ENTER;
@@@ -2272,6 -2447,22 +2271,6 @@@ int bm_server_app_usage_insert_to_db(ap
  }
  
  /* For App usage related queries */
 -appid_usage_s *bm_server_query_app_usage_by_appid(const char *app_id, int period_type, int *error_code)
 -{
 -      ENTER;
 -
 -      appid_usage_s *bm_app_type = NULL;
 -      *error_code = BATTERY_MONITOR_ERROR_NONE;
 -
 -      BM_RETURN_VAL((g_hBatteryMonitorDB != NULL), { *error_code =  BATTERY_MONITOR_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
 -      BM_RETURN_VAL((app_id != NULL), { *error_code = BATTERY_MONITOR_ERROR_INVALID_PARAMETER; }, NULL, ("The Battery Monitor Handle is NULL"));
 -
 -      bm_app_type = bm_query_usage_by_appid(g_hBatteryMonitorDB, app_id, error_code, period_type);
 -
 -      EXIT;
 -      return bm_app_type;
 -}
 -
  appid_usage_s *bm_server_query_app_usage_by_appid_ci(const char *app_id, long long s_time, long long e_time, int *error_code)
  {
        ENTER;
  }
  
  /* For Resource usage related queries */
 -resourceid_usage_s *bm_server_query_resource_usage_resourceid(const char *resource_id, int period_type, int *error_code)
 -{
 -      ENTER;
 -
 -      resourceid_usage_s *bm_resource_type = NULL;
 -      *error_code = BATTERY_MONITOR_ERROR_NONE;
 -
 -      BM_RETURN_VAL((g_hBatteryMonitorDB != NULL), { *error_code = BATTERY_MONITOR_ERROR_DB_NOT_OPENED; }, NULL, ("The database isn't connected."));
 -      BM_RETURN_VAL((resource_id != NULL), { *error_code = BATTERY_MONITOR_ERROR_INVALID_PARAMETER; }, NULL, ("The Battery Monitor Handle is NULL"));
 -
 -      bm_resource_type = bm_query_usage_by_resourceid(g_hBatteryMonitorDB, resource_id, error_code, period_type);
 -
 -      EXIT;
 -      return bm_resource_type;
 -}
 -
  resourceid_usage_s *bm_server_query_resource_usage_resourceid_ci(const char *resource_id, long long s_time, long long e_time, int *error_code)
  {
        resourceid_usage_s *bm_resource_type = NULL;
@@@ -2543,6 -2750,9 +2542,9 @@@ int initialize_database(void
  
        int return_code = BATTERY_MONITOR_ERROR_NONE;
  
+       if (pthread_mutex_init(&battery_monitor_mutex, NULL))
+               _ERR("error initializing mutex");
        return_code = bm_db_open();
        if (return_code != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("Battery Monitor DB Open Error");
@@@ -2565,6 -2775,9 +2567,9 @@@ int deinitialize_database(void
                return -1;
        }
  
+       if (pthread_mutex_destroy(&battery_monitor_mutex))
+               _ERR("error destroying mutex");
        EXIT;
        return BATTERY_MONITOR_ERROR_NONE;
  }