Adding: Mapping of AppId to numerical value 76/217176/2 accepted/tizen/unified/20191108.065932 submit/tizen/20191107.135014
authorDewal Agarwal <d1.agarwal@samsung.com>
Thu, 7 Nov 2019 07:58:20 +0000 (13:28 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Thu, 7 Nov 2019 09:32:53 +0000 (15:02 +0530)
Change-Id: I54e7b89849c15160e36ab6686ca5a2977da9b171
Signed-off-by: Dewal Agarwal <d1.agarwal@samsung.com>
include/bm_private.h
include/bm_server_db.h
packaging/batterymonitor.spec
src/bm_power_engine.c
src/bm_server_db.c
src/bm_util.c

index 823f1fe..b117e1c 100644 (file)
@@ -31,8 +31,8 @@ extern "C"
 #endif
 
 typedef struct _appid_map_s {
-       char *AppName;
-       int AppId;
+       int id;
+       char *AppId;
 } appid_map_s;
 
 typedef struct _appid_usage_s {
index 50fa068..71a1083 100644 (file)
@@ -47,8 +47,8 @@ BM_RESOURCE_ID_MAX,
 /* TO DO: Cleaning of redundant records from DB */
 #define APP_ID_MAP_SCHEMA "create table appid_map \n"\
        "(\n"\
-"AppName TEXT, "\
-"AppId INT "\
+"id INTEGER PRIMARY KEY AUTOINCREMENT, "\
+"AppId TEXT "\
 ");"
 
 /* Battery Monitor APPID Vs RESOURCE ID */
@@ -112,6 +112,7 @@ int bm_server_delete_table_by_appid(const char* app_id);
 
 int bm_server_delete_table_by_time_interval(void);
 
+int bm_server_appid_insert_to_db(const char* app_id);
 #ifdef __cplusplus
 }
 #endif
index 4a77bec..d7a1a21 100644 (file)
@@ -1,6 +1,6 @@
 Name:          batterymonitor
 Summary:       batterymonitor service daemon
-Version:       0.0.1
+Version:       0.0.2
 Release:       1
 License:       Apache-2.0
 Source0:       %{name}-%{version}.tar.gz
@@ -77,7 +77,7 @@ mkdir -p %{TZ_SYS_DB}
 if [ ! -f %{TZ_SYS_DB}/.battery-monitor.db ]
 then
        sqlite3 %{TZ_SYS_DB}/.battery-monitor.db 'PRAGMA journal_mode = PERSIST;
-       CREATE TABLE if not exists appid_map (AppName TEXT, AppId INT);
+       CREATE TABLE if not exists appid_map (id INTEGER PRIMARY KEY AUTOINCREMENT, AppId TEXT);
        CREATE TABLE if not exists appid_usage (AppId TEXT, log_time INT, rId_ble INT, rId_wifi INT, rId_cpu INT, rId_display INT, rId_device_network INT,
        rId_gps INT, rId_hrm INT, rId_battery INT, rId_newone INT, rId_newtwo INT, rId_newthree INT);
        CREATE TABLE if not exists resourceid_usage (ResourceId TEXT, log_time INT, usage INT);
index 62424bf..cced36b 100644 (file)
@@ -30,7 +30,7 @@
 bm_feature_data_h bm_data_handle = NULL;
 GHashTable *gl_hash = NULL;
 static struct bm_req_feature_data_handle_flag_s *bm_req_flag_h = NULL;
-static int battery_capacity = 500;
+int battery_capacity = 500;
 
 int bm_engine_get_usage_by_app_id_for_resource_id(const gchar *app_id, gint resource_id, gint duration, int *b_usage)
 {
@@ -372,6 +372,12 @@ static int bm_insert_appid_session_usage_to_db()
                        app_usage->rId_wifi, app_usage->rId_cpu, app_usage->rId_display,
                                        app_usage->rId_device_network);
 
+               ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
+               if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                       _ERR("Insert of Appid failed ");
+               else
+                       _DBG("Insert successful");
+
                ret_val = bm_server_app_usage_insert_to_db(app_usage);
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
                        _ERR("Insert of App Usage failed ");
@@ -527,9 +533,12 @@ int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode)
                _DBG("Calculated Power for Appid(%s) -  P_power_app_bt(%d), P_power_app_conn(%d), P_power_app_data(%d)",
                                        appid, P_power_app_bt, P_power_app_conn, P_power_app_data);
 
