Free stored history data upon deinit 82/225482/2
authorDewal Agarwal <d1.agarwal@samsung.com>
Thu, 20 Feb 2020 07:03:02 +0000 (12:33 +0530)
committerDewal Agarwal <d1.agarwal@samsung.com>
Mon, 24 Feb 2020 06:28:29 +0000 (11:58 +0530)
- Add history data dump free & reset
- Handle case of invalid events

Change-Id: I83ff741c9cd4573d0bdbef689646202aebb2bedb
Signed-off-by: Dewal Agarwal <d1.agarwal@samsung.com>
src/battery_dump/bd_history_item.c
src/battery_dump/bd_history_item_dummy.c

index 024bf2f..2283516 100644 (file)
@@ -678,7 +678,6 @@ int bd_print_history_item_reset(history_item_s* old_state)
        old_state->state_2 = 0;
        old_state->usage_type = 0;
        old_state->usage = 0;
-       //TEMP: Change to 0;
        old_state->battery_level = -1;
        old_state->battery_status = -1;
        old_state->battery_health = -1;
@@ -1123,6 +1122,65 @@ int bd_print_history_item_main(int fd, int num_h_items, long long base_time, boo
                return BATTERY_MONITOR_ERROR_NONE;
 }
 
+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 int bd_free_history_data_memory()
+{
+       ENTER;
+
+       int ret = BATTERY_MONITOR_ERROR_NONE;
+       h_flag = false; //Lock
+       _DBG("Free History Data Memory");
+       for (int i = 0; i < h_count; i++) {
+               if (history_data[i].wakelock_tag != NULL) {
+                       _DBG("WL str");
+                       g_string_free (history_data[i].wakelock_tag->string_info, TRUE);
+                       _DBG("WL");
+                       free(history_data[i].wakelock_tag);
+               }
+               if (history_data[i].wakereason_tag != NULL) {
+                       _DBG("WR str");
+                       g_string_free(history_data[i].wakereason_tag->string_info, TRUE);
+                       _DBG("WR");
+                       free(history_data[i].wakereason_tag);
+               }
+               if (history_data[i].event_tag != NULL) {
+                       _DBG("ET str");
+                       g_string_free(history_data[i].event_tag->string_info, TRUE);
+                       _DBG("ET");
+                       free(history_data[i].event_tag);
+               }
+               ret = bd_dump_data_item_reset(&history_data[i]);
+               if (ret != BATTERY_MONITOR_ERROR_NONE)
+                       _ERR("Internal error %d while freeing at idx %d", ret , i);
+       }
+
+       h_count = 0; h_flag = true;
+       EXIT;
+       return ret;
+}
+
 static int bd_set_history_from_listener(history_item_s* nrec)
 {
        ENTER;
@@ -1134,7 +1192,6 @@ static int bd_set_history_from_listener(history_item_s* nrec)
 
        time_t current_time = tv.tv_sec;
 
-
 #ifdef DUMP_DUMMY
        history_data[h_count].time_s = nrec->time_s;
 #else
@@ -1164,8 +1221,13 @@ static int bd_set_history_from_listener(history_item_s* nrec)
        if (nrec->wakereason_tag != NULL)
                history_data[h_count].wakereason_tag = nrec->wakereason_tag;
        if (nrec->event_code != ET_NONE) {
-               if (nrec->event_tag == NULL)
-                       _ERR("IT is NULL");
+               if (nrec->event_tag == NULL) {
+                       _ERR("ET is NULL but Event Code is not");
+                       BM_FREE(history_data[h_count].wakelock_tag);
+                       BM_FREE(history_data[h_count].wakereason_tag);
+                       bd_dump_data_item_reset(&history_data[h_count]);
+                       return BATTERY_MONITOR_ERROR_NO_DATA;
+               }
                history_data[h_count].event_code = nrec->event_code;
                history_data[h_count].event_tag = nrec->event_tag;
        }
@@ -1178,6 +1240,8 @@ static int bd_set_history_from_listener(history_item_s* nrec)
        history_data[h_count].usage_type = nrec->usage_type;
        history_data[h_count].usage = nrec->usage;
 
+       nrec->event_tag = NULL; nrec->wakereason_tag = NULL;
+       nrec->wakelock_tag = NULL;
        if (h_count < HISTORY_SIZE_MAX - 1)
                h_count++;
        else {
@@ -1187,7 +1251,11 @@ static int bd_set_history_from_listener(history_item_s* nrec)
                        h_count = 0;
                        return ret;
                }
-               h_count = 0;
+               ret = bd_free_history_data_memory();
+               if (ret != BATTERY_MONITOR_ERROR_NONE) {
+                       _WARN("Error while memory free %x", ret);
+                       return ret;
+               }
        }
 
        EXIT;
@@ -1287,6 +1355,10 @@ int bd_deinitialize_battery_dump()
                return BATTERY_MONITOR_ERROR_NOT_INITIALIZED;
        }
 
+       ret = bd_free_history_data_memory();
+       if (ret != BATTERY_MONITOR_ERROR_NONE)
+               _WARN("memory free failed (%x)", ret);
+
        _INFO("Battery Dump De-Init Successful");
 
        EXIT;
index 32545e8..08ae2fa 100644 (file)
@@ -69,7 +69,7 @@ static int create_event_tag(history_item_s* ts, int mask, int state, int on)
        ts->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
        if (ts->event_tag != NULL) {
                _DBG("Memory created successfully");
-               GString *temp = ts->event_tag->string_info;
+               GString *temp = NULL;
                temp = g_string_sized_new(30);
                BM_CHECK_MEM_ALLOC(temp, {});
                ts->event_tag->uid = 1000; //All events are associated with tizen
@@ -92,8 +92,8 @@ static int create_event_tag(history_item_s* ts, int mask, int state, int on)
                } else
                        ts->event_tag->sp_idx = pool_idx++;
 
-               _DBG("Value of ON is %d:" on);
-               temp = g_string_append(temp, "tizen");
+               _DBG("Value of ON is %d:", on);
+               g_string_append(temp, "tizen");
                ts->event_tag->string_info = temp;
                _DBG("%s", ts->event_tag->string_info->str);
        }
@@ -109,7 +109,7 @@ static int create_wakelock_tag(history_item_s* ts, int mask, int state, int on)
        ts->wakelock_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
        if (ts->wakelock_tag != NULL) {
                _DBG("Memory created successfully");
-               GString *temp = ts->wakelock_tag->string_info;
+               GString *temp = NULL;
                temp = g_string_sized_new(30);
                BM_CHECK_MEM_ALLOC(temp, {});
                ts->wakelock_tag->uid = 1000;
@@ -124,8 +124,8 @@ static int create_wakelock_tag(history_item_s* ts, int mask, int state, int on)
                } else
                        ts->wakelock_tag->sp_idx = pool_idx++;
 
-               _DBG("Value of ON is %d:" on);
-               temp = g_string_append(temp, "tizen");
+               _DBG("Value of ON is %d:", on);
+               g_string_append(temp, "tizen");
                ts->wakelock_tag->string_info = temp;
                _DBG("%s", ts->wakelock_tag->string_info->str);
        }