1 /******************************************************************************
2 *
3 * Copyright 2005-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This file contains the HID HOST API in the subsystem of BTA.
22 *
23 ******************************************************************************/
24
25 #define LOG_TAG "bt_bta_hh"
26
27 #include <cstdint>
28
29 #include "bt_target.h" // Must be first to define build configuration
30 #include "bta/hh/bta_hh_int.h"
31 #include "bta/sys/bta_sys.h"
32 #include "osi/include/allocator.h"
33 #include "osi/include/osi.h" // UNUSED_ATTR
34 #include "stack/include/bt_hdr.h"
35 #include "stack/include/btu.h"
36 #include "types/raw_address.h"
37
38 /*****************************************************************************
39 * Constants
40 ****************************************************************************/
41
42 static const tBTA_SYS_REG bta_hh_reg = {bta_hh_hdl_event, BTA_HhDisable};
43
44 /*******************************************************************************
45 *
46 * Function BTA_HhEnable
47 *
48 * Description Enable the HID host. This function must be called before
49 * any other functions in the HID host API are called. When the
50 * enable operation is complete the callback function will be
51 * called with BTA_HH_ENABLE_EVT.
52 *
53 *
54 * Returns void
55 *
56 ******************************************************************************/
BTA_HhEnable(tBTA_HH_CBACK * p_cback)57 void BTA_HhEnable(tBTA_HH_CBACK* p_cback) {
58 /* register with BTA system manager */
59 bta_sys_register(BTA_ID_HH, &bta_hh_reg);
60
61 post_on_bt_main([p_cback]() {
62 tBTA_HH_DATA data = {
63 .api_enable =
64 {
65 .hdr =
66 {
67 .event = BTA_HH_API_ENABLE_EVT,
68 },
69 .p_cback = p_cback,
70 },
71 };
72 bta_hh_api_enable(&data);
73 });
74 }
75
76 /*******************************************************************************
77 *
78 * Function BTA_HhDisable
79 *
80 * Description Disable the HID host. If the server is currently
81 * connected, the connection will be closed.
82 *
83 * Returns void
84 *
85 ******************************************************************************/
BTA_HhDisable(void)86 void BTA_HhDisable(void) {
87 bta_sys_deregister(BTA_ID_HH);
88
89 post_on_bt_main([]() { bta_hh_api_disable(); });
90 }
91
92 /*******************************************************************************
93 *
94 * Function BTA_HhClose
95 *
96 * Description Disconnect a connection.
97 *
98 * Returns void
99 *
100 ******************************************************************************/
BTA_HhClose(uint8_t dev_handle)101 void BTA_HhClose(uint8_t dev_handle) {
102 BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));
103
104 p_buf->event = BTA_HH_API_CLOSE_EVT;
105 p_buf->layer_specific = (uint16_t)dev_handle;
106
107 bta_sys_sendmsg(p_buf);
108 }
109
110 /*******************************************************************************
111 *
112 * Function BTA_HhOpen
113 *
114 * Description Connect to a device of specified BD address in specified
115 * protocol mode and security level.
116 *
117 * Returns void
118 *
119 ******************************************************************************/
BTA_HhOpen(const RawAddress & dev_bda)120 void BTA_HhOpen(const RawAddress& dev_bda) {
121 tBTA_HH_API_CONN* p_buf =
122 (tBTA_HH_API_CONN*)osi_calloc(sizeof(tBTA_HH_API_CONN));
123 tBTA_HH_PROTO_MODE mode = BTA_HH_PROTO_RPT_MODE;
124
125 p_buf->hdr.event = BTA_HH_API_OPEN_EVT;
126 p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
127 p_buf->mode = mode;
128 p_buf->bd_addr = dev_bda;
129
130 bta_sys_sendmsg((void*)p_buf);
131 }
132
133 /*******************************************************************************
134 *
135 * Function bta_hh_snd_write_dev
136 *
137 ******************************************************************************/
bta_hh_snd_write_dev(uint8_t dev_handle,uint8_t t_type,uint8_t param,uint16_t data,uint8_t rpt_id,BT_HDR * p_data)138 static void bta_hh_snd_write_dev(uint8_t dev_handle, uint8_t t_type,
139 uint8_t param, uint16_t data, uint8_t rpt_id,
140 BT_HDR* p_data) {
141 tBTA_HH_CMD_DATA* p_buf =
142 (tBTA_HH_CMD_DATA*)osi_calloc(sizeof(tBTA_HH_CMD_DATA));
143
144 p_buf->hdr.event = BTA_HH_API_WRITE_DEV_EVT;
145 p_buf->hdr.layer_specific = (uint16_t)dev_handle;
146 p_buf->t_type = t_type;
147 p_buf->data = data;
148 p_buf->param = param;
149 p_buf->p_data = p_data;
150 p_buf->rpt_id = rpt_id;
151
152 bta_sys_sendmsg(p_buf);
153 }
154
155 /*******************************************************************************
156 *
157 * Function BTA_HhSetReport
158 *
159 * Description send SET_REPORT to device.
160 *
161 * Parameter dev_handle: device handle
162 * r_type: report type, could be BTA_HH_RPTT_OUTPUT or
163 * BTA_HH_RPTT_FEATURE.
164 * Returns void
165 *
166 ******************************************************************************/
BTA_HhSetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,BT_HDR * p_data)167 void BTA_HhSetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
168 BT_HDR* p_data) {
169 bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_REPORT, r_type, 0, 0, p_data);
170 }
171 /*******************************************************************************
172 *
173 * Function BTA_HhGetReport
174 *
175 * Description Send a GET_REPORT to HID device.
176 *
177 * Returns void
178 *
179 ******************************************************************************/
BTA_HhGetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,uint8_t rpt_id,uint16_t buf_size)180 void BTA_HhGetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
181 uint8_t rpt_id, uint16_t buf_size) {
182 uint8_t param = (buf_size) ? (r_type | 0x08) : r_type;
183
184 bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_REPORT, param, buf_size,
185 rpt_id, NULL);
186 }
187 /*******************************************************************************
188 *
189 * Function BTA_HhSetProtoMode
190 *
191 * Description This function set the protocol mode at specified HID handle
192 *
193 * Returns void
194 *
195 ******************************************************************************/
BTA_HhSetProtoMode(uint8_t dev_handle,tBTA_HH_PROTO_MODE p_type)196 void BTA_HhSetProtoMode(uint8_t dev_handle, tBTA_HH_PROTO_MODE p_type) {
197 bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_PROTOCOL, (uint8_t)p_type, 0,
198 0, NULL);
199 }
200 /*******************************************************************************
201 *
202 * Function BTA_HhGetProtoMode
203 *
204 * Description This function get protocol mode information.
205 *
206 * Returns void
207 *
208 ******************************************************************************/
BTA_HhGetProtoMode(uint8_t dev_handle)209 void BTA_HhGetProtoMode(uint8_t dev_handle) {
210 bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_PROTOCOL, 0, 0, 0, NULL);
211 }
212 /*******************************************************************************
213 *
214 * Function BTA_HhSetIdle
215 *
216 * Description send SET_IDLE to device.
217 *
218 * Returns void
219 *
220 ******************************************************************************/
BTA_HhSetIdle(uint8_t dev_handle,uint16_t idle_rate)221 void BTA_HhSetIdle(uint8_t dev_handle, uint16_t idle_rate) {
222 bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_IDLE, 0, idle_rate, 0, NULL);
223 }
224
225 /*******************************************************************************
226 *
227 * Function BTA_HhGetIdle
228 *
229 * Description Send a GET_IDLE from HID device.
230 *
231 * Returns void
232 *
233 ******************************************************************************/
BTA_HhGetIdle(uint8_t dev_handle)234 void BTA_HhGetIdle(uint8_t dev_handle) {
235 bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_IDLE, 0, 0, 0, NULL);
236 }
237 /*******************************************************************************
238 *
239 * Function BTA_HhSendCtrl
240 *
241 * Description Send a control command to HID device.
242 *
243 * Returns void
244 *
245 ******************************************************************************/
BTA_HhSendCtrl(uint8_t dev_handle,tBTA_HH_TRANS_CTRL_TYPE c_type)246 void BTA_HhSendCtrl(uint8_t dev_handle, tBTA_HH_TRANS_CTRL_TYPE c_type) {
247 bta_hh_snd_write_dev(dev_handle, HID_TRANS_CONTROL, (uint8_t)c_type, 0, 0,
248 NULL);
249 }
250 /*******************************************************************************
251 *
252 * Function BTA_HhSendData
253 *
254 * Description This function send DATA transaction to HID device.
255 *
256 * Parameter dev_handle: device handle
257 * dev_bda: remote device address
258 * p_data: data to be sent in the DATA transaction; or
259 * the data to be write into the Output Report of a LE
260 * HID device. The report is identified the report ID
261 * which is the value of the byte
262 * (uint8_t *)(p_buf + 1) + *p_buf->offset.
263 * p_data->layer_specific needs to be set to the report
264 * type. It can be OUTPUT report, or FEATURE report.
265 *
266 * Returns void
267 *
268 ******************************************************************************/
BTA_HhSendData(uint8_t dev_handle,UNUSED_ATTR const RawAddress & dev_bda,BT_HDR * p_data)269 void BTA_HhSendData(uint8_t dev_handle, UNUSED_ATTR const RawAddress& dev_bda,
270 BT_HDR* p_data) {
271 if (p_data->layer_specific != BTA_HH_RPTT_OUTPUT) {
272 APPL_TRACE_ERROR(
273 "ERROR! Wrong report type! Write Command only valid for output "
274 "report!");
275 return;
276 }
277 bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA,
278 (uint8_t)p_data->layer_specific, 0, 0, p_data);
279 }
280
281 /*******************************************************************************
282 *
283 * Function BTA_HhGetDscpInfo
284 *
285 * Description Get HID device report descriptor
286 *
287 * Returns void
288 *
289 ******************************************************************************/
BTA_HhGetDscpInfo(uint8_t dev_handle)290 void BTA_HhGetDscpInfo(uint8_t dev_handle) {
291 BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));
292
293 p_buf->event = BTA_HH_API_GET_DSCP_EVT;
294 p_buf->layer_specific = (uint16_t)dev_handle;
295
296 bta_sys_sendmsg(p_buf);
297 }
298
299 /*******************************************************************************
300 *
301 * Function BTA_HhAddDev
302 *
303 * Description Add a virtually cabled device into HID-Host device list
304 * to manage and assign a device handle for future API call,
305 * host applciation call this API at start-up to initialize its
306 * virtually cabled devices.
307 *
308 * Returns void
309 *
310 ******************************************************************************/
BTA_HhAddDev(const RawAddress & bda,tBTA_HH_ATTR_MASK attr_mask,uint8_t sub_class,uint8_t app_id,tBTA_HH_DEV_DSCP_INFO dscp_info)311 void BTA_HhAddDev(const RawAddress& bda, tBTA_HH_ATTR_MASK attr_mask,
312 uint8_t sub_class, uint8_t app_id,
313 tBTA_HH_DEV_DSCP_INFO dscp_info) {
314 size_t len = sizeof(tBTA_HH_MAINT_DEV) + dscp_info.descriptor.dl_len;
315 tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(len);
316
317 p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
318 p_buf->sub_event = BTA_HH_ADD_DEV_EVT;
319 p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
320
321 p_buf->attr_mask = (uint16_t)attr_mask;
322 p_buf->sub_class = sub_class;
323 p_buf->app_id = app_id;
324 p_buf->bda = bda;
325
326 memcpy(&p_buf->dscp_info, &dscp_info, sizeof(tBTA_HH_DEV_DSCP_INFO));
327 if (dscp_info.descriptor.dl_len != 0 && dscp_info.descriptor.dsc_list) {
328 p_buf->dscp_info.descriptor.dl_len = dscp_info.descriptor.dl_len;
329 p_buf->dscp_info.descriptor.dsc_list = (uint8_t*)(p_buf + 1);
330 memcpy(p_buf->dscp_info.descriptor.dsc_list, dscp_info.descriptor.dsc_list,
331 dscp_info.descriptor.dl_len);
332 } else {
333 p_buf->dscp_info.descriptor.dsc_list = NULL;
334 p_buf->dscp_info.descriptor.dl_len = 0;
335 }
336
337 bta_sys_sendmsg(p_buf);
338 }
339
340 /*******************************************************************************
341 *
342 * Function BTA_HhRemoveDev
343 *
344 * Description Remove a device from the HID host devices list.
345 *
346 * Returns void
347 *
348 ******************************************************************************/
BTA_HhRemoveDev(uint8_t dev_handle)349 void BTA_HhRemoveDev(uint8_t dev_handle) {
350 tBTA_HH_MAINT_DEV* p_buf =
351 (tBTA_HH_MAINT_DEV*)osi_calloc(sizeof(tBTA_HH_MAINT_DEV));
352
353 p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
354 p_buf->sub_event = BTA_HH_RMV_DEV_EVT;
355 p_buf->hdr.layer_specific = (uint16_t)dev_handle;
356
357 bta_sys_sendmsg(p_buf);
358 }
359