1 /******************************************************************************
2  *
3  *  Copyright 2016 The Android Open Source Project
4  *  Copyright 2005-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 /******************************************************************************
21  *
22  *  This file contains the HID DEVICE API in the subsystem of BTA.
23  *
24  ******************************************************************************/
25 
26 #include "bt_target.h"
27 
28 #if defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE)
29 
30 #include <log/log.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "bta_hd_api.h"
36 #include "bta_hd_int.h"
37 
38 /*****************************************************************************
39  *  Constants
40  ****************************************************************************/
41 
42 static const tBTA_SYS_REG bta_hd_reg = {bta_hd_hdl_event, BTA_HdDisable};
43 
44 /*******************************************************************************
45  *
46  * Function         BTA_HdEnable
47  *
48  * Description      Enables HID device
49  *
50  * Returns          void
51  *
52  ******************************************************************************/
BTA_HdEnable(tBTA_HD_CBACK * p_cback)53 void BTA_HdEnable(tBTA_HD_CBACK* p_cback) {
54   APPL_TRACE_API("%s", __func__);
55 
56   bta_sys_register(BTA_ID_HD, &bta_hd_reg);
57 
58   tBTA_HD_API_ENABLE* p_buf =
59       (tBTA_HD_API_ENABLE*)osi_malloc((uint16_t)sizeof(tBTA_HD_API_ENABLE));
60 
61   memset(p_buf, 0, sizeof(tBTA_HD_API_ENABLE));
62 
63   p_buf->hdr.event = BTA_HD_API_ENABLE_EVT;
64   p_buf->p_cback = p_cback;
65 
66   bta_sys_sendmsg(p_buf);
67 }
68 
69 /*******************************************************************************
70  *
71  * Function         BTA_HdDisable
72  *
73  * Description      Disables HID device.
74  *
75  * Returns          void
76  *
77  ******************************************************************************/
BTA_HdDisable(void)78 void BTA_HdDisable(void) {
79   APPL_TRACE_API("%s", __func__);
80 
81   bta_sys_deregister(BTA_ID_HD);
82 
83   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
84   p_buf->event = BTA_HD_API_DISABLE_EVT;
85   bta_sys_sendmsg(p_buf);
86 }
87 
88 /*******************************************************************************
89  *
90  * Function         BTA_HdRegisterApp
91  *
92  * Description      This function is called when application should be
93 *registered
94  *
95  * Returns          void
96  *
97  ******************************************************************************/
BTA_HdRegisterApp(tBTA_HD_APP_INFO * p_app_info,tBTA_HD_QOS_INFO * p_in_qos,tBTA_HD_QOS_INFO * p_out_qos)98 extern void BTA_HdRegisterApp(tBTA_HD_APP_INFO* p_app_info,
99                               tBTA_HD_QOS_INFO* p_in_qos,
100                               tBTA_HD_QOS_INFO* p_out_qos) {
101   APPL_TRACE_API("%s", __func__);
102 
103   tBTA_HD_REGISTER_APP* p_buf =
104       (tBTA_HD_REGISTER_APP*)osi_malloc(sizeof(tBTA_HD_REGISTER_APP));
105   p_buf->hdr.event = BTA_HD_API_REGISTER_APP_EVT;
106 
107   if (p_app_info->p_name) {
108     strlcpy(p_buf->name, p_app_info->p_name, BTA_HD_APP_NAME_LEN);
109   } else {
110     p_buf->name[0] = '\0';
111   }
112 
113   if (p_app_info->p_description) {
114     strlcpy(p_buf->description, p_app_info->p_description,
115             BTA_HD_APP_DESCRIPTION_LEN);
116   } else {
117     p_buf->description[0] = '\0';
118   }
119 
120   if (p_app_info->p_provider) {
121     strlcpy(p_buf->provider, p_app_info->p_provider, BTA_HD_APP_PROVIDER_LEN);
122   } else {
123     p_buf->provider[0] = '\0';
124   }
125 
126   p_buf->subclass = p_app_info->subclass;
127 
128   if (p_app_info->descriptor.dl_len > BTA_HD_APP_DESCRIPTOR_LEN) {
129     p_app_info->descriptor.dl_len = BTA_HD_APP_DESCRIPTOR_LEN;
130     android_errorWriteLog(0x534e4554, "113111784");
131   }
132   p_buf->d_len = p_app_info->descriptor.dl_len;
133   memcpy(p_buf->d_data, p_app_info->descriptor.dsc_list,
134          p_app_info->descriptor.dl_len);
135 
136   // copy qos data as-is
137   memcpy(&p_buf->in_qos, p_in_qos, sizeof(tBTA_HD_QOS_INFO));
138   memcpy(&p_buf->out_qos, p_out_qos, sizeof(tBTA_HD_QOS_INFO));
139 
140   bta_sys_sendmsg(p_buf);
141 }
142 
143 /*******************************************************************************
144  *
145  * Function         BTA_HdUnregisterApp
146  *
147  * Description      This function is called when application should be
148 *unregistered
149  *
150  * Returns          void
151  *
152  ******************************************************************************/
BTA_HdUnregisterApp(void)153 extern void BTA_HdUnregisterApp(void) {
154   APPL_TRACE_API("%s", __func__);
155 
156   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
157   p_buf->event = BTA_HD_API_UNREGISTER_APP_EVT;
158 
159   bta_sys_sendmsg(p_buf);
160 }
161 
162 /*******************************************************************************
163  *
164  * Function         BTA_HdSendReport
165  *
166  * Description      This function is called when report is to be sent
167  *
168  * Returns          void
169  *
170  ******************************************************************************/
BTA_HdSendReport(tBTA_HD_REPORT * p_report)171 extern void BTA_HdSendReport(tBTA_HD_REPORT* p_report) {
172   APPL_TRACE_VERBOSE("%s", __func__);
173 
174   if (p_report->len > BTA_HD_REPORT_LEN) {
175     APPL_TRACE_WARNING(
176         "%s, report len (%d) > MTU len (%d), can't send report."
177         " Increase value of HID_DEV_MTU_SIZE to send larger reports",
178         __func__, p_report->len, BTA_HD_REPORT_LEN);
179     return;
180   }
181 
182   tBTA_HD_SEND_REPORT* p_buf =
183       (tBTA_HD_SEND_REPORT*)osi_malloc(sizeof(tBTA_HD_SEND_REPORT));
184   p_buf->hdr.event = BTA_HD_API_SEND_REPORT_EVT;
185 
186   p_buf->use_intr = p_report->use_intr;
187   p_buf->type = p_report->type;
188   p_buf->id = p_report->id;
189   p_buf->len = p_report->len;
190   memcpy(p_buf->data, p_report->p_data, p_report->len);
191 
192   bta_sys_sendmsg(p_buf);
193 }
194 
195 /*******************************************************************************
196  *
197  * Function         BTA_HdVirtualCableUnplug
198  *
199  * Description      This function is called when VCU shall be sent
200  *
201  * Returns          void
202  *
203  ******************************************************************************/
BTA_HdVirtualCableUnplug(void)204 extern void BTA_HdVirtualCableUnplug(void) {
205   APPL_TRACE_API("%s", __func__);
206 
207   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
208   p_buf->event = BTA_HD_API_VC_UNPLUG_EVT;
209 
210   bta_sys_sendmsg(p_buf);
211 }
212 
213 /*******************************************************************************
214  *
215  * Function         BTA_HdConnect
216  *
217  * Description      This function is called when connection to host shall be
218 *made
219  *
220  * Returns          void
221  *
222  ******************************************************************************/
BTA_HdConnect(const RawAddress & addr)223 extern void BTA_HdConnect(const RawAddress& addr) {
224   APPL_TRACE_API("%s", __func__);
225 
226   tBTA_HD_DEVICE_CTRL* p_buf =
227       (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
228   p_buf->hdr.event = BTA_HD_API_CONNECT_EVT;
229 
230   p_buf->addr = addr;
231 
232   bta_sys_sendmsg(p_buf);
233 }
234 
235 /*******************************************************************************
236  *
237  * Function         BTA_HdDisconnect
238  *
239  * Description      This function is called when host shall be disconnected
240  *
241  * Returns          void
242  *
243  ******************************************************************************/
BTA_HdDisconnect(void)244 extern void BTA_HdDisconnect(void) {
245   APPL_TRACE_API("%s", __func__);
246   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
247   p_buf->event = BTA_HD_API_DISCONNECT_EVT;
248 
249   bta_sys_sendmsg(p_buf);
250 }
251 
252 /*******************************************************************************
253  *
254  * Function         BTA_HdAddDevice
255  *
256  * Description      This function is called when a device is virtually cabled
257  *
258  * Returns          void
259  *
260  ******************************************************************************/
BTA_HdAddDevice(const RawAddress & addr)261 extern void BTA_HdAddDevice(const RawAddress& addr) {
262   APPL_TRACE_API("%s", __func__);
263   tBTA_HD_DEVICE_CTRL* p_buf =
264       (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
265   p_buf->hdr.event = BTA_HD_API_ADD_DEVICE_EVT;
266 
267   p_buf->addr = addr;
268 
269   bta_sys_sendmsg(p_buf);
270 }
271 
272 /*******************************************************************************
273  *
274  * Function         BTA_HdRemoveDevice
275  *
276  * Description      This function is called when a device is virtually uncabled
277  *
278  * Returns          void
279  *
280  ******************************************************************************/
BTA_HdRemoveDevice(const RawAddress & addr)281 extern void BTA_HdRemoveDevice(const RawAddress& addr) {
282   APPL_TRACE_API("%s", __func__);
283   tBTA_HD_DEVICE_CTRL* p_buf =
284       (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
285   p_buf->hdr.event = BTA_HD_API_REMOVE_DEVICE_EVT;
286 
287   p_buf->addr = addr;
288 
289   bta_sys_sendmsg(p_buf);
290 }
291 
292 /*******************************************************************************
293  *
294  * Function         BTA_HdReportError
295  *
296  * Description      This function is called when reporting error for set report
297  *
298  * Returns          void
299  *
300  ******************************************************************************/
BTA_HdReportError(uint8_t error)301 extern void BTA_HdReportError(uint8_t error) {
302   APPL_TRACE_API("%s", __func__);
303   tBTA_HD_REPORT_ERR* p_buf =
304       (tBTA_HD_REPORT_ERR*)osi_malloc(sizeof(tBTA_HD_REPORT_ERR));
305   p_buf->hdr.event = BTA_HD_API_REPORT_ERROR_EVT;
306   p_buf->error = error;
307 
308   bta_sys_sendmsg(p_buf);
309 }
310 
311 #endif /* BTA_HD_INCLUDED */
312