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