Add CPU stats for Battery Dump
[platform/core/system/batterymonitor.git] / src / bm_power_engine.c
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <sys/time.h>
19
20 #include <device/battery.h>
21 #include <device/callback.h>
22
23 #include "bm_power_engine.h"
24 #include "bm_dbg.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"
31 #include "bm_util.h"
32 #include "bd_history_item.h"
33
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;
39 int gtimeo_id[2];
40
41 int bm_engine_get_usage_by_app_id_for_resource_id(const gchar *app_id, gint resource_id, gint duration, int *b_usage)
42 {
43         ENTER;
44
45         BM_CHECK_INPUT_PARAM(app_id);
46
47         BM_RETURN_VAL((resource_id < BM_PLUGIN_ID_MAX), {},
48                         BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid resource-id param");
49
50         BM_RETURN_VAL((duration < BM_DURATION_TYPE_MAX), {},
51                         BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration param");
52
53         _INFO("usage requested - app-id[%s], resource_id[%d], duration[%d]",
54                                 app_id, resource_id, duration);
55
56         int error = BATTERY_MONITOR_ERROR_NONE;
57
58         appid_usage_s *app_usage = bm_server_query_app_usage_by_appid(app_id, duration, &error);
59
60         if (!app_usage) {
61                 _ERR("battery usage data for app_id(%s) is not available", app_id);
62                 return BATTERY_MONITOR_ERROR_INTERNAL;
63         }
64
65         if (error != BATTERY_MONITOR_ERROR_NONE) {
66                 _ERR("error reported, error = %d", error);
67                 return BATTERY_MONITOR_ERROR_INTERNAL;
68         }
69
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 */
88
89         bm_appid_usage_free(app_usage);
90
91         EXIT;
92         return BATTERY_MONITOR_ERROR_NONE;
93 }
94
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)
96 {
97         ENTER;
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);
103
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);
106         if (!app_usage) {
107                 _ERR("battery usage data for app_id(%s) is not available", app_id);
108                 return BATTERY_MONITOR_ERROR_INTERNAL;
109         }
110         if (error != BATTERY_MONITOR_ERROR_NONE) {
111                 _ERR("error reported, error = %d", error);
112                 return BATTERY_MONITOR_ERROR_INTERNAL;
113         }
114
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);
130         else
131                 *battery_usage = -1;
132 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
133
134         bm_appid_usage_free(app_usage);
135
136         EXIT;
137         return BATTERY_MONITOR_ERROR_NONE;
138 }
139
140 int bm_engine_get_total_usage_by_app_id(const gchar *app_id, gint duration, int *b_usage)
141 {
142         ENTER;
143
144         BM_CHECK_INPUT_PARAM(app_id);
145
146         BM_RETURN_VAL((duration < BM_DURATION_TYPE_MAX), {},
147                         BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration param");
148
149         _INFO("usage requested - app-id[%s], duration[%d]", app_id, duration);
150
151         int error = BATTERY_MONITOR_ERROR_NONE;
152
153         appid_usage_s *app_usage = bm_server_query_app_usage_by_appid(app_id, duration, &error);
154
155         if (!app_usage) {
156                 _ERR("battery usage data for app_id(%s) is not available", app_id);
157                 return BATTERY_MONITOR_ERROR_INTERNAL;
158         }
159
160         if (error != BATTERY_MONITOR_ERROR_NONE) {
161                 _ERR("error reported, error = %d", error);
162                 return BATTERY_MONITOR_ERROR_INTERNAL;
163         }
164
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);
167
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 */
171
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);
174
175         bm_appid_usage_free(app_usage);
176
177         EXIT;
178         return BATTERY_MONITOR_ERROR_NONE;
179 }
180
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)
182 {
183         ENTER;
184
185         BM_CHECK_INPUT_PARAM(app_id);
186
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);
191         if (!app_usage) {
192                 _ERR("battery usage data for app_id(%s) is not available", app_id);
193                 return BATTERY_MONITOR_ERROR_INTERNAL;
194         }
195         if (error != BATTERY_MONITOR_ERROR_NONE) {
196                 _ERR("error reported, error = %d", error);
197                 return BATTERY_MONITOR_ERROR_INTERNAL;
198         }
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);
206
207         bm_appid_usage_free(app_usage);
208         EXIT;
209         return BATTERY_MONITOR_ERROR_NONE;
210 }
211
212 int bm_engine_get_total_usage_by_resource_id(gint resource_id, gint duration, int *b_usage)
213 {
214         ENTER;
215
216         BM_RETURN_VAL((resource_id < BM_PLUGIN_ID_MAX), {},
217                         BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid resource-id");
218
219         BM_RETURN_VAL((duration < BM_DURATION_TYPE_MAX), {},
220                         BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration param");
221
222         _INFO("usage requested - resource_id[%d], duration[%d]", resource_id, duration);
223
224         int error = BATTERY_MONITOR_ERROR_NONE;
225
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;
231         }
232
233         _INFO("resource string - [%s]", resource_id_str);
234
235         resourceid_usage_s *resource_usage = bm_server_query_resource_usage_resourceid(resource_id_str, duration, &error);
236
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;
240         }
241
242         if (error != BATTERY_MONITOR_ERROR_NONE) {
243                 _ERR("error reported, error = %d", error);
244                 return BATTERY_MONITOR_ERROR_INTERNAL;
245         }
246
247         *b_usage = bm_calc_individual_consumption(resource_usage->usage, battery_capacity);
248
249         bm_resourceid_usage_free(resource_usage);
250
251         EXIT;
252         return BATTERY_MONITOR_ERROR_NONE;
253 }
254
255 int bm_engine_get_total_mah_usage_by_resource_id_ci(gint resource_id, gint64 start_time, gint64 end_time, double *battery_usage)
256 {
257         ENTER;
258
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);
263
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;
270         }
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;
276         }
277         if (error != BATTERY_MONITOR_ERROR_NONE) {
278                 _ERR("error reported, error = %d", error);
279                 return BATTERY_MONITOR_ERROR_INTERNAL;
280         }
281         *battery_usage = bm_calc_individual_mah_consumption(resource_usage->usage, cmah);
282         bm_resourceid_usage_free(resource_usage);
283
284         EXIT;
285         return BATTERY_MONITOR_ERROR_NONE;
286 }
287
288 int bm_engine_get_all_resource_usage(const gchar *app_id, gint duration, bm_total_consumption_h data)
289 {
290         ENTER;
291
292         BM_CHECK_INPUT_PARAM(app_id);
293
294         BM_CHECK_INPUT_PARAM(data);
295
296         BM_RETURN_VAL((duration < BM_DURATION_TYPE_MAX), {},
297                         BATTERY_MONITOR_ERROR_INVALID_PARAMETER, "invalid duration param");
298
299         _INFO("usage requested - app-id[%s], duration[%d]", app_id, duration);
300
301         int error = BATTERY_MONITOR_ERROR_NONE;
302
303         appid_usage_s *app_usage = bm_server_query_app_usage_by_appid(app_id, duration, &error);
304         if (!app_usage) {
305                 _ERR("battery usage data for app_id(%s) is not available", app_id);
306                 return BATTERY_MONITOR_ERROR_INTERNAL;
307         }
308         if (error != BATTERY_MONITOR_ERROR_NONE) {
309                 _ERR("error reported, error = %d", error);
310                 return BATTERY_MONITOR_ERROR_INTERNAL;
311         }
312
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 */
323
324         bm_appid_usage_free(app_usage);
325
326         EXIT;
327         return BATTERY_MONITOR_ERROR_NONE;
328 }
329
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)
331 {
332         ENTER;
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);
338         if (!app_usage) {
339                 _ERR("battery usage data for app_id(%s) is not available", app_id);
340                 return BATTERY_MONITOR_ERROR_INTERNAL;
341         }
342         if (error != BATTERY_MONITOR_ERROR_NONE) {
343                 _ERR("error reported, error = %d", error);
344                 return BATTERY_MONITOR_ERROR_INTERNAL;
345         }
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 */
353
354         bm_appid_usage_free(app_usage);
355
356         EXIT;
357         return BATTERY_MONITOR_ERROR_NONE;
358         EXIT;
359 }
360
361 void bm_engine_set_req_flag_handle(bm_plugin_id_e req_plugin_id, bool value)
362 {
363         ENTER;
364
365         if (!bm_req_flag_h) {
366                 _ERR("invalid flag handle");
367                 return;
368         }
369
370         switch (req_plugin_id) {
371         case BM_PLUGIN_ID_BLE:
372                 bm_req_flag_h->req_ble_data = value;
373                 break;
374         case BM_PLUGIN_ID_WIFI:
375                 bm_req_flag_h->req_wifi_data = value;
376                 break;
377         case BM_PLUGIN_ID_CPU:
378                 bm_req_flag_h->req_cpu_data = value;
379                 break;
380         case BM_PLUGIN_ID_DISPLAY:
381                 bm_req_flag_h->req_dp_data = value;
382                 break;
383         case BM_PLUGIN_ID_DEVICE_NETWORK:
384                 bm_req_flag_h->req_dn_data = value;
385                 break;
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;
389                 break;
390         case BM_PLUGIN_ID_HRM_SENSOR:
391                 bm_req_flag_h->req_hrm_data = value;
392                 break;
393 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
394         default:
395                 _DBG("Invalid plugin ID");
396                 break;
397         }
398
399         EXIT;
400         return;
401 }
402
403 void bm_set_req_flag_handles(bool value)
404 {
405         ENTER;
406
407         if (!bm_req_flag_h) {
408                 _ERR("invalid flag handle");
409                 return;
410         }
411
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 */
421
422         _INFO("request flags set-[%s]", (value == true) ? "TRUE" : "FALSE");
423
424         EXIT;
425         return;
426 }
427
428 static int bm_appid_session_usage_map(char *app_id, int app_usage, bm_plugin_id_e resource_id)
429 {
430         ENTER;
431
432         BM_CHECK_INPUT_PARAM(app_id);
433
434         void *prv_data = NULL;
435         void *prv_app_id = NULL;
436
437         int ret_val = BATTERY_MONITOR_ERROR_NONE;
438         _DBG("app_id [%s] app_usage is [%d]", app_id, app_usage);
439
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 */
458
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);
462         } else {
463                 appid_usage_s *temp = (appid_usage_s *)calloc(1, sizeof(appid_usage_s));
464                 if (temp == NULL) {
465                         _ERR("memory allocation failed");
466                         return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
467                 }
468
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 */
485
486                 _DBG("insert - app_usage app_id(%s), for resource(%d), by usage(%d)", app_id, resource_id, app_usage);
487
488                 _INFO("inserted - %d", g_hash_table_insert(gl_hash, app_id, temp));
489         }
490
491         EXIT;
492         return ret_val;
493 }
494
495 static int bm_insert_appid_session_usage_to_db()
496 {
497         ENTER;
498
499         GHashTableIter iter;
500         gpointer key, value;
501         appid_usage_s *app_usage = NULL;
502
503         int ret_val = BATTERY_MONITOR_ERROR_NONE;
504
505         long int ret_time = bm_get_log_time();
506
507         g_hash_table_iter_init(&iter, gl_hash);
508
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;
513
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);
518
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 ");
522                 else
523                         _DBG("Insert successful");
524
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 ");
528                 else
529                         _DBG("Insert successful");
530         }
531
532         EXIT;
533         return ret_val;
534 }
535
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)
538 {
539         ENTER;
540
541         BM_CHECK_INPUT_PARAM(handle);
542
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;
548
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);
551
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;
555         double onTime = 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;
568                         if (!bt_atm_node) {
569                                 _DBG("no data available");
570                                 continue;
571                         }
572                         _DBG("bt data available");
573
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);
578
579                                 RX_system += bt_atm_node->rx;
580                                 TX_system += bt_atm_node->tx;
581
582                                 bm_data_free(bt_atm_node->app_id);
583                                 bm_atm_st2_free(bt_atm_node);
584                                 continue;
585                         }
586
587                         RX_app += bt_atm_node->rx;
588                         TX_app += bt_atm_node->tx;
589                         onTime += bt_atm_node->time;
590                         prv_data = NULL;
591                         prv_app_id = NULL;
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));
601                         } else {
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));
605                         }
606                 }
607         }
608
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;
611
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");
615
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;
623
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 */
626         GHashTableIter iter;
627         gpointer key, value;
628         g_hash_table_iter_init(&iter, hash);
629
630         long int P_power_app_bt = 0, P_power_app_data = 0, P_power_app_conn = 0;
631         char *appid = NULL;
632         long int apprx = 0, apptx = 0;
633         double apptime = 0;
634         long int rx_t = 0, tx_t = 0;
635         long int ret_time = bm_get_log_time();
636
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;
641         }
642
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);
652                 if (onTime != 0)
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;
655
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);
662                 if (mode) {
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 ");
671                 else
672                         _DBG("Insert successful");
673         }
674
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;
681         }
682
683         res_usage->ResourceId = BM_RID_BLE;
684         res_usage->log_time = ret_time;
685         res_usage->usage = P_power_bt;
686
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 ");
690
691         bm_appid_usage_free(app_usage);
692         bm_resourceid_usage_free(res_usage);
693
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;
698         }
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);
703         }
704         gble_st->ble_pwi_val = P_power_bt;
705
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 ");
709         BM_FREE(gble_st);
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;
714         }
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 ");
721         BM_FREE(gbst_st);
722         /* Free the hash map */
723         g_hash_table_destroy(hash);
724
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) {
728                 history_item_s hi;
729                 bd_print_history_item_reset(&hi);
730
731                 hi.cmd_s = CM_USAGE;
732                 hi.usage_type = USAGE_BLUETOOTH;
733                 hi.usage = RX_system + TX_system + RX_app + TX_app;
734
735                 bd_store_history_item(&hi);
736         }
737
738         EXIT;
739         return ret_val;
740 }
741
742 int bm_wifi_calc_power_and_commit(bm_wifi_st *handle, bool mode)
743 {
744         ENTER;
745
746         BM_CHECK_INPUT_PARAM(handle);
747
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;
753
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);
756
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;
760         double onTime = 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");
780                                 continue;
781                         }
782                         _DBG("wifi data available");
783                         RX += wifi_atm_node->rx;
784                         TX += wifi_atm_node->tx;
785                         onTime += wifi_atm_node->time;
786                         prv_app_id = NULL;
787                         prv_data = NULL;
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));
797                         } else {
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));
801                         }
802                 }
803         }
804
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;
808
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");
813
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);
817
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;
829
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 */
832         GHashTableIter iter;
833         gpointer key, value;
834         g_hash_table_iter_init(&iter, hash);
835
836         long int P_power_app_wifi = 0, P_power_app_data = 0, P_power_app_conn = 0;
837         char *appid = NULL;
838         long int apprx = 0, apptx = 0;
839         double apptime = 0;
840         long int rx_t = 0, tx_t = 0;
841         long int ret_time = bm_get_log_time();
842
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;
847         }
848
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;
857                 apptime /= 1000;
858                 P_power_app_data = ((sP_power_wf_tx) * apptx) + ((sP_power_wf_rx) * apprx);
859
860                 if (onTime != 0)
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;
863
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);
870
871                 if (mode) {
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 ");
880                 else
881                         _DBG("Insert successful");
882         }
883
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;
890         }
891
892         res_usage->ResourceId = BM_RID_WIFI;
893         res_usage->log_time = ret_time;
894         res_usage->usage = P_power_wifi;
895
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 ");
899         else
900                 _DBG("Insert successful");
901
902         bm_appid_usage_free(app_usage);
903         bm_resourceid_usage_free(res_usage);
904
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;
909         }
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);
915         }
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 ");
919         else
920                 _DBG("Insert successful");
921         BM_FREE(gwfl_st);
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;
926         }
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 ");
933         else
934                 _DBG("Insert successful");
935         BM_FREE(gwst_st);
936
937         /* Free the Hash Map */
938         g_hash_table_destroy(hash);
939
940         /* Dump resource usage */
941         _DBG("Wi-Fi usage : %ld", RX + TX);
942         if (RX > 0 || TX > 0) {
943                 history_item_s hi;
944                 bd_print_history_item_reset(&hi);
945
946                 struct timeval tv;
947                 gettimeofday(&tv, NULL);
948
949                 hi.time_s = ((long long)tv.tv_sec * 1000) + ((long long)tv.tv_usec / 1000);
950
951                 hi.cmd_s = CM_USAGE;
952                 hi.usage_type = USAGE_WIFI;
953                 hi.usage = RX + TX;
954
955                 bd_store_history_item(&hi);
956         }
957
958         EXIT;
959         return ret_val;
960 }
961
962 int bm_cpu_calc_power_and_commit(bm_cpu_st *handle, bool mode)
963 {
964         ENTER;
965
966         BM_CHECK_INPUT_PARAM(handle);
967
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;
973
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);
976
977         long int uTime = 0, sTime = 0;
978         double onTime = 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;
987                         if (!cpu_atm_node) {
988                                 _DBG("no data available");
989                                 continue;
990                         }
991                         _DBG("cpu data available");
992                         onTime += cpu_atm_node->time;
993                         prv_app_id = NULL;
994                         prv_data = NULL;
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));
1001                         } else {
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));
1004                         }
1005                 }
1006         }
1007         /* Read standard Rated Values from Device Spec File/Power Profile File */
1008         int sP_power_cpu_user = 0, sP_power_cpu_system = 0;
1009
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");
1013
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;
1022
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);
1028
1029         long int P_power_app_cpu = 0, P_power_app_use = 0;
1030         double apptime = 0;
1031         char *appid = NULL;
1032         long int ret_time = bm_get_log_time();
1033
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;
1038         }
1039
1040         while (g_hash_table_iter_next(&iter, &key, &value)) {
1041                 P_power_app_cpu = 0; P_power_app_use = 0;
1042                 apptime = 0;
1043                 app_time_map_st1 *temp = (app_time_map_st1 *)value;
1044                 appid = temp->app_id;
1045                 apptime = (temp->time);
1046                 apptime /= 1000;
1047                 if (onTime != 0)
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;
1050
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);
1056
1057                 if (mode) {
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);
1062                 } else
1063                         ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_cpu, BM_PLUGIN_ID_CPU);
1064                 /* Error Check */
1065                 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1066                         _ERR("Insert of CPU App failed ");
1067                 else
1068                         _DBG("Insert successful");
1069         }
1070
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;
1076         }
1077
1078         res_usage->ResourceId = BM_RID_CPU;
1079         res_usage->log_time = ret_time;
1080         res_usage->usage = P_power_cpu;
1081
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 ");
1085
1086         bm_appid_usage_free(app_usage);
1087         bm_resourceid_usage_free(res_usage);
1088
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;
1093         }
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");
1099         else
1100                 _DBG("Insert GCPU successful");
1101         BM_FREE(gcpu_st);
1102         /* Free the Hash Map */
1103         g_hash_table_destroy(hash);
1104
1105         EXIT;
1106         return ret_val;
1107 }
1108
1109 int bm_display_calc_power_and_commit(bm_display_st *handle, bool mode)
1110 {
1111         ENTER;
1112
1113         BM_CHECK_INPUT_PARAM(handle);
1114
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;
1120
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);
1123
1124         long int edTime = 0, stTime = 0, sesTime = 0;
1125         long int highTime = 0, medTime = 0, lowTime = 0;
1126         double onTime = 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;
1139                         if (!dp_atm_node) {
1140                                 _DBG("no data available");
1141                                 continue;
1142                         }
1143                         _DBG("display data available");
1144                         onTime += dp_atm_node->time;
1145                         prv_data = NULL;
1146                         prv_app_id = NULL;
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));
1153                         } else {
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));
1156                         }
1157                 }
1158         }
1159
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;
1162
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");
1166
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);
1179
1180         long int P_power_app_disp = 0, P_power_app_on = 0;
1181         char *appid = NULL;
1182         double apptime = 0;
1183         long int ret_time = bm_get_log_time();
1184
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;
1189         }
1190
1191         while (g_hash_table_iter_next(&iter, &key, &value)) {
1192                 P_power_app_disp = 0; P_power_app_on = 0;
1193                 apptime = 0;
1194                 app_time_map_st1 *temp = (app_time_map_st1 *)value;
1195                 appid = temp->app_id;
1196                 apptime = temp->time;
1197                 apptime /= 1000;
1198                 if (onTime != 0)
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;
1201
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);
1207                 if (mode) {
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);
1212                 } else
1213                         ret_val = bm_appid_session_usage_map(g_strdup(app_usage->AppId), app_usage->rId_display, BM_PLUGIN_ID_DISPLAY);
1214                 /* Error Check */
1215                 if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1216                         _ERR("Insert of Display App failed ");
1217                 else
1218                         _DBG("Insert successful");
1219         }
1220
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;
1227         }
1228
1229         res_usage->ResourceId = BM_RID_DISP;
1230         res_usage->log_time = ret_time;
1231         res_usage->usage = P_power_disp;
1232
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 ");
1236         else
1237                 _DBG("Insert successful");
1238
1239         bm_appid_usage_free(app_usage);
1240         bm_resourceid_usage_free(res_usage);
1241
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;
1246         }
1247
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;
1251
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 ");
1255         else
1256                 _DBG("Insert GBR successful");
1257         BM_FREE(gbr_st);
1258         /* Free the Hash Map */
1259         g_hash_table_destroy(hash);
1260
1261         EXIT;
1262         return ret_val;
1263 }
1264
1265 int bm_device_network_calc_power_and_commit(bm_device_network_st *handle, bool mode)
1266 {
1267         ENTER;
1268
1269         BM_CHECK_INPUT_PARAM(handle);
1270
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;
1276
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);
1279
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;
1296                         if (!dn_atm_node) {
1297                                 _DBG("no data available");
1298                                 continue;
1299                         }
1300                         _DBG("device-network data available");
1301                         RX += dn_atm_node->rx;
1302                         TX += dn_atm_node->tx;
1303                         prv_data = NULL;
1304                         prv_app_id = NULL;
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));
1314                         } else {
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));
1318                         }
1319                 }
1320         }
1321
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;
1325
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);
1334
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;
1342
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);
1348
1349         long int P_power_app_dntw = 0, P_power_app_data = 0, P_power_app_conn = 0;
1350         char *appid = NULL;
1351         long int apprx = 0, apptx = 0;
1352         double apptime = 0;
1353         long int ret_time = bm_get_log_time();
1354
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;
1359         }
1360
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);
1369                 if (tl_total != 0)
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;
1372
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);
1378                 if (mode) {
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);
1383                 } else
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 ");
1387                 else
1388                         _DBG("Insert successful");
1389         }
1390
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;
1397         }
1398
1399         res_usage->ResourceId = BM_RID_DNTW;
1400         res_usage->log_time = ret_time;
1401         res_usage->usage = P_power_dntw;
1402
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 ");
1406         else
1407                 _DBG("Insert successful");
1408
1409         bm_appid_usage_free(app_usage);
1410         bm_resourceid_usage_free(res_usage);
1411
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;
1416         }
1417
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 ");
1424         else
1425                 _DBG("Insert successful");
1426         BM_FREE(gst_st);
1427
1428         /* Free the Hash Map */
1429         g_hash_table_destroy(hash);
1430
1431         EXIT;
1432         return ret_val;
1433 }
1434
1435 #ifdef DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN
1436 int bm_gps_calc_power_and_commit(bm_gps_st *handle)
1437 {
1438         ENTER;
1439
1440         BM_CHECK_INPUT_PARAM(handle);
1441
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;
1447
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);
1450
1451         long int edTime = 0;
1452         long int stTime = 0;
1453         int sesTime = 0;
1454         int onTime = 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");
1466                                 continue;
1467                         }
1468                         _DBG("gps data available");
1469                         onTime += gps_atm_node->time;
1470                         prv_app_id = NULL;
1471                         prv_data = NULL;
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));
1478                         } else {
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));
1481                         }
1482                 }
1483         }
1484
1485         /* Read standard Rated Values from Device Spec File/Power Profile File */
1486         int sP_power_gps_conn = 0;
1487
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");
1491
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));
1496
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);
1499
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);
1504
1505         int P_power_app_gps = 0, P_power_app_conn = 0;
1506         char *appid = NULL;
1507         int apptime = 0;
1508         long int ret_time = bm_get_log_time();
1509
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;
1514         }
1515
1516         while (g_hash_table_iter_next(&iter, &key, &value)) {
1517                 P_power_app_gps = 0; P_power_app_conn = 0;
1518                 apptime = 0;
1519                 app_time_map_st1 *temp = (app_time_map_st1 *)value;
1520                 appid = temp->app_id;
1521                 apptime = temp->time;
1522
1523                 if (onTime != 0)
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;
1526
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 ");
1535                 else
1536                         _DBG("Insert successful");
1537         }
1538
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;
1545         }
1546
1547         res_usage->ResourceId = BM_RID_GPS;
1548         res_usage->log_time = ret_time;
1549         res_usage->usage = P_power_gps;
1550
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 ");
1554         else
1555                 _DBG("Insert successful");
1556
1557         bm_appid_usage_free(app_usage);
1558         bm_resourceid_usage_free(res_usage);
1559
1560         /* Free the Hash Map */
1561         g_hash_table_destroy(hash);
1562
1563         EXIT;
1564         return ret_val;
1565 }
1566
1567 int bm_hrm_calc_power_and_commit(bm_hrm_sensor_st *handle)
1568 {
1569         ENTER;
1570
1571         BM_CHECK_INPUT_PARAM(handle);
1572
1573         GSList *hrm_data_iterator = NULL;
1574         GSList *hrm_atm_iterator = NULL;
1575
1576         void *prv_data = NULL;
1577         void *prv_app_id = NULL;
1578
1579         int ret_val = BATTERY_MONITOR_ERROR_NONE;
1580
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);
1583
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) {
1586
1587                 hrm_atm_iterator = ((bm_hrm_sensor_st *)(hrm_data_iterator->data))->atm_list;
1588
1589                 for ( ; hrm_atm_iterator; hrm_atm_iterator = hrm_atm_iterator->next) {
1590
1591                         app_time_map_st1 *hrm_atm_node = (app_time_map_st1 *)hrm_atm_iterator->data;
1592
1593                         if (!hrm_atm_node) {
1594                                 _DBG("no bt data available");
1595                                 continue;
1596                         }
1597
1598                         _DBG("hrm data available");
1599
1600                         prv_data = NULL;
1601                         prv_app_id = NULL;
1602
1603                         if (g_hash_table_lookup_extended(hash, hrm_atm_node->app_id, &prv_app_id, &prv_data) == true) {
1604
1605                                 _DBG("previous app_id = %s", (char *)prv_app_id);
1606
1607                                 app_time_map_st1 *hrm_atm_prv_node = (app_time_map_st1 *)prv_data;
1608
1609                                 hrm_atm_node->time += hrm_atm_prv_node->time;
1610
1611                                 _DBG("update - app_id(%s), time(%d)", hrm_atm_node->app_id, hrm_atm_node->time);
1612
1613                                 _INFO("updated - %d", g_hash_table_replace(hash, hrm_atm_node->app_id, hrm_atm_node));
1614
1615                         } else {
1616
1617                                 _DBG("insert - app_id(%s), time(%d)", hrm_atm_node->app_id, hrm_atm_node->time);
1618
1619                                 _INFO("inserted - %d", g_hash_table_insert(hash, hrm_atm_node->app_id, hrm_atm_node));
1620                         }
1621                 }
1622         }
1623
1624         int sP_power_hrm_conn = 0;
1625
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");
1630
1631         _DBG("hrm-sensor power params - gps[%d]", sP_power_hrm_conn);
1632
1633         /* apply power calculation on the hash-map data*/
1634
1635         /* commit to database the output after power calculation */
1636
1637         /* Free the Hash Map */
1638         g_hash_table_destroy(hash);
1639
1640         EXIT;
1641         return ret_val;
1642 }
1643 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
1644
1645 int bm_battery_calc_power_and_commit(void)
1646 {
1647         ENTER;
1648
1649         int ret_val = DEVICE_ERROR_NONE;
1650         int battery_percent = 0;
1651         bool is_charging = false;
1652
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");
1656
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");
1660
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");
1665
1666         switch (battery_level) {
1667         case DEVICE_BATTERY_LEVEL_EMPTY:
1668                 _DBG("battery level - EMPTY");
1669                 break;
1670         case DEVICE_BATTERY_LEVEL_CRITICAL:
1671                 _DBG("battery level - CRITICAL");
1672                 break;
1673         case DEVICE_BATTERY_LEVEL_LOW:
1674                 _DBG("battery level - LOW");
1675                 break;
1676         case DEVICE_BATTERY_LEVEL_HIGH:
1677                 _DBG("battery level - HIGH");
1678                 break;
1679         case DEVICE_BATTERY_LEVEL_FULL:
1680                 _DBG("battery level - FULL");
1681                 break;
1682         default:
1683                 _DBG("invalid battery-level");
1684                 break;
1685         }
1686
1687         _INFO("battery percent[%d] & charging status[%s]",
1688                 battery_percent, (is_charging == true) ? "TRUE" : "FALSE");
1689
1690         int sP_power_battery = 0;
1691
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");
1695
1696         _DBG("received battery power params - battery[%d]", sP_power_battery);
1697
1698         ret_val = BATTERY_MONITOR_ERROR_NONE;
1699
1700         EXIT;
1701         return ret_val;
1702 }
1703
1704 int bm_get_data_from_handles(void)
1705 {
1706         ENTER;
1707
1708         BM_CHECK_INPUT_PARAM(bm_data_handle);
1709         BM_CHECK_INPUT_PARAM(bm_req_flag_h);
1710
1711         int ret_val = BATTERY_MONITOR_ERROR_NONE;
1712         bool mode = false;
1713
1714         gl_hash = g_hash_table_new_full(g_str_hash, g_str_equal, bm_data_free, bm_atm_st1_free);
1715         if (!gl_hash)
1716                 _ERR("Global Hash creation failed");
1717
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");
1723         } else {
1724                 _DBG("set ble flag - true");
1725                 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_BLE, true);
1726         }
1727         _DBG("completed ble data request");
1728
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");
1734         } else {
1735                 _DBG("set wifi flag - true");
1736                 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_WIFI, true);
1737         }
1738         _DBG("completed wifi data request");
1739
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");
1745         } else {
1746                 _DBG("set cpu flag - true");
1747                 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_CPU, true);
1748         }
1749         _DBG("completed cpu data request");
1750
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");
1756         } else {
1757                 _DBG("set display flag - true");
1758                 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_DISPLAY, true);
1759         }
1760         _DBG("completed display data request");
1761
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");
1767         } else {
1768                 _DBG("set device-network flag - true");
1769                 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_DEVICE_NETWORK, true);
1770         }
1771         _DBG("completed device-network data request");
1772
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");
1779         } else {
1780                 _DBG("set gps-sensor flag - true");
1781                 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_GPS_SENSOR, true);
1782         }
1783         _DBG("completed gps data request");
1784
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");
1790         } else {
1791                 _DBG("set hrm-sensor flag - true");
1792                 bm_engine_set_req_flag_handle(BM_PLUGIN_ID_HRM_SENSOR, true);
1793         }
1794         _DBG("completed hrm data request");
1795 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
1796
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");
1802
1803         ret_val = bm_insert_appid_session_usage_to_db();
1804         BM_CHECK_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), "battery session logging failure");
1805
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);
1810         }
1811
1812         /* set free global hash values */
1813         if (gl_hash) {
1814                 g_hash_table_destroy(gl_hash);
1815                 gl_hash = NULL;
1816         }
1817
1818         EXIT;
1819         return BATTERY_MONITOR_ERROR_NONE;
1820 }
1821
1822 int bm_start_getting_feature_data(void)
1823 {
1824         ENTER;
1825
1826         int ret_val = BATTERY_MONITOR_ERROR_NONE;
1827         int id;
1828         bm_plugin_info_h *bm_plugin = NULL;
1829         bm_data_h handle = NULL;
1830
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;
1837                 }
1838         }
1839
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) {
1843 #else
1844         for (id = BM_PLUGIN_ID_BLE; id <= BM_PLUGIN_ID_DEVICE_NETWORK; ++id) {
1845 #endif
1846                 bm_get_feature_plugin_handle(&bm_plugin, id);
1847                 handle = NULL;
1848                 if (!bm_plugin || !bm_plugin->api) {
1849                         _WARN("Plugin with ID: %s is NULL", bm_get_plugin_name(id));
1850                         continue;
1851                 }
1852                 _INFO(" requesting data from plugin - %s", bm_get_plugin_name(id));
1853
1854                 switch (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");
1859                                 handle = NULL;
1860                         }
1861                         if (handle != NULL) {
1862                                 _DBG("received ble data handle");
1863                                 bm_data_handle->bm_ble_handle = (bm_bluetooth_st *)(handle);
1864                         }
1865                         break;
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");
1870                                 handle = NULL;
1871                         }
1872                         if (handle != NULL) {
1873                                 _DBG("received wifi data handle");
1874                                 bm_data_handle->bm_wifi_handle = (bm_wifi_st *)(handle);
1875                         }
1876                         break;
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");
1881                                 handle = NULL;
1882                         }
1883                         if (handle != NULL) {
1884                                 _DBG("received cpu data handle");
1885                                 bm_data_handle->bm_cpu_handle = (bm_cpu_st *)(handle);
1886                         }
1887                         break;
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");
1892                                 handle = NULL;
1893                         }
1894                         if (handle != NULL) {
1895                                 _DBG("received display data handle");
1896                                 bm_data_handle->bm_display_handle = (bm_display_st *)(handle);
1897                         }
1898                         break;
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");
1903                                 handle = NULL;
1904                         }
1905                         if (handle != NULL) {
1906                                 _DBG("received device network data handle");
1907                                 bm_data_handle->bm_dn_handle = (bm_device_network_st *)(handle);
1908                         }
1909                         break;
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");
1915                                 handle = NULL;
1916                         }
1917                         if (handle != NULL) {
1918                                 _DBG("received gps data handle");
1919                                 bm_data_handle->bm_gps_handle = (bm_gps_st *)(handle);
1920                         }
1921                         break;
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");
1926                                 handle = NULL;
1927                         }
1928                         if (handle != NULL) {
1929                                 _DBG("received hrm data handle");
1930                                 bm_data_handle->bm_hrm_handle = (bm_hrm_sensor_st *)(handle);
1931                         }
1932                         break;
1933 #endif /* DISABLE_FEATURE_DATA_FROM_GPS_HRM_PLUGIN */
1934                 default:
1935                         break;
1936                 }
1937         }
1938
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");
1943
1944         EXIT;
1945         return ret_val;
1946 }
1947
1948 int bm_clean_db_table_for_job_id(void)
1949 {
1950         ENTER;
1951
1952         int ret_val = BATTERY_MONITOR_ERROR_NONE;
1953
1954         ret_val = bm_server_delete_table_by_time_interval();
1955
1956         BM_RETURN_VAL((ret_val == BATTERY_MONITOR_ERROR_NONE), {},
1957                         BATTERY_MONITOR_ERROR_DB_FAILED, "error in deleting old db data");
1958
1959         EXIT;
1960         return ret_val;
1961 }
1962
1963 gboolean bm_request_feature_data(gpointer data)
1964 {
1965         ENTER;
1966
1967         int ret_val = BATTERY_MONITOR_ERROR_NONE;
1968
1969         ret_val = bm_start_getting_feature_data();
1970         if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1971                 _ERR("error requesting feature data");
1972
1973         EXIT;
1974         return true;
1975 }
1976
1977 gboolean bm_delete_data_from_db(gpointer data)
1978 {
1979         ENTER;
1980
1981         int ret_val = BATTERY_MONITOR_ERROR_NONE;
1982
1983         ret_val = bm_clean_db_table_for_job_id();
1984         if (ret_val != BATTERY_MONITOR_ERROR_NONE)
1985                 _ERR("error cleaning database");
1986
1987         EXIT;
1988         return true;
1989 }
1990
1991 int initialize_power_engine(void)
1992 {
1993         ENTER;
1994
1995         int ret_val = BATTERY_MONITOR_ERROR_NONE;
1996
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;
2002         }
2003
2004         bm_set_req_flag_handles(true);
2005
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]);
2010
2011         ret_val = bm_clean_db_table_for_job_id();
2012         if (ret_val != BATTERY_MONITOR_ERROR_NONE)
2013                 _ERR("error cleaning database");
2014
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
2019
2020         EXIT;
2021         return ret_val;
2022 }
2023
2024 void deinitialize_power_engine(void)
2025 {
2026         ENTER;
2027
2028         /* remove timeouts*/
2029         for (int i = 0; i < 2; i++) {
2030                 if (gtimeo_id[i] != 0)
2031                         g_source_remove(gtimeo_id[i]);
2032         }
2033
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);
2038         }
2039
2040         /* set flag handle free*/
2041         BM_FREE(bm_req_flag_h);
2042
2043         EXIT;
2044         return;
2045 }