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