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, {});
165 g_string_append(dump_p, TIZEN_DUMP_VERSION);
167 g_string_append_printf(dump_p, ",0,l,pws,%d,0,0,0", battery_capacity);
168 g_string_append(dump_p, "\n");
170 _DBG("%s", dump_p->str);
172 if (write(fd, dump_p->str, dump_p->len) < 0)
173 _WARN("write error");
175 g_string_free(dump_p, TRUE);
177 g_hash_table_iter_init(&iter, app_map);
178 while (g_hash_table_iter_next(&iter, &key, &value)) {
179 uid_n = (int *)key; appid = (char *)value;
180 _DBG("%d %s", *uid_n, appid); usageMAh = 0;
181 // Currently giving app usage of last one week;
182 // Modify it wrt to time
183 appid_usage_s *app_use = bm_server_query_app_usage_by_appid(appid, 1, &ret);
184 if (app_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
185 _ERR("For appid %s", appid);
188 usageMAh = (app_use->rId_ble + app_use->rId_wifi + app_use->rId_cpu +
189 app_use->rId_display + app_use->rId_device_network +
190 app_use->rId_gps + app_use->rId_hrm);
191 // usage stored as mAs changed to mAh
192 usageMAh = (usageMAh/3600);
193 dump_p = g_string_sized_new(30);
194 BM_CHECK_MEM_ALLOC(dump_p, { BM_FREE(app_use); });
195 g_string_append(dump_p, TIZEN_DUMP_VERSION);
196 g_string_append_printf(dump_p, ",%d,l,pwi,uid,", *uid_n);
197 g_string_append_printf(dump_p, "%0.2f", usageMAh);
198 g_string_append(dump_p, "\n");
199 bm_appid_usage_free(app_use);
200 _DBG("%s", dump_p->str);
201 if (write(fd, dump_p->str, dump_p->len) < 0)
202 _WARN("write error");
203 g_string_free(dump_p, TRUE);
206 const char *rid_str = NULL;
207 for (int i = 0; i < 6; i++) {
209 rid_str = bm_get_resource_id_string(i);
210 if (g_strcmp0(rid_str, "UNKNOWN RESOURCE-ID") == 0) {
211 _ERR("invalid resource-id");
212 return BATTERY_MONITOR_ERROR_INTERNAL;
214 resourceid_usage_s *res_use =
215 bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
216 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
217 _ERR("For resourceid %s", rid_str);
220 dump_p = g_string_sized_new(30);
221 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(res_use); });
222 usage = res_use->usage;
223 // usage stored as mAs changed to mAh
224 usageMAh = (float)usage/3600;
225 g_string_append(dump_p, TIZEN_DUMP_VERSION);
228 g_string_append(dump_p, ",0,l,pwi,blue,");
231 g_string_append(dump_p, ",0,l,pwi,wifi,");
234 g_string_append(dump_p, ",0,l,pwi,phone,");
237 g_string_append(dump_p, ",0,l,pwi,scrn,");
240 g_string_append(dump_p, ",0,l,pwi,cell,");
243 g_string_append(dump_p, ",0,l,pwi,unacc,");
246 g_string_append_printf(dump_p, "%0.2f", usageMAh);
247 g_string_append(dump_p, "\n");
248 bm_resourceid_usage_free(res_use);
249 _DBG("%s", dump_p->str);
250 if (write(fd, dump_p->str, dump_p->len) < 0)
251 _WARN("write error");
252 g_string_free(dump_p, TRUE);
259 static int bd_get_and_print_gbr_stat_entry_from_dump(int fd, long int duration)
262 // Dump brightness Data
263 bd_gl_sbr_stat_s *gbr_stat = (bd_gl_sbr_stat_s *)calloc(1, sizeof(bd_gl_sbr_stat_s));
264 BM_CHECK_MEM_ALLOC(gbr_stat, {});
266 int ret = BATTERY_MONITOR_ERROR_NONE;
267 ret = bm_server_battery_dump_query_from_db(gbr_stat, 3, duration);
268 if (ret != BATTERY_MONITOR_ERROR_NONE) {
269 _ERR("Unable to fetch data from DB");
274 GString *dump_p = g_string_sized_new(30);
275 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gbr_stat); });
276 g_string_append(dump_p, TIZEN_DUMP_VERSION);
277 g_string_append(dump_p, ",0,l,");
278 g_string_append(dump_p, "br,");
279 g_string_append_printf(dump_p, "%ld,", gbr_stat->dark); //OFF
280 g_string_append_printf(dump_p, "%ld,", gbr_stat->dim); // All are time values
281 g_string_append_printf(dump_p, "%ld,", gbr_stat->medium);
282 g_string_append_printf(dump_p, "%ld,", gbr_stat->light); //DEF: 0
283 g_string_append_printf(dump_p, "%ld", gbr_stat->bright);
284 g_string_append(dump_p, "\n");
285 _DBG("%s", dump_p->str);//write
286 if (write(fd, dump_p->str, dump_p->len) < 0)
287 _WARN("write error");
288 g_string_free(dump_p, TRUE);
295 static int bd_get_and_print_misc_stat_entry_from_dump(int fd, long int duration)
298 //Dump screen on and off time
299 //Modify m - field accordingly.
300 bd_gl_sbr_stat_s *gbr_stat = (bd_gl_sbr_stat_s *)calloc(1, sizeof(bd_gl_sbr_stat_s));
301 BM_CHECK_MEM_ALLOC(gbr_stat, {});
303 int ret = BATTERY_MONITOR_ERROR_NONE;
304 ret = bm_server_battery_dump_query_from_db(gbr_stat, 3, duration);
305 if (ret != BATTERY_MONITOR_ERROR_NONE) {
306 _ERR("Unable to fetch data from DB");
311 GString *dump_p = g_string_sized_new(30);
312 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gbr_stat); });
313 g_string_append(dump_p, TIZEN_DUMP_VERSION);
314 g_string_append(dump_p, ",0,l,");
315 g_string_append(dump_p, "m,");
316 long int off_time = gbr_stat->dark;
317 long int on_time = (gbr_stat->dim + gbr_stat->medium + gbr_stat->bright);
318 g_string_append_printf(dump_p, "%ld,", on_time);
319 g_string_append_printf(dump_p, "%ld", off_time);
320 g_string_append(dump_p, "\n");
321 _DBG("%s", dump_p->str);
322 if (write(fd, dump_p->str, dump_p->len) < 0)
323 _WARN("write error");
324 g_string_free(dump_p, TRUE);
331 static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration)
336 bd_gl_ntw_stat_s *gn_stat = (bd_gl_ntw_stat_s *)calloc(1, sizeof(bd_gl_ntw_stat_s));
337 BM_CHECK_MEM_ALLOC(gn_stat, {});
338 int ret = BATTERY_MONITOR_ERROR_NONE;
339 ret = bm_server_battery_dump_query_from_db(gn_stat, 2, duration);
340 if (ret != BATTERY_MONITOR_ERROR_NONE) {
341 _ERR("Unable to fetch data from DB");
346 GString *dump_p = g_string_sized_new(30);
347 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gn_stat); });
349 g_string_append(dump_p, TIZEN_DUMP_VERSION);
350 g_string_append(dump_p, ",0,l,");
351 g_string_append(dump_p, "gn,");
352 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_rx_byt);
353 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_tx_byt);
354 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_rx_byt);
355 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_tx_byt);
356 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_rx_pkt);
357 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_tx_pkt);
358 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_rx_pkt);
359 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_tx_pkt);
360 g_string_append_printf(dump_p, "%d,", 0); //add this to struct as well
361 g_string_append_printf(dump_p, "%d", 0);
362 g_string_append(dump_p, "\n");
364 _DBG("%s", dump_p->str);//write
365 if (write(fd, dump_p->str, dump_p->len) < 0)
366 _WARN("write error");
368 g_string_free(dump_p, TRUE);
375 static int bd_get_and_print_gwf_stat_entry_from_dump(int fd, long int duration)
379 bd_gl_wifi_stat_s *gwfl_stat = (bd_gl_wifi_stat_s *)calloc(1, sizeof(bd_gl_wifi_stat_s));
380 BM_CHECK_MEM_ALLOC(gwfl_stat, {});
382 int ret = BATTERY_MONITOR_ERROR_NONE;
383 ret = bm_server_battery_dump_query_from_db(gwfl_stat, 1, duration);
384 if (ret != BATTERY_MONITOR_ERROR_NONE) {
385 _ERR("Unable to fetch data from DB");
390 GString *dump_p = g_string_sized_new(30);
391 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
392 g_string_append(dump_p, TIZEN_DUMP_VERSION);
393 g_string_append(dump_p, ",0,l,");
394 g_string_append(dump_p, "gwfl,");
395 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_on_time);
396 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_conn_time);
397 g_string_append(dump_p, "0,0,0");//legacy
399 g_string_append(dump_p, "\n");
400 _DBG("%s", dump_p->str);//write
401 if (write(fd, dump_p->str, dump_p->len) < 0)
402 _WARN("write error");
403 g_string_free(dump_p, TRUE);
405 dump_p = g_string_sized_new(30);
406 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
407 g_string_append(dump_p, TIZEN_DUMP_VERSION);
408 g_string_append(dump_p, ",0,l,");
409 g_string_append(dump_p, "gwfcd,");
411 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_idle_time);
412 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_rx_time);
413 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_pwi_val);
414 g_string_append_printf(dump_p, "%ld", gwfl_stat->wifi_tx_time);
415 g_string_append(dump_p, "\n");
416 _DBG("%s", dump_p->str);//write
417 if (write(fd, dump_p->str, dump_p->len) < 0)
418 _WARN("write error");
419 g_string_free(dump_p, TRUE);
426 static int bd_get_and_print_gble_stat_entry_from_dump(int fd, long int duration)
430 bd_gl_ble_stat_s *gble_stat = (bd_gl_ble_stat_s *)calloc(1, sizeof(bd_gl_ble_stat_s));
431 BM_CHECK_MEM_ALLOC(gble_stat, {});
433 int ret = BATTERY_MONITOR_ERROR_NONE;
434 ret = bm_server_battery_dump_query_from_db(gble_stat, 0, duration);
435 if (ret != BATTERY_MONITOR_ERROR_NONE) {
436 _ERR("Unable to fetch data from DB");
441 GString *dump_p = g_string_sized_new(30);
442 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gble_stat); });
443 g_string_append(dump_p, TIZEN_DUMP_VERSION);
444 g_string_append(dump_p, ",0,l,");
445 g_string_append(dump_p, "gble,");
446 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_idle_time);
447 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_rx_time);
448 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_tx_time);
449 g_string_append_printf(dump_p, "%ld", gble_stat->ble_pwi_val);
450 g_string_append(dump_p, "\n");
452 _DBG("%s", dump_p->str);//write
453 if (write(fd, dump_p->str, dump_p->len) < 0)
454 _WARN("write error");
455 g_string_free(dump_p, TRUE);
462 static int bd_get_and_print_gst_stat_entry_from_dump(int fd, long int duration)
466 int ret = BATTERY_MONITOR_ERROR_NONE;
468 bd_gl_sgt_stat_s *gst_stat = (bd_gl_sgt_stat_s *)calloc(1, sizeof(bd_gl_sgt_stat_s));
469 BM_CHECK_MEM_ALLOC(gst_stat, {});
471 ret = bm_server_battery_dump_query_from_db(gst_stat, 4, duration);
472 if (ret != BATTERY_MONITOR_ERROR_NONE) {
473 _ERR("Unable to fetch data from DB");
478 GString *dump_p = g_string_sized_new(30);
479 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
481 g_string_append(dump_p, TIZEN_DUMP_VERSION);
482 g_string_append(dump_p, ",0,l,");
483 g_string_append(dump_p, "sgt,");
484 g_string_append_printf(dump_p, "%ld,", gst_stat->none_time);
485 g_string_append_printf(dump_p, "%ld,", gst_stat->poor_time);
486 g_string_append_printf(dump_p, "%ld,", gst_stat->med_time);
487 g_string_append_printf(dump_p, "%ld,", gst_stat->good_time);
488 g_string_append_printf(dump_p, "%ld", gst_stat->grt_time);
489 g_string_append(dump_p, "\n");
491 _DBG("%s", dump_p->str);//write
493 if (write(fd, dump_p->str, dump_p->len) < 0)
494 _WARN("write error");
495 g_string_free(dump_p, TRUE);
497 dump_p = g_string_sized_new(30);
498 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
500 g_string_append(dump_p, TIZEN_DUMP_VERSION);
501 g_string_append(dump_p, ",0,l,");
502 g_string_append(dump_p, "sst,");
503 g_string_append_printf(dump_p, "%ld", gst_stat->scan_time);
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");
510 g_string_free(dump_p, TRUE);
517 static int bd_get_and_print_gwst_stat_entry_from_dump(int fd, long int duration)
520 bd_gl_wst_stat_s *gwst_stat = (bd_gl_wst_stat_s *)calloc(1, sizeof(bd_gl_wst_stat_s));
521 BM_CHECK_MEM_ALLOC(gwst_stat, {});
523 int ret = BATTERY_MONITOR_ERROR_NONE;
524 ret = bm_server_battery_dump_query_from_db(gwst_stat, 5, duration);
525 if (ret != BATTERY_MONITOR_ERROR_NONE) {
526 _ERR("Unable to fetch data from DB");
531 GString *dump_p = g_string_sized_new(30);
532 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwst_stat); });
533 g_string_append(dump_p, TIZEN_DUMP_VERSION);
534 g_string_append(dump_p, ",0,l,");
535 g_string_append(dump_p, "wsgt,");
536 // g_string_append_printf(dump_p, "%ld,", gwst_stat->wscan_time);
537 g_string_append_printf(dump_p, "%ld,", gwst_stat->wnone_time);
538 g_string_append_printf(dump_p, "%ld,", gwst_stat->wpoor_time);
539 g_string_append_printf(dump_p, "%ld,", gwst_stat->wmed_time);
540 g_string_append_printf(dump_p, "%ld,", gwst_stat->wgood_time);
541 g_string_append_printf(dump_p, "%ld", gwst_stat->wgrt_time);
542 g_string_append(dump_p, "\n");
543 _DBG("%s", dump_p->str);//write
544 if (write(fd, dump_p->str, dump_p->len) < 0)
545 _WARN("write error");
546 g_string_free(dump_p, TRUE);
553 static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration)
557 int ret = BATTERY_MONITOR_ERROR_NONE;
558 const char *rid_str = NULL; int usage = 0;
559 rid_str = bm_get_resource_id_string(2);
560 resourceid_usage_s *res_use =
561 bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
562 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
563 _ERR("For resourceid %s", rid_str);
566 usage = res_use->usage;
567 float usagemah = (float)usage/3600;
568 bd_gl_cpu_stat_s *gcpu_stat = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
569 BM_CHECK_MEM_ALLOC(gcpu_stat, {BM_FREE(res_use); });
570 ret = bm_server_battery_dump_query_from_db(gcpu_stat, 7, duration);
571 if (ret != BATTERY_MONITOR_ERROR_NONE) {
572 _ERR("Unable to fetch data from DB");
578 GString *dump_p = g_string_sized_new(30);
579 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gcpu_stat); BM_FREE(res_use); });
580 g_string_append(dump_p, TIZEN_DUMP_VERSION);
581 g_string_append(dump_p, ",0,l,");
582 g_string_append(dump_p, "cpu,");
583 g_string_append_printf(dump_p, "%ld,", gcpu_stat->usr_time);
584 g_string_append_printf(dump_p, "%ld,", gcpu_stat->sys_time);
585 g_string_append_printf(dump_p, "%0.2f", usagemah);
586 g_string_append(dump_p, "\n");
587 bm_resourceid_usage_free(res_use);
588 _DBG("%s", dump_p->str);
589 if (write(fd, dump_p->str, dump_p->len) < 0)
590 _WARN("write error");
592 g_string_free(dump_p, TRUE);
599 static int bd_get_and_print_uid_info_entry_from_dump(int fd)
603 int ret = BATTERY_MONITOR_ERROR_NONE;
604 GSList *list = bm_server_query_appid_map(&ret);
606 _ERR("NO LIST DATA FOUND");
609 app_map = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
610 GSList *iter = NULL; GString *dump_p = NULL;
611 dump_p = g_string_sized_new(30);
612 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
613 g_string_append(dump_p, TIZEN_DUMP_VERSION);
614 g_string_append(dump_p, ",0,i,uid,1000,tizen\n");
615 if (write(fd, dump_p->str, dump_p->len) < 0)
616 _WARN("write error");
617 g_string_free(dump_p, TRUE);
619 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
620 dump_p = g_string_sized_new(30);
621 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
622 g_string_append(dump_p, TIZEN_DUMP_VERSION);
623 g_string_append(dump_p, ",0");
624 g_string_append(dump_p, ",i");
625 g_string_append(dump_p, ",uid,");
626 appid_map_s *amap_data = (appid_map_s *)iter->data;
627 g_string_append_printf(dump_p, "%d,", (amap_data->id+10000));
628 gchar *temp_str = g_strdup(amap_data->AppId);
629 g_string_append(dump_p, amap_data->AppId);
630 g_string_append(dump_p, "\n");
631 _DBG("%s", dump_p->str);//write
632 if (write(fd, dump_p->str, dump_p->len) < 0)
633 _WARN("write error");
634 int *temp_id = (int *)malloc(4);
635 *temp_id = (amap_data->id + 10000);
636 _DBG("Insert id %d, str %s, res %d", *temp_id, temp_str,
637 g_hash_table_insert(app_map, temp_id, temp_str));
638 g_string_free(dump_p, TRUE);
640 bm_set_free_gslist_appid_map(list);
646 static int bd_get_history_detail_from_dump(int index, history_item_s* dump_node)
649 BM_CHECK_INPUT_PARAM(dump_node);
651 if (h_count >= HISTORY_SIZE_MAX || index > h_count) {
652 _ERR("Some Error occured with global index");
653 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
656 _DBG("Value of index %d", index);
657 _DBG("Value of h_count %d", h_count);
659 int dvar1 = 0, dvar2 = 0;
660 dvar1 = history_data[index].dump_1;
661 dvar2 = history_data[index].dump_2;
663 _DBG("Value of encoded value 1 %d", dvar1);
664 _DBG("Value of encoded value 2 %d", dvar2);
666 dump_node->cmd_s = ((dvar1) & 0xff);
667 _DBG("cmd_s %d", dump_node->cmd_s);
669 dump_node->battery_level = ((dvar1>>8) & 0xff);
670 _DBG("battery_level %d", dump_node->battery_level);
672 dump_node->battery_status = ((dvar1>>16) & 0xf);
673 _DBG("battery_status %d", dump_node->battery_status);
675 dump_node->battery_health = ((dvar1>>20) & 0xf);
676 _DBG("battery_health %d", dump_node->battery_health);
678 dump_node->battery_plugtype = ((dvar1>>24) & 0xf);
679 _DBG("battery_plugtype %d", dump_node->battery_plugtype);
681 dump_node->battery_temperature = ((dvar2) & 0xffff);
682 _DBG("battery_temperature %d", dump_node->battery_temperature);
684 dump_node->battery_voltage = ((dvar2>>16) & 0xffff);
685 _DBG("battery_voltage %d", dump_node->battery_voltage);
687 dump_node->battery_charge = history_data[index].battery_chargeUAh;
688 _DBG("battery_charge %d", dump_node->battery_charge);
690 dump_node->event_code = history_data[index].event_code;
691 _DBG("eventcode %d", dump_node->event_code);
693 dump_node->state_1 = history_data[index].state_1;
694 _DBG("state_1 %d", dump_node->state_1);
696 dump_node->state_2 = history_data[index].state_2;
697 _DBG("state_2 %d", dump_node->state_2);
699 dump_node->event_tag = history_data[index].event_tag;
700 if (dump_node->event_tag == NULL)
701 _DBG("Event_Tag_NULL");
703 _DBG("%d %d %s", dump_node->event_tag->uid, dump_node->event_tag->sp_idx,
704 dump_node->event_tag->string_info->str);
705 dump_node->wakelock_tag = history_data[index].wakelock_tag;
706 if (dump_node->wakelock_tag == NULL)
707 _DBG("Wakelock_Tag_NULL");
708 dump_node->wakereason_tag = history_data[index].wakereason_tag;
710 if (dump_node->wakereason_tag == NULL)
711 _DBG("Wakereason_Tag_NULL");
713 dump_node->time_s = history_data[index].time_s;
714 _DBG("TIME_S is %lld", dump_node->time_s);
716 dump_node->time_current = history_data[index].time_current;
717 _DBG("C TIME is %lld", dump_node->time_current);
719 dump_node->usage_type = history_data[index].usage_type;
720 _DBG("Usage type is %d", dump_node->usage_type);
722 dump_node->usage = history_data[index].usage;
723 _DBG("Usage is %d", dump_node->usage);
726 return BATTERY_MONITOR_ERROR_NONE;
729 int bd_print_history_item_reset(history_item_s* old_state)
732 BM_CHECK_INPUT_PARAM(old_state);
734 old_state->time_s = -1;
735 old_state->state_1 = 0;
736 old_state->state_2 = 0;
737 old_state->usage_type = 0;
738 old_state->usage = 0;
739 old_state->battery_level = -1;
740 old_state->battery_status = -1;
741 old_state->battery_health = -1;
742 old_state->battery_plugtype = -1;
743 old_state->battery_temperature = -1;
744 old_state->battery_voltage = -1;
745 old_state->battery_charge = -1;
746 old_state->event_code = 0;
747 old_state->event_tag = NULL;
748 old_state->wakelock_tag = NULL;
749 old_state->wakereason_tag = NULL;
752 return BATTERY_MONITOR_ERROR_NONE;
755 int bd_get_base_time_from_dump(long long *base_time)
759 *base_time = history_data[0].time_s;
762 return BATTERY_MONITOR_ERROR_NONE;
765 int bd_print_history_item_main(int fd, int num_h_items, long long base_time, bool chkin)
771 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
774 if (h_count >= HISTORY_SIZE_MAX) {
775 _ERR("Some Error occured with global index");
776 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
779 int ret = BATTERY_MONITOR_ERROR_NONE;
780 h_flag = false; //Lock
782 history_item_s new_state;
783 history_item_s old_state;
785 ret = bd_print_history_item_reset(&old_state);
786 if (ret != BATTERY_MONITOR_ERROR_NONE) {
787 _ERR("Old State reset failed");
791 ret = bd_get_pool_entry_from_dump(fd);
792 if (ret != BATTERY_MONITOR_ERROR_NONE)
793 _ERR("S pool data dump failed");
795 ret = bd_get_base_time_from_dump(&base_time);
796 if (ret != BATTERY_MONITOR_ERROR_NONE)
797 _ERR("Base time error");
799 int log_count = h_count; GString *dump_p = NULL;
800 for (int i = 0; i < log_count; i++) {
801 ret = bd_get_history_detail_from_dump(i, &new_state);
802 if (ret != BATTERY_MONITOR_ERROR_NONE) {
803 _ERR("Decode Failed");
806 dump_p = g_string_sized_new(30);
807 BM_CHECK_MEM_ALLOC(dump_p, {});
809 g_string_append(dump_p, TIZEN_DUMP_VERSION);
810 g_string_append_c(dump_p, ',');
811 _DBG("value %s", dump_p->str);
812 g_string_append(dump_p, DUMP_DATA_TAG);
813 g_string_append_c(dump_p, ',');
814 _DBG("value %s", dump_p->str);
815 if (old_state.time_s < 0) {
816 g_string_append_printf(dump_p, "%lld",
817 (new_state.time_s - base_time));
818 _DBG("value %s", dump_p->str);
820 g_string_append_printf(dump_p, "%lld",
821 (new_state.time_s - old_state.time_s));
822 _DBG("value %s", dump_p->str);
824 old_state.time_s = new_state.time_s;
825 _DBG("Value old_state.time_s %lld", old_state.time_s);
826 } else { //: Let's skip the NC mode in initial version
828 if (new_state.cmd_s == CM_ST) {
830 g_string_append_c(dump_p, ':');
831 _DBG("value %s", dump_p->str);
835 g_string_append(dump_p, "START\n");
836 _DBG("value %s", dump_p->str);
837 bd_print_history_item_reset(&old_state);
838 } else if (new_state.cmd_s == CM_CRR_TIME || new_state.cmd_s == CM_RST) {
840 g_string_append_c(dump_p, ':');
841 _DBG("value %s", dump_p->str);
843 if (new_state.cmd_s == CM_RST) {
844 g_string_append(dump_p, "RESET:");
845 _DBG("value %s", dump_p->str);
846 bd_print_history_item_reset(&old_state);
848 g_string_append(dump_p, "TIME:");
849 _DBG("value %s", dump_p->str);
851 g_string_append_printf(dump_p, "%lld", new_state.time_current);
852 _DBG("value %s", dump_p->str);
853 g_string_append(dump_p, "\n");
854 _DBG("value %s", dump_p->str);
858 } else if (new_state.cmd_s == CM_SHTDN) {
860 g_string_append_c(dump_p, ':');
861 _DBG("value %s", dump_p->str);
865 g_string_append(dump_p, "SHUTDOWN\n");
866 _DBG("value %s", dump_p->str);
867 } else if (new_state.cmd_s == CM_OVRFL) {
869 g_string_append_c(dump_p, ':');
870 _DBG("value %s", dump_p->str);
872 g_string_append(dump_p, "*OVERFLOW*\n");
873 _DBG("value %s", dump_p->str);
874 } else if (new_state.cmd_s == CM_USAGE) {
875 for (int i = 0; i < 32; i++) {
876 if (new_state.usage_type == (1 << i)) {
877 g_string_append(dump_p, ",");
878 g_string_append_printf(dump_p, "%s", usage_map[i]);
879 g_string_append(dump_p, "=");
880 g_string_append_printf(dump_p, "%d", new_state.usage);
881 g_string_append(dump_p, "\n");
882 _DBG("value %d", new_state.usage);
889 if (old_state.battery_level != new_state.battery_level) {
890 old_state.battery_level = new_state.battery_level;
891 g_string_append(dump_p, ",Bl=");
892 _DBG("value %s", dump_p->str);
893 g_string_append_printf(dump_p, "%d",
894 new_state.battery_level);
895 _DBG("value %s", dump_p->str);
898 if (old_state.battery_status != new_state.battery_status) {
899 old_state.battery_status = new_state.battery_status;
900 g_string_append(dump_p, ",Bs="); //:for NC
901 _DBG("value %s", dump_p->str);
902 switch (old_state.battery_status) {
904 g_string_append_c(dump_p, '?');
907 g_string_append_c(dump_p, 'c');
910 g_string_append_c(dump_p, 'd');
913 g_string_append_c(dump_p, 'n');
916 g_string_append_c(dump_p, 'f');
919 g_string_append_c(dump_p, '?');
920 _DBG("value %s", dump_p->str);
924 if (old_state.battery_health != new_state.battery_health) {
925 old_state.battery_health = new_state.battery_health;
926 g_string_append(dump_p, ",Bh="); //:for NC
927 _DBG("value %s", dump_p->str);
928 switch (old_state.battery_health) {
930 g_string_append_c(dump_p, 'g');
933 g_string_append_c(dump_p, '?');
936 g_string_append_c(dump_p, 'h');
939 g_string_append_c(dump_p, 'd');
942 g_string_append_c(dump_p, 'v');
945 g_string_append_c(dump_p, 'f');
948 g_string_append_c(dump_p, 'c');
951 g_string_append_c(dump_p, '?');
952 _DBG("value %s", dump_p->str);
956 if (old_state.battery_plugtype != new_state.battery_plugtype) {
957 old_state.battery_plugtype = new_state.battery_plugtype;
958 g_string_append(dump_p, ",Bp="); //:for NC
959 _DBG("value %s", dump_p->str);
960 switch (old_state.battery_plugtype) {
962 g_string_append_c(dump_p, 'n');
965 g_string_append_c(dump_p, 'a');
968 g_string_append_c(dump_p, 'u');
971 g_string_append_c(dump_p, 'w');
974 g_string_append_c(dump_p, 'n');
975 _DBG("value %s", dump_p->str);
979 if (old_state.battery_temperature != new_state.battery_temperature) {
980 old_state.battery_temperature = new_state.battery_temperature;
981 g_string_append(dump_p, ",Bt="); //:for NC
982 g_string_append_printf(dump_p, "%d",
983 new_state.battery_temperature);
984 _DBG("value %s", dump_p->str);
986 if (old_state.battery_voltage != new_state.battery_voltage) {
987 old_state.battery_voltage = new_state.battery_voltage;
988 g_string_append(dump_p, ",Bv="); //:for NC
989 _DBG("value %s", dump_p->str);
990 g_string_append_printf(dump_p, "%d", new_state.battery_voltage);
991 _DBG("value %s", dump_p->str);
993 if (old_state.battery_charge != new_state.battery_charge) {
994 old_state.battery_charge = new_state.battery_charge;
995 g_string_append(dump_p, ",Bcc=");
996 _DBG("value %s", dump_p->str);
998 g_string_append_printf(dump_p, "%d", (new_state.battery_charge/1000));
999 _DBG("value %s", dump_p->str);
1001 int change = (old_state.state_1 ^ new_state.state_1);
1003 bool wake_fl = false; int k = 0;
1004 if ((change & ST1_BRIGHTNESS_MASK) != 0) {
1005 k = (new_state.state_1 & ST1_BRIGHTNESS_MASK);
1006 g_string_append(dump_p, ",Sb=");
1007 k = k >> ST1_BRIGHTNESS_SHIFT;
1009 g_string_append_printf(dump_p, "%d", k);
1012 if ((change & ST1_PHONE_SIGNAL_STRENGTH_MASK) != 0) {
1013 k = (new_state.state_1 & ST1_PHONE_SIGNAL_STRENGTH_MASK);
1014 g_string_append(dump_p, ",Pss=");
1015 k = k >> ST1_PHONE_SIGNAL_STRENGTH_SHIFT;
1016 _DBG("Pss = %d", k);
1017 g_string_append_printf(dump_p, "%d", k);
1020 int shifts = (1 << 14);
1021 for (int i = 14; i < 32; i++) {
1022 if ((change & shifts) != 0) {
1023 g_string_append_c(dump_p, ',');
1024 //Considering only -ve shift cases
1027 if ((new_state.state_1 & (1 << i)) != 0)
1028 g_string_append_c(dump_p, '+');
1030 g_string_append_c(dump_p, '-');
1031 gchar *temp = g_strdup(state1_map[i]);
1032 g_string_append(dump_p, temp);
1034 //_DBG("value %s", dump_p->str);
1035 if ((1 << i) == ST1_WAKE_LOCK_FLAG &&
1036 new_state.wakelock_tag != NULL) {
1038 g_string_append_c(dump_p, '=');
1039 //_DBG("value %s", dump_p->str);
1041 g_string_append_printf(dump_p,
1042 "%d", new_state.wakelock_tag->sp_idx);
1046 //_DBG("value %s", dump_p->str);
1049 //With non-negative shift
1052 shifts = (shifts << 1);
1054 if (!wake_fl && new_state.wakelock_tag != NULL) {
1055 g_string_append(dump_p, ",w=");
1057 g_string_append_printf(dump_p, "%d",
1058 new_state.wakelock_tag->sp_idx);
1059 _DBG("value %s", dump_p->str);
1064 change = (old_state.state_2 ^ new_state.state_2);
1067 for (int i = 0; i < 32; i++) {
1068 if ((change & shifts) != 0) {
1069 g_string_append_c(dump_p, ',');
1070 //_DBG("value %s", dump_p->str);
1073 if ((new_state.state_2 & (1 << i) & change) != 0)
1074 g_string_append_c(dump_p, '+');
1076 g_string_append_c(dump_p, '-');
1077 //_DBG("value %s", dump_p->str);
1078 gchar *temp = g_strdup(state2_map[i]);
1079 g_string_append(dump_p, temp);
1081 _DBG("value %s", dump_p->str);
1084 shifts = (shifts << 1);
1087 if (new_state.wakereason_tag != NULL) {
1089 g_string_append(dump_p, ",wr=");
1090 _DBG("value %s", dump_p->str);
1091 g_string_append_printf(dump_p, "%d",
1092 new_state.wakereason_tag->sp_idx);
1093 _DBG("value %s", dump_p->str);
1098 if (new_state.event_code != ET_NONE) {
1099 g_string_append_c(dump_p, ',');
1100 _DBG("value %s", dump_p->str);
1101 if ((new_state.event_code & ET_FLAG_START) != 0)
1102 g_string_append_c(dump_p, '+');
1103 else if ((new_state.event_code & ET_FLAG_FINISH) != 0)
1104 g_string_append_c(dump_p, '-');
1105 _DBG("value %s", dump_p->str);
1106 int k = new_state.event_code & ~(ET_FLAG_START | ET_FLAG_FINISH);
1107 if (k >= 0 && k < 32) {
1108 gchar* temp = g_strdup(event_map[k]);
1109 g_string_append(dump_p, temp);
1110 _DBG("value %s", dump_p->str);
1113 g_string_append(dump_p, "Ev");
1114 g_string_append_printf(dump_p, "%d", k);
1115 _DBG("value %s", dump_p->str);
1117 g_string_append_c(dump_p, '=');
1118 _DBG("value %s", dump_p->str);
1120 g_string_append_printf(dump_p, "%d",
1121 new_state.event_tag->sp_idx);
1127 g_string_append(dump_p, "\n");
1129 old_state.state_1 = new_state.state_1;
1130 old_state.state_2 = new_state.state_2;
1132 _DBG("State assigned");
1133 _DBG("%s", dump_p->str);
1134 if (write(fd, dump_p->str, dump_p->len) < 0)
1135 _WARN("write error");
1136 g_string_free(dump_p, TRUE);
1139 // Dump the elapsed time from last event
1140 // It should be dumped without storing
1141 GString *dump_tmp_p = g_string_sized_new(30);
1143 gettimeofday(&tv, NULL);
1145 g_string_append(dump_tmp_p, TIZEN_DUMP_VERSION);
1146 g_string_append_c(dump_tmp_p, ',');
1147 g_string_append(dump_tmp_p, DUMP_DATA_TAG);
1148 g_string_append_c(dump_tmp_p, ',');
1149 g_string_append_printf(dump_tmp_p, "%lld",
1150 ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000) - old_state.time_s);
1151 g_string_append(dump_tmp_p, "\n");
1153 if (write(fd, dump_tmp_p->str, dump_tmp_p->len) < 0)
1154 _WARN("write error");
1155 g_string_free(dump_p, TRUE);
1157 //Dump i & l values after this
1158 ret = bd_get_and_print_uid_info_entry_from_dump(fd);
1159 if (ret != BATTERY_MONITOR_ERROR_NONE)
1160 _ERR("UID data dump failed");
1161 // Get Last charge time to dump "l" field
1162 // for the time being dumping all data
1163 long int duration = 0; //Time of last charge
1164 ret = bd_get_and_print_gn_stat_entry_from_dump(fd, duration);
1165 if (ret != BATTERY_MONITOR_ERROR_NONE)
1166 _ERR("GN dump failed");
1167 ret = bd_get_and_print_gwf_stat_entry_from_dump(fd, duration);
1168 if (ret != BATTERY_MONITOR_ERROR_NONE)
1169 _ERR("GWF data dump failed");
1170 ret = bd_get_and_print_gble_stat_entry_from_dump(fd, duration);
1171 if (ret != BATTERY_MONITOR_ERROR_NONE)
1172 _ERR("GBL data dump failed");
1173 ret = bd_get_and_print_gbr_stat_entry_from_dump(fd, duration);
1174 if (ret != BATTERY_MONITOR_ERROR_NONE)
1175 _ERR("GBR data dump failed");
1176 ret = bd_get_and_print_gst_stat_entry_from_dump(fd, duration);
1177 if (ret != BATTERY_MONITOR_ERROR_NONE)
1178 _ERR("GST data dump failed");
1179 ret = bd_get_and_print_gwst_stat_entry_from_dump(fd, duration);
1180 if (ret != BATTERY_MONITOR_ERROR_NONE)
1181 _ERR("GWST data dump failed");
1182 ret = bd_get_and_print_gcpu_stat_entry_from_dump(fd, duration);
1183 if (ret != BATTERY_MONITOR_ERROR_NONE)
1184 _ERR("GCPU data dump failed");
1185 ret = bd_get_and_print_misc_stat_entry_from_dump(fd, duration);
1186 if (ret != BATTERY_MONITOR_ERROR_NONE)
1187 _ERR("MISc data dump failed");
1188 // Battery Usage Dump
1189 // Add usage for each of the resource
1190 // and each of the application
1191 ret = bd_get_and_print_battery_usage_from_dump(fd, duration);
1192 if (ret != BATTERY_MONITOR_ERROR_NONE)
1193 _ERR("Battery Usage failed");
1195 _DBG("DUMP COMPLETED/DELETE HASH TABLE");
1197 g_hash_table_destroy(app_map);
1201 return BATTERY_MONITOR_ERROR_NONE;
1204 static int bd_dump_data_item_reset(dump_data_s* old_state)
1207 BM_CHECK_INPUT_PARAM(old_state);
1209 old_state->time_s = -1;
1210 old_state->time_current = -1;
1211 old_state->dump_1 = 0;
1212 old_state->dump_2 = 0;
1213 old_state->state_1 = 0;
1214 old_state->state_2 = 0;
1215 old_state->battery_chargeUAh = 0;
1216 old_state->usage_type = 0;
1217 old_state->usage = 0;
1218 old_state->event_code = ET_NONE;
1219 old_state->event_tag = NULL;
1220 old_state->wakelock_tag = NULL;
1221 old_state->wakereason_tag = NULL;
1224 return BATTERY_MONITOR_ERROR_NONE;
1227 static int bd_free_history_data_memory()
1231 int ret = BATTERY_MONITOR_ERROR_NONE;
1232 h_flag = false; //Lock
1233 _DBG("Free History Data Memory");
1234 for (int i = 0; i < h_count; i++) {
1235 if (history_data[i].wakelock_tag != NULL) {
1237 g_string_free(history_data[i].wakelock_tag->string_info, TRUE);
1239 free(history_data[i].wakelock_tag);
1241 if (history_data[i].wakereason_tag != NULL) {
1243 g_string_free(history_data[i].wakereason_tag->string_info, TRUE);
1245 free(history_data[i].wakereason_tag);
1247 if (history_data[i].event_tag != NULL) {
1249 g_string_free(history_data[i].event_tag->string_info, TRUE);
1251 free(history_data[i].event_tag);
1253 ret = bd_dump_data_item_reset(&history_data[i]);
1254 if (ret != BATTERY_MONITOR_ERROR_NONE)
1255 _ERR("Internal error %d while freeing at idx %d", ret , i);
1258 h_count = 0; h_flag = true;
1263 static int bd_set_history_from_listener(history_item_s* nrec)
1266 BM_CHECK_INPUT_PARAM(nrec);
1268 int dvar1 = 0, dvar2 = 0;
1270 gettimeofday(&tv, NULL);
1272 time_t current_time = tv.tv_sec;
1275 history_data[h_count].time_s = nrec->time_s;
1277 history_data[h_count].time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1280 dvar1 = ((nrec->cmd_s) & 0xff) | ((nrec->battery_level << 8) & 0xff00) |
1281 ((nrec->battery_status << 16) & 0xf0000) |
1282 ((nrec->battery_health << 20) & 0xf00000) |
1283 ((nrec->battery_plugtype << 24) & 0xf000000) |
1284 ((nrec->wakelock_tag != NULL) ? 0x10000000 : 0) |
1285 ((nrec->wakereason_tag != NULL) ? 0x20000000 : 0) |
1286 ((nrec->event_code != ET_NONE) ? 0x40000000 : 0);
1288 dvar2 = ((nrec->battery_temperature) & 0xffff) |
1289 ((nrec->battery_voltage << 16) & 0xffff0000);
1291 _DBG("Value of encoded value 1 %d", dvar1);
1292 _DBG("Value of encoded value 2 %d", dvar2);
1294 history_data[h_count].dump_1 = dvar1;
1295 history_data[h_count].dump_2 = dvar2;
1296 history_data[h_count].battery_chargeUAh = nrec->battery_charge;
1298 if (nrec->wakelock_tag != NULL)
1299 history_data[h_count].wakelock_tag = nrec->wakelock_tag;
1300 if (nrec->wakereason_tag != NULL)
1301 history_data[h_count].wakereason_tag = nrec->wakereason_tag;
1302 if (nrec->event_code != ET_NONE) {
1303 if (nrec->event_tag == NULL) {
1304 _ERR("ET is NULL but Event Code is not");
1305 BM_FREE(history_data[h_count].wakelock_tag);
1306 BM_FREE(history_data[h_count].wakereason_tag);
1307 bd_dump_data_item_reset(&history_data[h_count]);
1308 return BATTERY_MONITOR_ERROR_NO_DATA;
1310 history_data[h_count].event_code = nrec->event_code;
1311 history_data[h_count].event_tag = nrec->event_tag;
1313 if (nrec->cmd_s == CM_CRR_TIME || nrec->cmd_s == CM_RST)
1314 history_data[h_count].time_current = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1316 history_data[h_count].state_1 = nrec->state_1;
1317 history_data[h_count].state_2 = nrec->state_2;
1319 history_data[h_count].usage_type = nrec->usage_type;
1320 history_data[h_count].usage = nrec->usage;
1322 nrec->event_tag = NULL; nrec->wakereason_tag = NULL;
1323 nrec->wakelock_tag = NULL;
1324 if (h_count < HISTORY_SIZE_MAX - 1)
1327 // FIXME modify 1st argument (fd)
1328 int ret = bd_print_history_item_main(-1, 0, current_time, 1);
1329 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1333 ret = bd_free_history_data_memory();
1334 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1335 _WARN("Error while memory free %x", ret);
1341 return BATTERY_MONITOR_ERROR_NONE;
1344 int bd_store_history_item(history_item_s* nrec)
1347 BM_CHECK_INPUT_PARAM(nrec);
1350 _ERR("Data Dump in progress");
1351 return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
1353 int ret = BATTERY_MONITOR_ERROR_NONE;
1354 ret = bd_set_history_from_listener(nrec);
1355 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1356 _ERR("Failed to store history");
1357 return BATTERY_MONITOR_ERROR_INTERNAL;
1361 return BATTERY_MONITOR_ERROR_NONE;
1364 static int *dumpsys_handler;
1366 int bd_callback(const int fd, const int argc, char **argv)
1370 bool checkin = false;
1373 * We must reset optind because it isn't initialized
1374 * on subsequent getopt calls
1377 while ((opt = getopt(argc, argv, "c")) != -1) {
1388 ret = bd_print_history_item_main(fd, -1, 0, checkin);
1389 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1390 _ERR("bd_print_history_item_main failed (%x)", ret);
1397 int bd_initialize_battery_dump()
1401 int ret = BATTERY_MONITOR_ERROR_NONE;
1403 h_count = 0; h_flag = true;
1409 ret = bd_print_history_item_reset(&ts);
1410 if (ret == BATTERY_MONITOR_ERROR_NONE) {
1411 ts.cmd_s = CM_CRR_TIME;
1412 ret = bd_store_history_item(&ts);
1413 if (ret != BATTERY_MONITOR_ERROR_NONE)
1414 _WARN("Init Node addition failed");
1418 ret = dumpsys_system_register_dump_cb(bd_callback, "batterystats", (void **)&dumpsys_handler);
1419 if (ret != TIZEN_ERROR_NONE) {
1420 _ERR("dumpsys_system_register_dump_cb failed (%x)", ret);
1421 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1424 _INFO("Battery Dump Init Successful");
1427 return BATTERY_MONITOR_ERROR_NONE;
1430 int bd_deinitialize_battery_dump()
1436 ret = dumpsys_system_unregister_dump_cb(dumpsys_handler);
1437 if (ret != TIZEN_ERROR_NONE) {
1438 _WARN("dumpsys_system_unregister_dump_cb failed (%x)", ret);
1439 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1442 ret = bd_free_history_data_memory();
1443 if (ret != BATTERY_MONITOR_ERROR_NONE)
1444 _WARN("memory free failed (%x)", ret);
1446 _INFO("Battery Dump De-Init Successful");
1449 return BATTERY_MONITOR_ERROR_NONE;