1 /******************************************************************************
2 *
3 * Copyright 2016 The Android Open Source Project
4 * Copyright 2005-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 /******************************************************************************
21 *
22 * This file contains the HID DEVICE API in the subsystem of BTA.
23 *
24 ******************************************************************************/
25
26 #define LOG_TAG "bluetooth"
27
28 // BTA_HD_INCLUDED
29 #include "bt_target.h" // Must be first to define build configuration
30 #if defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE)
31
32 #include "bta/hd/bta_hd_int.h"
33 #include "osi/include/allocator.h"
34 #include "osi/include/compat.h"
35 #include "osi/include/log.h"
36 #include "stack/include/bt_hdr.h"
37 #include "types/raw_address.h"
38
39 /*****************************************************************************
40 * Constants
41 ****************************************************************************/
42
43 static const tBTA_SYS_REG bta_hd_reg = {bta_hd_hdl_event, BTA_HdDisable};
44
45 /*******************************************************************************
46 *
47 * Function BTA_HdEnable
48 *
49 * Description Enables HID device
50 *
51 * Returns void
52 *
53 ******************************************************************************/
BTA_HdEnable(tBTA_HD_CBACK * p_cback)54 void BTA_HdEnable(tBTA_HD_CBACK* p_cback) {
55 APPL_TRACE_API("%s", __func__);
56
57 bta_sys_register(BTA_ID_HD, &bta_hd_reg);
58
59 tBTA_HD_API_ENABLE* p_buf =
60 (tBTA_HD_API_ENABLE*)osi_malloc((uint16_t)sizeof(tBTA_HD_API_ENABLE));
61
62 memset(p_buf, 0, sizeof(tBTA_HD_API_ENABLE));
63
64 p_buf->hdr.event = BTA_HD_API_ENABLE_EVT;
65 p_buf->p_cback = p_cback;
66
67 bta_sys_sendmsg(p_buf);
68 }
69
70 /*******************************************************************************
71 *
72 * Function BTA_HdDisable
73 *
74 * Description Disables HID device.
75 *
76 * Returns void
77 *
78 ******************************************************************************/
BTA_HdDisable(void)79 void BTA_HdDisable(void) {
80 APPL_TRACE_API("%s", __func__);
81
82 if (!bluetooth::common::init_flags::
83 delay_hidh_cleanup_until_hidh_ready_start_is_enabled()) {
84 bta_sys_deregister(BTA_ID_HD);
85 }
86
87 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
88 p_buf->event = BTA_HD_API_DISABLE_EVT;
89 bta_sys_sendmsg(p_buf);
90 }
91
92 /*******************************************************************************
93 *
94 * Function BTA_HdRegisterApp
95 *
96 * Description This function is called when application should be
97 *registered
98 *
99 * Returns void
100 *
101 ******************************************************************************/
BTA_HdRegisterApp(tBTA_HD_APP_INFO * p_app_info,tBTA_HD_QOS_INFO * p_in_qos,tBTA_HD_QOS_INFO * p_out_qos)102 void BTA_HdRegisterApp(tBTA_HD_APP_INFO* p_app_info, tBTA_HD_QOS_INFO* p_in_qos,
103 tBTA_HD_QOS_INFO* p_out_qos) {
104 APPL_TRACE_API("%s", __func__);
105
106 tBTA_HD_REGISTER_APP* p_buf =
107 (tBTA_HD_REGISTER_APP*)osi_malloc(sizeof(tBTA_HD_REGISTER_APP));
108 p_buf->hdr.event = BTA_HD_API_REGISTER_APP_EVT;
109
110 if (p_app_info->p_name) {
111 strlcpy(p_buf->name, p_app_info->p_name, BTA_HD_APP_NAME_LEN);
112 } else {
113 p_buf->name[0] = '\0';
114 }
115
116 if (p_app_info->p_description) {
117 strlcpy(p_buf->description, p_app_info->p_description,
118 BTA_HD_APP_DESCRIPTION_LEN);
119 } else {
120 p_buf->description[0] = '\0';
121 }
122
123 if (p_app_info->p_provider) {
124 strlcpy(p_buf->provider, p_app_info->p_provider, BTA_HD_APP_PROVIDER_LEN);
125 } else {
126 p_buf->provider[0] = '\0';
127 }
128
129 p_buf->subclass = p_app_info->subclass;
130
131 if (p_app_info->descriptor.dl_len > BTA_HD_APP_DESCRIPTOR_LEN) {
132 p_app_info->descriptor.dl_len = BTA_HD_APP_DESCRIPTOR_LEN;
133 }
134 p_buf->d_len = p_app_info->descriptor.dl_len;
135 memcpy(p_buf->d_data, p_app_info->descriptor.dsc_list,
136 p_app_info->descriptor.dl_len);
137
138 // copy qos data as-is
139 memcpy(&p_buf->in_qos, p_in_qos, sizeof(tBTA_HD_QOS_INFO));
140 memcpy(&p_buf->out_qos, p_out_qos, sizeof(tBTA_HD_QOS_INFO));
141
142 bta_sys_sendmsg(p_buf);
143 }
144
145 /*******************************************************************************
146 *
147 * Function BTA_HdUnregisterApp
148 *
149 * Description This function is called when application should be
150 *unregistered
151 *
152 * Returns void
153 *
154 ******************************************************************************/
BTA_HdUnregisterApp(void)155 void BTA_HdUnregisterApp(void) {
156 APPL_TRACE_API("%s", __func__);
157
158 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
159 p_buf->event = BTA_HD_API_UNREGISTER_APP_EVT;
160
161 bta_sys_sendmsg(p_buf);
162 }
163
164 /*******************************************************************************
165 *
166 * Function BTA_HdSendReport
167 *
168 * Description This function is called when report is to be sent
169 *
170 * Returns void
171 *
172 ******************************************************************************/
BTA_HdSendReport(tBTA_HD_REPORT * p_report)173 void BTA_HdSendReport(tBTA_HD_REPORT* p_report) {
174 APPL_TRACE_VERBOSE("%s", __func__);
175
176 if (p_report->len > BTA_HD_REPORT_LEN) {
177 APPL_TRACE_WARNING(
178 "%s, report len (%d) > MTU len (%d), can't send report."
179 " Increase value of HID_DEV_MTU_SIZE to send larger reports",
180 __func__, p_report->len, BTA_HD_REPORT_LEN);
181 return;
182 }
183
184 tBTA_HD_SEND_REPORT* p_buf =
185 (tBTA_HD_SEND_REPORT*)osi_malloc(sizeof(tBTA_HD_SEND_REPORT));
186 p_buf->hdr.event = BTA_HD_API_SEND_REPORT_EVT;
187
188 p_buf->use_intr = p_report->use_intr;
189 p_buf->type = p_report->type;
190 p_buf->id = p_report->id;
191 p_buf->len = p_report->len;
192 memcpy(p_buf->data, p_report->p_data, p_report->len);
193
194 bta_sys_sendmsg(p_buf);
195 }
196
197 /*******************************************************************************
198 *
199 * Function BTA_HdVirtualCableUnplug
200 *
201 * Description This function is called when VCU shall be sent
202 *
203 * Returns void
204 *
205 ******************************************************************************/
BTA_HdVirtualCableUnplug(void)206 void BTA_HdVirtualCableUnplug(void) {
207 APPL_TRACE_API("%s", __func__);
208
209 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
210 p_buf->event = BTA_HD_API_VC_UNPLUG_EVT;
211
212 bta_sys_sendmsg(p_buf);
213 }
214
215 /*******************************************************************************
216 *
217 * Function BTA_HdConnect
218 *
219 * Description This function is called when connection to host shall be
220 *made
221 *
222 * Returns void
223 *
224 ******************************************************************************/
BTA_HdConnect(const RawAddress & addr)225 void BTA_HdConnect(const RawAddress& addr) {
226 APPL_TRACE_API("%s", __func__);
227
228 tBTA_HD_DEVICE_CTRL* p_buf =
229 (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
230 p_buf->hdr.event = BTA_HD_API_CONNECT_EVT;
231
232 p_buf->addr = addr;
233
234 bta_sys_sendmsg(p_buf);
235 }
236
237 /*******************************************************************************
238 *
239 * Function BTA_HdDisconnect
240 *
241 * Description This function is called when host shall be disconnected
242 *
243 * Returns void
244 *
245 ******************************************************************************/
BTA_HdDisconnect(void)246 void BTA_HdDisconnect(void) {
247 APPL_TRACE_API("%s", __func__);
248 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
249 p_buf->event = BTA_HD_API_DISCONNECT_EVT;
250
251 bta_sys_sendmsg(p_buf);
252 }
253
254 /*******************************************************************************
255 *
256 * Function BTA_HdAddDevice
257 *
258 * Description This function is called when a device is virtually cabled
259 *
260 * Returns void
261 *
262 ******************************************************************************/
BTA_HdAddDevice(const RawAddress & addr)263 void BTA_HdAddDevice(const RawAddress& addr) {
264 APPL_TRACE_API("%s", __func__);
265 tBTA_HD_DEVICE_CTRL* p_buf =
266 (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
267 p_buf->hdr.event = BTA_HD_API_ADD_DEVICE_EVT;
268
269 p_buf->addr = addr;
270
271 bta_sys_sendmsg(p_buf);
272 }
273
274 /*******************************************************************************
275 *
276 * Function BTA_HdRemoveDevice
277 *
278 * Description This function is called when a device is virtually uncabled
279 *
280 * Returns void
281 *
282 ******************************************************************************/
BTA_HdRemoveDevice(const RawAddress & addr)283 void BTA_HdRemoveDevice(const RawAddress& addr) {
284 APPL_TRACE_API("%s", __func__);
285 tBTA_HD_DEVICE_CTRL* p_buf =
286 (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
287 p_buf->hdr.event = BTA_HD_API_REMOVE_DEVICE_EVT;
288
289 p_buf->addr = addr;
290
291 bta_sys_sendmsg(p_buf);
292 }
293
294 /*******************************************************************************
295 *
296 * Function BTA_HdReportError
297 *
298 * Description This function is called when reporting error for set report
299 *
300 * Returns void
301 *
302 ******************************************************************************/
BTA_HdReportError(uint8_t error)303 void BTA_HdReportError(uint8_t error) {
304 APPL_TRACE_API("%s", __func__);
305 tBTA_HD_REPORT_ERR* p_buf =
306 (tBTA_HD_REPORT_ERR*)osi_malloc(sizeof(tBTA_HD_REPORT_ERR));
307 p_buf->hdr.event = BTA_HD_API_REPORT_ERROR_EVT;
308 p_buf->error = error;
309
310 bta_sys_sendmsg(p_buf);
311 }
312
313 #endif /* BTA_HD_INCLUDED */
314