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