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 <bluetooth/log.h>
30 
31 #include <cstddef>
32 #include <cstdint>
33 #include <cstring>
34 
35 #include "bta/hh/bta_hh_int.h"
36 #include "bta/sys/bta_sys.h"
37 #include "hiddefs.h"
38 #include "osi/include/allocator.h"
39 #include "stack/include/bt_hdr.h"
40 #include "stack/include/main_thread.h"
41 #include "types/ble_address_with_type.h"
42 #include "types/bluetooth/uuid.h"
43 
44 using namespace bluetooth;
45 
46 /*****************************************************************************
47  *  Constants
48  ****************************************************************************/
49 
50 /**
51  * Android Headtracker Service UUIDs
52  */
53 const Uuid ANDROID_HEADTRACKER_SERVICE_UUID =
54         Uuid::FromString(ANDROID_HEADTRACKER_SERVICE_UUID_STRING);
55 const Uuid ANDROID_HEADTRACKER_VERSION_CHARAC_UUID =
56         Uuid::FromString(ANDROID_HEADTRACKER_VERSION_CHARAC_UUID_STRING);
57 const Uuid ANDROID_HEADTRACKER_CONTROL_CHARAC_UUID =
58         Uuid::FromString(ANDROID_HEADTRACKER_CONTROL_CHARAC_UUID_STRING);
59 const Uuid ANDROID_HEADTRACKER_REPORT_CHARAC_UUID =
60         Uuid::FromString(ANDROID_HEADTRACKER_REPORT_CHARAC_UUID_STRING);
61 
62 static const tBTA_SYS_REG bta_hh_reg = {bta_hh_hdl_event, BTA_HhDisable};
63 
64 /*******************************************************************************
65  *
66  * Function         BTA_HhEnable
67  *
68  * Description      Enable the HID host.  This function must be called before
69  *                  any other functions in the HID host API are called. When the
70  *                  enable operation is complete the callback function will be
71  *                  called with BTA_HH_ENABLE_EVT.
72  *
73  *
74  * Returns          void
75  *
76  ******************************************************************************/
BTA_HhEnable(tBTA_HH_CBACK * p_cback,bool enable_hid,bool enable_hogp)77 void BTA_HhEnable(tBTA_HH_CBACK* p_cback, bool enable_hid, bool enable_hogp) {
78   /* register with BTA system manager */
79   bta_sys_register(BTA_ID_HH, &bta_hh_reg);
80 
81   post_on_bt_main([p_cback, enable_hid, enable_hogp]() {
82     bta_hh_api_enable(p_cback, enable_hid, enable_hogp);
83   });
84 }
85 
86 /*******************************************************************************
87  *
88  * Function         BTA_HhDisable
89  *
90  * Description      Disable the HID host. If the server is currently
91  *                  connected, the connection will be closed.
92  *
93  * Returns          void
94  *
95  ******************************************************************************/
BTA_HhDisable(void)96 void BTA_HhDisable(void) {
97   bta_sys_deregister(BTA_ID_HH);
98 
99   post_on_bt_main([]() { bta_hh_api_disable(); });
100 }
101 
102 /*******************************************************************************
103  *
104  * Function         BTA_HhClose
105  *
106  * Description      Disconnect a connection.
107  *
108  * Returns          void
109  *
110  ******************************************************************************/
BTA_HhClose(uint8_t dev_handle)111 void BTA_HhClose(uint8_t dev_handle) {
112   BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));
113 
114   p_buf->event = BTA_HH_API_CLOSE_EVT;
115   p_buf->layer_specific = (uint16_t)dev_handle;
116 
117   bta_sys_sendmsg(p_buf);
118 }
119 
120 /*******************************************************************************
121  *
122  * Function         BTA_HhOpen
123  *
124  * Description      Connect to a device of specified BD address in specified
125  *                  protocol mode and security level.
126  *
127  * Returns          void
128  *
129  ******************************************************************************/
BTA_HhOpen(const tAclLinkSpec & link_spec)130 void BTA_HhOpen(const tAclLinkSpec& link_spec) {
131   tBTA_HH_API_CONN* p_buf = (tBTA_HH_API_CONN*)osi_calloc(sizeof(tBTA_HH_API_CONN));
132   tBTA_HH_PROTO_MODE mode = BTA_HH_PROTO_RPT_MODE;
133 
134   p_buf->hdr.event = BTA_HH_API_OPEN_EVT;
135   p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
136   p_buf->mode = mode;
137   p_buf->link_spec = link_spec;
138 
139   bta_sys_sendmsg((void*)p_buf);
140 }
141 
142 /*******************************************************************************
143  *
144  * Function  bta_hh_snd_write_dev
145  *
146  ******************************************************************************/
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)147 static void bta_hh_snd_write_dev(uint8_t dev_handle, uint8_t t_type, uint8_t param, uint16_t data,
148                                  uint8_t rpt_id, BT_HDR* p_data) {
149   tBTA_HH_CMD_DATA* p_buf = (tBTA_HH_CMD_DATA*)osi_calloc(sizeof(tBTA_HH_CMD_DATA));
150 
151   p_buf->hdr.event = BTA_HH_API_WRITE_DEV_EVT;
152   p_buf->hdr.layer_specific = (uint16_t)dev_handle;
153   p_buf->t_type = t_type;
154   p_buf->data = data;
155   p_buf->param = param;
156   p_buf->p_data = p_data;
157   p_buf->rpt_id = rpt_id;
158 
159   bta_sys_sendmsg(p_buf);
160 }
161 
162 /*******************************************************************************
163  *
164  * Function         BTA_HhSetReport
165  *
166  * Description      send SET_REPORT to device.
167  *
168  * Parameter        dev_handle: device handle
169  *                  r_type:     report type, could be BTA_HH_RPTT_OUTPUT or
170  *                              BTA_HH_RPTT_FEATURE.
171  * Returns          void
172  *
173  ******************************************************************************/
BTA_HhSetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,BT_HDR * p_data)174 void BTA_HhSetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type, BT_HDR* p_data) {
175   bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_REPORT, r_type, 0, 0, p_data);
176 }
177 /*******************************************************************************
178  *
179  * Function         BTA_HhGetReport
180  *
181  * Description      Send a GET_REPORT to HID device.
182  *
183  * Returns          void
184  *
185  ******************************************************************************/
BTA_HhGetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,uint8_t rpt_id,uint16_t buf_size)186 void BTA_HhGetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type, uint8_t rpt_id,
187                      uint16_t buf_size) {
188   uint8_t param = (buf_size) ? (r_type | 0x08) : r_type;
189 
190   bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_REPORT, param, buf_size, 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, 0, NULL);
203 }
204 /*******************************************************************************
205  *
206  * Function         BTA_HhGetProtoMode
207  *
208  * Description      This function get protocol mode information.
209  *
210  * Returns          void
211  *
212  ******************************************************************************/
BTA_HhGetProtoMode(uint8_t dev_handle)213 void BTA_HhGetProtoMode(uint8_t dev_handle) {
214   bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_PROTOCOL, 0, 0, 0, NULL);
215 }
216 /*******************************************************************************
217  *
218  * Function         BTA_HhSetIdle
219  *
220  * Description      send SET_IDLE to device.
221  *
222  * Returns          void
223  *
224  ******************************************************************************/
BTA_HhSetIdle(uint8_t dev_handle,uint16_t idle_rate)225 void BTA_HhSetIdle(uint8_t dev_handle, uint16_t idle_rate) {
226   bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_IDLE, 0, idle_rate, 0, NULL);
227 }
228 
229 /*******************************************************************************
230  *
231  * Function         BTA_HhGetIdle
232  *
233  * Description      Send a GET_IDLE from HID device.
234  *
235  * Returns          void
236  *
237  ******************************************************************************/
BTA_HhGetIdle(uint8_t dev_handle)238 void BTA_HhGetIdle(uint8_t dev_handle) {
239   bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_IDLE, 0, 0, 0, NULL);
240 }
241 /*******************************************************************************
242  *
243  * Function         BTA_HhSendCtrl
244  *
245  * Description      Send a control command to HID device.
246  *
247  * Returns          void
248  *
249  ******************************************************************************/
BTA_HhSendCtrl(uint8_t dev_handle,tBTA_HH_TRANS_CTRL_TYPE c_type)250 void BTA_HhSendCtrl(uint8_t dev_handle, tBTA_HH_TRANS_CTRL_TYPE c_type) {
251   bta_hh_snd_write_dev(dev_handle, HID_TRANS_CONTROL, (uint8_t)c_type, 0, 0, NULL);
252 }
253 /*******************************************************************************
254  *
255  * Function         BTA_HhSendData
256  *
257  * Description      This function send DATA transaction to HID device.
258  *
259  * Parameter        dev_handle: device handle
260  *                  link_spec : remote device acl link specification
261  *                  p_data: data to be sent in the DATA transaction; or
262  *                          the data to be write into the Output Report of a LE
263  *                          HID device. The report is identified the report ID
264  *                          which is the value of the byte
265  *                          (uint8_t *)(p_buf + 1) + *p_buf->offset.
266  *                          p_data->layer_specific needs to be set to the report
267  *                          type. It can be OUTPUT report, or FEATURE report.
268  *
269  * Returns          void
270  *
271  ******************************************************************************/
BTA_HhSendData(uint8_t dev_handle,const tAclLinkSpec &,BT_HDR * p_data)272 void BTA_HhSendData(uint8_t dev_handle, const tAclLinkSpec& /* link_spec */, BT_HDR* p_data) {
273   if (p_data->layer_specific != BTA_HH_RPTT_OUTPUT) {
274     log::error("ERROR! Wrong report type! Write Command only valid for output report!");
275     return;
276   }
277   bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA, (uint8_t)p_data->layer_specific, 0, 0, p_data);
278 }
279 
280 /*******************************************************************************
281  *
282  * Function         BTA_HhGetDscpInfo
283  *
284  * Description      Get HID device report descriptor
285  *
286  * Returns          void
287  *
288  ******************************************************************************/
BTA_HhGetDscpInfo(uint8_t dev_handle)289 void BTA_HhGetDscpInfo(uint8_t dev_handle) {
290   BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));
291 
292   p_buf->event = BTA_HH_API_GET_DSCP_EVT;
293   p_buf->layer_specific = (uint16_t)dev_handle;
294 
295   bta_sys_sendmsg(p_buf);
296 }
297 
298 /*******************************************************************************
299  *
300  * Function         BTA_HhAddDev
301  *
302  * Description      Add a virtually cabled device into HID-Host device list
303  *                  to manage and assign a device handle for future API call,
304  *                  host applciation call this API at start-up to initialize its
305  *                  virtually cabled devices.
306  *
307  * Returns          void
308  *
309  ******************************************************************************/
BTA_HhAddDev(const tAclLinkSpec & link_spec,tBTA_HH_ATTR_MASK attr_mask,uint8_t sub_class,uint8_t app_id,tBTA_HH_DEV_DSCP_INFO dscp_info)310 void BTA_HhAddDev(const tAclLinkSpec& link_spec, tBTA_HH_ATTR_MASK attr_mask, uint8_t sub_class,
311                   uint8_t app_id, tBTA_HH_DEV_DSCP_INFO dscp_info) {
312   size_t len = sizeof(tBTA_HH_MAINT_DEV) + dscp_info.descriptor.dl_len;
313   tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(len);
314 
315   p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
316   p_buf->sub_event = BTA_HH_ADD_DEV_EVT;
317   p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
318 
319   p_buf->attr_mask = (uint16_t)attr_mask;
320   p_buf->sub_class = sub_class;
321   p_buf->app_id = app_id;
322   p_buf->link_spec = link_spec;
323 
324   memcpy(&p_buf->dscp_info, &dscp_info, sizeof(tBTA_HH_DEV_DSCP_INFO));
325   if (dscp_info.descriptor.dl_len != 0 && dscp_info.descriptor.dsc_list) {
326     p_buf->dscp_info.descriptor.dl_len = dscp_info.descriptor.dl_len;
327     p_buf->dscp_info.descriptor.dsc_list = (uint8_t*)(p_buf + 1);
328     memcpy(p_buf->dscp_info.descriptor.dsc_list, dscp_info.descriptor.dsc_list,
329            dscp_info.descriptor.dl_len);
330   } else {
331     p_buf->dscp_info.descriptor.dsc_list = NULL;
332     p_buf->dscp_info.descriptor.dl_len = 0;
333   }
334 
335   bta_sys_sendmsg(p_buf);
336 }
337 
338 /*******************************************************************************
339  *
340  * Function         BTA_HhRemoveDev
341  *
342  * Description      Remove a device from the HID host devices list.
343  *
344  * Returns          void
345  *
346  ******************************************************************************/
BTA_HhRemoveDev(uint8_t dev_handle)347 void BTA_HhRemoveDev(uint8_t dev_handle) {
348   tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(sizeof(tBTA_HH_MAINT_DEV));
349 
350   p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
351   p_buf->sub_event = BTA_HH_RMV_DEV_EVT;
352   p_buf->hdr.layer_specific = (uint16_t)dev_handle;
353 
354   bta_sys_sendmsg(p_buf);
355 }
356 
357 /*******************************************************************************
358  *
359  * Function         BTA_HhDump
360  *
361  * Description      Dump BTA HH control block
362  *
363  * Returns          void
364  *
365  ******************************************************************************/
BTA_HhDump(int fd)366 void BTA_HhDump(int fd) { bta_hh_dump(fd); }
367