2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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.
22 #include <sys/socket.h>
23 #include <sys/errno.h>
28 #include <bluetooth.h>
29 #include <bt_hf_client.h>
31 #include "oal-common.h"
32 #include "oal-internal.h"
33 #include "oal-hf-client.h"
34 #include "oal-utils.h"
36 static const bthf_client_interface_t *blued_hf_client_interface = NULL;
38 #define CHECK_OAL_HFP_CLIENT_ENABLED() \
40 if (blued_hf_client_interface == NULL) { \
41 BT_ERR("OAL, HFP Client Not Enabled"); \
42 return OAL_STATUS_NOT_READY; \
46 static void cb_hf_client_connection_state(bthf_client_connection_state_t state, unsigned int peer_feat,
47 unsigned int chld_feat, bt_bdaddr_t *bd_addr);
49 static bthf_client_callbacks_t blued_hf_client_cb = {
50 .size = sizeof(blued_hf_client_cb),
51 .connection_state_cb = cb_hf_client_connection_state,
52 .audio_state_cb = NULL,
54 .network_state_cb = NULL,
55 .network_roaming_cb = NULL,
56 .network_signal_cb = NULL,
57 .battery_level_cb = NULL,
58 .current_operator_cb = NULL,
62 .resp_and_hold_cb = NULL,
64 .call_waiting_cb = NULL,
65 .current_calls_cb = NULL,
66 .volume_change_cb = NULL,
67 .cmd_complete_cb = NULL,
68 .subscriber_info_cb = NULL,
69 .in_band_ring_tone_cb = NULL,
70 .last_voice_tag_number_callback = NULL,
71 .ring_indication_cb = NULL,
72 .get_voice_data_cb = NULL
75 oal_status_t hf_client_enable(void)
77 const bt_interface_t* blued_inf;
80 API_TRACE("HFP Client Enable");
82 if ((blued_inf = adapter_get_stack_interface()) == NULL) {
83 BT_ERR("Bluetooth module is not loaded");
84 return OAL_STATUS_NOT_READY;
87 if (blued_hf_client_interface != NULL) {
88 BT_WARN("HFP Client Interface is already initialized...");
89 return OAL_STATUS_ALREADY_DONE;
92 if ((blued_hf_client_interface = (const bthf_client_interface_t *)blued_inf->get_profile_interface(BT_PROFILE_HANDSFREE_CLIENT_ID)) == NULL) {
93 BT_ERR("OAL, Failed to get Bluetooth HFP Client Interface");
94 return OAL_STATUS_INTERNAL_ERROR;
97 BT_DBG("Got profile interface");
98 if ((ret = blued_hf_client_interface->init(&blued_hf_client_cb)) != BT_STATUS_SUCCESS) {
99 BT_ERR("Failed to initialize Bluetooth HFP Client, status: %s", status2string(ret));
100 blued_hf_client_interface = NULL;
101 return convert_to_oal_status(ret);
103 BT_DBG("OAL, Bluetooth HFP Client interface initialised");
105 return OAL_STATUS_SUCCESS;
108 /* HFP Client deinit: Resets all the HFP Client information
109 * Note: Adapter should be disabled before calling deinit
111 oal_status_t hf_client_disable(void)
113 API_TRACE("HFP Client disable");
114 CHECK_OAL_HFP_CLIENT_ENABLED();
116 blued_hf_client_interface->cleanup();
117 blued_hf_client_interface = NULL;
119 return OAL_STATUS_SUCCESS;
122 void hf_client_cleanup(void)
124 API_TRACE("HFP Client Cleanup");
125 blued_hf_client_interface = NULL;
128 oal_status_t hf_client_connect(bt_address_t *device_address)
130 int result = OAL_STATUS_SUCCESS;
133 API_TRACE("HF Client Connect");
135 CHECK_OAL_HFP_CLIENT_ENABLED();
136 OAL_CHECK_PARAMETER(device_address, return);
138 BT_INFO("BT remote AG Device Address: %s", bdt_bd2str(device_address, &bdstr));
139 /* Call connect function of Bluedroid*/
140 status = blued_hf_client_interface->connect((bt_bdaddr_t *)device_address);
141 if ((status != BT_STATUS_SUCCESS) && (status != BT_STATUS_DONE)) {
142 BT_ERR("Connection could not be established, err: %s", status2string(status));;
143 result = convert_to_oal_status(status);
149 oal_status_t hf_client_disconnect(bt_address_t *device_address)
151 int result = OAL_STATUS_SUCCESS;
154 API_TRACE("HF Client Disconnect");
156 CHECK_OAL_HFP_CLIENT_ENABLED();
157 OAL_CHECK_PARAMETER(device_address, return);
159 BT_INFO("BT remote AG Address: %s", bdt_bd2str(device_address, &bdstr));
160 /* call Disconnect function of Bluedroid */
161 status = blued_hf_client_interface->disconnect((bt_bdaddr_t *)device_address);
162 if ((status != BT_STATUS_SUCCESS) && (status != BT_STATUS_DONE)) {
163 BT_ERR("OAL, Disconnection failed err: %s", status2string(status));
164 result = convert_to_oal_status(status);
169 void cb_hf_client_connection_state(bthf_client_connection_state_t state, unsigned int peer_feat,
170 unsigned int chld_feat, bt_bdaddr_t *bd_addr)
174 bt_address_t * event_data = NULL;
176 if (bd_addr == NULL) {
177 BT_ERR("Address is NULL");
181 event_data = g_new0(bt_address_t, 1);
182 if (event_data == NULL) {
183 BT_ERR("failed to allocate event_data");
186 memcpy(event_data->addr, bd_addr->address, BT_ADDRESS_BYTES_NUM);
188 BT_INFO("HFP Client Profile state = [%d], BT Address = [%s]", state, bdt_bd2str((bt_address_t*)bd_addr, &bdstr));
190 case BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED: {
191 event_type = OAL_EVENT_HF_CLIENT_DISCONNECTED;
194 case BTHF_CLIENT_CONNECTION_STATE_CONNECTING: {
195 event_type = OAL_EVENT_HF_CLIENT_CONNECTING;
198 case BTHF_CLIENT_CONNECTION_STATE_CONNECTED: {
199 event_type = OAL_EVENT_HF_CLIENT_CONNECTED;
202 case BTHF_CLIENT_CONNECTION_STATE_DISCONNECTING: {
203 event_type = OAL_EVENT_HF_CLIENT_DISCONNECTING;
207 BT_INFO("Invalid state");
212 send_event_bda_trace(event_type, event_data, sizeof(bt_address_t), (bt_address_t*)bd_addr);