Providing bluetooth usage data for battery monitor framework
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / bt-service-battery-monitor.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Sudipto Bal <sudipto.bal@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <glib.h>
23 #include <string.h>
24 #include <dlog.h>
25 #include <time.h>
26
27 #include <oal-event.h>
28
29 #include "bt-service-battery-monitor.h"
30 #include "bt-service-common.h"
31
32 static time_t scan_start = 0;
33 static time_t connect_start = 0;
34 static int scan_cnt = 0;
35 static int connect_cnt = 0;
36 static gboolean is_session_started = FALSE;
37
38 typedef struct {
39         int type;
40         void *data;
41 } bt_service_oal_event_data_t;
42
43 _bt_battery_data_t *current_session_data = NULL;
44
45 static void __bt_display_session_data()
46 {
47         BT_DBG("Displaying session data...");
48         BT_DBG("session_start_time = %ld", current_session_data->session_start_time);
49         BT_DBG("session_end_time = %ld", current_session_data->session_end_time);
50         BT_DBG("session_scan_time = %d", current_session_data->session_scan_time);
51         BT_DBG("session_connected_time = %d", current_session_data->session_connected_time);
52 }
53
54 /*After reading data, the function resets it*/
55 int _bt_bm_read_data(_bt_battery_data_t *data)
56 {
57         BT_DBG("");
58
59         if (data == NULL) {
60                 BT_ERR("Received NULL pointer in argument, returning...");
61                 return BLUETOOTH_ERROR_NO_DATA;
62         }
63
64         data->session_start_time = current_session_data->session_start_time;
65         data->session_end_time = time(NULL);
66         current_session_data->session_start_time = time(NULL);
67         current_session_data->session_end_time = 0;
68
69         data->session_scan_time = current_session_data->session_scan_time;
70         if (scan_cnt) {
71                 data->session_scan_time += (uint16_t) (time(NULL) - scan_start);
72                 scan_start = time(NULL);
73         }
74
75         data->session_connected_time = current_session_data->session_connected_time;
76         if (connect_cnt) {
77                 data->session_connected_time += (uint16_t) (time(NULL) - connect_start);
78                 connect_start = time(NULL);
79         }
80         return BLUETOOTH_ERROR_NONE;
81 }
82
83 void _bt_start_session_time()
84 {
85         if (is_session_started == FALSE) {
86                 BT_DBG("Bt session starting...");
87                 is_session_started = TRUE;
88                 current_session_data = g_malloc0(sizeof(_bt_battery_data_t));
89                 current_session_data->session_start_time = time(NULL);
90                 current_session_data->session_end_time = 0;
91                 current_session_data->session_connected_time = 0;
92                 current_session_data->session_scan_time = 0;
93                 current_session_data->atm_list = NULL;
94         } else {
95                 if (current_session_data == NULL)
96                         BT_ERR("Session in progress but data structure is not initialized"); //error handling
97                 else
98                         BT_DBG("Bt session already in progress... Returning");
99         }
100 }
101
102 void _bt_stop_session_time()
103 {
104         if (is_session_started == FALSE) {
105                 BT_DBG("BT session not in progress... Returning"); //error handling
106                 return;
107         }
108         BT_DBG("Bt session ending...");
109         is_session_started = FALSE;
110         current_session_data->session_end_time = time(NULL);
111         __bt_display_session_data();
112 }
113
114 void _bt_start_scan_time()
115 {
116         if (current_session_data != NULL) {
117                 if (scan_cnt == 0) {
118                         BT_DBG("Starting scan time");
119                         scan_start = time(NULL);
120                 }
121                 scan_cnt++;
122         } else {
123                 BT_ERR("Data structure uninitialized"); //error handling
124         }
125 }
126
127 void _bt_stop_scan_time()
128 {
129         if (scan_cnt == 0 || current_session_data == NULL)
130                 BT_ERR("Error encountered, returning..."); //error handling
131         else {
132                 scan_cnt--;
133                 if(scan_cnt == 0) {
134                         time_t temp = time(NULL);
135                         current_session_data->session_scan_time += (uint16_t) (temp - scan_start);
136                 }
137         }
138 }
139
140 void _bt_start_connect_time()
141 {
142         if (current_session_data != NULL) {
143                 if (connect_cnt == 0) {
144                         BT_DBG("Starting connect time");
145                         connect_start = time(NULL);
146                 }
147                 connect_cnt++;
148         }
149         else {
150                 BT_ERR("Data structure uninitialized"); //error handling
151         }
152 }
153
154 void _bt_stop_connect_time()
155 {
156         if(connect_cnt == 0 || current_session_data == NULL) {
157                 BT_ERR("Error encountered, returning..."); //error handling
158         }
159         else {
160                 connect_cnt--;
161                 if(connect_cnt == 0) {
162                         time_t temp = time(NULL);
163                         current_session_data->session_connected_time += (uint16_t) (temp - connect_start);
164                 }
165         }
166 }
167
168 void _bt_bm_event_handler(gpointer data)
169 {
170         bt_service_oal_event_data_t *oal_event = data;
171         int event_type = oal_event->type;
172
173         switch(event_type) {
174         case OAL_EVENT_ADAPTER_ENABLED:
175                 BT_DBG("Handling Adapter Enabled");
176                 _bt_start_session_time();
177                 break;
178         case OAL_EVENT_ADAPTER_DISABLED:
179                 BT_DBG("Handling Adapter Disabled");
180                 _bt_stop_session_time();
181                 break;
182         case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
183         case OAL_EVENT_BLE_DISCOVERY_STARTED:
184                 BT_DBG("Handling Adapter Discovery Start");
185                 _bt_start_scan_time();
186                 break;
187         case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
188         case OAL_EVENT_BLE_DISCOVERY_STOPPED:
189                 BT_DBG("Handling Adapter Discovery Stop");
190                 _bt_stop_scan_time();
191                 break;
192         case OAL_EVENT_SOCKET_OUTGOING_CONNECTED:
193         case OAL_EVENT_GATTC_CONNECTION_COMPLETED:
194                 BT_DBG("Handling Connection Start");
195                 _bt_start_connect_time();
196                 break;
197         case OAL_EVENT_SOCKET_DISCONNECTED:
198         case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED:
199                 BT_DBG("Handling Connection Stop");
200                 _bt_stop_connect_time();
201                 break;
202         default:
203                 BT_DBG("The event is not currently being handled");
204         }
205 }