3c443b02afd3aa3db9882a4f4af389671b96b30e
[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         bd_gl_cpu_stat_s *gcpu_stat = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
520         BM_CHECK_MEM_ALLOC(gcpu_stat, {});
521
522         int ret = BATTERY_MONITOR_ERROR_NONE;
523         const char *rid_str = NULL; int usage = 0;
524         rid_str = bm_get_resource_id_string(2);
525         resourceid_usage_s *res_use =
526                 bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
527         if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
528                 _ERR("For resourceid %s", rid_str);
529                 _DBG("Dump only system stats");
530         }
531         usage = res_use->usage;
532         float usagemah = (float)usage/3600;
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         GString *dump_p = g_string_sized_new(30);
541         BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gcpu_stat); BM_FREE(res_use);});
542         g_string_append(dump_p, TIZEN_DUMP_VERSION);
543         g_string_append(dump_p, ",0,l,");
544         g_string_append(dump_p, "cpu,");
545         g_string_append_printf(dump_p, "%ld,", gcpu_stat->usr_time);
546         g_string_append_printf(dump_p, "%ld,", gcpu_stat->sys_time);
547         g_string_append_printf(dump_p, "%0.2f", usagemah);
548         g_string_append(dump_p, "\n");
549         bm_resourceid_usage_free(res_use);
550         _DBG("%s", dump_p->str);
551         if (write(fd, dump_p->str, dump_p->len) < 0)
552                 _WARN("write error");
553         g_string_free(dump_p, TRUE);
554
555         EXIT;
556         return ret;
557 }
558
559 static int bd_get_and_print_uid_info_entry_from_dump(int fd)
560 {
561         ENTER;
562         // Dump UID entry
563         int ret = BATTERY_MONITOR_ERROR_NONE;
564         GSList *list = bm_server_query_appid_map(&ret);
565         if (list == NULL) {
566                 _ERR("NO LIST DATA FOUND");
567                 return ret;
568         }
569         app_map = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
570         GSList *iter = NULL; GString *dump_p = NULL;
571         dump_p = g_string_sized_new(30);
572         BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
573         g_string_append(dump_p, TIZEN_DUMP_VERSION);
574         g_string_append(dump_p, ",0,i,uid,1000,tizen\n");
575         if (write(fd, dump_p->str, dump_p->len) < 0)
576                 _WARN("write error");
577         g_string_free(dump_p, TRUE);
578
579         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
580                 dump_p = g_string_sized_new(30);
581                 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
582                 g_string_append(dump_p, TIZEN_DUMP_VERSION);
583                 g_string_append(dump_p, ",0");
584                 g_string_append(dump_p, ",i");
585                 g_string_append(dump_p, ",uid,");
586                 appid_map_s *amap_data = (appid_map_s *)iter->data;
587                 g_string_append_printf(dump_p, "%d,", (amap_data->id+10000));
588                 gchar *temp_str = g_strdup(amap_data->AppId);
589                 g_string_append(dump_p, amap_data->AppId);
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                 int *temp_id = (int *)malloc(4);
595                 *temp_id = (amap_data->id + 10000);
596                 _DBG("Insert id %d, str %s, res %d", *temp_id, temp_str,
597                 g_hash_table_insert(app_map, temp_id, temp_str));
598                 g_string_free(dump_p, TRUE);
599         }
600         bm_set_free_gslist_appid_map(list);
601
602         EXIT;
603         return ret;
604 }
605
606 static int bd_get_history_detail_from_dump(int index, history_item_s* dump_node)
607 {
608         ENTER;
609         BM_CHECK_INPUT_PARAM(dump_node);
610
611         if (h_count >= HISTORY_SIZE_MAX || index > h_count) {
612                 _ERR("Some Error occured with global index");
613                 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
614         }
615
616         _DBG("Value of index %d", index);
617         _DBG("Value of h_count %d", h_count);
618
619         int dvar1 = 0, dvar2 = 0;
620         dvar1 = history_data[index].dump_1;
621         dvar2 = history_data[index].dump_2;
622
623         _DBG("Value of encoded value 1 %d", dvar1);
624         _DBG("Value of encoded value 2 %d", dvar2);
625
626         dump_node->cmd_s = ((dvar1) & 0xff);
627         _DBG("cmd_s %d", dump_node->cmd_s);
628
629         dump_node->battery_level = ((dvar1>>8) & 0xff);
630         _DBG("battery_level %d", dump_node->battery_level);
631
632         dump_node->battery_status = ((dvar1>>16) & 0xf);
633         _DBG("battery_status %d", dump_node->battery_status);
634
635         dump_node->battery_health = ((dvar1>>20) & 0xf);
636         _DBG("battery_health %d", dump_node->battery_health);
637
638         dump_node->battery_plugtype = ((dvar1>>24) & 0xf);
639         _DBG("battery_plugtype %d", dump_node->battery_plugtype);
640
641         dump_node->battery_temperature = ((dvar2) & 0xffff);
642         _DBG("battery_temperature %d", dump_node->battery_temperature);
643
644         dump_node->battery_voltage = ((dvar2>>16) & 0xffff);
645         _DBG("battery_voltage %d", dump_node->battery_voltage);
646
647         dump_node->battery_charge = history_data[index].battery_chargeUAh;
648         _DBG("battery_charge %d", dump_node->battery_charge);
649
650         dump_node->event_code = history_data[index].event_code;
651         _DBG("eventcode %d", dump_node->event_code);
652
653         dump_node->state_1 = history_data[index].state_1;
654         _DBG("state_1 %d", dump_node->state_1);
655
656         dump_node->state_2 = history_data[index].state_2;
657         _DBG("state_2 %d", dump_node->state_2);
658
659         dump_node->event_tag = history_data[index].event_tag;
660         if (dump_node->event_tag == NULL)
661                 _DBG("Event_Tag_NULL");
662         else//Remove
663                 _DBG("%d %d %s", dump_node->event_tag->uid, dump_node->event_tag->sp_idx,
664                 dump_node->event_tag->string_info->str);
665         dump_node->wakelock_tag = history_data[index].wakelock_tag;
666         if (dump_node->wakelock_tag == NULL)
667                 _DBG("Wakelock_Tag_NULL");
668         dump_node->wakereason_tag = history_data[index].wakereason_tag;
669         //:Not supported
670         if (dump_node->wakereason_tag == NULL)
671                 _DBG("Wakereason_Tag_NULL");
672
673         dump_node->time_s = history_data[index].time_s;
674         _DBG("TIME_S is %lld", dump_node->time_s);
675
676         dump_node->time_current = history_data[index].time_current;
677         _DBG("C TIME is %lld", dump_node->time_current);
678
679         dump_node->usage_type = history_data[index].usage_type;
680         _DBG("Usage type is %d", dump_node->usage_type);
681
682         dump_node->usage = history_data[index].usage;
683         _DBG("Usage is %d", dump_node->usage);
684
685         EXIT;
686         return BATTERY_MONITOR_ERROR_NONE;
687 }
688
689 int bd_print_history_item_reset(history_item_s* old_state)
690 {
691         ENTER;
692         BM_CHECK_INPUT_PARAM(old_state);
693
694         old_state->time_s = -1;
695         old_state->state_1 = 0;
696         old_state->state_2 = 0;
697         old_state->usage_type = 0;
698         old_state->usage = 0;
699         old_state->battery_level = -1;
700         old_state->battery_status = -1;
701         old_state->battery_health = -1;
702         old_state->battery_plugtype = -1;
703         old_state->battery_temperature = -1;
704         old_state->battery_voltage = -1;
705         old_state->battery_charge = -1;
706         old_state->event_code = 0;
707         old_state->event_tag = NULL;
708         old_state->wakelock_tag = NULL;
709         old_state->wakereason_tag = NULL;
710
711         EXIT;
712         return BATTERY_MONITOR_ERROR_NONE;
713 }
714
715 int bd_get_base_time_from_dump(long long *base_time)
716 {
717         ENTER;
718
719         *base_time = history_data[0].time_s;
720
721         EXIT;
722         return BATTERY_MONITOR_ERROR_NONE;
723 }
724
725 int bd_print_history_item_main(int fd, int num_h_items, long long base_time, bool chkin)
726 {
727         ENTER;
728
729         if (fd < 0) {
730                 _ERR("Invalid fd");
731                 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
732         }
733
734         if (h_count >= HISTORY_SIZE_MAX) {
735                 _ERR("Some Error occured with global index");
736                 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
737         }
738
739         int ret = BATTERY_MONITOR_ERROR_NONE;
740         h_flag = false; //Lock
741
742         history_item_s new_state;
743         history_item_s old_state;
744
745         ret = bd_print_history_item_reset(&old_state);
746         if (ret != BATTERY_MONITOR_ERROR_NONE) {
747                 _ERR("Old State reset failed");
748                 return ret;
749         }
750
751         ret = bd_get_pool_entry_from_dump(fd);
752         if (ret != BATTERY_MONITOR_ERROR_NONE)
753                 _ERR("S pool data dump failed");
754
755         ret = bd_get_base_time_from_dump(&base_time);
756         if (ret != BATTERY_MONITOR_ERROR_NONE)
757                 _ERR("Base time error");
758
759         int log_count = h_count; GString *dump_p = NULL;
760         for (int i = 0; i < log_count; i++) {
761                 ret = bd_get_history_detail_from_dump(i, &new_state);
762                 if (ret != BATTERY_MONITOR_ERROR_NONE) {
763                         _ERR("Decode Failed");
764                         return ret;
765                 }
766                 dump_p = g_string_sized_new(30);
767                 BM_CHECK_MEM_ALLOC(dump_p, {});
768                 if (chkin) {
769                         g_string_append(dump_p, TIZEN_DUMP_VERSION);
770                         g_string_append_c(dump_p, ',');
771                         _DBG("value %s", dump_p->str);
772                         g_string_append(dump_p, DUMP_DATA_TAG);
773                         g_string_append_c(dump_p, ',');
774                         _DBG("value %s", dump_p->str);
775                         if (old_state.time_s < 0) {
776                                 g_string_append_printf(dump_p, "%lld",
777                                                 (new_state.time_s - base_time));
778                                 _DBG("value %s", dump_p->str);
779                         } else {
780                                 g_string_append_printf(dump_p, "%lld",
781                                                 (new_state.time_s - old_state.time_s));
782                                 _DBG("value %s", dump_p->str);
783                         }
784                         old_state.time_s = new_state.time_s;
785                         _DBG("Value old_state.time_s %lld", old_state.time_s);
786                 } else { //: Let's skip the NC mode in initial version
787                 }
788                 if (new_state.cmd_s == CM_ST) {
789                         if (chkin) {
790                                 g_string_append_c(dump_p, ':');
791                                 _DBG("value %s", dump_p->str);
792                         } else {
793                                 //NC
794                         }
795                         g_string_append(dump_p, "START\n");
796                         _DBG("value %s", dump_p->str);
797                         bd_print_history_item_reset(&old_state);
798                 } else if (new_state.cmd_s == CM_CRR_TIME || new_state.cmd_s == CM_RST) {
799                         if (chkin) {
800                                 g_string_append_c(dump_p, ':');
801                                 _DBG("value %s", dump_p->str);
802                         }
803                         if (new_state.cmd_s == CM_RST) {
804                                 g_string_append(dump_p, "RESET:");
805                                 _DBG("value %s", dump_p->str);
806                                 bd_print_history_item_reset(&old_state);
807                         }
808                         g_string_append(dump_p, "TIME:");
809                         _DBG("value %s", dump_p->str);
810                         if (chkin) {
811                                 g_string_append_printf(dump_p, "%lld", new_state.time_current);
812                                 _DBG("value %s", dump_p->str);
813                                 g_string_append(dump_p, "\n");
814                                 _DBG("value %s", dump_p->str);
815                         } else {
816                                 //NC
817                         }
818                 } else if (new_state.cmd_s == CM_SHTDN) {
819                         if (chkin) {
820                                 g_string_append_c(dump_p, ':');
821                                 _DBG("value %s", dump_p->str);
822                         } else {
823                                 //NC
824                         }
825                         g_string_append(dump_p, "SHUTDOWN\n");
826                         _DBG("value %s", dump_p->str);
827                 } else if (new_state.cmd_s == CM_OVRFL) {
828                         if (chkin) {
829                                 g_string_append_c(dump_p, ':');
830                                 _DBG("value %s", dump_p->str);
831                         }
832                         g_string_append(dump_p, "*OVERFLOW*\n");
833                         _DBG("value %s", dump_p->str);
834                 } else if (new_state.cmd_s == CM_USAGE) {
835                         for (int i = 0; i < 32; i++) {
836                                 if (new_state.usage_type == (1 << i)) {
837                                         g_string_append(dump_p, ",");
838                                         g_string_append_printf(dump_p, "%s", usage_map[i]);
839                                         g_string_append(dump_p, "=");
840                                         g_string_append_printf(dump_p, "%d", new_state.usage);
841                                         g_string_append(dump_p, "\n");
842                                         _DBG("value %d", new_state.usage);
843                                 }
844                         }
845                 } else {
846                         if (!chkin) {
847                                 //NC
848                         } else {
849                                 if (old_state.battery_level != new_state.battery_level) {
850                                         old_state.battery_level = new_state.battery_level;
851                                         g_string_append(dump_p, ",Bl=");
852                                         _DBG("value %s", dump_p->str);
853                                         g_string_append_printf(dump_p, "%d",
854                                                         new_state.battery_level);
855                                         _DBG("value %s", dump_p->str);
856                                 }
857                         }
858                         if (old_state.battery_status != new_state.battery_status) {
859                                 old_state.battery_status = new_state.battery_status;
860                                 g_string_append(dump_p, ",Bs="); //:for NC
861                                 _DBG("value %s", dump_p->str);
862                                 switch (old_state.battery_status) {
863                                 case BD_BSTA_UNKN:
864                                         g_string_append_c(dump_p, '?');
865                                         break;
866                                 case BD_BSTA_CHAR:
867                                         g_string_append_c(dump_p, 'c');
868                                         break;
869                                 case BD_BSTA_DISC:
870                                         g_string_append_c(dump_p, 'd');
871                                         break;
872                                 case BD_BSTA_NCHA:
873                                         g_string_append_c(dump_p, 'n');
874                                         break;
875                                 case BD_BSTA_FULL:
876                                         g_string_append_c(dump_p, 'f');
877                                         break;
878                                 default:
879                                         g_string_append_c(dump_p, '?');
880                                         _DBG("value %s", dump_p->str);
881                                         break;
882                                 }
883                         }
884                         if (old_state.battery_health != new_state.battery_health) {
885                                 old_state.battery_health = new_state.battery_health;
886                                 g_string_append(dump_p, ",Bh="); //:for NC
887                                 _DBG("value %s", dump_p->str);
888                                 switch (old_state.battery_health) {
889                                 case BD_BHLT_GOOD:
890                                         g_string_append_c(dump_p, 'g');
891                                         break;
892                                 case BD_BHLT_UNKN:
893                                         g_string_append_c(dump_p, '?');
894                                         break;
895                                 case BD_BHLT_OVRH:
896                                         g_string_append_c(dump_p, 'h');
897                                         break;
898                                 case BD_BHLT_DEAD:
899                                         g_string_append_c(dump_p, 'd');
900                                         break;
901                                 case BD_BHLT_OVRV:
902                                         g_string_append_c(dump_p, 'v');
903                                         break;
904                                 case BD_BHLT_UNSP:
905                                         g_string_append_c(dump_p, 'f');
906                                         break;
907                                 case BD_BHLT_CLD:
908                                         g_string_append_c(dump_p, 'c');
909                                         break;
910                                 default:
911                                         g_string_append_c(dump_p, '?');
912                                         _DBG("value %s", dump_p->str);
913                                         break;
914                                 }
915                         }
916                         if (old_state.battery_plugtype != new_state.battery_plugtype) {
917                                 old_state.battery_plugtype = new_state.battery_plugtype;
918                                 g_string_append(dump_p, ",Bp="); //:for NC
919                                 _DBG("value %s", dump_p->str);
920                                 switch (old_state.battery_plugtype) {
921                                 case BD_BPLT_DIS:
922                                         g_string_append_c(dump_p, 'n');
923                                         break;
924                                 case BD_BPLT_AC:
925                                         g_string_append_c(dump_p, 'a');
926                                         break;
927                                 case BD_BPLT_USB:
928                                         g_string_append_c(dump_p, 'u');
929                                         break;
930                                 case BD_BPLT_WL:
931                                         g_string_append_c(dump_p, 'w');
932                                         break;
933                                 default:
934                                         g_string_append_c(dump_p, 'n');
935                                         _DBG("value %s", dump_p->str);
936                                         break;
937                                 }
938                         }
939                         if (old_state.battery_temperature != new_state.battery_temperature) {
940                                 old_state.battery_temperature = new_state.battery_temperature;
941                                 g_string_append(dump_p, ",Bt="); //:for NC
942                                 g_string_append_printf(dump_p, "%d",
943                                                 new_state.battery_temperature);
944                                 _DBG("value %s", dump_p->str);
945                         }
946                         if (old_state.battery_voltage != new_state.battery_voltage) {
947                                 old_state.battery_voltage = new_state.battery_voltage;
948                                 g_string_append(dump_p, ",Bv="); //:for NC
949                                 _DBG("value %s", dump_p->str);
950                                 g_string_append_printf(dump_p, "%d", new_state.battery_voltage);
951                                 _DBG("value %s", dump_p->str);
952                         }
953                         if (old_state.battery_charge != new_state.battery_charge) {
954                                 old_state.battery_charge = new_state.battery_charge;
955                                 g_string_append(dump_p, ",Bcc=");
956                                 _DBG("value %s", dump_p->str);
957                                 //Charge in MAh
958                                 g_string_append_printf(dump_p, "%d", (new_state.battery_charge/1000));
959                                 _DBG("value %s", dump_p->str);
960                         }
961                         int change = (old_state.state_1 ^ new_state.state_1);
962                         if (change != 0) {
963                                 bool wake_fl = false; int k = 0;
964                                 if ((change & ST1_BRIGHTNESS_MASK) != 0) {
965                                         k = (new_state.state_1 & ST1_BRIGHTNESS_MASK);
966                                         g_string_append(dump_p, ",Sb=");
967                                         k = k >> ST1_BRIGHTNESS_SHIFT;
968                                         _DBG("Sb = %d", k);
969                                         g_string_append_printf(dump_p, "%d", k);
970                                         k = 0;
971                                 }
972                                 if ((change & ST1_PHONE_SIGNAL_STRENGTH_MASK) != 0) {
973                                         k = (new_state.state_1 & ST1_PHONE_SIGNAL_STRENGTH_MASK);
974                                         g_string_append(dump_p, ",Pss=");
975                                         k = k >> ST1_PHONE_SIGNAL_STRENGTH_SHIFT;
976                                         _DBG("Pss = %d", k);
977                                         g_string_append_printf(dump_p, "%d", k);
978                                         k = 0;
979                                 }
980                                 int shifts = (1 << 14);
981                                 for (int i = 14; i < 32; i++) {
982                                         if ((change & shifts) != 0) {
983                                                 g_string_append_c(dump_p, ',');
984                                                 //Considering only -ve shift cases
985                                                 int shift = -1;
986                                                 if (shift < 0) {
987                                                         if ((new_state.state_1 & (1 << i)) != 0)
988                                                                 g_string_append_c(dump_p, '+');
989                                                         else
990                                                                 g_string_append_c(dump_p, '-');
991                                                         gchar *temp = g_strdup(state1_map[i]);
992                                                         g_string_append(dump_p, temp);
993                                                         g_free(temp);
994                                                         //_DBG("value %s", dump_p->str);
995                                                         if ((1 << i) == ST1_WAKE_LOCK_FLAG &&
996                                                                 new_state.wakelock_tag != NULL) {
997                                                                 wake_fl = true;
998                                                                 g_string_append_c(dump_p, '=');
999                                                                 //_DBG("value %s", dump_p->str);
1000                                                                 if (chkin)
1001                                                                         g_string_append_printf(dump_p,
1002                                                                 "%d", new_state.wakelock_tag->sp_idx);
1003                                                                 else {
1004                                                                         //: NC
1005                                                                 }
1006                                                                 //_DBG("value %s", dump_p->str);
1007                                                         }
1008                                                 } else {
1009                                                 //With non-negative shift
1010                                                 }
1011                                         }
1012                                         shifts = (shifts << 1);
1013                                 }
1014                                 if (!wake_fl && new_state.wakelock_tag != NULL) {
1015                                         g_string_append(dump_p, ",w=");
1016                                         if (chkin) {
1017                                                 g_string_append_printf(dump_p, "%d",
1018                                                 new_state.wakelock_tag->sp_idx);
1019                                                 _DBG("value %s", dump_p->str);
1020                                         } else {
1021                                         }
1022                                 }
1023                         }
1024                         change = (old_state.state_2 ^ new_state.state_2);
1025                         if (change != 0) {
1026                                 int shifts = 1;
1027                                 for (int i = 0; i < 32; i++) {
1028                                         if ((change & shifts) != 0) {
1029                                                 g_string_append_c(dump_p, ',');
1030                                                 //_DBG("value %s", dump_p->str);
1031                                                 int shift = -1;
1032                                                 if (shift < 0) {
1033                                                         if ((new_state.state_2 & (1 << i) & change) != 0)
1034                                                                 g_string_append_c(dump_p, '+');
1035                                                         else
1036                                                                 g_string_append_c(dump_p, '-');
1037                                                         //_DBG("value %s", dump_p->str);
1038                                                         gchar *temp = g_strdup(state2_map[i]);
1039                                                         g_string_append(dump_p, temp);
1040                                                         g_free(temp);
1041                                                         _DBG("value %s", dump_p->str);
1042                                                 }
1043                                         }
1044                                         shifts = (shifts << 1);
1045                                 }
1046                         }
1047                         if (new_state.wakereason_tag != NULL) {
1048                                 if (chkin) {
1049                                         g_string_append(dump_p, ",wr=");
1050                                         _DBG("value %s", dump_p->str);
1051                                         g_string_append_printf(dump_p, "%d",
1052                                         new_state.wakereason_tag->sp_idx);
1053                                         _DBG("value %s", dump_p->str);
1054                                 }
1055                         } else {
1056                                 //: for NC
1057                         }
1058                         if (new_state.event_code != ET_NONE) {
1059                                 g_string_append_c(dump_p, ',');
1060                                 _DBG("value %s", dump_p->str);
1061                                 if ((new_state.event_code & ET_FLAG_START) != 0)
1062                                         g_string_append_c(dump_p, '+');
1063                                 else if ((new_state.event_code & ET_FLAG_FINISH) != 0)
1064                                         g_string_append_c(dump_p, '-');
1065                                 _DBG("value %s", dump_p->str);
1066                                 int k = new_state.event_code & ~(ET_FLAG_START | ET_FLAG_FINISH);
1067                                 if (k >= 0 && k < 32) {
1068                                         gchar* temp = g_strdup(event_map[k]);
1069                                         g_string_append(dump_p, temp);
1070                                         _DBG("value %s", dump_p->str);
1071                                         g_free(temp);
1072                                 } else {
1073                                         g_string_append(dump_p, "Ev");
1074                                         g_string_append_printf(dump_p, "%d", k);
1075                                         _DBG("value %s", dump_p->str);
1076                                 }
1077                                 g_string_append_c(dump_p, '=');
1078                                 _DBG("value %s", dump_p->str);
1079                                 if (chkin) {
1080                                         g_string_append_printf(dump_p, "%d",
1081                                                 new_state.event_tag->sp_idx);
1082                                         //:Free sp memory.
1083                                 } else {
1084                                         //: for NC
1085                                 }
1086                         }
1087                         g_string_append(dump_p, "\n");
1088                         //: step details
1089                         old_state.state_1 = new_state.state_1;
1090                         old_state.state_2 = new_state.state_2;
1091                 }
1092                 _DBG("State assigned");
1093                 _DBG("%s", dump_p->str);
1094                 if (write(fd, dump_p->str, dump_p->len) < 0)
1095                         _WARN("write error");
1096                 g_string_free(dump_p, TRUE);
1097         }
1098
1099         // Dump the elapsed time from last event
1100         // It should be dumped without storing
1101         GString *dump_tmp_p = g_string_sized_new(30);
1102         struct timeval tv;
1103         gettimeofday(&tv, NULL);
1104
1105         g_string_append(dump_tmp_p, TIZEN_DUMP_VERSION);
1106         g_string_append_c(dump_tmp_p, ',');
1107         g_string_append(dump_tmp_p, DUMP_DATA_TAG);
1108         g_string_append_c(dump_tmp_p, ',');
1109         g_string_append_printf(dump_tmp_p, "%lld",
1110                         ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000) - old_state.time_s);
1111         g_string_append(dump_tmp_p, "\n");
1112
1113         if (write(fd, dump_tmp_p->str, dump_tmp_p->len) < 0)
1114                 _WARN("write error");
1115         g_free(dump_tmp_p->str);
1116
1117                 //Dump i & l values after this
1118                 ret = bd_get_and_print_uid_info_entry_from_dump(fd);
1119                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1120                         _ERR("UID data dump failed");
1121                 // Get Last charge time to dump "l" field
1122                 // for the time being dumping all data
1123                 long int duration = 0; //Time of last charge
1124                 ret = bd_get_and_print_gn_stat_entry_from_dump(fd, duration);
1125                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1126                         _ERR("GN dump failed");
1127                 ret = bd_get_and_print_gwf_stat_entry_from_dump(fd, duration);
1128                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1129                         _ERR("GWF data dump failed");
1130                 ret = bd_get_and_print_gble_stat_entry_from_dump(fd, duration);
1131                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1132                         _ERR("GBL data dump failed");
1133                 ret = bd_get_and_print_gbr_stat_entry_from_dump(fd, duration);
1134                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1135                         _ERR("GBR data dump failed");
1136                 ret = bd_get_and_print_gst_stat_entry_from_dump(fd, duration);
1137                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1138                         _ERR("GST data dump failed");
1139                 ret = bd_get_and_print_gwst_stat_entry_from_dump(fd, duration);
1140                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1141                         _ERR("GWST data dump failed");
1142                 ret = bd_get_and_print_gcpu_stat_entry_from_dump(fd, duration);
1143                         _ERR("GCPU data dump failed");
1144                 // Battery Usage Dump
1145                 // Add usage for each of the resource
1146                 // and each of the application
1147                 ret = bd_get_and_print_battery_usage_from_dump(fd, duration);
1148                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1149                         _ERR("Battery Usage failed");
1150
1151                 _DBG("DUMP COMPLETED/DELETE HASH TABLE");
1152
1153                 g_hash_table_destroy(app_map);
1154                 h_flag = true;
1155                 EXIT;
1156                 return BATTERY_MONITOR_ERROR_NONE;
1157 }
1158
1159 static int bd_dump_data_item_reset(dump_data_s* old_state)
1160 {
1161         ENTER;
1162         BM_CHECK_INPUT_PARAM(old_state);
1163
1164         old_state->time_s = -1;
1165         old_state->time_current = -1;
1166         old_state->dump_1 = 0;
1167         old_state->dump_2 = 0;
1168         old_state->state_1 = 0;
1169         old_state->state_2 = 0;
1170         old_state->battery_chargeUAh = 0;
1171         old_state->usage_type = 0;
1172         old_state->usage = 0;
1173         old_state->event_code = ET_NONE;
1174         old_state->event_tag = NULL;
1175         old_state->wakelock_tag = NULL;
1176         old_state->wakereason_tag = NULL;
1177
1178         EXIT;
1179         return BATTERY_MONITOR_ERROR_NONE;
1180 }
1181
1182 static int bd_free_history_data_memory()
1183 {
1184         ENTER;
1185
1186         int ret = BATTERY_MONITOR_ERROR_NONE;
1187         h_flag = false; //Lock
1188         _DBG("Free History Data Memory");
1189         for (int i = 0; i < h_count; i++) {
1190                 if (history_data[i].wakelock_tag != NULL) {
1191                         _DBG("WL str");
1192                         g_string_free(history_data[i].wakelock_tag->string_info, TRUE);
1193                         _DBG("WL");
1194                         free(history_data[i].wakelock_tag);
1195                 }
1196                 if (history_data[i].wakereason_tag != NULL) {
1197                         _DBG("WR str");
1198                         g_string_free(history_data[i].wakereason_tag->string_info, TRUE);
1199                         _DBG("WR");
1200                         free(history_data[i].wakereason_tag);
1201                 }
1202                 if (history_data[i].event_tag != NULL) {
1203                         _DBG("ET str");
1204                         g_string_free(history_data[i].event_tag->string_info, TRUE);
1205                         _DBG("ET");
1206                         free(history_data[i].event_tag);
1207                 }
1208                 ret = bd_dump_data_item_reset(&history_data[i]);
1209                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1210                         _ERR("Internal error %d while freeing at idx %d", ret , i);
1211         }
1212
1213         h_count = 0; h_flag = true;
1214         EXIT;
1215         return ret;
1216 }
1217
1218 static int bd_set_history_from_listener(history_item_s* nrec)
1219 {
1220         ENTER;
1221         BM_CHECK_INPUT_PARAM(nrec);
1222
1223         int dvar1 = 0, dvar2 = 0;
1224         struct timeval tv;
1225         gettimeofday(&tv, NULL);
1226
1227         time_t current_time = tv.tv_sec;
1228
1229 #ifdef DUMP_DUMMY
1230         history_data[h_count].time_s = nrec->time_s;
1231 #else
1232         history_data[h_count].time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1233 #endif
1234
1235         dvar1 = ((nrec->cmd_s) & 0xff) | ((nrec->battery_level << 8) & 0xff00) |
1236                 ((nrec->battery_status << 16) & 0xf0000) |
1237                 ((nrec->battery_health << 20) & 0xf00000) |
1238                 ((nrec->battery_plugtype << 24) & 0xf000000) |
1239                 ((nrec->wakelock_tag != NULL) ? 0x10000000 : 0) |
1240                 ((nrec->wakereason_tag != NULL) ? 0x20000000 : 0) |
1241                 ((nrec->event_code != ET_NONE) ? 0x40000000 : 0);
1242
1243         dvar2 = ((nrec->battery_temperature) & 0xffff) |
1244                 ((nrec->battery_voltage << 16) & 0xffff0000);
1245
1246         _DBG("Value of encoded value 1 %d", dvar1);
1247         _DBG("Value of encoded value 2 %d", dvar2);
1248
1249         history_data[h_count].dump_1 = dvar1;
1250         history_data[h_count].dump_2 = dvar2;
1251         history_data[h_count].battery_chargeUAh = nrec->battery_charge;
1252
1253         if (nrec->wakelock_tag != NULL)
1254                 history_data[h_count].wakelock_tag = nrec->wakelock_tag;
1255         if (nrec->wakereason_tag != NULL)
1256                 history_data[h_count].wakereason_tag = nrec->wakereason_tag;
1257         if (nrec->event_code != ET_NONE) {
1258                 if (nrec->event_tag == NULL) {
1259                         _ERR("ET is NULL but Event Code is not");
1260                         BM_FREE(history_data[h_count].wakelock_tag);
1261                         BM_FREE(history_data[h_count].wakereason_tag);
1262                         bd_dump_data_item_reset(&history_data[h_count]);
1263                         return BATTERY_MONITOR_ERROR_NO_DATA;
1264                 }
1265                 history_data[h_count].event_code = nrec->event_code;
1266                 history_data[h_count].event_tag = nrec->event_tag;
1267         }
1268         if (nrec->cmd_s == CM_CRR_TIME || nrec->cmd_s == CM_RST)
1269                 history_data[h_count].time_current = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1270
1271         history_data[h_count].state_1 = nrec->state_1;
1272         history_data[h_count].state_2 = nrec->state_2;
1273
1274         history_data[h_count].usage_type = nrec->usage_type;
1275         history_data[h_count].usage = nrec->usage;
1276
1277         nrec->event_tag = NULL; nrec->wakereason_tag = NULL;
1278         nrec->wakelock_tag = NULL;
1279         if (h_count < HISTORY_SIZE_MAX - 1)
1280                 h_count++;
1281         else {
1282                 // FIXME modify 1st argument (fd)
1283                 int ret = bd_print_history_item_main(-1, 0, current_time, 1);
1284                 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1285                         h_count = 0;
1286                         return ret;
1287                 }
1288                 ret = bd_free_history_data_memory();
1289                 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1290                         _WARN("Error while memory free %x", ret);
1291                         return ret;
1292                 }
1293         }
1294
1295         EXIT;
1296         return BATTERY_MONITOR_ERROR_NONE;
1297 }
1298
1299 int bd_store_history_item(history_item_s* nrec)
1300 {
1301         ENTER;
1302         BM_CHECK_INPUT_PARAM(nrec);
1303
1304         if (!h_flag) {
1305                 _ERR("Data Dump in progress");
1306                 return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
1307         }
1308         int ret = BATTERY_MONITOR_ERROR_NONE;
1309         ret = bd_set_history_from_listener(nrec);
1310         if (ret != BATTERY_MONITOR_ERROR_NONE) {
1311                 _ERR("Failed to store history");
1312                 return BATTERY_MONITOR_ERROR_INTERNAL;
1313         }
1314
1315         EXIT;
1316         return BATTERY_MONITOR_ERROR_NONE;
1317 }
1318
1319 static int *dumpsys_handler;
1320
1321 int bd_callback(const int fd, const int argc, char **argv)
1322 {
1323         int ret;
1324         int opt;
1325         bool checkin = false;
1326
1327         /**
1328          * We must reset optind because it isn't initialized
1329          * on subsequent getopt calls
1330          */
1331         optind = 1;
1332         while ((opt = getopt(argc, argv, "c")) != -1) {
1333                 switch (opt) {
1334                 case 'c':
1335                         checkin = true;
1336                         break;
1337                 default:
1338                         break;
1339                 }
1340         }
1341
1342         checkin = true;
1343         ret = bd_print_history_item_main(fd, -1, 0, checkin);
1344         if (ret != BATTERY_MONITOR_ERROR_NONE) {
1345                 _ERR("bd_print_history_item_main failed (%x)", ret);
1346                 return 0;
1347         }
1348
1349         return 0;
1350 }
1351
1352 int bd_initialize_battery_dump()
1353 {
1354         ENTER;
1355
1356         int ret = BATTERY_MONITOR_ERROR_NONE;
1357
1358         h_count = 0; h_flag = true;
1359
1360 #ifdef DUMP_DUMMY
1361         bd_dummy_test_fn();
1362 #else
1363         history_item_s ts;
1364         ret = bd_print_history_item_reset(&ts);
1365         if (ret == BATTERY_MONITOR_ERROR_NONE) {
1366                 ts.cmd_s = CM_CRR_TIME;
1367                 ret = bd_store_history_item(&ts);
1368                 if (ret != BATTERY_MONITOR_ERROR_NONE)
1369                         _WARN("Init Node addition failed");
1370         }
1371 #endif
1372
1373         ret = dumpsys_system_register_dump_cb(bd_callback, "batterystats", (void **)&dumpsys_handler);
1374         if (ret != TIZEN_ERROR_NONE) {
1375                 _ERR("dumpsys_system_register_dump_cb failed (%x)", ret);
1376                 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1377         }
1378
1379         _INFO("Battery Dump Init Successful");
1380
1381         EXIT;
1382         return BATTERY_MONITOR_ERROR_NONE;
1383 }
1384
1385 int bd_deinitialize_battery_dump()
1386 {
1387         ENTER;
1388
1389         int ret;
1390
1391         ret = dumpsys_system_unregister_dump_cb(dumpsys_handler);
1392         if (ret != TIZEN_ERROR_NONE) {
1393                 _WARN("dumpsys_system_unregister_dump_cb failed (%x)", ret);
1394                 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1395         }
1396
1397         ret = bd_free_history_data_memory();
1398         if (ret != BATTERY_MONITOR_ERROR_NONE)
1399                 _WARN("memory free failed (%x)", ret);
1400
1401         _INFO("Battery Dump De-Init Successful");
1402
1403         EXIT;
1404         return BATTERY_MONITOR_ERROR_NONE;
1405 }