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