Battery Monitor Dump Implementation: Initial Commit 45/220545/3
authorDewal Agarwal <d1.agarwal@samsung.com>
Thu, 19 Dec 2019 08:41:16 +0000 (16:41 +0800)
committerKichan Kwon <k_c.kwon@samsung.com>
Thu, 26 Dec 2019 00:42:55 +0000 (09:42 +0900)
Change-Id: I61d97f80d0302c8506b1e08ce4edf1dfabbe7478
Signed-off-by: Dewal Agarwal <d1.agarwal@samsung.com>
CMakeLists.txt
include/bd_history_item.h [new file with mode: 0644]
src/battery_dump/bd_history_item.c [new file with mode: 0644]
src/bm_server.c

index ec86a72..813ca80 100644 (file)
@@ -40,6 +40,7 @@ SET(SRCS
        src/bm_server_db.c
        src/bm_util.c
        src/bm_server.c
+       src/battery_dump/bd_history_item.c
 )
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
diff --git a/include/bd_history_item.h b/include/bd_history_item.h
new file mode 100644 (file)
index 0000000..e7b2ca7
--- /dev/null
@@ -0,0 +1,218 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __BD_HISTORY_ITEM_H__
+#define __BD_HISTORY_ITEM_H__
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <glib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// All the structures
+typedef struct _history_tag_s {
+       GString* string_info;
+       int uid;
+       int hsp_idx;
+} history_tag_s;
+
+typedef struct _history_item_s {
+       long int time_s;
+       long int time_current;
+       int cmd_s;
+       int state_1;
+       int state_2;
+       int battery_level;
+       int battery_status;
+       int battery_health;
+       int battery_plugtype;
+       int battery_temperature;
+       int battery_voltage;
+       int battery_charge;//UAh
+       // SPool related fields
+       int event_code;
+       history_tag_s* event_tag;
+       history_tag_s* wakelock_tag;//Wakelock acquired
+       history_tag_s* wakereason_tag;//Wakereason information
+} history_item_s;
+
+// Private
+typedef struct _dump_data_s {
+       long int time_s;
+       long int time_current;
+       int dump_1;
+       int dump_2;
+       int state_1;
+       int state_2;
+       int battery_chargeUAh;
+       int event_code;
+       history_tag_s* event_tag;
+       history_tag_s* wakelock_tag;
+       history_tag_s* wakereason_tag;
+} dump_data_s;
+
+/*
+typedef struct _encoding_map_s {
+       int mask;
+       int shift;
+       //skip long name char name_full[];
+       char name_short[4];
+       char values_short[7][4];
+} encoding_map_s;
+*/
+
+#define HISTORY_SIZE_MAX 1000000 // Maximum Allowed History
+#define TIZEN_DUMP_VERSION "9"
+#define DUMP_DATA_TAG "h"
+
+#define CM_UPD 0
+#define CM_NULL -1
+#define CM_ST 4
+#define CM_CRR_TIME 5
+#define CM_RST 7
+#define CM_SHTDN 8
+#define CM_OVRFL 6 //:Used in NC
+
+// Functions for the other part of the service
+int bd_initialize_battery_dump(void);
+int bd_deinitialize_battery_dump(void);
+int bd_print_history_item_main(int num_h_items, long base_time, bool chkin);
+int bd_store_history_item(history_item_s* nrec);
+
+// Constants for State Changes of Type 1
+//static const int ST1_BRIGHTNESS_SHIFT = 0;
+//static const int ST1_BRIGHTNESS_MASK = (0x7 << 0);
+//static const int ST1_PHONE_SIGNAL_STRENGTH_SHIFT = 3; // 3 bit left Shift from initial point
+//static const int ST1_PHONE_SIGNAL_STRENGTH_MASK = (0x7 << 3);
+//static const int ST1_PHONE_STATE_SHIFT = 6;
+//static const int ST1_PHONE_STATE_MASK = (0x7 << 6);
+//static const int ST1_DATA_CONNECTION_SHIFT = 9;
+//static const int ST1_DATA_CONNECTION_MASK = (0x1f << 9);
+static const int ST1_CPU_RUNNING_FLAG = 1<<31;
+static const int ST1_WAKE_LOCK_FLAG = 1<<30;
+static const int ST1_GPS_ON_FLAG = 1<<29;
+static const int ST1_WIFI_FULL_LOCK_FLAG = 1<<28;
+static const int ST1_WIFI_SCAN_FLAG = 1<<27;
+static const int ST1_WIFI_RADIO_ACTIVE_FLAG = 1<<26;
+static const int ST1_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
+static const int ST1_RESERVED_0 = 1<<24;
+// These are on the lower bits used for the command;
+// if they change we need to write another int of data.
+static const int ST1_SENSOR_ON_FLAG = 1<<23;
+static const int ST1_AUDIO_ON_FLAG = 1<<22;
+static const int ST1_PHONE_SCANNING_FLAG = 1<<21;
+static const int ST1_SCREEN_ON_FLAG = 1<<20;       // consider moving to states2
+static const int ST1_BATTERY_PLUGGED_FLAG = 1<<19; // consider moving to states2
+static const int ST1_SCREEN_DOZE_FLAG = 1 << 18;
+static const int ST1_EMPTY_SLOT = 1 << 17; //empty slot
+static const int ST1_WIFI_MULTICAST_ON_FLAG = 1<<16;
+
+/*
+static const int MOST_INTERESTING_ST1 =
+                       ST1_BATTERY_PLUGGED_FLAG | ST1_SCREEN_ON_FLAG | ST1_SCREEN_DOZE_FLAG;
+static const int SETTLE_TO_ZERO_ST1 = 0xffff0000 & ~MOST_INTERESTING_ST1;
+*/
+
+// Constants for State of Type 2
+//static const int ST2_WIFI_SUPPL_STATE_SHIFT = 0;
+//static const int ST2_WIFI_SUPPL_STATE_MASK = 0xf;
+//static const int ST2_WIFI_SIGNAL_STRENGTH_SHIFT = 4;
+//static const int ST2_WIFI_SIGNAL_STRENGTH_MASK = (0x7 << 4);
+//static const int ST2_GPS_SIGNAL_QUALITY_SHIFT = 7;
+//static const int ST2_GPS_SIGNAL_QUALITY_MASK = (0x1 << 7);
+static const int ST2_POWER_SAVE_FLAG = 1<<31;
+static const int ST2_VIDEO_ON_FLAG = 1<<30;
+static const int ST2_WIFI_RUNNING_FLAG = 1<<29;
+static const int ST2_WIFI_ON_FLAG = 1<<28;
+static const int ST2_FLASHLIGHT_FLAG = 1<<27;
+//static const int ST2_DEVICE_IDLE_SHIFT = 25;
+//static const int ST2_DEVICE_IDLE_MASK = (0x3 << 25);
+static const int ST2_CHARGING_FLAG = 1<<24;
+static const int ST2_PHONE_IN_CALL_FLAG = 1<<23;
+static const int ST2_BLUETOOTH_ON_FLAG = 1<<22;
+static const int ST2_CAMERA_FLAG = 1<<21;
+static const int ST2_BLUETOOTH_SCAN_FLAG = 1 << 20;
+static const int ST2_CELLULAR_HIGH_TX_POWER_FLAG = 1 << 19;
+static const int ST2_USB_DATA_LINK_FLAG = 1 << 18;
+
+/*
+static const int MOST_INTERESTING_ST2 =
+                       ST2_POWER_SAVE_FLAG | ST2_WIFI_ON_FLAG | ST2_DEVICE_IDLE_MASK
+                       | ST2_CHARGING_FLAG | ST2_PHONE_IN_CALL_FLAG | ST2_BLUETOOTH_ON_FLAG;
+static const int SETTLE_TO_ZERO_ST2 = 0xffff0000 & ~MOST_INTERESTING_ST2;
+*/
+//public HistoryStepDetails stepDetails; ==> Move them to struct
+
+// Constants for events
+static const int ET_FLAG_START = 0x8000;
+static const int ET_FLAG_FINISH = 0x4000;
+static const int ET_NONE = 0x0000;
+static const int ET_PROC = 0x0001;
+static const int ET_FOREGROUND = 0x0002;
+static const int ET_TOP = 0x0003;
+static const int ET_SYNC = 0x0004;
+static const int ET_WAKE_LOCK = 0x0005;
+static const int ET_JOB = 0x0006;
+static const int ET_USER_RUNNING = 0x0007;
+static const int ET_USER_FOREGROUND = 0x0008;
+static const int ET_CONNECTIVITY_CHANGED = 0x0009;
+static const int ET_ACTIVE = 0x000a;
+static const int ET_PACKAGE_INSTALLED = 0x000b;
+static const int ET_PACKAGE_UNINSTALLED = 0x000c;
+static const int ET_ALARM = 0x000d;
+static const int ET_COLLECT_EXTERNAL_STATS = 0x000e;
+static const int ET_PACKAGE_INACTIVE = 0x000f;
+static const int ET_PACKAGE_ACTIVE = 0x0010;
+static const int ET_TEMP_WHITELIST = 0x0011;
+static const int ET_SCREEN_WAKE_UP = 0x0012;
+static const int ET_WAKEUP_AP = 0x0013;
+static const int ET_LONG_WAKE_LOCK = 0x0014;
+static const int ET_COUNT = 0x0016;
+/*
+static const int ET_TYPE_MASK = ~(ET_FLAG_START|ET_FLAG_FINISH);
+static const int ET_PROC_START = ET_PROC | ET_FLAG_START;
+static const int ET_PROC_FINISH = ET_PROC | ET_FLAG_FINISH;
+static const int ET_FOREGROUND_START = ET_FOREGROUND | ET_FLAG_START;
+static const int ET_FOREGROUND_FINISH = ET_FOREGROUND | ET_FLAG_FINISH;
+static const int ET_TOP_START = ET_TOP | ET_FLAG_START;
+static const int ET_TOP_FINISH = ET_TOP | ET_FLAG_FINISH;
+static const int ET_SYNC_START = ET_SYNC | ET_FLAG_START;
+static const int ET_SYNC_FINISH = ET_SYNC | ET_FLAG_FINISH;
+static const int ET_WAKE_LOCK_START = ET_WAKE_LOCK | ET_FLAG_START;
+static const int ET_WAKE_LOCK_FINISH = ET_WAKE_LOCK | ET_FLAG_FINISH;
+static const int ET_JOB_START = ET_JOB | ET_FLAG_START;
+static const int ET_JOB_FINISH = ET_JOB | ET_FLAG_FINISH;
+static const int ET_USER_RUNNING_START = ET_USER_RUNNING | ET_FLAG_START;
+static const int ET_USER_RUNNING_FINISH = ET_USER_RUNNING | ET_FLAG_FINISH;
+static const int ET_USER_FOREGROUND_START = ET_USER_FOREGROUND | ET_FLAG_START;
+static const int ET_USER_FOREGROUND_FINISH = ET_USER_FOREGROUND | ET_FLAG_FINISH;
+static const int ET_ALARM_START = ET_ALARM | ET_FLAG_START;
+static const int ET_ALARM_FINISH = ET_ALARM | ET_FLAG_FINISH;
+static const int ET_TEMP_WHITELIST_START = ET_TEMP_WHITELIST | ET_FLAG_START;
+static const int ET_TEMP_WHITELIST_FINISH = ET_TEMP_WHITELIST | ET_FLAG_FINISH;
+static const int ET_LONG_WAKE_LOCK_START = ET_LONG_WAKE_LOCK | ET_FLAG_START;
+static const int ET_LONG_WAKE_LOCK_FINISH = ET_LONG_WAKE_LOCK | ET_FLAG_FINISH;
+*/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BD_HISTORY_ITEM_H__ */
diff --git a/src/battery_dump/bd_history_item.c b/src/battery_dump/bd_history_item.c
new file mode 100644 (file)
index 0000000..af972bd
--- /dev/null
@@ -0,0 +1,565 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdlib.h>
+#include <time.h>
+#include <glib/gprintf.h>
+
+#include <bd_history_item.h>
+#include <bm_dbg.h>
+#include <bm_common.h>
+
+dump_data_s history_data[HISTORY_SIZE_MAX];
+int counter = 0;
+char state1_map[32][4] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Wm",
+       "", "Sd", "BP", "S", "Psc", "a", "s", "", "Pr", "Wr", "Ws", "Wl", "g", "w", "r"};
+char state2_map[32][5] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
+       "Ud", "Chtp", "bles", "ca", "b", "Pcl", "ch", "", "", "fl", "W", "Ww", "v", "ps"};
+char event_map[32][4] = {"Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
+       "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw", "Esw", "Ewa", "Elw", "Eec"};
+
+// The index passed in the fn should be preferrably continuous,
+// Other things should be implemented at dumping function.
+static int bd_get_history_detail_from_dump(int index, history_item_s* dump_node)
+{
+       ENTER;
+
+       if (counter >= HISTORY_SIZE_MAX || index > counter) {
+               // This assumes that counter is always greater
+               // and old data should be dumped.
+               _ERR("Some Error occured with global index");
+               return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
+       }
+
+       _DBG("Value of index %d", index);
+       _DBG("Value of counter %d", counter);
+       // Print time if required
+       int dvar1 = 0, dvar2 = 0;
+       dvar1 = history_data[index].dump_1;
+       dvar2 = history_data[index].dump_2;
+
+       _DBG("Value of encoded value 1 %d", dvar1);
+       _DBG("Value of encoded value 2 %d", dvar2);
+
+       dump_node->cmd_s = ((dvar1) & 0xff);
+       _DBG("cmd_s %d", dump_node->cmd_s);
+       dump_node->battery_level = ((dvar1>>8) & 0xff);
+       _DBG("battery_level %d", dump_node->battery_level);
+       dump_node->battery_status = ((dvar1>>16) & 0xf);
+       _DBG("battery_status %d", dump_node->battery_status);
+       dump_node->battery_health = ((dvar1>>20) & 0xf);
+       _DBG("battery_health %d", dump_node->battery_health);
+       dump_node->battery_plugtype = ((dvar1>>24) & 0xf);
+       _DBG("battery_plugtype %d", dump_node->battery_plugtype);
+       dump_node->battery_temperature = ((dvar2) & 0xffff);
+       _DBG("battery_temperature %d", dump_node->battery_temperature);
+       dump_node->battery_voltage = ((dvar2>>16) & 0xffff);
+       _DBG("battery_voltage %d", dump_node->battery_voltage);
+       dump_node->battery_charge = history_data[index].battery_chargeUAh;
+       _DBG("battery_charge %d", dump_node->battery_charge);
+       dump_node->event_code = history_data[index].event_code;
+       _DBG("eventcode %d", dump_node->event_code);
+       dump_node->state_1 = history_data[index].state_1;
+       _DBG("state_1 %d", dump_node->state_1);
+       dump_node->state_2 = history_data[index].state_2;
+       _DBG("state_2 %d", dump_node->state_2);
+       dump_node->event_tag = history_data[index].event_tag;
+       if (dump_node->event_tag == NULL)
+               _DBG("Event_Tag_NULL");
+       dump_node->wakelock_tag = history_data[index].wakelock_tag;
+       if (dump_node->wakelock_tag == NULL)
+               _DBG("Wakelock_Tag_NULL");
+       dump_node->wakereason_tag = history_data[index].wakereason_tag;
+       if (dump_node->wakereason_tag == NULL)
+               _DBG("Wakereason_Tag_NULL");
+       dump_node->time_s = history_data[index].time_s;
+       _DBG("TIME_S is %ld", dump_node->time_s);
+       dump_node->time_current = history_data[index].time_current;
+       _DBG("C TIME is %ld", dump_node->time_current);
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+static int bd_print_history_item_reset(history_item_s* old_state)
+{
+       ENTER;
+
+       old_state->time_s = -1;
+       old_state->state_1 = 0;
+       old_state->state_2 = 0;
+       old_state->battery_level = -1;
+       old_state->battery_status = -1;
+       old_state->battery_health = -1;
+       old_state->battery_plugtype = -1;
+       old_state->battery_temperature = -1;
+       old_state->battery_voltage = -1;
+       old_state->battery_charge = -1;
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+int bd_print_history_item_main(int num_h_items, long base_time, bool chkin)
+{
+       ENTER;
+
+       if (counter >= HISTORY_SIZE_MAX) {
+               _ERR("Some Error occured with global index");
+               return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
+       }
+
+       int ret = BATTERY_MONITOR_ERROR_NONE;
+
+       history_item_s new_state;
+       history_item_s old_state;
+
+       ret = bd_print_history_item_reset(&old_state);
+       if (ret != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("Old State reset failed");
+               return ret;
+       }
+
+       int log_count = counter;
+       for (int i = 0; i < log_count; i++) {
+
+               ret = bd_get_history_detail_from_dump(i, &new_state);
+               if (ret != BATTERY_MONITOR_ERROR_NONE) {
+                       _ERR("Decode Failed");
+                       return ret;
+               }
+
+               GString *dump_p = g_string_sized_new(30);
+               if (chkin) {
+                       dump_p = g_string_append(dump_p, TIZEN_DUMP_VERSION);
+                       dump_p = g_string_append_c(dump_p, ',');
+                       _DBG("value %s", dump_p->str);
+                       dump_p = g_string_append(dump_p, DUMP_DATA_TAG);
+                       dump_p = g_string_append_c(dump_p, ',');
+                       _DBG("value %s", dump_p->str);
+                       if (old_state.time_s < 0) {
+                               g_string_append_printf(dump_p, "%ld",
+                                               (new_state.time_s - base_time));
+                               _DBG("value %s", dump_p->str);
+                       } else {
+                               g_string_append_printf(dump_p, "%ld",
+                                               (new_state.time_s - base_time));
+                               _DBG("value %s", dump_p->str);
+                       }
+                       old_state.time_s = new_state.time_s;
+                       _DBG("Value old_state.time_s %ld", old_state.time_s);
+               } else { //: Let's skip the NC mode in initial version
+               }
+               if (new_state.cmd_s == CM_ST) {
+                       if (chkin) {
+                               dump_p = g_string_append_c(dump_p, ':');
+                               _DBG("value %s", dump_p->str);
+                       } else {
+                               //
+                       }
+                       dump_p = g_string_append(dump_p, "START\n");
+                       _DBG("value %s", dump_p->str);
+                       bd_print_history_item_reset(&old_state);
+               } else if (new_state.cmd_s == CM_CRR_TIME || new_state.cmd_s == CM_RST) {
+                       if (chkin) {
+                               dump_p = g_string_append_c(dump_p, ':');
+                               _DBG("value %s", dump_p->str);
+                       }
+                       if (new_state.cmd_s == CM_RST) {
+                               dump_p = g_string_append(dump_p, "RESET:");
+                               _DBG("value %s", dump_p->str);
+                               bd_print_history_item_reset(&old_state);
+                       }
+                       dump_p = g_string_append(dump_p, "TIME:");
+                       _DBG("value %s", dump_p->str);
+                       if (chkin) {
+                               g_string_append_printf(dump_p, "%ld", new_state.time_current);
+                               _DBG("value %s", dump_p->str);
+                               dump_p = g_string_append(dump_p, "\n");
+                               _DBG("value %s", dump_p->str);
+                       } else {
+                               //
+                       }
+               } else if (new_state.cmd_s == CM_SHTDN) {
+                       if (chkin) {
+                               dump_p = g_string_append_c(dump_p, ':');
+                               _DBG("value %s", dump_p->str);
+                       } else {
+                               //
+                       }
+                       dump_p = g_string_append(dump_p, "SHUTDOWN\n");
+                       _DBG("value %s", dump_p->str);
+               } else if (new_state.cmd_s == CM_OVRFL) {
+                       if (chkin) {
+                               dump_p = g_string_append_c(dump_p, ':');
+                               _DBG("value %s", dump_p->str);
+                       }
+                       dump_p = g_string_append(dump_p, "*OVERFLOW*\n");
+                       _DBG("value %s", dump_p->str);
+               } else {
+                       if (!chkin) {
+                               //
+                       } else {
+                               if (old_state.battery_level != new_state.battery_level) {
+                                       old_state.battery_level = new_state.battery_level;
+                                       dump_p = g_string_append(dump_p, ",Bl=");
+                                       _DBG("value %s", dump_p->str);
+                                       g_string_append_printf(dump_p, "%d",
+                                                       new_state.battery_level);
+                                       _DBG("value %s", dump_p->str);
+                               }
+                       }
+               }
+               if (old_state.battery_status != new_state.battery_status) {
+                       old_state.battery_status = new_state.battery_status;
+                       dump_p = g_string_append(dump_p, ",Bs="); //:for NC
+                       _DBG("value %s", dump_p->str);
+                       switch (old_state.battery_status) {
+                               //: write for substates
+                       default:
+                               g_string_append_printf(dump_p, "%d",
+                                               new_state.battery_status);
+                               _DBG("value %s", dump_p->str);
+                               break;
+                       }
+               }
+               if (old_state.battery_health != new_state.battery_health) {
+                       old_state.battery_health = new_state.battery_health;
+                       dump_p = g_string_append(dump_p, ",Bh="); //:for NC
+                       _DBG("value %s", dump_p->str);
+                       switch (old_state.battery_health) {
+                               //: write for substates
+                       default:
+                               g_string_append_printf(dump_p, "%d",
+                                               new_state.battery_health);
+                               _DBG("value %s", dump_p->str);
+                               break;
+                       }
+               }
+               if (old_state.battery_plugtype != new_state.battery_plugtype) {
+                       old_state.battery_plugtype = new_state.battery_plugtype;
+                       dump_p = g_string_append(dump_p, ",Bp="); //:for NC
+                       _DBG("value %s", dump_p->str);
+                       switch (old_state.battery_plugtype) {
+                               //: write for substates
+                       default:
+                               g_string_append_printf(dump_p, "%d",
+                                               new_state.battery_plugtype);
+                               _DBG("value %s", dump_p->str);
+                               break;
+                       }
+               }
+               if (old_state.battery_temperature != new_state.battery_temperature) {
+                       old_state.battery_temperature = new_state.battery_temperature;
+                       dump_p = g_string_append(dump_p, ",Bt="); //:for NC
+                       _DBG("value %s", dump_p->str);
+                       switch (old_state.battery_temperature) {
+                               //: write for substates
+                       default:
+                               g_string_append_printf(dump_p, "%d",
+                                               new_state.battery_temperature);
+                               _DBG("value %s", dump_p->str);
+                               break;
+                       }
+               }
+               if (old_state.battery_voltage != new_state.battery_voltage) {
+                       old_state.battery_voltage = new_state.battery_voltage;
+                       dump_p = g_string_append(dump_p, ",Bv="); //:for NC
+                       _DBG("value %s", dump_p->str);
+                       g_string_append_printf(dump_p, "%d", new_state.battery_voltage);
+                       _DBG("value %s", dump_p->str);
+               }
+               if (old_state.battery_charge != new_state.battery_charge) {
+                       old_state.battery_charge = new_state.battery_charge;
+                       dump_p = g_string_append(dump_p, ",Bcc=");
+                       _DBG("value %s", dump_p->str);
+                       //Charge in MAh
+                       g_string_append_printf(dump_p, "%d", (new_state.battery_charge/1000));
+                       _DBG("value %s", dump_p->str);
+               }
+               int change = (old_state.state_1 ^ new_state.state_1);
+               if (change != 0) {
+                       bool wake_fl = false;
+                       int shifts = 1;
+                       for (int i = 0; i < 32; i++) {
+                               if ((change & shifts) != 0) {
+                                       dump_p = g_string_append_c(dump_p, ',');
+                                       //Considering only -ve shift cases
+                                       int shift = -1;
+                                       if (shift < 0) {
+                                               if ((new_state.state_1 & (1 << i)) != 0)
+                                                       dump_p = g_string_append_c(dump_p, '+');
+                                               else
+                                                       dump_p = g_string_append_c(dump_p, '-');
+                                               gchar *temp = g_strdup(state1_map[i]);
+                                               g_string_append(dump_p, temp);
+                                               g_free(temp);
+                                               //_DBG("value %s", dump_p->str);
+                                               if ((1 << i) == ST1_WAKE_LOCK_FLAG &&
+                                                               new_state.wakelock_tag != NULL) {
+                                                       wake_fl = true;
+                                                       dump_p = g_string_append_c(dump_p, '=');
+                                                       //_DBG("value %s", dump_p->str);
+                                                       if (chkin)
+                                                               g_string_append_printf(dump_p, "%d",
+                                                               new_state.wakelock_tag->hsp_idx);
+                                                       else{
+                                                               //: NC
+                                                       }
+                                                       //_DBG("value %s", dump_p->str);
+                                               }
+                                       } else {
+                                               //With non-negative shift
+                                       }
+                               }
+                               shifts = (shifts << 1);
+                       }
+                       if (!wake_fl && new_state.wakelock_tag != NULL) {
+                               dump_p = g_string_append(dump_p, ",w=");
+                               if (chkin) {
+                                       g_string_append_printf(dump_p, "%d",
+                                       new_state.wakelock_tag->hsp_idx);
+                                       //_DBG("value %s", dump_p->str);
+                               } else {
+                               }
+                       }
+               }
+               change = (old_state.state_2 ^ new_state.state_2);
+               if (change != 0) {
+                       int shifts = 1;
+                       for (int i = 0; i < 32; i++) {
+                               if ((change & shifts) != 0) {
+                                       dump_p = g_string_append_c(dump_p, ',');
+                                       //_DBG("value %s", dump_p->str);
+                                       int shift = -1;
+                                       if (shift < 0) {
+                                               if ((new_state.state_2 & (1 << i) & change) != 0)
+                                                       dump_p = g_string_append_c(dump_p, '+');
+                                               else
+                                                       dump_p = g_string_append_c(dump_p, '-');
+                                               //_DBG("value %s", dump_p->str);
+                                               gchar *temp = g_strdup(state2_map[i]);
+                                               g_string_append(dump_p, temp);
+                                               g_free(temp);
+                                               _DBG("value %s", dump_p->str);
+                                       }
+                               }
+                               shifts = (shifts << 1);
+                       }
+               }
+               if (new_state.wakelock_tag != NULL) {
+                       if (chkin) {
+                               dump_p = g_string_append(dump_p, ",wr=");
+                               _DBG("value %s", dump_p->str);
+                               g_string_append_printf(dump_p, "%d",
+                               new_state.wakereason_tag->hsp_idx);
+                               _DBG("value %s", dump_p->str);
+                       }
+               } else {
+                       //: for NC
+               }
+               if (new_state.event_code != ET_NONE) {
+                       dump_p = g_string_append_c(dump_p, ',');
+                       _DBG("value %s", dump_p->str);
+                       if ((new_state.event_code & ET_FLAG_START) != 0)
+                               dump_p = g_string_append_c(dump_p, '+');
+                       else if ((new_state.event_code & ET_FLAG_FINISH) != 0)
+                               dump_p = g_string_append_c(dump_p, '-');
+                       _DBG("value %s", dump_p->str);
+                       int k = new_state.event_code & ~(ET_FLAG_START | ET_FLAG_FINISH);
+                       if (k >= 0 && k < 32) {
+                               gchar* temp = g_strdup(event_map[k]);
+                               g_string_append(dump_p, temp);
+                               _DBG("value %s", dump_p->str);
+                       } else {
+                               dump_p = g_string_append(dump_p, "Ev");
+                               g_string_append_printf(dump_p, "%d", k);
+                               _DBG("value %s", dump_p->str);
+                       }
+                       dump_p = g_string_append_c(dump_p, '=');
+                       _DBG("value %s", dump_p->str);
+                       if (chkin) {
+                               g_string_append_printf(dump_p, "%d",
+                                       new_state.event_tag->hsp_idx);
+                       } else {
+                               //: for NC
+                       }
+               }
+               dump_p = g_string_append(dump_p, "\n");
+               //: step details
+               old_state.state_1 = new_state.state_1;
+               old_state.state_2 = new_state.state_2;
+               _DBG("State assigned");
+               _DBG("%s", dump_p->str);
+               g_free(dump_p->str);
+       }
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+static int bd_set_history_from_listener(history_item_s* nrec)
+{
+       ENTER;
+
+       int dvar1 = 0, dvar2 = 0;
+       time_t current_time;
+       time(&current_time); //This time needs to be changed to MT
+
+       history_data[counter].time_s = current_time;
+
+       dvar1 = ((nrec->cmd_s) & 0xff) | ((nrec->battery_level << 8) & 0xff00) |
+               ((nrec->battery_status << 16) & 0xf0000) |
+               ((nrec->battery_health << 20) & 0xf00000) |
+               ((nrec->battery_plugtype << 24) & 0xf000000) |
+               ((nrec->wakelock_tag != NULL) ? 0x10000000 : 0) |
+               ((nrec->wakereason_tag != NULL) ? 0x20000000 : 0) |
+               ((nrec->event_code != ET_NONE) ? 0x40000000 : 0);
+
+       dvar2 = ((nrec->battery_temperature) & 0xffff) |
+               ((nrec->battery_voltage << 16) & 0xffff0000);
+
+       _DBG("Value of encoded value 1 %d", dvar1);
+       _DBG("Value of encoded value 2 %d", dvar2);
+
+       history_data[counter].dump_1 = dvar1;
+       history_data[counter].dump_2 = dvar2;
+       history_data[counter].battery_chargeUAh = nrec->battery_charge;
+
+       if (nrec->wakelock_tag != NULL)
+               history_data[counter].wakelock_tag = nrec->wakelock_tag;
+
+       if (nrec->wakereason_tag != NULL)
+               history_data[counter].wakereason_tag = nrec->wakereason_tag;
+
+       if (nrec->event_code != ET_NONE) {
+               history_data[counter].event_code = nrec->event_code;
+               history_data[counter].event_tag = nrec->event_tag;
+       }
+
+       if (nrec->cmd_s == CM_CRR_TIME || nrec->cmd_s == CM_RST)
+               history_data[counter].time_current = current_time;
+
+       history_data[counter].state_1 = nrec->state_1;
+       history_data[counter].state_2 = nrec->state_2;
+
+       if (counter < HISTORY_SIZE_MAX)
+               counter++;
+       else {
+               //: Give some buffer
+               int ret = bd_print_history_item_main(0, current_time, 1);
+               if (ret != BATTERY_MONITOR_ERROR_NONE)
+                       return ret;
+
+               counter = 0;
+       }
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+int bd_store_history_item(history_item_s* nrec)
+{
+       ENTER;
+
+       if (nrec == NULL) {
+               _ERR("Invalid history item");
+               return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
+       }
+
+       int ret = BATTERY_MONITOR_ERROR_NONE;
+       ret = bd_set_history_from_listener(nrec);
+       if (ret != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("Failed to store history");
+               return BATTERY_MONITOR_ERROR_INTERNAL;
+       }
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+//: remove this
+static int bd_test_fn()
+{
+       ENTER;
+
+       history_item_s ts;
+
+       ts.cmd_s = CM_UPD;
+       ts.time_s = 0;
+       ts.time_current = 0;
+
+       ts.state_1 = 0;
+       ts.state_1 = ST1_WIFI_SCAN_FLAG;
+       ts.state_1 |= (ST1_SCREEN_ON_FLAG);
+       ts.state_2 = ST2_VIDEO_ON_FLAG;
+       ts.battery_level = 99;
+       ts.battery_status = 'd';
+       ts.battery_health = 'g';
+       ts.battery_plugtype = 'a';
+       ts.battery_temperature = 226;
+       ts.battery_voltage = 400;
+       ts.battery_charge = 0;
+       ts.event_code = ET_NONE;
+       ts.event_tag = NULL;
+       ts.wakelock_tag = NULL;
+       ts.wakereason_tag = NULL;
+       bd_set_history_from_listener(&ts);
+
+       ts.state_1 &= ~(ST1_WIFI_SCAN_FLAG);
+       bd_set_history_from_listener(&ts);
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+static int bd_make_string_maps()
+{
+       ENTER;
+
+       bd_test_fn();
+       history_item_s ts;
+       bd_get_history_detail_from_dump(0, &ts);
+       bd_get_history_detail_from_dump(1, &ts);
+       bd_print_history_item_main(0, 1576753138, true);
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+int bd_initialize_battery_dump()
+{
+       ENTER;
+
+       counter = 0;
+       _DBG("Battery Dump Init Successful");
+       bd_make_string_maps(); //add  return integer check.
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+int bd_deinitialize_battery_dump()
+{
+       ENTER;
+
+       _INFO("Battery Dump De-Init Successful");
+
+       EXIT;
+       return BATTERY_MONITOR_ERROR_NONE;
+}
+
+//Remove all DBG statements
index 6b97dc8..a0aece3 100644 (file)
@@ -27,6 +27,7 @@
 #include "bm_power_engine.h"
 #include "bm_util.h"
 #include "bm_common.h"
+#include "bd_history_item.h"
 
 static GMainLoop *mainloop = NULL;
 static guint owner_id = 0;
@@ -400,6 +401,13 @@ static void bm_server_initialize()
                goto END;
        }
 
+       /* battery-dump initialization */
+       return_code = bd_initialize_battery_dump();
+       if (return_code != BATTERY_MONITOR_ERROR_NONE) {
+               _ERR("battery-dump initialization failed");
+               goto END;
+       }
+
        _INFO("SUCCESS");
 
        EXIT;
@@ -426,6 +434,9 @@ static void bm_server_deinitialize()
        /* power-engine deinitialization */
        deinitialize_power_engine();
 
+       /* battery-dump deinitialization */
+       bd_deinitialize_battery_dump();
+
        _INFO("de-initialization success");
 
        EXIT;