1 /******************************************************************************
2  *
3  *  Copyright (C) 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(LOG_TAG, "%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(BD_ADDR dev_bda,tBTA_HH_PROTO_MODE mode,tBTA_SEC sec_mask)125 void BTA_HhOpen(BD_ADDR dev_bda, tBTA_HH_PROTO_MODE mode, tBTA_SEC sec_mask) {
126   tBTA_HH_API_CONN* p_buf =
127       (tBTA_HH_API_CONN*)osi_calloc(sizeof(tBTA_HH_API_CONN));
128 
129   p_buf->hdr.event = BTA_HH_API_OPEN_EVT;
130   p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
131   p_buf->sec_mask = sec_mask;
132   p_buf->mode = mode;
133   bdcpy(p_buf->bd_addr, dev_bda);
134 
135   bta_sys_sendmsg((void*)p_buf);
136 }
137 
138 /*******************************************************************************
139  *
140  * Function  bta_hh_snd_write_dev
141  *
142  ******************************************************************************/
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)143 static void bta_hh_snd_write_dev(uint8_t dev_handle, uint8_t t_type,
144                                  uint8_t param, uint16_t data, uint8_t rpt_id,
145                                  BT_HDR* p_data) {
146   tBTA_HH_CMD_DATA* p_buf =
147       (tBTA_HH_CMD_DATA*)osi_calloc(sizeof(tBTA_HH_CMD_DATA));
148 
149   p_buf->hdr.event = BTA_HH_API_WRITE_DEV_EVT;
150   p_buf->hdr.layer_specific = (uint16_t)dev_handle;
151   p_buf->t_type = t_type;
152   p_buf->data = data;
153   p_buf->param = param;
154   p_buf->p_data = p_data;
155   p_buf->rpt_id = rpt_id;
156 
157   bta_sys_sendmsg(p_buf);
158 }
159 
160 /*******************************************************************************
161  *
162  * Function         BTA_HhSetReport
163  *
164  * Description      send SET_REPORT to device.
165  *
166  * Parameter        dev_handle: device handle
167  *                  r_type:     report type, could be BTA_HH_RPTT_OUTPUT or
168  *                              BTA_HH_RPTT_FEATURE.
169  * Returns          void
170  *
171  ******************************************************************************/
BTA_HhSetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,BT_HDR * p_data)172 void BTA_HhSetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
173                      BT_HDR* p_data) {
174   bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_REPORT, r_type, 0, 0, p_data);
175 }
176 /*******************************************************************************
177  *
178  * Function         BTA_HhGetReport
179  *
180  * Description      Send a GET_REPORT to HID device.
181  *
182  * Returns          void
183  *
184  ******************************************************************************/
BTA_HhGetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,uint8_t rpt_id,uint16_t buf_size)185 void BTA_HhGetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
186                      uint8_t rpt_id, uint16_t buf_size) {
187   uint8_t param = (buf_size) ? (r_type | 0x08) : r_type;
188 
189   bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_REPORT, param, buf_size,
190                        rpt_id, NULL);
191 }
192 /*******************************************************************************
193  *
194  * Function         BTA_HhSetProtoMode
195  *
196  * Description      This function set the protocol mode at specified HID handle
197  *
198  * Returns          void
199  *
200  ******************************************************************************/
BTA_HhSetProtoMode(uint8_t dev_handle,tBTA_HH_PROTO_MODE p_type)201 void BTA_HhSetProtoMode(uint8_t dev_handle, tBTA_HH_PROTO_MODE p_type) {
202   bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_PROTOCOL, (uint8_t)p_type, 0,
203                        0, NULL);
204 }
205 /*******************************************************************************
206  *
207  * Function         BTA_HhGetProtoMode
208  *
209  * Description      This function get protocol mode information.
210  *
211  * Returns          void
212  *
213  ******************************************************************************/
BTA_HhGetProtoMode(uint8_t dev_handle)214 void BTA_HhGetProtoMode(uint8_t dev_handle) {
215   bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_PROTOCOL, 0, 0, 0, NULL);
216 }
217 /*******************************************************************************
218  *
219  * Function         BTA_HhSetIdle
220  *
221  * Description      send SET_IDLE to device.
222  *
223  * Returns          void
224  *
225  ******************************************************************************/
BTA_HhSetIdle(uint8_t dev_handle,uint16_t idle_rate)226 void BTA_HhSetIdle(uint8_t dev_handle, uint16_t idle_rate) {
227   bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_IDLE, 0, idle_rate, 0, NULL);
228 }
229 
230 /*******************************************************************************
231  *
232  * Function         BTA_HhGetIdle
233  *
234  * Description      Send a GET_IDLE from HID device.
235  *
236  * Returns          void
237  *
238  ******************************************************************************/
BTA_HhGetIdle(uint8_t dev_handle)239 void BTA_HhGetIdle(uint8_t dev_handle) {
240   bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_IDLE, 0, 0, 0, NULL);
241 }
242 /*******************************************************************************
243  *
244  * Function         BTA_HhSendCtrl
245  *
246  * Description      Send a control command to HID device.
247  *
248  * Returns          void
249  *
250  ******************************************************************************/
BTA_HhSendCtrl(uint8_t dev_handle,tBTA_HH_TRANS_CTRL_TYPE c_type)251 void BTA_HhSendCtrl(uint8_t dev_handle, tBTA_HH_TRANS_CTRL_TYPE c_type) {
252   bta_hh_snd_write_dev(dev_handle, HID_TRANS_CONTROL, (uint8_t)c_type, 0, 0,
253                        NULL);
254 }
255 /*******************************************************************************
256  *
257  * Function         BTA_HhSendData
258  *
259  * Description      This function send DATA transaction to HID device.
260  *
261  * Parameter        dev_handle: device handle
262  *                  dev_bda: remote device address
263  *                  p_data: data to be sent in the DATA transaction; or
264  *                          the data to be write into the Output Report of a LE
265  *                          HID device. The report is identified the report ID
266  *                          which is the value of the byte
267  *                          (uint8_t *)(p_buf + 1) + *p_buf->offset.
268  *                          p_data->layer_specific needs to be set to the report
269  *                          type. It can be OUTPUT report, or FEATURE report.
270  *
271  * Returns          void
272  *
273  ******************************************************************************/
BTA_HhSendData(uint8_t dev_handle,UNUSED_ATTR BD_ADDR dev_bda,BT_HDR * p_data)274 void BTA_HhSendData(uint8_t dev_handle, UNUSED_ATTR BD_ADDR dev_bda,
275                     BT_HDR* p_data) {
276 #if (BTA_HH_LE_INCLUDED == TRUE)
277   if (p_data->layer_specific != BTA_HH_RPTT_OUTPUT) {
278     APPL_TRACE_ERROR(
279         "ERROR! Wrong report type! Write Command only valid for output "
280         "report!");
281     return;
282   }
283 #endif
284   bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA,
285                        (uint8_t)p_data->layer_specific, 0, 0, p_data);
286 }
287 
288 /*******************************************************************************
289  *
290  * Function         BTA_HhGetDscpInfo
291  *
292  * Description      Get HID device report descriptor
293  *
294  * Returns          void
295  *
296  ******************************************************************************/
BTA_HhGetDscpInfo(uint8_t dev_handle)297 void BTA_HhGetDscpInfo(uint8_t dev_handle) {
298   BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));
299 
300   p_buf->event = BTA_HH_API_GET_DSCP_EVT;
301   p_buf->layer_specific = (uint16_t)dev_handle;
302 
303   bta_sys_sendmsg(p_buf);
304 }
305 
306 /*******************************************************************************
307  *
308  * Function         BTA_HhAddDev
309  *
310  * Description      Add a virtually cabled device into HID-Host device list
311  *                  to manage and assign a device handle for future API call,
312  *                  host applciation call this API at start-up to initialize its
313  *                  virtually cabled devices.
314  *
315  * Returns          void
316  *
317  ******************************************************************************/
BTA_HhAddDev(BD_ADDR bda,tBTA_HH_ATTR_MASK attr_mask,uint8_t sub_class,uint8_t app_id,tBTA_HH_DEV_DSCP_INFO dscp_info)318 void BTA_HhAddDev(BD_ADDR bda, tBTA_HH_ATTR_MASK attr_mask, uint8_t sub_class,
319                   uint8_t app_id, tBTA_HH_DEV_DSCP_INFO dscp_info) {
320   size_t len = sizeof(tBTA_HH_MAINT_DEV) + dscp_info.descriptor.dl_len;
321   tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(len);
322 
323   p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
324   p_buf->sub_event = BTA_HH_ADD_DEV_EVT;
325   p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
326 
327   p_buf->attr_mask = (uint16_t)attr_mask;
328   p_buf->sub_class = sub_class;
329   p_buf->app_id = app_id;
330   bdcpy(p_buf->bda, bda);
331 
332   memcpy(&p_buf->dscp_info, &dscp_info, sizeof(tBTA_HH_DEV_DSCP_INFO));
333   if (dscp_info.descriptor.dl_len != 0 && dscp_info.descriptor.dsc_list) {
334     p_buf->dscp_info.descriptor.dl_len = dscp_info.descriptor.dl_len;
335     p_buf->dscp_info.descriptor.dsc_list = (uint8_t*)(p_buf + 1);
336     memcpy(p_buf->dscp_info.descriptor.dsc_list, dscp_info.descriptor.dsc_list,
337            dscp_info.descriptor.dl_len);
338   } else {
339     p_buf->dscp_info.descriptor.dsc_list = NULL;
340     p_buf->dscp_info.descriptor.dl_len = 0;
341   }
342 
343   bta_sys_sendmsg(p_buf);
344 }
345 
346 /*******************************************************************************
347  *
348  * Function         BTA_HhRemoveDev
349  *
350  * Description      Remove a device from the HID host devices list.
351  *
352  * Returns          void
353  *
354  ******************************************************************************/
BTA_HhRemoveDev(uint8_t dev_handle)355 void BTA_HhRemoveDev(uint8_t dev_handle) {
356   tBTA_HH_MAINT_DEV* p_buf =
357       (tBTA_HH_MAINT_DEV*)osi_calloc(sizeof(tBTA_HH_MAINT_DEV));
358 
359   p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
360   p_buf->sub_event = BTA_HH_RMV_DEV_EVT;
361   p_buf->hdr.layer_specific = (uint16_t)dev_handle;
362 
363   bta_sys_sendmsg(p_buf);
364 }
365 
366 /******************************************************************************/
367 /*                          Utility Function */
368 /******************************************************************************/
369 
370 /*******************************************************************************
371  *
372  * Function         BTA_HhParseBootRpt
373  *
374  * Description      This utility function parse a boot mode report.
375  *                  For keyboard report, report data will carry the keycode max
376  *                  up to 6 key press in one report. Application need to convert
377  *                  the keycode into keypress character according to keyboard
378  *                  language.
379  *
380  * Returns          void
381  *
382  ******************************************************************************/
BTA_HhParseBootRpt(tBTA_HH_BOOT_RPT * p_data,uint8_t * p_report,uint16_t report_len)383 void BTA_HhParseBootRpt(tBTA_HH_BOOT_RPT* p_data, uint8_t* p_report,
384                         uint16_t report_len) {
385   p_data->dev_type = BTA_HH_DEVT_UNKNOWN;
386 
387   if (p_report) {
388     /* first byte is report ID */
389     switch (p_report[0]) {
390       case BTA_HH_KEYBD_RPT_ID: /* key board report ID */
391         p_data->dev_type = p_report[0];
392         bta_hh_parse_keybd_rpt(p_data, p_report + 1,
393                                (uint16_t)(report_len - 1));
394         break;
395 
396       case BTA_HH_MOUSE_RPT_ID: /* mouse report ID */
397         p_data->dev_type = p_report[0];
398         bta_hh_parse_mice_rpt(p_data, p_report + 1, (uint16_t)(report_len - 1));
399         break;
400 
401       default:
402         APPL_TRACE_DEBUG("Unknown boot report: %d", p_report[0]);
403         ;
404         break;
405     }
406   }
407 
408   return;
409 }
410 
411 #endif /* BTA_HH_INCLUDED */
412