correcting handle usage in API's 55/213255/1
authorAbhishek Vijay <abhishek.v@samsung.com>
Tue, 3 Sep 2019 08:56:16 +0000 (14:26 +0530)
committerAbhishek Vijay <abhishek.v@samsung.com>
Tue, 3 Sep 2019 08:56:16 +0000 (14:26 +0530)
Change-Id: Ic8b3987631c32d4b43dcddd57ccf44af64f06f3e
Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
include/battery_monitor.h
include/battery_monitor_internal.h
include/battery_monitor_util.h
src/battery_monitor.c
src/battery_monitor_util.c
test/test_cli_app/src/bmt_usage.c

index 146081df90f25d2f9cbb4961e3d59beb3bf3fe24..1b2604d8a745ad6add33b5c70bb5edc279af3fbd 100644 (file)
@@ -161,7 +161,7 @@ int battery_monitor_get_usage_for_resource_id(battery_monitor_h handle, battery_
  * @retval #BATTERY_MONITOR_ERROR_RECORD_NOT_FOUND      Record Not found
  * @retval #BATTERY_MONITOR_ERROR_INTERNAL              Internal Error
  */
-int battery_monitor_get_usage_by_app_id_for_all_resource_id(char *app_id, battery_monitor_duration_type_e duration, battery_monitor_h handle);
+int battery_monitor_get_usage_by_app_id_for_all_resource_id(char *app_id, battery_monitor_duration_type_e duration, battery_monitor_h *handle);
 
 /**
  * @brief  Gets the battery-percent by an app-id for a specific resource.
index 910b31176140b1147c3e2c3fadd76b5414b9badf..54a6d9ca3faf2056c99c798a0f3dd48a8f6be78d 100644 (file)
@@ -27,7 +27,7 @@ extern "C" {
  * @brief        Structure for total battery consumption
  * @since_tizen  5.5
  */
-struct battery_monitor_total_consumption_s {
+typedef struct _battery_monitor_total_consumption_s {
        int ble_val;   /**< Battery consumption by BLE(Bluetooth Low Energy) */
        int wifi_val;  /**< Battery consumption by Wi-Fi */
        int cpu_val;   /**< Battery consumption by CPU */
@@ -36,7 +36,7 @@ struct battery_monitor_total_consumption_s {
        int gps_val;   /**< Battery consumption by GPS */
        int hrm_val;   /**< Battery consumption by HRM(Heart Rate Monitor) sensor */
        int bat_val;   /**< Total battery consumption/ Change in battery level */
-};
+} battery_monitor_total_consumption_s;
 
 
 #ifdef __cplusplus
index e12b039b2454b62260d9d05db10494725b0152d4..4f9494b357caebb74abe9f7f562c0680102de2f9 100644 (file)
@@ -140,7 +140,7 @@ extern "C" {
 
 int battery_monitor_get_error_code(bool is_success, GError *error);
 
-void unmarshal_serialized_data(GVariant *handle, battery_monitor_data_handle);
+void unmarshal_serialized_data(GVariant *handle, battery_monitor_total_consumption_s *data_handle);
 
 #ifdef __cplusplus
 }
index c96da2b3d5eec62d8d993a5f4e88ee59faa64172..7c8db9ca08f411781303cd425ac53262dde02358 100644 (file)
@@ -124,12 +124,10 @@ BM_API int battery_monitor_create(battery_monitor_h *handle)
 
        BM_CHECK_INPUT_PARAM(handle);
 
-       battery_monitor_h data_handle = (battery_monitor_h)calloc(1, sizeof(struct battery_monitor_total_consumption_s));
+       battery_monitor_total_consumption_s *data_handle = \
+               (battery_monitor_total_consumption_s *)calloc(1, sizeof(battery_monitor_total_consumption_s));
 
-       if (data_handle == NULL) {
-               _ERR("Memory Allocation Failed");
-               return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
-       }
+       BM_RETURN_VAL((data_handle != NULL), {}, BATTERY_MONITOR_ERROR_OUT_OF_MEMORY, "memory allocation failed");
 
        data_handle->ble_val = 0;
        data_handle->wifi_val = 0;
@@ -140,7 +138,7 @@ BM_API int battery_monitor_create(battery_monitor_h *handle)
        data_handle->hrm_val = 0;
        data_handle->bat_val = 0;
 
-       *handle = data_handle;
+       *handle = (battery_monitor_h)data_handle;
 
        EXIT;
        return BATTERY_MONITOR_ERROR_NONE;
@@ -176,30 +174,33 @@ BM_API int battery_monitor_get_usage_for_resource_id(battery_monitor_h handle, b
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
+       battery_monitor_total_consumption_s *data_handle = \
+                               (battery_monitor_total_consumption_s *)handle;
+
        switch (resource_id) {
        case BATTERY_MONITOR_RESOURCE_ID_BLE:
-               *battery_usage = handle->ble_val;
+               *battery_usage = data_handle->ble_val;
                break;
        case BATTERY_MONITOR_RESOURCE_ID_WIFI:
-               *battery_usage = handle->wifi_val;
+               *battery_usage = data_handle->wifi_val;
                break;
        case BATTERY_MONITOR_RESOURCE_ID_CPU:
-               *battery_usage = handle->cpu_val;
+               *battery_usage = data_handle->cpu_val;
                break;
        case BATTERY_MONITOR_RESOURCE_ID_DISPLAY:
-               *battery_usage = handle->dp_val;
+               *battery_usage = data_handle->dp_val;
                break;
        case BATTERY_MONITOR_RESOURCE_ID_DEVICE_NETWORK:
-               *battery_usage = handle->dn_val;
+               *battery_usage = data_handle->dn_val;
                break;
        case BATTERY_MONITOR_RESOURCE_ID_GPS_SENSOR:
-               *battery_usage = handle->gps_val;
+               *battery_usage = data_handle->gps_val;
                break;
        case BATTERY_MONITOR_RESOURCE_ID_HRM_SENSOR:
-               *battery_usage = handle->hrm_val;
+               *battery_usage = data_handle->hrm_val;
                break;
        case BATTERY_MONITOR_RESOURCE_ID_BATTERY:
-               *battery_usage = handle->bat_val;
+               *battery_usage = data_handle->bat_val;
                break;
        default:
                break;
@@ -209,14 +210,14 @@ BM_API int battery_monitor_get_usage_for_resource_id(battery_monitor_h handle, b
        return BATTERY_MONITOR_ERROR_NONE;
 }
 
-BM_API int battery_monitor_get_usage_by_app_id_for_all_resource_id(char* app_id, battery_monitor_duration_type_e duration, battery_monitor_h handle)
+BM_API int battery_monitor_get_usage_by_app_id_for_all_resource_id(char* app_id, battery_monitor_duration_type_e duration, battery_monitor_h *handle)
 {
        ENTER;
 
        CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
 
        BM_CHECK_INPUT_PARAM(app_id);
-       BM_CHECK_INPUT_PARAM(handle);
+       BM_CHECK_INPUT_PARAM(*handle);
        BM_RETURN_VAL((duration < BATTERY_MONITOR_DURATION_TYPE_MAX), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration");
 
        _INFO("total consumption requested for [%s] for duration [%d]", app_id, duration);
@@ -241,15 +242,20 @@ BM_API int battery_monitor_get_usage_by_app_id_for_all_resource_id(char* app_id,
        BM_CATCH_ERROR((is_success != false), {}, battery_monitor_get_error_code(is_success, error), "Failed to get dbus.");
        g_clear_error(&error);
 
-       battery_monitor_h resource_handle = handle;
+       battery_monitor_total_consumption_s *data_handle = \
+               (battery_monitor_total_consumption_s *)calloc(1, sizeof(battery_monitor_total_consumption_s));
+
+       BM_RETURN_VAL((data_handle != NULL), {}, BATTERY_MONITOR_ERROR_OUT_OF_MEMORY, "memory allocation failed");
 
        _INFO("before un-marshal data");
 
-       unmarshal_serialized_data(handle_variant, resource_handle);
+       unmarshal_serialized_data(handle_variant, data_handle);
        g_variant_unref(handle_variant);
 
        _INFO("after un-marshal data");
 
+       *handle = (battery_monitor_h)data_handle;
+
        _INFO("battery_get_usage_by_app_id_for_all_resource_id() - SUCCESS");
 
        EXIT;
index 5b474c9929ec35ad25f88d5ed41c852c4024d502..bc92149479828226d871f1c88ea0d537e0f7b519 100644 (file)
@@ -75,12 +75,17 @@ int battery_monitor_get_error_code(bool is_success, GError *error)
        return BATTERY_MONITOR_ERROR_NONE;
 }
 
-void unmarshal_serialized_data(GVariant *handle, battery_monitor_data_handle)
+void unmarshal_serialized_data(GVariant *handle, battery_monitor_total_consumption_s *data_handle)
 {
        ENTER;
 
        if (!handle) {
-               _ERR("null handle received");
+               _ERR("invalid variant handle");
+               return;
+       }
+
+       if (!data_handle) {
+               _ERR("invalid data handle");
                return;
        }
 
@@ -90,7 +95,7 @@ void unmarshal_serialized_data(GVariant *handle, battery_monitor_h data_handle)
        gchar *key = NULL;
        GVariant *value = NULL;
 
-       battery_monitor_data = data_handle;
+       battery_monitor_total_consumption_s *data = data_handle;
 
        while (g_variant_iter_loop(&iter, "{sv}", &key, &value)) {
                if (g_strcmp0(key, BATTERY_MONITOR_BLE_DATA_ID) == 0) {
index 661dae181124c656ffcc1c3e2f030a2c5704a1f8..1b5682adc411616001c0f37333c44ba93e70f579 100644 (file)
@@ -118,12 +118,6 @@ static gboolean bmt_testapp_get_total_usage_by_app_id_for_all_resource_id()
 
        battery_monitor_h data_handle = NULL;
 
-       error_code = battery_monitor_create(&data_handle);
-       if (error_code != BATTERY_MONITOR_ERROR_NONE) {
-               testapp_print(" \n Memory Allocation failed \n");
-               return FALSE;
-       }
-
        testapp_print("\n Input AppID using Look Up table:\n");
        if (0 >= scanf("%s", app_name)) {
                testapp_print("Invalid input ");
@@ -136,7 +130,7 @@ static gboolean bmt_testapp_get_total_usage_by_app_id_for_all_resource_id()
                return FALSE;
        }
 
-       error_code = battery_monitor_get_usage_by_app_id_for_all_resource_id(app_name, duration, data_handle);
+       error_code = battery_monitor_get_usage_by_app_id_for_all_resource_id(app_name, duration, &data_handle);
 
        if (error_code == BATTERY_MONITOR_ERROR_NONE) {
                testapp_print ("Valid Handle Received, Value of error_code is [%d] \n", error_code);
@@ -147,36 +141,35 @@ static gboolean bmt_testapp_get_total_usage_by_app_id_for_all_resource_id()
                        battery_usage = -1;
                        error_code = battery_monitor_get_usage_for_resource_id(data_handle, id, &battery_usage);
 
-                       if (error_code != BATTERY_MONITOR_ERROR_NONE)
+                       if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                                testapp_print ("Error Received, Value of error_code is [%d] \n", error_code);
+                               continue;
+                       }
 
-                       else {
-                               switch (id) {
-                               case BATTERY_MONITOR_RESOURCE_ID_BLE:
-                                       testapp_print ("Resource ID : BT App Usage Value = [%d] \n", battery_usage);
-                                       break;
-                               case BATTERY_MONITOR_RESOURCE_ID_WIFI:
-                                       testapp_print ("Resource ID : WIFI App Usage Value = [%d] \n", battery_usage);
-                                       break;
-                               case BATTERY_MONITOR_RESOURCE_ID_CPU:
-                                       testapp_print ("Resource ID : CPU App Usage Value = [%d] \n", battery_usage);
-                                       break;
-                               case BATTERY_MONITOR_RESOURCE_ID_DISPLAY:
-                                       testapp_print ("Resource ID : DISPLAY App Usage Value = [%d] \n", battery_usage);
-                                       break;
-                               case BATTERY_MONITOR_RESOURCE_ID_DEVICE_NETWORK:
-                                       testapp_print ("Resource ID : DEVICE NETWORK App Usage Value = [%d] \n", battery_usage);
-                                       break;
-                               case BATTERY_MONITOR_RESOURCE_ID_GPS_SENSOR:
-                                       testapp_print ("Resource ID : GPS App Usage Value = [%d] \n", battery_usage);
-                                       break;
-                               case BATTERY_MONITOR_RESOURCE_ID_HRM_SENSOR:
-                                       testapp_print ("Resource ID : HRM App Usage Value = [%d] \n", battery_usage);
-                                       break;
-
-                               default:
-                                       break;
-                               }
+                       switch (id) {
+                       case BATTERY_MONITOR_RESOURCE_ID_BLE:
+                               testapp_print ("Resource ID : BT App Usage Value = [%d] \n", battery_usage);
+                               break;
+                       case BATTERY_MONITOR_RESOURCE_ID_WIFI:
+                               testapp_print ("Resource ID : WIFI App Usage Value = [%d] \n", battery_usage);
+                               break;
+                       case BATTERY_MONITOR_RESOURCE_ID_CPU:
+                               testapp_print ("Resource ID : CPU App Usage Value = [%d] \n", battery_usage);
+                               break;
+                       case BATTERY_MONITOR_RESOURCE_ID_DISPLAY:
+                               testapp_print ("Resource ID : DISPLAY App Usage Value = [%d] \n", battery_usage);
+                               break;
+                       case BATTERY_MONITOR_RESOURCE_ID_DEVICE_NETWORK:
+                               testapp_print ("Resource ID : DEVICE NETWORK App Usage Value = [%d] \n", battery_usage);
+                               break;
+                       case BATTERY_MONITOR_RESOURCE_ID_GPS_SENSOR:
+                               testapp_print ("Resource ID : GPS App Usage Value = [%d] \n", battery_usage);
+                               break;
+                       case BATTERY_MONITOR_RESOURCE_ID_HRM_SENSOR:
+                               testapp_print ("Resource ID : HRM App Usage Value = [%d] \n", battery_usage);
+                               break;
+                       default:
+                               break;
                        }
                }
        }