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 <bluetooth/log.h>
28 
29 #include <cstdint>
30 
31 #include "bta/hh/bta_hh_int.h"
32 #include "bta/sys/bta_sys.h"
33 #include "os/log.h"
34 #include "osi/include/allocator.h"
35 #include "stack/include/bt_hdr.h"
36 #include "stack/include/main_thread.h"
37 #include "types/raw_address.h"
38 
39 using namespace bluetooth;
40 
41 /*****************************************************************************
42  *  Constants
43  ****************************************************************************/
44 
45 /**
46  * Android Headtracker Service UUIDs
47  */
48 const Uuid ANDROID_HEADTRACKER_SERVICE_UUID =
49     Uuid::FromString(ANDROID_HEADTRACKER_SERVICE_UUID_STRING);
50 const Uuid ANDROID_HEADTRACKER_VERSION_CHARAC_UUID =
51     Uuid::FromString(ANDROID_HEADTRACKER_VERSION_CHARAC_UUID_STRING);
52 const Uuid ANDROID_HEADTRACKER_CONTROL_CHARAC_UUID =
53     Uuid::FromString(ANDROID_HEADTRACKER_CONTROL_CHARAC_UUID_STRING);
54 const Uuid ANDROID_HEADTRACKER_REPORT_CHARAC_UUID =
55     Uuid::FromString(ANDROID_HEADTRACKER_REPORT_CHARAC_UUID_STRING);
56 
57 static const tBTA_SYS_REG bta_hh_reg = {bta_hh_hdl_event, BTA_HhDisable};
58 
59 /*******************************************************************************
60  *
61  * Function         BTA_HhEnable
62  *
63  * Description      Enable the HID host.  This function must be called before
64  *                  any other functions in the HID host API are called. When the
65  *                  enable operation is complete the callback function will be
66  *                  called with BTA_HH_ENABLE_EVT.
67  *
68  *
69  * Returns          void
70  *
71  ******************************************************************************/
BTA_HhEnable(tBTA_HH_CBACK * p_cback,bool enable_hid,bool enable_hogp)72 void BTA_HhEnable(tBTA_HH_CBACK* p_cback, bool enable_hid, bool enable_hogp) {
73   /* register with BTA system manager */
74   bta_sys_register(BTA_ID_HH, &bta_hh_reg);
75 
76   post_on_bt_main([p_cback, enable_hid, enable_hogp]() {
77     bta_hh_api_enable(p_cback, enable_hid, enable_hogp);
78   });
79 }
80 
81 /*******************************************************************************
82  *
83  * Function         BTA_HhDisable
84  *
85  * Description      Disable the HID host. If the server is currently
86  *                  connected, the connection will be closed.
87  *
88  * Returns          void
89  *
90  ******************************************************************************/
BTA_HhDisable(void)91 void BTA_HhDisable(void) {
92   bta_sys_deregister(BTA_ID_HH);
93 
94   post_on_bt_main([]() { bta_hh_api_disable(); });
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 tAclLinkSpec & link_spec)125 void BTA_HhOpen(const tAclLinkSpec& link_spec) {
126   tBTA_HH_API_CONN* p_buf =
127       (tBTA_HH_API_CONN*)osi_calloc(sizeof(tBTA_HH_API_CONN));
128   tBTA_HH_PROTO_MODE mode = BTA_HH_PROTO_RPT_MODE;
129 
130   p_buf->hdr.event = BTA_HH_API_OPEN_EVT;
131   p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
132   p_buf->mode = mode;
133   p_buf->link_spec = link_spec;
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  *                  link_spec : remote device acl link specification
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,const tAclLinkSpec &,BT_HDR * p_data)274 void BTA_HhSendData(uint8_t dev_handle, const tAclLinkSpec& /* link_spec */,
275                     BT_HDR* p_data) {
276   if (p_data->layer_specific != BTA_HH_RPTT_OUTPUT) {
277     log::error(
278         "ERROR! Wrong report type! Write Command only valid for output "
279         "report!");
280     return;
281   }
282   bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA,
283                        (uint8_t)p_data->layer_specific, 0, 0, p_data);
284 }
285 
286 /*******************************************************************************
287  *
288  * Function         BTA_HhGetDscpInfo
289  *
290  * Description      Get HID device report descriptor
291  *
292  * Returns          void
293  *
294  ******************************************************************************/
BTA_HhGetDscpInfo(uint8_t dev_handle)295 void BTA_HhGetDscpInfo(uint8_t dev_handle) {
296   BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));
297 
298   p_buf->event = BTA_HH_API_GET_DSCP_EVT;
299   p_buf->layer_specific = (uint16_t)dev_handle;
300 
301   bta_sys_sendmsg(p_buf);
302 }
303 
304 /*******************************************************************************
305  *
306  * Function         BTA_HhAddDev
307  *
308  * Description      Add a virtually cabled device into HID-Host device list
309  *                  to manage and assign a device handle for future API call,
310  *                  host applciation call this API at start-up to initialize its
311  *                  virtually cabled devices.
312  *
313  * Returns          void
314  *
315  ******************************************************************************/
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)316 void BTA_HhAddDev(const tAclLinkSpec& link_spec, tBTA_HH_ATTR_MASK attr_mask,
317                   uint8_t sub_class, uint8_t app_id,
318                   tBTA_HH_DEV_DSCP_INFO dscp_info) {
319   size_t len = sizeof(tBTA_HH_MAINT_DEV) + dscp_info.descriptor.dl_len;
320   tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(len);
321 
322   p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
323   p_buf->sub_event = BTA_HH_ADD_DEV_EVT;
324   p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
325 
326   p_buf->attr_mask = (uint16_t)attr_mask;
327   p_buf->sub_class = sub_class;
328   p_buf->app_id = app_id;
329   p_buf->link_spec = link_spec;
330 
331   memcpy(&p_buf->dscp_info, &dscp_info, sizeof(tBTA_HH_DEV_DSCP_INFO));
332   if (dscp_info.descriptor.dl_len != 0 && dscp_info.descriptor.dsc_list) {
333     p_buf->dscp_info.descriptor.dl_len = dscp_info.descriptor.dl_len;
334     p_buf->dscp_info.descriptor.dsc_list = (uint8_t*)(p_buf + 1);
335     memcpy(p_buf->dscp_info.descriptor.dsc_list, dscp_info.descriptor.dsc_list,
336            dscp_info.descriptor.dl_len);
337   } else {
338     p_buf->dscp_info.descriptor.dsc_list = NULL;
339     p_buf->dscp_info.descriptor.dl_len = 0;
340   }
341 
342   bta_sys_sendmsg(p_buf);
343 }
344 
345 /*******************************************************************************
346  *
347  * Function         BTA_HhRemoveDev
348  *
349  * Description      Remove a device from the HID host devices list.
350  *
351  * Returns          void
352  *
353  ******************************************************************************/
BTA_HhRemoveDev(uint8_t dev_handle)354 void BTA_HhRemoveDev(uint8_t dev_handle) {
355   tBTA_HH_MAINT_DEV* p_buf =
356       (tBTA_HH_MAINT_DEV*)osi_calloc(sizeof(tBTA_HH_MAINT_DEV));
357 
358   p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
359   p_buf->sub_event = BTA_HH_RMV_DEV_EVT;
360   p_buf->hdr.layer_specific = (uint16_t)dev_handle;
361 
362   bta_sys_sendmsg(p_buf);
363 }
364