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