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 "hal-utils.h"
27 #include "ipc-common.h"
30 static const btrc_ctrl_callbacks_t *cbs = NULL;
32 static bool interface_ready(void)
37 static void handle_connection_state(void *buf, uint16_t len, int fd)
39 struct hal_ev_avrcp_ctrl_conn_state *ev = buf;
41 if (cbs->connection_state_cb)
42 cbs->connection_state_cb(ev->state,
43 (bt_bdaddr_t *) (ev->bdaddr));
46 static void handle_passthrough_rsp(void *buf, uint16_t len, int fd)
48 struct hal_ev_avrcp_ctrl_passthrough_rsp *ev = buf;
50 if (cbs->passthrough_rsp_cb)
51 cbs->passthrough_rsp_cb(ev->id, ev->key_state);
55 * handlers will be called from notification thread context,
56 * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
58 static const struct hal_ipc_handler ev_handlers[] = {
59 /* HAL_EV_AVRCP_CTRL_CONN_STATE */
60 { handle_connection_state, false,
61 sizeof(struct hal_ev_avrcp_ctrl_conn_state) },
62 /* HAL_EV_AVRCP_CTRL_PASSTHROUGH_RSP */
63 { handle_passthrough_rsp, false,
64 sizeof(struct hal_ev_avrcp_ctrl_passthrough_rsp) },
67 static bt_status_t init(btrc_ctrl_callbacks_t *callbacks)
69 struct hal_cmd_register_module cmd;
74 if (interface_ready())
75 return BT_STATUS_DONE;
79 hal_ipc_register(HAL_SERVICE_ID_AVRCP_CTRL, ev_handlers,
80 sizeof(ev_handlers) / sizeof(ev_handlers[0]));
82 cmd.service_id = HAL_SERVICE_ID_AVRCP_CTRL;
83 cmd.mode = HAL_MODE_DEFAULT;
86 ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
87 sizeof(cmd), &cmd, NULL, NULL, NULL);
89 if (ret != BT_STATUS_SUCCESS) {
91 hal_ipc_unregister(HAL_SERVICE_ID_AVRCP_CTRL);
97 static bt_status_t send_pass_through_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code,
100 struct hal_cmd_avrcp_ctrl_send_passthrough cmd;
104 if (!interface_ready())
105 return BT_STATUS_NOT_READY;
107 memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
108 cmd.key_code = key_code;
109 cmd.key_state = key_state;
111 return hal_ipc_cmd(HAL_SERVICE_ID_AVRCP_CTRL,
112 HAL_OP_AVRCP_CTRL_SEND_PASSTHROUGH,
113 sizeof(cmd), &cmd, NULL, NULL, NULL);
116 static void cleanup(void)
118 struct hal_cmd_unregister_module cmd;
122 if (!interface_ready())
125 cmd.service_id = HAL_SERVICE_ID_AVRCP_CTRL;
127 hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
128 sizeof(cmd), &cmd, NULL, NULL, NULL);
130 hal_ipc_unregister(HAL_SERVICE_ID_AVRCP_CTRL);
135 static btrc_ctrl_interface_t iface = {
136 .size = sizeof(iface),
138 .send_pass_through_cmd = send_pass_through_cmd,
142 btrc_ctrl_interface_t *bt_get_avrcp_ctrl_interface(void)