1 /******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 //#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
20
21 #include <ctype.h>
22 #include <fcntl.h>
23 #include <sys/poll.h>
24 #include <pthread.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <errno.h>
29 #include <linux/uhid.h>
30 #include "btif_hh.h"
31 #include "bta_api.h"
32 #include "bta_hh_api.h"
33
34
35
36 const char *dev_path = "/dev/uhid";
37
38
39 /*Internal function to perform UHID write and error checking*/
uhid_write(int fd,const struct uhid_event * ev)40 static int uhid_write(int fd, const struct uhid_event *ev)
41 {
42 ssize_t ret;
43 ret = write(fd, ev, sizeof(*ev));
44 if (ret < 0){
45 int rtn = -errno;
46 APPL_TRACE_ERROR2("%s: Cannot write to uhid:%s", __FUNCTION__, strerror(errno));
47 return rtn;
48 } else if (ret != sizeof(*ev)) {
49 APPL_TRACE_ERROR3("%s: Wrong size written to uhid: %ld != %lu",
50 __FUNCTION__, ret, sizeof(*ev));
51 return -EFAULT;
52 } else {
53 return 0;
54 }
55 }
56
57 /* Internal function to parse the events received from UHID driver*/
uhid_event(btif_hh_device_t * p_dev)58 static int uhid_event(btif_hh_device_t *p_dev)
59 {
60 struct uhid_event ev;
61 ssize_t ret;
62 memset(&ev, 0, sizeof(ev));
63 if(!p_dev)
64 APPL_TRACE_ERROR1("%s: Device not found",__FUNCTION__)
65 ret = read(p_dev->fd, &ev, sizeof(ev));
66 if (ret == 0) {
67 APPL_TRACE_ERROR2("%s: Read HUP on uhid-cdev %s", __FUNCTION__,
68 strerror(errno));
69 return -EFAULT;
70 } else if (ret < 0) {
71 APPL_TRACE_ERROR2("%s:Cannot read uhid-cdev: %s", __FUNCTION__,
72 strerror(errno));
73 return -errno;
74 } else if (ret != sizeof(ev)) {
75 APPL_TRACE_ERROR3("%s:Invalid size read from uhid-dev: %ld != %lu",
76 __FUNCTION__, ret, sizeof(ev));
77 return -EFAULT;
78 }
79
80 switch (ev.type) {
81 case UHID_START:
82 APPL_TRACE_DEBUG0("UHID_START from uhid-dev\n");
83 break;
84 case UHID_STOP:
85 APPL_TRACE_DEBUG0("UHID_STOP from uhid-dev\n");
86 break;
87 case UHID_OPEN:
88 APPL_TRACE_DEBUG0("UHID_OPEN from uhid-dev\n");
89 break;
90 case UHID_CLOSE:
91 APPL_TRACE_DEBUG0("UHID_CLOSE from uhid-dev\n");
92 break;
93 case UHID_OUTPUT:
94 APPL_TRACE_DEBUG0("UHID_OUTPUT from uhid-dev\n");
95 APPL_TRACE_DEBUG2("UHID_OUTPUT: Report type = %d, report_size = %d"
96 ,ev.u.output.rtype, ev.u.output.size);
97 //Send SET_REPORT with feature report if the report type in output event is FEATURE
98 if(ev.u.output.rtype == UHID_FEATURE_REPORT)
99 btif_hh_setreport(p_dev,BTHH_FEATURE_REPORT,ev.u.output.size,ev.u.output.data);
100 else if(ev.u.output.rtype == UHID_OUTPUT_REPORT)
101 btif_hh_setreport(p_dev,BTHH_OUTPUT_REPORT,ev.u.output.size,ev.u.output.data);
102 else
103 btif_hh_setreport(p_dev,BTHH_INPUT_REPORT,ev.u.output.size,ev.u.output.data);
104 break;
105 case UHID_OUTPUT_EV:
106 APPL_TRACE_DEBUG0("UHID_OUTPUT_EV from uhid-dev\n");
107 break;
108 case UHID_FEATURE:
109 APPL_TRACE_DEBUG0("UHID_FEATURE from uhid-dev\n");
110 break;
111 case UHID_FEATURE_ANSWER:
112 APPL_TRACE_DEBUG0("UHID_FEATURE_ANSWER from uhid-dev\n");
113 break;
114
115 default:
116 APPL_TRACE_DEBUG1("Invalid event from uhid-dev: %u\n", ev.type);
117 }
118
119 return 0;
120 }
121
122 /*******************************************************************************
123 **
124 ** Function create_thread
125 **
126 ** Description creat a select loop
127 **
128 ** Returns pthread_t
129 **
130 *******************************************************************************/
create_thread(void * (* start_routine)(void *),void * arg)131 static inline pthread_t create_thread(void *(*start_routine)(void *), void * arg){
132 APPL_TRACE_DEBUG0("create_thread: entered");
133 pthread_attr_t thread_attr;
134
135 pthread_attr_init(&thread_attr);
136 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
137 pthread_t thread_id = -1;
138 if ( pthread_create(&thread_id, &thread_attr, start_routine, arg)!=0 )
139 {
140 APPL_TRACE_ERROR1("pthread_create : %s", strerror(errno));
141 return -1;
142 }
143 APPL_TRACE_DEBUG0("create_thread: thread created successfully");
144 return thread_id;
145 }
146
147 /*******************************************************************************
148 **
149 ** Function btif_hh_poll_event_thread
150 **
151 ** Description the polling thread which polls for event from UHID driver
152 **
153 ** Returns void
154 **
155 *******************************************************************************/
btif_hh_poll_event_thread(void * arg)156 static void *btif_hh_poll_event_thread(void *arg)
157 {
158
159 btif_hh_device_t *p_dev = arg;
160 APPL_TRACE_DEBUG2("%s: Thread created fd = %d", __FUNCTION__, p_dev->fd);
161 struct pollfd pfds[1];
162 int ret;
163 pfds[0].fd = p_dev->fd;
164 pfds[0].events = POLLIN;
165
166 while(p_dev->hh_keep_polling){
167 ret = poll(pfds, 1, 500);
168 if (ret < 0) {
169 APPL_TRACE_ERROR2("%s: Cannot poll for fds: %s\n", __FUNCTION__, strerror(errno));
170 break;
171 }
172 if (pfds[0].revents & POLLIN) {
173 APPL_TRACE_DEBUG0("btif_hh_poll_event_thread: POLLIN");
174 ret = uhid_event(p_dev);
175 if (ret){
176 break;
177 }
178 }
179 }
180
181 p_dev->hh_poll_thread_id = -1;
182 return 0;
183 }
184
btif_hh_close_poll_thread(btif_hh_device_t * p_dev)185 static inline void btif_hh_close_poll_thread(btif_hh_device_t *p_dev)
186 {
187 APPL_TRACE_DEBUG1("%s", __FUNCTION__);
188 p_dev->hh_keep_polling = 0;
189 if(p_dev->hh_poll_thread_id > 0)
190 pthread_join(p_dev->hh_poll_thread_id,NULL);
191
192 return;
193 }
194
bta_hh_co_destroy(int fd)195 void bta_hh_co_destroy(int fd)
196 {
197 struct uhid_event ev;
198 memset(&ev, 0, sizeof(ev));
199 ev.type = UHID_DESTROY;
200 uhid_write(fd, &ev);
201 close(fd);
202 }
203
bta_hh_co_write(int fd,UINT8 * rpt,UINT16 len)204 int bta_hh_co_write(int fd, UINT8* rpt, UINT16 len)
205 {
206 APPL_TRACE_DEBUG0("bta_hh_co_data: UHID write");
207 struct uhid_event ev;
208 memset(&ev, 0, sizeof(ev));
209 ev.type = UHID_INPUT;
210 ev.u.input.size = len;
211 if(len > sizeof(ev.u.input.data)){
212 APPL_TRACE_WARNING1("%s:report size greater than allowed size",__FUNCTION__);
213 return -1;
214 }
215 memcpy(ev.u.input.data, rpt, len);
216 return uhid_write(fd, &ev);
217
218 }
219
220
221 /*******************************************************************************
222 **
223 ** Function bta_hh_co_open
224 **
225 ** Description When connection is opened, this call-out function is executed
226 ** by HH to do platform specific initialization.
227 **
228 ** Returns void.
229 *******************************************************************************/
bta_hh_co_open(UINT8 dev_handle,UINT8 sub_class,tBTA_HH_ATTR_MASK attr_mask,UINT8 app_id)230 void bta_hh_co_open(UINT8 dev_handle, UINT8 sub_class, tBTA_HH_ATTR_MASK attr_mask,
231 UINT8 app_id)
232 {
233 UINT32 i;
234 btif_hh_device_t *p_dev = NULL;
235
236 if (dev_handle == BTA_HH_INVALID_HANDLE) {
237 APPL_TRACE_WARNING2("%s: Oops, dev_handle (%d) is invalid...", __FUNCTION__, dev_handle);
238 return;
239 }
240
241 for (i = 0; i < BTIF_HH_MAX_HID; i++) {
242 p_dev = &btif_hh_cb.devices[i];
243 if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && p_dev->dev_handle == dev_handle) {
244 // We found a device with the same handle. Must be a device reconnected.
245 APPL_TRACE_WARNING2("%s: Found an existing device with the same handle "
246 "dev_status = %d",__FUNCTION__,
247 p_dev->dev_status);
248 APPL_TRACE_WARNING6("%s: bd_addr = [%02X:%02X:%02X:%02X:%02X:]", __FUNCTION__,
249 p_dev->bd_addr.address[0], p_dev->bd_addr.address[1], p_dev->bd_addr.address[2],
250 p_dev->bd_addr.address[3], p_dev->bd_addr.address[4]);
251 APPL_TRACE_WARNING4("%s: attr_mask = 0x%04x, sub_class = 0x%02x, app_id = %d",
252 __FUNCTION__, p_dev->attr_mask, p_dev->sub_class, p_dev->app_id);
253
254 if(p_dev->fd<0) {
255 p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC);
256 if (p_dev->fd < 0){
257 APPL_TRACE_ERROR2("%s: Error: failed to open uhid, err:%s",
258 __FUNCTION__,strerror(errno));
259 }else
260 APPL_TRACE_DEBUG2("%s: uhid fd = %d", __FUNCTION__, p_dev->fd);
261 }
262 p_dev->hh_keep_polling = 1;
263 p_dev->hh_poll_thread_id = create_thread(btif_hh_poll_event_thread, p_dev);
264 break;
265 }
266 p_dev = NULL;
267 }
268
269 if (p_dev == NULL) {
270 // Did not find a device reconnection case. Find an empty slot now.
271 for (i = 0; i < BTIF_HH_MAX_HID; i++) {
272 if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_UNKNOWN) {
273 p_dev = &btif_hh_cb.devices[i];
274 p_dev->dev_handle = dev_handle;
275 p_dev->attr_mask = attr_mask;
276 p_dev->sub_class = sub_class;
277 p_dev->app_id = app_id;
278
279 btif_hh_cb.device_num++;
280 // This is a new device,open the uhid driver now.
281 p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC);
282 if (p_dev->fd < 0){
283 APPL_TRACE_ERROR2("%s: Error: failed to open uhid, err:%s",
284 __FUNCTION__,strerror(errno));
285 }else{
286 APPL_TRACE_DEBUG2("%s: uhid fd = %d", __FUNCTION__, p_dev->fd);
287 p_dev->hh_keep_polling = 1;
288 p_dev->hh_poll_thread_id = create_thread(btif_hh_poll_event_thread, p_dev);
289 }
290
291
292 break;
293 }
294 }
295 }
296
297 if (p_dev == NULL) {
298 APPL_TRACE_ERROR1("%s: Error: too many HID devices are connected", __FUNCTION__);
299 return;
300 }
301
302 p_dev->dev_status = BTHH_CONN_STATE_CONNECTED;
303 APPL_TRACE_DEBUG2("%s: Return device status %d", __FUNCTION__, p_dev->dev_status);
304 }
305
306
307 /*******************************************************************************
308 **
309 ** Function bta_hh_co_close
310 **
311 ** Description When connection is closed, this call-out function is executed
312 ** by HH to do platform specific finalization.
313 **
314 ** Parameters dev_handle - device handle
315 ** app_id - application id
316 **
317 ** Returns void.
318 *******************************************************************************/
bta_hh_co_close(UINT8 dev_handle,UINT8 app_id)319 void bta_hh_co_close(UINT8 dev_handle, UINT8 app_id)
320 {
321 UINT32 i;
322 btif_hh_device_t *p_dev = NULL;
323
324 APPL_TRACE_WARNING3("%s: dev_handle = %d, app_id = %d", __FUNCTION__, dev_handle, app_id);
325 if (dev_handle == BTA_HH_INVALID_HANDLE) {
326 APPL_TRACE_WARNING2("%s: Oops, dev_handle (%d) is invalid...", __FUNCTION__, dev_handle);
327 return;
328 }
329
330 for (i = 0; i < BTIF_HH_MAX_HID; i++) {
331 p_dev = &btif_hh_cb.devices[i];
332 if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && p_dev->dev_handle == dev_handle) {
333 APPL_TRACE_WARNING3("%s: Found an existing device with the same handle "
334 "dev_status = %d, dev_handle =%d",__FUNCTION__,
335 p_dev->dev_status,p_dev->dev_handle);
336 btif_hh_close_poll_thread(p_dev);
337 break;
338 }
339 }
340
341 }
342
343
344 /*******************************************************************************
345 **
346 ** Function bta_hh_co_data
347 **
348 ** Description This function is executed by BTA when HID host receive a data
349 ** report.
350 **
351 ** Parameters dev_handle - device handle
352 ** *p_rpt - pointer to the report data
353 ** len - length of report data
354 ** mode - Hid host Protocol Mode
355 ** sub_clas - Device Subclass
356 ** app_id - application id
357 **
358 ** Returns void
359 *******************************************************************************/
bta_hh_co_data(UINT8 dev_handle,UINT8 * p_rpt,UINT16 len,tBTA_HH_PROTO_MODE mode,UINT8 sub_class,UINT8 ctry_code,BD_ADDR peer_addr,UINT8 app_id)360 void bta_hh_co_data(UINT8 dev_handle, UINT8 *p_rpt, UINT16 len, tBTA_HH_PROTO_MODE mode,
361 UINT8 sub_class, UINT8 ctry_code, BD_ADDR peer_addr, UINT8 app_id)
362 {
363 btif_hh_device_t *p_dev;
364
365 APPL_TRACE_DEBUG6("%s: dev_handle = %d, subclass = 0x%02X, mode = %d, "
366 "ctry_code = %d, app_id = %d",
367 __FUNCTION__, dev_handle, sub_class, mode, ctry_code, app_id);
368
369 p_dev = btif_hh_find_connected_dev_by_handle(dev_handle);
370 if (p_dev == NULL) {
371 APPL_TRACE_WARNING2("%s: Error: unknown HID device handle %d", __FUNCTION__, dev_handle);
372 return;
373 }
374 // Send the HID report to the kernel.
375 if (p_dev->fd >= 0) {
376 bta_hh_co_write(p_dev->fd, p_rpt, len);
377 }else {
378 APPL_TRACE_WARNING3("%s: Error: fd = %d, len = %d", __FUNCTION__, p_dev->fd, len);
379 }
380 }
381
382
383 /*******************************************************************************
384 **
385 ** Function bta_hh_co_send_hid_info
386 **
387 ** Description This function is called in btif_hh.c to process DSCP received.
388 **
389 ** Parameters dev_handle - device handle
390 ** dscp_len - report descriptor length
391 ** *p_dscp - report descriptor
392 **
393 ** Returns void
394 *******************************************************************************/
bta_hh_co_send_hid_info(btif_hh_device_t * p_dev,char * dev_name,UINT16 vendor_id,UINT16 product_id,UINT16 version,UINT8 ctry_code,int dscp_len,UINT8 * p_dscp)395 void bta_hh_co_send_hid_info(btif_hh_device_t *p_dev, char *dev_name, UINT16 vendor_id,
396 UINT16 product_id, UINT16 version, UINT8 ctry_code,
397 int dscp_len, UINT8 *p_dscp)
398 {
399 int result;
400 struct uhid_event ev;
401
402 if (p_dev->fd < 0) {
403 APPL_TRACE_WARNING3("%s: Error: fd = %d, dscp_len = %d", __FUNCTION__, p_dev->fd, dscp_len);
404 return;
405 }
406
407 APPL_TRACE_WARNING4("%s: fd = %d, name = [%s], dscp_len = %d", __FUNCTION__,
408 p_dev->fd, dev_name, dscp_len);
409 APPL_TRACE_WARNING5("%s: vendor_id = 0x%04x, product_id = 0x%04x, version= 0x%04x,"
410 "ctry_code=0x%02x",__FUNCTION__,
411 vendor_id, product_id,
412 version, ctry_code);
413
414 //Create and send hid descriptor to kernel
415 memset(&ev, 0, sizeof(ev));
416 ev.type = UHID_CREATE;
417 strncpy((char*)ev.u.create.name, dev_name, sizeof(ev.u.create.name) - 1);
418 ev.u.create.rd_size = dscp_len;
419 ev.u.create.rd_data = p_dscp;
420 ev.u.create.bus = BUS_BLUETOOTH;
421 ev.u.create.vendor = vendor_id;
422 ev.u.create.product = product_id;
423 ev.u.create.version = version;
424 ev.u.create.country = ctry_code;
425 result = uhid_write(p_dev->fd, &ev);
426
427 APPL_TRACE_WARNING4("%s: fd = %d, dscp_len = %d, result = %d", __FUNCTION__,
428 p_dev->fd, dscp_len, result);
429
430 if (result) {
431 APPL_TRACE_WARNING2("%s: Error: failed to send DSCP, result = %d", __FUNCTION__, result);
432
433 /* The HID report descriptor is corrupted. Close the driver. */
434 close(p_dev->fd);
435 p_dev->fd = -1;
436 }
437 }
438
439
440