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"
33 //#define DUMP_DUMMY 1
35 #include "bd_history_item_dummy.h"
38 dump_data_s history_data[HISTORY_SIZE_MAX];
39 int h_count = 0; int h_flag = false;
40 char state1_map[32][4] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Wm",
41 "", "Sd", "BP", "S", "Psc", "a", "s", "", "Pr", "Wr", "Ws", "Wl", "g", "w", "r"};
42 char state2_map[32][5] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
43 "", "Ud", "Chtp", "bles", "ca", "b", "Pcl", "ch", "", "", "fl", "W", "Ww", "v", "ps"};
44 char event_map[32][4] = {"Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
45 "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw", "Esw", "Ewa", "Elw", "Eec"};
46 char usage_map[32][4] = {"Bu", "Wu", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
47 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
48 GHashTable *app_map = NULL;
50 static int bd_print_pool_entry(int fd, int idx, history_tag_s *pool_info)
53 BM_CHECK_INPUT_PARAM(pool_info);
54 _DBG("Value from history data %d", idx);
56 GString *dump_p = g_string_sized_new(30);
57 BM_CHECK_MEM_ALLOC(dump_p, {});
59 g_string_append(dump_p, TIZEN_DUMP_VERSION);
60 g_string_append_c(dump_p, ',');
61 g_string_append(dump_p, DUMP_DATA_TAG_1);
62 g_string_append_c(dump_p, ',');
63 g_string_append_printf(dump_p, "%d,", pool_info->sp_idx);
64 g_string_append_printf(dump_p, "%d,", pool_info->uid);
65 g_string_append(dump_p, "\"");
67 if (pool_info->string_info != NULL) {
68 gchar *temp = g_strdup(pool_info->string_info->str);
69 g_string_append(dump_p, temp);
73 g_string_append(dump_p, "\"");
74 g_string_append(dump_p, "\n");
75 _DBG("%s", dump_p->str);
76 if (write(fd, dump_p->str, dump_p->len) < 0)
81 return BATTERY_MONITOR_ERROR_NONE;
84 static int bd_get_pool_entry_from_dump(int fd)
88 GString *dump_p = g_string_sized_new(30);
89 BM_CHECK_MEM_ALLOC(dump_p, {});
90 g_string_append(dump_p, TIZEN_DUMP_VERSION);
91 g_string_append(dump_p, ",0");
92 g_string_append(dump_p, ",i");
93 g_string_append(dump_p, ",vers");
94 g_string_append(dump_p, ",19,150,NMF26V,NMF26V");
95 g_string_append(dump_p, "\n");
96 _DBG("%s", dump_p->str);
97 if (write(fd, dump_p->str, dump_p->len) < 0)
101 int ret = BATTERY_MONITOR_ERROR_NONE;
102 int log_count = h_count; int pool_count = 0;
103 for (int i = 0; i < log_count; i++) {
104 history_tag_s *pool_info = NULL;
105 if (history_data[i].event_tag != NULL) {
106 pool_info = history_data[i].event_tag;
107 if (pool_count == pool_info->sp_idx) {
108 ret = bd_print_pool_entry(fd, i, pool_info);
110 if (ret != BATTERY_MONITOR_ERROR_NONE)
111 return BATTERY_MONITOR_ERROR_INTERNAL;
114 if (history_data[i].wakelock_tag != NULL) {
115 pool_info = history_data[i].wakelock_tag;
116 if (pool_count == pool_info->sp_idx) {
117 ret = bd_print_pool_entry(fd, i, pool_info);
119 if (ret != BATTERY_MONITOR_ERROR_NONE)
120 return BATTERY_MONITOR_ERROR_INTERNAL;
123 if (history_data[i].wakereason_tag != NULL) {
124 pool_info = history_data[i].wakereason_tag;
125 if (pool_count == pool_info->sp_idx) {
126 ret = bd_print_pool_entry(fd, i, pool_info);
128 if (ret != BATTERY_MONITOR_ERROR_NONE)
129 return BATTERY_MONITOR_ERROR_INTERNAL;
135 return BATTERY_MONITOR_ERROR_NONE;
138 /* Depricated till there are no separate UID related events
139 static int bd_print_uid_entry(int idx, history_tag_s *pool_info)
142 BM_CHECK_INPUT_PARAM(pool_info);
143 GString *dump_p = g_string_sized_new(30);
144 BM_CHECK_MEM_ALLOC(dump_p, {});
145 g_string_append_printf(dump_p, "%d,", pool_info->uid);
146 if (pool_info->string_info != NULL) {
147 gchar *temp = g_strdup(pool_info->string_info->str);
148 g_string_append(dump_p, temp);
151 g_string_append(dump_p, "\n");
152 _DBG("%s", dump_p->str);//write
155 return BATTERY_MONITOR_ERROR_NONE;
158 static int bd_get_uid_entry_from_dump(void)
161 // should be maintained at listeners for uniqueness
162 // move there and modify
163 GString *dump_p = g_string_sized_new(30);
164 BM_CHECK_MEM_ALLOC(dump_p, {});
165 g_string_append(dump_p, TIZEN_DUMP_VERSION);
166 g_string_append(dump_p, ",0");
167 g_string_append(dump_p, ",i");
168 g_string_append(dump_p, ",uid,");
169 int ret = BATTERY_MONITOR_ERROR_NONE;
170 int log_count = h_count;
171 for (int i = 0; i < log_count; i++) {
172 history_tag_s *pool_info = NULL;
173 if (history_data[i].event_tag != NULL) {
174 pool_info = history_data[i].event_tag;
175 _DBG("%s", dump_p->str);//write
176 ret = bd_print_uid_entry(i, pool_info);
177 if (ret != BATTERY_MONITOR_ERROR_NONE)
178 return BATTERY_MONITOR_ERROR_INTERNAL;
180 if (history_data[i].wakelock_tag != NULL) {
181 pool_info = history_data[i].wakelock_tag;
182 _DBG("%s", dump_p->str);//write
183 ret = bd_print_uid_entry(i, pool_info);
184 if (ret != BATTERY_MONITOR_ERROR_NONE)
185 return BATTERY_MONITOR_ERROR_INTERNAL;
187 if (history_data[i].wakereason_tag != NULL) {
188 pool_info = history_data[i].wakereason_tag;
189 _DBG("%s", dump_p->str);//write
190 ret = bd_print_uid_entry(i, pool_info);
191 if (ret != BATTERY_MONITOR_ERROR_NONE)
192 return BATTERY_MONITOR_ERROR_INTERNAL;
197 return BATTERY_MONITOR_ERROR_NONE;
201 static int bd_get_and_print_battery_usage_from_dump(int fd, long int duration)
204 int ret = BATTERY_MONITOR_ERROR_NONE;
205 // Application usage;
207 gpointer key, value; GString *dump_p = NULL;
208 char *appid = NULL; int *uid_n = NULL;
210 g_hash_table_iter_init(&iter, app_map);
211 while (g_hash_table_iter_next(&iter, &key, &value)) {
212 uid_n = (int *)key; appid = (char *)value;
213 _DBG("%d %s", *uid_n, appid); usageMAh = 0;
214 // Currently giving app usage of last one week;
215 // Modify it wrt to time
216 appid_usage_s *app_use = bm_server_query_app_usage_by_appid(appid, 1, &ret);
217 if (app_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
218 _ERR("For appid %s", appid);
221 usageMAh = (app_use->rId_ble + app_use->rId_wifi + app_use->rId_cpu +
222 app_use->rId_display + app_use->rId_device_network +
223 app_use->rId_gps + app_use->rId_hrm);
224 // usage stored as mAs changed to mAh
225 usageMAh = (usageMAh/3600);
226 dump_p = g_string_sized_new(30);
227 BM_CHECK_MEM_ALLOC(dump_p, { BM_FREE(app_use); });
228 g_string_append(dump_p, TIZEN_DUMP_VERSION);
229 g_string_append_printf(dump_p, ",%d,l,pwi,uid,", *uid_n);
230 g_string_append_printf(dump_p, "%0.2f", usageMAh);
231 g_string_append(dump_p, "\n");
232 bm_appid_usage_free(app_use);
233 _DBG("%s", dump_p->str);
234 if (write(fd, dump_p->str, dump_p->len) < 0)
235 _WARN("write error");
239 const char *rid_str = NULL;
240 for (int i = 0; i < 6; i++) {
242 rid_str = bm_get_resource_id_string(i);
243 if (g_strcmp0(rid_str, "UNKNOWN RESOURCE-ID") == 0) {
244 _ERR("invalid resource-id");
245 return BATTERY_MONITOR_ERROR_INTERNAL;
247 resourceid_usage_s *res_use =
248 bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
249 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
250 _ERR("For resourceid %s", rid_str);
253 dump_p = g_string_sized_new(30);
254 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(res_use); });
255 usage = res_use->usage;
256 // usage stored as mAs changed to mAh
257 usageMAh = (float)usage/3600;
258 g_string_append(dump_p, TIZEN_DUMP_VERSION);
261 g_string_append(dump_p, ",0,l,pwi,blue,");
264 g_string_append(dump_p, ",0,l,pwi,wifi,");
267 g_string_append(dump_p, ",0,l,pwi,phone,");
270 g_string_append(dump_p, ",0,l,pwi,scrn,");
273 g_string_append(dump_p, ",0,l,pwi,cell,");
276 g_string_append(dump_p, ",0,l,pwi,unacc,");
279 g_string_append_printf(dump_p, "%0.2f", usageMAh);
280 g_string_append(dump_p, "\n");
281 bm_resourceid_usage_free(res_use);
282 _DBG("%s", dump_p->str);
283 if (write(fd, dump_p->str, dump_p->len) < 0)
284 _WARN("write error");
292 static int bd_get_and_print_gbr_stat_entry_from_dump(int fd, long int duration)
295 // Dump brightness Data
296 bd_gl_sbr_stat_s *gbr_stat = (bd_gl_sbr_stat_s *)calloc(1, sizeof(bd_gl_sbr_stat_s));
297 BM_CHECK_MEM_ALLOC(gbr_stat, {});
299 int ret = BATTERY_MONITOR_ERROR_NONE;
300 ret = bm_server_battery_dump_query_from_db(gbr_stat, 3, duration);
301 if (ret != BATTERY_MONITOR_ERROR_NONE) {
302 _ERR("Unable to fetch data from DB");
307 GString *dump_p = g_string_sized_new(30);
308 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gbr_stat); });
309 g_string_append(dump_p, TIZEN_DUMP_VERSION);
310 g_string_append(dump_p, ",0,l,");
311 g_string_append(dump_p, "br,");
312 g_string_append_printf(dump_p, "%ld,", gbr_stat->dark); //OFF
313 g_string_append_printf(dump_p, "%ld,", gbr_stat->dim); // All are time values
314 g_string_append_printf(dump_p, "%ld,", gbr_stat->medium);
315 g_string_append_printf(dump_p, "%ld,", gbr_stat->light); //DEF: 0
316 g_string_append_printf(dump_p, "%ld", gbr_stat->bright);
317 g_string_append(dump_p, "\n");
318 _DBG("%s", dump_p->str);//write
319 if (write(fd, dump_p->str, dump_p->len) < 0)
320 _WARN("write error");
328 static int bd_get_and_print_gn_stat_entry_from_dump(int fd, long int duration)
332 bd_gl_ntw_stat_s *gn_stat = (bd_gl_ntw_stat_s *)calloc(1, sizeof(bd_gl_ntw_stat_s));
333 BM_CHECK_MEM_ALLOC(gn_stat, {});
335 int ret = BATTERY_MONITOR_ERROR_NONE;
336 ret = bm_server_battery_dump_query_from_db(gn_stat, 2, duration);
337 if (ret != BATTERY_MONITOR_ERROR_NONE) {
338 _ERR("Unable to fetch data from DB");
343 GString *dump_p = g_string_sized_new(30);
344 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gn_stat); });
345 g_string_append(dump_p, TIZEN_DUMP_VERSION);
346 g_string_append(dump_p, ",0,l,");
347 g_string_append(dump_p, "gn,");
348 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_rx_byt);
349 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_tx_byt);
350 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_rx_byt);
351 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_tx_byt);
352 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_rx_pkt);
353 g_string_append_printf(dump_p, "%ld,", gn_stat->dn_tx_pkt);
354 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_rx_pkt);
355 g_string_append_printf(dump_p, "%ld,", gn_stat->wifi_tx_pkt);
356 g_string_append_printf(dump_p, "%d,", 0); //add this to struct as well
357 g_string_append_printf(dump_p, "%d", 0);
358 g_string_append(dump_p, "\n");
359 _DBG("%s", dump_p->str);//write
360 if (write(fd, dump_p->str, dump_p->len) < 0)
361 _WARN("write error");
369 static int bd_get_and_print_gwf_stat_entry_from_dump(int fd, long int duration)
373 bd_gl_wifi_stat_s *gwfl_stat = (bd_gl_wifi_stat_s *)calloc(1, sizeof(bd_gl_wifi_stat_s));
374 BM_CHECK_MEM_ALLOC(gwfl_stat, {});
376 int ret = BATTERY_MONITOR_ERROR_NONE;
377 ret = bm_server_battery_dump_query_from_db(gwfl_stat, 1, duration);
378 if (ret != BATTERY_MONITOR_ERROR_NONE) {
379 _ERR("Unable to fetch data from DB");
384 GString *dump_p = g_string_sized_new(30);
385 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
386 g_string_append(dump_p, TIZEN_DUMP_VERSION);
387 g_string_append(dump_p, ",0,l,");
388 g_string_append(dump_p, "gwfl,");
389 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_on_time);
390 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_conn_time);
391 g_string_append(dump_p, "0,0,0");//legacy
393 g_string_append(dump_p, "\n");
394 _DBG("%s", dump_p->str);//write
395 if (write(fd, dump_p->str, dump_p->len) < 0)
396 _WARN("write error");
399 dump_p = g_string_sized_new(30);
400 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwfl_stat); });
401 g_string_append(dump_p, TIZEN_DUMP_VERSION);
402 g_string_append(dump_p, ",0,l,");
403 g_string_append(dump_p, "gwfcd,");
405 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_idle_time);
406 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_rx_time);
407 g_string_append_printf(dump_p, "%ld,", gwfl_stat->wifi_pwi_val);
408 g_string_append_printf(dump_p, "%ld", gwfl_stat->wifi_tx_time);
409 g_string_append(dump_p, "\n");
410 _DBG("%s", dump_p->str);//write
411 if (write(fd, dump_p->str, dump_p->len) < 0)
412 _WARN("write error");
420 static int bd_get_and_print_gble_stat_entry_from_dump(int fd, long int duration)
424 bd_gl_ble_stat_s *gble_stat = (bd_gl_ble_stat_s *)calloc(1, sizeof(bd_gl_ble_stat_s));
425 BM_CHECK_MEM_ALLOC(gble_stat, {});
427 int ret = BATTERY_MONITOR_ERROR_NONE;
428 ret = bm_server_battery_dump_query_from_db(gble_stat, 0, duration);
429 if (ret != BATTERY_MONITOR_ERROR_NONE) {
430 _ERR("Unable to fetch data from DB");
435 GString *dump_p = g_string_sized_new(30);
436 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gble_stat); });
437 g_string_append(dump_p, TIZEN_DUMP_VERSION);
438 g_string_append(dump_p, ",0,l,");
439 g_string_append(dump_p, "gble,");
440 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_idle_time);
441 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_rx_time);
442 g_string_append_printf(dump_p, "%ld,", gble_stat->ble_tx_time);
443 g_string_append_printf(dump_p, "%ld", gble_stat->ble_pwi_val);
444 g_string_append(dump_p, "\n");
446 _DBG("%s", dump_p->str);//write
447 if (write(fd, dump_p->str, dump_p->len) < 0)
448 _WARN("write error");
456 static int bd_get_and_print_gst_stat_entry_from_dump(int fd, long int duration)
459 bd_gl_sgt_stat_s *gst_stat = (bd_gl_sgt_stat_s *)calloc(1, sizeof(bd_gl_sgt_stat_s));
460 BM_CHECK_MEM_ALLOC(gst_stat, {});
462 int ret = BATTERY_MONITOR_ERROR_NONE;
463 ret = bm_server_battery_dump_query_from_db(gst_stat, 4, duration);
464 if (ret != BATTERY_MONITOR_ERROR_NONE) {
465 _ERR("Unable to fetch data from DB");
470 GString *dump_p = g_string_sized_new(30);
471 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
472 g_string_append(dump_p, TIZEN_DUMP_VERSION);
473 g_string_append(dump_p, ",0,l,");
474 g_string_append(dump_p, "sgt,");
475 g_string_append_printf(dump_p, "%ld,", gst_stat->none_time);
476 g_string_append_printf(dump_p, "%ld,", gst_stat->poor_time);
477 g_string_append_printf(dump_p, "%ld,", gst_stat->med_time);
478 g_string_append_printf(dump_p, "%ld,", gst_stat->good_time);
479 g_string_append_printf(dump_p, "%ld", gst_stat->grt_time);
480 g_string_append(dump_p, "\n");
482 _DBG("%s", dump_p->str);//write
483 if (write(fd, dump_p->str, dump_p->len) < 0)
484 _WARN("write error");
487 dump_p = g_string_sized_new(30);
488 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gst_stat); });
489 g_string_append(dump_p, TIZEN_DUMP_VERSION);
490 g_string_append(dump_p, ",0,l,");
491 g_string_append(dump_p, "sst,");
492 g_string_append_printf(dump_p, "%ld", gst_stat->scan_time);
494 g_string_append(dump_p, "\n");
495 _DBG("%s", dump_p->str);//write
496 if (write(fd, dump_p->str, dump_p->len) < 0)
497 _WARN("write error");
506 static int bd_get_and_print_gwst_stat_entry_from_dump(int fd, long int duration)
509 bd_gl_wst_stat_s *gwst_stat = (bd_gl_wst_stat_s *)calloc(1, sizeof(bd_gl_wst_stat_s));
510 BM_CHECK_MEM_ALLOC(gwst_stat, {});
512 int ret = BATTERY_MONITOR_ERROR_NONE;
513 ret = bm_server_battery_dump_query_from_db(gwst_stat, 5, duration);
514 if (ret != BATTERY_MONITOR_ERROR_NONE) {
515 _ERR("Unable to fetch data from DB");
520 GString *dump_p = g_string_sized_new(30);
521 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gwst_stat); });
522 g_string_append(dump_p, TIZEN_DUMP_VERSION);
523 g_string_append(dump_p, ",0,l,");
524 g_string_append(dump_p, "wsgt,");
525 // g_string_append_printf(dump_p, "%ld,", gwst_stat->wscan_time);
526 g_string_append_printf(dump_p, "%ld,", gwst_stat->wnone_time);
527 g_string_append_printf(dump_p, "%ld,", gwst_stat->wpoor_time);
528 g_string_append_printf(dump_p, "%ld,", gwst_stat->wmed_time);
529 g_string_append_printf(dump_p, "%ld,", gwst_stat->wgood_time);
530 g_string_append_printf(dump_p, "%ld", gwst_stat->wgrt_time);
531 g_string_append(dump_p, "\n");
532 _DBG("%s", dump_p->str);//write
533 if (write(fd, dump_p->str, dump_p->len) < 0)
534 _WARN("write error");
542 static int bd_get_and_print_gcpu_stat_entry_from_dump(int fd, long int duration)
545 bd_gl_cpu_stat_s *gcpu_stat = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
546 BM_CHECK_MEM_ALLOC(gcpu_stat, {});
548 int ret = BATTERY_MONITOR_ERROR_NONE;
549 const char *rid_str = NULL; int usage = 0;
550 rid_str = bm_get_resource_id_string(2);
551 resourceid_usage_s *res_use =
552 bm_server_query_resource_usage_resourceid(rid_str, 1, &ret);
553 if (res_use == NULL || ret != BATTERY_MONITOR_ERROR_NONE) {
554 _ERR("For resourceid %s", rid_str);
555 _DBG("Dump only system stats");
557 usage = res_use->usage;
558 float usagemah = (float)usage/3600;
559 ret = bm_server_battery_dump_query_from_db(gcpu_stat, 7, duration);
560 if (ret != BATTERY_MONITOR_ERROR_NONE) {
561 _ERR("Unable to fetch data from DB");
566 GString *dump_p = g_string_sized_new(30);
567 BM_CHECK_MEM_ALLOC(dump_p, {BM_FREE(gcpu_stat); BM_FREE(res_use);});
568 g_string_append(dump_p, TIZEN_DUMP_VERSION);
569 g_string_append(dump_p, ",0,l,");
570 g_string_append(dump_p, "cpu,");
571 g_string_append_printf(dump_p, "%ld,", gcpu_stat->usr_time);
572 g_string_append_printf(dump_p, "%ld,", gcpu_stat->sys_time);
573 g_string_append_printf(dump_p, "%0.2f", usagemah);
574 g_string_append(dump_p, "\n");
575 bm_resourceid_usage_free(res_use);
576 _DBG("%s", dump_p->str);
577 if (write(fd, dump_p->str, dump_p->len) < 0)
578 _WARN("write error");
585 static int bd_get_and_print_uid_info_entry_from_dump(int fd)
589 int ret = BATTERY_MONITOR_ERROR_NONE;
590 GSList *list = bm_server_query_appid_map(&ret);
592 _ERR("NO LIST DATA FOUND");
595 app_map = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
596 GSList *iter = NULL; GString *dump_p = NULL;
597 dump_p = g_string_sized_new(30);
598 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
599 g_string_append(dump_p, TIZEN_DUMP_VERSION);
600 g_string_append(dump_p, ",0,i,uid,1000,tizen\n");
601 if (write(fd, dump_p->str, dump_p->len) < 0)
602 _WARN("write error");
605 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
606 dump_p = g_string_sized_new(30);
607 BM_CHECK_MEM_ALLOC(dump_p, {bm_set_free_gslist_appid_map(list); });
608 g_string_append(dump_p, TIZEN_DUMP_VERSION);
609 g_string_append(dump_p, ",0");
610 g_string_append(dump_p, ",i");
611 g_string_append(dump_p, ",uid,");
612 appid_map_s *amap_data = (appid_map_s *)iter->data;
613 g_string_append_printf(dump_p, "%d,", (amap_data->id+10000));
614 gchar *temp_str = g_strdup(amap_data->AppId);
615 g_string_append(dump_p, amap_data->AppId);
616 g_string_append(dump_p, "\n");
617 _DBG("%s", dump_p->str);//write
618 if (write(fd, dump_p->str, dump_p->len) < 0)
619 _WARN("write error");
620 int *temp_id = (int *)malloc(4);
621 *temp_id = (amap_data->id + 10000);
622 _DBG("Insert id %d, str %s, res %d", *temp_id, temp_str,
623 g_hash_table_insert(app_map, temp_id, temp_str));
626 bm_set_free_gslist_appid_map(list);
632 static int bd_get_history_detail_from_dump(int index, history_item_s* dump_node)
635 BM_CHECK_INPUT_PARAM(dump_node);
637 if (h_count >= HISTORY_SIZE_MAX || index > h_count) {
638 _ERR("Some Error occured with global index");
639 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
642 _DBG("Value of index %d", index);
643 _DBG("Value of h_count %d", h_count);
645 int dvar1 = 0, dvar2 = 0;
646 dvar1 = history_data[index].dump_1;
647 dvar2 = history_data[index].dump_2;
649 _DBG("Value of encoded value 1 %d", dvar1);
650 _DBG("Value of encoded value 2 %d", dvar2);
652 dump_node->cmd_s = ((dvar1) & 0xff);
653 _DBG("cmd_s %d", dump_node->cmd_s);
655 dump_node->battery_level = ((dvar1>>8) & 0xff);
656 _DBG("battery_level %d", dump_node->battery_level);
658 dump_node->battery_status = ((dvar1>>16) & 0xf);
659 _DBG("battery_status %d", dump_node->battery_status);
661 dump_node->battery_health = ((dvar1>>20) & 0xf);
662 _DBG("battery_health %d", dump_node->battery_health);
664 dump_node->battery_plugtype = ((dvar1>>24) & 0xf);
665 _DBG("battery_plugtype %d", dump_node->battery_plugtype);
667 dump_node->battery_temperature = ((dvar2) & 0xffff);
668 _DBG("battery_temperature %d", dump_node->battery_temperature);
670 dump_node->battery_voltage = ((dvar2>>16) & 0xffff);
671 _DBG("battery_voltage %d", dump_node->battery_voltage);
673 dump_node->battery_charge = history_data[index].battery_chargeUAh;
674 _DBG("battery_charge %d", dump_node->battery_charge);
676 dump_node->event_code = history_data[index].event_code;
677 _DBG("eventcode %d", dump_node->event_code);
679 dump_node->state_1 = history_data[index].state_1;
680 _DBG("state_1 %d", dump_node->state_1);
682 dump_node->state_2 = history_data[index].state_2;
683 _DBG("state_2 %d", dump_node->state_2);
685 dump_node->event_tag = history_data[index].event_tag;
686 if (dump_node->event_tag == NULL)
687 _DBG("Event_Tag_NULL");
689 _DBG("%d %d %s", dump_node->event_tag->uid, dump_node->event_tag->sp_idx,
690 dump_node->event_tag->string_info->str);
691 dump_node->wakelock_tag = history_data[index].wakelock_tag;
692 if (dump_node->wakelock_tag == NULL)
693 _DBG("Wakelock_Tag_NULL");
694 dump_node->wakereason_tag = history_data[index].wakereason_tag;
696 if (dump_node->wakereason_tag == NULL)
697 _DBG("Wakereason_Tag_NULL");
699 dump_node->time_s = history_data[index].time_s;
700 _DBG("TIME_S is %lld", dump_node->time_s);
702 dump_node->time_current = history_data[index].time_current;
703 _DBG("C TIME is %lld", dump_node->time_current);
705 dump_node->usage_type = history_data[index].usage_type;
706 _DBG("Usage type is %d", dump_node->usage_type);
708 dump_node->usage = history_data[index].usage;
709 _DBG("Usage is %d", dump_node->usage);
712 return BATTERY_MONITOR_ERROR_NONE;
715 int bd_print_history_item_reset(history_item_s* old_state)
718 BM_CHECK_INPUT_PARAM(old_state);
720 old_state->time_s = -1;
721 old_state->state_1 = 0;
722 old_state->state_2 = 0;
723 old_state->usage_type = 0;
724 old_state->usage = 0;
725 old_state->battery_level = -1;
726 old_state->battery_status = -1;
727 old_state->battery_health = -1;
728 old_state->battery_plugtype = -1;
729 old_state->battery_temperature = -1;
730 old_state->battery_voltage = -1;
731 old_state->battery_charge = -1;
732 old_state->event_code = 0;
733 old_state->event_tag = NULL;
734 old_state->wakelock_tag = NULL;
735 old_state->wakereason_tag = NULL;
738 return BATTERY_MONITOR_ERROR_NONE;
741 int bd_get_base_time_from_dump(long long *base_time)
745 *base_time = history_data[0].time_s;
748 return BATTERY_MONITOR_ERROR_NONE;
751 int bd_print_history_item_main(int fd, int num_h_items, long long base_time, bool chkin)
757 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
760 if (h_count >= HISTORY_SIZE_MAX) {
761 _ERR("Some Error occured with global index");
762 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
765 int ret = BATTERY_MONITOR_ERROR_NONE;
766 h_flag = false; //Lock
768 history_item_s new_state;
769 history_item_s old_state;
771 ret = bd_print_history_item_reset(&old_state);
772 if (ret != BATTERY_MONITOR_ERROR_NONE) {
773 _ERR("Old State reset failed");
777 ret = bd_get_pool_entry_from_dump(fd);
778 if (ret != BATTERY_MONITOR_ERROR_NONE)
779 _ERR("S pool data dump failed");
781 ret = bd_get_base_time_from_dump(&base_time);
782 if (ret != BATTERY_MONITOR_ERROR_NONE)
783 _ERR("Base time error");
785 int log_count = h_count; GString *dump_p = NULL;
786 for (int i = 0; i < log_count; i++) {
787 ret = bd_get_history_detail_from_dump(i, &new_state);
788 if (ret != BATTERY_MONITOR_ERROR_NONE) {
789 _ERR("Decode Failed");
792 dump_p = g_string_sized_new(30);
793 BM_CHECK_MEM_ALLOC(dump_p, {});
795 g_string_append(dump_p, TIZEN_DUMP_VERSION);
796 g_string_append_c(dump_p, ',');
797 _DBG("value %s", dump_p->str);
798 g_string_append(dump_p, DUMP_DATA_TAG);
799 g_string_append_c(dump_p, ',');
800 _DBG("value %s", dump_p->str);
801 if (old_state.time_s < 0) {
802 g_string_append_printf(dump_p, "%lld",
803 (new_state.time_s - base_time));
804 _DBG("value %s", dump_p->str);
806 g_string_append_printf(dump_p, "%lld",
807 (new_state.time_s - old_state.time_s));
808 _DBG("value %s", dump_p->str);
810 old_state.time_s = new_state.time_s;
811 _DBG("Value old_state.time_s %lld", old_state.time_s);
812 } else { //: Let's skip the NC mode in initial version
814 if (new_state.cmd_s == CM_ST) {
816 g_string_append_c(dump_p, ':');
817 _DBG("value %s", dump_p->str);
821 g_string_append(dump_p, "START\n");
822 _DBG("value %s", dump_p->str);
823 bd_print_history_item_reset(&old_state);
824 } else if (new_state.cmd_s == CM_CRR_TIME || new_state.cmd_s == CM_RST) {
826 g_string_append_c(dump_p, ':');
827 _DBG("value %s", dump_p->str);
829 if (new_state.cmd_s == CM_RST) {
830 g_string_append(dump_p, "RESET:");
831 _DBG("value %s", dump_p->str);
832 bd_print_history_item_reset(&old_state);
834 g_string_append(dump_p, "TIME:");
835 _DBG("value %s", dump_p->str);
837 g_string_append_printf(dump_p, "%lld", new_state.time_current);
838 _DBG("value %s", dump_p->str);
839 g_string_append(dump_p, "\n");
840 _DBG("value %s", dump_p->str);
844 } else if (new_state.cmd_s == CM_SHTDN) {
846 g_string_append_c(dump_p, ':');
847 _DBG("value %s", dump_p->str);
851 g_string_append(dump_p, "SHUTDOWN\n");
852 _DBG("value %s", dump_p->str);
853 } else if (new_state.cmd_s == CM_OVRFL) {
855 g_string_append_c(dump_p, ':');
856 _DBG("value %s", dump_p->str);
858 g_string_append(dump_p, "*OVERFLOW*\n");
859 _DBG("value %s", dump_p->str);
860 } else if (new_state.cmd_s == CM_USAGE) {
861 for (int i = 0; i < 32; i++) {
862 if (new_state.usage_type == (1 << i)) {
863 g_string_append(dump_p, ",");
864 g_string_append_printf(dump_p, "%s", usage_map[i]);
865 g_string_append(dump_p, "=");
866 g_string_append_printf(dump_p, "%d", new_state.usage);
867 g_string_append(dump_p, "\n");
868 _DBG("value %d", new_state.usage);
875 if (old_state.battery_level != new_state.battery_level) {
876 old_state.battery_level = new_state.battery_level;
877 g_string_append(dump_p, ",Bl=");
878 _DBG("value %s", dump_p->str);
879 g_string_append_printf(dump_p, "%d",
880 new_state.battery_level);
881 _DBG("value %s", dump_p->str);
884 if (old_state.battery_status != new_state.battery_status) {
885 old_state.battery_status = new_state.battery_status;
886 g_string_append(dump_p, ",Bs="); //:for NC
887 _DBG("value %s", dump_p->str);
888 switch (old_state.battery_status) {
890 g_string_append_c(dump_p, '?');
893 g_string_append_c(dump_p, 'c');
896 g_string_append_c(dump_p, 'd');
899 g_string_append_c(dump_p, 'n');
902 g_string_append_c(dump_p, 'f');
905 g_string_append_c(dump_p, '?');
906 _DBG("value %s", dump_p->str);
910 if (old_state.battery_health != new_state.battery_health) {
911 old_state.battery_health = new_state.battery_health;
912 g_string_append(dump_p, ",Bh="); //:for NC
913 _DBG("value %s", dump_p->str);
914 switch (old_state.battery_health) {
916 g_string_append_c(dump_p, 'g');
919 g_string_append_c(dump_p, '?');
922 g_string_append_c(dump_p, 'h');
925 g_string_append_c(dump_p, 'd');
928 g_string_append_c(dump_p, 'v');
931 g_string_append_c(dump_p, 'f');
934 g_string_append_c(dump_p, 'c');
937 g_string_append_c(dump_p, '?');
938 _DBG("value %s", dump_p->str);
942 if (old_state.battery_plugtype != new_state.battery_plugtype) {
943 old_state.battery_plugtype = new_state.battery_plugtype;
944 g_string_append(dump_p, ",Bp="); //:for NC
945 _DBG("value %s", dump_p->str);
946 switch (old_state.battery_plugtype) {
948 g_string_append_c(dump_p, 'n');
951 g_string_append_c(dump_p, 'a');
954 g_string_append_c(dump_p, 'u');
957 g_string_append_c(dump_p, 'w');
960 g_string_append_c(dump_p, 'n');
961 _DBG("value %s", dump_p->str);
965 if (old_state.battery_temperature != new_state.battery_temperature) {
966 old_state.battery_temperature = new_state.battery_temperature;
967 g_string_append(dump_p, ",Bt="); //:for NC
968 g_string_append_printf(dump_p, "%d",
969 new_state.battery_temperature);
970 _DBG("value %s", dump_p->str);
972 if (old_state.battery_voltage != new_state.battery_voltage) {
973 old_state.battery_voltage = new_state.battery_voltage;
974 g_string_append(dump_p, ",Bv="); //:for NC
975 _DBG("value %s", dump_p->str);
976 g_string_append_printf(dump_p, "%d", new_state.battery_voltage);
977 _DBG("value %s", dump_p->str);
979 if (old_state.battery_charge != new_state.battery_charge) {
980 old_state.battery_charge = new_state.battery_charge;
981 g_string_append(dump_p, ",Bcc=");
982 _DBG("value %s", dump_p->str);
984 g_string_append_printf(dump_p, "%d", (new_state.battery_charge/1000));
985 _DBG("value %s", dump_p->str);
987 int change = (old_state.state_1 ^ new_state.state_1);
989 bool wake_fl = false; int k = 0;
990 if ((change & ST1_BRIGHTNESS_MASK) != 0) {
991 k = (new_state.state_1 & ST1_BRIGHTNESS_MASK);
992 g_string_append(dump_p, ",Sb=");
993 k = k >> ST1_BRIGHTNESS_SHIFT;
995 g_string_append_printf(dump_p, "%d", k);
998 if ((change & ST1_PHONE_SIGNAL_STRENGTH_MASK) != 0) {
999 k = (new_state.state_1 & ST1_PHONE_SIGNAL_STRENGTH_MASK);
1000 g_string_append(dump_p, ",Pss=");
1001 k = k >> ST1_PHONE_SIGNAL_STRENGTH_SHIFT;
1002 _DBG("Pss = %d", k);
1003 g_string_append_printf(dump_p, "%d", k);
1006 int shifts = (1 << 14);
1007 for (int i = 14; i < 32; i++) {
1008 if ((change & shifts) != 0) {
1009 g_string_append_c(dump_p, ',');
1010 //Considering only -ve shift cases
1013 if ((new_state.state_1 & (1 << i)) != 0)
1014 g_string_append_c(dump_p, '+');
1016 g_string_append_c(dump_p, '-');
1017 gchar *temp = g_strdup(state1_map[i]);
1018 g_string_append(dump_p, temp);
1020 //_DBG("value %s", dump_p->str);
1021 if ((1 << i) == ST1_WAKE_LOCK_FLAG &&
1022 new_state.wakelock_tag != NULL) {
1024 g_string_append_c(dump_p, '=');
1025 //_DBG("value %s", dump_p->str);
1027 g_string_append_printf(dump_p,
1028 "%d", new_state.wakelock_tag->sp_idx);
1032 //_DBG("value %s", dump_p->str);
1035 //With non-negative shift
1038 shifts = (shifts << 1);
1040 if (!wake_fl && new_state.wakelock_tag != NULL) {
1041 g_string_append(dump_p, ",w=");
1043 g_string_append_printf(dump_p, "%d",
1044 new_state.wakelock_tag->sp_idx);
1045 _DBG("value %s", dump_p->str);
1050 change = (old_state.state_2 ^ new_state.state_2);
1053 for (int i = 0; i < 32; i++) {
1054 if ((change & shifts) != 0) {
1055 g_string_append_c(dump_p, ',');
1056 //_DBG("value %s", dump_p->str);
1059 if ((new_state.state_2 & (1 << i) & change) != 0)
1060 g_string_append_c(dump_p, '+');
1062 g_string_append_c(dump_p, '-');
1063 //_DBG("value %s", dump_p->str);
1064 gchar *temp = g_strdup(state2_map[i]);
1065 g_string_append(dump_p, temp);
1067 _DBG("value %s", dump_p->str);
1070 shifts = (shifts << 1);
1073 if (new_state.wakereason_tag != NULL) {
1075 g_string_append(dump_p, ",wr=");
1076 _DBG("value %s", dump_p->str);
1077 g_string_append_printf(dump_p, "%d",
1078 new_state.wakereason_tag->sp_idx);
1079 _DBG("value %s", dump_p->str);
1084 if (new_state.event_code != ET_NONE) {
1085 g_string_append_c(dump_p, ',');
1086 _DBG("value %s", dump_p->str);
1087 if ((new_state.event_code & ET_FLAG_START) != 0)
1088 g_string_append_c(dump_p, '+');
1089 else if ((new_state.event_code & ET_FLAG_FINISH) != 0)
1090 g_string_append_c(dump_p, '-');
1091 _DBG("value %s", dump_p->str);
1092 int k = new_state.event_code & ~(ET_FLAG_START | ET_FLAG_FINISH);
1093 if (k >= 0 && k < 32) {
1094 gchar* temp = g_strdup(event_map[k]);
1095 g_string_append(dump_p, temp);
1096 _DBG("value %s", dump_p->str);
1099 g_string_append(dump_p, "Ev");
1100 g_string_append_printf(dump_p, "%d", k);
1101 _DBG("value %s", dump_p->str);
1103 g_string_append_c(dump_p, '=');
1104 _DBG("value %s", dump_p->str);
1106 g_string_append_printf(dump_p, "%d",
1107 new_state.event_tag->sp_idx);
1113 g_string_append(dump_p, "\n");
1115 old_state.state_1 = new_state.state_1;
1116 old_state.state_2 = new_state.state_2;
1118 _DBG("State assigned");
1119 _DBG("%s", dump_p->str);
1120 if (write(fd, dump_p->str, dump_p->len) < 0)
1121 _WARN("write error");
1122 g_free(dump_p->str);
1125 // Dump the elapsed time from last event
1126 // It should be dumped without storing
1127 GString *dump_tmp_p = g_string_sized_new(30);
1129 gettimeofday(&tv, NULL);
1131 g_string_append(dump_tmp_p, TIZEN_DUMP_VERSION);
1132 g_string_append_c(dump_tmp_p, ',');
1133 g_string_append(dump_tmp_p, DUMP_DATA_TAG);
1134 g_string_append_c(dump_tmp_p, ',');
1135 g_string_append_printf(dump_tmp_p, "%lld",
1136 ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000) - old_state.time_s);
1137 g_string_append(dump_tmp_p, "\n");
1139 if (write(fd, dump_tmp_p->str, dump_tmp_p->len) < 0)
1140 _WARN("write error");
1141 g_free(dump_tmp_p->str);
1143 //Dump i & l values after this
1144 ret = bd_get_and_print_uid_info_entry_from_dump(fd);
1145 if (ret != BATTERY_MONITOR_ERROR_NONE)
1146 _ERR("UID data dump failed");
1147 // Get Last charge time to dump "l" field
1148 // for the time being dumping all data
1149 long int duration = 0; //Time of last charge
1150 ret = bd_get_and_print_gn_stat_entry_from_dump(fd, duration);
1151 if (ret != BATTERY_MONITOR_ERROR_NONE)
1152 _ERR("GN dump failed");
1153 ret = bd_get_and_print_gwf_stat_entry_from_dump(fd, duration);
1154 if (ret != BATTERY_MONITOR_ERROR_NONE)
1155 _ERR("GWF data dump failed");
1156 ret = bd_get_and_print_gble_stat_entry_from_dump(fd, duration);
1157 if (ret != BATTERY_MONITOR_ERROR_NONE)
1158 _ERR("GBL data dump failed");
1159 ret = bd_get_and_print_gbr_stat_entry_from_dump(fd, duration);
1160 if (ret != BATTERY_MONITOR_ERROR_NONE)
1161 _ERR("GBR data dump failed");
1162 ret = bd_get_and_print_gst_stat_entry_from_dump(fd, duration);
1163 if (ret != BATTERY_MONITOR_ERROR_NONE)
1164 _ERR("GST data dump failed");
1165 ret = bd_get_and_print_gwst_stat_entry_from_dump(fd, duration);
1166 if (ret != BATTERY_MONITOR_ERROR_NONE)
1167 _ERR("GWST data dump failed");
1168 ret = bd_get_and_print_gcpu_stat_entry_from_dump(fd, duration);
1169 _ERR("GCPU data dump failed");
1170 // Battery Usage Dump
1171 // Add usage for each of the resource
1172 // and each of the application
1173 ret = bd_get_and_print_battery_usage_from_dump(fd, duration);
1174 if (ret != BATTERY_MONITOR_ERROR_NONE)
1175 _ERR("Battery Usage failed");
1177 _DBG("DUMP COMPLETED/DELETE HASH TABLE");
1179 g_hash_table_destroy(app_map);
1182 return BATTERY_MONITOR_ERROR_NONE;
1185 static int bd_dump_data_item_reset(dump_data_s* old_state)
1188 BM_CHECK_INPUT_PARAM(old_state);
1190 old_state->time_s = -1;
1191 old_state->time_current = -1;
1192 old_state->dump_1 = 0;
1193 old_state->dump_2 = 0;
1194 old_state->state_1 = 0;
1195 old_state->state_2 = 0;
1196 old_state->battery_chargeUAh = 0;
1197 old_state->usage_type = 0;
1198 old_state->usage = 0;
1199 old_state->event_code = ET_NONE;
1200 old_state->event_tag = NULL;
1201 old_state->wakelock_tag = NULL;
1202 old_state->wakereason_tag = NULL;
1205 return BATTERY_MONITOR_ERROR_NONE;
1208 static int bd_free_history_data_memory()
1212 int ret = BATTERY_MONITOR_ERROR_NONE;
1213 h_flag = false; //Lock
1214 _DBG("Free History Data Memory");
1215 for (int i = 0; i < h_count; i++) {
1216 if (history_data[i].wakelock_tag != NULL) {
1218 g_string_free(history_data[i].wakelock_tag->string_info, TRUE);
1220 free(history_data[i].wakelock_tag);
1222 if (history_data[i].wakereason_tag != NULL) {
1224 g_string_free(history_data[i].wakereason_tag->string_info, TRUE);
1226 free(history_data[i].wakereason_tag);
1228 if (history_data[i].event_tag != NULL) {
1230 g_string_free(history_data[i].event_tag->string_info, TRUE);
1232 free(history_data[i].event_tag);
1234 ret = bd_dump_data_item_reset(&history_data[i]);
1235 if (ret != BATTERY_MONITOR_ERROR_NONE)
1236 _ERR("Internal error %d while freeing at idx %d", ret , i);
1239 h_count = 0; h_flag = true;
1244 static int bd_set_history_from_listener(history_item_s* nrec)
1247 BM_CHECK_INPUT_PARAM(nrec);
1249 int dvar1 = 0, dvar2 = 0;
1251 gettimeofday(&tv, NULL);
1253 time_t current_time = tv.tv_sec;
1256 history_data[h_count].time_s = nrec->time_s;
1258 history_data[h_count].time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1261 dvar1 = ((nrec->cmd_s) & 0xff) | ((nrec->battery_level << 8) & 0xff00) |
1262 ((nrec->battery_status << 16) & 0xf0000) |
1263 ((nrec->battery_health << 20) & 0xf00000) |
1264 ((nrec->battery_plugtype << 24) & 0xf000000) |
1265 ((nrec->wakelock_tag != NULL) ? 0x10000000 : 0) |
1266 ((nrec->wakereason_tag != NULL) ? 0x20000000 : 0) |
1267 ((nrec->event_code != ET_NONE) ? 0x40000000 : 0);
1269 dvar2 = ((nrec->battery_temperature) & 0xffff) |
1270 ((nrec->battery_voltage << 16) & 0xffff0000);
1272 _DBG("Value of encoded value 1 %d", dvar1);
1273 _DBG("Value of encoded value 2 %d", dvar2);
1275 history_data[h_count].dump_1 = dvar1;
1276 history_data[h_count].dump_2 = dvar2;
1277 history_data[h_count].battery_chargeUAh = nrec->battery_charge;
1279 if (nrec->wakelock_tag != NULL)
1280 history_data[h_count].wakelock_tag = nrec->wakelock_tag;
1281 if (nrec->wakereason_tag != NULL)
1282 history_data[h_count].wakereason_tag = nrec->wakereason_tag;
1283 if (nrec->event_code != ET_NONE) {
1284 if (nrec->event_tag == NULL) {
1285 _ERR("ET is NULL but Event Code is not");
1286 BM_FREE(history_data[h_count].wakelock_tag);
1287 BM_FREE(history_data[h_count].wakereason_tag);
1288 bd_dump_data_item_reset(&history_data[h_count]);
1289 return BATTERY_MONITOR_ERROR_NO_DATA;
1291 history_data[h_count].event_code = nrec->event_code;
1292 history_data[h_count].event_tag = nrec->event_tag;
1294 if (nrec->cmd_s == CM_CRR_TIME || nrec->cmd_s == CM_RST)
1295 history_data[h_count].time_current = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
1297 history_data[h_count].state_1 = nrec->state_1;
1298 history_data[h_count].state_2 = nrec->state_2;
1300 history_data[h_count].usage_type = nrec->usage_type;
1301 history_data[h_count].usage = nrec->usage;
1303 nrec->event_tag = NULL; nrec->wakereason_tag = NULL;
1304 nrec->wakelock_tag = NULL;
1305 if (h_count < HISTORY_SIZE_MAX - 1)
1308 // FIXME modify 1st argument (fd)
1309 int ret = bd_print_history_item_main(-1, 0, current_time, 1);
1310 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1314 ret = bd_free_history_data_memory();
1315 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1316 _WARN("Error while memory free %x", ret);
1322 return BATTERY_MONITOR_ERROR_NONE;
1325 int bd_store_history_item(history_item_s* nrec)
1328 BM_CHECK_INPUT_PARAM(nrec);
1331 _ERR("Data Dump in progress");
1332 return BATTERY_MONITOR_ERROR_DATABASE_BUSY;
1334 int ret = BATTERY_MONITOR_ERROR_NONE;
1335 ret = bd_set_history_from_listener(nrec);
1336 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1337 _ERR("Failed to store history");
1338 return BATTERY_MONITOR_ERROR_INTERNAL;
1342 return BATTERY_MONITOR_ERROR_NONE;
1346 int bd_set_old_state_to_history_item(history_item_s* nrec)
1349 //Move this with listeners
1351 return BATTERY_MONITOR_ERROR_NONE;
1355 static int *dumpsys_handler;
1357 int bd_callback(const int fd, const int argc, char **argv)
1361 bool checkin = false;
1364 * We must reset optind because it isn't initialized
1365 * on subsequent getopt calls
1368 while ((opt = getopt(argc, argv, "c")) != -1) {
1379 ret = bd_print_history_item_main(fd, -1, 0, checkin);
1380 if (ret != BATTERY_MONITOR_ERROR_NONE) {
1381 _ERR("bd_print_history_item_main failed (%x)", ret);
1388 int bd_initialize_battery_dump()
1392 int ret = BATTERY_MONITOR_ERROR_NONE;
1394 h_count = 0; h_flag = true;
1400 ret = bd_print_history_item_reset(&ts);
1401 if (ret == BATTERY_MONITOR_ERROR_NONE) {
1402 ts.cmd_s = CM_CRR_TIME;
1403 ret = bd_store_history_item(&ts);
1404 if (ret != BATTERY_MONITOR_ERROR_NONE)
1405 _WARN("Init Node addition failed");
1409 ret = dumpsys_system_register_dump_cb(bd_callback, "batterystats", (void **)&dumpsys_handler);
1410 if (ret != TIZEN_ERROR_NONE) {
1411 _ERR("dumpsys_system_register_dump_cb failed (%x)", ret);
1412 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1415 _INFO("Battery Dump Init Successful");
1418 return BATTERY_MONITOR_ERROR_NONE;
1421 int bd_deinitialize_battery_dump()
1427 ret = dumpsys_system_unregister_dump_cb(dumpsys_handler);
1428 if (ret != TIZEN_ERROR_NONE) {
1429 _WARN("dumpsys_system_unregister_dump_cb failed (%x)", ret);
1430 return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
1433 ret = bd_free_history_data_memory();
1434 if (ret != BATTERY_MONITOR_ERROR_NONE)
1435 _WARN("memory free failed (%x)", ret);
1437 _INFO("Battery Dump De-Init Successful");
1440 return BATTERY_MONITOR_ERROR_NONE;