2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dumpsys-system.h>
22 #include <glib/gprintf.h>
25 #include "bd_history_item.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"
34 //#define DUMP_DUMMY 1
36 #include "bd_history_item_dummy.h"
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;
51 static int bd_print_pool_entry(int fd, int idx, history_tag_s *pool_info)
55 BM_CHECK_INPUT_PARAM(pool_info);
56 _DBG("Value from history data %d", idx);
58 GString *dump_p = g_string_sized_new(30);
59 BM_CHECK_MEM_ALLOC(dump_p, {});
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, "\"");
69 if (pool_info->string_info != NULL) {
70 gchar *temp = g_strdup(pool_info->string_info->str);
71 g_string_append(dump_p, temp);
75 g_string_append(dump_p, "\"");
76 g_string_append(dump_p, "\n");
78 _DBG("%s", dump_p->str);
80 if (write(fd, dump_p->str, dump_p->len) < 0)
83 g_string_free(dump_p, TRUE);
86 return BATTERY_MONITOR_ERROR_NONE;
89 static int bd_get_pool_entry_from_dump(int fd)
93 GString *dump_p = g_string_sized_new(30);
94 BM_CHECK_MEM_ALLOC(dump_p, {});
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");
103 _DBG("%s", dump_p->str);
105 if (write(fd, dump_p->str, dump_p->len) < 0)
106 _WARN("write error");
107 g_string_free(dump_p, TRUE);
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);
118 if (ret != BATTERY_MONITOR_ERROR_NONE)
119 return BATTERY_MONITOR_ERROR_INTERNAL;
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);
127 if (ret != BATTERY_MONITOR_ERROR_NONE)
128 return BATTERY_MONITOR_ERROR_INTERNAL;
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);
136 if (ret != BATTERY_MONITOR_ERROR_NONE)
137 return BATTERY_MONITOR_ERROR_INTERNAL;
143 return BATTERY_MONITOR_ERROR_NONE;
146 static int bd_get_and_print_battery_usage_from_dump(int fd, long int duration)
149 int ret = BATTERY_MONITOR_ERROR_NONE;
150 // Application usage;
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
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;
162 dump_p = g_string_sized_new(30);
163 BM_CHECK_MEM_ALLOC(dump_p, {});
164 g_string_append(dump_p, TIZEN_DUMP_VERSION);
166 g_string_append_printf(dump_p, ",0,l,pws,%d,0,0,0", battery_capacity);
167 g_string_append(dump_p, "\n");
168 _DBG("%s", dump_p->str);
169 if (write(fd, dump_p->str, dump_p->len) < 0)
170 _WARN("write error");
171 g_string_free(dump_p, TRUE);
173 g_hash_table_iter_init(&iter, app_map);
174 while (g_hash_table_iter_next(&iter, &key, &value)) {
175 uid_n = (int *)key; appid = (char *)value;
176 _DBG("%d %s", *uid_n, appid); usageMAh = 0;
177 // Currently giving app usage of last one week;
178 // Modify it wrt to time
179 appid_usage_s *app_use = bm_server_query_app_usage_by_appid(appid, 1, &ret);
180 if (app_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
181 _ERR("For appid %s", appid);
184 usageMAh = (app_use->rId_ble + app_use->rId_wifi + app_use->rId_cpu +
185 app_use->rId_display + app_use->rId_device_network +
186 app_use->rId_gps + app_use->rId_hrm);
187 // usage stored as mAs changed to mAh
188 usageMAh = (usageMAh/3600);
189 dump_p = g_string_sized_new(30);
190 BM_CHECK_MEM_ALLOC(dump_p, { BM_FREE(app_use); });
191 g_string_append(dump_p, TIZEN_DUMP_VERSION);
192 g_string_append_printf(dump_p, ",%d,l,pwi,uid,", *uid_n);
193 g_string_append_printf(dump_p, "%0.2f", usageMAh);
194 g_string_append(dump_p, "\n");
195 bm_appid_usage_free(app_use);
196 _DBG("%s", dump_p->str);
197 if (write(fd, dump_p->str, dump_p->len) < 0)
198 _WARN("write error");
199 g_string_free(dump_p, TRUE);
202 const char *rid_str = NULL;
203 for (int i = 0; i < 6; i++) {
205 rid_str = bm_get_resource_id_string(i);
206 if (g_strcmp0(rid_str, "UNKNOWN RESOURCE-ID") == 0) {
207 _ERR("invalid resource-id");
208 return BATTERY_MONITOR_ERROR_INTERNAL;
210 resourceid_usage_s *res_use =
211 bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
212 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
213 _ERR("For resourceid %s", rid_str);
216 dump_p = g_string_sized_new(30);
217 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(res_use); });
218 usage = res_use->usage;
219 // usage stored as mAs changed to mAh
220 usageMAh = (float)usage/3600;
221 g_string_append(dump_p, TIZEN_DUMP_VERSION);
224 g_string_append(dump_p, ",0,l,pwi,blue,");
227 g_string_append(dump_p, ",0,l,pwi,wifi,");
230 g_string_append(dump_p, ",0,l,pwi,phone,");
233 g_string_append(dump_p, ",0,l,pwi,scrn,");
236 g_string_append(dump_p, ",0,l,pwi,cell,");
239 g_string_append(dump_p, ",0,l,pwi,unacc,");
242 g_string_append_printf(dump_p, "%0.2f", usageMAh);
243 g_string_append(dump_p, "\n");
244 bm_resourceid_usage_free(res_use);
245 _DBG("%s", dump_p->str);
246 if (write(fd, dump_p->str, dump_p->len) < 0)
247 _WARN("write error");
248 g_string_free(dump_p, TRUE);
255 static int bd_get_and_print_gbr_stat_entry_from_dump(int fd, long int duration)
258 // Dump brightness Data
259 bd_gl_sbr_stat_s *gbr_stat = (bd_gl_sbr_stat_s *)calloc(1, sizeof(bd_gl_sbr_stat_s));
260 BM_CHECK_MEM_ALLOC(gbr_stat, {});
262 int ret = BATTERY_MONITOR_ERROR_NONE;
263 ret = bm_server_battery_dump_query_from_db(gbr_stat, 3, duration);
264 if (ret != BATTERY_MONITOR_ERROR_NONE) {
265 _ERR("Unable to fetch data from DB");
270 GString *dump_p = g_string_sized_new(30);
271 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gbr_stat); });
272 g_string_append(dump_p, TIZEN_DUMP_VERSION);
273 g_string_append(dump_p, ",0,l,");
274 g_string_append(dump_p, "br,");
275 g_string_append_printf(dump_p, "%ld,", gbr_stat->dark); //OFF
276 g_string_append_printf(dump_p, "%ld,", gbr_stat->dim); // All are time values
277 g_string_append_printf(dump_p, "%ld,", gbr_stat->medium);
278 g_string_append_printf(dump_p, "%ld,", gbr_stat->light); //DEF: 0
279 g_string_append_printf(dump_p, "%ld", gbr_stat->bright);
280 g_string_append(dump_p, "\n");
281 _DBG("%s", dump_p->str);//write
282 if (write(fd, dump_p->str, dump_p->len) < 0)
283 _WARN("write error");
284 g_string_free(dump_p, TRUE);
291 static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration)
295 int ret = BATTERY_MONITOR_ERROR_NONE;
298 bd_gl_ntw_stat_s *gn_stat = (bd_gl_ntw_stat_s *)calloc(1, sizeof(bd_gl_ntw_stat_s));
299 BM_CHECK_MEM_ALLOC(gn_stat, {});
301 ret = bm_server_battery_dump_query_from_db(gn_stat, 2, duration);
302 if (ret != BATTERY_MONITOR_ERROR_NONE) {
303 _ERR("Unable to fetch data from DB");
308 GString *dump_p = g_string_sized_new(30);
309 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gn_stat); });
311 g_string_append(dump_p, TIZEN_DUMP_VERSION);
312 g_string_append(dump_p, ",0,l,");
313 g_string_append(dump_p, "gn,");
314 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_rx_byt);
315 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_tx_byt);
316 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_rx_byt);
317 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_tx_byt);
318 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_rx_pkt);
319 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_tx_pkt);
320 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_rx_pkt);
321 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_tx_pkt);
322 g_string_append_printf(dump_p, "%d,", 0); //add this to struct as well
323 g_string_append_printf(dump_p, "%d", 0);
324 g_string_append(dump_p, "\n");
326 _DBG("%s", dump_p->str);//write
328 if (write(fd, dump_p->str, dump_p->len) < 0)
329 _WARN("write error");
331 g_string_free(dump_p, TRUE);
338 static int bd_get_and_print_gwf_stat_entry_from_dump(int fd, long int duration)
342 bd_gl_wifi_stat_s *gwfl_stat = (bd_gl_wifi_stat_s *)calloc(1, sizeof(bd_gl_wifi_stat_s));
343 BM_CHECK_MEM_ALLOC(gwfl_stat, {});
345 int ret = BATTERY_MONITOR_ERROR_NONE;
346 ret = bm_server_battery_dump_query_from_db(gwfl_stat, 1, duration);
347 if (ret != BATTERY_MONITOR_ERROR_NONE) {
348 _ERR("Unable to fetch data from DB");
353 GString *dump_p = g_string_sized_new(30);
354 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
355 g_string_append(dump_p, TIZEN_DUMP_VERSION);
356 g_string_append(dump_p, ",0,l,");
357 g_string_append(dump_p, "gwfl,");
358 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_on_time);
359 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_conn_time);
360 g_string_append(dump_p, "0,0,0");//legacy
362 g_string_append(dump_p, "\n");
363 _DBG("%s", dump_p->str);//write
364 if (write(fd, dump_p->str, dump_p->len) < 0)
365 _WARN("write error");
366 g_string_free(dump_p, TRUE);
368 dump_p = g_string_sized_new(30);
369 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
370 g_string_append(dump_p, TIZEN_DUMP_VERSION);
371 g_string_append(dump_p, ",0,l,");
372 g_string_append(dump_p, "gwfcd,");
374 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_idle_time);
375 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_rx_time);
376 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_pwi_val);
377 g_string_append_printf(dump_p, "%ld", gwfl_stat->wifi_tx_time);
378 g_string_append(dump_p, "\n");
379 _DBG("%s", dump_p->str);//write
380 if (write(fd, dump_p->str, dump_p->len) < 0)
381 _WARN("write error");
382 g_string_free(dump_p, TRUE);
389 static int bd_get_and_print_gble_stat_entry_from_dump(int fd, long int duration)
393 bd_gl_ble_stat_s *gble_stat = (bd_gl_ble_stat_s *)calloc(1, sizeof(bd_gl_ble_stat_s));
394 BM_CHECK_MEM_ALLOC(gble_stat, {});
396 int ret = BATTERY_MONITOR_ERROR_NONE;
397 ret = bm_server_battery_dump_query_from_db(gble_stat, 0, duration);
398 if (ret != BATTERY_MONITOR_ERROR_NONE) {
399 _ERR("Unable to fetch data from DB");
404 GString *dump_p = g_string_sized_new(30);
405 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gble_stat); });
406 g_string_append(dump_p, TIZEN_DUMP_VERSION);
407 g_string_append(dump_p, ",0,l,");
408 g_string_append(dump_p, "gble,");
409 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_idle_time);
410 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_rx_time);
411 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_tx_time);
412 g_string_append_printf(dump_p, "%ld", gble_stat->ble_pwi_val);
413 g_string_append(dump_p, "\n");
415 _DBG("%s", dump_p->str);//write
416 if (write(fd, dump_p->str, dump_p->len) < 0)
417 _WARN("write error");
418 g_string_free(dump_p, TRUE);
425 static int bd_get_and_print_gst_stat_entry_from_dump(int fd, long int duration)
429 int ret = BATTERY_MONITOR_ERROR_NONE;
431 bd_gl_sgt_stat_s *gst_stat = (bd_gl_sgt_stat_s *)calloc(1, sizeof(bd_gl_sgt_stat_s));
432 BM_CHECK_MEM_ALLOC(gst_stat, {});
434 ret = bm_server_battery_dump_query_from_db(gst_stat, 4, duration);
435 if (ret != BATTERY_MONITOR_ERROR_NONE) {
436 _ERR("Unable to fetch data from DB");
441 GString *dump_p = g_string_sized_new(30);
442 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
444 g_string_append(dump_p, TIZEN_DUMP_VERSION);
445 g_string_append(dump_p, ",0,l,");
446 g_string_append(dump_p, "sgt,");
447 g_string_append_printf(dump_p, "%ld,", gst_stat->none_time);
448 g_string_append_printf(dump_p, "%ld,", gst_stat->poor_time);
449 g_string_append_printf(dump_p, "%ld,", gst_stat->med_time);
450 g_string_append_printf(dump_p, "%ld,", gst_stat->good_time);
451 g_string_append_printf(dump_p, "%ld", gst_stat->grt_time);
452 g_string_append(dump_p, "\n");
454 _DBG("%s", dump_p->str);//write
456 if (write(fd, dump_p->str, dump_p->len) < 0)
457 _WARN("write error");
458 g_string_free(dump_p, TRUE);
460 dump_p = g_string_sized_new(30);
461 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
463 g_string_append(dump_p, TIZEN_DUMP_VERSION);
464 g_string_append(dump_p, ",0,l,");
465 g_string_append(dump_p, "sst,");
466 g_string_append_printf(dump_p, "%ld", gst_stat->scan_time);
468 g_string_append(dump_p, "\n");
469 _DBG("%s", dump_p->str);//write
470 if (write(fd, dump_p->str, dump_p->len) < 0)
471 _WARN("write error");
473 g_string_free(dump_p, TRUE);
480 static int bd_get_and_print_gwst_stat_entry_from_dump(int fd, long int duration)
483 bd_gl_wst_stat_s *gwst_stat = (bd_gl_wst_stat_s *)calloc(1, sizeof(bd_gl_wst_stat_s));
484 BM_CHECK_MEM_ALLOC(gwst_stat, {});
486 int ret = BATTERY_MONITOR_ERROR_NONE;
487 ret = bm_server_battery_dump_query_from_db(gwst_stat, 5, duration);
488 if (ret != BATTERY_MONITOR_ERROR_NONE) {
489 _ERR("Unable to fetch data from DB");
494 GString *dump_p = g_string_sized_new(30);
495 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwst_stat); });
496 g_string_append(dump_p, TIZEN_DUMP_VERSION);
497 g_string_append(dump_p, ",0,l,");
498 g_string_append(dump_p, "wsgt,");
499 // g_string_append_printf(dump_p, "%ld,", gwst_stat->wscan_time);
500 g_string_append_printf(dump_p, "%ld,", gwst_stat->wnone_time);
501 g_string_append_printf(dump_p, "%ld,", gwst_stat->wpoor_time);
502 g_string_append_printf(dump_p, "%ld,", gwst_stat->wmed_time);
503 g_string_append_printf(dump_p, "%ld,", gwst_stat->wgood_time);
504 g_string_append_printf(dump_p, "%ld", gwst_stat->wgrt_time);
505 g_string_append(dump_p, "\n");
506 _DBG("%s", dump_p->str);//write
507 if (write(fd, dump_p->str, dump_p->len) < 0)
508 _WARN("write error");
509 g_string_free(dump_p, TRUE);
516 static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration)
520 int ret = BATTERY_MONITOR_ERROR_NONE;
521 const char *rid_str = NULL; int usage = 0;
522 rid_str = bm_get_resource_id_string(2);
523 resourceid_usage_s *res_use =
524 bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
525 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
526 _ERR("For resourceid %s", rid_str);
529 usage = res_use->usage;
530 float usagemah = (float)usage/3600;
531 bd_gl_cpu_stat_s *gcpu_stat = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
532 BM_CHECK_MEM_ALLOC(gcpu_stat, {BM_FREE(res_use); });
533 ret = bm_server_battery_dump_query_from_db(gcpu_stat, 7, duration);
534 if (ret != BATTERY_MONITOR_ERROR_NONE) {
535 _ERR("Unable to fetch data from DB");
541 GString *dump_p = g_string_sized_new(30);
542 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gcpu_stat); BM_FREE(res_use);});
543 g_string_append(dump_p, TIZEN_DUMP_VERSION);
544 g_string_append(dump_p, ",0,l,");
545 g_string_append(dump_p, "cpu,");
546 g_string_append_printf(dump_p, "%ld,", gcpu_stat->usr_time);
547 g_string_append_printf(dump_p, "%ld,", gcpu_stat->sys_time);
548 g_string_append_printf(dump_p, "%0.2f", usagemah);
549 g_string_append(dump_p, "\n");
550 bm_resourceid_usage_free(res_use);
551 _DBG("%s", dump_p->str);
552 if (write(fd, dump_p->str, dump_p->len) < 0)
553 _WARN("write error");
562 static int bd_get_and_print_uid_info_entry_from_dump(int fd)
566 int ret = BATTERY_MONITOR_ERROR_NONE;
567 GSList *list = bm_server_query_appid_map(&ret);
569 _ERR("NO LIST DATA FOUND");
572 app_map = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
573 GSList *iter = NULL; GString *dump_p = NULL;
574 dump_p = g_string_sized_new(30);
575 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
576 g_string_append(dump_p, TIZEN_DUMP_VERSION);
577 g_string_append(dump_p, ",0,i,uid,1000,tizen\n");
578 if (write(fd, dump_p->str, dump_p->len) < 0)
579 _WARN("write error");
580 g_string_free(dump_p, TRUE);
582 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
583 dump_p = g_string_sized_new(30);
584 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
585 g_string_append(dump_p, TIZEN_DUMP_VERSION);
586 g_string_append(dump_p, ",0");
587 g_string_append(dump_p, ",i");
588 g_string_append(dump_p, ",uid,");
589 appid_map_s *amap_data = (appid_map_s *)iter->data;
590 g_string_append_printf(dump_p, "%d,", (amap_data->id+10000));
591 gchar *temp_str = g_strdup(amap_data->AppId);
592 g_string_append(dump_p, amap_data->AppId);
593 g_string_append(dump_p, "\n");
594 _DBG("%s", dump_p->str);//write
595 if (write(fd, dump_p->str, dump_p->len) < 0)
596 _WARN("write error");
597 int *temp_id = (int *)malloc(4);
598 *temp_id = (amap_data->id + 10000);
599 _DBG("Insert id %d, str %s, res %d", *temp_id, temp_str,
600 g_hash_table_insert(app_map, temp_id, temp_str));
601 g_string_free(dump_p, TRUE);
603 bm_set_free_gslist_appid_map(list);
609 static int bd_get_history_detail_from_dump(int index, history_item_s* dump_node)
612 BM_CHECK_INPUT_PARAM(dump_node);
614 if (h_count >= HISTORY_SIZE_MAX || index > h_count) {
615 _ERR("Some Error occured with global index");
616 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
619 _DBG("Value of index %d", index);
620 _DBG("Value of h_count %d", h_count);
622 int dvar1 = 0, dvar2 = 0;
623 dvar1 = history_data[index].dump_1;
624 dvar2 = history_data[index].dump_2;
626 _DBG("Value of encoded value 1 %d", dvar1);
627 _DBG("Value of encoded value 2 %d", dvar2);
629 dump_node->cmd_s = ((dvar1) & 0xff);
630 _DBG("cmd_s %d", dump_node->cmd_s);
632 dump_node->battery_level = ((dvar1>>8) & 0xff);
633 _DBG("battery_level %d", dump_node->battery_level);
635 dump_node->battery_status = ((dvar1>>16) & 0xf);
636 _DBG("battery_status %d", dump_node->battery_status);
638 dump_node->battery_health = ((dvar1>>20) & 0xf);
639 _DBG("battery_health %d", dump_node->battery_health);
641 dump_node->battery_plugtype = ((dvar1>>24) & 0xf);
642 _DBG("battery_plugtype %d", dump_node->battery_plugtype);
644 dump_node->battery_temperature = ((dvar2) & 0xffff);
645 _DBG("battery_temperature %d", dump_node->battery_temperature);
647 dump_node->battery_voltage = ((dvar2>>16) & 0xffff);
648 _DBG("battery_voltage %d", dump_node->battery_voltage);
650 dump_node->battery_charge = history_data[index].battery_chargeUAh;
651 _DBG("battery_charge %d", dump_node->battery_charge);
653 dump_node->event_code = history_data[index].event_code;
654 _DBG("eventcode %d", dump_node->event_code);
656 dump_node->state_1 = history_data[index].state_1;
657 _DBG("state_1 %d", dump_node->state_1);
659 dump_node->state_2 = history_data[index].state_2;
660 _DBG("state_2 %d", dump_node->state_2);
662 dump_node->event_tag = history_data[index].event_tag;
663 if (dump_node->event_tag == NULL)
664 _DBG("Event_Tag_NULL");
666 _DBG("%d %d %s", dump_node->event_tag->uid, dump_node->event_tag->sp_idx,
667 dump_node->event_tag->string_info->str);
668 dump_node->wakelock_tag = history_data[index].wakelock_tag;
669 if (dump_node->wakelock_tag == NULL)
670 _DBG("Wakelock_Tag_NULL");
671 dump_node->wakereason_tag = history_data[index].wakereason_tag;
673 if (dump_node->wakereason_tag == NULL)
674 _DBG("Wakereason_Tag_NULL");
676 dump_node->time_s = history_data[index].time_s;
677 _DBG("TIME_S is %lld", dump_node->time_s);
679 dump_node->time_current = history_data[index].time_current;
680 _DBG("C TIME is %lld", dump_node->time_current);
682 dump_node->usage_type = history_data[index].usage_type;
683 _DBG("Usage type is %d", dump_node->usage_type);
685 dump_node->usage = history_data[index].usage;
686 _DBG("Usage is %d", dump_node->usage);
689 return BATTERY_MONITOR_ERROR_NONE;
692 int bd_print_history_item_reset(history_item_s* old_state)
695 BM_CHECK_INPUT_PARAM(old_state);
697 old_state->time_s = -1;
698 old_state->state_1 = 0;
699 old_state->state_2 = 0;
700 old_state->usage_type = 0;
701 old_state->usage = 0;
702 old_state->battery_level = -1;
703 old_state->battery_status = -1;
704 old_state->battery_health = -1;
705 old_state->battery_plugtype = -1;
706 old_state->battery_temperature = -1;
707 old_state->battery_voltage = -1;
708 old_state->battery_charge = -1;
709 old_state->event_code = 0;
710 old_state->event_tag = NULL;
711 old_state->wakelock_tag = NULL;
712 old_state->wakereason_tag = NULL;
715 return BATTERY_MONITOR_ERROR_NONE;
718 int bd_get_base_time_from_dump(long long *base_time)
722 *base_time = history_data[0].time_s;
725 return BATTERY_MONITOR_ERROR_NONE;
728 int bd_print_history_item_main(int fd, int num_h_items, long long base_time, bool chkin)
734 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
737 if (h_count >= HISTORY_SIZE_MAX) {
738 _ERR("Some Error occured with global index");
739 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
742 int ret = BATTERY_MONITOR_ERROR_NONE;
743 h_flag = false; //Lock
745 history_item_s new_state;
746 history_item_s old_state;
748 ret = bd_print_history_item_reset(&old_state);
749 if (ret != BATTERY_MONITOR_ERROR_NONE) {
750 _ERR("Old State reset failed");
754 ret = bd_get_pool_entry_from_dump(fd);
755 if (ret != BATTERY_MONITOR_ERROR_NONE)
756 _ERR("S pool data dump failed");
758 ret = bd_get_base_time_from_dump(&base_time);
759 if (ret != BATTERY_MONITOR_ERROR_NONE)
760 _ERR("Base time error");
762 int log_count = h_count; GString *dump_p = NULL;
763 for (int i = 0; i < log_count; i++) {
764 ret = bd_get_history_detail_from_dump(i, &new_state);
765 if (ret != BATTERY_MONITOR_ERROR_NONE) {
766 _ERR("Decode Failed");
769 dump_p = g_string_sized_new(30);
770 BM_CHECK_MEM_ALLOC(dump_p, {});
772 g_string_append(dump_p, TIZEN_DUMP_VERSION);
773 g_string_append_c(dump_p, ',');
774 _DBG("value %s", dump_p->str);
775 g_string_append(dump_p, DUMP_DATA_TAG);
776 g_string_append_c(dump_p, ',');
777 _DBG("value %s", dump_p->str);
778 if (old_state.time_s < 0) {
779 g_string_append_printf(dump_p, "%lld",
780 (new_state.time_s - base_time));
781 _DBG("value %s", dump_p->str);
783 g_string_append_printf(dump_p, "%lld",
784 (new_state.time_s - old_state.time_s));
785 _DBG("value %s", dump_p->str);
787 old_state.time_s = new_state.time_s;
788 _DBG("Value old_state.time_s %lld", old_state.time_s);
789 } else { //: Let's skip the NC mode in initial version
791 if (new_state.cmd_s == CM_ST) {
793 g_string_append_c(dump_p, ':');
794 _DBG("value %s", dump_p->str);
798 g_string_append(dump_p, "START\n");
799 _DBG("value %s", dump_p->str);
800 bd_print_history_item_reset(&old_state);
801 } else if (new_state.cmd_s == CM_CRR_TIME || new_state.cmd_s == CM_RST) {
803 g_string_append_c(dump_p, ':');
804 _DBG("value %s", dump_p->str);
806 if (new_state.cmd_s == CM_RST) {
807 g_string_append(dump_p, "RESET:");
808 _DBG("value %s", dump_p->str);
809 bd_print_history_item_reset(&old_state);
811 g_string_append(dump_p, "TIME:");
812 _DBG("value %s", dump_p->str);
814 g_string_append_printf(dump_p, "%lld", new_state.time_current);
815 _DBG("value %s", dump_p->str);
816 g_string_append(dump_p, "\n");
817 _DBG("value %s", dump_p->str);
821 } else if (new_state.cmd_s == CM_SHTDN) {
823 g_string_append_c(dump_p, ':');
824 _DBG("value %s", dump_p->str);
828 g_string_append(dump_p, "SHUTDOWN\n");
829 _DBG("value %s", dump_p->str);
830 } else if (new_state.cmd_s == CM_OVRFL) {
832 g_string_append_c(dump_p, ':');
833 _DBG("value %s", dump_p->str);
835 g_string_append(dump_p, "*OVERFLOW*\n");
836 _DBG("value %s", dump_p->str);
837 } else if (new_state.cmd_s == CM_USAGE) {
838 for (int i = 0; i < 32; i++) {
839 if (new_state.usage_type == (1 << i)) {
840 g_string_append(dump_p, ",");
841 g_string_append_printf(dump_p, "%s", usage_map[i]);
842 g_string_append(dump_p, "=");
843 g_string_append_printf(dump_p, "%d", new_state.usage);
844 g_string_append(dump_p, "\n");
845 _DBG("value %d", new_state.usage);
852 if (old_state.battery_level != new_state.battery_level) {
853 old_state.battery_level = new_state.battery_level;
854 g_string_append(dump_p, ",Bl=");
855 _DBG("value %s", dump_p->str);
856 g_string_append_printf(dump_p, "%d",
857 new_state.battery_level);
858 _DBG("value %s", dump_p->str);
861 if (old_state.battery_status != new_state.battery_status) {
862 old_state.battery_status = new_state.battery_status;
863 g_string_append(dump_p, ",Bs="); //:for NC
864 _DBG("value %s", dump_p->str);
865 switch (old_state.battery_status) {
867 g_string_append_c(dump_p, '?');
870 g_string_append_c(dump_p, 'c');
873 g_string_append_c(dump_p, 'd');
876 g_string_append_c(dump_p, 'n');
879 g_string_append_c(dump_p, 'f');
882 g_string_append_c(dump_p, '?');
883 _DBG("value %s", dump_p->str);
887 if (old_state.battery_health != new_state.battery_health) {
888 old_state.battery_health = new_state.battery_health;
889 g_string_append(dump_p, ",Bh="); //:for NC
890 _DBG("value %s", dump_p->str);
891 switch (old_state.battery_health) {
893 g_string_append_c(dump_p, 'g');
896 g_string_append_c(dump_p, '?');
899 g_string_append_c(dump_p, 'h');
902 g_string_append_c(dump_p, 'd');
905 g_string_append_c(dump_p, 'v');
908 g_string_append_c(dump_p, 'f');
911 g_string_append_c(dump_p, 'c');
914 g_string_append_c(dump_p, '?');
915 _DBG("value %s", dump_p->str);
919 if (old_state.battery_plugtype != new_state.battery_plugtype) {
920 old_state.battery_plugtype = new_state.battery_plugtype;
921 g_string_append(dump_p, ",Bp="); //:for NC
922 _DBG("value %s", dump_p->str);
923 switch (old_state.battery_plugtype) {
925 g_string_append_c(dump_p, 'n');
928 g_string_append_c(dump_p, 'a');
931 g_string_append_c(dump_p, 'u');
934 g_string_append_c(dump_p, 'w');
937 g_string_append_c(dump_p, 'n');
938 _DBG("value %s", dump_p->str);
942 if (old_state.battery_temperature != new_state.battery_temperature) {
943 old_state.battery_temperature = new_state.battery_temperature;
944 g_string_append(dump_p, ",Bt="); //:for NC
945 g_string_append_printf(dump_p, "%d",
946 new_state.battery_temperature);
947 _DBG("value %s", dump_p->str);
949 if (old_state.battery_voltage != new_state.battery_voltage) {
950 old_state.battery_voltage = new_state.battery_voltage;
951 g_string_append(dump_p, ",Bv="); //:for NC
952 _DBG("value %s", dump_p->str);
953 g_string_append_printf(dump_p, "%d", new_state.battery_voltage);
954 _DBG("value %s", dump_p->str);
956 if (old_state.battery_charge != new_state.battery_charge) {
957 old_state.battery_charge = new_state.battery_charge;
958 g_string_append(dump_p, ",Bcc=");
959 _DBG("value %s", dump_p->str);
961 g_string_append_printf(dump_p, "%d", (new_state.battery_charge/1000));
962 _DBG("value %s", dump_p->str);
964 int change = (old_state.state_1 ^ new_state.state_1);
966 bool wake_fl = false; int k = 0;
967 if ((change & ST1_BRIGHTNESS_MASK) != 0) {
968 k = (new_state.state_1 & ST1_BRIGHTNESS_MASK);
969 g_string_append(dump_p, ",Sb=");
970 k = k >> ST1_BRIGHTNESS_SHIFT;
972 g_string_append_printf(dump_p, "%d", k);
975 if ((change & ST1_PHONE_SIGNAL_STRENGTH_MASK) != 0) {
976 k = (new_state.state_1 & ST1_PHONE_SIGNAL_STRENGTH_MASK);
977 g_string_append(dump_p, ",Pss=");
978 k = k >> ST1_PHONE_SIGNAL_STRENGTH_SHIFT;
980 g_string_append_printf(dump_p, "%d", k);
983 int shifts = (1 << 14);
984 for (int i = 14; i < 32; i++) {
985 if ((change & shifts) != 0) {
986 g_string_append_c(dump_p, ',');
987 //Considering only -ve shift cases
990 if ((new_state.state_1 & (1 << i)) != 0)
991 g_string_append_c(dump_p, '+');
993 g_string_append_c(dump_p, '-');
994 gchar *temp = g_strdup(state1_map[i]);
995 g_string_append(dump_p, temp);
997 //_DBG("value %s", dump_p->str);
998 if ((1 << i) == ST1_WAKE_LOCK_FLAG &&
999 new_state.wakelock_tag != NULL) {
1001 g_string_append_c(dump_p, '=');
1002 //_DBG("value %s", dump_p->str);
1004 g_string_append_printf(dump_p,
1005 "%d", new_state.wakelock_tag->sp_idx);
1009 //_DBG("value %s", dump_p->str);
1012 //With non-negative shift
1015 shifts = (shifts << 1);
1017 if (!wake_fl && new_state.wakelock_tag != NULL) {
1018 g_string_append(dump_p, ",w=");
1020 g_string_append_printf(dump_p, "%d",
1021 new_state.wakelock_tag->sp_idx);
1022 _DBG("value %s", dump_p->str);
1027 change = (old_state.state_2 ^ new_state.state_2);
1030 for (int i = 0; i < 32; i++) {
1031 if ((change & shifts) != 0) {
1032 g_string_append_c(dump_p, ',');
1033 //_DBG("value %s", dump_p->str);
1036 if ((new_state.state_2 & (1 << i) & change) != 0)
1037 g_string_append_c(dump_p, '+');
1039 g_string_append_c(dump_p, '-');
1040 //_DBG("value %s", dump_p->str);
1041 gchar *temp = g_strdup(state2_map[i]);
1042 g_string_append(dump_p, temp);
1044 _DBG("value %s", dump_p->str);
1047 shifts = (shifts << 1);
1050 if (new_state.wakereason_tag != NULL) {
1052 g_string_append(dump_p, ",wr=");
1053 _DBG("value %s", dump_p->str);
1054 g_string_append_printf(dump_p, "%d",
1055 new_state.wakereason_tag->sp_idx);
1056 _DBG("value %s", dump_p->str);
1061 if (new_state.event_code != ET_NONE) {
1062 g_string_append_c(dump_p, ',');
1063 _DBG("value %s", dump_p->str);
1064 if ((new_state.event_code & ET_FLAG_START) != 0)
1065 g_string_append_c(dump_p, '+');
1066 else if ((new_state.event_code & ET_FLAG_FINISH) != 0)
1067 g_string_append_c(dump_p, '-');
1068 _DBG("value %s", dump_p->str);
1069 int k = new_state.event_code & ~(ET_FLAG_START | ET_FLAG_FINISH);
1070 if (k >= 0 && k < 32) {
1071 gchar* temp = g_strdup(event_map[k]);
1072 g_string_append(dump_p, temp);
1073 _DBG("value %s", dump_p->str);
1076 g_string_append(dump_p, "Ev");
1077 g_string_append_printf(dump_p, "%d", k);
1078 _DBG("value %s", dump_p->str);
1080 g_string_append_c(dump_p, '=');
1081 _DBG("value %s", dump_p->str);
1083 g_string_append_printf(dump_p, "%d",
1084 new_state.event_tag->sp_idx);
1090 g_string_append(dump_p, "\n");
1092 old_state.state_1 = new_state.state_1;
1093 old_state.state_2 = new_state.state_2;
1095 _DBG("State assigned");
1096 _DBG("%s", dump_p->str);
1097 if (write(fd, dump_p->str, dump_p->len) < 0)
1098 _WARN("write error");
1099 g_string_free(dump_p, TRUE);
1102 // Dump the elapsed time from last event
1103 // It should be dumped without storing
1104 GString *dump_tmp_p = g_string_sized_new(30);
1106 gettimeofday(&tv, NULL);
1108 g_string_append(dump_tmp_p, TIZEN_DUMP_VERSION);
1109 g_string_append_c(dump_tmp_p, ',');
1110 g_string_append(dump_tmp_p, DUMP_DATA_TAG);
1111 g_string_append_c(dump_tmp_p, ',');
1112 g_string_append_printf(dump_tmp_p, "%lld",
1113 ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000) - old_state.time_s);
1114 g_string_append(dump_tmp_p, "\n");
1116 if (write(fd, dump_tmp_p->str, dump_tmp_p->len) < 0)
1117 _WARN("write error");
1118 g_free(dump_tmp_p->str);
1120 //Dump i & l values after this
1121 ret = bd_get_and_print_uid_info_entry_from_dump(fd);
1122 if (ret != BATTERY_MONITOR_ERROR_NONE)
1123 _ERR("UID data dump failed");
1124 // Get Last charge time to dump "l" field
1125 // for the time being dumping all data
1126 long int duration = 0; //Time of last charge
1127 ret = bd_get_and_print_gn_stat_entry_from_dump(fd, duration);
1128 if (ret != BATTERY_MONITOR_ERROR_NONE)
1129 _ERR("GN dump failed");
1130 ret = bd_get_and_print_gwf_stat_entry_from_dump(fd, duration);
1131 if (ret != BATTERY_MONITOR_ERROR_NONE)
1132 _ERR("GWF data dump failed");
1133 ret = bd_get_and_print_gble_stat_entry_from_dump(fd, duration);
1134 if (ret != BATTERY_MONITOR_ERROR_NONE)
1135 _ERR("GBL data dump failed");
1136 ret = bd_get_and_print_gbr_stat_entry_from_dump(fd, duration);
1137 if (ret != BATTERY_MONITOR_ERROR_NONE)
1138 _ERR("GBR data dump failed");
1139 ret = bd_get_and_print_gst_stat_entry_from_dump(fd, duration);
1140 if (ret != BATTERY_MONITOR_ERROR_NONE)
1141 _ERR("GST data dump failed");
1142 ret = bd_get_and_print_gwst_stat_entry_from_dump(fd, duration);
1143 if (ret != BATTERY_MONITOR_ERROR_NONE)
1144 _ERR("GWST data dump failed");
1145 ret = bd_get_and_print_gcpu_stat_entry_from_dump(fd, duration);
1146 _ERR("GCPU data dump failed");
1147 // Battery Usage Dump
1148 // Add usage for each of the resource
1149 // and each of the application
1150 ret = bd_get_and_print_battery_usage_from_dump(fd, duration);
1151 if (ret != BATTERY_MONITOR_ERROR_NONE)
1152 _ERR("Battery Usage failed");
1154 _DBG("DUMP COMPLETED/DELETE HASH TABLE");
1156 g_hash_table_destroy(app_map);
1159 return BATTERY_MONITOR_ERROR_NONE;
1162 static int bd_dump_data_item_reset(dump_data_s* old_state)
1165 BM_CHECK_INPUT_PARAM(old_state);
1167 old_state->time_s = -1;
1168 old_state->time_current = -1;
1169 old_state->dump_1 = 0;
1170 old_state->dump_2 = 0;
1171 old_state->state_1 = 0;
1172 old_state->state_2 = 0;
1173 old_state->battery_chargeUAh = 0;
1174 old_state->usage_type = 0;
1175 old_state->usage = 0;
1176 old_state->event_code = ET_NONE;
1177 old_state->event_tag = NULL;
1178 old_state->wakelock_tag = NULL;
1179 old_state->wakereason_tag = NULL;
1182 return BATTERY_MONITOR_ERROR_NONE;
1185 static int bd_free_history_data_memory()
1189 int ret = BATTERY_MONITOR_ERROR_NONE;
1190 h_flag = false; //Lock
1191 _DBG("Free History Data Memory");
1192 for (int i = 0; i < h_count; i++) {
1193 if (history_data[i].wakelock_tag != NULL) {
1195 g_string_free(history_data[i].wakelock_tag->string_info, TRUE);
1197 free(history_data[i].wakelock_tag);
1199 if (history_data[i].wakereason_tag != NULL) {
1201 g_string_free(history_data[i].wakereason_tag->string_info, TRUE);
1203 free(history_data[i].wakereason_tag);
1205 if (history_data[i].event_tag != NULL) {
1207 g_string_free(history_data[i].event_tag->string_info, TRUE);
1209 free(history_data[i].event_tag);
1211 ret = bd_dump_data_item_reset(&history_data[i]);
1212 if (ret != BATTERY_MONITOR_ERROR_NONE)
1213 _ERR("Internal error %d while freeing at idx %d", ret , i);
1216 h_count = 0; h_flag = true;
1221 static int bd_set_history_from_listener(history_item_s* nrec)
1224 BM_CHECK_INPUT_PARAM(nrec);
1226 int dvar1 = 0, dvar2 = 0;
1228 gettimeofday(&tv, NULL);
1230 time_t current_time = tv.tv_sec;
1233 history_data[h_count].time_s = nrec->time_s;
1235 history_data[h_count].time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1238 dvar1 = ((nrec->cmd_s) & 0xff) | ((nrec->battery_level << 8) & 0xff00) |
1239 ((nrec->battery_status << 16) & 0xf0000) |
1240 ((nrec->battery_health << 20) & 0xf00000) |
1241 ((nrec->battery_plugtype << 24) & 0xf000000) |
1242 ((nrec->wakelock_tag != NULL) ? 0x10000000 : 0) |
1243 ((nrec->wakereason_tag != NULL) ? 0x20000000 : 0) |
1244 ((nrec->event_code != ET_NONE) ? 0x40000000 : 0);
1246 dvar2 = ((nrec->battery_temperature) & 0xffff) |
1247 ((nrec->battery_voltage << 16) & 0xffff0000);
1249 _DBG("Value of encoded value 1 %d", dvar1);
1250 _DBG("Value of encoded value 2 %d", dvar2);
1252 history_data[h_count].dump_1 = dvar1;
1253 history_data[h_count].dump_2 = dvar2;
1254 history_data[h_count].battery_chargeUAh = nrec->battery_charge;
1256 if (nrec->wakelock_tag != NULL)
1257 history_data[h_count].wakelock_tag = nrec->wakelock_tag;
1258 if (nrec->wakereason_tag != NULL)
1259 history_data[h_count].wakereason_tag = nrec->wakereason_tag;
1260 if (nrec->event_code != ET_NONE) {
1261 if (nrec->event_tag == NULL) {
1262 _ERR("ET is NULL but Event Code is not");
1263 BM_FREE(history_data[h_count].wakelock_tag);
1264 BM_FREE(history_data[h_count].wakereason_tag);
1265 bd_dump_data_item_reset(&history_data[h_count]);
1266 return BATTERY_MONITOR_ERROR_NO_DATA;
1268 history_data[h_count].event_code = nrec->event_code;
1269 history_data[h_count].event_tag = nrec->event_tag;
1271 if (nrec->cmd_s == CM_CRR_TIME || nrec->cmd_s == CM_RST)
1272 history_data[h_count].time_current = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1274 history_data[h_count].state_1 = nrec->state_1;
1275 history_data[h_count].state_2 = nrec->state_2;
1277 history_data[h_count].usage_type = nrec->usage_type;
1278 history_data[h_count].usage = nrec->usage;
1280 nrec->event_tag = NULL; nrec->wakereason_tag = NULL;
1281 nrec->wakelock_tag = NULL;
1282 if (h_count < HISTORY_SIZE_MAX - 1)
1285 // FIXME modify 1st argument (fd)
1286 int ret = bd_print_history_item_main(-1, 0, current_time, 1);
1287 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1291 ret = bd_free_history_data_memory();
1292 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1293 _WARN("Error while memory free %x", ret);
1299 return BATTERY_MONITOR_ERROR_NONE;
1302 int bd_store_history_item(history_item_s* nrec)
1305 BM_CHECK_INPUT_PARAM(nrec);
1308 _ERR("Data Dump in progress");
1309 return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
1311 int ret = BATTERY_MONITOR_ERROR_NONE;
1312 ret = bd_set_history_from_listener(nrec);
1313 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1314 _ERR("Failed to store history");
1315 return BATTERY_MONITOR_ERROR_INTERNAL;
1319 return BATTERY_MONITOR_ERROR_NONE;
1322 static int *dumpsys_handler;
1324 int bd_callback(const int fd, const int argc, char **argv)
1328 bool checkin = false;
1331 * We must reset optind because it isn't initialized
1332 * on subsequent getopt calls
1335 while ((opt = getopt(argc, argv, "c")) != -1) {
1346 ret = bd_print_history_item_main(fd, -1, 0, checkin);
1347 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1348 _ERR("bd_print_history_item_main failed (%x)", ret);
1355 int bd_initialize_battery_dump()
1359 int ret = BATTERY_MONITOR_ERROR_NONE;
1361 h_count = 0; h_flag = true;
1367 ret = bd_print_history_item_reset(&ts);
1368 if (ret == BATTERY_MONITOR_ERROR_NONE) {
1369 ts.cmd_s = CM_CRR_TIME;
1370 ret = bd_store_history_item(&ts);
1371 if (ret != BATTERY_MONITOR_ERROR_NONE)
1372 _WARN("Init Node addition failed");
1376 ret = dumpsys_system_register_dump_cb(bd_callback, "batterystats", (void **)&dumpsys_handler);
1377 if (ret != TIZEN_ERROR_NONE) {
1378 _ERR("dumpsys_system_register_dump_cb failed (%x)", ret);
1379 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1382 _INFO("Battery Dump Init Successful");
1385 return BATTERY_MONITOR_ERROR_NONE;
1388 int bd_deinitialize_battery_dump()
1394 ret = dumpsys_system_unregister_dump_cb(dumpsys_handler);
1395 if (ret != TIZEN_ERROR_NONE) {
1396 _WARN("dumpsys_system_unregister_dump_cb failed (%x)", ret);
1397 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1400 ret = bd_free_history_data_memory();
1401 if (ret != BATTERY_MONITOR_ERROR_NONE)
1402 _WARN("memory free failed (%x)", ret);
1404 _INFO("Battery Dump De-Init Successful");
1407 return BATTERY_MONITOR_ERROR_NONE;