From: Abhishek Vijay Date: Tue, 3 Sep 2019 08:56:16 +0000 (+0530) Subject: correcting handle usage in API's X-Git-Tag: submit/tizen/20190919.020747~10 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a890a5520d5671cfc81fff7a72b7fe033a3f8e30;p=platform%2Fcore%2Fapi%2Fbatterymonitor.git correcting handle usage in API's Change-Id: Ic8b3987631c32d4b43dcddd57ccf44af64f06f3e Signed-off-by: Abhishek Vijay --- diff --git a/include/battery_monitor.h b/include/battery_monitor.h index 146081d..1b2604d 100644 --- a/include/battery_monitor.h +++ b/include/battery_monitor.h @@ -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. diff --git a/include/battery_monitor_internal.h b/include/battery_monitor_internal.h index 910b311..54a6d9c 100644 --- a/include/battery_monitor_internal.h +++ b/include/battery_monitor_internal.h @@ -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 diff --git a/include/battery_monitor_util.h b/include/battery_monitor_util.h index e12b039..4f9494b 100644 --- a/include/battery_monitor_util.h +++ b/include/battery_monitor_util.h @@ -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_h data_handle); +void unmarshal_serialized_data(GVariant *handle, battery_monitor_total_consumption_s *data_handle); #ifdef __cplusplus } diff --git a/src/battery_monitor.c b/src/battery_monitor.c index c96da2b..7c8db9c 100644 --- a/src/battery_monitor.c +++ b/src/battery_monitor.c @@ -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; diff --git a/src/battery_monitor_util.c b/src/battery_monitor_util.c index 5b474c9..bc92149 100644 --- a/src/battery_monitor_util.c +++ b/src/battery_monitor_util.c @@ -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_h 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_h 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) { diff --git a/test/test_cli_app/src/bmt_usage.c b/test/test_cli_app/src/bmt_usage.c index 661dae1..1b5682a 100644 --- a/test/test_cli_app/src/bmt_usage.c +++ b/test/test_cli_app/src/bmt_usage.c @@ -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; } } }