2 * Copyright (C) 2014 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <cutils/properties.h>
28 #include "ipc-common.h"
31 static const bthf_client_callbacks_t *cbs = NULL;
33 static bool interface_ready(void)
38 static void handle_conn_state(void *buf, uint16_t len, int fd)
40 struct hal_ev_hf_client_conn_state *ev = buf;
42 if (cbs->connection_state_cb)
43 cbs->connection_state_cb(ev->state, ev->peer_feat,
45 (bt_bdaddr_t *) ev->bdaddr);
48 static void handle_audio_state(void *buf, uint16_t len, int fd)
50 struct hal_ev_hf_client_audio_state *ev = buf;
52 if (cbs->audio_state_cb)
53 cbs->audio_state_cb(ev->state, (bt_bdaddr_t *) (ev->bdaddr));
56 static void handle_vr_state(void *buf, uint16_t len, int fd)
58 struct hal_ev_hf_client_vr_state *ev = buf;
61 cbs->vr_cmd_cb(ev->state);
64 static void handle_network_state(void *buf, uint16_t len, int fd)
66 struct hal_ev_hf_client_net_state *ev = buf;
68 if (cbs->network_state_cb)
69 cbs->network_state_cb(ev->state);
72 static void handle_network_roaming(void *buf, uint16_t len, int fd)
74 struct hal_ev_hf_client_net_roaming_type *ev = buf;
76 if (cbs->network_roaming_cb)
77 cbs->network_roaming_cb(ev->state);
80 static void handle_network_signal(void *buf, uint16_t len, int fd)
82 struct hal_ev_hf_client_net_signal_strength *ev = buf;
84 if (cbs->network_signal_cb)
85 cbs->network_signal_cb(ev->signal_strength);
88 static void handle_battery_level(void *buf, uint16_t len, int fd)
90 struct hal_ev_hf_client_battery_level *ev = buf;
92 if (cbs->battery_level_cb)
93 cbs->battery_level_cb(ev->battery_level);
96 static void handle_operator_name(void *buf, uint16_t len, int fd)
98 struct hal_ev_hf_client_operator_name *ev = buf;
99 uint16_t name_len = ev->name_len;
102 if (len != sizeof(*ev) + name_len ||
103 (name_len != 0 && ev->name[name_len - 1] != '\0')) {
104 error("invalid operator name, aborting");
109 name = (char *) ev->name;
111 if (cbs->current_operator_cb)
112 cbs->current_operator_cb(name);
115 static void handle_call(void *buf, uint16_t len, int fd)
117 struct hal_ev_hf_client_call_indicator *ev = buf;
120 cbs->call_cb(ev->call);
123 static void handle_call_setup(void *buf, uint16_t len, int fd)
125 struct hal_ev_hf_client_call_setup_indicator *ev = buf;
127 if (cbs->callsetup_cb)
128 cbs->callsetup_cb(ev->call_setup);
131 static void handle_call_held(void *buf, uint16_t len, int fd)
133 struct hal_ev_hf_client_call_held_indicator *ev = buf;
135 if (cbs->callheld_cb)
136 cbs->callheld_cb(ev->call_held);
139 static void handle_response_and_hold(void *buf, uint16_t len, int fd)
141 struct hal_ev_hf_client_response_and_hold_status *ev = buf;
143 if (cbs->resp_and_hold_cb)
144 cbs->resp_and_hold_cb(ev->status);
147 static void handle_clip(void *buf, uint16_t len, int fd)
149 struct hal_ev_hf_client_calling_line_ident *ev = buf;
150 uint16_t num_len = ev->number_len;
153 if (len != sizeof(*ev) + num_len ||
154 (num_len != 0 && ev->number[num_len - 1] != '\0')) {
155 error("invalid clip, aborting");
160 number = (char *) ev->number;
163 cbs->clip_cb(number);
166 static void handle_call_waiting(void *buf, uint16_t len, int fd)
168 struct hal_ev_hf_client_call_waiting *ev = buf;
169 uint16_t num_len = ev->number_len;
172 if (len != sizeof(*ev) + num_len ||
173 (num_len != 0 && ev->number[num_len - 1] != '\0')) {
174 error("invalid call waiting, aborting");
179 number = (char *) ev->number;
181 if (cbs->call_waiting_cb)
182 cbs->call_waiting_cb(number);
185 static void handle_current_calls(void *buf, uint16_t len, int fd)
187 struct hal_ev_hf_client_current_call *ev = buf;
188 uint16_t num_len = ev->number_len;
191 if (len != sizeof(*ev) + num_len ||
192 (num_len != 0 && ev->number[num_len - 1] != '\0')) {
193 error("invalid current calls, aborting");
198 number = (char *) ev->number;
200 if (cbs->current_calls_cb)
201 cbs->current_calls_cb(ev->index, ev->direction, ev->call_state,
202 ev->multiparty, number);
205 static void handle_volume_change(void *buf, uint16_t len, int fd)
207 struct hal_ev_hf_client_volume_changed *ev = buf;
209 if (cbs->volume_change_cb)
210 cbs->volume_change_cb(ev->type, ev->volume);
213 static void handle_command_cmp(void *buf, uint16_t len, int fd)
215 struct hal_ev_hf_client_command_complete *ev = buf;
217 if (cbs->cmd_complete_cb)
218 cbs->cmd_complete_cb(ev->type, ev->cme);
221 static void handle_subscriber_info(void *buf, uint16_t len, int fd)
223 const struct hal_ev_hf_client_subscriber_service_info *ev = buf;
224 uint16_t name_len = ev->name_len;
227 if (len != sizeof(*ev) + name_len ||
228 (name_len != 0 && ev->name[name_len - 1] != '\0')) {
229 error("invalid sunscriber info, aborting");
234 name = (char *) ev->name;
236 if (cbs->subscriber_info_cb)
237 cbs->subscriber_info_cb(name, ev->type);
240 static void handle_in_band_ringtone(void *buf, uint16_t len, int fd)
242 struct hal_ev_hf_client_inband_settings *ev = buf;
244 if (cbs->in_band_ring_tone_cb)
245 cbs->in_band_ring_tone_cb(ev->state);
248 static void handle_last_voice_tag_number(void *buf, uint16_t len, int fd)
250 const struct hal_ev_hf_client_last_void_call_tag_num *ev = buf;
252 uint16_t num_len = ev->number_len;
254 if (len != sizeof(*ev) + num_len ||
255 (num_len != 0 && ev->number[num_len - 1] != '\0')) {
256 error("invalid voice tag, aborting");
261 number = (char *) ev->number;
263 if (cbs->last_voice_tag_number_callback)
264 cbs->last_voice_tag_number_callback(number);
267 static void handle_ring_indication(void *buf, uint16_t len, int fd)
269 if (cbs->ring_indication_cb)
270 cbs->ring_indication_cb();
274 * handlers will be called from notification thread context,
275 * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
277 static const struct hal_ipc_handler ev_handlers[] = {
278 /* HAL_EV_HF_CLIENT_CONN_STATE */
279 { handle_conn_state, false,
280 sizeof(struct hal_ev_hf_client_conn_state) },
281 /* HAL_EV_HF_CLIENT_AUDIO_STATE */
282 { handle_audio_state, false,
283 sizeof(struct hal_ev_hf_client_audio_state) },
284 /* HAL_EV_HF_CLIENT_VR_STATE */
285 { handle_vr_state, false, sizeof(struct hal_ev_hf_client_vr_state) },
286 /*HAL_EV_HF_CLIENT_NET_STATE */
287 { handle_network_state, false,
288 sizeof(struct hal_ev_hf_client_net_state)},
289 /*HAL_EV_HF_CLIENT_NET_ROAMING_TYPE */
290 { handle_network_roaming, false,
291 sizeof(struct hal_ev_hf_client_net_roaming_type) },
292 /* HAL_EV_HF_CLIENT_NET_SIGNAL_STRENGTH */
293 { handle_network_signal, false,
294 sizeof(struct hal_ev_hf_client_net_signal_strength) },
295 /* HAL_EV_HF_CLIENT_BATTERY_LEVEL */
296 { handle_battery_level, false,
297 sizeof(struct hal_ev_hf_client_battery_level) },
298 /* HAL_EV_HF_CLIENT_OPERATOR_NAME */
299 { handle_operator_name, true,
300 sizeof(struct hal_ev_hf_client_operator_name) },
301 /* HAL_EV_HF_CLIENT_CALL_INDICATOR */
302 { handle_call, false,
303 sizeof(struct hal_ev_hf_client_call_indicator) },
304 /* HAL_EV_HF_CLIENT_CALL_SETUP_INDICATOR */
305 { handle_call_setup, false,
306 sizeof(struct hal_ev_hf_client_call_setup_indicator) },
307 /* HAL_EV_HF_CLIENT_CALL_HELD_INDICATOR */
308 { handle_call_held, false,
309 sizeof(struct hal_ev_hf_client_call_held_indicator) },
310 /* HAL_EV_HF_CLIENT_RESPONSE_AND_HOLD_STATUS */
311 { handle_response_and_hold, false,
312 sizeof(struct hal_ev_hf_client_response_and_hold_status) },
313 /* HAL_EV_HF_CLIENT_CALLING_LINE_IDENT */
315 sizeof(struct hal_ev_hf_client_calling_line_ident) },
316 /* HAL_EV_HF_CLIENT_CALL_WAITING */
317 { handle_call_waiting, true,
318 sizeof(struct hal_ev_hf_client_call_waiting) },
319 /* HAL_EV_HF_CLIENT_CURRENT_CALL */
320 { handle_current_calls, true,
321 sizeof(struct hal_ev_hf_client_current_call) },
322 /* HAL_EV_CLIENT_VOLUME_CHANGED */
323 { handle_volume_change, false,
324 sizeof(struct hal_ev_hf_client_volume_changed) },
325 /* HAL_EV_CLIENT_COMMAND_COMPLETE */
326 { handle_command_cmp, false,
327 sizeof(struct hal_ev_hf_client_command_complete) },
328 /* HAL_EV_CLIENT_SUBSCRIBER_SERVICE_INFO */
329 { handle_subscriber_info, true,
330 sizeof(struct hal_ev_hf_client_subscriber_service_info) },
331 /* HAL_EV_CLIENT_INBAND_SETTINGS */
332 { handle_in_band_ringtone, false,
333 sizeof(struct hal_ev_hf_client_inband_settings) },
334 /* HAL_EV_CLIENT_LAST_VOICE_CALL_TAG_NUM */
335 { handle_last_voice_tag_number, true,
336 sizeof(struct hal_ev_hf_client_last_void_call_tag_num) },
337 /* HAL_EV_CLIENT_RING_INDICATION */
338 { handle_ring_indication, false, 0 },
341 static bt_status_t init(bthf_client_callbacks_t *callbacks)
343 struct hal_cmd_register_module cmd;
348 if (interface_ready())
349 return BT_STATUS_DONE;
353 hal_ipc_register(HAL_SERVICE_ID_HANDSFREE_CLIENT, ev_handlers,
354 sizeof(ev_handlers)/sizeof(ev_handlers[0]));
356 cmd.service_id = HAL_SERVICE_ID_HANDSFREE_CLIENT;
357 cmd.mode = HAL_MODE_DEFAULT;
360 ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
361 sizeof(cmd), &cmd, NULL, NULL, NULL);
362 if (ret != BT_STATUS_SUCCESS) {
364 hal_ipc_unregister(HAL_SERVICE_ID_HANDSFREE_CLIENT);
370 static bt_status_t hf_client_connect(bt_bdaddr_t *bd_addr)
372 struct hal_cmd_hf_client_connect cmd;
376 if (!interface_ready())
377 return BT_STATUS_NOT_READY;
380 return BT_STATUS_PARM_INVALID;
382 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
384 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
385 HAL_OP_HF_CLIENT_CONNECT, sizeof(cmd), &cmd,
389 static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
391 struct hal_cmd_hf_client_disconnect cmd;
395 if (!interface_ready())
396 return BT_STATUS_NOT_READY;
399 return BT_STATUS_PARM_INVALID;
401 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
403 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
404 HAL_OP_HF_CLIENT_DISCONNECT, sizeof(cmd), &cmd,
408 static bt_status_t connect_audio(bt_bdaddr_t *bd_addr)
410 struct hal_cmd_hf_client_connect_audio cmd;
414 if (!interface_ready())
415 return BT_STATUS_NOT_READY;
418 return BT_STATUS_PARM_INVALID;
420 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
422 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
423 HAL_OP_HF_CLIENT_CONNECT_AUDIO, sizeof(cmd),
424 &cmd, NULL, NULL, NULL);
427 static bt_status_t disconnect_audio(bt_bdaddr_t *bd_addr)
429 struct hal_cmd_hf_client_disconnect_audio cmd;
433 if (!interface_ready())
434 return BT_STATUS_NOT_READY;
437 return BT_STATUS_PARM_INVALID;
439 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
441 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
442 HAL_OP_HF_CLIENT_DISCONNECT_AUDIO, sizeof(cmd),
443 &cmd, NULL, NULL, NULL);
446 static bt_status_t start_voice_recognition(void)
450 if (!interface_ready())
451 return BT_STATUS_NOT_READY;
453 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
454 HAL_OP_HF_CLIENT_START_VR, 0, NULL, NULL, NULL,
458 static bt_status_t stop_voice_recognition(void)
462 if (!interface_ready())
463 return BT_STATUS_NOT_READY;
465 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
466 HAL_OP_HF_CLIENT_STOP_VR, 0, NULL, NULL, NULL,
470 static bt_status_t volume_control(bthf_client_volume_type_t type,
473 struct hal_cmd_hf_client_volume_control cmd;
477 if (!interface_ready())
478 return BT_STATUS_NOT_READY;
483 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
484 HAL_OP_HF_CLIENT_VOLUME_CONTROL, sizeof(cmd),
485 &cmd, NULL, NULL, NULL);
488 static bt_status_t dial(const char *number)
491 struct hal_cmd_hf_client_dial *cmd = (void *) buf;
496 if (!interface_ready())
497 return BT_STATUS_NOT_READY;
500 cmd->number_len = strlen(number) + 1;
501 memcpy(cmd->number, number, cmd->number_len);
506 len = sizeof(*cmd) + cmd->number_len;
508 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
509 HAL_OP_HF_CLIENT_DIAL, len, cmd, NULL, NULL,
513 static bt_status_t dial_memory(int location)
515 struct hal_cmd_hf_client_dial_memory cmd;
519 if (!interface_ready())
520 return BT_STATUS_NOT_READY;
522 cmd.location = location;
524 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
525 HAL_OP_HF_CLIENT_DIAL_MEMORY,
526 sizeof(cmd), &cmd, NULL, NULL, NULL);
529 static bt_status_t call_action(bthf_client_call_action_t action, int index)
531 struct hal_cmd_hf_client_call_action cmd;
535 if (!interface_ready())
536 return BT_STATUS_NOT_READY;
541 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
542 HAL_OP_HF_CLIENT_CALL_ACTION, sizeof(cmd), &cmd,
546 static bt_status_t query_current_calls(void)
550 if (!interface_ready())
551 return BT_STATUS_NOT_READY;
553 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
554 HAL_OP_HF_CLIENT_QUERY_CURRENT_CALLS, 0, NULL,
558 static bt_status_t query_operator_name(void)
562 if (!interface_ready())
563 return BT_STATUS_NOT_READY;
565 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
566 HAL_OP_HF_CLIENT_QUERY_OPERATOR_NAME, 0, NULL,
570 static bt_status_t retrieve_subsr_info(void)
574 if (!interface_ready())
575 return BT_STATUS_NOT_READY;
577 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
578 HAL_OP_HF_CLIENT_RETRIEVE_SUBSCR_INFO, 0, NULL,
582 static bt_status_t send_dtmf(char tone)
584 struct hal_cmd_hf_client_send_dtmf cmd;
588 if (!interface_ready())
589 return BT_STATUS_NOT_READY;
593 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
594 HAL_OP_HF_CLIENT_SEND_DTMF, sizeof(cmd), &cmd,
598 static bt_status_t request_last_voice_tag_number(void)
602 if (!interface_ready())
603 return BT_STATUS_NOT_READY;
605 return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE_CLIENT,
606 HAL_OP_HF_CLIENT_GET_LAST_VOICE_TAG_NUM,
607 0, NULL, NULL, NULL, NULL);
610 static void cleanup(void)
612 struct hal_cmd_unregister_module cmd;
616 if (!interface_ready())
619 cmd.service_id = HAL_SERVICE_ID_HANDSFREE_CLIENT;
621 hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
622 sizeof(cmd), &cmd, NULL, NULL, NULL);
624 hal_ipc_unregister(HAL_SERVICE_ID_HANDSFREE_CLIENT);
629 static bthf_client_interface_t iface = {
630 .size = sizeof(iface),
632 .connect = hf_client_connect,
633 .disconnect = disconnect,
634 .connect_audio = connect_audio,
635 .disconnect_audio = disconnect_audio,
636 .start_voice_recognition = start_voice_recognition,
637 .stop_voice_recognition = stop_voice_recognition,
638 .volume_control = volume_control,
640 .dial_memory = dial_memory,
641 .handle_call_action = call_action,
642 .query_current_calls = query_current_calls,
643 .query_current_operator_name = query_operator_name,
644 .retrieve_subscriber_info = retrieve_subsr_info,
645 .send_dtmf = send_dtmf,
646 .request_last_voice_tag_number = request_last_voice_tag_number,
650 bthf_client_interface_t *bt_get_hf_client_interface(void)