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