1 /******************************************************************************
3 * Copyright 2016 The Android Open Source Project
4 * Copyright 2005-2012 Broadcom Corporation
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 ******************************************************************************/
20 /******************************************************************************
22 * This file contains the HID DEVICE API in the subsystem of BTA.
24 ******************************************************************************/
26 #define LOG_TAG "bluetooth"
29 #include "bt_target.h" // Must be first to define build configuration
30 #if defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE)
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"
37 /*****************************************************************************
39 ****************************************************************************/
41 static const tBTA_SYS_REG bta_hd_reg = {bta_hd_hdl_event, BTA_HdDisable};
43 /*******************************************************************************
45 * Function BTA_HdEnable
47 * Description Enables HID device
51 ******************************************************************************/
52 void BTA_HdEnable(tBTA_HD_CBACK* p_cback) {
53 APPL_TRACE_API("%s", __func__);
55 bta_sys_register(BTA_ID_HD, &bta_hd_reg);
57 tBTA_HD_API_ENABLE* p_buf =
58 (tBTA_HD_API_ENABLE*)osi_malloc((uint16_t)sizeof(tBTA_HD_API_ENABLE));
60 memset(p_buf, 0, sizeof(tBTA_HD_API_ENABLE));
62 p_buf->hdr.event = BTA_HD_API_ENABLE_EVT;
63 p_buf->p_cback = p_cback;
65 bta_sys_sendmsg(p_buf);
68 /*******************************************************************************
70 * Function BTA_HdDisable
72 * Description Disables HID device.
76 ******************************************************************************/
77 void BTA_HdDisable(void) {
78 APPL_TRACE_API("%s", __func__);
80 bta_sys_deregister(BTA_ID_HD);
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);
87 /*******************************************************************************
89 * Function BTA_HdRegisterApp
91 * Description This function is called when application should be
96 ******************************************************************************/
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__);
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;
106 if (p_app_info->p_name) {
107 strlcpy(p_buf->name, p_app_info->p_name, BTA_HD_APP_NAME_LEN);
109 p_buf->name[0] = '\0';
112 if (p_app_info->p_description) {
113 strlcpy(p_buf->description, p_app_info->p_description,
114 BTA_HD_APP_DESCRIPTION_LEN);
116 p_buf->description[0] = '\0';
119 if (p_app_info->p_provider) {
120 strlcpy(p_buf->provider, p_app_info->p_provider, BTA_HD_APP_PROVIDER_LEN);
122 p_buf->provider[0] = '\0';
125 p_buf->subclass = p_app_info->subclass;
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");
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);
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));
139 bta_sys_sendmsg(p_buf);
142 /*******************************************************************************
144 * Function BTA_HdUnregisterApp
146 * Description This function is called when application should be
151 ******************************************************************************/
152 extern void BTA_HdUnregisterApp(void) {
153 APPL_TRACE_API("%s", __func__);
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;
158 bta_sys_sendmsg(p_buf);
161 /*******************************************************************************
163 * Function BTA_HdSendReport
165 * Description This function is called when report is to be sent
169 ******************************************************************************/
170 extern void BTA_HdSendReport(tBTA_HD_REPORT* p_report) {
171 APPL_TRACE_VERBOSE("%s", __func__);
173 if (p_report->len > BTA_HD_REPORT_LEN) {
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);
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;
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);
191 bta_sys_sendmsg(p_buf);
194 /*******************************************************************************
196 * Function BTA_HdVirtualCableUnplug
198 * Description This function is called when VCU shall be sent
202 ******************************************************************************/
203 extern void BTA_HdVirtualCableUnplug(void) {
204 APPL_TRACE_API("%s", __func__);
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;
209 bta_sys_sendmsg(p_buf);
212 /*******************************************************************************
214 * Function BTA_HdConnect
216 * Description This function is called when connection to host shall be
221 ******************************************************************************/
222 extern void BTA_HdConnect(const RawAddress& addr) {
223 APPL_TRACE_API("%s", __func__);
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;
231 bta_sys_sendmsg(p_buf);
234 /*******************************************************************************
236 * Function BTA_HdDisconnect
238 * Description This function is called when host shall be disconnected
242 ******************************************************************************/
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;
248 bta_sys_sendmsg(p_buf);
251 /*******************************************************************************
253 * Function BTA_HdAddDevice
255 * Description This function is called when a device is virtually cabled
259 ******************************************************************************/
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;
268 bta_sys_sendmsg(p_buf);
271 /*******************************************************************************
273 * Function BTA_HdRemoveDevice
275 * Description This function is called when a device is virtually uncabled
279 ******************************************************************************/
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;
288 bta_sys_sendmsg(p_buf);
291 /*******************************************************************************
293 * Function BTA_HdReportError
295 * Description This function is called when reporting error for set report
299 ******************************************************************************/
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;
307 bta_sys_sendmsg(p_buf);
310 #endif /* BTA_HD_INCLUDED */