-               if (mode)
+               if (mode) {
+                       ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
+                       if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                               _ERR("Insert of BT appid failed ");
                        ret_val = bm_server_app_usage_insert_to_db(app_usage);
-               else/* Call Global Hash */
+               else/* Call Global Hash */
                        ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_ble, BM_PLUGIN_ID_BLE);
 
                /* Error Check */
@@ -728,9 +737,12 @@ int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode)
                _DBG("Calculated Power for Appid(%s) -  P_power_app_wifi(%d), P_power_app_conn(%d), P_power_app_data(%d)",
                                        appid, P_power_app_wifi, P_power_app_conn, P_power_app_data);
 
-               if (mode)
+               if (mode) {
+                       ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
+                       if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                               _ERR("Insert of WiFi appid failed ");
                        ret_val = bm_server_app_usage_insert_to_db(app_usage);
-               else/* Call Global Hash */
+               else/* Call Global Hash */
                        ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_wifi, BM_PLUGIN_ID_WIFI);
 
                /* Error Check */
@@ -888,9 +900,13 @@ int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
 
                /* Call Insert Function */
                _DBG("Calculated Power for Appid (%s) P_power_app_cpu (%d),  P_power_app_use (%d)", appid, P_power_app_cpu, P_power_app_use);
-               if (mode)
+
+               if (mode) {
+                       ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
+                       if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                               _ERR("Insert of CPU appid failed ");
                        ret_val = bm_server_app_usage_insert_to_db(app_usage);
-               else
+               else
                        ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_cpu, BM_PLUGIN_ID_CPU);
                /* Error Check */
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
@@ -964,7 +980,6 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
                sesTime += edTime - stTime;
 
                dp_atm_iterator = datalistnode->atm_list;
-//             dp_atm_iterator = ((bm_display_st *)(dp_data_iterator->data))->atm_list;
 
                for ( ; dp_atm_iterator; dp_atm_iterator = dp_atm_iterator->next) {
 
@@ -1056,9 +1071,12 @@ int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
 
                /* Call Insert Function */
                _DBG("Calculated Power for Appid (%s) P_power_app_disp(%d),  P_power_app_on (%d)", appid, P_power_app_disp, P_power_app_on);
-               if (mode)
+               if (mode) {
+                       ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
+                       if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                               _ERR("Insert of DSP appid failed ");
                        ret_val = bm_server_app_usage_insert_to_db(app_usage);
-               else
+               else
                        ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_display, BM_PLUGIN_ID_DISPLAY);
                /* Error Check */
                if (ret_val != BATTERY_MONITOR_ERROR_NONE)
@@ -1240,9 +1258,12 @@ int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool m
 
                /* Call Insert Function */
                _DBG("Calculated Power for Appid (%s) P_power_app_dntw(%d),  P_power_app_conn (%d), P_power_app_data(%d)", appid, P_power_app_dntw, P_power_app_conn, P_power_app_data);
-               if (mode)
+               if (mode) {
+                       ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
+                       if (ret_val != BATTERY_MONITOR_ERROR_NONE)
+                               _ERR("Insert of DN appid failed ");
                        ret_val = bm_server_app_usage_insert_to_db(app_usage);
-               else
+               else
                        ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_device_network, BM_PLUGIN_ID_DEVICE_NETWORK);
 
                /* Error Check */
index c31c72f..044725d 100644 (file)
@@ -339,7 +339,7 @@ static int bm_db_handle_close(sqlite3 *bm_db_handle)
        return ret;
 }
 
