ce6c3d111fcf774bc5447cb70f46c58e37e2c1f7
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-hf-dbus-handler.c
1 /*
2  * BLUETOOTH HAL
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 <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
26
27 #include <glib.h>
28 #include <gio/gio.h>
29 #include <dlog.h>
30 #include <vconf.h>
31
32 #include "bt-hal-hf-dbus-handler.h"
33 #include "bt-hal-dbus-common-utils.h"
34 #include "bt-hal-internal.h"
35
36 static handle_stack_msg event_cb = NULL;
37
38 /* To send stack event to hal-hf handler */
39 void _bt_hal_register_hf_dbus_handler_cb(handle_stack_msg cb)
40 {
41         event_cb = cb;
42 }
43
44 /* To send stack event to hal-hf handler */
45 void _bt_hal_unregister_hf_dbus_handler_cb()
46 {
47         event_cb = NULL;
48 }
49
50 static void __bt_hf_connect_cb(GDBusProxy *proxy, GAsyncResult *res,
51                                                         gpointer user_data)
52 {
53         GError *g_error = NULL;
54         struct hal_ev_handsfree_conn_state ev;
55         GVariant *reply = NULL;
56         char *address = user_data;
57         int result = BT_STATUS_SUCCESS;
58
59         DBG("+");
60
61         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
62         g_object_unref(proxy);
63         if (reply == NULL) {
64                 ERR("HF Connect Dbus Call Error");
65                 if (g_error) {
66                         ERR("Error: %s\n", g_error->message);
67                         g_clear_error(&g_error);
68                 }
69                 result = BT_STATUS_FAIL;
70         }
71         g_variant_unref(reply);
72
73         DBG("Address: %s", address);
74         /*
75          * If result is success, HF connected event will be triggered
76          * automatically from stack, so return from here.
77          */
78         if (result == BT_STATUS_SUCCESS) {
79                 /* Prepare to send HF connecting event */
80                 memset(&ev, 0, sizeof(ev));
81                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
82                 ev.state = HAL_EV_HANDSFREE_CONN_STATE_CONNECTING;
83                 if (!event_cb)
84                         ERR("HF dbus handler callback not registered");
85                 else
86                         event_cb(HAL_EV_HANDSFREE_CONN_STATE, (void *)&ev, sizeof(ev));
87
88         } else {
89                 /* Prepare to send HF connection state event */
90                 ERR("HF Connection has failed!!");
91                 memset(&ev, 0, sizeof(ev));
92                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
93                 ev.state = HAL_EV_HANDSFREE_CONN_STATE_DISCONNECTED;
94                 if (!event_cb)
95                         ERR("HF DBUS handler callback not registered");
96                 else
97                         event_cb(HAL_EV_HANDSFREE_CONN_STATE, (void *)&ev, sizeof(ev));
98         }
99         g_free(address);
100 }
101
102 bt_status_t _bt_hal_dbus_handler_hf_connect(bt_bdaddr_t *bd_addr)
103 {
104         char *address;
105         GDBusConnection *conn;
106         int ret;
107
108         if(!bd_addr) {
109                 ERR("bd_addr is NULL, return");
110                 return BT_STATUS_PARM_INVALID;
111         }
112
113         conn = _bt_hal_get_system_gconn();
114         if(!conn) {
115                 ERR("_bt_hal_get_system_gconn returned NULL, return");
116                 return BT_STATUS_FAIL;
117         }
118
119         address = g_malloc0(BT_HAL_ADDRESS_STRING_SIZE * sizeof(char));
120         if (!address) {
121                 ERR("Memory allocation failed");
122                 return BT_STATUS_NOMEM;
123         }
124         _bt_hal_convert_addr_type_to_string(address, bd_addr->address);
125
126         ret = _bt_hal_connect_profile(address, HFP_HF_UUID,
127                         __bt_hf_connect_cb, address);
128
129         if (ret != BT_HAL_ERROR_NONE) {
130                 ERR("_bt_hal_connect_profile(HF) Error");
131                 return BT_STATUS_FAIL;
132         }
133
134         return BT_STATUS_SUCCESS;
135 }
136
137 static void __bt_hf_disconnect_cb(GDBusProxy *proxy, GAsyncResult *res,
138                                                         gpointer user_data)
139 {
140         GError *g_error = NULL;
141         struct hal_ev_handsfree_conn_state ev;
142         GVariant *reply = NULL;
143         char *address = user_data;
144         int result = BT_STATUS_SUCCESS;
145
146         DBG("+");
147
148         reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
149         g_object_unref(proxy);
150         if (reply == NULL) {
151                 ERR("HF Disconnect Dbus Call Error");
152                 if (g_error) {
153                         ERR("Error: %s\n", g_error->message);
154                         g_clear_error(&g_error);
155                 }
156                 result = BT_STATUS_FAIL;
157         }
158         g_variant_unref(reply);
159
160         if (result != BT_STATUS_FAIL)
161                 DBG("HF Disconnect successful for Device: %s", address);
162         else
163                 DBG("HF Disconnect un-successful for Device: %s", address);
164
165         /*
166          * If result is success, HF connected event will be triggered
167          * automatically from stack, so return from here.
168          */
169         if (result == BT_STATUS_SUCCESS) {
170                 /* Prepare to send HID connecting event */
171                 memset(&ev, 0, sizeof(ev));
172                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
173                 ev.state = HAL_EV_HANDSFREE_CONN_STATE_DISCONNECTING;
174                 if (!event_cb)
175                         ERR("HF dbus handler callback not registered");
176                 else
177                         event_cb(HAL_EV_HANDSFREE_CONN_STATE, (void *)&ev, sizeof(ev));
178
179         } else {
180                 /* Prepare to send HF connection state event */
181                 ERR("HF Connection has failed!!");
182                 memset(&ev, 0, sizeof(ev));
183                 _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
184                 ev.state = HAL_EV_HANDSFREE_CONN_STATE_DISCONNECTED;
185                 if (!event_cb)
186                         ERR("HF DBUS handler callback not registered");
187                 else
188                         event_cb(HAL_EV_HANDSFREE_CONN_STATE, (void *)&ev, sizeof(ev));
189         }
190         g_free(address);
191         DBG("-");
192 }
193
194 bt_status_t _bt_hal_dbus_handler_hf_disconnect(bt_bdaddr_t *bd_addr)
195 {
196         char *address;
197         GDBusConnection *conn;
198         int ret;
199
200         if(!bd_addr) {
201                 ERR("bd_addr is NULL, return");
202                 return BT_STATUS_PARM_INVALID;
203         }
204
205         conn = _bt_hal_get_system_gconn();
206         if(!conn) {
207                 ERR("_bt_hal_get_system_gconn returned NULL, return");
208                 return BT_STATUS_FAIL;
209         }
210
211         address = g_malloc0(BT_HAL_ADDRESS_STRING_SIZE * sizeof(char));
212         if (!address) {
213                 ERR("Memory allocation failed");
214                 return BT_STATUS_NOMEM;
215         }
216         _bt_hal_convert_addr_type_to_string(address, bd_addr->address);
217
218         ret = _bt_hal_disconnect_profile(address, HFP_HF_UUID,
219                         __bt_hf_disconnect_cb, address);
220
221         if (ret != BT_HAL_ERROR_NONE) {
222                 ERR("_bt_hal_connect_profile Error");
223                 return BT_STATUS_FAIL;
224         }
225
226         return BT_STATUS_SUCCESS;
227 }