Dump the start/end time of usage collection time
[platform/core/system/batterymonitor.git] / src / battery_dump / bd_history_item.c
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 #include <dumpsys-system.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <sys/time.h>
22 #include <glib/gprintf.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include "bd_history_item.h"
26 #include "bm_util.h"
27 #include "bm_dbg.h"
28 #include "bm_common.h"
29 #include "bm_server_db.h"
30 #include "bm_private.h"
31 #include "bd_private.h"
32 #include "bm_config_parser.h"
33
34 //#define DUMP_DUMMY 1
35 #ifdef DUMP_DUMMY
36 #include "bd_history_item_dummy.h"
37 #endif
38
39 dump_data_s history_data[HISTORY_SIZE_MAX];
40 int h_count = 0; int h_flag = false;
41 char state1_map[32][4] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Wm",
42         "", "Sd", "BP", "S", "Psc", "a", "s", "", "Pr", "Wr", "Ws", "Wl", "g", "w", "r"};
43 char state2_map[32][5] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
44         "", "Ud", "Chtp", "bles", "ca", "b", "Pcl", "ch", "", "", "fl", "W", "Ww", "v", "ps"};
45 char event_map[32][4] = {"Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
46         "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw", "Esw", "Ewa", "Elw", "Eec"};
47 char usage_map[32][4] = {"Bu", "Wu", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
48         "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
49 GHashTable *app_map = NULL;
50 static long int lst_charge = -1;
51
52 static long long min_idle_check_time;
53
54 static int bd_print_pool_entry(int fd, int idx, int pc, history_tag_s *pool_info)
55 {
56         ENTER;
57
58         BM_CHECK_INPUT_PARAM(pool_info);
59         _DBG("Value from history data %d", idx);
60
61         GString *dump_p = g_string_sized_new(30);
62         BM_CHECK_MEM_ALLOC(dump_p, {});
63
64         g_string_append(dump_p, TIZEN_DUMP_VERSION);
65         g_string_append_c(dump_p, ',');
66         g_string_append(dump_p, DUMP_DATA_TAG_1);
67         g_string_append_c(dump_p, ',');
68         g_string_append_printf(dump_p, "%d,", pc);
69         g_string_append_printf(dump_p, "%d,", pool_info->uid);
70         g_string_append(dump_p, "\"");
71
72         if (pool_info->string_info != NULL) {
73                 gchar *temp = g_strdup(pool_info->string_info->str);
74                 g_string_append(dump_p, temp);
75                 g_free(temp);
76         }
77
78         g_string_append(dump_p, "\"");
79         g_string_append(dump_p, "\n");
80
81         _DBG("%s", dump_p->str);
82
83         if (write(fd, dump_p->str, dump_p->len) < 0)
84                 _WARN("write error");
85
86         g_string_free(dump_p, TRUE);
87
88         EXIT;
89         return BATTERY_MONITOR_ERROR_NONE;
90 }
91
92 static int bd_get_pool_entry_from_dump(int fd)
93 {
94         ENTER;
95
96         GString *dump_p = g_string_sized_new(30);
97         BM_CHECK_MEM_ALLOC(dump_p, {});
98
99         g_string_append(dump_p, TIZEN_DUMP_VERSION);
100         g_string_append(dump_p, ",0");
101         g_string_append(dump_p, ",i");
102         g_string_append(dump_p, ",vers");
103         g_string_append(dump_p, ",19,150,NMF26V,NMF26V");
104         g_string_append(dump_p, "\n");
105
106         _DBG("%s", dump_p->str);
107
108         if (write(fd, dump_p->str, dump_p->len) < 0)
109                 _WARN("write error");
110         g_string_free(dump_p, TRUE);
111
112         int ret = BATTERY_MONITOR_ERROR_NONE;
113         int log_count = h_count; int pool_count = 0;
114         for (int i = 0; i < log_count; i++) {
115                 history_tag_s *pool_info = NULL;
116                 if (history_data[i].event_tag != NULL && (history_data[i].event_code & ET_FLAG_START)) {
117                         pool_info = history_data[i].event_tag;
118                                 ret = bd_print_pool_entry(fd, i, pool_count, pool_info);
119                                 pool_count++;
120                                 if (ret != BATTERY_MONITOR_ERROR_NONE)
121                                         return BATTERY_MONITOR_ERROR_INTERNAL;
122                 }
123                 if (history_data[i].wakelock_tag != NULL) {
124                         pool_info = history_data[i].wakelock_tag;
125                                 ret = bd_print_pool_entry(fd, i, pool_count, pool_info);
126                                 pool_count++;
127                                 if (ret != BATTERY_MONITOR_ERROR_NONE)
128                                         return BATTERY_MONITOR_ERROR_INTERNAL;
129                 }
130                 if (history_data[i].wakereason_tag != NULL) {
131                         pool_info = history_data[i].wakereason_tag;
132                                 ret = bd_print_pool_entry(fd, i, pool_count, pool_info);
133                                 pool_count++;
134                                 if (ret != BATTERY_MONITOR_ERROR_NONE)
135                                         return BATTERY_MONITOR_ERROR_INTERNAL;
136                 }
137         }
138
139         EXIT;
140         return BATTERY_MONITOR_ERROR_NONE;
141 }
142
143 static int bd_get_and_print_system_base_battery_usage_from_dump(int fd, long int duration)
144 {
145         ENTER;
146
147         int ret = BATTERY_MONITOR_ERROR_NONE;
148         float usage_mah = 0;
149         time_t crr_time = 0; time(&crr_time);
150         int uid_n = 1000; //For Tizen
151         // Base Resource usage;
152         const char *rid_str = NULL;
153         for (int i = 0; i < 6; i++) {
154                 rid_str = bm_get_resource_id_string(i);
155                 if (g_strcmp0(rid_str, "UNKNOWN RESOURCE-ID") == 0) {
156                         _ERR("invalid resource-id");
157                         return BATTERY_MONITOR_ERROR_INTERNAL;
158                 }
159                 resourceid_usage_s *res_use =
160                         bm_server_query_resource_base_usage_resourceid_ci(rid_str, duration, crr_time, &ret);
161                 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
162                         _ERR("For resourceid %s", rid_str);
163                         continue;
164                 }
165                 usage_mah += res_use->usage;
166                 bm_resourceid_usage_free(res_use);
167         }
168         usage_mah /= 3600;
169         GString *dump_p = NULL;
170         dump_p = g_string_sized_new(30);
171         BM_CHECK_MEM_ALLOC(dump_p, {});
172         g_string_append(dump_p, TIZEN_DUMP_VERSION);
173         g_string_append_printf(dump_p, ",%d,l,pwi,uid,", uid_n);
174         g_string_append_printf(dump_p, "%0.2f", usage_mah);
175         g_string_append(dump_p, "\n");
176         _DBG("%s", dump_p->str);
177         if (write(fd, dump_p->str, dump_p->len) < 0)
178                 _WARN("write error");
179         g_string_free(dump_p, TRUE);
180
181         EXIT;
182         return ret;
183 }
184
185 static int bd_get_and_print_battery_usage_from_dump(int fd, long int duration)
186 {
187         ENTER;
188         int ret = BATTERY_MONITOR_ERROR_NONE;
189         // Application usage;
190         GHashTableIter iter;
191         gpointer key, value; GString *dump_p = NULL;
192         char *appid = NULL; int *uid_n = NULL;
193         time_t crr_time = 0; time(&crr_time);
194         long int end_t = crr_time;
195         float usageMAh = 0; int battery_capacity = 500;//default
196
197         ret = bm_get_battery_power_params(&battery_capacity);
198         if (ret != BATTERY_MONITOR_ERROR_NONE) {
199                 _ERR("unable read read battery capacity");
200                 battery_capacity = 500;
201         }
202
203         dump_p = g_string_sized_new(30);
204         BM_CHECK_MEM_ALLOC(dump_p, {});
205
206         g_string_append(dump_p, TIZEN_DUMP_VERSION);
207         // Power Capacity
208         g_string_append_printf(dump_p, ",0,l,pws,%d,0,0,0", battery_capacity);
209         g_string_append(dump_p, "\n");
210
211         _DBG("%s", dump_p->str);
212
213         if (write(fd, dump_p->str, dump_p->len) < 0)
214                 _WARN("write error");
215
216         g_string_free(dump_p, TRUE);
217
218         g_hash_table_iter_init(&iter, app_map);
219         while (g_hash_table_iter_next(&iter, &key, &value)) {
220                 uid_n = (int *)key; appid = (char *)value;
221                 _DBG("%d %s", *uid_n, appid); usageMAh = 0;
222                 // Currently giving app usage of last one week;
223                 // Modify it wrt to time
224                 appid_usage_s *app_use = bm_server_query_app_usage_by_appid_ci(appid, duration, end_t, &ret);
225                 if (app_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
226                         _ERR("For appid %s", appid);
227                         continue;
228                 }
229                 usageMAh = (app_use->rId_ble + app_use->rId_wifi + app_use->rId_cpu +
230                                 app_use->rId_display + app_use->rId_device_network +
231                                 app_use->rId_gps + app_use->rId_hrm);
232                 // usage stored as mAs changed to mAh
233                 usageMAh = (usageMAh/3600);
234                 dump_p = g_string_sized_new(30);
235                 BM_CHECK_MEM_ALLOC(dump_p, { BM_FREE(app_use); });
236                 g_string_append(dump_p, TIZEN_DUMP_VERSION);
237                 g_string_append_printf(dump_p, ",%d,l,pwi,uid,", *uid_n);
238                 g_string_append_printf(dump_p, "%0.2f", usageMAh);
239                 g_string_append(dump_p, "\n");
240                 bm_appid_usage_free(app_use);
241                 _DBG("%s", dump_p->str);
242                 if (write(fd, dump_p->str, dump_p->len) < 0)
243                         _WARN("write error");
244                 g_string_free(dump_p, TRUE);
245         }
246         // Resource usage;
247         const char *rid_str = NULL;
248         for (int i = 0; i < 6; i++) {
249                 int usage = 0;
250                 rid_str = bm_get_resource_id_string(i);
251                 if (g_strcmp0(rid_str, "UNKNOWN RESOURCE-ID") == 0) {
252                         _ERR("invalid resource-id");
253                         return BATTERY_MONITOR_ERROR_INTERNAL;
254                 }
255                 resourceid_usage_s *res_use =
256                         bm_server_query_resource_usage_resourceid_ci(rid_str, duration, end_t, &ret);
257                 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
258                         _ERR("For resourceid %s", rid_str);
259                         continue;
260                 }
261                 dump_p = g_string_sized_new(30);
262                 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(res_use); });
263                 usage = res_use->usage;
264                 // usage stored as mAs changed to mAh
265                 usageMAh = (float)usage/3600;
266                 g_string_append(dump_p, TIZEN_DUMP_VERSION);
267                 switch (i) {
268                 case 0://BT
269                         g_string_append(dump_p, ",0,l,pwi,blue,");
270                         break;
271                 case 1://WIFI
272                         g_string_append(dump_p, ",0,l,pwi,wifi,");
273                         break;
274                 case 2://CPU
275                         g_string_append(dump_p, ",0,l,pwi,phone,");
276                         break;
277                 case 3://DISP
278                         g_string_append(dump_p, ",0,l,pwi,scrn,");
279                         break;
280                 case 4://DEVICE NT
281                         g_string_append(dump_p, ",0,l,pwi,cell,");
282                         break;
283                 default://GPS HRM
284                         g_string_append(dump_p, ",0,l,pwi,unacc,");
285                         break;
286                 }
287                 g_string_append_printf(dump_p, "%0.2f", usageMAh);
288                 g_string_append(dump_p, "\n");
289                 bm_resourceid_usage_free(res_use);
290                 _DBG("%s", dump_p->str);
291                 if (write(fd, dump_p->str, dump_p->len) < 0)
292                         _WARN("write error");
293                 g_string_free(dump_p, TRUE);
294         }
295
296         ret = bd_get_and_print_system_base_battery_usage_from_dump(fd, duration);
297         if (ret != BATTERY_MONITOR_ERROR_NONE)
298                 _ERR("Base usage dump failed");
299
300         EXIT;
301         return ret;
302 }
303
304 static int bd_get_and_print_gbr_stat_entry_from_dump(int fd, long int duration)
305 {
306         ENTER;
307         // Dump brightness Data
308         bd_gl_sbr_stat_s *gbr_stat = (bd_gl_sbr_stat_s *)calloc(1, sizeof(bd_gl_sbr_stat_s));
309         BM_CHECK_MEM_ALLOC(gbr_stat, {});
310
311         int ret = BATTERY_MONITOR_ERROR_NONE;
312         ret = bm_server_battery_dump_query_from_db(gbr_stat, 3, duration);
313         if (ret != BATTERY_MONITOR_ERROR_NONE) {
314                 _ERR("Unable to fetch data from DB");
315                 BM_FREE(gbr_stat);
316                 return ret;
317         }
318
319         GString *dump_p = g_string_sized_new(30);
320         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gbr_stat); });
321         g_string_append(dump_p, TIZEN_DUMP_VERSION);
322         g_string_append(dump_p, ",0,l,");
323         g_string_append(dump_p, "br,");
324         g_string_append_printf(dump_p, "%ld,", gbr_stat->dark); //OFF
325         g_string_append_printf(dump_p, "%ld,", gbr_stat->dim); // All are time values
326         g_string_append_printf(dump_p, "%ld,", gbr_stat->medium);
327         g_string_append_printf(dump_p, "%ld,", gbr_stat->light); //DEF: 0
328         g_string_append_printf(dump_p, "%ld", gbr_stat->bright);
329         g_string_append(dump_p, "\n");
330         _DBG("%s", dump_p->str);//write
331         if (write(fd, dump_p->str, dump_p->len) < 0)
332                 _WARN("write error");
333         g_string_free(dump_p, TRUE);
334         BM_FREE(gbr_stat);
335
336         EXIT;
337         return ret;
338 }
339
340 static int bd_get_and_print_misc_stat_entry_from_dump(int fd, long int duration)
341 {
342         ENTER;
343         //Dump screen on and off time
344         //Modify m - field accordingly.
345         bd_gl_sbr_stat_s *gbr_stat = (bd_gl_sbr_stat_s *)calloc(1, sizeof(bd_gl_sbr_stat_s));
346         BM_CHECK_MEM_ALLOC(gbr_stat, {});
347
348         int ret = BATTERY_MONITOR_ERROR_NONE;
349         ret = bm_server_battery_dump_query_from_db(gbr_stat, 3, duration);
350         if (ret != BATTERY_MONITOR_ERROR_NONE) {
351                 _ERR("Unable to fetch data from DB");
352                 BM_FREE(gbr_stat);
353                 return ret;
354         }
355
356         GString *dump_p = g_string_sized_new(30);
357         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gbr_stat); });
358         g_string_append(dump_p, TIZEN_DUMP_VERSION);
359         g_string_append(dump_p, ",0,l,");
360         g_string_append(dump_p, "m,");
361         long int off_time = gbr_stat->dark;
362         long int on_time = (gbr_stat->dim + gbr_stat->medium + gbr_stat->bright);
363         g_string_append_printf(dump_p, "%ld,", on_time);
364         g_string_append_printf(dump_p, "%ld", off_time);
365         g_string_append(dump_p, "\n");
366 /*
367         _DBG("%s", dump_p->str);
368         if (write(fd, dump_p->str, dump_p->len) < 0)
369                 _WARN("write error");*/
370         g_string_free(dump_p, TRUE);
371         BM_FREE(gbr_stat);
372
373         EXIT;
374         return ret;
375 }
376
377 static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration)
378 {
379         ENTER;
380
381         // Dump Network Data
382         bd_gl_ntw_stat_s *gn_stat = (bd_gl_ntw_stat_s *)calloc(1, sizeof(bd_gl_ntw_stat_s));
383         BM_CHECK_MEM_ALLOC(gn_stat, {});
384         int ret = BATTERY_MONITOR_ERROR_NONE;
385         ret = bm_server_battery_dump_query_from_db(gn_stat, 2, duration);
386         if (ret != BATTERY_MONITOR_ERROR_NONE) {
387                 _ERR("Unable to fetch data from DB");
388                 BM_FREE(gn_stat);
389                 return ret;
390         }
391
392         GString *dump_p = g_string_sized_new(30);
393         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gn_stat); });
394
395         g_string_append(dump_p, TIZEN_DUMP_VERSION);
396         g_string_append(dump_p, ",0,l,");
397         g_string_append(dump_p, "gn,");
398         g_string_append_printf(dump_p, "%ld,", gn_stat->dn_rx_byt);
399         g_string_append_printf(dump_p, "%ld,", gn_stat->dn_tx_byt);
400         g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_rx_byt);
401         g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_tx_byt);
402         g_string_append_printf(dump_p, "%ld,", gn_stat->dn_rx_pkt);
403         g_string_append_printf(dump_p, "%ld,", gn_stat->dn_tx_pkt);
404         g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_rx_pkt);
405         g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_tx_pkt);
406         g_string_append_printf(dump_p, "%d,", 0); //add this to struct as well
407         g_string_append_printf(dump_p, "%d", 0);
408         g_string_append(dump_p, "\n");
409
410         _DBG("%s", dump_p->str);//write
411         if (write(fd, dump_p->str, dump_p->len) < 0)
412                 _WARN("write error");
413
414         g_string_free(dump_p, TRUE);
415         BM_FREE(gn_stat);
416
417         EXIT;
418         return ret;
419 }
420
421 static int bd_get_and_print_gwf_stat_entry_from_dump(int fd, long int duration)
422 {
423         ENTER;
424         // Dump Wifi Stats
425         bd_gl_wifi_stat_s *gwfl_stat = (bd_gl_wifi_stat_s *)calloc(1, sizeof(bd_gl_wifi_stat_s));
426         BM_CHECK_MEM_ALLOC(gwfl_stat, {});
427
428         int ret = BATTERY_MONITOR_ERROR_NONE;
429         ret = bm_server_battery_dump_query_from_db(gwfl_stat, 1, duration);
430         if (ret != BATTERY_MONITOR_ERROR_NONE) {
431                 _ERR("Unable to fetch data from DB");
432                 BM_FREE(gwfl_stat);
433                 return ret;
434         }
435
436         GString *dump_p = g_string_sized_new(30);
437         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
438         g_string_append(dump_p, TIZEN_DUMP_VERSION);
439         g_string_append(dump_p, ",0,l,");
440         g_string_append(dump_p, "gwfl,");
441         g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_on_time);
442         g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_conn_time);
443         g_string_append(dump_p, "0,0,0");//legacy
444
445         g_string_append(dump_p, "\n");
446         _DBG("%s", dump_p->str);//write
447         if (write(fd, dump_p->str, dump_p->len) < 0)
448                 _WARN("write error");
449         g_string_free(dump_p, TRUE);
450
451         dump_p = g_string_sized_new(30);
452         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
453         g_string_append(dump_p, TIZEN_DUMP_VERSION);
454         g_string_append(dump_p, ",0,l,");
455         g_string_append(dump_p, "gwfcd,");
456         //idle rx pwi tx
457         float usage_mah = (float)gwfl_stat->wifi_pwi_val/3600;
458         g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_idle_time);
459         g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_rx_time);
460         g_string_append_printf(dump_p, "%0.2f,", usage_mah);
461         g_string_append_printf(dump_p, "%ld", gwfl_stat->wifi_tx_time);
462         g_string_append(dump_p, "\n");
463         _DBG("%s", dump_p->str);//write
464         if (write(fd, dump_p->str, dump_p->len) < 0)
465                 _WARN("write error");
466         g_string_free(dump_p, TRUE);
467         BM_FREE(gwfl_stat);
468
469         EXIT;
470         return ret;
471 }
472
473 static int bd_get_and_print_gble_stat_entry_from_dump(int fd, long int duration)
474 {
475         ENTER;
476         // Dump bt Data
477         bd_gl_ble_stat_s *gble_stat = (bd_gl_ble_stat_s *)calloc(1, sizeof(bd_gl_ble_stat_s));
478         BM_CHECK_MEM_ALLOC(gble_stat, {});
479
480         int ret = BATTERY_MONITOR_ERROR_NONE;
481         ret = bm_server_battery_dump_query_from_db(gble_stat, 0, duration);
482         if (ret != BATTERY_MONITOR_ERROR_NONE) {
483                 _ERR("Unable to fetch data from DB");
484                 BM_FREE(gble_stat);
485                 return ret;
486         }
487
488         GString *dump_p = g_string_sized_new(30);
489         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gble_stat); });
490         g_string_append(dump_p, TIZEN_DUMP_VERSION);
491         g_string_append(dump_p, ",0,l,");
492         g_string_append(dump_p, "gble,");
493         float usage_mah = (float)gble_stat->ble_pwi_val/3600;
494         g_string_append_printf(dump_p, "%ld,", gble_stat->ble_idle_time);
495         g_string_append_printf(dump_p, "%ld,", gble_stat->ble_rx_time);
496         g_string_append_printf(dump_p, "%ld,", gble_stat->ble_tx_time);
497         g_string_append_printf(dump_p, "%0.2f", usage_mah);
498         g_string_append(dump_p, "\n");
499
500         _DBG("%s", dump_p->str);//write
501         if (write(fd, dump_p->str, dump_p->len) < 0)
502                 _WARN("write error");
503         g_string_free(dump_p, TRUE);
504         BM_FREE(gble_stat);
505
506         EXIT;
507         return ret;
508 }
509
510 static int bd_get_and_print_gst_stat_entry_from_dump(int fd, long int duration)
511 {
512         ENTER;
513
514         int ret = BATTERY_MONITOR_ERROR_NONE;
515
516         bd_gl_sgt_stat_s *gst_stat = (bd_gl_sgt_stat_s *)calloc(1, sizeof(bd_gl_sgt_stat_s));
517         BM_CHECK_MEM_ALLOC(gst_stat, {});
518
519         ret = bm_server_battery_dump_query_from_db(gst_stat, 4, duration);
520         if (ret != BATTERY_MONITOR_ERROR_NONE) {
521                 _ERR("Unable to fetch data from DB");
522                 BM_FREE(gst_stat);
523                 return ret;
524         }
525
526         GString *dump_p = g_string_sized_new(30);
527         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
528
529         g_string_append(dump_p, TIZEN_DUMP_VERSION);
530         g_string_append(dump_p, ",0,l,");
531         g_string_append(dump_p, "sgt,");
532         g_string_append_printf(dump_p, "%ld,", gst_stat->none_time);
533         g_string_append_printf(dump_p, "%ld,", gst_stat->poor_time);
534         g_string_append_printf(dump_p, "%ld,", gst_stat->med_time);
535         g_string_append_printf(dump_p, "%ld,", gst_stat->good_time);
536         g_string_append_printf(dump_p, "%ld", gst_stat->grt_time);
537         g_string_append(dump_p, "\n");
538
539         _DBG("%s", dump_p->str);//write
540
541         if (write(fd, dump_p->str, dump_p->len) < 0)
542                 _WARN("write error");
543         g_string_free(dump_p, TRUE);
544
545         dump_p = g_string_sized_new(30);
546         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
547
548         g_string_append(dump_p, TIZEN_DUMP_VERSION);
549         g_string_append(dump_p, ",0,l,");
550         g_string_append(dump_p, "sst,");
551         g_string_append_printf(dump_p, "%ld", gst_stat->scan_time);
552
553         g_string_append(dump_p, "\n");
554         _DBG("%s", dump_p->str);//write
555         if (write(fd, dump_p->str, dump_p->len) < 0)
556                 _WARN("write error");
557
558         g_string_free(dump_p, TRUE);
559         BM_FREE(gst_stat);
560
561         EXIT;
562         return ret;
563 }
564
565 static int bd_get_and_print_gwst_stat_entry_from_dump(int fd, long int duration)
566 {
567         ENTER;
568         bd_gl_wst_stat_s *gwst_stat = (bd_gl_wst_stat_s *)calloc(1, sizeof(bd_gl_wst_stat_s));
569         BM_CHECK_MEM_ALLOC(gwst_stat, {});
570
571         int ret = BATTERY_MONITOR_ERROR_NONE;
572         ret = bm_server_battery_dump_query_from_db(gwst_stat, 5, duration);
573         if (ret != BATTERY_MONITOR_ERROR_NONE) {
574                 _ERR("Unable to fetch data from DB");
575                 BM_FREE(gwst_stat);
576                 return ret;
577         }
578
579         GString *dump_p = g_string_sized_new(30);
580         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwst_stat); });
581         g_string_append(dump_p, TIZEN_DUMP_VERSION);
582         g_string_append(dump_p, ",0,l,");
583         g_string_append(dump_p, "wsgt,");
584 //      g_string_append_printf(dump_p, "%ld,", gwst_stat->wscan_time);
585         g_string_append_printf(dump_p, "%ld,", gwst_stat->wnone_time);
586         g_string_append_printf(dump_p, "%ld,", gwst_stat->wpoor_time);
587         g_string_append_printf(dump_p, "%ld,", gwst_stat->wmed_time);
588         g_string_append_printf(dump_p, "%ld,", gwst_stat->wgood_time);
589         g_string_append_printf(dump_p, "%ld", gwst_stat->wgrt_time);
590         g_string_append(dump_p, "\n");
591         _DBG("%s", dump_p->str);//write
592         if (write(fd, dump_p->str, dump_p->len) < 0)
593                 _WARN("write error");
594         g_string_free(dump_p, TRUE);
595         BM_FREE(gwst_stat);
596
597         EXIT;
598         return ret;
599 }
600
601 static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration)
602 {
603         ENTER;
604
605         int ret = BATTERY_MONITOR_ERROR_NONE;
606         const char *rid_str = NULL; int usage = 0;
607         time_t crr_time = 0; time(&crr_time);
608         long int end_t = crr_time;
609         rid_str = bm_get_resource_id_string(2);
610         resourceid_usage_s *res_use =
611                 bm_server_query_resource_usage_resourceid_ci(rid_str, duration, end_t, &ret);
612         if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
613                 _ERR("For resourceid %s", rid_str);
614                 return ret;
615         }
616         usage = res_use->usage;
617         float usagemah = (float)usage/3600;
618         bd_gl_cpu_stat_s *gcpu_stat = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
619         BM_CHECK_MEM_ALLOC(gcpu_stat, {BM_FREE(res_use); });
620         ret = bm_server_battery_dump_query_from_db(gcpu_stat, 7, duration);
621         if (ret != BATTERY_MONITOR_ERROR_NONE) {
622                 _ERR("Unable to fetch data from DB");
623                 BM_FREE(gcpu_stat);
624                 BM_FREE(res_use);
625                 return ret;
626         }
627
628         GString *dump_p = g_string_sized_new(30);
629         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gcpu_stat); BM_FREE(res_use); });
630         g_string_append(dump_p, TIZEN_DUMP_VERSION);
631         g_string_append(dump_p, ",0,l,");
632         g_string_append(dump_p, "cpu,");
633         g_string_append_printf(dump_p, "%ld,", gcpu_stat->usr_time);
634         g_string_append_printf(dump_p, "%ld,", gcpu_stat->sys_time);
635         g_string_append_printf(dump_p, "%0.2f", usagemah);
636         g_string_append(dump_p, "\n");
637         bm_resourceid_usage_free(res_use);
638         _DBG("%s", dump_p->str);
639         if (write(fd, dump_p->str, dump_p->len) < 0)
640                 _WARN("write error");
641
642         g_string_free(dump_p, TRUE);
643         BM_FREE(gcpu_stat);
644
645         EXIT;
646         return ret;
647 }
648
649 static int bd_get_and_print_uid_info_entry_from_dump(int fd)
650 {
651         ENTER;
652         // Dump UID entry
653         int ret = BATTERY_MONITOR_ERROR_NONE;
654         GSList *list = bm_server_query_appid_map(&ret);
655         if (list == NULL) {
656                 _ERR("NO LIST DATA FOUND");
657                 return ret;
658         }
659         app_map = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
660         GSList *iter = NULL; GString *dump_p = NULL;
661         dump_p = g_string_sized_new(30);
662         BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
663         g_string_append(dump_p, TIZEN_DUMP_VERSION);
664         g_string_append(dump_p, ",0,i,uid,1000,Tizen\n");
665         if (write(fd, dump_p->str, dump_p->len) < 0)
666                 _WARN("write error");
667         g_string_free(dump_p, TRUE);
668
669         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
670                 dump_p = g_string_sized_new(30);
671                 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
672                 g_string_append(dump_p, TIZEN_DUMP_VERSION);
673                 g_string_append(dump_p, ",0");
674                 g_string_append(dump_p, ",i");
675                 g_string_append(dump_p, ",uid,");
676                 appid_map_s *amap_data = (appid_map_s *)iter->data;
677                 g_string_append_printf(dump_p, "%d,", (amap_data->id+10000));
678                 gchar *temp_str = g_strdup(amap_data->AppId);
679                 g_string_append(dump_p, amap_data->AppId);
680                 g_string_append(dump_p, "\n");
681                 _DBG("%s", dump_p->str);//write
682                 if (write(fd, dump_p->str, dump_p->len) < 0)
683                         _WARN("write error");
684                 g_string_free(dump_p, TRUE);
685                 int *temp_id = (int *)calloc(1, sizeof(int));
686                 if (temp_id != NULL) {
687                         *temp_id = (amap_data->id + 10000);
688                         _DBG("Insert id %d, str %s, res %d", *temp_id, temp_str,
689                         g_hash_table_insert(app_map, temp_id, temp_str));
690                 } else {
691                         _WARN("Memory Allocation Failed");
692                         g_free(temp_str);
693                 }
694         }
695         bm_set_free_gslist_appid_map(list);
696
697         EXIT;
698         return ret;
699 }
700
701 static int bd_get_history_detail_from_dump(int index, history_item_s* dump_node)
702 {
703         ENTER;
704         BM_CHECK_INPUT_PARAM(dump_node);
705
706         if (h_count >= HISTORY_SIZE_MAX || index > h_count) {
707                 _ERR("Some Error occured with global index");
708                 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
709         }
710
711         _DBG("Value of index %d", index);
712         _DBG("Value of h_count %d", h_count);
713
714         int dvar1 = 0, dvar2 = 0;
715         dvar1 = history_data[index].dump_1;
716         dvar2 = history_data[index].dump_2;
717
718         _DBG("Value of encoded value 1 %d", dvar1);
719         _DBG("Value of encoded value 2 %d", dvar2);
720
721         dump_node->cmd_s = ((dvar1) & 0xff);
722         _DBG("cmd_s %d", dump_node->cmd_s);
723
724         dump_node->battery_level = ((dvar1>>8) & 0xff);
725         _DBG("battery_level %d", dump_node->battery_level);
726
727         dump_node->battery_status = ((dvar1>>16) & 0xf);
728         _DBG("battery_status %d", dump_node->battery_status);
729
730         dump_node->battery_health = ((dvar1>>20) & 0xf);
731         _DBG("battery_health %d", dump_node->battery_health);
732
733         dump_node->battery_plugtype = ((dvar1>>24) & 0xf);
734         _DBG("battery_plugtype %d", dump_node->battery_plugtype);
735
736         dump_node->battery_temperature = ((dvar2) & 0xffff);
737         _DBG("battery_temperature %d", dump_node->battery_temperature);
738
739         dump_node->battery_voltage = ((dvar2>>16) & 0xffff);
740         _DBG("battery_voltage %d", dump_node->battery_voltage);
741
742         dump_node->battery_charge = history_data[index].battery_chargeUAh;
743         _DBG("battery_charge %d", dump_node->battery_charge);
744
745         dump_node->event_code = history_data[index].event_code;
746         _DBG("eventcode %d", dump_node->event_code);
747
748         dump_node->state_1 = history_data[index].state_1;
749         _DBG("state_1 %d", dump_node->state_1);
750
751         dump_node->state_2 = history_data[index].state_2;
752         _DBG("state_2 %d", dump_node->state_2);
753
754         dump_node->event_tag = history_data[index].event_tag;
755         if (dump_node->event_tag == NULL)
756                 _DBG("Event_Tag_NULL");
757         dump_node->wakelock_tag = history_data[index].wakelock_tag;
758         if (dump_node->wakelock_tag == NULL)
759                 _DBG("Wakelock_Tag_NULL");
760         dump_node->wakereason_tag = history_data[index].wakereason_tag;
761         //:Not supported
762         if (dump_node->wakereason_tag == NULL)
763                 _DBG("Wakereason_Tag_NULL");
764
765         dump_node->time_s = history_data[index].time_s;
766         _DBG("TIME_S is %lld", dump_node->time_s);
767
768         dump_node->time_current = history_data[index].time_current;
769         _DBG("C TIME is %lld", dump_node->time_current);
770
771         dump_node->usage_type = history_data[index].usage_type;
772         _DBG("Usage type is %d", dump_node->usage_type);
773
774         dump_node->usage = history_data[index].usage;
775         _DBG("Usage is %d", dump_node->usage);
776
777         EXIT;
778         return BATTERY_MONITOR_ERROR_NONE;
779 }
780
781 static void bd_get_last_charge_time(long int def_time, int *ret)
782 {
783         ENTER;
784
785         if (lst_charge < 0) {
786                 char *str = "battery";
787                 resourceid_usage_s *bat_use = bm_server_query_battery_last_charge(str, ret);
788                 if (*ret == BATTERY_MONITOR_ERROR_NONE) {
789                         if (bat_use->log_time > 0)
790                                 lst_charge = bat_use->log_time;
791                         else
792                                 lst_charge = def_time;
793                         _DBG("LC [%ld]", lst_charge);
794                         BM_FREE(bat_use);
795                 } else {
796                         _ERR("LAST CHG time failed");
797                         lst_charge = def_time;
798                 }
799         }
800
801         EXIT;
802         return;
803 }
804
805 int bd_print_history_item_reset(history_item_s* old_state)
806 {
807         ENTER;
808         BM_CHECK_INPUT_PARAM(old_state);
809
810         old_state->time_s = -1;
811         old_state->state_1 = 0;
812         old_state->state_2 = 0;
813         old_state->usage_type = 0;
814         old_state->usage = 0;
815         old_state->battery_level = -1;
816         old_state->battery_status = -1;
817         old_state->battery_health = -1;
818         old_state->battery_plugtype = -1;
819         old_state->battery_temperature = -1;
820         old_state->battery_voltage = -1;
821         old_state->battery_charge = -1;
822         old_state->event_code = 0;
823         old_state->event_tag = NULL;
824         old_state->wakelock_tag = NULL;
825         old_state->wakereason_tag = NULL;
826
827         EXIT;
828         return BATTERY_MONITOR_ERROR_NONE;
829 }
830
831 int bd_get_base_time_from_dump(long long *base_time)
832 {
833         ENTER;
834
835         *base_time = history_data[0].time_s;
836
837         EXIT;
838         return BATTERY_MONITOR_ERROR_NONE;
839 }
840
841 int bd_print_history_item_main(int fd, int num_h_items, long long base_time, bool chkin)
842 {
843         ENTER;
844
845         if (fd < 0) {
846                 _ERR("Invalid fd");
847                 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
848         }
849
850         if (h_count >= HISTORY_SIZE_MAX) {
851                 _ERR("Some Error occured with global index");
852                 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
853         }
854
855         int ret = BATTERY_MONITOR_ERROR_NONE;
856         h_flag = false; //Lock
857
858         history_item_s new_state;
859         history_item_s old_state;
860
861         ret = bd_print_history_item_reset(&old_state);
862         if (ret != BATTERY_MONITOR_ERROR_NONE) {
863                 _ERR("Old State reset failed");
864                 return ret;
865         }
866
867         ret = bd_get_pool_entry_from_dump(fd);
868         if (ret != BATTERY_MONITOR_ERROR_NONE)
869                 _ERR("S pool data dump failed");
870
871         ret = bd_get_base_time_from_dump(&base_time);
872         if (ret != BATTERY_MONITOR_ERROR_NONE)
873                 _ERR("Base time error");
874
875         int log_count = h_count; GString *dump_p = NULL;
876         for (int i = 0; i < log_count; i++) {
877                 ret = bd_get_history_detail_from_dump(i, &new_state);
878                 if (ret != BATTERY_MONITOR_ERROR_NONE) {
879                         _ERR("Decode Failed");
880                         return ret;
881                 }
882                 dump_p = g_string_sized_new(30);
883                 BM_CHECK_MEM_ALLOC(dump_p, {});
884                 if (chkin) {
885                         g_string_append(dump_p, TIZEN_DUMP_VERSION);
886                         g_string_append_c(dump_p, ',');
887                         _DBG("value %s", dump_p->str);
888                         g_string_append(dump_p, DUMP_DATA_TAG);
889                         g_string_append_c(dump_p, ',');
890                         _DBG("value %s", dump_p->str);
891                         if (old_state.time_s < 0) {
892                                 g_string_append_printf(dump_p, "%lld",
893                                                 (new_state.time_s - base_time));
894                                 _DBG("value %s", dump_p->str);
895                         } else {
896                                 g_string_append_printf(dump_p, "%lld",
897                                                 (new_state.time_s - old_state.time_s));
898                                 _DBG("value %s", dump_p->str);
899                         }
900                         old_state.time_s = new_state.time_s;
901                         _DBG("Value old_state.time_s %lld", old_state.time_s);
902                 } else { //: Let's skip the NC mode in initial version
903                 }
904                 if (new_state.cmd_s == CM_ST) {
905                         if (chkin) {
906                                 g_string_append_c(dump_p, ':');
907                                 _DBG("value %s", dump_p->str);
908                         } else {
909                                 //NC
910                         }
911                         g_string_append(dump_p, "START\n");
912                         _DBG("value %s", dump_p->str);
913                         bd_print_history_item_reset(&old_state);
914                 } else if (new_state.cmd_s == CM_CRR_TIME || new_state.cmd_s == CM_RST) {
915                         if (chkin) {
916                                 g_string_append_c(dump_p, ':');
917                                 _DBG("value %s", dump_p->str);
918                         }
919                         if (new_state.cmd_s == CM_RST) {
920                                 g_string_append(dump_p, "RESET:");
921                                 _DBG("value %s", dump_p->str);
922                                 bd_print_history_item_reset(&old_state);
923                         }
924                         g_string_append(dump_p, "TIME:");
925                         _DBG("value %s", dump_p->str);
926                         if (chkin) {
927                                 g_string_append_printf(dump_p, "%lld", new_state.time_current);
928                                 _DBG("value %s", dump_p->str);
929                                 g_string_append(dump_p, "\n");
930                                 _DBG("value %s", dump_p->str);
931                         } else {
932                                 //NC
933                         }
934                 } else if (new_state.cmd_s == CM_USAGE) {
935                         for (int i = 0; i < 32; i++) {
936                                 if (new_state.usage_type == (1 << i)) {
937                                         g_string_append(dump_p, ",");
938                                         g_string_append_printf(dump_p, "%s", usage_map[i]);
939                                         g_string_append(dump_p, "=");
940                                         g_string_append_printf(dump_p, "%d", new_state.usage);
941                                         g_string_append(dump_p, ",");
942                                         g_string_append_printf(dump_p, "%lld", new_state.time_current);
943                                         g_string_append(dump_p, ",");
944                                         g_string_append_printf(dump_p, "%lld", (new_state.time_s - min_idle_check_time) / 1000);
945                                         g_string_append(dump_p, "\n");
946                                         _DBG("value %d", new_state.usage);
947                                 }
948                         }
949                 } else {
950                         if (!chkin) {
951                                 //NC
952                         } else {
953                                 if (old_state.battery_level != new_state.battery_level) {
954                                         old_state.battery_level = new_state.battery_level;
955                                         g_string_append(dump_p, ",Bl=");
956                                         _DBG("value %s", dump_p->str);
957                                         g_string_append_printf(dump_p, "%d",
958                                                         new_state.battery_level);
959                                         _DBG("value %s", dump_p->str);
960                                 }
961                         }
962                         if (old_state.battery_status != new_state.battery_status) {
963                                 old_state.battery_status = new_state.battery_status;
964                                 g_string_append(dump_p, ",Bs="); //:for NC
965                                 _DBG("value %s", dump_p->str);
966                                 switch (old_state.battery_status) {
967                                 case BD_BSTA_UNKN:
968                                         g_string_append_c(dump_p, '?');
969                                         break;
970                                 case BD_BSTA_CHAR:
971                                         g_string_append_c(dump_p, 'c');
972                                         break;
973                                 case BD_BSTA_DISC:
974                                         g_string_append_c(dump_p, 'd');
975                                         break;
976                                 case BD_BSTA_NCHA:
977                                         g_string_append_c(dump_p, 'n');
978                                         break;
979                                 case BD_BSTA_FULL:
980                                         g_string_append_c(dump_p, 'f');
981                                         break;
982                                 default:
983                                         g_string_append_c(dump_p, '?');
984                                         _DBG("value %s", dump_p->str);
985                                         break;
986                                 }
987                         }
988                         if (old_state.battery_health != new_state.battery_health) {
989                                 old_state.battery_health = new_state.battery_health;
990                                 g_string_append(dump_p, ",Bh="); //:for NC
991                                 _DBG("value %s", dump_p->str);
992                                 switch (old_state.battery_health) {
993                                 case BD_BHLT_GOOD:
994                                         g_string_append_c(dump_p, 'g');
995                                         break;
996                                 case BD_BHLT_UNKN:
997                                         g_string_append_c(dump_p, '?');
998                                         break;
999                                 case BD_BHLT_OVRH:
1000                                         g_string_append_c(dump_p, 'h');
1001                                         break;
1002                                 case BD_BHLT_DEAD:
1003                                         g_string_append_c(dump_p, 'd');
1004                                         break;
1005                                 case BD_BHLT_OVRV:
1006                                         g_string_append_c(dump_p, 'v');
1007                                         break;
1008                                 case BD_BHLT_UNSP:
1009                                         g_string_append_c(dump_p, 'f');
1010                                         break;
1011                                 case BD_BHLT_CLD:
1012                                         g_string_append_c(dump_p, 'c');
1013                                         break;
1014                                 default:
1015                                         g_string_append_c(dump_p, '?');
1016                                         _DBG("value %s", dump_p->str);
1017                                         break;
1018                                 }
1019                         }
1020                         if (old_state.battery_plugtype != new_state.battery_plugtype) {
1021                                 old_state.battery_plugtype = new_state.battery_plugtype;
1022                                 g_string_append(dump_p, ",Bp="); //:for NC
1023                                 _DBG("value %s", dump_p->str);
1024                                 switch (old_state.battery_plugtype) {
1025                                 case BD_BPLT_DIS:
1026                                         g_string_append_c(dump_p, 'n');
1027                                         break;
1028                                 case BD_BPLT_AC:
1029                                         g_string_append_c(dump_p, 'a');
1030                                         break;
1031                                 case BD_BPLT_USB:
1032                                         g_string_append_c(dump_p, 'u');
1033                                         break;
1034                                 case BD_BPLT_WL:
1035                                         g_string_append_c(dump_p, 'w');
1036                                         break;
1037                                 default:
1038                                         g_string_append_c(dump_p, 'n');
1039                                         _DBG("value %s", dump_p->str);
1040                                         break;
1041                                 }
1042                         }
1043                         if (old_state.battery_temperature != new_state.battery_temperature) {
1044                                 old_state.battery_temperature = new_state.battery_temperature;
1045                                 g_string_append(dump_p, ",Bt="); //:for NC
1046                                 g_string_append_printf(dump_p, "%d",
1047                                                 new_state.battery_temperature);
1048                                 _DBG("value %s", dump_p->str);
1049                         }
1050                         if (old_state.battery_voltage != new_state.battery_voltage) {
1051                                 old_state.battery_voltage = new_state.battery_voltage;
1052                                 g_string_append(dump_p, ",Bv="); //:for NC
1053                                 _DBG("value %s", dump_p->str);
1054                                 g_string_append_printf(dump_p, "%d", new_state.battery_voltage);
1055                                 _DBG("value %s", dump_p->str);
1056                         }
1057                         if (old_state.battery_charge != new_state.battery_charge) {
1058                                 old_state.battery_charge = new_state.battery_charge;
1059                                 g_string_append(dump_p, ",Bcc=");
1060                                 _DBG("value %s", dump_p->str);
1061                                 //Charge in MAh
1062                                 g_string_append_printf(dump_p, "%d", (new_state.battery_charge/1000));
1063                                 _DBG("value %s", dump_p->str);
1064                         }
1065                         int change = (old_state.state_1 ^ new_state.state_1);
1066                         if (change != 0) {
1067                                 bool wake_fl = false; int k = 0;
1068                                 if ((change & ST1_BRIGHTNESS_MASK) != 0) {
1069                                         k = (new_state.state_1 & ST1_BRIGHTNESS_MASK);
1070                                         g_string_append(dump_p, ",Sb=");
1071                                         k = k >> ST1_BRIGHTNESS_SHIFT;
1072                                         _DBG("Sb = %d", k);
1073                                         g_string_append_printf(dump_p, "%d", k);
1074                                         k = 0;
1075                                 }
1076                                 if ((change & ST1_PHONE_SIGNAL_STRENGTH_MASK) != 0) {
1077                                         k = (new_state.state_1 & ST1_PHONE_SIGNAL_STRENGTH_MASK);
1078                                         g_string_append(dump_p, ",Pss=");
1079                                         k = k >> ST1_PHONE_SIGNAL_STRENGTH_SHIFT;
1080                                         _DBG("Pss = %d", k);
1081                                         g_string_append_printf(dump_p, "%d", k);
1082                                         k = 0;
1083                                 }
1084                                 int shifts = (1 << 14);
1085                                 for (int i = 14; i < 32; i++) {
1086                                         if ((change & shifts) != 0) {
1087                                                 g_string_append_c(dump_p, ',');
1088                                                 //Considering only -ve shift cases
1089                                                 int shift = -1;
1090                                                 if (shift < 0) {
1091                                                         if ((new_state.state_1 & (1 << i)) != 0)
1092                                                                 g_string_append_c(dump_p, '+');
1093                                                         else
1094                                                                 g_string_append_c(dump_p, '-');
1095                                                         gchar *temp = g_strdup(state1_map[i]);
1096                                                         g_string_append(dump_p, temp);
1097                                                         g_free(temp);
1098                                                         //_DBG("value %s", dump_p->str);
1099                                                         if ((1 << i) == ST1_WAKE_LOCK_FLAG &&
1100                                                                 new_state.wakelock_tag != NULL) {
1101                                                                 wake_fl = true;
1102                                                                 g_string_append_c(dump_p, '=');
1103                                                                 //_DBG("value %s", dump_p->str);
1104                                                                 if (chkin)
1105                                                                         g_string_append_printf(dump_p,
1106                                                                 "%d", new_state.wakelock_tag->sp_idx);
1107                                                                 else {
1108                                                                         //: NC
1109                                                                 }
1110                                                                 //_DBG("value %s", dump_p->str);
1111                                                         }
1112                                                 } else {
1113                                                 //With non-negative shift
1114                                                 }
1115                                         }
1116                                         shifts = (shifts << 1);
1117                                 }
1118                                 if (!wake_fl && new_state.wakelock_tag != NULL) {
1119                                         g_string_append(dump_p, ",w=");
1120                                         if (chkin) {
1121                                                 g_string_append_printf(dump_p, "%d",
1122                                                 new_state.wakelock_tag->sp_idx);
1123                                                 _DBG("value %s", dump_p->str);
1124                                         } else {
1125                                         }
1126                                 }
1127                         }
1128                         change = (old_state.state_2 ^ new_state.state_2);
1129                         if (change != 0) {
1130                                 int shifts = 1;
1131                                 for (int i = 0; i < 32; i++) {
1132                                         if ((change & shifts) != 0) {
1133                                                 g_string_append_c(dump_p, ',');
1134                                                 //_DBG("value %s", dump_p->str);
1135                                                 int shift = -1;
1136                                                 if (shift < 0) {
1137                                                         if ((new_state.state_2 & (1 << i) & change) != 0)
1138                                                                 g_string_append_c(dump_p, '+');
1139                                                         else
1140                                                                 g_string_append_c(dump_p, '-');
1141                                                         //_DBG("value %s", dump_p->str);
1142                                                         gchar *temp = g_strdup(state2_map[i]);
1143                                                         g_string_append(dump_p, temp);
1144                                                         g_free(temp);
1145                                                         _DBG("value %s", dump_p->str);
1146                                                 }
1147                                         }
1148                                         shifts = (shifts << 1);
1149                                 }
1150                         }
1151                         if (new_state.wakereason_tag != NULL) {
1152                                 if (chkin) {
1153                                         g_string_append(dump_p, ",wr=");
1154                                         _DBG("value %s", dump_p->str);
1155                                         g_string_append_printf(dump_p, "%d",
1156                                         new_state.wakereason_tag->sp_idx);
1157                                         _DBG("value %s", dump_p->str);
1158                                 }
1159                         } else {
1160                                 //: for NC
1161                         }
1162                         if (new_state.event_code != ET_NONE) {
1163                                 g_string_append_c(dump_p, ',');
1164                                 _DBG("value %s", dump_p->str);
1165                                 if ((new_state.event_code & ET_FLAG_START) != 0)
1166                                         g_string_append_c(dump_p, '+');
1167                                 else if ((new_state.event_code & ET_FLAG_FINISH) != 0)
1168                                         g_string_append_c(dump_p, '-');
1169                                 _DBG("value %s", dump_p->str);
1170                                 int k = new_state.event_code & ~(ET_FLAG_START | ET_FLAG_FINISH);
1171                                 if (k >= 0 && k < 32) {
1172                                         gchar* temp = g_strdup(event_map[k]);
1173                                         g_string_append(dump_p, temp);
1174                                         _DBG("value %s", dump_p->str);
1175                                         g_free(temp);
1176                                 } else {
1177                                         g_string_append(dump_p, "Ev");
1178                                         g_string_append_printf(dump_p, "%d", k);
1179                                         _DBG("value %s", dump_p->str);
1180                                 }
1181                                 g_string_append_c(dump_p, '=');
1182                                 _DBG("value %s", dump_p->str);
1183                                 if (chkin) {
1184                                         g_string_append_printf(dump_p, "%d",
1185                                                 new_state.event_tag->sp_idx);
1186                                         //:Free sp memory.
1187                                 } else {
1188                                         //: for NC
1189                                 }
1190                         }
1191                         g_string_append(dump_p, "\n");
1192                         //: step details
1193                         old_state.state_1 = new_state.state_1;
1194                         old_state.state_2 = new_state.state_2;
1195                 }
1196                 _DBG("State assigned");
1197                 _DBG("%s", dump_p->str);
1198                 if (write(fd, dump_p->str, dump_p->len) < 0)
1199                         _WARN("write error");
1200                 g_string_free(dump_p, TRUE);
1201         }
1202
1203         // Dump the elapsed time from last event
1204         // It should be dumped without storing
1205         GString *dump_tmp_p = g_string_sized_new(30);
1206         BM_CHECK_MEM_ALLOC(dump_tmp_p, {});
1207         struct timeval tv;
1208         gettimeofday(&tv, NULL);
1209
1210         g_string_append(dump_tmp_p, TIZEN_DUMP_VERSION);
1211         g_string_append_c(dump_tmp_p, ',');
1212         g_string_append(dump_tmp_p, DUMP_DATA_TAG);
1213         g_string_append_c(dump_tmp_p, ',');
1214         g_string_append_printf(dump_tmp_p, "%lld",
1215                         ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000) - old_state.time_s);
1216         g_string_append(dump_tmp_p, "\n");
1217
1218         if (write(fd, dump_tmp_p->str, dump_tmp_p->len) < 0)
1219                 _WARN("write error");
1220         g_string_free(dump_tmp_p, TRUE);
1221
1222         //Dump i & l values after this
1223         ret = bd_get_and_print_uid_info_entry_from_dump(fd);
1224         if (ret != BATTERY_MONITOR_ERROR_NONE)
1225                 _ERR("UID data dump failed");
1226         // Get Last charge time to dump "l" field
1227         long int duration = 0;
1228         bd_get_last_charge_time(base_time/1000, &ret);
1229         duration = lst_charge;
1230
1231         ret = bd_get_and_print_gn_stat_entry_from_dump(fd, duration);
1232         if (ret != BATTERY_MONITOR_ERROR_NONE)
1233                 _ERR("GN dump failed");
1234         ret = bd_get_and_print_gwf_stat_entry_from_dump(fd, duration);
1235         if (ret != BATTERY_MONITOR_ERROR_NONE)
1236                 _ERR("GWF data dump failed");
1237         ret = bd_get_and_print_gble_stat_entry_from_dump(fd, duration);
1238         if (ret != BATTERY_MONITOR_ERROR_NONE)
1239                 _ERR("GBL data dump failed");
1240         ret = bd_get_and_print_gbr_stat_entry_from_dump(fd, duration);
1241         if (ret != BATTERY_MONITOR_ERROR_NONE)
1242                 _ERR("GBR data dump failed");
1243         ret = bd_get_and_print_gst_stat_entry_from_dump(fd, duration);
1244         if (ret != BATTERY_MONITOR_ERROR_NONE)
1245                 _ERR("GST data dump failed");
1246         ret = bd_get_and_print_gwst_stat_entry_from_dump(fd, duration);
1247         if (ret != BATTERY_MONITOR_ERROR_NONE)
1248                 _ERR("GWST data dump failed");
1249         ret = bd_get_and_print_gcpu_stat_entry_from_dump(fd, duration);
1250         if (ret != BATTERY_MONITOR_ERROR_NONE)
1251                 _ERR("GCPU data dump failed");
1252         ret = bd_get_and_print_misc_stat_entry_from_dump(fd, duration);
1253         if (ret != BATTERY_MONITOR_ERROR_NONE)
1254                 _ERR("MISc data dump failed");
1255         // Battery Usage Dump
1256         ret = bd_get_and_print_battery_usage_from_dump(fd, duration);
1257         if (ret != BATTERY_MONITOR_ERROR_NONE)
1258                 _ERR("Battery Usage failed");
1259
1260         _DBG("DUMP COMPLETED/DELETE HASH TABLE");
1261
1262         g_hash_table_destroy(app_map);
1263         h_flag = true;
1264
1265         EXIT;
1266         return BATTERY_MONITOR_ERROR_NONE;
1267 }
1268
1269 static int bd_dump_data_item_reset(dump_data_s* old_state)
1270 {
1271         ENTER;
1272         BM_CHECK_INPUT_PARAM(old_state);
1273
1274         old_state->time_s = -1;
1275         old_state->time_current = -1;
1276         old_state->dump_1 = 0;
1277         old_state->dump_2 = 0;
1278         old_state->state_1 = 0;
1279         old_state->state_2 = 0;
1280         old_state->battery_chargeUAh = 0;
1281         old_state->usage_type = 0;
1282         old_state->usage = 0;
1283         old_state->event_code = ET_NONE;
1284         old_state->event_tag = NULL;
1285         old_state->wakelock_tag = NULL;
1286         old_state->wakereason_tag = NULL;
1287
1288         EXIT;
1289         return BATTERY_MONITOR_ERROR_NONE;
1290 }
1291
1292 static int bd_free_history_data_memory()
1293 {
1294         ENTER;
1295
1296         int ret = BATTERY_MONITOR_ERROR_NONE;
1297         h_flag = false; //Lock
1298         _DBG("Free History Data Memory");
1299         for (int i = 0; i < h_count; i++) {
1300                 if (history_data[i].wakelock_tag != NULL) {
1301                         g_string_free(history_data[i].wakelock_tag->string_info, TRUE);
1302                         free(history_data[i].wakelock_tag);
1303                 }
1304                 if (history_data[i].wakereason_tag != NULL) {
1305                         g_string_free(history_data[i].wakereason_tag->string_info, TRUE);
1306                         free(history_data[i].wakereason_tag);
1307                 }
1308                 if (history_data[i].event_tag != NULL) {
1309                         g_string_free(history_data[i].event_tag->string_info, TRUE);
1310                         free(history_data[i].event_tag);
1311                 }
1312                 ret = bd_dump_data_item_reset(&history_data[i]);
1313                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1314                         _ERR("Internal error %d while freeing at idx %d", ret , i);
1315         }
1316
1317         h_count = 0; h_flag = true;
1318         EXIT;
1319         return ret;
1320 }
1321
1322 static int bd_set_history_from_listener(history_item_s* nrec)
1323 {
1324         ENTER;
1325         BM_CHECK_INPUT_PARAM(nrec);
1326
1327         int dvar1 = 0, dvar2 = 0;
1328         struct timeval tv;
1329         gettimeofday(&tv, NULL);
1330
1331         time_t current_time = tv.tv_sec;
1332
1333 #ifdef DUMP_DUMMY
1334         history_data[h_count].time_s = nrec->time_s;
1335 #else
1336         history_data[h_count].time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1337 #endif
1338
1339         if (nrec->battery_plugtype == BD_BPLT_AC || nrec->battery_plugtype == BD_BPLT_WL)
1340                 lst_charge = tv.tv_sec;
1341
1342         dvar1 = ((nrec->cmd_s) & 0xff) | ((nrec->battery_level << 8) & 0xff00) |
1343                 ((nrec->battery_status << 16) & 0xf0000) |
1344                 ((nrec->battery_health << 20) & 0xf00000) |
1345                 ((nrec->battery_plugtype << 24) & 0xf000000) |
1346                 ((nrec->wakelock_tag != NULL) ? 0x10000000 : 0) |
1347                 ((nrec->wakereason_tag != NULL) ? 0x20000000 : 0) |
1348                 ((nrec->event_code != ET_NONE) ? 0x40000000 : 0);
1349
1350         dvar2 = ((nrec->battery_temperature) & 0xffff) |
1351                 ((nrec->battery_voltage << 16) & 0xffff0000);
1352
1353         _DBG("Value of encoded value 1 %d", dvar1);
1354         _DBG("Value of encoded value 2 %d", dvar2);
1355
1356         history_data[h_count].dump_1 = dvar1;
1357         history_data[h_count].dump_2 = dvar2;
1358         history_data[h_count].battery_chargeUAh = nrec->battery_charge;
1359
1360         if (nrec->wakelock_tag != NULL)
1361                 history_data[h_count].wakelock_tag = nrec->wakelock_tag;
1362         if (nrec->wakereason_tag != NULL)
1363                 history_data[h_count].wakereason_tag = nrec->wakereason_tag;
1364         if (nrec->event_code != ET_NONE) {
1365                 if (nrec->event_tag == NULL) {
1366                         _ERR("ET is NULL but Event Code is not");
1367                         BM_FREE(history_data[h_count].wakelock_tag);
1368                         BM_FREE(history_data[h_count].wakereason_tag);
1369                         bd_dump_data_item_reset(&history_data[h_count]);
1370                         return BATTERY_MONITOR_ERROR_NO_DATA;
1371                 }
1372                 history_data[h_count].event_code = nrec->event_code;
1373                 history_data[h_count].event_tag = nrec->event_tag;
1374         }
1375         if (nrec->cmd_s == CM_CRR_TIME || nrec->cmd_s == CM_RST)
1376                 history_data[h_count].time_current = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1377         else if (nrec->cmd_s == CM_USAGE)
1378                 history_data[h_count].time_current = nrec->time_current;
1379
1380         history_data[h_count].state_1 = nrec->state_1;
1381         history_data[h_count].state_2 = nrec->state_2;
1382
1383         history_data[h_count].usage_type = nrec->usage_type;
1384         history_data[h_count].usage = nrec->usage;
1385
1386         nrec->event_tag = NULL; nrec->wakereason_tag = NULL;
1387         nrec->wakelock_tag = NULL;
1388         if (h_count < HISTORY_SIZE_MAX - 1)
1389                 h_count++;
1390         else {
1391                 // FIXME modify 1st argument (fd)
1392                 int ret = bd_print_history_item_main(-1, 0, current_time, 1);
1393                 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1394                         h_count = 0;
1395                         return ret;
1396                 }
1397                 ret = bd_free_history_data_memory();
1398                 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1399                         _WARN("Error while memory free %x", ret);
1400                         return ret;
1401                 }
1402         }
1403
1404         EXIT;
1405         return BATTERY_MONITOR_ERROR_NONE;
1406 }
1407
1408 int bd_store_history_item(history_item_s* nrec)
1409 {
1410         ENTER;
1411         BM_CHECK_INPUT_PARAM(nrec);
1412
1413         if (!h_flag) {
1414                 _ERR("Data Dump in progress");
1415                 return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
1416         }
1417         int ret = BATTERY_MONITOR_ERROR_NONE;
1418         ret = bd_set_history_from_listener(nrec);
1419         if (ret != BATTERY_MONITOR_ERROR_NONE) {
1420                 _ERR("Failed to store history");
1421                 return BATTERY_MONITOR_ERROR_INTERNAL;
1422         }
1423
1424         EXIT;
1425         return BATTERY_MONITOR_ERROR_NONE;
1426 }
1427
1428 static int *dumpsys_handler;
1429
1430 int bd_callback(const int fd, const int argc, char **argv)
1431 {
1432         int ret;
1433         int opt;
1434         bool checkin = false;
1435
1436         /**
1437          * We must reset optind because it isn't initialized
1438          * on subsequent getopt calls
1439          */
1440         optind = 1;
1441         while ((opt = getopt(argc, argv, "c")) != -1) {
1442                 switch (opt) {
1443                 case 'c':
1444                         checkin = true;
1445                         break;
1446                 default:
1447                         break;
1448                 }
1449         }
1450
1451         checkin = true;
1452         ret = bd_print_history_item_main(fd, -1, 0, checkin);
1453         if (ret != BATTERY_MONITOR_ERROR_NONE) {
1454                 _ERR("bd_print_history_item_main failed (%x)", ret);
1455                 return 0;
1456         }
1457
1458         return 0;
1459 }
1460
1461 void bd_get_idle_check_time()
1462 {
1463         ENTER;
1464
1465         int ret;
1466         int not_used_int;
1467         int data_collection_try_period;
1468         int data_collection_accept_count;
1469
1470         ret = bm_get_job_scheduler_params(&not_used_int, &data_collection_try_period, &data_collection_accept_count, &not_used_int);
1471         if (ret != BATTERY_MONITOR_ERROR_NONE) {
1472                 min_idle_check_time = 0;
1473                 return;
1474         }
1475
1476         min_idle_check_time = (long long)data_collection_try_period * data_collection_accept_count;
1477
1478         EXIT;
1479         return;
1480 }
1481
1482 int bd_initialize_battery_dump()
1483 {
1484         ENTER;
1485
1486         int ret = BATTERY_MONITOR_ERROR_NONE;
1487
1488         h_count = 0; h_flag = true;
1489         lst_charge = -1;
1490 #ifdef DUMP_DUMMY
1491         bd_dummy_test_fn();
1492 #else
1493         history_item_s ts;
1494         ret = bd_print_history_item_reset(&ts);
1495         if (ret == BATTERY_MONITOR_ERROR_NONE) {
1496                 ts.cmd_s = CM_CRR_TIME;
1497                 ret = bd_store_history_item(&ts);
1498                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1499                         _WARN("Init Node addition failed");
1500         }
1501 #endif
1502
1503         ret = dumpsys_system_register_dump_cb(bd_callback, "batterystats", (void **)&dumpsys_handler);
1504         if (ret != TIZEN_ERROR_NONE) {
1505                 _ERR("dumpsys_system_register_dump_cb failed (%x)", ret);
1506                 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1507         }
1508
1509         bd_get_idle_check_time();
1510
1511         _INFO("Battery Dump Init Successful");
1512
1513         EXIT;
1514         return BATTERY_MONITOR_ERROR_NONE;
1515 }
1516
1517 int bd_deinitialize_battery_dump()
1518 {
1519         ENTER;
1520
1521         int ret;
1522
1523         ret = dumpsys_system_unregister_dump_cb(dumpsys_handler);
1524         if (ret != TIZEN_ERROR_NONE) {
1525                 _WARN("dumpsys_system_unregister_dump_cb failed (%x)", ret);
1526                 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1527         }
1528
1529         if (lst_charge != -1) {
1530                 resourceid_usage_s battery_use;
1531                 battery_use.ResourceId = "battery";
1532                 battery_use.log_time = lst_charge;
1533                 battery_use.usage = 0;
1534                 ret = bm_server_resource_base_usage_insert_to_db(&battery_use);
1535                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1536                         _WARN("Battery Time Insert fail");
1537         }
1538
1539         ret = bd_free_history_data_memory();
1540         if (ret != BATTERY_MONITOR_ERROR_NONE)
1541                 _WARN("memory free failed (%x)", ret);
1542
1543         _INFO("Battery Dump De-Init Successful");
1544
1545         EXIT;
1546         return BATTERY_MONITOR_ERROR_NONE;
1547 }