Merge "[Non-ACR] improve SAM Score" into tizen
[platform/core/system/batterymonitor.git] / include / bm_server_db.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #ifndef __BM_SERVER_DB_H__
19 #define __BM_SERVER_DB_H__
20
21 #include <stdbool.h>
22 #include <glib.h>
23 #include <tzplatform_config.h>
24 #include <db-util.h>
25 #include <pthread.h>
26
27 #include "bm_private.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /* List of Resources
34 BM_RESOURCE_ID_BLE ==> rId_ble,
35 BM_RESOURCE_ID_WIFI,
36 BM_RESOURCE_ID_CPU,
37 BM_RESOURCE_ID_DISPLAY,
38 BM_RESOURCE_ID_DEVICE_NETWORK,
39 BM_RESOURCE_ID_GPS_SENSOR,
40 BM_RESOURCE_ID_BATTERY,
41 BM_RESOURCE_ID_MAX,*/
42
43 /* Battery Monitor AppId Vs Integer Map */
44 #define APP_ID_MAP_SCHEMA "create table appid_map \n"\
45         "(\n"\
46 "id INTEGER PRIMARY KEY AUTOINCREMENT, "\
47 "AppId TEXT "\
48 ");"
49
50 /* Battery Monitor APPID Vs RESOURCE ID */
51 #define APP_ID_USAGE_SCHEMA "create table appid_usage \n"\
52         "(\n"\
53 "AppId TEXT, "\
54 "log_time INT, "\
55 "rId_ble INT, "\
56 "rId_wifi INT, "\
57 "rId_cpu INT, "\
58 "rId_display INT, "\
59 "rId_device_network INT, "\
60 "rId_gps INT, "\
61 "rId_battery INT "\
62 ");"
63
64 /* Battery Monitor RESOURCE ID Vs TIME */
65 #define RESOURCE_ID_USAGE_SCHEMA "create table resourceid_usage \n"\
66         "(\n"\
67 "ResourceId TEXT, "\
68 "log_time INT, "\
69 "usage INT "\
70 ");"
71
72 #define RES_ID_BASE_USAGE_SCHEMA "create table res_base_usage \n"\
73         "(\n"\
74 "ResourceId TEXT, "\
75 "log_time INT, "\
76 "usage INT "\
77 ");"
78
79 #define GBL_BLE_STAT_SCHEMA "create table gble_stat \n"\
80         "(\n"\
81 "time_s LONG INT, "\
82 "ble_idle_time INT, "\
83 "ble_rx_time INT, "\
84 "ble_tx_time INT, "\
85 "ble_pwi_val INT "\
86 ");"
87
88 #define GBL_WFL_STAT_SCHEMA "create table gwfl_stat \n"\
89         "(\n"\
90 "time_s LONG INT, "\
91 "wifi_on_time INT, "\
92 "wifi_conn_time INT, "\
93 "wifi_idle_time INT, "\
94 "wifi_rx_time INT, "\
95 "wifi_tx_time INT, "\
96 "wifi_pwi_val INT "\
97 ");"
98
99 #define GBL_NT_STAT_SCHEMA "create table gn_stat \n"\
100         "(\n"\
101 "time_s LONG INT, "\
102 "dn_rx_byt INT, "\
103 "dn_tx_byt INT, "\
104 "wifi_rx_byt INT, "\
105 "wifi_tx_byt INT, "\
106 "dn_rx_pkt INT, "\
107 "dn_tx_pkt INT, "\
108 "wifi_rx_pkt INT, "\
109 "wifi_tx_pkt INT, "\
110 "bt_tx_byt INT, "\
111 "bt_rx_byt INT "\
112 ");"
113
114 #define GBL_BR_STAT_SCHEMA "create table gbr_stat \n"\
115         "(\n"\
116 "time_s LONG INT, "\
117 "dark INT, "\
118 "dim INT, "\
119 "medium INT, "\
120 "light INT, "\
121 "bright INT "\
122 ");"
123
124 #define GBL_SGT_STAT_SCHEMA "create table gsgt_stat \n"\
125         "(\n"\
126 "time_s LONG INT, "\
127 "scan_time INT, "\
128 "none_time INT, "\
129 "poor_time INT, "\
130 "med_time INT, "\
131 "good_time INT, "\
132 "grt_time INT "\
133 ");"
134
135 #define GBL_WST_STAT_SCHEMA "create table gwst_stat \n"\
136         "(\n"\
137 "time_s LONG INT, "\
138 "wscan_time INT, "\
139 "wnone_time INT, "\
140 "wpoor_time INT, "\
141 "wmed_time INT, "\
142 "wgood_time INT, "\
143 "wgrt_time INT "\
144 ");"
145
146 #define GBL_BST_STAT_SCHEMA "create table gbst_stat \n"\
147         "(\n"\
148 "time_s LONG INT, "\
149 "off_time INT, "\
150 "low_time INT, "\
151 "med_time INT, "\
152 "high_time INT "\
153 ");"
154
155 #define GBL_CPU_STAT_SCHEMA "create table gcpu_stat \n"\
156         "(\n"\
157 "time_s LONG INT, "\
158 "usr_time INT, "\
159 "sys_time INT "\
160 ");"
161
162 #define OWNER_ROOT 0
163
164 /* Macros of Time Duration */
165 #define BM_DURATION_1DAY                86400                   /*One Day in Seconds*/
166 #define BM_DURATION_1WEEK               604800                  /*One Week(7 Days) in Seconds*/
167
168 /* #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) */
169
170 #define BATTERY_MONITOR_APP_ID_MAP_TABLE                "appid_map"
171 #define BATTERY_MONITOR_APP_ID_USAGE_TABLE              "appid_usage"
172 #define BATTERY_MONITOR_RESOURCE_ID_USAGE_TABLE         "resourceid_usage"
173 #define BATTERY_MONITOR_RES_ID_BASE_USAGE_TABLE         "res_base_usage"
174 #define BATTERY_MONITOR_GBL_BLE_STAT_TABLE              "gble_stat"
175 #define BATTERY_MONITOR_GBL_WFL_STAT_TABLE              "gwfl_stat"
176 #define BATTERY_MONITOR_GBL_NT_STAT_TABLE               "gn_stat"
177 #define BATTERY_MONITOR_GBL_BR_STAT_TABLE               "gbr_stat"
178 #define BATTERY_MONITOR_GBL_SGT_STAT_TABLE              "gsgt_stat"
179 #define BATTERY_MONITOR_GBL_WST_STAT_TABLE              "gwst_stat"
180 #define BATTERY_MONITOR_GBL_BST_STAT_TABLE              "gbst_stat"
181 #define BATTERY_MONITOR_GBL_CPU_STAT_TABLE              "gcpu_stat"
182 #define BATTERY_MONITOR_SQLITE_SEQ                      "sqlite_sequence"
183 #define BATTERY_MONITOR_SQL_LEN_MAX                     1024
184 #define BATTERY_MONITOR_TABLE_COUNT                     12
185
186 typedef sqlite3_stmt *bm_stmt;
187
188 /* Server Functions */
189 int initialize_database(void);
190
191 int deinitialize_database(void);
192
193 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);
194
195 int bm_server_app_usage_insert_to_db(appid_usage_s *bm_app_type);
196
197 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);
198
199 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);
200
201 resourceid_usage_s *bm_server_query_battery_last_charge(const char *resource_id, int *error_code);
202
203 int bm_server_resource_usage_insert_to_db(resourceid_usage_s *bm_resource_type);
204
205 int bm_server_resource_base_usage_insert_to_db(resourceid_usage_s *bm_resource_type);
206
207 int bm_server_battery_dump_insert_to_db(void* str_data, int type);
208
209 int bm_server_battery_dump_query_from_db(void *str_data, int type, long int duration);
210
211 int bm_server_delete_table_by_appid(const char* app_id);
212
213 int bm_server_delete_table_by_time_interval(void);
214
215 int bm_server_appid_insert_to_db(const char* app_id);
216
217 GSList* bm_server_query_appid_map(int *error_code);
218
219 #ifdef __cplusplus
220 }
221 #endif
222
223 #endif /* __BM_SERVER_DB_H__ */