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