2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include "battery_monitor.h"
23 #include "battery_monitor_util.h"
25 #define BATTERY_MONITOR_RESOURCE_ID_MAX (BATTERY_MONITOR_RESOURCE_ID_HRM_SENSOR + 1)
26 #define BATTERY_MONITOR_DURATION_TYPE_MAX (BATTERY_MONITOR_DURATION_TYPE_1WEEK + 1)
28 static BatteryMonitor *bm_server = NULL;
29 static GDBusConnection *connection = NULL;
30 static int instance_count = 0;
31 static pthread_mutex_t bm_proxy_instance_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static void battery_monitor_destroy_instance()
37 g_object_unref(connection);
40 g_object_unref(bm_server);
47 static void battery_monitor_release_instance()
49 pthread_mutex_lock(&bm_proxy_instance_mutex);
50 BATTERY_MONITOR_DECREASE_COUNT(instance_count);
52 _INFO("instance count - %d", instance_count);
54 if (instance_count <= 0)
55 battery_monitor_destroy_instance();
57 pthread_mutex_unlock(&bm_proxy_instance_mutex);
60 static BatteryMonitor* battery_monitor_get_instance()
62 pthread_mutex_lock(&bm_proxy_instance_mutex);
64 if (bm_server != NULL) {
65 _DBG("server instance available");
66 BATTERY_MONITOR_INCREASE_COUNT(instance_count);
67 pthread_mutex_unlock(&bm_proxy_instance_mutex);
71 #if !GLIB_CHECK_VERSION(2, 35, 0)
77 connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
79 _INFO("after g_bus_get_sync");
83 _ERR("Unable to connect to gdbus: %s", error->message);
84 g_clear_error(&error);
86 pthread_mutex_unlock(&bm_proxy_instance_mutex);
90 g_clear_error(&error);
92 /* create server object */
93 bm_server = battery_monitor_proxy_new_sync(connection, G_DBUS_PROXY_FLAGS_NONE,
94 BATTERY_MONITOR_DBUS_NAME, BATTERY_MONITOR_DBUS_PATH, NULL, &error);
98 _ERR("Unable account_manager_proxy_new_sync: %s", error->message);
99 g_clear_error(&error);
102 g_object_unref(connection);
105 pthread_mutex_unlock(&bm_proxy_instance_mutex);
109 g_clear_error(&error);
111 BATTERY_MONITOR_INCREASE_COUNT(instance_count);
113 pthread_mutex_unlock(&bm_proxy_instance_mutex);
119 BM_API int battery_monitor_create(battery_monitor_h *handle)
123 CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
125 BM_CHECK_INPUT_PARAM(handle);
127 battery_monitor_total_consumption_s *data_handle = \
128 (battery_monitor_total_consumption_s *)calloc(1, sizeof(battery_monitor_total_consumption_s));
130 BM_RETURN_VAL((data_handle != NULL), {}, BATTERY_MONITOR_ERROR_OUT_OF_MEMORY, "memory allocation failed");
132 data_handle->ble_val = 0;
133 data_handle->wifi_val = 0;
134 data_handle->cpu_val = 0;
135 data_handle->dp_val = 0;
136 data_handle->dn_val = 0;
137 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
138 data_handle->gps_val = 0;
139 data_handle->hrm_val = 0;
140 data_handle->bat_val = 0;
141 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
143 *handle = (battery_monitor_h)data_handle;
146 return BATTERY_MONITOR_ERROR_NONE;
149 BM_API int battery_monitor_destroy(battery_monitor_h handle)
153 CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
155 BM_CHECK_INPUT_PARAM(handle);
157 battery_monitor_h data_handle = handle;
159 BM_FREE(data_handle);
162 return BATTERY_MONITOR_ERROR_NONE;
165 BM_API int battery_monitor_get_usage_for_resource_id(battery_monitor_h handle, battery_monitor_resource_id_e resource_id, int *battery_usage)
169 CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
171 BM_CHECK_INPUT_PARAM(handle);
172 BM_CHECK_INPUT_PARAM(battery_usage);
174 if (resource_id >= BATTERY_MONITOR_RESOURCE_ID_MAX) {
175 _ERR("Unknown resource id specified");
176 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
179 battery_monitor_total_consumption_s *data_handle = \
180 (battery_monitor_total_consumption_s *)handle;
182 switch (resource_id) {
183 case BATTERY_MONITOR_RESOURCE_ID_BLE:
184 *battery_usage = data_handle->ble_val;
186 case BATTERY_MONITOR_RESOURCE_ID_WIFI:
187 *battery_usage = data_handle->wifi_val;
189 case BATTERY_MONITOR_RESOURCE_ID_CPU:
190 *battery_usage = data_handle->cpu_val;
192 case BATTERY_MONITOR_RESOURCE_ID_DISPLAY:
193 *battery_usage = data_handle->dp_val;
195 case BATTERY_MONITOR_RESOURCE_ID_DEVICE_NETWORK:
196 *battery_usage = data_handle->dn_val;
198 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
199 case BATTERY_MONITOR_RESOURCE_ID_GPS_SENSOR:
200 *battery_usage = data_handle->gps_val;
202 case BATTERY_MONITOR_RESOURCE_ID_HRM_SENSOR:
203 *battery_usage = data_handle->hrm_val;
205 case BATTERY_MONITOR_RESOURCE_ID_BATTERY:
206 *battery_usage = data_handle->bat_val;
208 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
214 return BATTERY_MONITOR_ERROR_NONE;
217 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)
221 CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
223 BM_CHECK_INPUT_PARAM(app_id);
224 BM_CHECK_INPUT_PARAM(handle);
225 BM_RETURN_VAL((duration < BATTERY_MONITOR_DURATION_TYPE_MAX), {}, BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration");
227 _INFO("total consumption requested for [%s] for duration [%d]", app_id, duration);
229 int error_code = BATTERY_MONITOR_ERROR_NONE;
230 GError *error = NULL;
232 bm_server = battery_monitor_get_instance();
233 BM_CATCH_ERROR((bm_server != NULL), {}, BATTERY_MONITOR_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
235 GVariant *handle_variant = NULL;
237 _INFO("before IPC call - get_all_resource_usage_sync()");
239 bool is_success = battery_monitor_call_get_all_resource_usage_sync(bm_server, app_id, duration,
240 (int)getuid(), &handle_variant, NULL, &error);
242 battery_monitor_release_instance();
244 _INFO("after IPC call - get_all_resource_usage_sync()");
246 BM_CATCH_ERROR((is_success != false), {}, battery_monitor_get_error_code(is_success, error), "Failed to get dbus.");
247 g_clear_error(&error);
249 battery_monitor_total_consumption_s *data_handle = \
250 (battery_monitor_total_consumption_s *)calloc(1, sizeof(battery_monitor_total_consumption_s));
252 BM_RETURN_VAL((data_handle != NULL), {}, BATTERY_MONITOR_ERROR_OUT_OF_MEMORY, "memory allocation failed");
254 _INFO("before un-marshal data");
256 unmarshal_serialized_data(handle_variant, data_handle);
257 g_variant_unref(handle_variant);
259 _INFO("after un-marshal data");
261 *handle = (battery_monitor_h)data_handle;
263 _INFO("battery_get_usage_by_app_id_for_all_resource_id() - SUCCESS");
266 return BATTERY_MONITOR_ERROR_NONE;
268 g_clear_error(&error);
269 _ERR("battery_monitor_call_get_all_resource_usage_sync()=[%d]", error_code);
273 BM_API int battery_monitor_get_usage_by_app_id_for_resource_id(char* app_id, battery_monitor_resource_id_e resource_id,
274 battery_monitor_duration_type_e duration, int* battery_usage)
278 CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
280 BM_CHECK_INPUT_PARAM(app_id);
281 BM_CHECK_INPUT_PARAM(battery_usage);
283 if (resource_id >= BATTERY_MONITOR_RESOURCE_ID_MAX) {
284 _ERR("Unknown resource id specified");
285 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
288 if (duration >= BATTERY_MONITOR_DURATION_TYPE_MAX) {
289 _ERR("Unknown duration specified");
290 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
293 int battery_consumption = 0;
294 int error_code = BATTERY_MONITOR_ERROR_NONE;
295 GError *error = NULL;
297 bm_server = battery_monitor_get_instance();
298 BM_CATCH_ERROR((bm_server != NULL), {}, BATTERY_MONITOR_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
300 _INFO("before IPC call - app_id(%s), resource_id(%d), duration(%d)", app_id, resource_id, duration);
302 bool is_success = battery_monitor_call_get_usage_by_app_id_for_resource_id_sync(bm_server, app_id, resource_id,
303 duration, (int)getuid(), &battery_consumption, NULL, &error);
305 battery_monitor_release_instance();
307 _INFO("after IPC call - battery_consumption(%d)", battery_consumption);
309 BM_CATCH_ERROR((is_success != false), {}, battery_monitor_get_error_code(is_success, error), "Failed to get dbus.");
310 g_clear_error(&error);
312 *battery_usage = battery_consumption;
314 _INFO("battery_get_usage_by_app_id_for_resource_id() - SUCCESS");
317 return BATTERY_MONITOR_ERROR_NONE;
319 g_clear_error(&error);
320 _ERR("battery_monitor_call_get_usage_by_app_id_for_resource_id_sync()=[%d]", error_code);
324 BM_API int battery_monitor_get_total_usage_by_app_id(char* app_id, battery_monitor_duration_type_e duration, int* battery_usage)
328 CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
330 BM_CHECK_INPUT_PARAM(app_id);
331 BM_CHECK_INPUT_PARAM(battery_usage);
333 if (duration >= BATTERY_MONITOR_DURATION_TYPE_MAX) {
334 _ERR("Unknown duration specified");
335 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
338 int battery_consumption = 0;
339 int error_code = BATTERY_MONITOR_ERROR_NONE;
340 GError *error = NULL;
342 bm_server = battery_monitor_get_instance();
343 BM_CATCH_ERROR((bm_server != NULL), {}, BATTERY_MONITOR_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
345 _INFO("before IPC call - app_id(%s), duration(%d)", app_id, duration);
347 bool is_success = battery_monitor_call_get_total_usage_by_app_id_sync(bm_server, app_id, duration,
348 (int)getuid(), &battery_consumption, NULL, &error);
350 battery_monitor_release_instance();
352 _INFO("after IPC call - battery_consumption(%d)", battery_consumption);
354 *battery_usage = battery_consumption;
356 BM_CATCH_ERROR((is_success != false), {}, battery_monitor_get_error_code(is_success, error), "Failed to get dbus.");
357 g_clear_error(&error);
359 _INFO("battery_monitor_call_get_total_usage_by_app_id_sync() - SUCCESS");
362 return BATTERY_MONITOR_ERROR_NONE;
364 g_clear_error(&error);
365 _ERR("battery_monitor_call_get_total_usage_by_app_id_sync()=[%d]", error_code);
369 BM_API int battery_monitor_get_total_usage_by_resource_id(battery_monitor_resource_id_e resource_id,
370 battery_monitor_duration_type_e duration, int* battery_usage)
374 CHECK_BATTERY_FEATURE_SUPPORTED(BATTERY_FEATURE);
376 BM_CHECK_INPUT_PARAM(battery_usage);
378 if (resource_id >= BATTERY_MONITOR_RESOURCE_ID_MAX) {
379 _ERR("Unknown resource id specified");
380 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
383 if (duration >= BATTERY_MONITOR_DURATION_TYPE_MAX) {
384 _ERR("Unknown duration specified");
385 return BATTERY_MONITOR_ERROR_INVALID_PARAMETER;
388 int battery_consumption = 0;
389 int error_code = BATTERY_MONITOR_ERROR_NONE;
390 GError *error = NULL;
392 bm_server = battery_monitor_get_instance();
393 BM_CATCH_ERROR((bm_server != NULL), {}, BATTERY_MONITOR_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
395 _INFO("before IPC call - resource_id(%d), duration(%d)", resource_id, duration);
397 bool is_success = battery_monitor_call_get_total_usage_by_resource_id_sync(bm_server, resource_id, duration,
398 (int)getuid(), &battery_consumption, NULL, &error);
400 battery_monitor_release_instance();
402 _INFO("after IPC call - battery_consumption(%d)", battery_consumption);
404 *battery_usage = battery_consumption;
406 BM_CATCH_ERROR((is_success != false), {}, battery_monitor_get_error_code(is_success, error), "Failed to get dbus.");
407 g_clear_error(&error);
409 _INFO("battery_get_total_usage_by_resource_id() - SUCCESS");
412 return BATTERY_MONITOR_ERROR_NONE;
414 g_clear_error(&error);
415 _ERR("battery_monitor_call_get_total_usage_by_resource_id_sync()=[%d]", error_code);