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.
20 #include <device/battery.h>
21 #include <device/callback.h>
23 #include "bm_power_engine.h"
25 #include "bm_common.h"
26 #include "bm_config_parser.h"
27 #include "bm_plugin_manager.h"
28 #include "bm_server_db.h"
29 #include "bm_private.h"
30 #include "bd_private.h"
32 #include "bd_history_item.h"
34 bm_feature_data_h bm_data_handle = NULL;
35 GHashTable *gl_hash = NULL;
36 static struct bm_req_feature_data_handle_flag_s *bm_req_flag_h = NULL;
37 const double cmah = 3600;
38 int battery_capacity = 500;
41 int bm_engine_get_usage_by_app_id_for_resource_id(const gchar *app_id, gint resource_id, gint duration, int *b_usage)
45 BM_CHECK_INPUT_PARAM(app_id);
47 BM_RETURN_VAL((resource_id < BM_PLUGIN_ID_MAX), {},
48 BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid resource-id param");
50 BM_RETURN_VAL((duration < BM_DURATION_TYPE_MAX), {},
51 BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration param");
53 _INFO("usage requested - app-id[%s], resource_id[%d], duration[%d]",
54 app_id, resource_id, duration);
56 int error = BATTERY_MONITOR_ERROR_NONE;
58 appid_usage_s *app_usage = bm_server_query_app_usage_by_appid(app_id, duration, &error);
61 _ERR("battery usage data for app_id(%s) is not available", app_id);
62 return BATTERY_MONITOR_ERROR_INTERNAL;
65 if (error != BATTERY_MONITOR_ERROR_NONE) {
66 _ERR("error reported, error = %d", error);
67 return BATTERY_MONITOR_ERROR_INTERNAL;
70 if (resource_id == BM_PLUGIN_ID_BLE)
71 *b_usage = bm_calc_individual_consumption(app_usage->rId_ble, battery_capacity);
72 else if (resource_id == BM_PLUGIN_ID_WIFI)
73 *b_usage = bm_calc_individual_consumption(app_usage->rId_wifi, battery_capacity);
74 else if (resource_id == BM_PLUGIN_ID_CPU)
75 *b_usage = bm_calc_individual_consumption(app_usage->rId_cpu, battery_capacity);
76 else if (resource_id == BM_PLUGIN_ID_DISPLAY)
77 *b_usage = bm_calc_individual_consumption(app_usage->rId_display, battery_capacity);
78 else if (resource_id == BM_PLUGIN_ID_DEVICE_NETWORK)
79 *b_usage = bm_calc_individual_consumption(app_usage->rId_device_network, battery_capacity);
80 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
81 else if (resource_id == BM_PLUGIN_ID_GPS_SENSOR)
82 *b_usage = bm_calc_individual_consumption(app_usage->rId_gps, battery_capacity);
83 else if (resource_id == BM_PLUGIN_ID_HRM_SENSOR)
84 *b_usage = bm_calc_individual_consumption(app_usage->rId_hrm, battery_capacity);
85 else if (resource_id == BM_PLUGIN_CONST_BATTERY)
86 *b_usage = bm_calc_individual_consumption(app_usage->rId_battery, battery_capacity);
87 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
89 bm_appid_usage_free(app_usage);
92 return BATTERY_MONITOR_ERROR_NONE;
95 int bm_engine_get_mah_usage_by_app_id_for_resource_id_ci(const gchar* app_id, gint resource_id, gint64 start_time, gint64 end_time, double *battery_usage)
98 BM_CHECK_INPUT_PARAM(app_id);
99 BM_RETURN_VAL((resource_id < BM_PLUGIN_ID_MAX), {},
100 BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid resource-id param");
101 _DBG("usage requested - app-id[%s], resource_id[%d], start_time [%lld] end_time[%lld]",
102 app_id, resource_id, start_time, end_time);
104 int error = BATTERY_MONITOR_ERROR_NONE;
105 appid_usage_s *app_usage = bm_server_query_app_usage_by_appid_ci(app_id, start_time, end_time, &error);
107 _ERR("battery usage data for app_id(%s) is not available", app_id);
108 return BATTERY_MONITOR_ERROR_INTERNAL;
110 if (error != BATTERY_MONITOR_ERROR_NONE) {
111 _ERR("error reported, error = %d", error);
112 return BATTERY_MONITOR_ERROR_INTERNAL;
115 if (resource_id == BM_PLUGIN_ID_BLE)
116 *battery_usage = bm_calc_individual_mah_consumption(app_usage->rId_ble, cmah);
117 else if (resource_id == BM_PLUGIN_ID_WIFI)
118 *battery_usage = bm_calc_individual_mah_consumption(app_usage->rId_wifi, cmah);
119 else if (resource_id == BM_PLUGIN_ID_CPU)
120 *battery_usage = bm_calc_individual_mah_consumption(app_usage->rId_cpu, cmah);
121 else if (resource_id == BM_PLUGIN_ID_DISPLAY)
122 *battery_usage = bm_calc_individual_mah_consumption(app_usage->rId_display, cmah);
123 else if (resource_id == BM_PLUGIN_ID_DEVICE_NETWORK)
124 *battery_usage = bm_calc_individual_mah_consumption(app_usage->rId_device_network, cmah);
125 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
126 else if (resource_id == BM_PLUGIN_ID_GPS_SENSOR)
127 *battery_usage = bm_calc_individual_mah_consumption(app_usage->rId_gps, cmah);
128 else if (resource_id == BM_PLUGIN_ID_HRM_SENSOR)
129 *battery_usage = bm_calc_individual_mah_consumption(app_usage->rId_hrm, cmah);
132 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
134 bm_appid_usage_free(app_usage);
137 return BATTERY_MONITOR_ERROR_NONE;
140 int bm_engine_get_total_usage_by_app_id(const gchar *app_id, gint duration, int *b_usage)
144 BM_CHECK_INPUT_PARAM(app_id);
146 BM_RETURN_VAL((duration < BM_DURATION_TYPE_MAX), {},
147 BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration param");
149 _INFO("usage requested - app-id[%s], duration[%d]", app_id, duration);
151 int error = BATTERY_MONITOR_ERROR_NONE;
153 appid_usage_s *app_usage = bm_server_query_app_usage_by_appid(app_id, duration, &error);
156 _ERR("battery usage data for app_id(%s) is not available", app_id);
157 return BATTERY_MONITOR_ERROR_INTERNAL;
160 if (error != BATTERY_MONITOR_ERROR_NONE) {
161 _ERR("error reported, error = %d", error);
162 return BATTERY_MONITOR_ERROR_INTERNAL;
165 int total_consumption = (app_usage->rId_ble + app_usage->rId_wifi + app_usage->rId_cpu + \
166 app_usage->rId_display + app_usage->rId_device_network);
168 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
169 total_consumption += (app_usage->rId_gps + app_usage->rId_hrm);
170 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
172 *b_usage = bm_calc_individual_consumption(total_consumption, battery_capacity);
173 _INFO("total battery consumption is [%d] for app-id[%s]", *b_usage, app_id);
175 bm_appid_usage_free(app_usage);
178 return BATTERY_MONITOR_ERROR_NONE;
181 int bm_engine_get_total_mah_usage_by_app_id_ci(const gchar* app_id, gint64 start_time, gint64 end_time, double *battery_usage)
185 BM_CHECK_INPUT_PARAM(app_id);
187 _DBG("usage requested - app-id[%s], start_time [%lld] end_time[%lld]",
188 app_id, start_time, end_time);
189 int error = BATTERY_MONITOR_ERROR_NONE;
190 appid_usage_s *app_usage = bm_server_query_app_usage_by_appid_ci(app_id, start_time, end_time, &error);
192 _ERR("battery usage data for app_id(%s) is not available", app_id);
193 return BATTERY_MONITOR_ERROR_INTERNAL;
195 if (error != BATTERY_MONITOR_ERROR_NONE) {
196 _ERR("error reported, error = %d", error);
197 return BATTERY_MONITOR_ERROR_INTERNAL;
199 int total_consumption = (app_usage->rId_ble + app_usage->rId_wifi + app_usage->rId_cpu + \
200 app_usage->rId_display + app_usage->rId_device_network);
201 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
202 total_consumption += (app_usage->rId_gps + app_usage->rId_hrm);
203 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
204 *battery_usage = bm_calc_individual_mah_consumption(total_consumption, cmah);
205 _INFO("total battery consumption is [%lf] for app-id[%s]", *battery_usage, app_id);
207 bm_appid_usage_free(app_usage);
209 return BATTERY_MONITOR_ERROR_NONE;
212 int bm_engine_get_total_usage_by_resource_id(gint resource_id, gint duration, int *b_usage)
216 BM_RETURN_VAL((resource_id < BM_PLUGIN_ID_MAX), {},
217 BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid resource-id");
219 BM_RETURN_VAL((duration < BM_DURATION_TYPE_MAX), {},
220 BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration param");
222 _INFO("usage requested - resource_id[%d], duration[%d]", resource_id, duration);
224 int error = BATTERY_MONITOR_ERROR_NONE;
226 const char *resource_id_str = NULL;
227 resource_id_str = bm_get_resource_id_string(resource_id);
228 if (g_strcmp0(resource_id_str, "UNKNOWN RESOURCE-ID") == 0) {
229 _ERR("invalid resource-id");
230 return BATTERY_MONITOR_ERROR_INTERNAL;
233 _INFO("resource string - [%s]", resource_id_str);
235 resourceid_usage_s *resource_usage = bm_server_query_resource_usage_resourceid(resource_id_str, duration, &error);
237 if (!resource_usage) {
238 _ERR("battery usage data for resource-id(%s) is not available", resource_id_str);
239 return BATTERY_MONITOR_ERROR_INTERNAL;
242 if (error != BATTERY_MONITOR_ERROR_NONE) {
243 _ERR("error reported, error = %d", error);
244 return BATTERY_MONITOR_ERROR_INTERNAL;
247 *b_usage = bm_calc_individual_consumption(resource_usage->usage, battery_capacity);
249 bm_resourceid_usage_free(resource_usage);
252 return BATTERY_MONITOR_ERROR_NONE;
255 int bm_engine_get_total_mah_usage_by_resource_id_ci(gint resource_id, gint64 start_time, gint64 end_time, double *battery_usage)
259 BM_RETURN_VAL((resource_id < BM_PLUGIN_ID_MAX), {},
260 BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid resource-id");
261 _DBG("usage requested - resource_id[%d], start_time [%lld] end_time[%lld]",
262 resource_id, start_time, end_time);
264 int error = BATTERY_MONITOR_ERROR_NONE;
265 const char *resource_id_str = NULL;
266 resource_id_str = bm_get_resource_id_string(resource_id);
267 if (g_strcmp0(resource_id_str, "UNKNOWN RESOURCE-ID") == 0) {
268 _ERR("invalid resource-id");
269 return BATTERY_MONITOR_ERROR_INTERNAL;
271 _INFO("resource string - [%s]", resource_id_str);
272 resourceid_usage_s *resource_usage = bm_server_query_resource_usage_resourceid_ci(resource_id_str, start_time, end_time, &error);
273 if (!resource_usage) {
274 _ERR("battery usage data for resource-id(%s) is not available", resource_id_str);
275 return BATTERY_MONITOR_ERROR_INTERNAL;
277 if (error != BATTERY_MONITOR_ERROR_NONE) {
278 _ERR("error reported, error = %d", error);
279 return BATTERY_MONITOR_ERROR_INTERNAL;
281 *battery_usage = bm_calc_individual_mah_consumption(resource_usage->usage, cmah);
282 bm_resourceid_usage_free(resource_usage);
285 return BATTERY_MONITOR_ERROR_NONE;
288 int bm_engine_get_all_resource_usage(const gchar *app_id, gint duration, bm_total_consumption_h data)
292 BM_CHECK_INPUT_PARAM(app_id);
294 BM_CHECK_INPUT_PARAM(data);
296 BM_RETURN_VAL((duration < BM_DURATION_TYPE_MAX), {},
297 BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration param");
299 _INFO("usage requested - app-id[%s], duration[%d]", app_id, duration);
301 int error = BATTERY_MONITOR_ERROR_NONE;
303 appid_usage_s *app_usage = bm_server_query_app_usage_by_appid(app_id, duration, &error);
305 _ERR("battery usage data for app_id(%s) is not available", app_id);
306 return BATTERY_MONITOR_ERROR_INTERNAL;
308 if (error != BATTERY_MONITOR_ERROR_NONE) {
309 _ERR("error reported, error = %d", error);
310 return BATTERY_MONITOR_ERROR_INTERNAL;
313 data->ble_val = bm_calc_individual_consumption(app_usage->rId_ble, battery_capacity);
314 data->wifi_val = bm_calc_individual_consumption(app_usage->rId_wifi, battery_capacity);
315 data->cpu_val = bm_calc_individual_consumption(app_usage->rId_cpu, battery_capacity);
316 data->dp_val = bm_calc_individual_consumption(app_usage->rId_display, battery_capacity);
317 data->dn_val = bm_calc_individual_consumption(app_usage->rId_device_network, battery_capacity);
318 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
319 data->gps_val = bm_calc_individual_consumption(app_usage->rId_gps, battery_capacity);
320 data->hrm_val = bm_calc_individual_consumption(app_usage->rId_hrm, battery_capacity);
321 data->bat_val = app_usage->rId_battery;
322 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
324 bm_appid_usage_free(app_usage);
327 return BATTERY_MONITOR_ERROR_NONE;
330 int bm_engine_get_all_resource_usage_handle_ci(const gchar* app_id, gint64 start_time, gint64 end_time, bm_total_consumption_h battery_data)
333 BM_CHECK_INPUT_PARAM(app_id);
334 BM_CHECK_INPUT_PARAM(battery_data);
335 _DBG("usage requested - app-id[%s], start_time[%lld] end_time[%lld]", app_id, start_time, end_time);
336 int error = BATTERY_MONITOR_ERROR_NONE;
337 appid_usage_s *app_usage = bm_server_query_app_usage_by_appid_ci(app_id, start_time, end_time, &error);
339 _ERR("battery usage data for app_id(%s) is not available", app_id);
340 return BATTERY_MONITOR_ERROR_INTERNAL;
342 if (error != BATTERY_MONITOR_ERROR_NONE) {
343 _ERR("error reported, error = %d", error);
344 return BATTERY_MONITOR_ERROR_INTERNAL;
346 battery_data->ble_val = app_usage->rId_ble; battery_data->wifi_val = app_usage->rId_wifi;
347 battery_data->cpu_val = app_usage->rId_cpu; battery_data->dp_val = app_usage->rId_display;
348 battery_data->dn_val = app_usage->rId_device_network;
349 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
350 battery_data->gps_val = app_usage->rId_gps; battery_data->hrm_val = app_usage->rId_hrm;
351 battery_data->bat_val = app_usage->rId_battery;
352 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
354 bm_appid_usage_free(app_usage);
357 return BATTERY_MONITOR_ERROR_NONE;
361 void bm_engine_set_req_flag_handle(bm_plugin_id_e req_plugin_id, bool value)
365 if (!bm_req_flag_h) {
366 _ERR("invalid flag handle");
370 switch (req_plugin_id) {
371 case BM_PLUGIN_ID_BLE:
372 bm_req_flag_h->req_ble_data = value;
374 case BM_PLUGIN_ID_WIFI:
375 bm_req_flag_h->req_wifi_data = value;
377 case BM_PLUGIN_ID_CPU:
378 bm_req_flag_h->req_cpu_data = value;
380 case BM_PLUGIN_ID_DISPLAY:
381 bm_req_flag_h->req_dp_data = value;
383 case BM_PLUGIN_ID_DEVICE_NETWORK:
384 bm_req_flag_h->req_dn_data = value;
386 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
387 case BM_PLUGIN_ID_GPS_SENSOR:
388 bm_req_flag_h->req_gps_data = value;
390 case BM_PLUGIN_ID_HRM_SENSOR:
391 bm_req_flag_h->req_hrm_data = value;
393 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
395 _DBG("Invalid plugin ID");
403 void bm_set_req_flag_handles(bool value)
407 if (!bm_req_flag_h) {
408 _ERR("invalid flag handle");
412 bm_req_flag_h->req_ble_data = value;
413 bm_req_flag_h->req_wifi_data = value;
414 bm_req_flag_h->req_cpu_data = value;
415 bm_req_flag_h->req_dp_data = value;
416 bm_req_flag_h->req_dn_data = value;
417 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
418 bm_req_flag_h->req_gps_data = value;
419 bm_req_flag_h->req_hrm_data = value;
420 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
422 _INFO("request flags set-[%s]", (value == true) ? "TRUE" : "FALSE");
428 static int bm_appid_session_usage_map(char *app_id, int app_usage, bm_plugin_id_e resource_id)
432 BM_CHECK_INPUT_PARAM(app_id);
434 void *prv_data = NULL;
435 void *prv_app_id = NULL;
437 int ret_val = BATTERY_MONITOR_ERROR_NONE;
438 _DBG("app_id [%s] app_usage is [%d]", app_id, app_usage);
440 if (g_hash_table_lookup_extended(gl_hash, app_id, &prv_app_id, &prv_data) == true) {
441 appid_usage_s *temp = (appid_usage_s *)prv_data;
442 if (resource_id == BM_PLUGIN_ID_BLE)
443 temp->rId_ble += app_usage;
444 else if (resource_id == BM_PLUGIN_ID_WIFI)
445 temp->rId_wifi += app_usage;
446 else if (resource_id == BM_PLUGIN_ID_CPU)
447 temp->rId_cpu += app_usage;
448 else if (resource_id == BM_PLUGIN_ID_DISPLAY)
449 temp->rId_display += app_usage;
450 else if (resource_id == BM_PLUGIN_ID_DEVICE_NETWORK)
451 temp->rId_device_network += app_usage;
452 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
453 else if (resource_id == BM_PLUGIN_ID_GPS_SENSOR)
454 temp->rId_gps += app_usage;
455 else if (resource_id == BM_PLUGIN_ID_HRM_SENSOR)
456 temp->rId_hrm += app_usage;
457 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
459 _DBG("update - app_usage for app_id(%s), for resource(%d), by usage(%d)", app_id, resource_id, app_usage);
460 //_DBG("session usage bt %d, wifi %d, cpu %d, dsp %d, dn %d", temp->rId_ble, temp->rId_wifi, temp->rId_cpu,
461 //temp->rId_display, temp->rId_device_network);
463 appid_usage_s *temp = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
465 _ERR("memory allocation failed");
466 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
469 if (resource_id == BM_PLUGIN_ID_BLE)
470 temp->rId_ble = app_usage;
471 else if (resource_id == BM_PLUGIN_ID_WIFI)
472 temp->rId_wifi = app_usage;
473 else if (resource_id == BM_PLUGIN_ID_CPU)
474 temp->rId_cpu = app_usage;
475 else if (resource_id == BM_PLUGIN_ID_DISPLAY)
476 temp->rId_display = app_usage;
477 else if (resource_id == BM_PLUGIN_ID_DEVICE_NETWORK)
478 temp->rId_device_network = app_usage;
479 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
480 else if (resource_id == BM_PLUGIN_ID_GPS_SENSOR)
481 temp->rId_gps = app_usage;
482 else if (resource_id == BM_PLUGIN_ID_HRM_SENSOR)
483 temp->rId_hrm = app_usage;
484 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
486 _DBG("insert - app_usage app_id(%s), for resource(%d), by usage(%d)", app_id, resource_id, app_usage);
488 _INFO("inserted - %d", g_hash_table_insert(gl_hash, app_id, temp));
495 static int bm_insert_appid_session_usage_to_db()
501 appid_usage_s *app_usage = NULL;
503 int ret_val = BATTERY_MONITOR_ERROR_NONE;
505 long int ret_time = bm_get_log_time();
507 g_hash_table_iter_init(&iter, gl_hash);
509 while (g_hash_table_iter_next(&iter, &key, &value)) {
510 app_usage = (appid_usage_s *)value;
511 app_usage->AppId = (char *)key;
512 app_usage->log_time = ret_time;
514 _DBG("Calculated Power for Appid(%s) for resources bt(%d), wifi(%d), cpu(%d), \
515 display(%d), dn(%d)", app_usage->AppId, app_usage->rId_ble,
516 app_usage->rId_wifi, app_usage->rId_cpu, app_usage->rId_display,
517 app_usage->rId_device_network);
519 ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
520 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
521 _ERR("Insert of Appid failed ");
523 _DBG("Insert successful");
525 ret_val = bm_server_app_usage_insert_to_db(app_usage);
526 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
527 _ERR("Insert of App Usage failed ");
529 _DBG("Insert successful");
536 /* TODO: Add Normalization of values based on sP_Power_xxx & Time unit */
537 int bm_ble_calc_power_and_commit(bm_bluetooth_st *handle, bool mode)
541 BM_CHECK_INPUT_PARAM(handle);
543 GSList *bt_data_iterator = NULL;
544 GSList *bt_atm_iterator = NULL;
545 void *prv_data = NULL;
546 void *prv_app_id = NULL;
547 int ret_val = BATTERY_MONITOR_ERROR_NONE;
549 /* creating hash-map with (key, value) = (app-id, data) */
550 GHashTable *hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st2_free);
552 long int edTime = 0, stTime = 0;
553 long int sesTime = 0, scTime = 0, conTime = 0, RX_app = 0, TX_app = 0;
554 long int RX_system = 0, TX_system = 0;
556 /* iterating over list for data accumulation */
557 for (bt_data_iterator = handle->bt_data_list; bt_data_iterator; bt_data_iterator = bt_data_iterator->next) {
558 bm_bluetooth_st *datalistnode = (bm_bluetooth_st *)(bt_data_iterator->data);
559 edTime = datalistnode->stopTime;
560 stTime = datalistnode->startTime;
561 scTime += datalistnode->scanTime;
562 conTime += datalistnode->connectedTime;
563 sesTime += (edTime-stTime);
564 bt_atm_iterator = datalistnode->atm_list;
565 for ( ; bt_atm_iterator; bt_atm_iterator = bt_atm_iterator->next) {
566 app_time_map_st2 *bt_atm_node = NULL;
567 bt_atm_node = bt_atm_iterator->data;
569 _DBG("no data available");
572 _DBG("bt data available");
574 /* Get the system usage */
575 if (g_strcmp0(bt_atm_node->app_id, BM_APPID_SYSTEM) == 0) {
576 _DBG("ignore - app_id(%s), time(%d), rx(%d), tx(%d)", bt_atm_node->app_id,
577 bt_atm_node->time, bt_atm_node->rx, bt_atm_node->tx);
579 RX_system += bt_atm_node->rx;
580 TX_system += bt_atm_node->tx;
582 bm_data_free(bt_atm_node->app_id);
583 bm_atm_st2_free(bt_atm_node);
587 RX_app += bt_atm_node->rx;
588 TX_app += bt_atm_node->tx;
589 onTime += bt_atm_node->time;
592 if (g_hash_table_lookup_extended(hash, bt_atm_node->app_id, &prv_app_id, &prv_data) == true) {
593 _DBG("previous app_id = %s", (char *)prv_app_id);
594 app_time_map_st2 *temp = (app_time_map_st2 *)prv_data;
595 bt_atm_node->time += temp->time;
596 bt_atm_node->rx += temp->rx;
597 bt_atm_node->tx += temp->tx;
598 _DBG("update - app_id(%s), time(%d), rx(%d), tx(%d)", bt_atm_node->app_id,
599 bt_atm_node->time, bt_atm_node->rx, bt_atm_node->tx);
600 _INFO("updated - %d", g_hash_table_replace(hash, bt_atm_node->app_id, bt_atm_node));
602 _DBG("insert - app_id(%s), time(%d), rx(%d), tx(%d)", bt_atm_node->app_id,
603 bt_atm_node->time, bt_atm_node->rx, bt_atm_node->tx);
604 _INFO("inserted - %d", g_hash_table_insert(hash, bt_atm_node->app_id, bt_atm_node));
609 /* Read standard Rated Values from Device Spec File/Power Profile File */
610 int sP_power_bt_idle = 0, sP_power_bt_scan = 0, sP_power_bt_conn = 0, sP_power_bt_tx = 0, sP_power_bt_rx = 0;
612 ret_val = bm_get_blutooth_power_params(&sP_power_bt_idle, &sP_power_bt_scan, &sP_power_bt_conn, &sP_power_bt_rx, &sP_power_bt_tx);
613 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
614 _ERR("failed to get bt power params");
616 _DBG("received bt power params - idle[%d], scan[%d], connected[%d], rx[%d], tx[%d]",
617 sP_power_bt_idle, sP_power_bt_scan, sP_power_bt_conn, sP_power_bt_tx, sP_power_bt_rx);
618 /* BT power consumption Level - 1 at the Resource Level */
619 long int P_power_bt = 0, P_power_data = 0, P_power_conn = 0;
620 P_power_conn = ((sP_power_bt_idle) * (sesTime - (scTime + conTime))) + ((sP_power_bt_scan) * (scTime)) + ((sP_power_bt_conn) * (conTime));
621 P_power_data = ((sP_power_bt_tx) * TX_app) + ((sP_power_bt_rx) * RX_app);
622 P_power_bt = P_power_conn + P_power_data;
624 _DBG("Calculated Power for Bluetooth P_power_bt(%ld), P_power_conn (%ld), P_power_data(%ld)", P_power_bt, P_power_conn, P_power_data);
625 /* BT power consumption Level - 2 at the Application Level */
628 g_hash_table_iter_init(&iter, hash);
630 long int P_power_app_bt = 0, P_power_app_data = 0, P_power_app_conn = 0;
632 long int apprx = 0, apptx = 0;
634 long int rx_t = 0, tx_t = 0;
635 long int ret_time = bm_get_log_time();
637 appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
638 if (app_usage == NULL) {
639 _ERR("memory allocation failed");
640 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
643 while (g_hash_table_iter_next(&iter, &key, &value)) {
644 P_power_app_bt = 0, P_power_app_data = 0, P_power_app_conn = 0;
645 apprx = 0, apptx = 0, apptime = 0;
646 app_time_map_st2 *temp = (app_time_map_st2 *)value;
647 appid = temp->app_id;
648 apprx = temp->rx; rx_t += apprx;
649 apptx = temp->tx; tx_t += apptx;
650 apptime = temp->time;
651 P_power_app_data = ((sP_power_bt_tx) * apptx) + ((sP_power_bt_rx) * apprx);
653 P_power_app_conn = (P_power_conn * apptime)/onTime; //check for 0 denominator & same units
654 P_power_app_bt = P_power_app_data + P_power_app_conn;
656 app_usage->AppId = appid;
657 app_usage->rId_ble = P_power_app_bt;
658 app_usage->log_time = ret_time;
659 /* Call Insert Function */
660 _DBG("Calculated Power for Appid(%s) - P_power_app_bt(%ld), P_power_app_conn(%ld), P_power_app_data(%ld)",
661 appid, P_power_app_bt, P_power_app_conn, P_power_app_data);
663 ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
664 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
665 _ERR("Insert of BT appid failed ");
666 ret_val = bm_server_app_usage_insert_to_db(app_usage);
667 } else/* Call Global Hash */
668 ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_ble, BM_PLUGIN_ID_BLE);
669 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
670 _ERR("Insert of BT App failed ");
672 _DBG("Insert successful");
675 /* commit to database the output after power calculation */
676 resourceid_usage_s *res_usage = (resourceid_usage_s *)calloc(1, sizeof(resourceid_usage_s));
677 if (res_usage == NULL) {
678 _ERR("memory allocation failed");
679 bm_appid_usage_free(app_usage);
680 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
683 res_usage->ResourceId = BM_RID_BLE;
684 res_usage->log_time = ret_time;
685 res_usage->usage = P_power_bt;
687 ret_val = bm_server_resource_usage_insert_to_db(res_usage);
688 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
689 _ERR("Insert of BT resource failed ");
691 bm_appid_usage_free(app_usage);
692 bm_resourceid_usage_free(res_usage);
694 bd_gl_ble_stat_s *gble_st = (bd_gl_ble_stat_s *)calloc(1, sizeof(bd_gl_ble_stat_s));
695 if (gble_st == NULL) {
696 _ERR("memory allocation failed");
697 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
699 gble_st->time_s = ret_time; gble_st->ble_idle_time = (sesTime - (scTime + conTime));
700 if (tx_t != 0 || rx_t != 0) {
701 gble_st->ble_rx_time = (conTime * rx_t) / (rx_t + tx_t);
702 gble_st->ble_tx_time = (conTime * tx_t) / (rx_t + tx_t);
704 gble_st->ble_pwi_val = P_power_bt;
706 ret_val = bm_server_battery_dump_insert_to_db(gble_st, 0);
707 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
708 _ERR("Insert of GBLE failed ");
710 bd_gl_bst_stat_s *gbst_st = (bd_gl_bst_stat_s *)calloc(1, sizeof(bd_gl_bst_stat_s));
711 if (gbst_st == NULL) {
712 _ERR("memory allocation failed");
713 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
715 gbst_st->time_s = ret_time; gbst_st->off_time = BATTERY_MONITOR_GET_DATA_JOB_PERIOD/1000;
716 gbst_st->off_time -= sesTime; gbst_st->low_time = 0/*(sesTime - (scTime + conTime))*/;
717 gbst_st->med_time = 0/*scTime*/; gbst_st->high_time = 0/*conTime*/;
718 ret_val = bm_server_battery_dump_insert_to_db(gbst_st, 6);
719 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
720 _ERR("Insert of GBLE failed ");
722 /* Free the hash map */
723 g_hash_table_destroy(hash);
725 /* Dump resource usage */
726 _DBG("BLE usage : %ld", RX_system + TX_system + RX_app + TX_app);
727 if (RX_system > 0 || TX_system > 0 || RX_app > 0 || TX_app > 0) {
729 bd_print_history_item_reset(&hi);
732 hi.usage_type = USAGE_BLUETOOTH;
733 hi.usage = RX_system + TX_system + RX_app + TX_app;
735 bd_store_history_item(&hi);
742 int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode)
746 BM_CHECK_INPUT_PARAM(handle);
748 GSList *wifi_data_iterator = NULL;
749 GSList *wifi_atm_iterator = NULL;
750 void *prv_data = NULL;
751 void *prv_app_id = NULL;
752 int ret_val = BATTERY_MONITOR_ERROR_NONE;
754 /* creating hash-map with (key, value) = (app-id, data) */
755 GHashTable *hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st2_free);
757 long int edTime = 0, stTime = 0;
758 long int sesTime = 0, scTime = 0, tl_total = 0;
759 long int tl0 = 0, tl1 = 0, tl2 = 0, tl3 = 0, tl4 = 0;
761 long int RX = 0, TX = 0;
762 /* iterating over list for data accumulation */
763 for (wifi_data_iterator = handle->wifi_data_list; wifi_data_iterator; wifi_data_iterator = wifi_data_iterator->next) {
764 bm_wifi_st *datalistnode = (bm_wifi_st *)(wifi_data_iterator->data);
765 edTime = datalistnode->endTime;
766 stTime = datalistnode->startTime;
767 sesTime += edTime - stTime;
768 scTime += datalistnode->scanTime;
769 tl0 += datalistnode->time_level_0;
770 tl1 += datalistnode->time_level_1;
771 tl2 += datalistnode->time_level_2;
772 tl3 += datalistnode->time_level_3;
773 tl4 += datalistnode->time_level_4;
774 wifi_atm_iterator = datalistnode->atm_list;
775 for ( ; wifi_atm_iterator; wifi_atm_iterator = wifi_atm_iterator->next) {
776 app_time_map_st2 *wifi_atm_node = NULL;
777 wifi_atm_node = wifi_atm_iterator->data;
778 if (!wifi_atm_node) {
779 _DBG("no data available");
782 _DBG("wifi data available");
783 RX += wifi_atm_node->rx;
784 TX += wifi_atm_node->tx;
785 onTime += wifi_atm_node->time;
788 if (g_hash_table_lookup_extended(hash, wifi_atm_node->app_id, &prv_app_id, &prv_data) == true) {
789 _DBG("previous app_id = %s", (char *)prv_app_id);
790 app_time_map_st2 *temp = (app_time_map_st2 *)prv_data;
791 wifi_atm_node->time += temp->time;
792 wifi_atm_node->rx += temp->rx;
793 wifi_atm_node->tx += temp->tx;
794 _DBG("update - app_id(%s), time(%d), rx(%d), tx(%d)", wifi_atm_node->app_id,
795 wifi_atm_node->time, wifi_atm_node->rx, wifi_atm_node->tx);
796 _INFO("updated - %d", g_hash_table_replace(hash, wifi_atm_node->app_id, wifi_atm_node));
798 _DBG("insert - app_id(%s), time(%d), rx(%d), tx(%d)", wifi_atm_node->app_id,
799 wifi_atm_node->time, wifi_atm_node->rx, wifi_atm_node->tx);
800 _INFO("inserted - %d", g_hash_table_insert(hash, wifi_atm_node->app_id, wifi_atm_node));
805 /* Read standard Rated Values from Device Spec File/Power Profile File */
806 int sP_power_wf_idle = 0, sP_power_wf_scan = 0, sP_power_wf_tl0 = 0, sP_power_wf_tl1 = 0, sP_power_wf_tl2 = 0;
807 int sP_power_wf_tl3 = 0, sP_power_wf_tl4 = 0, sP_power_wf_tx = 0, sP_power_wf_rx = 0;
809 ret_val = bm_get_wifi_power_params(&sP_power_wf_tl0, &sP_power_wf_tl1, &sP_power_wf_tl2, &sP_power_wf_tl3,
810 &sP_power_wf_tl4, &sP_power_wf_scan, &sP_power_wf_idle, &sP_power_wf_rx, &sP_power_wf_tx);
811 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
812 _ERR("failed to get wifi power params");
814 _DBG("received wifi power params - rssi_0[%d], rssi_1[%d], rssi_2[%d], rssi_3[%d], rssi_4[%d], scan[%d], \
815 idle[%d], rx[%d], tx[%d]", sP_power_wf_tl0, sP_power_wf_tl1, sP_power_wf_tl2, sP_power_wf_tl3,
816 sP_power_wf_tl4, sP_power_wf_scan, sP_power_wf_idle, sP_power_wf_rx, sP_power_wf_tx);
818 tl0 /= 1000; tl1 /= 1000; tl2 /= 1000; tl3 /= 1000; tl4 /= 1000;
819 scTime /= 1000; sesTime /= 1000; onTime /= 1000;
820 tl_total = tl0 + tl1 + tl2 + tl3 + tl4;
821 /* Wifi power consumption Level - 1 at the Resource Level */
822 long int P_power_wifi = 0, P_power_data = 0, P_power_conn = 0;
823 P_power_conn = ((sP_power_wf_tl0 * tl0) + (sP_power_wf_tl1 * tl1) +
824 (sP_power_wf_tl2 * tl2) + (sP_power_wf_tl3 * tl3) +
825 (sP_power_wf_tl4 * tl4) + (sP_power_wf_scan * scTime) +
826 (sP_power_wf_idle * (sesTime - (tl_total + scTime))));
827 P_power_data = ((sP_power_wf_tx) * TX) + ((sP_power_wf_rx) * RX);
828 P_power_wifi = P_power_conn + P_power_data;
830 _DBG("Calculated Power for Wifi P_power_wifi(%ld), P_power_conn (%ld), P_power_data(%ld)", P_power_wifi, P_power_conn, P_power_data);
831 /* Wifi power consumption Level - 2 at the Application Level */
834 g_hash_table_iter_init(&iter, hash);
836 long int P_power_app_wifi = 0, P_power_app_data = 0, P_power_app_conn = 0;
838 long int apprx = 0, apptx = 0;
840 long int rx_t = 0, tx_t = 0;
841 long int ret_time = bm_get_log_time();
843 appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
844 if (app_usage == NULL) {
845 _ERR("memory allocation failed");
846 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
849 while (g_hash_table_iter_next(&iter, &key, &value)) {
850 P_power_app_wifi = 0; P_power_app_data = 0; P_power_app_conn = 0;
851 apprx = 0, apptx = 0, apptime = 0;
852 app_time_map_st2 *temp = (app_time_map_st2 *)value;
853 appid = temp->app_id;
854 apprx = temp->rx; rx_t += apprx;
855 apptx = temp->tx; tx_t += apptx;
856 apptime = temp->time;
858 P_power_app_data = ((sP_power_wf_tx) * apptx) + ((sP_power_wf_rx) * apprx);
861 P_power_app_conn = (P_power_conn * apptime)/onTime; //check for 0 denominator & same units
862 P_power_app_wifi = P_power_app_data + P_power_app_conn;
864 app_usage->AppId = appid;
865 app_usage->rId_wifi = P_power_app_wifi;
866 app_usage->log_time = ret_time;
867 /* Call Insert Function */
868 _DBG("Calculated Power for Appid(%s) - P_power_app_wifi(%ld), P_power_app_conn(%ld), P_power_app_data(%ld)",
869 appid, P_power_app_wifi, P_power_app_conn, P_power_app_data);
872 ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
873 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
874 _ERR("Insert of WiFi appid failed ");
875 ret_val = bm_server_app_usage_insert_to_db(app_usage);
876 } else/* Call Global Hash */
877 ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_wifi, BM_PLUGIN_ID_WIFI);
878 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
879 _ERR("Insert of Wifi App failed ");
881 _DBG("Insert successful");
884 /* commit to database the output after power calculation */
885 resourceid_usage_s *res_usage = (resourceid_usage_s *)calloc(1, sizeof(resourceid_usage_s));
886 if (res_usage == NULL) {
887 _ERR("memory allocation failed");
888 bm_appid_usage_free(app_usage);
889 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
892 res_usage->ResourceId = BM_RID_WIFI;
893 res_usage->log_time = ret_time;
894 res_usage->usage = P_power_wifi;
896 ret_val = bm_server_resource_usage_insert_to_db(res_usage);
897 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
898 _ERR("Insert of Wifi resource failed ");
900 _DBG("Insert successful");
902 bm_appid_usage_free(app_usage);
903 bm_resourceid_usage_free(res_usage);
905 bd_gl_wifi_stat_s *gwfl_st = (bd_gl_wifi_stat_s *)calloc(1, sizeof(bd_gl_wifi_stat_s));
906 if (gwfl_st == NULL) {
907 _ERR("memory allocation failed");
908 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
910 gwfl_st->time_s = ret_time; gwfl_st->wifi_on_time = sesTime;
911 gwfl_st->wifi_conn_time = tl_total; gwfl_st->wifi_idle_time = (sesTime - (tl_total + scTime));
912 if (tx_t != 0 || rx_t != 0) {
913 gwfl_st->wifi_rx_time = (tl_total * rx_t) / (rx_t + tx_t);
914 gwfl_st->wifi_tx_time = (tl_total * tx_t) / (rx_t + tx_t);
916 ret_val = bm_server_battery_dump_insert_to_db(gwfl_st, 1);
917 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
918 _ERR("Insert of GWFL failed ");
920 _DBG("Insert successful");
922 bd_gl_wst_stat_s *gwst_st = (bd_gl_wst_stat_s *)calloc(1, sizeof(bd_gl_wst_stat_s));
923 if (gwst_st == NULL) {
924 _ERR("memory allocation failed");
925 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
927 gwst_st->time_s = ret_time; gwst_st->wscan_time = scTime;
928 gwst_st->wnone_time = tl0; gwst_st->wpoor_time = tl1; gwst_st->wmed_time = tl2;
929 gwst_st->wgood_time = tl3; gwst_st->wgrt_time = tl4;
930 ret_val = bm_server_battery_dump_insert_to_db(gwst_st, 5);
931 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
932 _ERR("Insert of GWFL failed ");
934 _DBG("Insert successful");
937 /* Free the Hash Map */
938 g_hash_table_destroy(hash);
940 /* Dump resource usage */
941 _DBG("Wi-Fi usage : %ld", RX + TX);
942 if (RX > 0 || TX > 0) {
944 bd_print_history_item_reset(&hi);
947 gettimeofday(&tv, NULL);
949 hi.time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
952 hi.usage_type = USAGE_WIFI;
955 bd_store_history_item(&hi);
962 int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
966 BM_CHECK_INPUT_PARAM(handle);
968 GSList *cpu_data_iterator = NULL;
969 GSList *cpu_atm_iterator = NULL;
970 void *prv_data = NULL;
971 void *prv_app_id = NULL;
972 int ret_val = BATTERY_MONITOR_ERROR_NONE;
974 /* creating hash-map with (key, value) = (app-id, data) */
975 GHashTable *hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st1_free);
977 long int uTime = 0, sTime = 0;
979 /* iterating over list for data accumulation */
980 for (cpu_data_iterator = handle->cpu_data_list; cpu_data_iterator; cpu_data_iterator = cpu_data_iterator->next) {
981 bm_cpu_st *datalistnode = (bm_cpu_st *)(cpu_data_iterator->data);
982 uTime += datalistnode->utime;
983 sTime += datalistnode->stime;
984 cpu_atm_iterator = datalistnode->atm_list;
985 for ( ; cpu_atm_iterator; cpu_atm_iterator = cpu_atm_iterator->next) {
986 app_time_map_st1 *cpu_atm_node = (app_time_map_st1 *)cpu_atm_iterator->data;
988 _DBG("no data available");
991 _DBG("cpu data available");
992 onTime += cpu_atm_node->time;
995 if (g_hash_table_lookup_extended(hash, cpu_atm_node->app_id, &prv_app_id, &prv_data) == true) {
996 _DBG("previous app_id = %s", (char *)prv_app_id);
997 app_time_map_st1 *cpu_atm_prv_node = (app_time_map_st1 *)prv_data;
998 cpu_atm_node->time += cpu_atm_prv_node->time;
999 _DBG("update - app_id(%s), time(%d)", cpu_atm_node->app_id, cpu_atm_node->time);
1000 _INFO("updated - %d", g_hash_table_replace(hash, cpu_atm_node->app_id, cpu_atm_node));
1002 _DBG("insert - app_id(%s), time(%d)", cpu_atm_node->app_id, cpu_atm_node->time);
1003 _INFO("inserted - %d", g_hash_table_insert(hash, cpu_atm_node->app_id, cpu_atm_node));
1007 /* Read standard Rated Values from Device Spec File/Power Profile File */
1008 int sP_power_cpu_user = 0, sP_power_cpu_system = 0;
1010 ret_val = bm_get_cpu_power_params(&sP_power_cpu_user, &sP_power_cpu_system);
1011 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1012 _ERR("failed to get cpu power params");
1014 _DBG("received cpu power params - user[%d]\n, system[%d]\n", sP_power_cpu_user, sP_power_cpu_system);
1015 /* CPU power consumption Level - 1 at the Resource Level */
1016 long int P_power_cpu = 0, P_power_user = 0, P_power_system = 0;
1017 _DBG("%ld %ld %lf", uTime, sTime, onTime);
1018 uTime /= 1000; sTime /= 1000; onTime /= 1000;
1019 P_power_user = ((sP_power_cpu_user * uTime));
1020 P_power_system = ((sP_power_cpu_system * sTime));
1021 P_power_cpu = P_power_user + P_power_system;
1023 _DBG("Calculated Power for CPU P_power_cpu (%ld), P_power_user (%ld), P_power_system (%ld)", P_power_cpu, P_power_user, P_power_system);
1024 /* CPU power consumption Level - 2 at the Application Level */
1025 GHashTableIter iter;
1026 gpointer key, value;
1027 g_hash_table_iter_init(&iter, hash);
1029 long int P_power_app_cpu = 0, P_power_app_use = 0;
1032 long int ret_time = bm_get_log_time();
1034 appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
1035 if (app_usage == NULL) {
1036 _ERR("memory allocation failed");
1037 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1040 while (g_hash_table_iter_next(&iter, &key, &value)) {
1041 P_power_app_cpu = 0; P_power_app_use = 0;
1043 app_time_map_st1 *temp = (app_time_map_st1 *)value;
1044 appid = temp->app_id;
1045 apptime = (temp->time);
1048 P_power_app_use = (P_power_cpu * apptime)/onTime; //check for 0 denominator & same units
1049 P_power_app_cpu = P_power_app_use;
1051 app_usage->AppId = appid;
1052 app_usage->rId_cpu = P_power_app_cpu;
1053 app_usage->log_time = ret_time;
1054 /* Call Insert Function */
1055 _DBG("Calculated Power for Appid (%s) P_power_app_cpu (%ld), P_power_app_use (%ld)", appid, P_power_app_cpu, P_power_app_use);
1058 ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
1059 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1060 _ERR("Insert of CPU appid failed ");
1061 ret_val = bm_server_app_usage_insert_to_db(app_usage);
1063 ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_cpu, BM_PLUGIN_ID_CPU);
1065 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1066 _ERR("Insert of CPU App failed ");
1068 _DBG("Insert successful");
1071 resourceid_usage_s *res_usage = (resourceid_usage_s *)calloc(1, sizeof(resourceid_usage_s));
1072 if (res_usage == NULL) {
1073 _ERR("memory allocation failed");
1074 bm_appid_usage_free(app_usage);
1075 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1078 res_usage->ResourceId = BM_RID_CPU;
1079 res_usage->log_time = ret_time;
1080 res_usage->usage = P_power_cpu;
1082 ret_val = bm_server_resource_usage_insert_to_db(res_usage);
1083 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1084 _ERR("Insert of CPU resource failed ");
1086 bm_appid_usage_free(app_usage);
1087 bm_resourceid_usage_free(res_usage);
1089 bd_gl_cpu_stat_s *gcpu_st = (bd_gl_cpu_stat_s *)calloc(1, sizeof(bd_gl_cpu_stat_s));
1090 if (gcpu_st == NULL) {
1091 _ERR("memory allocation failed");
1092 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1094 gcpu_st->time_s = ret_time; gcpu_st->usr_time = uTime;
1095 gcpu_st->sys_time = sTime;
1096 ret_val = bm_server_battery_dump_insert_to_db(gcpu_st, 7);
1097 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1098 _ERR("Insert GCPU failed");
1100 _DBG("Insert GCPU successful");
1102 /* Free the Hash Map */
1103 g_hash_table_destroy(hash);
1109 int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
1113 BM_CHECK_INPUT_PARAM(handle);
1115 GSList *dp_data_iterator = NULL;
1116 GSList *dp_atm_iterator = NULL;
1117 void *prv_data = NULL;
1118 void *prv_app_id = NULL;
1119 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1121 /* creating hash-map with (key, value) = (app-id, data) */
1122 GHashTable *hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st1_free);
1124 long int edTime = 0, stTime = 0, sesTime = 0;
1125 long int highTime = 0, medTime = 0, lowTime = 0;
1127 /* iterating over list for data accumulation */
1128 for (dp_data_iterator = handle->display_list; dp_data_iterator; dp_data_iterator = dp_data_iterator->next) {
1129 bm_display_st *datalistnode = (bm_display_st *)(dp_data_iterator->data);
1130 edTime = datalistnode->stop;
1131 stTime = datalistnode->start;
1132 highTime += datalistnode->high;
1133 medTime += datalistnode->med;
1134 lowTime += datalistnode->low;
1135 sesTime += edTime - stTime;
1136 dp_atm_iterator = datalistnode->atm_list;
1137 for ( ; dp_atm_iterator; dp_atm_iterator = dp_atm_iterator->next) {
1138 app_time_map_st1 *dp_atm_node = (app_time_map_st1 *)dp_atm_iterator->data;
1140 _DBG("no data available");
1143 _DBG("display data available");
1144 onTime += dp_atm_node->time;
1147 if (g_hash_table_lookup_extended(hash, dp_atm_node->app_id, &prv_app_id, &prv_data) == true) {
1148 _DBG("previous app_id = %s", (char *)prv_app_id);
1149 app_time_map_st1 *dp_atm_prv_node = (app_time_map_st1 *)prv_data;
1150 dp_atm_node->time += dp_atm_prv_node->time;
1151 _DBG("update - app_id(%s), time(%d)", dp_atm_node->app_id, dp_atm_node->time);
1152 _INFO("updated - %d", g_hash_table_replace(hash, dp_atm_node->app_id, dp_atm_node));
1154 _DBG("insert - app_id(%s), time(%d)", dp_atm_node->app_id, dp_atm_node->time);
1155 _INFO("inserted - %d", g_hash_table_insert(hash, dp_atm_node->app_id, dp_atm_node));
1160 /* Read standard Rated Values from Device Spec File/Power Profile File */
1161 int sP_power_dp_low = 0, sP_power_dp_med = 0, sP_power_dp_high = 0;
1163 ret_val = bm_get_display_power_params(&sP_power_dp_high, &sP_power_dp_med, &sP_power_dp_low);
1164 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1165 _ERR("failed to get cpu power params");
1167 _DBG("received display power params - high[%d], medium[%d], low[%d]", sP_power_dp_high, sP_power_dp_med, sP_power_dp_low);
1168 /* Display power consumption Level - 1 at the Resource Level */
1169 lowTime /= 1000; medTime /= 1000; highTime /= 1000; onTime /= 1000;
1170 long int P_power_disp = 0, P_power_on = 0;
1171 _DBG("Total Time in s low %ld med %ld high %ld session %ld onTime %lf", lowTime, medTime, highTime, sesTime, onTime);
1172 P_power_on = ((sP_power_dp_low * lowTime) + (sP_power_dp_med * medTime) + (sP_power_dp_high * highTime));
1173 P_power_disp = P_power_on;
1174 _DBG("Calculated Power for Display P_power_disp (%ld), P_power_on (%ld)", P_power_disp, P_power_on);
1175 /* Display power consumption Level - 2 at the Application Level */
1176 GHashTableIter iter;
1177 gpointer key, value;
1178 g_hash_table_iter_init(&iter, hash);
1180 long int P_power_app_disp = 0, P_power_app_on = 0;
1183 long int ret_time = bm_get_log_time();
1185 appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
1186 if (app_usage == NULL) {
1187 _ERR("memory allocation failed");
1188 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1191 while (g_hash_table_iter_next(&iter, &key, &value)) {
1192 P_power_app_disp = 0; P_power_app_on = 0;
1194 app_time_map_st1 *temp = (app_time_map_st1 *)value;
1195 appid = temp->app_id;
1196 apptime = temp->time;
1199 P_power_app_on = (P_power_on * apptime)/onTime; //check for 0 denominator & same units
1200 P_power_app_disp = P_power_app_on;
1202 app_usage->AppId = appid;
1203 app_usage->rId_display = P_power_app_disp;
1204 app_usage->log_time = ret_time;
1205 /* Call Insert Function */
1206 _DBG("Calculated Power for Appid (%s) P_power_app_disp(%ld), P_power_app_on (%ld)", appid, P_power_app_disp, P_power_app_on);
1208 ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
1209 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1210 _ERR("Insert of DSP appid failed ");
1211 ret_val = bm_server_app_usage_insert_to_db(app_usage);
1213 ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_display, BM_PLUGIN_ID_DISPLAY);
1215 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1216 _ERR("Insert of Display App failed ");
1218 _DBG("Insert successful");
1221 /* commit to database the output after power calculation */
1222 resourceid_usage_s *res_usage = (resourceid_usage_s *)calloc(1, sizeof(resourceid_usage_s));
1223 if (res_usage == NULL) {
1224 _ERR("memory allocation failed");
1225 bm_appid_usage_free(app_usage);
1226 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1229 res_usage->ResourceId = BM_RID_DISP;
1230 res_usage->log_time = ret_time;
1231 res_usage->usage = P_power_disp;
1233 ret_val = bm_server_resource_usage_insert_to_db(res_usage);
1234 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1235 _ERR("Insert of Display resource failed ");
1237 _DBG("Insert successful");
1239 bm_appid_usage_free(app_usage);
1240 bm_resourceid_usage_free(res_usage);
1242 bd_gl_sbr_stat_s *gbr_st = (bd_gl_sbr_stat_s *)calloc(1, sizeof(bd_gl_sbr_stat_s));
1243 if (gbr_st == NULL) {
1244 _ERR("memory allocation failed");
1245 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1248 gbr_st->time_s = ret_time; gbr_st->dark = (BATTERY_MONITOR_GET_DATA_JOB_PERIOD/1000);
1249 gbr_st->dark -= sesTime; gbr_st->dim = lowTime; gbr_st->medium = medTime;
1250 gbr_st->bright = highTime;
1252 ret_val = bm_server_battery_dump_insert_to_db(gbr_st, 3);
1253 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1254 _ERR("Insert of GBR failed ");
1256 _DBG("Insert GBR successful");
1258 /* Free the Hash Map */
1259 g_hash_table_destroy(hash);
1265 int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool mode)
1269 BM_CHECK_INPUT_PARAM(handle);
1271 GSList *dn_data_iterator = NULL;
1272 GSList *dn_atm_iterator = NULL;
1273 void *prv_data = NULL;
1274 void *prv_app_id = NULL;
1275 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1277 /* creating hash-map with (key, value) = (app-id, data) */
1278 GHashTable *hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st2_free);
1280 long int tl0 = 0, tl1 = 0, tl2 = 0, tl3 = 0, tl4 = 0, tl5 = 0, tl6 = 0;
1281 long int RX = 0, TX = 0;
1282 double tl_total = 0;
1283 /* iterating over list for data accumulation */
1284 for (dn_data_iterator = handle->dn_data_list; dn_data_iterator; dn_data_iterator = dn_data_iterator->next) {
1285 bm_device_network_st *datalistnode = (bm_device_network_st *)(dn_data_iterator->data);
1286 tl0 += datalistnode->time_level_0;
1287 tl1 += datalistnode->time_level_1;
1288 tl2 += datalistnode->time_level_2;
1289 tl3 += datalistnode->time_level_3;
1290 tl4 += datalistnode->time_level_4;
1291 tl5 += datalistnode->time_level_5;
1292 tl6 += datalistnode->time_level_6;
1293 dn_atm_iterator = datalistnode->atm_list;
1294 for ( ; dn_atm_iterator; dn_atm_iterator = dn_atm_iterator->next) {
1295 app_time_map_st2 *dn_atm_node = (app_time_map_st2 *)dn_atm_iterator->data;
1297 _DBG("no data available");
1300 _DBG("device-network data available");
1301 RX += dn_atm_node->rx;
1302 TX += dn_atm_node->tx;
1305 if (g_hash_table_lookup_extended(hash, dn_atm_node->app_id, &prv_app_id, &prv_data) == true) {
1306 _DBG("previous app_id = %s", (char *)prv_app_id);
1307 app_time_map_st2 *dn_atm_prv_node = (app_time_map_st2 *)prv_data;
1308 dn_atm_node->time += dn_atm_prv_node->time;
1309 dn_atm_node->rx += dn_atm_prv_node->rx;
1310 dn_atm_node->tx += dn_atm_prv_node->tx;
1311 _DBG("update - app_id(%s), time(%d), rx(%d), tx(%d)", dn_atm_node->app_id,
1312 dn_atm_node->time, dn_atm_node->rx, dn_atm_node->tx);
1313 _INFO("updated - %d", g_hash_table_replace(hash, dn_atm_node->app_id, dn_atm_node));
1315 _DBG("insert - app_id(%s), time(%d), rx(%d), tx(%d)", dn_atm_node->app_id,
1316 dn_atm_node->time, dn_atm_node->rx, dn_atm_node->tx);
1317 _INFO("inserted - %d", g_hash_table_insert(hash, dn_atm_node->app_id, dn_atm_node));
1322 /* Read standard Rated Values from Device Spec File/Power Profile File */
1323 int sP_power_dn_tl0 = 0, sP_power_dn_tl1 = 0, sP_power_dn_tl2 = 0, sP_power_dn_tl3 = 0, sP_power_dn_tl4 = 0,
1324 sP_power_dn_tl5 = 0, sP_power_dn_tl6 = 0, sP_power_dn_tx = 0, sP_power_dn_rx = 0;
1326 ret_val = bm_get_device_network_power_params(&sP_power_dn_tl0, &sP_power_dn_tl1, &sP_power_dn_tl2, &sP_power_dn_tl3,
1327 &sP_power_dn_tl4, &sP_power_dn_tl5, &sP_power_dn_tl6, &sP_power_dn_rx, &sP_power_dn_tx);
1328 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1329 _ERR("failed to get cpu power params");
1330 _DBG("received device-network power params - signal_0[%d]\n, signal_1[%d]\n, signal_2[%d]\n, \
1331 signal_3[%d]\n, signal_4[%d]\n, signal_5[%d]\n, signal_6[%d]\n, rx[%d]\n, tx[%d]\n",
1332 sP_power_dn_tl0, sP_power_dn_tl1, sP_power_dn_tl2, sP_power_dn_tl3, sP_power_dn_tl4, sP_power_dn_tl5,
1333 sP_power_dn_tl6, sP_power_dn_rx, sP_power_dn_tx);
1335 tl0 /= 1000; tl1 /= 1000; tl2 /= 1000; tl3 /= 1000; tl4 /= 1000; tl5 /= 1000; tl6 /= 1000;
1336 tl_total = tl0 + tl1 + tl2 + tl3 + tl4 + tl5 + tl6;
1337 /* Device Network power consumption Level - 1 at the Resource Level */
1338 long int P_power_dntw = 0, P_power_data = 0, P_power_conn = 0;
1339 P_power_conn = ((sP_power_dn_tl0 * tl0) + (sP_power_dn_tl1 * tl1) + (sP_power_dn_tl2 * tl2) + (sP_power_dn_tl3 * tl3) + (sP_power_dn_tl4 * tl4) + (sP_power_dn_tl5 * tl5) + (sP_power_dn_tl6 * tl6));
1340 P_power_data = ((sP_power_dn_tx) * TX) + ((sP_power_dn_rx) * RX);
1341 P_power_dntw = P_power_conn + P_power_data;
1343 _DBG("Calculated Power for Device Network P_power_ntw(%ld), P_power_conn (%ld), P_power_data(%ld)", P_power_dntw, P_power_conn, P_power_data);
1344 /* Device Network power consumption Level - 2 at the Application Level */
1345 GHashTableIter iter;
1346 gpointer key, value;
1347 g_hash_table_iter_init(&iter, hash);
1349 long int P_power_app_dntw = 0, P_power_app_data = 0, P_power_app_conn = 0;
1351 long int apprx = 0, apptx = 0;
1353 long int ret_time = bm_get_log_time();
1355 appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
1356 if (app_usage == NULL) {
1357 _ERR("memory allocation failed");
1358 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1361 while (g_hash_table_iter_next(&iter, &key, &value)) {
1362 P_power_app_dntw = 0; P_power_app_data = 0 ; P_power_app_conn = 0;
1363 apprx = 0, apptx = 0, apptime = 0;
1364 app_time_map_st2 *temp = (app_time_map_st2 *)value;
1365 appid = temp->app_id;
1366 apprx = temp->rx; apptx = temp->tx;
1367 apptime = temp->time; apptime /= 1000;
1368 P_power_app_data = ((sP_power_dn_tx) * apptx) + ((sP_power_dn_rx) * apprx);
1370 P_power_app_conn = (P_power_conn * apptime)/tl_total; //check for 0 denominator & same units
1371 P_power_app_dntw = P_power_app_data + P_power_app_conn;
1373 app_usage->AppId = appid;
1374 app_usage->rId_device_network = P_power_app_dntw;
1375 app_usage->log_time = ret_time;
1376 /* Call Insert Function */
1377 _DBG("Calculated Power for Appid (%s) P_power_app_dntw(%ld), P_power_app_conn (%ld), P_power_app_data(%ld)", appid, P_power_app_dntw, P_power_app_conn, P_power_app_data);
1379 ret_val = bm_server_appid_insert_to_db(app_usage->AppId);
1380 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1381 _ERR("Insert of DN appid failed ");
1382 ret_val = bm_server_app_usage_insert_to_db(app_usage);
1384 ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_device_network, BM_PLUGIN_ID_DEVICE_NETWORK);
1385 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1386 _ERR("Insert of Data Ntwk App failed ");
1388 _DBG("Insert successful");
1391 /* commit to database the output after power calculation */
1392 resourceid_usage_s *res_usage = (resourceid_usage_s *)calloc(1, sizeof(resourceid_usage_s));
1393 if (res_usage == NULL) {
1394 _ERR("memory allocation failed");
1395 bm_appid_usage_free(app_usage);
1396 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1399 res_usage->ResourceId = BM_RID_DNTW;
1400 res_usage->log_time = ret_time;
1401 res_usage->usage = P_power_dntw;
1403 ret_val = bm_server_resource_usage_insert_to_db(res_usage);
1404 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1405 _ERR("Insert of Data Network resource failed ");
1407 _DBG("Insert successful");
1409 bm_appid_usage_free(app_usage);
1410 bm_resourceid_usage_free(res_usage);
1412 bd_gl_sgt_stat_s *gst_st = (bd_gl_sgt_stat_s *)calloc(1, sizeof(bd_gl_sgt_stat_s));
1413 if (gst_st == NULL) {
1414 _ERR("memory allocation failed");
1415 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1418 gst_st->time_s = ret_time; gst_st->scan_time = tl0; gst_st->none_time = tl1;
1419 gst_st->poor_time = tl2; gst_st->med_time = tl3 + tl4;
1420 gst_st->good_time = tl5; gst_st->grt_time = tl6;
1421 ret_val = bm_server_battery_dump_insert_to_db(gst_st, 4);
1422 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1423 _ERR("Insert of GWFL failed ");
1425 _DBG("Insert successful");
1428 /* Free the Hash Map */
1429 g_hash_table_destroy(hash);
1435 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
1436 int bm_gps_calc_power_and_commit(bm_gps_st *handle)
1440 BM_CHECK_INPUT_PARAM(handle);
1442 GSList *gps_data_iterator = NULL;
1443 GSList *gps_atm_iterator = NULL;
1444 void *prv_data = NULL;
1445 void *prv_app_id = NULL;
1446 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1448 /* creating hash-map with (key, value) = (app-id, data) */
1449 GHashTable *hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st1_free);
1451 long int edTime = 0;
1452 long int stTime = 0;
1455 /* iterating over list for data accumulation */
1456 for (gps_data_iterator = handle->gps_data_list; gps_data_iterator; gps_data_iterator = gps_data_iterator->next) {
1457 bm_gps_st *datalistnode = (bm_gps_st *)(gps_data_iterator->data);
1458 edTime = datalistnode->connStartTime;
1459 stTime = datalistnode->connStopTime;
1460 sesTime = edTime - stTime;
1461 gps_atm_iterator = datalistnode->atm_list;
1462 for ( ; gps_atm_iterator; gps_atm_iterator = gps_atm_iterator->next) {
1463 app_time_map_st1 *gps_atm_node = (app_time_map_st1 *)gps_atm_iterator->data;
1464 if (!gps_atm_node) {
1465 _DBG("no bt data available");
1468 _DBG("gps data available");
1469 onTime += gps_atm_node->time;
1472 if (g_hash_table_lookup_extended(hash, gps_atm_node->app_id, &prv_app_id, &prv_data) == true) {
1473 _DBG("previous app_id = %s", (char *)prv_app_id);
1474 app_time_map_st1 *gps_atm_prv_node = (app_time_map_st1 *)prv_data;
1475 gps_atm_node->time += gps_atm_prv_node->time;
1476 _DBG("update - app_id(%s), time(%d)", gps_atm_node->app_id, gps_atm_node->time);
1477 _INFO("updated - %d", g_hash_table_replace(hash, gps_atm_node->app_id, gps_atm_node));
1479 _DBG("insert - app_id(%s), time(%d)", gps_atm_node->app_id, gps_atm_node->time);
1480 _INFO("inserted - %d", g_hash_table_insert(hash, gps_atm_node->app_id, gps_atm_node));
1485 /* Read standard Rated Values from Device Spec File/Power Profile File */
1486 int sP_power_gps_conn = 0;
1488 ret_val = bm_get_gps_sensor_power_params(&sP_power_gps_conn);
1489 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1490 _ERR("failed to get gps-sensor power params");
1492 _DBG("gps-sensor power params - gps[%d]", sP_power_gps_conn);
1493 /* GPS power consumption Level - 1 at the Resource Level */
1494 int P_power_gps = 0, P_power_conn = 0;
1495 P_power_conn = ((sP_power_gps_conn * sesTime));
1497 P_power_gps = P_power_conn;
1498 _DBG("Calculated Power for GPS P_power_gps (%d), P_power_conn (%d)", P_power_gps, P_power_conn);
1500 /* GPS power consumption Level - 2 at the Application Level */
1501 GHashTableIter iter;
1502 gpointer key, value;
1503 g_hash_table_iter_init(&iter, hash);
1505 int P_power_app_gps = 0, P_power_app_conn = 0;
1508 long int ret_time = bm_get_log_time();
1510 appid_usage_s *app_usage = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
1511 if (app_usage == NULL) {
1512 _ERR("memory allocation failed");
1513 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1516 while (g_hash_table_iter_next(&iter, &key, &value)) {
1517 P_power_app_gps = 0; P_power_app_conn = 0;
1519 app_time_map_st1 *temp = (app_time_map_st1 *)value;
1520 appid = temp->app_id;
1521 apptime = temp->time;
1524 P_power_app_conn = (P_power_conn * apptime)/onTime; //check for 0 denominator & same units
1525 P_power_app_gps = P_power_app_conn;
1527 app_usage->AppId = appid;
1528 app_usage->rId_gps = P_power_app_gps;
1529 app_usage->log_time = ret_time;
1530 /* Call Insert Function */
1531 _DBG("Calculated Power for Appid (%s) P_power_app_gps (%d), P_power_app_conn (%d)", appid, P_power_app_gps, P_power_app_conn);
1532 ret_val = bm_server_app_usage_insert_to_db(app_usage);
1533 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1534 _ERR("Insert of GPS App failed ");
1536 _DBG("Insert successful");
1539 /* commit to database the output after power calculation */
1540 resourceid_usage_s *res_usage = (resourceid_usage_s *)calloc(1, sizeof(resourceid_usage_s));
1541 if (res_usage == NULL) {
1542 _ERR("memory allocation failed");
1543 bm_appid_usage_free(app_usage);
1544 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1547 res_usage->ResourceId = BM_RID_GPS;
1548 res_usage->log_time = ret_time;
1549 res_usage->usage = P_power_gps;
1551 ret_val = bm_server_resource_usage_insert_to_db(res_usage);
1552 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1553 _ERR("Insert of GPS resource failed ");
1555 _DBG("Insert successful");
1557 bm_appid_usage_free(app_usage);
1558 bm_resourceid_usage_free(res_usage);
1560 /* Free the Hash Map */
1561 g_hash_table_destroy(hash);
1567 int bm_hrm_calc_power_and_commit(bm_hrm_sensor_st *handle)
1571 BM_CHECK_INPUT_PARAM(handle);
1573 GSList *hrm_data_iterator = NULL;
1574 GSList *hrm_atm_iterator = NULL;
1576 void *prv_data = NULL;
1577 void *prv_app_id = NULL;
1579 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1581 /* creating hash-map with (key, value) = (app-id, data) */
1582 GHashTable *hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st1_free);
1584 /* iterating over list for data accumulation */
1585 for (hrm_data_iterator = handle->hrm_data_list; hrm_data_iterator; hrm_data_iterator = hrm_data_iterator->next) {
1587 hrm_atm_iterator = ((bm_hrm_sensor_st *)(hrm_data_iterator->data))->atm_list;
1589 for ( ; hrm_atm_iterator; hrm_atm_iterator = hrm_atm_iterator->next) {
1591 app_time_map_st1 *hrm_atm_node = (app_time_map_st1 *)hrm_atm_iterator->data;
1593 if (!hrm_atm_node) {
1594 _DBG("no bt data available");
1598 _DBG("hrm data available");
1603 if (g_hash_table_lookup_extended(hash, hrm_atm_node->app_id, &prv_app_id, &prv_data) == true) {
1605 _DBG("previous app_id = %s", (char *)prv_app_id);
1607 app_time_map_st1 *hrm_atm_prv_node = (app_time_map_st1 *)prv_data;
1609 hrm_atm_node->time += hrm_atm_prv_node->time;
1611 _DBG("update - app_id(%s), time(%d)", hrm_atm_node->app_id, hrm_atm_node->time);
1613 _INFO("updated - %d", g_hash_table_replace(hash, hrm_atm_node->app_id, hrm_atm_node));
1617 _DBG("insert - app_id(%s), time(%d)", hrm_atm_node->app_id, hrm_atm_node->time);
1619 _INFO("inserted - %d", g_hash_table_insert(hash, hrm_atm_node->app_id, hrm_atm_node));
1624 int sP_power_hrm_conn = 0;
1626 /* read hrm-sensor power params */
1627 ret_val = bm_get_hrm_sensor_power_params(&sP_power_hrm_conn);
1628 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1629 _ERR("failed to get hrm-sensor power params");
1631 _DBG("hrm-sensor power params - gps[%d]", sP_power_hrm_conn);
1633 /* apply power calculation on the hash-map data*/
1635 /* commit to database the output after power calculation */
1637 /* Free the Hash Map */
1638 g_hash_table_destroy(hash);
1643 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
1645 int bm_battery_calc_power_and_commit(void)
1649 int ret_val = DEVICE_ERROR_NONE;
1650 int battery_percent = 0;
1651 bool is_charging = false;
1653 ret_val = device_battery_get_percent(&battery_percent);
1654 BM_RETURN_VAL((ret_val == DEVICE_ERROR_NONE), {},
1655 BATTERY_MONITOR_ERROR_INTERNAL, "failed to get battery percent");
1657 ret_val = device_battery_is_charging(&is_charging);
1658 BM_RETURN_VAL((ret_val == DEVICE_ERROR_NONE), {},
1659 BATTERY_MONITOR_ERROR_INTERNAL, "failed to get charging status");
1661 device_battery_level_e battery_level;
1662 ret_val = device_battery_get_level_status(&battery_level);
1663 BM_RETURN_VAL((ret_val == DEVICE_ERROR_NONE), {},
1664 BATTERY_MONITOR_ERROR_INTERNAL, "failed to get warning status");
1666 switch (battery_level) {
1667 case DEVICE_BATTERY_LEVEL_EMPTY:
1668 _DBG("battery level - EMPTY");
1670 case DEVICE_BATTERY_LEVEL_CRITICAL:
1671 _DBG("battery level - CRITICAL");
1673 case DEVICE_BATTERY_LEVEL_LOW:
1674 _DBG("battery level - LOW");
1676 case DEVICE_BATTERY_LEVEL_HIGH:
1677 _DBG("battery level - HIGH");
1679 case DEVICE_BATTERY_LEVEL_FULL:
1680 _DBG("battery level - FULL");
1683 _DBG("invalid battery-level");
1687 _INFO("battery percent[%d] & charging status[%s]",
1688 battery_percent, (is_charging == true) ? "TRUE" : "FALSE");
1690 int sP_power_battery = 0;
1692 ret_val = bm_get_battery_power_params(&sP_power_battery);
1693 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1694 _ERR("failed to get battery power params");
1696 _DBG("received battery power params - battery[%d]", sP_power_battery);
1698 ret_val = BATTERY_MONITOR_ERROR_NONE;
1704 int bm_get_data_from_handles(void)
1708 BM_CHECK_INPUT_PARAM(bm_data_handle);
1709 BM_CHECK_INPUT_PARAM(bm_req_flag_h);
1711 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1714 gl_hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st1_free);
1716 _ERR("Global Hash creation failed");
1718 /* parsing ble data */
1719 if (bm_data_handle->bm_ble_handle != NULL && bm_req_flag_h->req_ble_data) {
1720 _DBG("parsing ble data");
1721 ret_val = bm_ble_calc_power_and_commit(bm_data_handle->bm_ble_handle, mode);
1722 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "ble power cal failure");
1724 _DBG("set ble flag - true");
1725 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_BLE, true);
1727 _DBG("completed ble data request");
1729 /* parsing wifi data */
1730 if (bm_data_handle->bm_wifi_handle != NULL && bm_req_flag_h->req_wifi_data) {
1731 _DBG("parsing wifi data");
1732 ret_val = bm_wifi_calc_power_and_commit(bm_data_handle->bm_wifi_handle, mode);
1733 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "wifi power calc failure");
1735 _DBG("set wifi flag - true");
1736 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_WIFI, true);
1738 _DBG("completed wifi data request");
1740 /* parsing cpu data */
1741 if (bm_data_handle->bm_cpu_handle != NULL && bm_req_flag_h->req_cpu_data) {
1742 _DBG("parsing cpu data");
1743 ret_val = bm_cpu_calc_power_and_commit(bm_data_handle->bm_cpu_handle, mode);
1744 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "cpu power calc failure");
1746 _DBG("set cpu flag - true");
1747 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_CPU, true);
1749 _DBG("completed cpu data request");
1751 /* parsing display data */
1752 if (bm_data_handle->bm_display_handle != NULL && bm_req_flag_h->req_dp_data) {
1753 _DBG("parsing display data");
1754 ret_val = bm_display_calc_power_and_commit(bm_data_handle->bm_display_handle, mode);
1755 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "display power calc failure");
1757 _DBG("set display flag - true");
1758 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_DISPLAY, true);
1760 _DBG("completed display data request");
1762 /* parsing device-network data */
1763 if (bm_data_handle->bm_dn_handle != NULL && bm_req_flag_h->req_dn_data) {
1764 _DBG("parsing device-network data");
1765 ret_val = bm_device_network_calc_power_and_commit(bm_data_handle->bm_dn_handle, mode);
1766 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "device-network power calc failure");
1768 _DBG("set device-network flag - true");
1769 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_DEVICE_NETWORK, true);
1771 _DBG("completed device-network data request");
1773 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
1774 /* parsing gps data */
1775 if (bm_data_handle->bm_gps_handle != NULL && bm_req_flag_h->req_gps_data) {
1776 _DBG("parsing gps data");
1777 ret_val = bm_gps_calc_power_and_commit(bm_data_handle->bm_gps_handle);
1778 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "gps-sensor power calc failure");
1780 _DBG("set gps-sensor flag - true");
1781 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_GPS_SENSOR, true);
1783 _DBG("completed gps data request");
1785 /* parsing hrm data */
1786 if (bm_data_handle->bm_hrm_handle != NULL && bm_req_flag_h->req_hrm_data) {
1787 _DBG("parsing hrm data");
1788 ret_val = bm_hrm_calc_power_and_commit(bm_data_handle->bm_hrm_handle);
1789 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "hrm-sensor power calc failure");
1791 _DBG("set hrm-sensor flag - true");
1792 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_HRM_SENSOR, true);
1794 _DBG("completed hrm data request");
1795 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
1797 /* parsing battery data */
1798 _DBG("parsing battery data");
1799 ret_val = bm_battery_calc_power_and_commit();
1800 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "battery power calc failure");
1801 _DBG("completed battery data request");
1803 ret_val = bm_insert_appid_session_usage_to_db();
1804 BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "battery session logging failure");
1806 /* set free all the data handles */
1807 if (bm_data_handle != NULL) {
1808 _DBG("Call Handle Free");
1809 bm_set_free_data_handles(&bm_data_handle);
1812 /* set free global hash values */
1814 g_hash_table_destroy(gl_hash);
1819 return BATTERY_MONITOR_ERROR_NONE;
1822 int bm_start_getting_feature_data(void)
1826 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1828 bm_plugin_info_h *bm_plugin = NULL;
1829 bm_data_h handle = NULL;
1831 /* allocate data & flag handle */
1832 if (bm_data_handle == NULL) {
1833 bm_data_handle = (bm_feature_data_h)calloc(1, sizeof(struct bm_feature_data_handles_s));
1834 if (!bm_data_handle) {
1835 _ERR("memory allocation failed - data handle");
1836 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
1840 /* request data from each plugin & store its handle */
1841 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
1842 for (id = BM_PLUGIN_ID_BLE; id < BM_PLUGIN_ID_MAX; ++id) {
1844 for (id = BM_PLUGIN_ID_BLE; id <= BM_PLUGIN_ID_DEVICE_NETWORK; ++id) {
1846 bm_get_feature_plugin_handle(&bm_plugin, id);
1848 if (!bm_plugin || !bm_plugin->api) {
1849 _WARN("Plugin with ID: %s is NULL", bm_get_plugin_name(id));
1852 _INFO(" requesting data from plugin - %s", bm_get_plugin_name(id));
1855 case BM_PLUGIN_ID_BLE: /* BLE Data */
1856 ret_val = bm_plugin->api->get_feature_data(&handle, id);
1857 if (ret_val != BATTERY_MONITOR_ERROR_NONE) {
1858 _DBG("ble data not available : improper value");
1861 if (handle != NULL) {
1862 _DBG("received ble data handle");
1863 bm_data_handle->bm_ble_handle = (bm_bluetooth_st *)(handle);
1866 case BM_PLUGIN_ID_WIFI: /* WI-FI Data */
1867 ret_val = bm_plugin->api->get_feature_data(&handle, id);
1868 if (ret_val != BATTERY_MONITOR_ERROR_NONE) {
1869 _DBG("wifi data not available : improper value");
1872 if (handle != NULL) {
1873 _DBG("received wifi data handle");
1874 bm_data_handle->bm_wifi_handle = (bm_wifi_st *)(handle);
1877 case BM_PLUGIN_ID_CPU: /* CPU Data */
1878 ret_val = bm_plugin->api->get_feature_data(&handle, id);
1879 if (ret_val != BATTERY_MONITOR_ERROR_NONE) {
1880 _DBG("cpu data not available : improper value");
1883 if (handle != NULL) {
1884 _DBG("received cpu data handle");
1885 bm_data_handle->bm_cpu_handle = (bm_cpu_st *)(handle);
1888 case BM_PLUGIN_ID_DISPLAY: /* DISPLAY Data */
1889 ret_val = bm_plugin->api->get_feature_data(&handle, id);
1890 if (ret_val != BATTERY_MONITOR_ERROR_NONE) {
1891 _DBG("display data not available : improper value");
1894 if (handle != NULL) {
1895 _DBG("received display data handle");
1896 bm_data_handle->bm_display_handle = (bm_display_st *)(handle);
1899 case BM_PLUGIN_ID_DEVICE_NETWORK: /* DEVICE-NETWORK Data */
1900 ret_val = bm_plugin->api->get_feature_data(&handle, id);
1901 if (ret_val != BATTERY_MONITOR_ERROR_NONE) {
1902 _DBG("device-network data not available : improper value");
1905 if (handle != NULL) {
1906 _DBG("received device network data handle");
1907 bm_data_handle->bm_dn_handle = (bm_device_network_st *)(handle);
1910 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
1911 case BM_PLUGIN_ID_GPS_SENSOR: /* GPS Sensor Data */
1912 ret_val = bm_plugin->api->get_feature_data(&handle, id);
1913 if (ret_val != BATTERY_MONITOR_ERROR_NONE) {
1914 _DBG("gps data not available : improper value");
1917 if (handle != NULL) {
1918 _DBG("received gps data handle");
1919 bm_data_handle->bm_gps_handle = (bm_gps_st *)(handle);
1922 case BM_PLUGIN_ID_HRM_SENSOR: /* HRM Sensor Data */
1923 ret_val = bm_plugin->api->get_feature_data(&handle, id);
1924 if (ret_val != BATTERY_MONITOR_ERROR_NONE) {
1925 _DBG("hrm data not available : improper value");
1928 if (handle != NULL) {
1929 _DBG("received hrm data handle");
1930 bm_data_handle->bm_hrm_handle = (bm_hrm_sensor_st *)(handle);
1933 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
1939 /* parse data which is received through handles */
1940 ret_val = bm_get_data_from_handles();
1941 BM_RETURN_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), {},
1942 BATTERY_MONITOR_ERROR_INTERNAL, "error in parsing data from handles");
1948 int bm_clean_db_table_for_job_id(void)
1952 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1954 ret_val = bm_server_delete_table_by_time_interval();
1956 BM_RETURN_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), {},
1957 BATTERY_MONITOR_ERROR_DB_FAILED, "error in deleting old db data");
1963 gboolean bm_request_feature_data(gpointer data)
1967 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1969 ret_val = bm_start_getting_feature_data();
1970 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1971 _ERR("error requesting feature data");
1977 gboolean bm_delete_data_from_db(gpointer data)
1981 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1983 ret_val = bm_clean_db_table_for_job_id();
1984 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1985 _ERR("error cleaning database");
1991 int initialize_power_engine(void)
1995 int ret_val = BATTERY_MONITOR_ERROR_NONE;
1997 bm_req_flag_h = (struct bm_req_feature_data_handle_flag_s *)
1998 calloc(1, sizeof(struct bm_req_feature_data_handle_flag_s));
1999 if (!bm_req_flag_h) {
2000 _ERR("memory allocation failed - requet flag handle");
2001 return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
2004 bm_set_req_flag_handles(true);
2006 gtimeo_id[0] = 0; gtimeo_id[1] = 0;
2007 gtimeo_id[0] = g_timeout_add(BATTERY_MONITOR_GET_DATA_JOB_PERIOD, bm_request_feature_data, NULL);
2008 gtimeo_id[1] = g_timeout_add(BATTERY_MONITOR_DELETE_DB_JOB_PERIOD, bm_delete_data_from_db, NULL);
2009 _DBG("timeout with id %d and %d added", gtimeo_id[0], gtimeo_id[1]);
2011 ret_val = bm_clean_db_table_for_job_id();
2012 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
2013 _ERR("error cleaning database");
2015 ret_val = bm_get_battery_power_params(&battery_capacity);
2016 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
2017 _ERR("unable ro read battery capacity");
2018 battery_capacity *= cmah; //Battery Capacity in mAs
2024 void deinitialize_power_engine(void)
2028 /* remove timeouts*/
2029 for (int i = 0; i < 2; i++) {
2030 if (gtimeo_id[i] != 0)
2031 g_source_remove(gtimeo_id[i]);
2034 /* set all data-handle free */
2035 if (bm_data_handle != NULL) {
2036 _DBG("setting handle free - deinitializing");
2037 bm_set_free_data_handles(&bm_data_handle);
2040 /* set flag handle free*/
2041 BM_FREE(bm_req_flag_h);