+static int front = -1, end = -1, is_full = 0;
+
+static int bd_dump_data_item_reset(dump_data_s* old_state)
+{
+ ENTER;
+ BM_CHECK_INPUT_PARAM(old_state);
+
+ old_state->time_s = -1;
+ old_state->time_current = -1;
+ old_state->dump_1 = 0;
+ old_state->dump_2 = 0;
+ old_state->state_1 = 0;
+ old_state->state_2 = 0;
+ old_state->battery_chargeUAh = 0;
+ old_state->usage_type = 0;
+ old_state->usage = 0;
+ old_state->event_code = ET_NONE;
+ old_state->event_tag = NULL;
+ old_state->wakelock_tag = NULL;
+ old_state->wakereason_tag = NULL;
+
+ EXIT;
+ return BATTERY_MONITOR_ERROR_NONE;
+}
+
+static void bd_clean_circ_buffer_front(int front_indx)
+{
+ ENTER;
+
+ if (history_data[front_indx].wakelock_tag != NULL) {
+ g_string_free(history_data[front_indx].wakelock_tag->string_info, TRUE);
+ free(history_data[front_indx].wakelock_tag);
+ }
+ if (history_data[front_indx].wakereason_tag != NULL) {
+ g_string_free(history_data[front_indx].wakereason_tag->string_info, TRUE);
+ free(history_data[front_indx].wakereason_tag);
+ }
+ if (history_data[front_indx].event_tag != NULL) {
+ g_string_free(history_data[front_indx].event_tag->string_info, TRUE);
+ free(history_data[front_indx].event_tag);
+ }
+
+ bd_dump_data_item_reset(&history_data[front_indx]);
+
+ EXIT;
+ return;
+}
+
+static int bd_get_index()
+{
+ ENTER;
+
+ if ((front == 0 && end == HISTORY_SIZE_MAX - 1) || (front == end + 1)) {
+ _DBG("buffer is full");
+ is_full = 1;
+ bd_clean_circ_buffer_front(front);
+ front = (front + 1) % HISTORY_SIZE_MAX;
+ /* Prepare TIME Node */
+ history_data[front].dump_1 = 0;
+ history_data[front].dump_1 |= CM_CRR_TIME;
+ history_data[front].time_current = history_data[front].time_s;
+ history_data[front].state_1 = 0;
+ history_data[front].state_2 = 0;
+ history_data[front].event_code = ET_NONE;
+ }
+
+ if (front == -1) {
+ _DBG("buffer is empty");
+ front = end = 0;
+ } else {
+ if (end == HISTORY_SIZE_MAX - 1)
+ end = 0;
+ else
+ end = end + 1;
+ }
+
+ EXIT;
+ return end;
+}