[Non-ACR] remove HRM & improve code readability 10/238110/1 accepted/tizen/unified/20200714.065728 submit/tizen/20200713.113326
authorAbhishek Vijay <abhishek.v@samsung.com>
Wed, 8 Jul 2020 13:30:16 +0000 (19:00 +0530)
committerAbhishek Vijay <abhishek.v@samsung.com>
Wed, 8 Jul 2020 13:30:16 +0000 (19:00 +0530)
Change-Id: I66012eabcfce1d66b6425fb419c0d3e3b94b01bf

include/battery_monitor_internal.h
src/battery_monitor.c
src/battery_monitor_util.c
test/test_cli_app/src/bmt_usage.c

index 54a6d9ca3faf2056c99c798a0f3dd48a8f6be78d..0910808a31cc101ea7f2709fa64000245aff907c 100644 (file)
@@ -34,7 +34,6 @@ typedef struct _battery_monitor_total_consumption_s {
        int dp_val;    /**< Battery consumption by display */
        int dn_val;    /**< Battery consumption by network */
        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;
 
index c8804c1b229bd96519ab53ae80f161c1b66afbb6..64b8b0a3ed0ee899df3ee5f1dbeb7c0ce10d58b6 100644 (file)
@@ -119,7 +119,12 @@ static BatteryMonitor* battery_monitor_get_instance()
 static int battery_monitor_check_time_interval(time_t *start_time, time_t *end_time)
 {
        ENTER;
+
+       BM_CHECK_INPUT_PARAM(start_time);
+       BM_CHECK_INPUT_PARAM(end_time);
+
        time_t now;
+       /* get current time */
        time(&now);
        if (now == -1) {
                _ERR("Time failed");
@@ -127,6 +132,7 @@ static int battery_monitor_check_time_interval(time_t *start_time, time_t *end_t
        }
 
        _DBG("current time [%ld]", now);
+
        if (*start_time >= 0 && *start_time < *end_time) {
                if ((*start_time >= (now - tmin)) && (*end_time <= now))
                        _INFO("Time Checked");
@@ -160,7 +166,8 @@ BM_API int battery_monitor_battery_usage_data_destroy(battery_usage_data_h resul
        return BATTERY_MONITOR_ERROR_NONE;
 }
 
-BM_API int battery_monitor_usage_data_get_power_usage_per_resource(battery_usage_data_h result, battery_monitor_resource_id_e resource, double *usage)
+BM_API int battery_monitor_usage_data_get_power_usage_per_resource(battery_usage_data_h result,
+                                       battery_monitor_resource_id_e resource, double *usage)
 {
        ENTER;
 
@@ -173,7 +180,9 @@ BM_API int battery_monitor_usage_data_get_power_usage_per_resource(battery_usage
                _ERR("Unknown resource id specified");
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
+
        int error_code = BATTERY_MONITOR_ERROR_NONE;
+
        error_code = battery_monitor_resource_id_is_supported(resource);
        if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("Not Supported Resource ID");
@@ -210,44 +219,57 @@ BM_API int battery_monitor_usage_data_get_power_usage_per_resource(battery_usage
        return BATTERY_MONITOR_ERROR_NONE;
 }
 
-BM_API int battery_monitor_get_power_usage_by_app_for_all_resources(const char* appid, time_t from, time_t to, battery_usage_data_h *result)
+BM_API int battery_monitor_get_power_usage_by_app_for_all_resources(const char* appid,
+                                       time_t from, time_t to, battery_usage_data_h *result)
 {
        ENTER;
+
        CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
+
        BM_CHECK_INPUT_PARAM(appid);
        BM_CHECK_INPUT_PARAM(result);
 
-       int error_code = BATTERY_MONITOR_ERROR_NONE;
-       error_code = battery_monitor_check_time_interval(&from, &to);
+       int error_code = battery_monitor_check_time_interval(&from, &to);
        if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("Invalid Time Interval: Invalid Params");
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
-       long long int start_time = from; long long int end_time = to;
-       _INFO("Consumption requested for [%s] for start_time(s) [%lld] and end_time(s) [%lld]", appid, start_time, end_time);
+       long long int start_time = from, end_time = to;
+
+       _INFO("Consumption requested for [%s] for start_time(s) [%lld] and end_time(s) [%lld]", \
+                                                               appid, start_time, end_time);
+
        GError *error = NULL;
        bm_server = battery_monitor_get_instance();
+
        BM_CATCH_ERROR((bm_server != NULL), {}, BATTERY_MONITOR_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
 
        GVariant *handle_variant = NULL;
 
        _INFO("before IPC call - get_usage_handle_by_app_id_for_all_resource_id_custom_interval_sync()");
-       bool is_success = battery_monitor_call_get_usage_handle_by_app_id_for_all_resource_id_custom_interval_sync(bm_server, appid, start_time, end_time,
-                                                                               (int)getuid(), &handle_variant, NULL, &error);
+
+       bool is_success = \
+               battery_monitor_call_get_usage_handle_by_app_id_for_all_resource_id_custom_interval_sync(bm_server, \
+                                       appid, start_time, end_time, (int)getuid(), &handle_variant, NULL, &error);
 
        battery_monitor_release_instance();
+
        _INFO("after IPC call - get_usage_handle_by_app_id_for_all_resource_id_custom_interval_sync()");
 
        BM_CATCH_ERROR((is_success != false), {}, battery_monitor_get_error_code(is_success, error), "Failed to get dbus.");
+
        g_clear_error(&error);
+
        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, data_handle);
+
        g_variant_unref(handle_variant);
 
        _INFO("after un-marshal data");
@@ -264,8 +286,8 @@ CATCH:
        return error_code;
 }
 
-BM_API int battery_monitor_get_power_usage_by_app_per_resource(const char* appid, battery_monitor_resource_id_e resource,
-                                                                       time_t from, time_t to, double *usage)
+BM_API int battery_monitor_get_power_usage_by_app_per_resource(const char* appid,
+               battery_monitor_resource_id_e resource, time_t from, time_t to, double *usage)
 {
        ENTER;
 
@@ -279,8 +301,7 @@ BM_API int battery_monitor_get_power_usage_by_app_per_resource(const char* appid
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
-       int error_code = BATTERY_MONITOR_ERROR_NONE;
-       error_code = battery_monitor_resource_id_is_supported(resource);
+       int error_code = battery_monitor_resource_id_is_supported(resource);
        if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("Not Supported Resource ID");
                return BATTERY_MONITOR_ERROR_RECORD_NOT_FOUND;
@@ -292,17 +313,24 @@ BM_API int battery_monitor_get_power_usage_by_app_per_resource(const char* appid
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
-       long long int start_time = from; long long int end_time = to;
+       long long int start_time = from, end_time = to;
        double battery_consumption = 0;
        GError *error = NULL;
 
-       _INFO("Consumption requested for [%s], resource_id(%d) for start_time(s) [%lld] and end_time(s) [%lld]", appid, resource, start_time, end_time);
+       _INFO("Consumption requested for [%s], \
+               resource_id(%d) for start_time(s) [%lld] and end_time(s) [%lld]", \
+                                       appid, resource, start_time, end_time);
+
        bm_server = battery_monitor_get_instance();
+
        BM_CATCH_ERROR((bm_server != NULL), {}, BATTERY_MONITOR_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
 
-       _INFO("before IPC call - app_id(%s), resource_id(%d), start_time(%lld), end_time(%lld)", appid, resource, start_time, end_time);
+       _INFO("before IPC call - app_id(%s), \
+               resource_id(%d), start_time(%lld), end_time(%lld)", \
+                                       appid, resource, start_time, end_time);
 
-       bool is_success = battery_monitor_call_get_mah_usage_by_app_id_for_resource_id_custom_interval_sync(bm_server, appid, resource,
+       bool is_success = \
+               battery_monitor_call_get_mah_usage_by_app_id_for_resource_id_custom_interval_sync(bm_server, appid, resource,
                                        start_time, end_time, (int)getuid(), &battery_consumption, NULL, &error);
 
        battery_monitor_release_instance();
@@ -333,25 +361,27 @@ BM_API int battery_monitor_get_power_usage_by_app(const char* appid, time_t from
        BM_CHECK_INPUT_PARAM(appid);
        BM_CHECK_INPUT_PARAM(usage);
 
-       int error_code = BATTERY_MONITOR_ERROR_NONE;
-       error_code = battery_monitor_check_time_interval(&from, &to);
+       int error_code = battery_monitor_check_time_interval(&from, &to);
        if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("Invalid Time Interval: Invalid Params");
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
-       long long int start_time = from; long long int end_time = to;
+       long long int start_time = from, end_time = to;
        double battery_consumption = 0;
        GError *error = NULL;
 
        _INFO("Consumption requested for app_id [%s] for start_time(s) [%lld] and end_time(s) [%lld]", appid, start_time, end_time);
+
        bm_server = battery_monitor_get_instance();
+
        BM_CATCH_ERROR((bm_server != NULL), {}, BATTERY_MONITOR_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
 
        _INFO("before IPC call - app_id(%s), start_time (%lld), end_time (%lld)", appid, start_time, end_time);
 
-       bool is_success = battery_monitor_call_get_total_mah_usage_by_app_id_custom_interval_sync(bm_server, appid, start_time, end_time,
-                                       (int)getuid(), &battery_consumption, NULL, &error);
+       bool is_success = \
+               battery_monitor_call_get_total_mah_usage_by_app_id_custom_interval_sync(bm_server, \
+                       appid, start_time, end_time, (int)getuid(), &battery_consumption, NULL, &error);
 
        battery_monitor_release_instance();
 
@@ -385,8 +415,7 @@ BM_API int battery_monitor_get_power_usage_by_resource(battery_monitor_resource_
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
-       int error_code = BATTERY_MONITOR_ERROR_NONE;
-       error_code = battery_monitor_resource_id_is_supported(resource);
+       int error_code = battery_monitor_resource_id_is_supported(resource);
        if (error_code != BATTERY_MONITOR_ERROR_NONE) {
                _ERR("Not Supported Resource ID");
                return BATTERY_MONITOR_ERROR_RECORD_NOT_FOUND;
@@ -398,18 +427,21 @@ BM_API int battery_monitor_get_power_usage_by_resource(battery_monitor_resource_
                return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
        }
 
-       long long int start_time = from; long long int end_time = to;
+       long long int start_time = from, end_time = to;
        double battery_consumption = 0;
        GError *error = NULL;
 
-       _INFO("Consumption requested for resource_id [%d] for start_time(s) [%lld] and end_time(s) [%lld]", resource, start_time, end_time);
+       _INFO("Consumption requested for resource_id [%d] for start_time(s) [%lld] and end_time(s) [%lld]", \
+                                                               resource, start_time, end_time);
+
        bm_server = battery_monitor_get_instance();
        BM_CATCH_ERROR((bm_server != NULL), {}, BATTERY_MONITOR_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
 
        _INFO("before IPC call - resource_id(%d), start_time (%lld), end_time (%lld)", resource, start_time, end_time);
 
-       bool is_success = battery_monitor_call_get_total_mah_usage_by_resource_id_custom_interval_sync(bm_server, resource, start_time,
-                                                       end_time, (int)getuid(), &battery_consumption, NULL, &error);
+       bool is_success = \
+               battery_monitor_call_get_total_mah_usage_by_resource_id_custom_interval_sync(bm_server,
+                               resource, start_time, end_time, (int)getuid(), &battery_consumption, NULL, &error);
 
        battery_monitor_release_instance();
 
index ec8f3d2f63b60c2d5513c20b2e9f076e086429fd..2c7fac400ab0c8e7d0907752ed197b8dd7ce4a67 100644 (file)
@@ -49,15 +49,14 @@ int battery_monitor_get_error_code(bool is_success, GError *error)
                                _INFO("Remote error[%s]", remote_error);
 
                                int error_enum_count = G_N_ELEMENTS(battery_monitor_svc_errors);
-                               int i = 0;
-                               for (i = 0; i < error_enum_count; i++) {
+                               for (int i = 0; i < error_enum_count; i++) {
                                        if (g_strcmp0(battery_monitor_svc_errors[i].dbus_error_name, remote_error) == 0) {
                                                _INFO("Remote error code matched[%d]", battery_monitor_svc_errors[i].error_code);
                                                g_free(remote_error);
                                                return battery_monitor_svc_errors[i].error_code;
                                                }
                                        }
-                               g_free(remote_error);
+                                       g_free(remote_error);
                                }
                        }
                        return BATTERY_MONITOR_ERROR_INTERNAL;
index c5db3ea68114afe9adab9960330af1e1f0e2216f..08f6750ddef7d11984cc33384fd46ea4a46f8bd2 100644 (file)
@@ -180,7 +180,7 @@ static gboolean bmt_testapp_get_total_power_usage_by_app_id_for_all_resource_id_
        if (error_code == BATTERY_MONITOR_ERROR_NONE) {
                testapp_print ("Valid Handle Received\n");
                double battery_usage = -1;
-#ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
+#ifdef DISABLE_FEATURE_DATA_FROM_GPS_PLUGIN
                for (int id = BATTERY_MONITOR_RESOURCE_ID_BLE; id < BATTERY_MONITOR_RESOURCE_ID_MAX; id++) {
 #else
                for (int id = BATTERY_MONITOR_RESOURCE_ID_BLE; id <= BATTERY_MONITOR_RESOURCE_ID_GPS_SENSOR; ++id) {
@@ -357,7 +357,7 @@ static gboolean bmt_testapp_get_power_usage_for_all_resource_id()
        }
        testapp_print("\n End Time: %lld \n", end_time);
 
-#ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
+#ifdef DISABLE_FEATURE_DATA_FROM_GPS_PLUGIN
        for (int id = BATTERY_MONITOR_RESOURCE_ID_BLE; id < BATTERY_MONITOR_RESOURCE_ID_MAX; id++) {
 #else
        for (int id = BATTERY_MONITOR_RESOURCE_ID_BLE; id <= BATTERY_MONITOR_RESOURCE_ID_GPS_SENSOR; id++) {
@@ -415,7 +415,7 @@ bool get_app_id_all_resource_cb(app_info_h app_info, void *user_data)
        error_code = battery_monitor_get_power_usage_by_app_for_all_resources(app_id, (time_t)(tv->from), (time_t)(tv->to), &data_handle);
        if (error_code == BATTERY_MONITOR_ERROR_NONE) {
                testapp_print ("Valid Handle Received for APPID [%s] \n", app_id);
-#ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
+#ifdef DISABLE_FEATURE_DATA_FROM_GPS_PLUGIN
                for (int id = BATTERY_MONITOR_RESOURCE_ID_BLE; id < BATTERY_MONITOR_RESOURCE_ID_MAX; id++) {
 #else
                for (int id = BATTERY_MONITOR_RESOURCE_ID_BLE; id <= BATTERY_MONITOR_RESOURCE_ID_GPS_SENSOR; ++id) {