Add CPU stats for Battery Dump
[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 "bm_private.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /* BATTERY_DB */
32 /* THIS DB IS THE DEFAULT DB TO BE USED FOR LOGGING APP BASED USAGE AND RESOURCE BASED USAGE IT WILL BE USED TO STORE PROCESSED VALUE OF THE USAGE, AND WILL BE REFERRED FOR ANSWERING API CALLS */
33
34 /* List of Resources
35 BM_RESOURCE_ID_BLE ==> rId_ble,
36 BM_RESOURCE_ID_WIFI,
37 BM_RESOURCE_ID_CPU,
38 BM_RESOURCE_ID_DISPLAY,
39 BM_RESOURCE_ID_DEVICE_NETWORK,
40 BM_RESOURCE_ID_GPS_SENSOR,
41 BM_RESOURCE_ID_ID_HRM_SENSOR,
42 BM_RESOURCE_ID_BATTERY,
43 BM_RESOURCE_ID_MAX,
44 */
45
46 /* Battery Monitor AppId Vs Integer Map */
47 /* TO DO: Cleaning of redundant records from DB */
48 #define APP_ID_MAP_SCHEMA "create table appid_map \n"\
49         "(\n"\
50 "id INTEGER PRIMARY KEY AUTOINCREMENT, "\
51 "AppId TEXT "\
52 ");"
53
54 /* Battery Monitor APPID Vs RESOURCE ID */
55 #define APP_ID_USAGE_SCHEMA "create table appid_usage \n"\
56         "(\n"\
57 "AppId TEXT, "\
58 "log_time INT, "\
59 "rId_ble INT, "\
60 "rId_wifi INT, "\
61 "rId_cpu INT, "\
62 "rId_display INT, "\
63 "rId_device_network INT, "\
64 "rId_gps INT, "\
65 "rId_hrm INT, "\
66 "rId_battery INT "\
67 ");"
68
69 /* Battery Monitor RESOURCE ID Vs TIME */
70 /* TO DO: Adding Enum for Resources */
71 #define RESOURCE_ID_USAGE_SCHEMA "create table resourceid_usage \n"\
72         "(\n"\
73 "ResourceId TEXT, "\
74 "log_time INT, "\
75 "usage INT "\
76 ");"
77
78 #define GBL_BLE_STAT_SCHEMA "create table gble_stat \n"\
79         "(\n"\
80 "time_s LONG INT, "\
81 "ble_idle_time INT, "\
82 "ble_rx_time INT, "\
83 "ble_tx_time INT, "\
84 "ble_pwi_val INT "\
85 ");"
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_GBL_BLE_STAT_TABLE              "gble_stat"
174 #define BATTERY_MONITOR_GBL_WFL_STAT_TABLE              "gwfl_stat"
175 #define BATTERY_MONITOR_GBL_NT_STAT_TABLE               "gn_stat"
176 #define BATTERY_MONITOR_GBL_BR_STAT_TABLE               "gbr_stat"
177 #define BATTERY_MONITOR_GBL_SGT_STAT_TABLE              "gsgt_stat"
178 #define BATTERY_MONITOR_GBL_WST_STAT_TABLE              "gwst_stat"
179 #define BATTERY_MONITOR_GBL_BST_STAT_TABLE              "gbst_stat"
180 #define BATTERY_MONITOR_GBL_CPU_STAT_TABLE              "gcpu_stat"
181 #define BATTERY_MONITOR_SQLITE_SEQ                      "sqlite_sequence"
182 #define BATTERY_MONITOR_SQL_LEN_MAX                     1024
183 #define BATTERY_MONITOR_TABLE_COUNT                     11
184
185 typedef sqlite3_stmt *bm_stmt;
186
187 /* Server Functions */
188 int initialize_database(void);
189
190 int deinitialize_database(void);
191
192 appid_usage_s *bm_server_query_app_usage_by_appid(const char *app_id, int period_type, int *error_code);
193
194 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);
195
196 int bm_server_app_usage_insert_to_db(appid_usage_s *bm_app_type);
197
198 resourceid_usage_s *bm_server_query_resource_usage_resourceid(const char *resource_id, int period_type, int *error_code);
199
200 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);
201
202 int bm_server_resource_usage_insert_to_db(resourceid_usage_s *bm_resource_type);
203
204 int bm_server_battery_dump_insert_to_db(void* str_data, int type);
205
206 int bm_server_battery_dump_query_from_db(void *str_data, int type, long int duration);
207
208 int bm_server_delete_table_by_appid(const char* app_id);
209
210 int bm_server_delete_table_by_time_interval(void);
211
212 int bm_server_appid_insert_to_db(const char* app_id);
213
214 GSList* bm_server_query_appid_map(int *error_code);
215 #ifdef __cplusplus
216 }
217 #endif
218
219 #endif /* __BM_SERVER_DB_H__ */