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 "bta_hh_api.h"
28
29 #include "bt_target.h"
30
31 #if (BTA_HH_INCLUDED == TRUE)
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "bta_hh_int.h"
38 #include "l2c_api.h"
39 #include "osi/include/log.h"
40 #include "osi/include/osi.h"
41 #include "utl.h"
42
43 /*****************************************************************************
44 * Constants
45 ****************************************************************************/
46
47 static const tBTA_SYS_REG bta_hh_reg = {bta_hh_hdl_event, BTA_HhDisable};
48
49 /*******************************************************************************
50 *
51 * Function BTA_HhEnable
52 *
53 * Description Enable the HID host. This function must be called before
54 * any other functions in the HID host API are called. When the
55 * enable operation is complete the callback function will be
56 * called with BTA_HH_ENABLE_EVT.
57 *
58 *
59 * Returns void
60 *
61 ******************************************************************************/
BTA_HhEnable(tBTA_SEC sec_mask,tBTA_HH_CBACK * p_cback)62 void BTA_HhEnable(tBTA_SEC sec_mask, tBTA_HH_CBACK* p_cback) {
63 tBTA_HH_API_ENABLE* p_buf =
64 (tBTA_HH_API_ENABLE*)osi_calloc(sizeof(tBTA_HH_API_ENABLE));
65
66 LOG_INFO("%s sec_mask:0x%x p_cback:%p", __func__, sec_mask, p_cback);
67
68 /* register with BTA system manager */
69 bta_sys_register(BTA_ID_HH, &bta_hh_reg);
70
71 p_buf->hdr.event = BTA_HH_API_ENABLE_EVT;
72 p_buf->p_cback = p_cback;
73 p_buf->sec_mask = sec_mask;
74
75 bta_sys_sendmsg(p_buf);
76 }
77
78 /*******************************************************************************
79 *
80 * Function BTA_HhDisable
81 *
82 * Description Disable the HID host. If the server is currently
83 * connected, the connection will be closed.
84 *
85 * Returns void
86 *
87 ******************************************************************************/
BTA_HhDisable(void)88 void BTA_HhDisable(void) {
89 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
90
91 bta_sys_deregister(BTA_ID_HH);
92 p_buf->event = BTA_HH_API_DISABLE_EVT;
93
94 bta_sys_sendmsg(p_buf);
95 }
96
97 /*******************************************************************************
98 *
99 * Function BTA_HhClose
100 *
101 * Description Disconnect a connection.
102 *
103 * Returns void
104 *
105 ******************************************************************************/
BTA_HhClose(uint8_t dev_handle)106 void BTA_HhClose(uint8_t dev_handle) {
107 BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));
108
109 p_buf->event = BTA_HH_API_CLOSE_EVT;
110 p_buf->layer_specific = (uint16_t)dev_handle;
111
112 bta_sys_sendmsg(p_buf);
113 }
114
115 /*******************************************************************************
116 *
117 * Function BTA_HhOpen
118 *
119 * Description Connect to a device of specified BD address in specified
120 * protocol mode and security level.
121 *
122 * Returns void
123 *
124 ******************************************************************************/
BTA_HhOpen(const RawAddress & dev_bda,tBTA_HH_PROTO_MODE mode,tBTA_SEC sec_mask)125 void BTA_HhOpen(const RawAddress& dev_bda, tBTA_HH_PROTO_MODE mode,
126 tBTA_SEC sec_mask) {
127 tBTA_HH_API_CONN* p_buf =
128 (tBTA_HH_API_CONN*)osi_calloc(sizeof(tBTA_HH_API_CONN));
129
130 p_buf->hdr.event = BTA_HH_API_OPEN_EVT;
131 p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
132 p_buf->sec_mask = sec_mask;
133 p_buf->mode = mode;
134 p_buf->bd_addr = dev_bda;
135
136 bta_sys_sendmsg((void*)p_buf);
137 }
138
139 /*******************************************************************************
140 *
141 * Function bta_hh_snd_write_dev
142 *
143 ******************************************************************************/
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)144 static void bta_hh_snd_write_dev(uint8_t dev_handle, uint8_t t_type,
145 uint8_t param, uint16_t data, uint8_t rpt_id,
146 BT_HDR* p_data) {
147 tBTA_HH_CMD_DATA* p_buf =
148 (tBTA_HH_CMD_DATA*)osi_calloc(sizeof(tBTA_HH_CMD_DATA));
149
150 p_buf->hdr.event = BTA_HH_API_WRITE_DEV_EVT;
151 p_buf->hdr.layer_specific = (uint16_t)dev_handle;
152 p_buf->t_type = t_type;
153 p_buf->data = data;
154 p_buf->param = param;
155 p_buf->p_data = p_data;
156 p_buf->rpt_id = rpt_id;
157
158 bta_sys_sendmsg(p_buf);
159 }
160
161 /*******************************************************************************
162 *
163 * Function BTA_HhSetReport
164 *
165 * Description send SET_REPORT to device.
166 *
167 * Parameter dev_handle: device handle
168 * r_type: report type, could be BTA_HH_RPTT_OUTPUT or
169 * BTA_HH_RPTT_FEATURE.
170 * Returns void
171 *
172 ******************************************************************************/
BTA_HhSetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,BT_HDR * p_data)173 void BTA_HhSetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
174 BT_HDR* p_data) {
175 /* send feature report on control channel */
176 if (r_type == BTA_HH_RPTT_FEATURE)
177 bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_REPORT, r_type, 0, 0,
178 p_data);
179 /* send output data report on interrupt channel */
180 else
181 bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA, r_type, 0, 0, p_data);
182 }
183 /*******************************************************************************
184 *
185 * Function BTA_HhGetReport
186 *
187 * Description Send a GET_REPORT to HID device.
188 *
189 * Returns void
190 *
191 ******************************************************************************/
BTA_HhGetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,uint8_t rpt_id,uint16_t buf_size)192 void BTA_HhGetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
193 uint8_t rpt_id, uint16_t buf_size) {
194 uint8_t param = (buf_size) ? (r_type | 0x08) : r_type;
195
196 bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_REPORT, param, buf_size,
197 rpt_id, NULL);
198 }
199 /*******************************************************************************
200 *
201 * Function BTA_HhSetProtoMode
202 *
203 * Description This function set the protocol mode at specified HID handle
204 *
205 * Returns void
206 *
207 ******************************************************************************/
BTA_HhSetProtoMode(uint8_t dev_handle,tBTA_HH_PROTO_MODE p_type)208 void BTA_HhSetProtoMode(uint8_t dev_handle, tBTA_HH_PROTO_MODE p_type) {
209 bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_PROTOCOL, (uint8_t)p_type, 0,
210 0, NULL);
211 }
212 /*******************************************************************************
213 *
214 * Function BTA_HhGetProtoMode
215 *
216 * Description This function get protocol mode information.
217 *
218 * Returns void
219 *
220 ******************************************************************************/
BTA_HhGetProtoMode(uint8_t dev_handle)221 void BTA_HhGetProtoMode(uint8_t dev_handle) {
222 bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_PROTOCOL, 0, 0, 0, NULL);
223 }
224 /*******************************************************************************
225 *
226 * Function BTA_HhSetIdle
227 *
228 * Description send SET_IDLE to device.
229 *
230 * Returns void
231 *
232 ******************************************************************************/
BTA_HhSetIdle(uint8_t dev_handle,uint16_t idle_rate)233 void BTA_HhSetIdle(uint8_t dev_handle, uint16_t idle_rate) {
234 bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_IDLE, 0, idle_rate, 0, NULL);
235 }
236
237 /*******************************************************************************
238 *
239 * Function BTA_HhGetIdle
240 *
241 * Description Send a GET_IDLE from HID device.
242 *
243 * Returns void
244 *
245 ******************************************************************************/
BTA_HhGetIdle(uint8_t dev_handle)246 void BTA_HhGetIdle(uint8_t dev_handle) {
247 bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_IDLE, 0, 0, 0, NULL);
248 }
249 /*******************************************************************************
250 *
251 * Function BTA_HhSendCtrl
252 *
253 * Description Send a control command to HID device.
254 *
255 * Returns void
256 *
257 ******************************************************************************/
BTA_HhSendCtrl(uint8_t dev_handle,tBTA_HH_TRANS_CTRL_TYPE c_type)258 void BTA_HhSendCtrl(uint8_t dev_handle, tBTA_HH_TRANS_CTRL_TYPE c_type) {
259 bta_hh_snd_write_dev(dev_handle, HID_TRANS_CONTROL, (uint8_t)c_type, 0, 0,
260 NULL);
261 }
262 /*******************************************************************************
263 *
264 * Function BTA_HhSendData
265 *
266 * Description This function send DATA transaction to HID device.
267 *
268 * Parameter dev_handle: device handle
269 * dev_bda: remote device address
270 * p_data: data to be sent in the DATA transaction; or
271 * the data to be write into the Output Report of a LE
272 * HID device. The report is identified the report ID
273 * which is the value of the byte
274 * (uint8_t *)(p_buf + 1) + *p_buf->offset.
275 * p_data->layer_specific needs to be set to the report
276 * type. It can be OUTPUT report, or FEATURE report.
277 *
278 * Returns void
279 *
280 ******************************************************************************/
BTA_HhSendData(uint8_t dev_handle,UNUSED_ATTR const RawAddress & dev_bda,BT_HDR * p_data)281 void BTA_HhSendData(uint8_t dev_handle, UNUSED_ATTR const RawAddress& dev_bda,
282 BT_HDR* p_data) {
283 #if (BTA_HH_LE_INCLUDED == TRUE)
284 if (p_data->layer_specific != BTA_HH_RPTT_OUTPUT) {
285 APPL_TRACE_ERROR(
286 "ERROR! Wrong report type! Write Command only valid for output "
287 "report!");
288 return;
289 }
290 #endif
291 bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA,
292 (uint8_t)p_data->layer_specific, 0, 0, p_data);
293 }
294
295 /*******************************************************************************
296 *
297 * Function BTA_HhGetDscpInfo
298 *
299 * Description Get HID device report descriptor
300 *
301 * Returns void
302 *
303 ******************************************************************************/
BTA_HhGetDscpInfo(uint8_t dev_handle)304 void BTA_HhGetDscpInfo(uint8_t dev_handle) {
305 BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));
306
307 p_buf->event = BTA_HH_API_GET_DSCP_EVT;
308 p_buf->layer_specific = (uint16_t)dev_handle;
309
310 bta_sys_sendmsg(p_buf);
311 }
312
313 /*******************************************************************************
314 *
315 * Function BTA_HhAddDev
316 *
317 * Description Add a virtually cabled device into HID-Host device list
318 * to manage and assign a device handle for future API call,
319 * host applciation call this API at start-up to initialize its
320 * virtually cabled devices.
321 *
322 * Returns void
323 *
324 ******************************************************************************/
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)325 void BTA_HhAddDev(const RawAddress& bda, tBTA_HH_ATTR_MASK attr_mask,
326 uint8_t sub_class, uint8_t app_id,
327 tBTA_HH_DEV_DSCP_INFO dscp_info) {
328 size_t len = sizeof(tBTA_HH_MAINT_DEV) + dscp_info.descriptor.dl_len;
329 tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(len);
330
331 p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
332 p_buf->sub_event = BTA_HH_ADD_DEV_EVT;
333 p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
334
335 p_buf->attr_mask = (uint16_t)attr_mask;
336 p_buf->sub_class = sub_class;
337 p_buf->app_id = app_id;
338 p_buf->bda = bda;
339
340 memcpy(&p_buf->dscp_info, &dscp_info, sizeof(tBTA_HH_DEV_DSCP_INFO));
341 if (dscp_info.descriptor.dl_len != 0 && dscp_info.descriptor.dsc_list) {
342 p_buf->dscp_info.descriptor.dl_len = dscp_info.descriptor.dl_len;
343 p_buf->dscp_info.descriptor.dsc_list = (uint8_t*)(p_buf + 1);
344 memcpy(p_buf->dscp_info.descriptor.dsc_list, dscp_info.descriptor.dsc_list,
345 dscp_info.descriptor.dl_len);
346 } else {
347 p_buf->dscp_info.descriptor.dsc_list = NULL;
348 p_buf->dscp_info.descriptor.dl_len = 0;
349 }
350
351 bta_sys_sendmsg(p_buf);
352 }
353
354 /*******************************************************************************
355 *
356 * Function BTA_HhRemoveDev
357 *
358 * Description Remove a device from the HID host devices list.
359 *
360 * Returns void
361 *
362 ******************************************************************************/
BTA_HhRemoveDev(uint8_t dev_handle)363 void BTA_HhRemoveDev(uint8_t dev_handle) {
364 tBTA_HH_MAINT_DEV* p_buf =
365 (tBTA_HH_MAINT_DEV*)osi_calloc(sizeof(tBTA_HH_MAINT_DEV));
366
367 p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
368 p_buf->sub_event = BTA_HH_RMV_DEV_EVT;
369 p_buf->hdr.layer_specific = (uint16_t)dev_handle;
370
371 bta_sys_sendmsg(p_buf);
372 }
373
374 /******************************************************************************/
375 /* Utility Function */
376 /******************************************************************************/
377
378 /*******************************************************************************
379 *
380 * Function BTA_HhParseBootRpt
381 *
382 * Description This utility function parse a boot mode report.
383 * For keyboard report, report data will carry the keycode max
384 * up to 6 key press in one report. Application need to convert
385 * the keycode into keypress character according to keyboard
386 * language.
387 *
388 * Returns void
389 *
390 ******************************************************************************/
BTA_HhParseBootRpt(tBTA_HH_BOOT_RPT * p_data,uint8_t * p_report,uint16_t report_len)391 void BTA_HhParseBootRpt(tBTA_HH_BOOT_RPT* p_data, uint8_t* p_report,
392 uint16_t report_len) {
393 p_data->dev_type = BTA_HH_DEVT_UNKNOWN;
394
395 if (p_report) {
396 /* first byte is report ID */
397 switch (p_report[0]) {
398 case BTA_HH_KEYBD_RPT_ID: /* key board report ID */
399 p_data->dev_type = p_report[0];
400 bta_hh_parse_keybd_rpt(p_data, p_report + 1,
401 (uint16_t)(report_len - 1));
402 break;
403
404 case BTA_HH_MOUSE_RPT_ID: /* mouse report ID */
405 p_data->dev_type = p_report[0];
406 bta_hh_parse_mice_rpt(p_data, p_report + 1, (uint16_t)(report_len - 1));
407 break;
408
409 default:
410 APPL_TRACE_DEBUG("Unknown boot report: %d", p_report[0]);
411 ;
412 break;
413 }
414 }
415
416 return;
417 }
418
419 #endif /* BTA_HH_INCLUDED */
420