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.
27 static const btav_callbacks_t *cbs = NULL;
29 static bool interface_ready(void)
34 static void handle_conn_state(void *buf, uint16_t len, int fd)
36 struct hal_ev_a2dp_conn_state *ev = buf;
38 if (cbs->connection_state_cb)
39 cbs->connection_state_cb(ev->state,
40 (bt_bdaddr_t *) (ev->bdaddr));
43 static void handle_audio_state(void *buf, uint16_t len, int fd)
45 struct hal_ev_a2dp_audio_state *ev = buf;
47 if (cbs->audio_state_cb)
48 cbs->audio_state_cb(ev->state, (bt_bdaddr_t *)(ev->bdaddr));
51 static void handle_audio_config(void *buf, uint16_t len, int fd)
53 struct hal_ev_a2dp_audio_config *ev = buf;
55 if (cbs->audio_config_cb)
56 cbs->audio_config_cb((bt_bdaddr_t *)(ev->bdaddr),
57 ev->sample_rate, ev->channel_count);
61 * handlers will be called from notification thread context,
62 * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
64 static const struct hal_ipc_handler ev_handlers[] = {
65 /* HAL_EV_A2DP_CONN_STATE */
66 { handle_conn_state, false, sizeof(struct hal_ev_a2dp_conn_state) },
67 /* HAL_EV_A2DP_AUDIO_STATE */
68 { handle_audio_state, false, sizeof(struct hal_ev_a2dp_audio_state) },
69 /* HAL_EV_A2DP_AUDIO_CONFIG */
70 { handle_audio_config, false, sizeof(struct hal_ev_a2dp_audio_config) },
73 static bt_status_t a2dp_connect(bt_bdaddr_t *bd_addr)
75 struct hal_cmd_a2dp_connect cmd;
79 if (!interface_ready())
80 return BT_STATUS_NOT_READY;
82 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
84 return hal_ipc_cmd(HAL_SERVICE_ID_A2DP_SINK, HAL_OP_A2DP_CONNECT,
85 sizeof(cmd), &cmd, NULL, NULL, NULL);
88 static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
90 struct hal_cmd_a2dp_disconnect cmd;
94 if (!interface_ready())
95 return BT_STATUS_NOT_READY;
97 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
99 return hal_ipc_cmd(HAL_SERVICE_ID_A2DP_SINK, HAL_OP_A2DP_DISCONNECT,
100 sizeof(cmd), &cmd, NULL, NULL, NULL);
103 static bt_status_t init(btav_callbacks_t *callbacks)
105 struct hal_cmd_register_module cmd;
110 if (interface_ready())
111 return BT_STATUS_DONE;
115 hal_ipc_register(HAL_SERVICE_ID_A2DP_SINK, ev_handlers,
116 sizeof(ev_handlers)/sizeof(ev_handlers[0]));
118 cmd.service_id = HAL_SERVICE_ID_A2DP_SINK;
119 cmd.mode = HAL_MODE_DEFAULT;
122 ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
123 sizeof(cmd), &cmd, NULL, NULL, NULL);
125 if (ret != BT_STATUS_SUCCESS) {
127 hal_ipc_unregister(HAL_SERVICE_ID_A2DP_SINK);
133 static void cleanup(void)
135 struct hal_cmd_unregister_module cmd;
139 if (!interface_ready())
142 cmd.service_id = HAL_SERVICE_ID_A2DP_SINK;
144 hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
145 sizeof(cmd), &cmd, NULL, NULL, NULL);
147 hal_ipc_unregister(HAL_SERVICE_ID_A2DP_SINK);
152 static btav_interface_t iface = {
153 .size = sizeof(iface),
155 .connect = a2dp_connect,
156 .disconnect = disconnect,
160 btav_interface_t *bt_get_a2dp_sink_interface(void)