2 * Copyright (C) 2013 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 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
54 struct hal_ev_a2dp_audio_config *ev = buf;
56 if (cbs->audio_config_cb)
57 cbs->audio_config_cb((bt_bdaddr_t *)(ev->bdaddr),
58 ev->sample_rate, ev->channel_count);
63 * handlers will be called from notification thread context,
64 * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
66 static const struct hal_ipc_handler ev_handlers[] = {
67 /* HAL_EV_A2DP_CONN_STATE */
68 { handle_conn_state, false, sizeof(struct hal_ev_a2dp_conn_state) },
69 /* HAL_EV_A2DP_AUDIO_STATE */
70 { handle_audio_state, false, sizeof(struct hal_ev_a2dp_audio_state) },
71 /* HAL_EV_A2DP_AUDIO_CONFIG */
72 { handle_audio_config, false, sizeof(struct hal_ev_a2dp_audio_config) },
75 static bt_status_t a2dp_connect(bt_bdaddr_t *bd_addr)
77 struct hal_cmd_a2dp_connect cmd;
81 if (!interface_ready())
82 return BT_STATUS_NOT_READY;
84 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
86 return hal_ipc_cmd(HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_CONNECT,
87 sizeof(cmd), &cmd, NULL, NULL, NULL);
90 static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
92 struct hal_cmd_a2dp_disconnect cmd;
96 if (!interface_ready())
97 return BT_STATUS_NOT_READY;
99 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
101 return hal_ipc_cmd(HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_DISCONNECT,
102 sizeof(cmd), &cmd, NULL, NULL, NULL);
105 static bt_status_t init(btav_callbacks_t *callbacks)
107 struct hal_cmd_register_module cmd;
112 if (interface_ready())
113 return BT_STATUS_DONE;
117 hal_ipc_register(HAL_SERVICE_ID_A2DP, ev_handlers,
118 sizeof(ev_handlers)/sizeof(ev_handlers[0]));
120 cmd.service_id = HAL_SERVICE_ID_A2DP;
121 cmd.mode = HAL_MODE_DEFAULT;
124 ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
125 sizeof(cmd), &cmd, NULL, NULL, NULL);
127 if (ret != BT_STATUS_SUCCESS) {
129 hal_ipc_unregister(HAL_SERVICE_ID_A2DP);
135 static void cleanup(void)
137 struct hal_cmd_unregister_module cmd;
141 if (!interface_ready())
144 cmd.service_id = HAL_SERVICE_ID_A2DP;
146 hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
147 sizeof(cmd), &cmd, NULL, NULL, NULL);
149 hal_ipc_unregister(HAL_SERVICE_ID_A2DP);
154 static btav_interface_t iface = {
155 .size = sizeof(iface),
157 .connect = a2dp_connect,
158 .disconnect = disconnect,
162 btav_interface_t *bt_get_a2dp_interface(void)