646669f78ab6caec321046372430d03d3059be2a
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / audio / hf / bt-service-hf-client.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <sys/errno.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <vconf.h>
24
25 #include "oal-hardware.h"
26 #include "oal-manager.h"
27 #include "oal-audio-src.h"
28 #include "oal-device-mgr.h"
29 #include "oal-hf-client.h"
30 #include "bt-service-common.h"
31
32 #include <bt-service-hf-client.h>
33 #include <bt-service-event.h>
34
35
36
37
38 static void __bt_reply_hf_client_disconnection_pending_request(bt_address_t *address)
39 {
40         BT_DBG("+");
41         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
42         bluetooth_device_address_t device_address;
43         GArray *out_param;
44         invocation_info_t *req_info;
45         int result = BLUETOOTH_ERROR_NONE;
46
47         ret_if(NULL == address);
48
49         memcpy(device_address.addr, address->addr, BLUETOOTH_ADDRESS_LENGTH);
50         _bt_convert_addr_type_to_string(addr, address->addr);
51
52         BT_INFO("HF Client Disconnection remote device [%s]", addr);
53
54         /* Find Async request information*/
55         req_info = _bt_get_request_info_data(BT_HF_DISCONNECT, addr);
56         if (NULL == req_info) {
57                 BT_INFO("HF DisConnect request not found or possibly already replied..");
58
59                 /* if HF Connect call fails on Bluez DBUS, Async callback from bluez will be received
60                    __bt_hf_client_connect_cb) which will internally trigger HF DISCONNECTED event */
61                 req_info = _bt_get_request_info_data(BT_HF_CONNECT, addr);
62                 if (NULL == req_info) {
63                         BT_INFO("HF Connect request not found..");
64                         return;
65                 } else {
66                         BT_INFO("HF Connect request found..reply this request");
67                 }
68         } else {
69                 BT_INFO("HF DisConnect request found..reply this request");
70         }
71
72         /* In any of the above cases, reply DBUS context */
73         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
74         g_array_append_vals(out_param, &device_address, sizeof(bluetooth_device_address_t));
75         _bt_service_method_return(req_info->context, out_param, result);
76         g_array_free(out_param, TRUE);
77         g_free(req_info->user_data);
78         _bt_free_info_from_invocation_list(req_info);
79
80         BT_DBG("-");
81 }
82
83 static void __bt_reply_hf_client_connection_pending_request(bt_address_t *address)
84 {
85         BT_DBG("+");
86         char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
87         bluetooth_device_address_t device_address;
88         GArray *out_param;
89         invocation_info_t *req_info;
90         int result = BLUETOOTH_ERROR_NONE;
91
92         ret_if(NULL == address);
93
94         memcpy(device_address.addr, address->addr, BLUETOOTH_ADDRESS_LENGTH);
95         _bt_convert_addr_type_to_string(addr, address->addr);
96
97         BT_INFO("HF Client Connection remote device [%s]", addr);
98
99         /* Find Async request information*/
100         req_info = _bt_get_request_info_data(BT_HF_CONNECT, addr);
101         if (NULL == req_info) {
102                 BT_INFO("HF Connect request not found or possibly already replied..");
103                 return;
104
105         } else {
106                 BT_INFO("HF Connect request found..");
107         }
108
109         /* In any of the above cases, reply DBUS context */
110         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
111         g_array_append_vals(out_param, &device_address, sizeof(bluetooth_device_address_t));
112         _bt_service_method_return(req_info->context, out_param, result);
113         g_array_free(out_param, TRUE);
114         g_free(req_info->user_data);
115         _bt_free_info_from_invocation_list(req_info);
116
117         BT_DBG("-");
118 }
119
120
121 /* This event handler process events for HF Client role */
122 void _bt_hf_client_event_handler(int oal_event, gpointer event_data)
123 {
124         BT_INFO("+");
125         bt_address_t *bt_addr = event_data;
126
127         switch (oal_event) {
128         case OAL_EVENT_HF_CLIENT_DISCONNECTED:
129                 /* Reply to async request for HF Client connect or disconnect request, if any */
130                 BT_INFO("HF client Profile disconnected, reply pending DBUS request and check waiting device");
131                 __bt_reply_hf_client_disconnection_pending_request(bt_addr);
132                 BT_PERMANENT_LOG("Disconnected HF");
133                 break;
134         case OAL_EVENT_HF_CLIENT_CONNECTED:
135                 BT_INFO("HF Client Profile connected, Event & DBUS context will be handled at finalizing SCO connect..");
136                 __bt_reply_hf_client_connection_pending_request(bt_addr);
137                 BT_PERMANENT_LOG("Connected HF");
138                 break;
139         case OAL_EVENT_HF_CLIENT_CONNECTING:
140                 BT_INFO("HF Client Profile connection successful, wait for Audio connect..");
141                 break;
142         case OAL_EVENT_HF_CLIENT_DISCONNECTING:
143                 BT_INFO("HF Client Connecting or Disconnecting..No need to send event to app");
144                 break;
145         default:
146                 break;
147         }
148 }
149
150 int _bt_connect_remote_ag(bluetooth_device_address_t *device_address)
151 {
152         oal_status_t status = OAL_STATUS_SUCCESS;
153         int result = BLUETOOTH_ERROR_NONE;
154         gboolean is_connected = FALSE;
155
156         BT_INFO("+");
157
158         is_connected = device_get_svc_conn_state((bt_address_t*)device_address, HFP_SERVICE_ID);
159         if (is_connected == TRUE) {
160                 BT_ERR("HF Client is already connected");
161                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
162         }
163
164         status = hf_client_connect((bt_address_t *)device_address);
165         if (status != OAL_STATUS_SUCCESS) {
166                 BT_ERR("HF Client Connection could not be established, err: [%d]", status);
167                 result = _bt_convert_oal_status_to_bt_error(status);
168         }
169         return result;
170 }
171
172 int _bt_disconnect_remote_ag(bluetooth_device_address_t *device_address)
173 {
174         oal_status_t status = OAL_STATUS_SUCCESS;
175         int result = BLUETOOTH_ERROR_NONE;
176         BT_INFO("+");
177
178         status = hf_client_disconnect((bt_address_t *)device_address);
179         if (status != OAL_STATUS_SUCCESS) {
180                 BT_ERR("HF Client DisConnection err: [%d]", status);
181                 result = _bt_convert_oal_status_to_bt_error(status);
182         }
183         return result;
184 }
185