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