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 extern void BTA_HdRegisterApp(tBTA_HD_APP_INFO* p_app_info,
103 tBTA_HD_QOS_INFO* p_in_qos,
104 tBTA_HD_QOS_INFO* p_out_qos) {
105 APPL_TRACE_API("%s", __func__);
106
107 tBTA_HD_REGISTER_APP* p_buf =
108 (tBTA_HD_REGISTER_APP*)osi_malloc(sizeof(tBTA_HD_REGISTER_APP));
109 p_buf->hdr.event = BTA_HD_API_REGISTER_APP_EVT;
110
111 if (p_app_info->p_name) {
112 strlcpy(p_buf->name, p_app_info->p_name, BTA_HD_APP_NAME_LEN);
113 } else {
114 p_buf->name[0] = '\0';
115 }
116
117 if (p_app_info->p_description) {
118 strlcpy(p_buf->description, p_app_info->p_description,
119 BTA_HD_APP_DESCRIPTION_LEN);
120 } else {
121 p_buf->description[0] = '\0';
122 }
123
124 if (p_app_info->p_provider) {
125 strlcpy(p_buf->provider, p_app_info->p_provider, BTA_HD_APP_PROVIDER_LEN);
126 } else {
127 p_buf->provider[0] = '\0';
128 }
129
130 p_buf->subclass = p_app_info->subclass;
131
132 if (p_app_info->descriptor.dl_len > BTA_HD_APP_DESCRIPTOR_LEN) {
133 p_app_info->descriptor.dl_len = BTA_HD_APP_DESCRIPTOR_LEN;
134 }
135 p_buf->d_len = p_app_info->descriptor.dl_len;
136 memcpy(p_buf->d_data, p_app_info->descriptor.dsc_list,
137 p_app_info->descriptor.dl_len);
138
139 // copy qos data as-is
140 memcpy(&p_buf->in_qos, p_in_qos, sizeof(tBTA_HD_QOS_INFO));
141 memcpy(&p_buf->out_qos, p_out_qos, sizeof(tBTA_HD_QOS_INFO));
142
143 bta_sys_sendmsg(p_buf);
144 }
145
146 /*******************************************************************************
147 *
148 * Function BTA_HdUnregisterApp
149 *
150 * Description This function is called when application should be
151 *unregistered
152 *
153 * Returns void
154 *
155 ******************************************************************************/
BTA_HdUnregisterApp(void)156 extern void BTA_HdUnregisterApp(void) {
157 APPL_TRACE_API("%s", __func__);
158
159 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
160 p_buf->event = BTA_HD_API_UNREGISTER_APP_EVT;
161
162 bta_sys_sendmsg(p_buf);
163 }
164
165 /*******************************************************************************
166 *
167 * Function BTA_HdSendReport
168 *
169 * Description This function is called when report is to be sent
170 *
171 * Returns void
172 *
173 ******************************************************************************/
BTA_HdSendReport(tBTA_HD_REPORT * p_report)174 extern void BTA_HdSendReport(tBTA_HD_REPORT* p_report) {
175 APPL_TRACE_VERBOSE("%s", __func__);
176
177 if (p_report->len > BTA_HD_REPORT_LEN) {
178 APPL_TRACE_WARNING(
179 "%s, report len (%d) > MTU len (%d), can't send report."
180 " Increase value of HID_DEV_MTU_SIZE to send larger reports",
181 __func__, p_report->len, BTA_HD_REPORT_LEN);
182 return;
183 }
184
185 tBTA_HD_SEND_REPORT* p_buf =
186 (tBTA_HD_SEND_REPORT*)osi_malloc(sizeof(tBTA_HD_SEND_REPORT));
187 p_buf->hdr.event = BTA_HD_API_SEND_REPORT_EVT;
188
189 p_buf->use_intr = p_report->use_intr;
190 p_buf->type = p_report->type;
191 p_buf->id = p_report->id;
192 p_buf->len = p_report->len;
193 memcpy(p_buf->data, p_report->p_data, p_report->len);
194
195 bta_sys_sendmsg(p_buf);
196 }
197
198 /*******************************************************************************
199 *
200 * Function BTA_HdVirtualCableUnplug
201 *
202 * Description This function is called when VCU shall be sent
203 *
204 * Returns void
205 *
206 ******************************************************************************/
BTA_HdVirtualCableUnplug(void)207 extern void BTA_HdVirtualCableUnplug(void) {
208 APPL_TRACE_API("%s", __func__);
209
210 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
211 p_buf->event = BTA_HD_API_VC_UNPLUG_EVT;
212
213 bta_sys_sendmsg(p_buf);
214 }
215
216 /*******************************************************************************
217 *
218 * Function BTA_HdConnect
219 *
220 * Description This function is called when connection to host shall be
221 *made
222 *
223 * Returns void
224 *
225 ******************************************************************************/
BTA_HdConnect(const RawAddress & addr)226 extern void BTA_HdConnect(const RawAddress& addr) {
227 APPL_TRACE_API("%s", __func__);
228
229 tBTA_HD_DEVICE_CTRL* p_buf =
230 (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
231 p_buf->hdr.event = BTA_HD_API_CONNECT_EVT;
232
233 p_buf->addr = addr;
234
235 bta_sys_sendmsg(p_buf);
236 }
237
238 /*******************************************************************************
239 *
240 * Function BTA_HdDisconnect
241 *
242 * Description This function is called when host shall be disconnected
243 *
244 * Returns void
245 *
246 ******************************************************************************/
BTA_HdDisconnect(void)247 extern void BTA_HdDisconnect(void) {
248 APPL_TRACE_API("%s", __func__);
249 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
250 p_buf->event = BTA_HD_API_DISCONNECT_EVT;
251
252 bta_sys_sendmsg(p_buf);
253 }
254
255 /*******************************************************************************
256 *
257 * Function BTA_HdAddDevice
258 *
259 * Description This function is called when a device is virtually cabled
260 *
261 * Returns void
262 *
263 ******************************************************************************/
BTA_HdAddDevice(const RawAddress & addr)264 extern void BTA_HdAddDevice(const RawAddress& addr) {
265 APPL_TRACE_API("%s", __func__);
266 tBTA_HD_DEVICE_CTRL* p_buf =
267 (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
268 p_buf->hdr.event = BTA_HD_API_ADD_DEVICE_EVT;
269
270 p_buf->addr = addr;
271
272 bta_sys_sendmsg(p_buf);
273 }
274
275 /*******************************************************************************
276 *
277 * Function BTA_HdRemoveDevice
278 *
279 * Description This function is called when a device is virtually uncabled
280 *
281 * Returns void
282 *
283 ******************************************************************************/
BTA_HdRemoveDevice(const RawAddress & addr)284 extern void BTA_HdRemoveDevice(const RawAddress& addr) {
285 APPL_TRACE_API("%s", __func__);
286 tBTA_HD_DEVICE_CTRL* p_buf =
287 (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
288 p_buf->hdr.event = BTA_HD_API_REMOVE_DEVICE_EVT;
289
290 p_buf->addr = addr;
291
292 bta_sys_sendmsg(p_buf);
293 }
294
295 /*******************************************************************************
296 *
297 * Function BTA_HdReportError
298 *
299 * Description This function is called when reporting error for set report
300 *
301 * Returns void
302 *
303 ******************************************************************************/
BTA_HdReportError(uint8_t error)304 extern void BTA_HdReportError(uint8_t error) {
305 APPL_TRACE_API("%s", __func__);
306 tBTA_HD_REPORT_ERR* p_buf =
307 (tBTA_HD_REPORT_ERR*)osi_malloc(sizeof(tBTA_HD_REPORT_ERR));
308 p_buf->hdr.event = BTA_HD_API_REPORT_ERROR_EVT;
309 p_buf->error = error;
310
311 bta_sys_sendmsg(p_buf);
312 }
313
314 #endif /* BTA_HD_INCLUDED */
315