Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / android / hal-avrcp-ctrl.c
1 /*
2  * Copyright (C) 2014 Intel Corporation
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include "hal-utils.h"
24 #include "hal-log.h"
25 #include "hal.h"
26 #include "hal-msg.h"
27 #include "ipc-common.h"
28 #include "hal-ipc.h"
29
30 static const btrc_ctrl_callbacks_t *cbs = NULL;
31
32 static bool interface_ready(void)
33 {
34         return cbs != NULL;
35 }
36
37 static void handle_connection_state(void *buf, uint16_t len, int fd)
38 {
39         struct hal_ev_avrcp_ctrl_conn_state *ev = buf;
40
41         if (cbs->connection_state_cb)
42                 cbs->connection_state_cb(ev->state,
43                                                 (bt_bdaddr_t *) (ev->bdaddr));
44 }
45
46 static void handle_passthrough_rsp(void *buf, uint16_t len, int fd)
47 {
48         struct hal_ev_avrcp_ctrl_passthrough_rsp *ev = buf;
49
50         if (cbs->passthrough_rsp_cb)
51                 cbs->passthrough_rsp_cb(ev->id, ev->key_state);
52 }
53
54 /*
55  * handlers will be called from notification thread context,
56  * index in table equals to 'opcode - HAL_MINIMUM_EVENT'
57  */
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) },
65 };
66
67 static bt_status_t init(btrc_ctrl_callbacks_t *callbacks)
68 {
69         struct hal_cmd_register_module cmd;
70         int ret;
71
72         DBG("");
73
74         if (interface_ready())
75                 return BT_STATUS_DONE;
76
77         cbs = callbacks;
78
79         hal_ipc_register(HAL_SERVICE_ID_AVRCP_CTRL, ev_handlers,
80                                 sizeof(ev_handlers) / sizeof(ev_handlers[0]));
81
82         cmd.service_id = HAL_SERVICE_ID_AVRCP_CTRL;
83         cmd.mode = HAL_MODE_DEFAULT;
84         cmd.max_clients = 1;
85
86         ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
87                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
88
89         if (ret != BT_STATUS_SUCCESS) {
90                 cbs = NULL;
91                 hal_ipc_unregister(HAL_SERVICE_ID_AVRCP_CTRL);
92         }
93
94         return ret;
95 }
96
97 static bt_status_t send_pass_through_cmd(bt_bdaddr_t *bd_addr, uint8_t key_code,
98                                                         uint8_t key_state)
99 {
100         struct hal_cmd_avrcp_ctrl_send_passthrough cmd;
101
102         DBG("");
103
104         if (!interface_ready())
105                 return BT_STATUS_NOT_READY;
106
107         memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
108         cmd.key_code = key_code;
109         cmd.key_state = key_state;
110
111         return hal_ipc_cmd(HAL_SERVICE_ID_AVRCP_CTRL,
112                                         HAL_OP_AVRCP_CTRL_SEND_PASSTHROUGH,
113                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
114 }
115
116 static void cleanup(void)
117 {
118         struct hal_cmd_unregister_module cmd;
119
120         DBG("");
121
122         if (!interface_ready())
123                 return;
124
125         cmd.service_id = HAL_SERVICE_ID_AVRCP_CTRL;
126
127         hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
128                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
129
130         hal_ipc_unregister(HAL_SERVICE_ID_AVRCP_CTRL);
131
132         cbs = NULL;
133 }
134
135 static btrc_ctrl_interface_t iface = {
136         .size = sizeof(iface),
137         .init = init,
138         .send_pass_through_cmd = send_pass_through_cmd,
139         .cleanup = cleanup
140 };
141
142 btrc_ctrl_interface_t *bt_get_avrcp_ctrl_interface(void)
143 {
144         return &iface;
145 }