4 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include "bt-hal-hf-dbus-handler.h"
31 #include "bt-hal-dbus-common-utils.h"
32 #include "bt-hal-internal.h"
34 static handle_stack_msg event_cb = NULL;
36 /* To send stack event to hal-hf handler */
37 void _bt_hal_register_hf_client_dbus_handler_cb(handle_stack_msg cb)
42 /* To send stack event to hal-hf handler */
43 void _bt_hal_unregister_hf_client_dbus_handler_cb()
48 static void __bt_hf_client_connect_cb(GDBusProxy *proxy, GAsyncResult *res,
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;
59 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
60 g_object_unref(proxy);
62 ERR("HF Client Connect Dbus Call Error");
64 ERR("Error: %s", g_error->message);
65 g_clear_error(&g_error);
67 result = BT_STATUS_FAIL;
69 g_variant_unref(reply);
71 DBG("Address: %s", address);
73 * If result is success, HF client connected event will be triggered
74 * automatically from stack, so return from here.
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;
82 ERR("HF client dbus handler callback not registered");
84 event_cb(HAL_EV_HF_CLIENT_CONN_STATE, (void *)&ev, sizeof(ev));
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;
93 ERR("HF client DBUS handler callback not registered");
95 event_cb(HAL_EV_HF_CLIENT_CONN_STATE, (void *)&ev, sizeof(ev));
100 bt_status_t _bt_hal_dbus_handler_hf_client_connect(bt_bdaddr_t *bd_addr)
106 ERR("bd_addr is NULL, return");
107 return BT_STATUS_PARM_INVALID;
110 address = g_malloc0(BT_HAL_ADDRESS_STRING_SIZE * sizeof(char));
112 ERR("Memory allocation failed");
113 return BT_STATUS_NOMEM;
115 _bt_hal_convert_addr_type_to_string(address, bd_addr->address);
117 DBG("Connect profile :HFP_AG_UUID");
118 ret = _bt_hal_connect_profile(address, HFP_AG_UUID,
119 __bt_hf_client_connect_cb, address);
121 if (ret != BT_HAL_ERROR_NONE) {
122 ERR("_bt_hal_connect_profile(HF) Error");
124 return BT_STATUS_FAIL;
127 return BT_STATUS_SUCCESS;
130 static void __bt_hf_client_disconnect_cb(GDBusProxy *proxy, GAsyncResult *res,
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;
141 reply = g_dbus_proxy_call_finish(proxy, res, &g_error);
142 g_object_unref(proxy);
144 ERR("HF Client Disconnect Dbus Call Error");
146 ERR("Error: %s\n", g_error->message);
147 g_clear_error(&g_error);
149 result = BT_STATUS_FAIL;
151 g_variant_unref(reply);
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;
160 ERR("HF Client dbus handler callback not registered");
162 event_cb(HAL_EV_HF_CLIENT_CONN_STATE, (void *)&ev, sizeof(ev));
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;
171 ERR("HF Client DBUS handler callback not registered");
173 event_cb(HAL_EV_HF_CLIENT_CONN_STATE, (void *)&ev, sizeof(ev));
179 bt_status_t _bt_hal_dbus_handler_hf_client_disconnect(bt_bdaddr_t *bd_addr)
185 ERR("bd_addr is NULL, return");
186 return BT_STATUS_PARM_INVALID;
189 address = g_malloc0(BT_HAL_ADDRESS_STRING_SIZE * sizeof(char));
191 ERR("Memory allocation failed");
192 return BT_STATUS_NOMEM;
194 _bt_hal_convert_addr_type_to_string(address, bd_addr->address);
196 DBG("Disconnect profile :HFP_AG_UUID");
198 ret = _bt_hal_disconnect_profile(address, HFP_AG_UUID,
199 __bt_hf_client_disconnect_cb, address);
201 if (ret != BT_HAL_ERROR_NONE) {
202 ERR("_bt_hal_connect_profile Error");
204 return BT_STATUS_FAIL;
207 return BT_STATUS_SUCCESS;