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