4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Sudipto Bal <sudipto.bal@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <oal-event.h>
29 #include "bt-service-battery-monitor.h"
30 #include "bt-service-common.h"
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;
41 } bt_service_oal_event_data_t;
43 _bt_battery_data_t *current_session_data = NULL;
45 static void __bt_display_session_data()
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);
54 /*After reading data, the function resets it*/
55 int _bt_bm_read_data(_bt_battery_data_t *data)
60 BT_ERR("Received NULL pointer in argument, returning...");
61 return BLUETOOTH_ERROR_NO_DATA;
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;
69 data->session_scan_time = current_session_data->session_scan_time;
71 data->session_scan_time += (uint16_t) (time(NULL) - scan_start);
72 scan_start = time(NULL);
75 data->session_connected_time = current_session_data->session_connected_time;
77 data->session_connected_time += (uint16_t) (time(NULL) - connect_start);
78 connect_start = time(NULL);
80 return BLUETOOTH_ERROR_NONE;
83 void _bt_start_session_time()
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;
95 if (current_session_data == NULL)
96 BT_ERR("Session in progress but data structure is not initialized"); //error handling
98 BT_DBG("Bt session already in progress... Returning");
102 void _bt_stop_session_time()
104 if (is_session_started == FALSE) {
105 BT_DBG("BT session not in progress... Returning"); //error handling
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();
114 void _bt_start_scan_time()
116 if (current_session_data != NULL) {
118 BT_DBG("Starting scan time");
119 scan_start = time(NULL);
123 BT_ERR("Data structure uninitialized"); //error handling
127 void _bt_stop_scan_time()
129 if (scan_cnt == 0 || current_session_data == NULL)
130 BT_ERR("Error encountered, returning..."); //error handling
134 time_t temp = time(NULL);
135 current_session_data->session_scan_time += (uint16_t) (temp - scan_start);
140 void _bt_start_connect_time()
142 if (current_session_data != NULL) {
143 if (connect_cnt == 0) {
144 BT_DBG("Starting connect time");
145 connect_start = time(NULL);
150 BT_ERR("Data structure uninitialized"); //error handling
154 void _bt_stop_connect_time()
156 if(connect_cnt == 0 || current_session_data == NULL) {
157 BT_ERR("Error encountered, returning..."); //error handling
161 if(connect_cnt == 0) {
162 time_t temp = time(NULL);
163 current_session_data->session_connected_time += (uint16_t) (temp - connect_start);
168 void _bt_bm_event_handler(gpointer data)
170 bt_service_oal_event_data_t *oal_event = data;
171 int event_type = oal_event->type;
174 case OAL_EVENT_ADAPTER_ENABLED:
175 BT_DBG("Handling Adapter Enabled");
176 _bt_start_session_time();
178 case OAL_EVENT_ADAPTER_DISABLED:
179 BT_DBG("Handling Adapter Disabled");
180 _bt_stop_session_time();
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();
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();
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();
197 case OAL_EVENT_SOCKET_DISCONNECTED:
198 case OAL_EVENT_GATTC_DISCONNECTION_COMPLETED:
199 BT_DBG("Handling Connection Stop");
200 _bt_stop_connect_time();
203 BT_DBG("The event is not currently being handled");