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)
519 bd_gl_cpu_stat_s *gcpu_stat = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
520 BM_CHECK_MEM_ALLOC(gcpu_stat, {});
522 int ret = BATTERY_MONITOR_ERROR_NONE;
523 const char *rid_str = NULL; int usage = 0;
524 rid_str = bm_get_resource_id_string(2);
525 resourceid_usage_s *res_use =
526 bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
527 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
528 _ERR("For resourceid %s", rid_str);
529 _DBG("Dump only system stats");
531 usage = res_use->usage;
532 float usagemah = (float)usage/3600;
533 ret = bm_server_battery_dump_query_from_db(gcpu_stat, 7, duration);
534 if (ret != BATTERY_MONITOR_ERROR_NONE) {
535 _ERR("Unable to fetch data from DB");
540 GString *dump_p = g_string_sized_new(30);
541 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gcpu_stat); BM_FREE(res_use);});
542 g_string_append(dump_p, TIZEN_DUMP_VERSION);
543 g_string_append(dump_p, ",0,l,");
544 g_string_append(dump_p, "cpu,");
545 g_string_append_printf(dump_p, "%ld,", gcpu_stat->usr_time);
546 g_string_append_printf(dump_p, "%ld,", gcpu_stat->sys_time);
547 g_string_append_printf(dump_p, "%0.2f", usagemah);
548 g_string_append(dump_p, "\n");
549 bm_resourceid_usage_free(res_use);
550 _DBG("%s", dump_p->str);
551 if (write(fd, dump_p->str, dump_p->len) < 0)
552 _WARN("write error");
553 g_string_free(dump_p, TRUE);
559 static int bd_get_and_print_uid_info_entry_from_dump(int fd)
563 int ret = BATTERY_MONITOR_ERROR_NONE;
564 GSList *list = bm_server_query_appid_map(&ret);
566 _ERR("NO LIST DATA FOUND");
569 app_map = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
570 GSList *iter = NULL; GString *dump_p = NULL;
571 dump_p = g_string_sized_new(30);
572 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
573 g_string_append(dump_p, TIZEN_DUMP_VERSION);
574 g_string_append(dump_p, ",0,i,uid,1000,tizen\n");
575 if (write(fd, dump_p->str, dump_p->len) < 0)
576 _WARN("write error");
577 g_string_free(dump_p, TRUE);
579 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
580 dump_p = g_string_sized_new(30);
581 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
582 g_string_append(dump_p, TIZEN_DUMP_VERSION);
583 g_string_append(dump_p, ",0");
584 g_string_append(dump_p, ",i");
585 g_string_append(dump_p, ",uid,");
586 appid_map_s *amap_data = (appid_map_s *)iter->data;
587 g_string_append_printf(dump_p, "%d,", (amap_data->id+10000));
588 gchar *temp_str = g_strdup(amap_data->AppId);
589 g_string_append(dump_p, amap_data->AppId);
590 g_string_append(dump_p, "\n");
591 _DBG("%s", dump_p->str);//write
592 if (write(fd, dump_p->str, dump_p->len) < 0)
593 _WARN("write error");
594 int *temp_id = (int *)malloc(4);
595 *temp_id = (amap_data->id + 10000);
596 _DBG("Insert id %d, str %s, res %d", *temp_id, temp_str,
597 g_hash_table_insert(app_map, temp_id, temp_str));
598 g_string_free(dump_p, TRUE);
600 bm_set_free_gslist_appid_map(list);
606 static int bd_get_history_detail_from_dump(int index, history_item_s* dump_node)
609 BM_CHECK_INPUT_PARAM(dump_node);
611 if (h_count >= HISTORY_SIZE_MAX || index > h_count) {
612 _ERR("Some Error occured with global index");
613 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
616 _DBG("Value of index %d", index);
617 _DBG("Value of h_count %d", h_count);
619 int dvar1 = 0, dvar2 = 0;
620 dvar1 = history_data[index].dump_1;
621 dvar2 = history_data[index].dump_2;
623 _DBG("Value of encoded value 1 %d", dvar1);
624 _DBG("Value of encoded value 2 %d", dvar2);
626 dump_node->cmd_s = ((dvar1) & 0xff);
627 _DBG("cmd_s %d", dump_node->cmd_s);
629 dump_node->battery_level = ((dvar1>>8) & 0xff);
630 _DBG("battery_level %d", dump_node->battery_level);
632 dump_node->battery_status = ((dvar1>>16) & 0xf);
633 _DBG("battery_status %d", dump_node->battery_status);
635 dump_node->battery_health = ((dvar1>>20) & 0xf);
636 _DBG("battery_health %d", dump_node->battery_health);
638 dump_node->battery_plugtype = ((dvar1>>24) & 0xf);
639 _DBG("battery_plugtype %d", dump_node->battery_plugtype);
641 dump_node->battery_temperature = ((dvar2) & 0xffff);
642 _DBG("battery_temperature %d", dump_node->battery_temperature);
644 dump_node->battery_voltage = ((dvar2>>16) & 0xffff);
645 _DBG("battery_voltage %d", dump_node->battery_voltage);
647 dump_node->battery_charge = history_data[index].battery_chargeUAh;
648 _DBG("battery_charge %d", dump_node->battery_charge);
650 dump_node->event_code = history_data[index].event_code;
651 _DBG("eventcode %d", dump_node->event_code);
653 dump_node->state_1 = history_data[index].state_1;
654 _DBG("state_1 %d", dump_node->state_1);
656 dump_node->state_2 = history_data[index].state_2;
657 _DBG("state_2 %d", dump_node->state_2);
659 dump_node->event_tag = history_data[index].event_tag;
660 if (dump_node->event_tag == NULL)
661 _DBG("Event_Tag_NULL");
663 _DBG("%d %d %s", dump_node->event_tag->uid, dump_node->event_tag->sp_idx,
664 dump_node->event_tag->string_info->str);
665 dump_node->wakelock_tag = history_data[index].wakelock_tag;
666 if (dump_node->wakelock_tag == NULL)
667 _DBG("Wakelock_Tag_NULL");
668 dump_node->wakereason_tag = history_data[index].wakereason_tag;
670 if (dump_node->wakereason_tag == NULL)
671 _DBG("Wakereason_Tag_NULL");
673 dump_node->time_s = history_data[index].time_s;
674 _DBG("TIME_S is %lld", dump_node->time_s);
676 dump_node->time_current = history_data[index].time_current;
677 _DBG("C TIME is %lld", dump_node->time_current);
679 dump_node->usage_type = history_data[index].usage_type;
680 _DBG("Usage type is %d", dump_node->usage_type);
682 dump_node->usage = history_data[index].usage;
683 _DBG("Usage is %d", dump_node->usage);
686 return BATTERY_MONITOR_ERROR_NONE;
689 int bd_print_history_item_reset(history_item_s* old_state)
692 BM_CHECK_INPUT_PARAM(old_state);
694 old_state->time_s = -1;
695 old_state->state_1 = 0;
696 old_state->state_2 = 0;
697 old_state->usage_type = 0;
698 old_state->usage = 0;
699 old_state->battery_level = -1;
700 old_state->battery_status = -1;
701 old_state->battery_health = -1;
702 old_state->battery_plugtype = -1;
703 old_state->battery_temperature = -1;
704 old_state->battery_voltage = -1;
705 old_state->battery_charge = -1;
706 old_state->event_code = 0;
707 old_state->event_tag = NULL;
708 old_state->wakelock_tag = NULL;
709 old_state->wakereason_tag = NULL;
712 return BATTERY_MONITOR_ERROR_NONE;
715 int bd_get_base_time_from_dump(long long *base_time)
719 *base_time = history_data[0].time_s;
722 return BATTERY_MONITOR_ERROR_NONE;
725 int bd_print_history_item_main(int fd, int num_h_items, long long base_time, bool chkin)
731 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
734 if (h_count >= HISTORY_SIZE_MAX) {
735 _ERR("Some Error occured with global index");
736 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
739 int ret = BATTERY_MONITOR_ERROR_NONE;
740 h_flag = false; //Lock
742 history_item_s new_state;
743 history_item_s old_state;
745 ret = bd_print_history_item_reset(&old_state);
746 if (ret != BATTERY_MONITOR_ERROR_NONE) {
747 _ERR("Old State reset failed");
751 ret = bd_get_pool_entry_from_dump(fd);
752 if (ret != BATTERY_MONITOR_ERROR_NONE)
753 _ERR("S pool data dump failed");
755 ret = bd_get_base_time_from_dump(&base_time);
756 if (ret != BATTERY_MONITOR_ERROR_NONE)
757 _ERR("Base time error");
759 int log_count = h_count; GString *dump_p = NULL;
760 for (int i = 0; i < log_count; i++) {
761 ret = bd_get_history_detail_from_dump(i, &new_state);
762 if (ret != BATTERY_MONITOR_ERROR_NONE) {
763 _ERR("Decode Failed");
766 dump_p = g_string_sized_new(30);
767 BM_CHECK_MEM_ALLOC(dump_p, {});
769 g_string_append(dump_p, TIZEN_DUMP_VERSION);
770 g_string_append_c(dump_p, ',');
771 _DBG("value %s", dump_p->str);
772 g_string_append(dump_p, DUMP_DATA_TAG);
773 g_string_append_c(dump_p, ',');
774 _DBG("value %s", dump_p->str);
775 if (old_state.time_s < 0) {
776 g_string_append_printf(dump_p, "%lld",
777 (new_state.time_s - base_time));
778 _DBG("value %s", dump_p->str);
780 g_string_append_printf(dump_p, "%lld",
781 (new_state.time_s - old_state.time_s));
782 _DBG("value %s", dump_p->str);
784 old_state.time_s = new_state.time_s;
785 _DBG("Value old_state.time_s %lld", old_state.time_s);
786 } else { //: Let's skip the NC mode in initial version
788 if (new_state.cmd_s == CM_ST) {
790 g_string_append_c(dump_p, ':');
791 _DBG("value %s", dump_p->str);
795 g_string_append(dump_p, "START\n");
796 _DBG("value %s", dump_p->str);
797 bd_print_history_item_reset(&old_state);
798 } else if (new_state.cmd_s == CM_CRR_TIME || new_state.cmd_s == CM_RST) {
800 g_string_append_c(dump_p, ':');
801 _DBG("value %s", dump_p->str);
803 if (new_state.cmd_s == CM_RST) {
804 g_string_append(dump_p, "RESET:");
805 _DBG("value %s", dump_p->str);
806 bd_print_history_item_reset(&old_state);
808 g_string_append(dump_p, "TIME:");
809 _DBG("value %s", dump_p->str);
811 g_string_append_printf(dump_p, "%lld", new_state.time_current);
812 _DBG("value %s", dump_p->str);
813 g_string_append(dump_p, "\n");
814 _DBG("value %s", dump_p->str);
818 } else if (new_state.cmd_s == CM_SHTDN) {
820 g_string_append_c(dump_p, ':');
821 _DBG("value %s", dump_p->str);
825 g_string_append(dump_p, "SHUTDOWN\n");
826 _DBG("value %s", dump_p->str);
827 } else if (new_state.cmd_s == CM_OVRFL) {
829 g_string_append_c(dump_p, ':');
830 _DBG("value %s", dump_p->str);
832 g_string_append(dump_p, "*OVERFLOW*\n");
833 _DBG("value %s", dump_p->str);
834 } else if (new_state.cmd_s == CM_USAGE) {
835 for (int i = 0; i < 32; i++) {
836 if (new_state.usage_type == (1 << i)) {
837 g_string_append(dump_p, ",");
838 g_string_append_printf(dump_p, "%s", usage_map[i]);
839 g_string_append(dump_p, "=");
840 g_string_append_printf(dump_p, "%d", new_state.usage);
841 g_string_append(dump_p, "\n");
842 _DBG("value %d", new_state.usage);
849 if (old_state.battery_level != new_state.battery_level) {
850 old_state.battery_level = new_state.battery_level;
851 g_string_append(dump_p, ",Bl=");
852 _DBG("value %s", dump_p->str);
853 g_string_append_printf(dump_p, "%d",
854 new_state.battery_level);
855 _DBG("value %s", dump_p->str);
858 if (old_state.battery_status != new_state.battery_status) {
859 old_state.battery_status = new_state.battery_status;
860 g_string_append(dump_p, ",Bs="); //:for NC
861 _DBG("value %s", dump_p->str);
862 switch (old_state.battery_status) {
864 g_string_append_c(dump_p, '?');
867 g_string_append_c(dump_p, 'c');
870 g_string_append_c(dump_p, 'd');
873 g_string_append_c(dump_p, 'n');
876 g_string_append_c(dump_p, 'f');
879 g_string_append_c(dump_p, '?');
880 _DBG("value %s", dump_p->str);
884 if (old_state.battery_health != new_state.battery_health) {
885 old_state.battery_health = new_state.battery_health;
886 g_string_append(dump_p, ",Bh="); //:for NC
887 _DBG("value %s", dump_p->str);
888 switch (old_state.battery_health) {
890 g_string_append_c(dump_p, 'g');
893 g_string_append_c(dump_p, '?');
896 g_string_append_c(dump_p, 'h');
899 g_string_append_c(dump_p, 'd');
902 g_string_append_c(dump_p, 'v');
905 g_string_append_c(dump_p, 'f');
908 g_string_append_c(dump_p, 'c');
911 g_string_append_c(dump_p, '?');
912 _DBG("value %s", dump_p->str);
916 if (old_state.battery_plugtype != new_state.battery_plugtype) {
917 old_state.battery_plugtype = new_state.battery_plugtype;
918 g_string_append(dump_p, ",Bp="); //:for NC
919 _DBG("value %s", dump_p->str);
920 switch (old_state.battery_plugtype) {
922 g_string_append_c(dump_p, 'n');
925 g_string_append_c(dump_p, 'a');
928 g_string_append_c(dump_p, 'u');
931 g_string_append_c(dump_p, 'w');
934 g_string_append_c(dump_p, 'n');
935 _DBG("value %s", dump_p->str);
939 if (old_state.battery_temperature != new_state.battery_temperature) {
940 old_state.battery_temperature = new_state.battery_temperature;
941 g_string_append(dump_p, ",Bt="); //:for NC
942 g_string_append_printf(dump_p, "%d",
943 new_state.battery_temperature);
944 _DBG("value %s", dump_p->str);
946 if (old_state.battery_voltage != new_state.battery_voltage) {
947 old_state.battery_voltage = new_state.battery_voltage;
948 g_string_append(dump_p, ",Bv="); //:for NC
949 _DBG("value %s", dump_p->str);
950 g_string_append_printf(dump_p, "%d", new_state.battery_voltage);
951 _DBG("value %s", dump_p->str);
953 if (old_state.battery_charge != new_state.battery_charge) {
954 old_state.battery_charge = new_state.battery_charge;
955 g_string_append(dump_p, ",Bcc=");
956 _DBG("value %s", dump_p->str);
958 g_string_append_printf(dump_p, "%d", (new_state.battery_charge/1000));
959 _DBG("value %s", dump_p->str);
961 int change = (old_state.state_1 ^ new_state.state_1);
963 bool wake_fl = false; int k = 0;
964 if ((change & ST1_BRIGHTNESS_MASK) != 0) {
965 k = (new_state.state_1 & ST1_BRIGHTNESS_MASK);
966 g_string_append(dump_p, ",Sb=");
967 k = k >> ST1_BRIGHTNESS_SHIFT;
969 g_string_append_printf(dump_p, "%d", k);
972 if ((change & ST1_PHONE_SIGNAL_STRENGTH_MASK) != 0) {
973 k = (new_state.state_1 & ST1_PHONE_SIGNAL_STRENGTH_MASK);
974 g_string_append(dump_p, ",Pss=");
975 k = k >> ST1_PHONE_SIGNAL_STRENGTH_SHIFT;
977 g_string_append_printf(dump_p, "%d", k);
980 int shifts = (1 << 14);
981 for (int i = 14; i < 32; i++) {
982 if ((change & shifts) != 0) {
983 g_string_append_c(dump_p, ',');
984 //Considering only -ve shift cases
987 if ((new_state.state_1 & (1 << i)) != 0)
988 g_string_append_c(dump_p, '+');
990 g_string_append_c(dump_p, '-');
991 gchar *temp = g_strdup(state1_map[i]);
992 g_string_append(dump_p, temp);
994 //_DBG("value %s", dump_p->str);
995 if ((1 << i) == ST1_WAKE_LOCK_FLAG &&
996 new_state.wakelock_tag != NULL) {
998 g_string_append_c(dump_p, '=');
999 //_DBG("value %s", dump_p->str);
1001 g_string_append_printf(dump_p,
1002 "%d", new_state.wakelock_tag->sp_idx);
1006 //_DBG("value %s", dump_p->str);
1009 //With non-negative shift
1012 shifts = (shifts << 1);
1014 if (!wake_fl && new_state.wakelock_tag != NULL) {
1015 g_string_append(dump_p, ",w=");
1017 g_string_append_printf(dump_p, "%d",
1018 new_state.wakelock_tag->sp_idx);
1019 _DBG("value %s", dump_p->str);
1024 change = (old_state.state_2 ^ new_state.state_2);
1027 for (int i = 0; i < 32; i++) {
1028 if ((change & shifts) != 0) {
1029 g_string_append_c(dump_p, ',');
1030 //_DBG("value %s", dump_p->str);
1033 if ((new_state.state_2 & (1 << i) & change) != 0)
1034 g_string_append_c(dump_p, '+');
1036 g_string_append_c(dump_p, '-');
1037 //_DBG("value %s", dump_p->str);
1038 gchar *temp = g_strdup(state2_map[i]);
1039 g_string_append(dump_p, temp);
1041 _DBG("value %s", dump_p->str);
1044 shifts = (shifts << 1);
1047 if (new_state.wakereason_tag != NULL) {
1049 g_string_append(dump_p, ",wr=");
1050 _DBG("value %s", dump_p->str);
1051 g_string_append_printf(dump_p, "%d",
1052 new_state.wakereason_tag->sp_idx);
1053 _DBG("value %s", dump_p->str);
1058 if (new_state.event_code != ET_NONE) {
1059 g_string_append_c(dump_p, ',');
1060 _DBG("value %s", dump_p->str);
1061 if ((new_state.event_code & ET_FLAG_START) != 0)
1062 g_string_append_c(dump_p, '+');
1063 else if ((new_state.event_code & ET_FLAG_FINISH) != 0)
1064 g_string_append_c(dump_p, '-');
1065 _DBG("value %s", dump_p->str);
1066 int k = new_state.event_code & ~(ET_FLAG_START | ET_FLAG_FINISH);
1067 if (k >= 0 && k < 32) {
1068 gchar* temp = g_strdup(event_map[k]);
1069 g_string_append(dump_p, temp);
1070 _DBG("value %s", dump_p->str);
1073 g_string_append(dump_p, "Ev");
1074 g_string_append_printf(dump_p, "%d", k);
1075 _DBG("value %s", dump_p->str);
1077 g_string_append_c(dump_p, '=');
1078 _DBG("value %s", dump_p->str);
1080 g_string_append_printf(dump_p, "%d",
1081 new_state.event_tag->sp_idx);
1087 g_string_append(dump_p, "\n");
1089 old_state.state_1 = new_state.state_1;
1090 old_state.state_2 = new_state.state_2;
1092 _DBG("State assigned");
1093 _DBG("%s", dump_p->str);
1094 if (write(fd, dump_p->str, dump_p->len) < 0)
1095 _WARN("write error");
1096 g_string_free(dump_p, TRUE);
1099 // Dump the elapsed time from last event
1100 // It should be dumped without storing
1101 GString *dump_tmp_p = g_string_sized_new(30);
1103 gettimeofday(&tv, NULL);
1105 g_string_append(dump_tmp_p, TIZEN_DUMP_VERSION);
1106 g_string_append_c(dump_tmp_p, ',');
1107 g_string_append(dump_tmp_p, DUMP_DATA_TAG);
1108 g_string_append_c(dump_tmp_p, ',');
1109 g_string_append_printf(dump_tmp_p, "%lld",
1110 ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000) - old_state.time_s);
1111 g_string_append(dump_tmp_p, "\n");
1113 if (write(fd, dump_tmp_p->str, dump_tmp_p->len) < 0)
1114 _WARN("write error");
1115 g_free(dump_tmp_p->str);
1117 //Dump i & l values after this
1118 ret = bd_get_and_print_uid_info_entry_from_dump(fd);
1119 if (ret != BATTERY_MONITOR_ERROR_NONE)
1120 _ERR("UID data dump failed");
1121 // Get Last charge time to dump "l" field
1122 // for the time being dumping all data
1123 long int duration = 0; //Time of last charge
1124 ret = bd_get_and_print_gn_stat_entry_from_dump(fd, duration);
1125 if (ret != BATTERY_MONITOR_ERROR_NONE)
1126 _ERR("GN dump failed");
1127 ret = bd_get_and_print_gwf_stat_entry_from_dump(fd, duration);
1128 if (ret != BATTERY_MONITOR_ERROR_NONE)
1129 _ERR("GWF data dump failed");
1130 ret = bd_get_and_print_gble_stat_entry_from_dump(fd, duration);
1131 if (ret != BATTERY_MONITOR_ERROR_NONE)
1132 _ERR("GBL data dump failed");
1133 ret = bd_get_and_print_gbr_stat_entry_from_dump(fd, duration);
1134 if (ret != BATTERY_MONITOR_ERROR_NONE)
1135 _ERR("GBR data dump failed");
1136 ret = bd_get_and_print_gst_stat_entry_from_dump(fd, duration);
1137 if (ret != BATTERY_MONITOR_ERROR_NONE)
1138 _ERR("GST data dump failed");
1139 ret = bd_get_and_print_gwst_stat_entry_from_dump(fd, duration);
1140 if (ret != BATTERY_MONITOR_ERROR_NONE)
1141 _ERR("GWST data dump failed");
1142 ret = bd_get_and_print_gcpu_stat_entry_from_dump(fd, duration);
1143 _ERR("GCPU data dump failed");
1144 // Battery Usage Dump
1145 // Add usage for each of the resource
1146 // and each of the application
1147 ret = bd_get_and_print_battery_usage_from_dump(fd, duration);
1148 if (ret != BATTERY_MONITOR_ERROR_NONE)
1149 _ERR("Battery Usage failed");
1151 _DBG("DUMP COMPLETED/DELETE HASH TABLE");
1153 g_hash_table_destroy(app_map);
1156 return BATTERY_MONITOR_ERROR_NONE;
1159 static int bd_dump_data_item_reset(dump_data_s* old_state)
1162 BM_CHECK_INPUT_PARAM(old_state);
1164 old_state->time_s = -1;
1165 old_state->time_current = -1;
1166 old_state->dump_1 = 0;
1167 old_state->dump_2 = 0;
1168 old_state->state_1 = 0;
1169 old_state->state_2 = 0;
1170 old_state->battery_chargeUAh = 0;
1171 old_state->usage_type = 0;
1172 old_state->usage = 0;
1173 old_state->event_code = ET_NONE;
1174 old_state->event_tag = NULL;
1175 old_state->wakelock_tag = NULL;
1176 old_state->wakereason_tag = NULL;
1179 return BATTERY_MONITOR_ERROR_NONE;
1182 static int bd_free_history_data_memory()
1186 int ret = BATTERY_MONITOR_ERROR_NONE;
1187 h_flag = false; //Lock
1188 _DBG("Free History Data Memory");
1189 for (int i = 0; i < h_count; i++) {
1190 if (history_data[i].wakelock_tag != NULL) {
1192 g_string_free(history_data[i].wakelock_tag->string_info, TRUE);
1194 free(history_data[i].wakelock_tag);
1196 if (history_data[i].wakereason_tag != NULL) {
1198 g_string_free(history_data[i].wakereason_tag->string_info, TRUE);
1200 free(history_data[i].wakereason_tag);
1202 if (history_data[i].event_tag != NULL) {
1204 g_string_free(history_data[i].event_tag->string_info, TRUE);
1206 free(history_data[i].event_tag);
1208 ret = bd_dump_data_item_reset(&history_data[i]);
1209 if (ret != BATTERY_MONITOR_ERROR_NONE)
1210 _ERR("Internal error %d while freeing at idx %d", ret , i);
1213 h_count = 0; h_flag = true;
1218 static int bd_set_history_from_listener(history_item_s* nrec)
1221 BM_CHECK_INPUT_PARAM(nrec);
1223 int dvar1 = 0, dvar2 = 0;
1225 gettimeofday(&tv, NULL);
1227 time_t current_time = tv.tv_sec;
1230 history_data[h_count].time_s = nrec->time_s;
1232 history_data[h_count].time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1235 dvar1 = ((nrec->cmd_s) & 0xff) | ((nrec->battery_level << 8) & 0xff00) |
1236 ((nrec->battery_status << 16) & 0xf0000) |
1237 ((nrec->battery_health << 20) & 0xf00000) |
1238 ((nrec->battery_plugtype << 24) & 0xf000000) |
1239 ((nrec->wakelock_tag != NULL) ? 0x10000000 : 0) |
1240 ((nrec->wakereason_tag != NULL) ? 0x20000000 : 0) |
1241 ((nrec->event_code != ET_NONE) ? 0x40000000 : 0);
1243 dvar2 = ((nrec->battery_temperature) & 0xffff) |
1244 ((nrec->battery_voltage << 16) & 0xffff0000);
1246 _DBG("Value of encoded value 1 %d", dvar1);
1247 _DBG("Value of encoded value 2 %d", dvar2);
1249 history_data[h_count].dump_1 = dvar1;
1250 history_data[h_count].dump_2 = dvar2;
1251 history_data[h_count].battery_chargeUAh = nrec->battery_charge;
1253 if (nrec->wakelock_tag != NULL)
1254 history_data[h_count].wakelock_tag = nrec->wakelock_tag;
1255 if (nrec->wakereason_tag != NULL)
1256 history_data[h_count].wakereason_tag = nrec->wakereason_tag;
1257 if (nrec->event_code != ET_NONE) {
1258 if (nrec->event_tag == NULL) {
1259 _ERR("ET is NULL but Event Code is not");
1260 BM_FREE(history_data[h_count].wakelock_tag);
1261 BM_FREE(history_data[h_count].wakereason_tag);
1262 bd_dump_data_item_reset(&history_data[h_count]);
1263 return BATTERY_MONITOR_ERROR_NO_DATA;
1265 history_data[h_count].event_code = nrec->event_code;
1266 history_data[h_count].event_tag = nrec->event_tag;
1268 if (nrec->cmd_s == CM_CRR_TIME || nrec->cmd_s == CM_RST)
1269 history_data[h_count].time_current = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1271 history_data[h_count].state_1 = nrec->state_1;
1272 history_data[h_count].state_2 = nrec->state_2;
1274 history_data[h_count].usage_type = nrec->usage_type;
1275 history_data[h_count].usage = nrec->usage;
1277 nrec->event_tag = NULL; nrec->wakereason_tag = NULL;
1278 nrec->wakelock_tag = NULL;
1279 if (h_count < HISTORY_SIZE_MAX - 1)
1282 // FIXME modify 1st argument (fd)
1283 int ret = bd_print_history_item_main(-1, 0, current_time, 1);
1284 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1288 ret = bd_free_history_data_memory();
1289 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1290 _WARN("Error while memory free %x", ret);
1296 return BATTERY_MONITOR_ERROR_NONE;
1299 int bd_store_history_item(history_item_s* nrec)
1302 BM_CHECK_INPUT_PARAM(nrec);
1305 _ERR("Data Dump in progress");
1306 return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
1308 int ret = BATTERY_MONITOR_ERROR_NONE;
1309 ret = bd_set_history_from_listener(nrec);
1310 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1311 _ERR("Failed to store history");
1312 return BATTERY_MONITOR_ERROR_INTERNAL;
1316 return BATTERY_MONITOR_ERROR_NONE;
1319 static int *dumpsys_handler;
1321 int bd_callback(const int fd, const int argc, char **argv)
1325 bool checkin = false;
1328 * We must reset optind because it isn't initialized
1329 * on subsequent getopt calls
1332 while ((opt = getopt(argc, argv, "c")) != -1) {
1343 ret = bd_print_history_item_main(fd, -1, 0, checkin);
1344 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1345 _ERR("bd_print_history_item_main failed (%x)", ret);
1352 int bd_initialize_battery_dump()
1356 int ret = BATTERY_MONITOR_ERROR_NONE;
1358 h_count = 0; h_flag = true;
1364 ret = bd_print_history_item_reset(&ts);
1365 if (ret == BATTERY_MONITOR_ERROR_NONE) {
1366 ts.cmd_s = CM_CRR_TIME;
1367 ret = bd_store_history_item(&ts);
1368 if (ret != BATTERY_MONITOR_ERROR_NONE)
1369 _WARN("Init Node addition failed");
1373 ret = dumpsys_system_register_dump_cb(bd_callback, "batterystats", (void **)&dumpsys_handler);
1374 if (ret != TIZEN_ERROR_NONE) {
1375 _ERR("dumpsys_system_register_dump_cb failed (%x)", ret);
1376 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1379 _INFO("Battery Dump Init Successful");
1382 return BATTERY_MONITOR_ERROR_NONE;
1385 int bd_deinitialize_battery_dump()
1391 ret = dumpsys_system_unregister_dump_cb(dumpsys_handler);
1392 if (ret != TIZEN_ERROR_NONE) {
1393 _WARN("dumpsys_system_unregister_dump_cb failed (%x)", ret);
1394 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1397 ret = bd_free_history_data_memory();
1398 if (ret != BATTERY_MONITOR_ERROR_NONE)
1399 _WARN("memory free failed (%x)", ret);
1401 _INFO("Battery Dump De-Init Successful");
1404 return BATTERY_MONITOR_ERROR_NONE;