Fix 64bit build error
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-hf-client.c
1 /*
2  * Open Adaptation Layer (OAL)
3  *
4  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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 <sys/un.h>
22 #include <sys/socket.h>
23 #include <sys/errno.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <dlog.h>
27
28 #include <bluetooth.h>
29 #include <bt_hf_client.h>
30
31 #include "oal-common.h"
32 #include "oal-internal.h"
33 #include "oal-hf-client.h"
34 #include "oal-utils.h"
35
36 static const bthf_client_interface_t *blued_hf_client_interface = NULL;
37
38 #define CHECK_OAL_HFP_CLIENT_ENABLED() \
39         do { \
40                 if (blued_hf_client_interface == NULL) { \
41                         BT_ERR("OAL, HFP Client Not Enabled"); \
42                         return OAL_STATUS_NOT_READY; \
43                 } \
44         } while (0)
45
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);
48
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,
53         .vr_cmd_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,
59         .call_cb = NULL,
60         .callsetup_cb = NULL,
61         .callheld_cb = NULL,
62         .resp_and_hold_cb = NULL,
63         .clip_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
73 };
74
75 oal_status_t hf_client_enable(void)
76 {
77         const bt_interface_t* blued_inf;
78         int ret;
79
80         API_TRACE("HFP Client Enable");
81
82         if ((blued_inf = adapter_get_stack_interface()) == NULL) {
83                 BT_ERR("Bluetooth module is not loaded");
84                 return OAL_STATUS_NOT_READY;
85         }
86
87         if (blued_hf_client_interface != NULL) {
88                 BT_WARN("HFP Client Interface is already initialized...");
89                 return OAL_STATUS_ALREADY_DONE;
90         }
91
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;
95         }
96
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);
102         }
103         BT_DBG("OAL, Bluetooth HFP Client interface initialised");
104
105         return OAL_STATUS_SUCCESS;
106 }
107
108 /* HFP Client deinit: Resets all the HFP Client information
109  * Note: Adapter should be disabled before calling deinit
110  * */
111 oal_status_t hf_client_disable(void)
112 {
113         API_TRACE("HFP Client disable");
114         CHECK_OAL_HFP_CLIENT_ENABLED();
115
116         blued_hf_client_interface->cleanup();
117         blued_hf_client_interface = NULL;
118
119         return OAL_STATUS_SUCCESS;
120 }
121
122 void hf_client_cleanup(void)
123 {
124         API_TRACE("HFP Client Cleanup");
125         blued_hf_client_interface = NULL;
126 }
127
128 oal_status_t hf_client_connect(bt_address_t *device_address)
129 {
130         int result = OAL_STATUS_SUCCESS;
131         bt_status_t status;
132         bdstr_t bdstr;
133         API_TRACE("HF Client Connect");
134
135         CHECK_OAL_HFP_CLIENT_ENABLED();
136         OAL_CHECK_PARAMETER(device_address, return);
137
138         BT_INFO("BT remote AG Device Address: %s", bdt_bd2str(device_address, &bdstr) + 12);
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);
144         }
145         return result;
146 }
147
148
149 oal_status_t hf_client_disconnect(bt_address_t *device_address)
150 {
151         int result = OAL_STATUS_SUCCESS;
152         bdstr_t bdstr;
153         bt_status_t status;
154         API_TRACE("HF Client Disconnect");
155
156         CHECK_OAL_HFP_CLIENT_ENABLED();
157         OAL_CHECK_PARAMETER(device_address, return);
158
159         BT_INFO("BT remote AG Address: %s", bdt_bd2str(device_address, &bdstr) + 12);
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);
165         }
166         return result;
167 }
168
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)
171 {
172         int event_type;
173         bdstr_t bdstr;
174         bt_address_t * event_data = NULL;
175
176         if (bd_addr == NULL) {
177                 BT_ERR("Address is NULL");
178                 return;
179         }
180
181         event_data = g_new0(bt_address_t, 1);
182         if (event_data == NULL) {
183                 BT_ERR("failed to allocate event_data");
184                 return;
185         }
186         memcpy(event_data->addr, bd_addr->address, BT_ADDRESS_BYTES_NUM);
187
188         BT_INFO("HFP Client Profile state = [%d],  BT Address = [%s]", state, bdt_bd2str((bt_address_t*)bd_addr, &bdstr) + 12);
189         switch (state) {
190         case BTHF_CLIENT_CONNECTION_STATE_DISCONNECTED: {
191                  event_type = OAL_EVENT_HF_CLIENT_DISCONNECTED;
192                  break;
193         }
194         case BTHF_CLIENT_CONNECTION_STATE_CONNECTING: {
195                 event_type = OAL_EVENT_HF_CLIENT_CONNECTING;
196                 break;
197         }
198         case BTHF_CLIENT_CONNECTION_STATE_CONNECTED: {
199                 event_type = OAL_EVENT_HF_CLIENT_CONNECTED;
200                 break;
201         }
202         case BTHF_CLIENT_CONNECTION_STATE_DISCONNECTING: {
203                 event_type = OAL_EVENT_HF_CLIENT_DISCONNECTING;
204                 break;
205         }
206         default: {
207                 BT_INFO("Invalid state");
208                 g_free(event_data);
209                 return;
210         }
211         }
212         send_event_bda_trace(event_type, event_data, sizeof(bt_address_t), (bt_address_t*)bd_addr);
213 }
214