8c931f856a1e5527826c22942e68730b0218bd31
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-hf.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 <stdbool.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <dlog.h>
27
28 #include "bt-hal.h"
29 #include "bt-hal-log.h"
30 #include "bt-hal-msg.h"
31 #include "bt-hal-utils.h"
32
33 #include "bt-hal-hf-dbus-handler.h"
34 #include "bt-hal-event-receiver.h"
35
36 static const bthf_callbacks_t *bt_hal_hf_cbacks;
37
38 static bool interface_ready(void)
39 {
40         return bt_hal_hf_cbacks != NULL;
41 }
42
43 static bt_status_t hf_connect(bt_bdaddr_t *bd_addr)
44 {
45         DBG("");
46         return _bt_hal_dbus_handler_hf_connect(bd_addr);
47 }
48
49 static bt_status_t hf_disconnect(bt_bdaddr_t *bd_addr)
50 {
51         DBG("");
52         return _bt_hal_dbus_handler_hf_disconnect(bd_addr);
53 }
54
55 static bt_status_t hf_connect_audio(bt_bdaddr_t *bd_addr)
56 {
57         DBG("");
58         return BT_STATUS_UNSUPPORTED;
59 }
60
61 static bt_status_t hf_disconnect_audio(bt_bdaddr_t *bd_addr)
62 {
63         DBG("");
64         return BT_STATUS_UNSUPPORTED;
65 }
66
67 static bt_status_t hf_start_voice_recognition(bt_bdaddr_t *bd_addr)
68 {
69         DBG("");
70         return BT_STATUS_UNSUPPORTED;
71 }
72
73 static bt_status_t hf_stop_voice_recognition(bt_bdaddr_t *bd_addr)
74 {
75         DBG("");
76         return BT_STATUS_UNSUPPORTED;
77 }
78
79 static bt_status_t hf_voume_control(bthf_volume_type_t type, int volume,
80                                                        bt_bdaddr_t *bd_addr)
81 {
82         DBG("");
83         return BT_STATUS_UNSUPPORTED;
84 }
85
86 static bt_status_t hf_device_status_notification(bthf_network_state_t ntk_state,
87                           bthf_service_type_t svc_type, int signal, int batt_chg)
88 {
89         DBG("");
90         return BT_STATUS_UNSUPPORTED;
91 }
92
93 static bt_status_t hf_cops_response(const char *cops, bt_bdaddr_t *bd_addr)
94 {
95         DBG("");
96         return BT_STATUS_UNSUPPORTED;
97 }
98
99 static bt_status_t hf_cind_response(int svc, int num_active, int num_held,
100                                      bthf_call_state_t call_setup_state,
101                                      int signal, int roam, int batt_chg,
102                                                     bt_bdaddr_t *bd_addr)
103 {
104         DBG("");
105         return BT_STATUS_UNSUPPORTED;
106 }
107
108 static bt_status_t hf_formatted_at_response(const char *rsp, bt_bdaddr_t *bd_addr)
109 {
110         DBG("");
111         return BT_STATUS_UNSUPPORTED;
112 }
113
114 static bt_status_t hf_at_response(bthf_at_response_t response_code,
115                                     int error_code, bt_bdaddr_t *bd_addr)
116 {
117         DBG("");
118         return BT_STATUS_UNSUPPORTED;
119 }
120
121 static bt_status_t hf_clcc_response(int index, bthf_call_direction_t dir,
122                                bthf_call_state_t state, bthf_call_mode_t mode,
123                                bthf_call_mpty_type_t mpty, const char *number,
124                                bthf_call_addrtype_t type, bt_bdaddr_t *bd_addr)
125 {
126         DBG("");
127         return BT_STATUS_UNSUPPORTED;
128 }
129
130 static bt_status_t hf_phone_state_change(int num_active, int num_held, bthf_call_state_t call_setup_state,
131                                             const char *number, bthf_call_addrtype_t type)
132 {
133         DBG("");
134         return BT_STATUS_UNSUPPORTED;
135 }
136
137 static void __bt_hal_handle_hf_conn_state(void *buf, uint16_t len)
138 {
139         struct hal_ev_handsfree_conn_state *ev = buf;
140
141         if (bt_hal_hf_cbacks->connection_state_cb)
142                 bt_hal_hf_cbacks->connection_state_cb(ev->state, (bt_bdaddr_t *) ev->bdaddr);
143 }
144
145 static void __bt_hal_handle_hf_audio_conn_state(void *buf, uint16_t len)
146 {
147         struct hal_ev_handsfree_audio_state *ev = buf;
148
149         if (bt_hal_hf_cbacks->audio_state_cb)
150                 bt_hal_hf_cbacks->audio_state_cb(ev->state, (bt_bdaddr_t *) ev->bdaddr);
151 }
152
153 static void __bt_hal_handle_hf_events(int message, void *buf, uint16_t len)
154 {
155         DBG("+");
156         if (!interface_ready())
157                 return;
158         switch(message) {
159         case HAL_EV_HANDSFREE_CONN_STATE:
160                 DBG("Event: HAL_EV_HANDSFREE_CONN_STATE");
161                 __bt_hal_handle_hf_conn_state(buf, len);
162                 break;
163         case HAL_EV_HANDSFREE_AUDIO_STATE:
164                 DBG("Event: HAL_EV_HANDSFREE_AUDIO_STATE");
165                 __bt_hal_handle_hf_audio_conn_state(buf, len);
166                 break;
167         default:
168                 DBG("Event Currently not handled!!");
169                 break;
170         }
171         DBG("-");
172 }
173
174 static bt_status_t init(bthf_callbacks_t* callbacks, int max_hf_clients)
175 {
176         DBG("");
177
178         if (interface_ready())
179                 return BT_STATUS_DONE;
180
181         if (max_hf_clients > 1)
182                 return BT_STATUS_UNSUPPORTED;
183         bt_hal_hf_cbacks = callbacks;
184         DBG("Register HF AG Role callback function");
185         _bt_hal_register_hf_dbus_handler_cb(__bt_hal_handle_hf_events);
186         _bt_hal_register_event_handler_cb(HAL_HF_AG, __bt_hal_handle_hf_events);
187         return BT_STATUS_SUCCESS;
188 }
189
190 static void cleanup(void)
191 {
192         DBG("");
193
194         if (!interface_ready())
195                 return;
196
197         _bt_hal_unregister_event_handler_cb(HAL_HF_AG);
198         bt_hal_hf_cbacks = NULL;
199 }
200
201 static bthf_interface_t bthf_if = {
202     .size = sizeof(bthf_if),
203     .init = init,
204     .connect = hf_connect,
205     .disconnect = hf_disconnect,
206     .connect_audio = hf_connect_audio,
207     .disconnect_audio = hf_disconnect_audio,
208     .start_voice_recognition = hf_start_voice_recognition,
209     .stop_voice_recognition = hf_stop_voice_recognition,
210     .volume_control = hf_voume_control,
211     .device_status_notification = hf_device_status_notification,
212     .cops_response = hf_cops_response,
213     .cind_response = hf_cind_response,
214     .formatted_at_response = hf_formatted_at_response,
215     .at_response = hf_at_response,
216     .clcc_response = hf_clcc_response,
217     .phone_state_change = hf_phone_state_change,
218     .cleanup = cleanup
219 };
220
221 /*******************************************************************************
222 **
223 ** Function         bt_get_hf_interface
224 **
225 ** Description      Get the hf callback interface
226 **
227 ** Returns          bthf_interface_t
228 **
229 *******************************************************************************/
230 const bthf_interface_t *bt_get_hf_interface()
231 {
232     return &bthf_if;
233 }