8ca051df0151f4865e344710548bda2f95e8b5b5
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / audio / a2dp_src / bt-service-a2dp-src.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Anupam Roy <anupam.r@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 <sys/socket.h>
23 #include <sys/errno.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <vconf.h>
27
28 #include "oal-hardware.h"
29 #include "oal-device-mgr.h"
30 #include <oal-manager.h>
31 #include <oal-audio-src.h>
32
33 #include <bt-service-a2dp-src.h>
34 #include <bt-service-event.h>
35
36 int _bt_a2dp_connect_remote_sink(bluetooth_device_address_t *device_address)
37 {
38         oal_status_t status = OAL_STATUS_SUCCESS;
39         int result = BLUETOOTH_ERROR_NONE;
40         BT_INFO("+");
41
42         status = audio_connect((bt_address_t*)device_address);
43         if (status != OAL_STATUS_SUCCESS) {
44                 BT_ERR("Connection could not be established, err: [%d]", status);
45                 result = BLUETOOTH_ERROR_INTERNAL;
46         }
47         return result;
48 }
49
50 int _bt_a2dp_disconnect_remote_sink(bluetooth_device_address_t *device_address)
51 {
52         oal_status_t status = OAL_STATUS_SUCCESS;
53         int result = BLUETOOTH_ERROR_NONE;
54         BT_INFO("+");
55
56         status = audio_disconnect((bt_address_t*)device_address);
57         if (status != OAL_STATUS_SUCCESS) {
58                 BT_ERR("DisConnection err: [%d]", status);
59                 result = BLUETOOTH_ERROR_INTERNAL;
60         }
61         return result;
62 }
63
64 static void __bt_handle_av_connected_state(bluetooth_device_address_t *address)
65 {
66         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
67         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
68         gboolean connected;
69         bt_headset_wait_t *wait_device = NULL;
70         bluetooth_device_address_t device_address;
71         bluetooth_device_address_t wait_device_address;
72         GVariant *param;
73
74         GArray *out_param;
75         invocation_info_t *req_info;
76         bt_pending_audio_conn_t *data = NULL;
77
78         int result = BLUETOOTH_ERROR_NONE;
79         ret_if(NULL == address);
80         BT_INFO("+");
81
82         _bt_convert_addr_type_to_string(addr, address->addr);
83         BT_INFO("Address of connected device [%s]", addr);
84
85         /* Set VCONF Key for A2DP Connected status */
86         _bt_set_device_values(TRUE, VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
87
88         /* Send A2DP(SRC Role) connected event to Application */
89         param = g_variant_new("(is)", result, addr);
90         _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AV_CONNECTED, param);
91
92         connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP, connected_address);
93         if (connected) {
94                 BT_INFO("connected_address: %s", connected_address);
95                 if (g_strcmp0(connected_address, addr) != 0) {
96                         _bt_convert_addr_string_to_type(
97                                         device_address.addr,
98                                         connected_address);
99                         _bt_audio_disconnect(BT_AUDIO_A2DP, &device_address);
100                 }
101         }
102
103         /* Add data from the connected list */
104         _bt_add_headset_to_list(BT_AUDIO_A2DP, BT_STATE_CONNECTED, addr);
105
106         /* Delete waiting device data if present */
107         wait_device = _bt_get_audio_wait_data();
108         if (wait_device != NULL &&
109                         (g_strcmp0(wait_device->address, addr) == 0))
110                 _bt_rel_wait_data();
111         else {
112                 if (wait_device != NULL) {
113                         BT_INFO("A2DP Profile connected for device [%s] but another device [%s]is waiting to be connnected type [%d]",
114                                         addr, wait_device->address, wait_device->type);
115                         BT_INFO("Trigger connect for the waiting device [%s], audio type [%d]",
116                                         wait_device->address, wait_device->type);
117                         _bt_convert_addr_string_to_type(wait_device_address.addr,
118                                         wait_device->address);
119                         _bt_audio_connect(wait_device->type, &wait_device_address);
120                         /* Now free the wait list */
121                         //_bt_rel_wait_data();
122                 }
123         }
124
125         /* DBUS Context return */
126         req_info = _bt_get_request_info_data(BT_AV_CONNECT, addr);
127         if (NULL == req_info) {
128                 BT_INFO("AV Connect request not found or possibly already replied");
129                 req_info = _bt_get_request_info_data(BT_AUDIO_CONNECT, addr);
130
131                 if (req_info == NULL) {
132                         BT_INFO("AV Connect request & Audio All connect not found or possibly already replied");
133                         return;
134                 } else {
135                         BT_INFO("Audio All connect request found for address [%s]", addr);
136                         /* TODO Delete the search info if present */
137                         data = _bt_get_pending_audio_conn_info(addr);
138                         if (data) {
139                                 BT_INFO("Audio Connect All request present and pending connect is also present, delete info");
140                                 _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(data, BLUETOOTH_ERROR_NONE);
141                                 return;
142                         } else {
143                                 BT_ERR("Abnormal: Audio Connect All request present but pending connect absent");
144                         }
145                 }
146         } else {
147                 BT_INFO("AV Connect request found for [%s]", addr);
148         }
149
150         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
151         g_array_append_vals(out_param, addr, BT_ADDRESS_STRING_SIZE);
152         _bt_service_method_return(req_info->context,
153                         out_param, result);
154         g_array_free(out_param, TRUE);
155         g_free(req_info->user_data);
156         _bt_free_info_from_invocation_list(req_info);
157         BT_INFO("-");
158 }
159
160 static void __bt_handle_av_disconnected_state(bluetooth_device_address_t *address)
161 {
162         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
163         int result = BLUETOOTH_ERROR_NONE;
164         GArray *out_param;
165         bt_headset_wait_t *wait_device;
166         bluetooth_device_address_t device_address;
167         bt_pending_audio_conn_t *data = NULL;
168         GVariant *param;
169         invocation_info_t *req_info = NULL;
170         ret_if(NULL == address);
171         BT_INFO("+");
172
173         _bt_convert_addr_type_to_string(addr, address->addr);
174
175         /* Set VCONF status for A2DP Disconnection */
176         _bt_set_device_values(FALSE, VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
177
178         /* Send A2DP(SRC Role) disconnected event to Application */
179         param = g_variant_new("(is)", result, addr);
180         _bt_send_event(BT_HEADSET_EVENT, BLUETOOTH_EVENT_AV_DISCONNECTED, param);
181
182         /* Remove data from the connected list */
183         _bt_remove_headset_from_list(BT_AUDIO_A2DP, addr);
184
185         req_info = _bt_get_request_info_data(BT_AV_DISCONNECT, addr);
186         BT_INFO("Address  of disconnected device[%s]", addr);
187
188         if (NULL == req_info) {
189                 BT_INFO("AV DisConnect request not found or possibly already replied");
190                 req_info = _bt_get_request_info_data(BT_AV_CONNECT, addr);
191                 if (NULL == req_info) {
192                         /* This means, AV Disconnect request has successfully passed, and real disconnect is completed */
193                         BT_INFO("Neither AV Connect or AV Disconnect request is found..means, AV Profile is disconnected");
194
195                         req_info = _bt_get_request_info_data(BT_AUDIO_CONNECT, addr);
196                         if (req_info == NULL) {
197                                 BT_INFO("Audio Connect All request is also Not found..");
198
199                                 req_info = _bt_get_request_info_data(BT_AUDIO_DISCONNECT, addr);
200                                 if (req_info == NULL) {
201                                         BT_INFO("Audio DisConnect All request is also Not found..");
202                                 } else {
203                                         BT_INFO("Audio DisConnect All request is found..");
204                                         /* Check if HFP profile is also connected, if yes, disconnect it too */
205                                         _bt_audio_check_pending_disconnection(addr, BT_AUDIO_HSP);
206                                 }
207                         } else {
208                                 BT_INFO("Audio Connect All request is found..");
209                                 result = BLUETOOTH_ERROR_INTERNAL;
210                                 data = _bt_get_pending_audio_conn_info(addr);
211                                 if (data) {
212                                         BT_INFO("Pending connect is found, delete it..");
213                                         _bt_cleanup_audio_pending_conn_info_and_reply_pending_req(data, result);
214                                         req_info = NULL;
215                                 } else {
216                                         BT_INFO("Pending connect is Not found..");
217                                 }
218
219                                 /* Delete waiting device data if present for this device */
220                                 wait_device = _bt_get_audio_wait_data();
221                                 if (wait_device && strncasecmp(wait_device->address, addr, BT_ADDRESS_STRING_SIZE) == 0) {
222                                         _bt_rel_wait_data();
223                                         goto dbus_return;
224                                 }
225                         }
226                         goto check_wait_device;
227                 } else {
228                         BT_ERR("AV Connect request has failed.."); /* DBUS return needed */
229                         result = BLUETOOTH_ERROR_INTERNAL;
230                         /* Delete waiting device data if present for this device */
231                         wait_device = _bt_get_audio_wait_data();
232                         if (wait_device && strncasecmp(wait_device->address, addr, BT_ADDRESS_STRING_SIZE) == 0) {
233                                 _bt_rel_wait_data();
234                                 goto dbus_return;
235                         }
236
237                         goto check_wait_device;
238                 }
239         } else {
240                 BT_ERR("AV Disconnect request found for [%s], means disconnect request is successful", addr); /* DBUS return needed */
241                 goto check_wait_device;
242         }
243 check_wait_device:
244         wait_device = _bt_get_audio_wait_data();
245
246         if (wait_device == NULL)
247                 goto dbus_return;
248
249         BT_INFO("type: %d, ag_flag: %d, disconnection_type: %d", wait_device->type,
250                         wait_device->ag_flag, wait_device->disconnection_type);
251         if (((wait_device->type == BT_AUDIO_ALL) &&
252                                 (wait_device->ag_flag == TRUE)) ||
253                         (wait_device->type == BT_AUDIO_A2DP) ||
254                         (wait_device->disconnection_type & BT_AUDIO_A2DP)) {
255                 if (g_strcmp0(wait_device->address, addr) != 0) {
256                         BT_INFO("Trigger connect for the waiting device [%s], audio type [%d]",
257                                         wait_device->address, wait_device->type);
258                         _bt_convert_addr_string_to_type(device_address.addr,
259                                         wait_device->address);
260                         _bt_audio_connect(wait_device->type, &device_address);
261                         /* Now free the wait list */
262                         //_bt_rel_wait_data();
263                 }
264         }
265
266 dbus_return:
267         if (req_info) {
268                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
269                 g_array_append_vals(out_param, addr, BT_ADDRESS_STRING_SIZE);
270                 _bt_service_method_return(req_info->context,
271                                 out_param, result);
272                 g_array_free(out_param, TRUE);
273                 g_free(req_info->user_data);
274                 _bt_free_info_from_invocation_list(req_info);
275         }
276 }
277
278 static void __bt_handle_av_audio_stopped(bluetooth_device_address_t *address)
279 {
280         BT_INFO("Currently not handled..");
281 }
282
283 static void __bt_handle_av_audio_started(bluetooth_device_address_t *address)
284 {
285         BT_INFO("Currently not handled..");
286 }
287
288 void _bt_a2dp_source_event_handler(int oal_event, gpointer event_data)
289 {
290         BT_INFO("+");
291         bluetooth_device_address_t * bd_addr = (bluetooth_device_address_t*)event_data;
292
293         switch (oal_event) {
294         case OAL_EVENT_AUDIO_CONNECTING:
295                 BT_INFO("A2DP Profile connecting..Ignore this event, wait for connected event");
296                 break;
297         case OAL_EVENT_AUDIO_DISCONNECTING:
298                 BT_INFO("A2DP Profile Disconnecting..Ignore this event, wait for Disconnected event");
299                 break;
300         case OAL_EVENT_AUDIO_CONNECTED:
301                 BT_INFO("A2DP Audio Profile connected..");
302                 __bt_handle_av_connected_state(bd_addr);
303                 BT_PERMANENT_LOG("Connected A2DP src");
304                 break;
305         case OAL_EVENT_AUDIO_DISCONNECTED:
306                 BT_INFO("A2DP Audio Profile dissconnected..");
307                 __bt_handle_av_disconnected_state(bd_addr);
308                 BT_PERMANENT_LOG("Disconnected A2DP src");
309                 break;
310         case OAL_EVENT_AUDIO_STOPPED:
311                 BT_INFO("A2DP Audio Stream stopped..");
312                 __bt_handle_av_audio_stopped(bd_addr);
313                 break;
314         case OAL_EVENT_AUDIO_STARTED:
315                 BT_INFO("A2DP Audio Stream started..");
316                 __bt_handle_av_audio_started(bd_addr);
317                 break;
318         default:
319                 BT_INFO("Invalid Event = %d", oal_event);
320                 break;
321         }
322         BT_INFO("-");
323 }
324
325 void _bt_a2dp_src_handle_incoming_authorization(char *address, int service_id)
326 {
327         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
328         gboolean connected;
329         bluetooth_device_address_t device_address;
330         oal_status_t res = OAL_STATUS_SUCCESS;
331         BT_INFO("+");
332
333         _bt_convert_addr_string_to_type(device_address.addr, address);
334
335         switch (service_id) {
336         case A2DP_SERVICE_ID:
337                 connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP, connected_address);
338                 if (connected) {
339                         BT_INFO("A2DP is already connected..check which device..");
340                         if (g_strcmp0(connected_address, address) != 0) {
341                                 BT_INFO("Different device is connected[%s] reject current [%s] device authorization request",
342                                                 connected_address, address);
343                                 res = device_reply_auth_request((bt_address_t*)&device_address, service_id, FALSE, FALSE);
344                                 if (res != OAL_STATUS_SUCCESS)
345                                         BT_ERR("authorize_response: %d", res);
346                                 return;
347                         }
348                 } else {
349                         res = device_reply_auth_request((bt_address_t*)&device_address, service_id, TRUE, FALSE);
350                         if (res != OAL_STATUS_SUCCESS)
351                                 BT_ERR("authorize_response: %d", res);
352                 }
353                 break;
354         default:
355                 BT_ERR("Invalid Audio Service ID..");
356         }
357
358         BT_INFO("-");
359 }