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 #define LOG_TAG "bluetooth"
27 
28 // BTA_HD_INCLUDED
29 #include "bt_target.h"  // Must be first to define build configuration
30 #if defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE)
31 
32 #include "bta/hd/bta_hd_int.h"
33 #include "osi/include/allocator.h"
34 #include "osi/include/compat.h"
35 #include "osi/include/log.h"
36 
37 /*****************************************************************************
38  *  Constants
39  ****************************************************************************/
40 
41 static const tBTA_SYS_REG bta_hd_reg = {bta_hd_hdl_event, BTA_HdDisable};
42 
43 /*******************************************************************************
44  *
45  * Function         BTA_HdEnable
46  *
47  * Description      Enables HID device
48  *
49  * Returns          void
50  *
51  ******************************************************************************/
BTA_HdEnable(tBTA_HD_CBACK * p_cback)52 void BTA_HdEnable(tBTA_HD_CBACK* p_cback) {
53   APPL_TRACE_API("%s", __func__);
54 
55   bta_sys_register(BTA_ID_HD, &bta_hd_reg);
56 
57   tBTA_HD_API_ENABLE* p_buf =
58       (tBTA_HD_API_ENABLE*)osi_malloc((uint16_t)sizeof(tBTA_HD_API_ENABLE));
59 
60   memset(p_buf, 0, sizeof(tBTA_HD_API_ENABLE));
61 
62   p_buf->hdr.event = BTA_HD_API_ENABLE_EVT;
63   p_buf->p_cback = p_cback;
64 
65   bta_sys_sendmsg(p_buf);
66 }
67 
68 /*******************************************************************************
69  *
70  * Function         BTA_HdDisable
71  *
72  * Description      Disables HID device.
73  *
74  * Returns          void
75  *
76  ******************************************************************************/
BTA_HdDisable(void)77 void BTA_HdDisable(void) {
78   APPL_TRACE_API("%s", __func__);
79 
80   bta_sys_deregister(BTA_ID_HD);
81 
82   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
83   p_buf->event = BTA_HD_API_DISABLE_EVT;
84   bta_sys_sendmsg(p_buf);
85 }
86 
87 /*******************************************************************************
88  *
89  * Function         BTA_HdRegisterApp
90  *
91  * Description      This function is called when application should be
92 *registered
93  *
94  * Returns          void
95  *
96  ******************************************************************************/
BTA_HdRegisterApp(tBTA_HD_APP_INFO * p_app_info,tBTA_HD_QOS_INFO * p_in_qos,tBTA_HD_QOS_INFO * p_out_qos)97 extern void BTA_HdRegisterApp(tBTA_HD_APP_INFO* p_app_info,
98                               tBTA_HD_QOS_INFO* p_in_qos,
99                               tBTA_HD_QOS_INFO* p_out_qos) {
100   APPL_TRACE_API("%s", __func__);
101 
102   tBTA_HD_REGISTER_APP* p_buf =
103       (tBTA_HD_REGISTER_APP*)osi_malloc(sizeof(tBTA_HD_REGISTER_APP));
104   p_buf->hdr.event = BTA_HD_API_REGISTER_APP_EVT;
105 
106   if (p_app_info->p_name) {
107     strlcpy(p_buf->name, p_app_info->p_name, BTA_HD_APP_NAME_LEN);
108   } else {
109     p_buf->name[0] = '\0';
110   }
111 
112   if (p_app_info->p_description) {
113     strlcpy(p_buf->description, p_app_info->p_description,
114             BTA_HD_APP_DESCRIPTION_LEN);
115   } else {
116     p_buf->description[0] = '\0';
117   }
118 
119   if (p_app_info->p_provider) {
120     strlcpy(p_buf->provider, p_app_info->p_provider, BTA_HD_APP_PROVIDER_LEN);
121   } else {
122     p_buf->provider[0] = '\0';
123   }
124 
125   p_buf->subclass = p_app_info->subclass;
126 
127   if (p_app_info->descriptor.dl_len > BTA_HD_APP_DESCRIPTOR_LEN) {
128     p_app_info->descriptor.dl_len = BTA_HD_APP_DESCRIPTOR_LEN;
129     android_errorWriteLog(0x534e4554, "113111784");
130   }
131   p_buf->d_len = p_app_info->descriptor.dl_len;
132   memcpy(p_buf->d_data, p_app_info->descriptor.dsc_list,
133          p_app_info->descriptor.dl_len);
134 
135   // copy qos data as-is
136   memcpy(&p_buf->in_qos, p_in_qos, sizeof(tBTA_HD_QOS_INFO));
137   memcpy(&p_buf->out_qos, p_out_qos, sizeof(tBTA_HD_QOS_INFO));
138 
139   bta_sys_sendmsg(p_buf);
140 }
141 
142 /*******************************************************************************
143  *
144  * Function         BTA_HdUnregisterApp
145  *
146  * Description      This function is called when application should be
147 *unregistered
148  *
149  * Returns          void
150  *
151  ******************************************************************************/
BTA_HdUnregisterApp(void)152 extern void BTA_HdUnregisterApp(void) {
153   APPL_TRACE_API("%s", __func__);
154 
155   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
156   p_buf->event = BTA_HD_API_UNREGISTER_APP_EVT;
157 
158   bta_sys_sendmsg(p_buf);
159 }
160 
161 /*******************************************************************************
162  *
163  * Function         BTA_HdSendReport
164  *
165  * Description      This function is called when report is to be sent
166  *
167  * Returns          void
168  *
169  ******************************************************************************/
BTA_HdSendReport(tBTA_HD_REPORT * p_report)170 extern void BTA_HdSendReport(tBTA_HD_REPORT* p_report) {
171   APPL_TRACE_VERBOSE("%s", __func__);
172 
173   if (p_report->len > BTA_HD_REPORT_LEN) {
174     APPL_TRACE_WARNING(
175         "%s, report len (%d) > MTU len (%d), can't send report."
176         " Increase value of HID_DEV_MTU_SIZE to send larger reports",
177         __func__, p_report->len, BTA_HD_REPORT_LEN);
178     return;
179   }
180 
181   tBTA_HD_SEND_REPORT* p_buf =
182       (tBTA_HD_SEND_REPORT*)osi_malloc(sizeof(tBTA_HD_SEND_REPORT));
183   p_buf->hdr.event = BTA_HD_API_SEND_REPORT_EVT;
184 
185   p_buf->use_intr = p_report->use_intr;
186   p_buf->type = p_report->type;
187   p_buf->id = p_report->id;
188   p_buf->len = p_report->len;
189   memcpy(p_buf->data, p_report->p_data, p_report->len);
190 
191   bta_sys_sendmsg(p_buf);
192 }
193 
194 /*******************************************************************************
195  *
196  * Function         BTA_HdVirtualCableUnplug
197  *
198  * Description      This function is called when VCU shall be sent
199  *
200  * Returns          void
201  *
202  ******************************************************************************/
BTA_HdVirtualCableUnplug(void)203 extern void BTA_HdVirtualCableUnplug(void) {
204   APPL_TRACE_API("%s", __func__);
205 
206   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
207   p_buf->event = BTA_HD_API_VC_UNPLUG_EVT;
208 
209   bta_sys_sendmsg(p_buf);
210 }
211 
212 /*******************************************************************************
213  *
214  * Function         BTA_HdConnect
215  *
216  * Description      This function is called when connection to host shall be
217 *made
218  *
219  * Returns          void
220  *
221  ******************************************************************************/
BTA_HdConnect(const RawAddress & addr)222 extern void BTA_HdConnect(const RawAddress& addr) {
223   APPL_TRACE_API("%s", __func__);
224 
225   tBTA_HD_DEVICE_CTRL* p_buf =
226       (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
227   p_buf->hdr.event = BTA_HD_API_CONNECT_EVT;
228 
229   p_buf->addr = addr;
230 
231   bta_sys_sendmsg(p_buf);
232 }
233 
234 /*******************************************************************************
235  *
236  * Function         BTA_HdDisconnect
237  *
238  * Description      This function is called when host shall be disconnected
239  *
240  * Returns          void
241  *
242  ******************************************************************************/
BTA_HdDisconnect(void)243 extern void BTA_HdDisconnect(void) {
244   APPL_TRACE_API("%s", __func__);
245   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
246   p_buf->event = BTA_HD_API_DISCONNECT_EVT;
247 
248   bta_sys_sendmsg(p_buf);
249 }
250 
251 /*******************************************************************************
252  *
253  * Function         BTA_HdAddDevice
254  *
255  * Description      This function is called when a device is virtually cabled
256  *
257  * Returns          void
258  *
259  ******************************************************************************/
BTA_HdAddDevice(const RawAddress & addr)260 extern void BTA_HdAddDevice(const RawAddress& addr) {
261   APPL_TRACE_API("%s", __func__);
262   tBTA_HD_DEVICE_CTRL* p_buf =
263       (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
264   p_buf->hdr.event = BTA_HD_API_ADD_DEVICE_EVT;
265 
266   p_buf->addr = addr;
267 
268   bta_sys_sendmsg(p_buf);
269 }
270 
271 /*******************************************************************************
272  *
273  * Function         BTA_HdRemoveDevice
274  *
275  * Description      This function is called when a device is virtually uncabled
276  *
277  * Returns          void
278  *
279  ******************************************************************************/
BTA_HdRemoveDevice(const RawAddress & addr)280 extern void BTA_HdRemoveDevice(const RawAddress& addr) {
281   APPL_TRACE_API("%s", __func__);
282   tBTA_HD_DEVICE_CTRL* p_buf =
283       (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
284   p_buf->hdr.event = BTA_HD_API_REMOVE_DEVICE_EVT;
285 
286   p_buf->addr = addr;
287 
288   bta_sys_sendmsg(p_buf);
289 }
290 
291 /*******************************************************************************
292  *
293  * Function         BTA_HdReportError
294  *
295  * Description      This function is called when reporting error for set report
296  *
297  * Returns          void
298  *
299  ******************************************************************************/
BTA_HdReportError(uint8_t error)300 extern void BTA_HdReportError(uint8_t error) {
301   APPL_TRACE_API("%s", __func__);
302   tBTA_HD_REPORT_ERR* p_buf =
303       (tBTA_HD_REPORT_ERR*)osi_malloc(sizeof(tBTA_HD_REPORT_ERR));
304   p_buf->hdr.event = BTA_HD_API_REPORT_ERROR_EVT;
305   p_buf->error = error;
306 
307   bta_sys_sendmsg(p_buf);
308 }
309 
310 #endif /* BTA_HD_INCLUDED */
311