• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,bool direct)130 void BTA_HhOpen(const tAclLinkSpec& link_spec, bool direct) {
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   p_buf->direct = direct;
139 
140   bta_sys_sendmsg((void*)p_buf);
141 }
142 
143 /*******************************************************************************
144  *
145  * Function  bta_hh_snd_write_dev
146  *
147  ******************************************************************************/
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)148 static void bta_hh_snd_write_dev(uint8_t dev_handle, uint8_t t_type, uint8_t param, uint16_t data,
149                                  uint8_t rpt_id, BT_HDR* p_data) {
150   tBTA_HH_CMD_DATA* p_buf = (tBTA_HH_CMD_DATA*)osi_calloc(sizeof(tBTA_HH_CMD_DATA));
151 
152   p_buf->hdr.event = BTA_HH_API_WRITE_DEV_EVT;
153   p_buf->hdr.layer_specific = (uint16_t)dev_handle;
154   p_buf->t_type = t_type;
155   p_buf->data = data;
156   p_buf->param = param;
157   p_buf->p_data = p_data;
158   p_buf->rpt_id = rpt_id;
159 
160   bta_sys_sendmsg(p_buf);
161 }
162 
163 /*******************************************************************************
164  *
165  * Function         BTA_HhSetReport
166  *
167  * Description      send SET_REPORT to device.
168  *
169  * Parameter        dev_handle: device handle
170  *                  r_type:     report type, could be BTA_HH_RPTT_OUTPUT or
171  *                              BTA_HH_RPTT_FEATURE.
172  * Returns          void
173  *
174  ******************************************************************************/
BTA_HhSetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,BT_HDR * p_data)175 void BTA_HhSetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type, BT_HDR* p_data) {
176   bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_REPORT, r_type, 0, 0, p_data);
177 }
178 /*******************************************************************************
179  *
180  * Function         BTA_HhGetReport
181  *
182  * Description      Send a GET_REPORT to HID device.
183  *
184  * Returns          void
185  *
186  ******************************************************************************/
BTA_HhGetReport(uint8_t dev_handle,tBTA_HH_RPT_TYPE r_type,uint8_t rpt_id,uint16_t buf_size)187 void BTA_HhGetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type, uint8_t rpt_id,
188                      uint16_t buf_size) {
189   uint8_t param = (buf_size) ? (r_type | 0x08) : r_type;
190 
191   bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_REPORT, param, buf_size, rpt_id, NULL);
192 }
193 /*******************************************************************************
194  *
195  * Function         BTA_HhSetProtoMode
196  *
197  * Description      This function set the protocol mode at specified HID handle
198  *
199  * Returns          void
200  *
201  ******************************************************************************/
BTA_HhSetProtoMode(uint8_t dev_handle,tBTA_HH_PROTO_MODE p_type)202 void BTA_HhSetProtoMode(uint8_t dev_handle, tBTA_HH_PROTO_MODE p_type) {
203   bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_PROTOCOL, (uint8_t)p_type, 0, 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, NULL);
253 }
254 /*******************************************************************************
255  *
256  * Function         BTA_HhSendData
257  *
258  * Description      This function send DATA transaction to HID device.
259  *
260  * Parameter        dev_handle: device handle
261  *                  link_spec : remote device acl link specification
262  *                  p_data: data to be sent in the DATA transaction; or
263  *                          the data to be write into the Output Report of a LE
264  *                          HID device. The report is identified the report ID
265  *                          which is the value of the byte
266  *                          (uint8_t *)(p_buf + 1) + *p_buf->offset.
267  *                          p_data->layer_specific needs to be set to the report
268  *                          type. It can be OUTPUT report, or FEATURE report.
269  *
270  * Returns          void
271  *
272  ******************************************************************************/
BTA_HhSendData(uint8_t dev_handle,const tAclLinkSpec &,BT_HDR * p_data)273 void BTA_HhSendData(uint8_t dev_handle, const tAclLinkSpec& /* link_spec */, BT_HDR* p_data) {
274   if (p_data->layer_specific != BTA_HH_RPTT_OUTPUT) {
275     log::error("ERROR! Wrong report type! Write Command only valid for output report!");
276     return;
277   }
278   bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA, (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 tAclLinkSpec & link_spec,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 tAclLinkSpec& link_spec, tBTA_HH_ATTR_MASK attr_mask, uint8_t sub_class,
312                   uint8_t app_id, tBTA_HH_DEV_DSCP_INFO dscp_info) {
313   size_t len = sizeof(tBTA_HH_MAINT_DEV) + dscp_info.descriptor.dl_len;
314   tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(len);
315 
316   p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
317   p_buf->sub_event = BTA_HH_ADD_DEV_EVT;
318   p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
319 
320   p_buf->attr_mask = (uint16_t)attr_mask;
321   p_buf->sub_class = sub_class;
322   p_buf->app_id = app_id;
323   p_buf->link_spec = link_spec;
324 
325   memcpy(&p_buf->dscp_info, &dscp_info, sizeof(tBTA_HH_DEV_DSCP_INFO));
326   if (dscp_info.descriptor.dl_len != 0 && dscp_info.descriptor.dsc_list) {
327     p_buf->dscp_info.descriptor.dl_len = dscp_info.descriptor.dl_len;
328     p_buf->dscp_info.descriptor.dsc_list = (uint8_t*)(p_buf + 1);
329     memcpy(p_buf->dscp_info.descriptor.dsc_list, dscp_info.descriptor.dsc_list,
330            dscp_info.descriptor.dl_len);
331   } else {
332     p_buf->dscp_info.descriptor.dsc_list = NULL;
333     p_buf->dscp_info.descriptor.dl_len = 0;
334   }
335 
336   bta_sys_sendmsg(p_buf);
337 }
338 
339 /*******************************************************************************
340  *
341  * Function         BTA_HhRemoveDev
342  *
343  * Description      Remove a device from the HID host devices list.
344  *
345  * Returns          void
346  *
347  ******************************************************************************/
BTA_HhRemoveDev(uint8_t dev_handle)348 void BTA_HhRemoveDev(uint8_t dev_handle) {
349   tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(sizeof(tBTA_HH_MAINT_DEV));
350 
351   p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
352   p_buf->sub_event = BTA_HH_RMV_DEV_EVT;
353   p_buf->hdr.layer_specific = (uint16_t)dev_handle;
354 
355   bta_sys_sendmsg(p_buf);
356 }
357 
358 /*******************************************************************************
359  *
360  * Function         BTA_HhDump
361  *
362  * Description      Dump BTA HH control block
363  *
364  * Returns          void
365  *
366  ******************************************************************************/
BTA_HhDump(int fd)367 void BTA_HhDump(int fd) { bta_hh_dump(fd); }
368