-static int bm_appid_map_usage_convert_to_sql(appid_map_s *bm_appid_map_type, bm_stmt hstmt, char *sql_value)
+static int bm_appid_map_usage_convert_to_sql(const char* app_id, bm_stmt hstmt, char *sql_value)
 {
        ENTER;
 
@@ -347,9 +347,7 @@ static int bm_appid_map_usage_convert_to_sql(appid_map_s *bm_appid_map_type, bm_
 
        /* Caution : Keep insert query orders. */
 
-       bm_query_bind_text(hstmt, count++, (char *)bm_appid_map_type->AppName);
-
-       bm_query_bind_int(hstmt, count++, bm_appid_map_type->AppId);
+       bm_query_bind_text(hstmt, count++, app_id);
 
        EXIT;
        return count;
@@ -703,6 +701,7 @@ static int bm_db_close(void)
        return ret;
 }
 
+/*
 int bm_get_appint_by_app_name(sqlite3 *bm_db_handle, const char *AppId)
 {
        ENTER;
@@ -749,19 +748,22 @@ int bm_get_appint_by_app_name(sqlite3 *bm_db_handle, const char *AppId)
        EXIT;
        return ncount;
 }
+*/
 
-static gboolean app_name_check_duplicated(sqlite3 *bm_db_handle, const char *AppId)
+static gboolean app_name_check_duplicated(sqlite3 *bm_db_handle, const char *app_id, int *error_code)
 {
        ENTER;
 
        char query[BATTERY_MONITOR_SQL_LEN_MAX] = {0, };
        int count = 0;
+       *error_code = BATTERY_MONITOR_ERROR_NONE;
+
+       BM_RETURN_VAL((bm_db_handle != NULL), { *error_code = BATTERY_MONITOR_ERROR_DB_NOT_OPENED; }, false, ("The database isn't connected."));
+       BM_RETURN_VAL((app_id != NULL), { *error_code = BATTERY_MONITOR_ERROR_INVALID_PARAMETER; }, false, ("app_id is NULL."));
 
-       BM_RETURN_VAL((bm_db_handle != NULL), {}, BATTERY_MONITOR_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
-       BM_RETURN_VAL((AppId != NULL), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, ("AppId is NULL."));
        BM_MEMSET(query, 0x00, sizeof(query));
 
-       BM_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppName='%s'", BATTERY_MONITOR_APP_ID_MAP_TABLE, AppId);
+       BM_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'", BATTERY_MONITOR_APP_ID_MAP_TABLE, app_id);
 
        count = bm_get_record_count(bm_db_handle, query);
        if (count > 0) {
@@ -880,7 +882,7 @@ static int bm_resourceid_usage_insert_to_db(sqlite3 *bm_db_handle, resourceid_us
        return error_code;
 }
 
-int bm_appid_map_insert_to_db(sqlite3 *bm_db_handle, appid_map_s *bm_appid_map_type)
+int bm_appid_map_insert_to_db(sqlite3 *bm_db_handle, const char *app_id)
 {
        ENTER;
 
@@ -888,23 +890,17 @@ int bm_appid_map_insert_to_db(sqlite3 *bm_db_handle, appid_map_s *bm_appid_map_t
        char query[BATTERY_MONITOR_SQL_LEN_MAX] = {0, };
        int error_code = BATTERY_MONITOR_ERROR_NONE;
        bm_stmt hstmt = NULL;
-       bool to_insert = false;
 
        BM_RETURN_VAL((bm_db_handle != NULL), {}, BATTERY_MONITOR_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
-       BM_RETURN_VAL((bm_appid_map_type != NULL), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, ("The battery monitor handle is NULL"));
 
        /* check mandatory field(app id)*/
-       if (!bm_appid_map_type->AppName)
+       if (!app_id)
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
 
-       to_insert = app_name_check_duplicated(bm_db_handle, bm_appid_map_type->AppName);
-       if (to_insert)
-               return BATTERY_MONITOR_ERROR_DUPLICATED;
-
        /* The AppId integer value should be auto increment */
        BM_MEMSET(query, 0x00, sizeof(query));
-       BM_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppName, AppId ) values "
-                       "(?, ?)", BATTERY_MONITOR_APP_ID_MAP_TABLE);
+       BM_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId ) values "
+                       "(?)", BATTERY_MONITOR_APP_ID_MAP_TABLE);
 
        hstmt = bm_prepare_query(bm_db_handle, query);
 
@@ -918,7 +914,7 @@ int bm_appid_map_insert_to_db(sqlite3 *bm_db_handle, appid_map_s *bm_appid_map_t
 
        BM_RETURN_VAL((hstmt != NULL), {}, BATTERY_MONITOR_ERROR_DB_FAILED, ("bm_prepare_query() failed(%s).\n", bm_db_err_msg(bm_db_handle)));
 
-       bm_appid_map_usage_convert_to_sql(bm_appid_map_type, hstmt, query);
+       bm_appid_map_usage_convert_to_sql(app_id, hstmt, query);
 
        rc = bm_query_step(hstmt);
        if (rc == SQLITE_BUSY) {
@@ -1306,6 +1302,64 @@ Manager Functions
 >-------Only these will be exposed for other files to use.
 */
 
+int bm_server_appid_insert_to_db(const char* app_id)
+{
+       ENTER;
+
+       int error_code = BATTERY_MONITOR_ERROR_NONE;
+       int ret_transaction = 0;
+       int to_insert = true;
+
+       BM_RETURN_VAL((g_hBatteryMonitorDB != NULL), {}, BATTERY_MONITOR_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
+       BM_RETURN_VAL((app_id != NULL), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, ("Mandatory field app_id NULL"));
+
+       /* Checking if already inserted */
+       to_insert = app_name_check_duplicated(g_hBatteryMonitorDB, app_id, &error_code);
+       if (to_insert && error_code == BATTERY_MONITOR_ERROR_NONE) {
+               _DBG("AppID - [%s]  already exists", app_id);
+               return BATTERY_MONITOR_ERROR_NONE;
+       } else if (error_code != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("Duplicate check failed");
+               return error_code;
+       }
+
+       pthread_mutex_lock(&battery_monitor_mutex);
+
+       /* Transaction Control Required */
+       ret_transaction = bm_begin_transaction(g_hBatteryMonitorDB);
+
+       if (bm_db_err_code(g_hBatteryMonitorDB) == SQLITE_PERM) {
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               _ERR("Access failed(%s)", bm_db_err_msg(g_hBatteryMonitorDB));
+               return BATTERY_MONITOR_ERROR_PERMISSION_DENIED;
+       }
+
+       if (ret_transaction == BATTERY_MONITOR_ERROR_DATABASE_BUSY) {
+               _ERR("AppId map table insert:bm_begin_transaction fail %d\n", ret_transaction);
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
+       } else if (ret_transaction != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("AppId map table insert:bm_begin_transaction fail %d\n", ret_transaction);
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               return ret_transaction;
+       }
+
+       error_code = bm_appid_map_insert_to_db(g_hBatteryMonitorDB, app_id);
+
+       if (error_code != BATTERY_MONITOR_ERROR_NONE) {
+               ret_transaction = bm_end_transaction(g_hBatteryMonitorDB, false);
+               _ERR("INSERT appid fail, rollback insert query(%d)!!!!\n", ret_transaction);
+               pthread_mutex_unlock(&battery_monitor_mutex);
+               return error_code;
+       }
+
+       pthread_mutex_unlock(&battery_monitor_mutex);
+       bm_end_transaction(g_hBatteryMonitorDB, true);
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
 int bm_server_resource_usage_insert_to_db(resourceid_usage_s *bm_resource_type)
 {
        ENTER;
@@ -1375,11 +1429,11 @@ int bm_server_app_usage_insert_to_db(appid_usage_s *bm_app_type)
        }
 
        if (ret_transaction == BATTERY_MONITOR_ERROR_DATABASE_BUSY) {
-               _ERR("Resource Usage insert:bm_begin_transaction fail %d\n", ret_transaction);
+               _ERR("App Usage insert:bm_begin_transaction fail %d\n", ret_transaction);
                pthread_mutex_unlock(&battery_monitor_mutex);
                return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
        } else if (ret_transaction != BATTERY_MONITOR_ERROR_NONE) {
-               _ERR("Resource Usage insert:bm_begin_transaction fail %d\n", ret_transaction);
+               _ERR("App Usage insert:bm_begin_transaction fail %d\n", ret_transaction);
                pthread_mutex_unlock(&battery_monitor_mutex);
                return ret_transaction;
        }
@@ -1388,7 +1442,7 @@ int bm_server_app_usage_insert_to_db(appid_usage_s *bm_app_type)
 
        if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                ret_transaction = bm_end_transaction(g_hBatteryMonitorDB, false);
-               _ERR("INSERT resource usage fail, rollback insert query(%d)!!!!\n", ret_transaction);
+               _ERR("INSERT app usage fail, rollback insert query(%d)!!!!\n", ret_transaction);
                pthread_mutex_unlock(&battery_monitor_mutex);
                return error_code;
        }
index e3bea0a..f4976e4 100644 (file)
@@ -292,11 +292,12 @@ const char *bm_get_resource_id_string(gint resource_id)
        BM_RESOURCE_ID_TO_STR(BM_PLUGIN_ID_CPU, BATTERY_MONITOR_CPU_DATA_ID)
        BM_RESOURCE_ID_TO_STR(BM_PLUGIN_ID_DISPLAY, BATTERY_MONITOR_DP_DATA_ID)
        BM_RESOURCE_ID_TO_STR(BM_PLUGIN_ID_DEVICE_NETWORK, BATTERY_MONITOR_DN_DATA_ID)
-#ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
        BM_RESOURCE_ID_TO_STR(BM_PLUGIN_ID_GPS_SENSOR, BATTERY_MONITOR_GPS_DATA_ID)
+#ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
        BM_RESOURCE_ID_TO_STR(BM_PLUGIN_ID_HRM_SENSOR, BATTERY_MONITOR_HRM_DATA_ID)
        BM_RESOURCE_ID_TO_STR(BM_PLUGIN_CONST_BATTERY, BATTERY_MONITOR_BATTERY_DATA_ID)
 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
+
        default:
                EXIT;
                return "UNKNOWN RESOURCE-ID";