1/******************************************************************************
2 *
3 *  Copyright (C) 2016 The Android Open Source Project
4 *  Copyright (C) 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 <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include "bta_hd_api.h"
35#include "bta_hd_int.h"
36
37/*****************************************************************************
38 *  Constants
39 ****************************************************************************/
40
41static 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 ******************************************************************************/
52void 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 ******************************************************************************/
77void BTA_HdDisable(void) {
78  APPL_TRACE_API("%s", __func__);
79
80  bta_sys_deregister(BTA_ID_HD);
81
82  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
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 ******************************************************************************/
97extern 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    strncpy(p_buf->name, p_app_info->p_name, BTA_HD_APP_NAME_LEN);
108    p_buf->name[BTA_HD_APP_NAME_LEN] = '\0';
109  } else {
110    p_buf->name[0] = '\0';
111  }
112
113  if (p_app_info->p_description) {
114    strncpy(p_buf->description, p_app_info->p_description,
115            BTA_HD_APP_DESCRIPTION_LEN);
116    p_buf->description[BTA_HD_APP_DESCRIPTION_LEN] = '\0';
117  } else {
118    p_buf->description[0] = '\0';
119  }
120
121  if (p_app_info->p_provider) {
122    strncpy(p_buf->provider, p_app_info->p_provider, BTA_HD_APP_PROVIDER_LEN);
123    p_buf->provider[BTA_HD_APP_PROVIDER_LEN] = '\0';
124  } else {
125    p_buf->provider[0] = '\0';
126  }
127
128  p_buf->subclass = p_app_info->subclass;
129
130  p_buf->d_len = p_app_info->descriptor.dl_len;
131  memcpy(p_buf->d_data, p_app_info->descriptor.dsc_list,
132         p_app_info->descriptor.dl_len);
133
134  // copy qos data as-is
135  memcpy(&p_buf->in_qos, p_in_qos, sizeof(tBTA_HD_QOS_INFO));
136  memcpy(&p_buf->out_qos, p_out_qos, sizeof(tBTA_HD_QOS_INFO));
137
138  bta_sys_sendmsg(p_buf);
139}
140
141/*******************************************************************************
142 *
143 * Function         BTA_HdUnregisterApp
144 *
145 * Description      This function is called when application should be
146*unregistered
147 *
148 * Returns          void
149 *
150 ******************************************************************************/
151extern void BTA_HdUnregisterApp(void) {
152  APPL_TRACE_API("%s", __func__);
153
154  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
155  p_buf->event = BTA_HD_API_UNREGISTER_APP_EVT;
156
157  bta_sys_sendmsg(p_buf);
158}
159
160/*******************************************************************************
161 *
162 * Function         BTA_HdSendReport
163 *
164 * Description      This function is called when report is to be sent
165 *
166 * Returns          void
167 *
168 ******************************************************************************/
169extern void BTA_HdSendReport(tBTA_HD_REPORT* p_report) {
170  APPL_TRACE_VERBOSE("%s", __func__);
171
172  if (p_report->len > BTA_HD_REPORT_LEN) {
173    APPL_TRACE_WARNING(
174        "%s, report len (%d) > MTU len (%d), can't send report."
175        " Increase value of HID_DEV_MTU_SIZE to send larger reports",
176        __func__, p_report->len, BTA_HD_REPORT_LEN);
177    return;
178  }
179
180  tBTA_HD_SEND_REPORT* p_buf =
181      (tBTA_HD_SEND_REPORT*)osi_malloc(sizeof(tBTA_HD_SEND_REPORT));
182  p_buf->hdr.event = BTA_HD_API_SEND_REPORT_EVT;
183
184  p_buf->use_intr = p_report->use_intr;
185  p_buf->type = p_report->type;
186  p_buf->id = p_report->id;
187  p_buf->len = p_report->len;
188  memcpy(p_buf->data, p_report->p_data, p_report->len);
189
190  bta_sys_sendmsg(p_buf);
191}
192
193/*******************************************************************************
194 *
195 * Function         BTA_HdVirtualCableUnplug
196 *
197 * Description      This function is called when VCU shall be sent
198 *
199 * Returns          void
200 *
201 ******************************************************************************/
202extern void BTA_HdVirtualCableUnplug(void) {
203  APPL_TRACE_API("%s", __func__);
204
205  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
206  p_buf->event = BTA_HD_API_VC_UNPLUG_EVT;
207
208  bta_sys_sendmsg(p_buf);
209}
210
211/*******************************************************************************
212 *
213 * Function         BTA_HdConnect
214 *
215 * Description      This function is called when connection to host shall be
216*made
217 *
218 * Returns          void
219 *
220 ******************************************************************************/
221extern void BTA_HdConnect(const RawAddress& addr) {
222  APPL_TRACE_API("%s", __func__);
223
224  tBTA_HD_DEVICE_CTRL* p_buf =
225      (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
226  p_buf->hdr.event = BTA_HD_API_CONNECT_EVT;
227
228  p_buf->addr = addr;
229
230  bta_sys_sendmsg(p_buf);
231}
232
233/*******************************************************************************
234 *
235 * Function         BTA_HdDisconnect
236 *
237 * Description      This function is called when host shall be disconnected
238 *
239 * Returns          void
240 *
241 ******************************************************************************/
242extern void BTA_HdDisconnect(void) {
243  APPL_TRACE_API("%s", __func__);
244  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
245  p_buf->event = BTA_HD_API_DISCONNECT_EVT;
246
247  bta_sys_sendmsg(p_buf);
248}
249
250/*******************************************************************************
251 *
252 * Function         BTA_HdAddDevice
253 *
254 * Description      This function is called when a device is virtually cabled
255 *
256 * Returns          void
257 *
258 ******************************************************************************/
259extern void BTA_HdAddDevice(const RawAddress& addr) {
260  APPL_TRACE_API("%s", __func__);
261  tBTA_HD_DEVICE_CTRL* p_buf =
262      (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
263  p_buf->hdr.event = BTA_HD_API_ADD_DEVICE_EVT;
264
265  p_buf->addr = addr;
266
267  bta_sys_sendmsg(p_buf);
268}
269
270/*******************************************************************************
271 *
272 * Function         BTA_HdRemoveDevice
273 *
274 * Description      This function is called when a device is virtually uncabled
275 *
276 * Returns          void
277 *
278 ******************************************************************************/
279extern void BTA_HdRemoveDevice(const RawAddress& addr) {
280  APPL_TRACE_API("%s", __func__);
281  tBTA_HD_DEVICE_CTRL* p_buf =
282      (tBTA_HD_DEVICE_CTRL*)osi_malloc(sizeof(tBTA_HD_DEVICE_CTRL));
283  p_buf->hdr.event = BTA_HD_API_REMOVE_DEVICE_EVT;
284
285  p_buf->addr = addr;
286
287  bta_sys_sendmsg(p_buf);
288}
289
290/*******************************************************************************
291 *
292 * Function         BTA_HdReportError
293 *
294 * Description      This function is called when reporting error for set report
295 *
296 * Returns          void
297 *
298 ******************************************************************************/
299extern void BTA_HdReportError(uint8_t error) {
300  APPL_TRACE_API("%s", __func__);
301  tBTA_HD_REPORT_ERR* p_buf =
302      (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