Provide RFCOMM data to battery monitor frwk
[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 <gio/gio.h>
24 #include <string.h>
25 #include <dlog.h>
26 #include <time.h>
27
28
29 #include <oal-event.h>
30
31 #include "bt-service-battery-monitor.h"
32 #include "bt-service-common.h"
33
34 static time_t scan_start = 0;
35 static time_t connect_start = 0;
36 static int scan_cnt = 0;
37 static int connect_cnt = 0;
38 static gboolean is_session_started = FALSE;
39
40 typedef struct {
41         int type;
42         void *data;
43 } bt_service_oal_event_data_t;
44
45 _bt_battery_data_t *current_session_data = NULL;
46
47 static void __bt_display_session_data()
48 {
49         BT_DBG("Displaying session data...");
50         BT_DBG("session_start_time = %ld", current_session_data->session_start_time);
51         BT_DBG("session_end_time = %ld", current_session_data->session_end_time);
52         BT_DBG("session_scan_time = %d", current_session_data->session_scan_time);
53         BT_DBG("session_connected_time = %d", current_session_data->session_connected_time);
54 }
55
56 /*After reading data, the function resets it*/
57 int _bt_bm_read_data(_bt_battery_data_t *data)
58 {
59         BT_DBG("");
60
61         if (data == NULL) {
62                 BT_ERR("Received NULL pointer in argument, returning...");
63                 return BLUETOOTH_ERROR_NO_DATA;
64         }
65
66         data->session_start_time = current_session_data->session_start_time;
67         data->session_end_time = time(NULL);
68         current_session_data->session_start_time = time(NULL);
69         current_session_data->session_end_time = 0;
70
71         data->session_scan_time = current_session_data->session_scan_time;
72         if (scan_cnt) {
73                 data->session_scan_time += (uint16_t) (time(NULL) - scan_start);
74                 scan_start = time(NULL);
75         }
76
77         data->session_connected_time = current_session_data->session_connected_time;
78         if (connect_cnt) {
79                 data->session_connected_time += (uint16_t) (time(NULL) - connect_start);
80                 connect_start = time(NULL);
81         }
82
83         data->atm_list = current_session_data->atm_list;
84         if (data->atm_list == NULL) {
85                 BT_DBG("No data transaction in this session");
86                 return BLUETOOTH_ERROR_NONE;
87         }
88
89         BT_DBG("App-wise data transaction details");
90         for (GSList *l = data->atm_list; l != NULL; l = g_slist_next(l)) {
91                 _bt_battery_app_data_t *t = (_bt_battery_app_data_t *)(l->data);
92                 BT_DBG("%ld %ld %d %d", (long int)(t->uid), (long int)(t->pid), t->rx_bytes, t->tx_bytes);
93         }
94
95         current_session_data->atm_list = NULL;
96         return BLUETOOTH_ERROR_NONE;
97 }
98
99 static GSList* is_app_present(GSList *start, uid_t uid, pid_t pid)
100 {
101         GSList *l = NULL;
102         _bt_battery_app_data_t *t;
103         BT_INFO("Checking Memory location %p", start);
104         for (l=start; l != NULL; l = g_slist_next(l)) {
105                 t = (_bt_battery_app_data_t *)(l->data);
106                 if (t->uid == uid && t->pid == pid) {
107                         BT_INFO("App details already exist");
108                         return l;
109                 }
110         }
111         return NULL;
112 }
113
114 void _bt_bm_add_transaction_details(uid_t uid, pid_t pid, int size, data_transaction_type_e type)
115 {
116         if (current_session_data == NULL) {
117                 BT_ERR("Session in progress but data structure is not initialized"); //error handling
118                 return;
119         }
120         GSList *t = is_app_present(current_session_data->atm_list, uid, pid);
121         _bt_battery_app_data_t *app_data = NULL;
122
123         if (t == NULL) {
124                 BT_INFO("Match not found, adding new node...");
125                 app_data = g_malloc0(sizeof(_bt_battery_app_data_t));
126                 app_data->uid = uid;
127                 app_data->pid = pid;
128                 if (type == RX_DATA)
129                         app_data->rx_bytes = size;
130                 else
131                         app_data->tx_bytes = size;
132                 current_session_data->atm_list = g_slist_append(current_session_data->atm_list, app_data);
133         }
134         else {
135                 BT_INFO("Match found, updating existing node...");
136                 app_data = (_bt_battery_app_data_t *)(t->data);
137                 if (type == RX_DATA)
138                         app_data->rx_bytes += size;
139                 else
140                         app_data->tx_bytes += size;
141         }
142 }
143
144 void _bt_start_session_time()
145 {
146         if (is_session_started == FALSE) {
147                 BT_DBG("Bt session starting...");
148                 is_session_started = TRUE;
149                 current_session_data = g_malloc0(sizeof(_bt_battery_data_t));
150                 current_session_data->session_start_time = time(NULL);
151                 current_session_data->session_end_time = 0;
152                 current_session_data->session_connected_time = 0;
153                 current_session_data->session_scan_time = 0;
154                 current_session_data->atm_list = NULL;
155         } else {
156                 if (current_session_data == NULL)
157                         BT_ERR("Session in progress but data structure is not initialized"); //error handling
158                 else
159                         BT_DBG("Bt session already in progress... Returning");
160         }
161 }
162
163 void _bt_stop_session_time()
164 {
165         if (is_session_started == FALSE) {
166                 BT_DBG("BT session not in progress... Returning"); //error handling
167                 return;
168         }
169         BT_DBG("Bt session ending...");
170         is_session_started = FALSE;
171         current_session_data->session_end_time = time(NULL);
172         __bt_display_session_data();
173 }
174
175 void _bt_start_scan_time()
176 {
177         if (current_session_data != NULL) {
178                 if (scan_cnt == 0) {
179                         BT_DBG("Starting scan time");
180                         scan_start = time(NULL);
181                 }
182                 scan_cnt++;
183         } else {
184                 BT_ERR("Data structure uninitialized"); //error handling
185         }
186 }
187
188 void _bt_stop_scan_time()
189 {
190         if (scan_cnt == 0 || current_session_data == NULL)
191                 BT_ERR("Error encountered, returning..."); //error handling
192         else {
193                 scan_cnt--;
194                 if(scan_cnt == 0) {
195                         time_t temp = time(NULL);
196                         current_session_data->session_scan_time += (uint16_t) (temp - scan_start);
197                 }
198         }
199 }
200
201 void _bt_start_connect_time()
202 {
203         if (current_session_data != NULL) {
204                 if (connect_cnt == 0) {
205                         BT_DBG("Starting connect time");
206                         connect_start = time(NULL);
207                 }
208                 connect_cnt++;
209         }
210         else {
211                 BT_ERR("Data structure uninitialized"); //error handling
212         }
213 }
214
215 void _bt_stop_connect_time()
216 {
217         if(connect_cnt == 0 || current_session_data == NULL) {
218                 BT_ERR("Error encountered, returning..."); //error handling
219         }
220         else {
221                 connect_cnt--;
222                 if(connect_cnt == 0) {
223                         time_t temp = time(NULL);
224                         current_session_data->session_connected_time += (uint16_t) (temp - connect_start);
225                 }
226         }
227 }
228
229 void _bt_bm_event_handler(gpointer data)
230 {
231         bt_service_oal_event_data_t *oal_event = data;
232         int event_type = oal_event->type;
233
234         switch(event_type) {
235         case OAL_EVENT_ADAPTER_ENABLED:
236                 BT_DBG("Handling Adapter Enabled");
237                 _bt_start_session_time();
238                 break;
239         case OAL_EVENT_ADAPTER_DISABLED:
240                 BT_DBG("Handling Adapter Disabled");
241                 _bt_stop_session_time();
242                 break;
243         case OAL_EVENT_ADAPTER_INQUIRY_STARTED:
244         case OAL_EVENT_BLE_DISCOVERY_STARTED:
245                 BT_DBG("Handling Adapter Discovery Start");
246                 _bt_start_scan_time();
247                 break;
248         case OAL_EVENT_ADAPTER_INQUIRY_FINISHED:
249         case OAL_EVENT_BLE_DISCOVERY_STOPPED:
250                 BT_DBG("Handling Adapter Discovery Stop");
251                 _bt_stop_scan_time();
252                 break;
253         case OAL_EVENT_SOCKET_OUTGOING_CONNECTED:
254         case OAL_EVENT_GATTC_CONNECTION_COMPLETED:
255                 BT_DBG("Handling Connection Start");
256                 _bt_start_connect_time();
257                 break;
258         case OAL_EVENT_SOCKET_DISCONNECTED:
259         case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED:
260                 BT_DBG("Handling Connection Stop");
261                 _bt_stop_connect_time();
262                 break;
263         default:
264                 BT_DBG("The event is not currently being handled");
265         }
266 }