1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 //#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
29 #include <linux/uhid.h>
33 #include "bta_hh_api.h"
34 #include "btif_util.h"
35 #include "bta_hh_co.h"
37 const char *dev_path = "/dev/uhid";
39 #if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE)
40 #include "btif_config.h"
41 #define BTA_HH_NV_LOAD_MAX 16
42 static tBTA_HH_RPT_CACHE_ENTRY sReportCache[BTA_HH_NV_LOAD_MAX];
45 /*Internal function to perform UHID write and error checking*/
46 static int uhid_write(int fd, const struct uhid_event *ev)
49 ret = write(fd, ev, sizeof(*ev));
52 APPL_TRACE_ERROR("%s: Cannot write to uhid:%s", __FUNCTION__, strerror(errno));
54 } else if (ret != sizeof(*ev)) {
55 APPL_TRACE_ERROR("%s: Wrong size written to uhid: %ld != %lu",
56 __FUNCTION__, ret, sizeof(*ev));
63 /* Internal function to parse the events received from UHID driver*/
64 static int uhid_event(btif_hh_device_t *p_dev)
68 memset(&ev, 0, sizeof(ev));
71 APPL_TRACE_ERROR("%s: Device not found",__FUNCTION__)
74 ret = read(p_dev->fd, &ev, sizeof(ev));
76 APPL_TRACE_ERROR("%s: Read HUP on uhid-cdev %s", __FUNCTION__,
80 APPL_TRACE_ERROR("%s:Cannot read uhid-cdev: %s", __FUNCTION__,
83 } else if (ret != sizeof(ev)) {
84 APPL_TRACE_ERROR("%s:Invalid size read from uhid-dev: %ld != %lu",
85 __FUNCTION__, ret, sizeof(ev));
91 APPL_TRACE_DEBUG("UHID_START from uhid-dev\n");
94 APPL_TRACE_DEBUG("UHID_STOP from uhid-dev\n");
97 APPL_TRACE_DEBUG("UHID_OPEN from uhid-dev\n");
100 APPL_TRACE_DEBUG("UHID_CLOSE from uhid-dev\n");
103 APPL_TRACE_DEBUG("UHID_OUTPUT: Report type = %d, report_size = %d"
104 ,ev.u.output.rtype, ev.u.output.size);
105 //Send SET_REPORT with feature report if the report type in output event is FEATURE
106 if(ev.u.output.rtype == UHID_FEATURE_REPORT)
107 btif_hh_setreport(p_dev,BTHH_FEATURE_REPORT,ev.u.output.size,ev.u.output.data);
108 else if(ev.u.output.rtype == UHID_OUTPUT_REPORT)
109 btif_hh_setreport(p_dev,BTHH_OUTPUT_REPORT,ev.u.output.size,ev.u.output.data);
111 btif_hh_setreport(p_dev,BTHH_INPUT_REPORT,ev.u.output.size,ev.u.output.data);
114 APPL_TRACE_DEBUG("UHID_OUTPUT_EV from uhid-dev\n");
117 APPL_TRACE_DEBUG("UHID_FEATURE from uhid-dev\n");
119 case UHID_FEATURE_ANSWER:
120 APPL_TRACE_DEBUG("UHID_FEATURE_ANSWER from uhid-dev\n");
124 APPL_TRACE_DEBUG("Invalid event from uhid-dev: %u\n", ev.type);
130 /*******************************************************************************
132 ** Function create_thread
134 ** Description creat a select loop
138 *******************************************************************************/
139 static inline pthread_t create_thread(void *(*start_routine)(void *), void * arg){
140 APPL_TRACE_DEBUG("create_thread: entered");
141 pthread_attr_t thread_attr;
143 pthread_attr_init(&thread_attr);
144 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
145 pthread_t thread_id = -1;
146 if ( pthread_create(&thread_id, &thread_attr, start_routine, arg)!=0 )
148 APPL_TRACE_ERROR("pthread_create : %s", strerror(errno));
151 APPL_TRACE_DEBUG("create_thread: thread created successfully");
155 /*******************************************************************************
157 ** Function btif_hh_poll_event_thread
159 ** Description the polling thread which polls for event from UHID driver
163 *******************************************************************************/
164 static void *btif_hh_poll_event_thread(void *arg)
167 btif_hh_device_t *p_dev = arg;
168 APPL_TRACE_DEBUG("%s: Thread created fd = %d", __FUNCTION__, p_dev->fd);
169 struct pollfd pfds[1];
171 pfds[0].fd = p_dev->fd;
172 pfds[0].events = POLLIN;
174 while(p_dev->hh_keep_polling){
175 ret = poll(pfds, 1, 50);
177 APPL_TRACE_ERROR("%s: Cannot poll for fds: %s\n", __FUNCTION__, strerror(errno));
180 if (pfds[0].revents & POLLIN) {
181 APPL_TRACE_DEBUG("btif_hh_poll_event_thread: POLLIN");
182 ret = uhid_event(p_dev);
189 p_dev->hh_poll_thread_id = -1;
193 static inline void btif_hh_close_poll_thread(btif_hh_device_t *p_dev)
195 APPL_TRACE_DEBUG("%s", __FUNCTION__);
196 p_dev->hh_keep_polling = 0;
197 if(p_dev->hh_poll_thread_id > 0)
198 pthread_join(p_dev->hh_poll_thread_id,NULL);
203 void bta_hh_co_destroy(int fd)
205 struct uhid_event ev;
206 memset(&ev, 0, sizeof(ev));
207 ev.type = UHID_DESTROY;
212 int bta_hh_co_write(int fd, UINT8* rpt, UINT16 len)
214 APPL_TRACE_DEBUG("bta_hh_co_data: UHID write");
215 struct uhid_event ev;
216 memset(&ev, 0, sizeof(ev));
217 ev.type = UHID_INPUT;
218 ev.u.input.size = len;
219 if(len > sizeof(ev.u.input.data)){
220 APPL_TRACE_WARNING("%s:report size greater than allowed size",__FUNCTION__);
223 memcpy(ev.u.input.data, rpt, len);
224 return uhid_write(fd, &ev);
229 /*******************************************************************************
231 ** Function bta_hh_co_open
233 ** Description When connection is opened, this call-out function is executed
234 ** by HH to do platform specific initialization.
237 *******************************************************************************/
238 void bta_hh_co_open(UINT8 dev_handle, UINT8 sub_class, tBTA_HH_ATTR_MASK attr_mask,
242 btif_hh_device_t *p_dev = NULL;
244 if (dev_handle == BTA_HH_INVALID_HANDLE) {
245 APPL_TRACE_WARNING("%s: Oops, dev_handle (%d) is invalid...", __FUNCTION__, dev_handle);
249 for (i = 0; i < BTIF_HH_MAX_HID; i++) {
250 p_dev = &btif_hh_cb.devices[i];
251 if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && p_dev->dev_handle == dev_handle) {
252 // We found a device with the same handle. Must be a device reconnected.
253 APPL_TRACE_WARNING("%s: Found an existing device with the same handle "
254 "dev_status = %d",__FUNCTION__,
256 APPL_TRACE_WARNING("%s: bd_addr = [%02X:%02X:%02X:%02X:%02X:]", __FUNCTION__,
257 p_dev->bd_addr.address[0], p_dev->bd_addr.address[1], p_dev->bd_addr.address[2],
258 p_dev->bd_addr.address[3], p_dev->bd_addr.address[4]);
259 APPL_TRACE_WARNING("%s: attr_mask = 0x%04x, sub_class = 0x%02x, app_id = %d",
260 __FUNCTION__, p_dev->attr_mask, p_dev->sub_class, p_dev->app_id);
263 p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC);
265 APPL_TRACE_ERROR("%s: Error: failed to open uhid, err:%s",
266 __FUNCTION__,strerror(errno));
268 APPL_TRACE_DEBUG("%s: uhid fd = %d", __FUNCTION__, p_dev->fd);
270 p_dev->hh_keep_polling = 1;
271 p_dev->hh_poll_thread_id = create_thread(btif_hh_poll_event_thread, p_dev);
278 // Did not find a device reconnection case. Find an empty slot now.
279 for (i = 0; i < BTIF_HH_MAX_HID; i++) {
280 if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_UNKNOWN) {
281 p_dev = &btif_hh_cb.devices[i];
282 p_dev->dev_handle = dev_handle;
283 p_dev->attr_mask = attr_mask;
284 p_dev->sub_class = sub_class;
285 p_dev->app_id = app_id;
286 p_dev->local_vup = FALSE;
288 btif_hh_cb.device_num++;
289 // This is a new device,open the uhid driver now.
290 p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC);
292 APPL_TRACE_ERROR("%s: Error: failed to open uhid, err:%s",
293 __FUNCTION__,strerror(errno));
295 APPL_TRACE_DEBUG("%s: uhid fd = %d", __FUNCTION__, p_dev->fd);
296 p_dev->hh_keep_polling = 1;
297 p_dev->hh_poll_thread_id = create_thread(btif_hh_poll_event_thread, p_dev);
307 APPL_TRACE_ERROR("%s: Error: too many HID devices are connected", __FUNCTION__);
311 p_dev->dev_status = BTHH_CONN_STATE_CONNECTED;
312 APPL_TRACE_DEBUG("%s: Return device status %d", __FUNCTION__, p_dev->dev_status);
316 /*******************************************************************************
318 ** Function bta_hh_co_close
320 ** Description When connection is closed, this call-out function is executed
321 ** by HH to do platform specific finalization.
323 ** Parameters dev_handle - device handle
324 ** app_id - application id
327 *******************************************************************************/
328 void bta_hh_co_close(UINT8 dev_handle, UINT8 app_id)
331 btif_hh_device_t *p_dev = NULL;
333 APPL_TRACE_WARNING("%s: dev_handle = %d, app_id = %d", __FUNCTION__, dev_handle, app_id);
334 if (dev_handle == BTA_HH_INVALID_HANDLE) {
335 APPL_TRACE_WARNING("%s: Oops, dev_handle (%d) is invalid...", __FUNCTION__, dev_handle);
339 for (i = 0; i < BTIF_HH_MAX_HID; i++) {
340 p_dev = &btif_hh_cb.devices[i];
341 if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && p_dev->dev_handle == dev_handle) {
342 APPL_TRACE_WARNING("%s: Found an existing device with the same handle "
343 "dev_status = %d, dev_handle =%d"
344 ,__FUNCTION__,p_dev->dev_status
346 btif_hh_close_poll_thread(p_dev);
353 /*******************************************************************************
355 ** Function bta_hh_co_data
357 ** Description This function is executed by BTA when HID host receive a data
360 ** Parameters dev_handle - device handle
361 ** *p_rpt - pointer to the report data
362 ** len - length of report data
363 ** mode - Hid host Protocol Mode
364 ** sub_clas - Device Subclass
365 ** app_id - application id
368 *******************************************************************************/
369 void bta_hh_co_data(UINT8 dev_handle, UINT8 *p_rpt, UINT16 len, tBTA_HH_PROTO_MODE mode,
370 UINT8 sub_class, UINT8 ctry_code, BD_ADDR peer_addr, UINT8 app_id)
372 btif_hh_device_t *p_dev;
375 APPL_TRACE_DEBUG("%s: dev_handle = %d, subclass = 0x%02X, mode = %d, "
376 "ctry_code = %d, app_id = %d",
377 __FUNCTION__, dev_handle, sub_class, mode, ctry_code, app_id);
379 p_dev = btif_hh_find_connected_dev_by_handle(dev_handle);
381 APPL_TRACE_WARNING("%s: Error: unknown HID device handle %d", __FUNCTION__, dev_handle);
384 // Send the HID report to the kernel.
385 if (p_dev->fd >= 0) {
386 bta_hh_co_write(p_dev->fd, p_rpt, len);
388 APPL_TRACE_WARNING("%s: Error: fd = %d, len = %d", __FUNCTION__, p_dev->fd, len);
393 /*******************************************************************************
395 ** Function bta_hh_co_send_hid_info
397 ** Description This function is called in btif_hh.c to process DSCP received.
399 ** Parameters dev_handle - device handle
400 ** dscp_len - report descriptor length
401 ** *p_dscp - report descriptor
404 *******************************************************************************/
405 void bta_hh_co_send_hid_info(btif_hh_device_t *p_dev, char *dev_name, UINT16 vendor_id,
406 UINT16 product_id, UINT16 version, UINT8 ctry_code,
407 int dscp_len, UINT8 *p_dscp)
410 struct uhid_event ev;
413 APPL_TRACE_WARNING("%s: Error: fd = %d, dscp_len = %d", __FUNCTION__, p_dev->fd, dscp_len);
417 APPL_TRACE_WARNING("%s: fd = %d, name = [%s], dscp_len = %d", __FUNCTION__,
418 p_dev->fd, dev_name, dscp_len);
419 APPL_TRACE_WARNING("%s: vendor_id = 0x%04x, product_id = 0x%04x, version= 0x%04x,"
420 "ctry_code=0x%02x",__FUNCTION__,
421 vendor_id, product_id,
424 //Create and send hid descriptor to kernel
425 memset(&ev, 0, sizeof(ev));
426 ev.type = UHID_CREATE;
427 strncpy((char*)ev.u.create.name, dev_name, sizeof(ev.u.create.name) - 1);
428 snprintf((char*)ev.u.create.uniq, sizeof(ev.u.create.uniq),
429 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
430 p_dev->bd_addr.address[5], p_dev->bd_addr.address[4],
431 p_dev->bd_addr.address[3], p_dev->bd_addr.address[2],
432 p_dev->bd_addr.address[1], p_dev->bd_addr.address[0]);
433 ev.u.create.rd_size = dscp_len;
434 ev.u.create.rd_data = p_dscp;
435 ev.u.create.bus = BUS_BLUETOOTH;
436 ev.u.create.vendor = vendor_id;
437 ev.u.create.product = product_id;
438 ev.u.create.version = version;
439 ev.u.create.country = ctry_code;
440 result = uhid_write(p_dev->fd, &ev);
442 APPL_TRACE_WARNING("%s: fd = %d, dscp_len = %d, result = %d", __FUNCTION__,
443 p_dev->fd, dscp_len, result);
446 APPL_TRACE_WARNING("%s: Error: failed to send DSCP, result = %d", __FUNCTION__, result);
448 /* The HID report descriptor is corrupted. Close the driver. */
454 #if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE)
455 /*******************************************************************************
457 ** Function bta_hh_le_co_rpt_info
459 ** Description This callout function is to convey the report information on
460 ** a HOGP device to the application. Application can save this
461 ** information in NV if device is bonded and load it back when
464 ** Parameters remote_bda - remote device address
465 ** p_entry - report entry pointer
466 ** app_id - application id
470 *******************************************************************************/
471 void bta_hh_le_co_rpt_info(BD_ADDR remote_bda, tBTA_HH_RPT_CACHE_ENTRY *p_entry, UINT8 app_id)
478 sprintf(bdstr, "%02x:%02x:%02x:%02x:%02x:%02x",
479 remote_bda[0], remote_bda[1], remote_bda[2],
480 remote_bda[3], remote_bda[4], remote_bda[5]);
482 size_t len = btif_config_get_bin_length(bdstr, "HidReport");
483 if (len >= sizeof(tBTA_HH_RPT_CACHE_ENTRY) && len <= sizeof(sReportCache))
485 btif_config_get_bin(bdstr, "HidReport", (uint8_t *)sReportCache, &len);
486 idx = len / sizeof(tBTA_HH_RPT_CACHE_ENTRY);
489 if (idx < BTA_HH_NV_LOAD_MAX)
491 memcpy(&sReportCache[idx++], p_entry, sizeof(tBTA_HH_RPT_CACHE_ENTRY));
492 btif_config_set_bin(bdstr, "HidReport", (const uint8_t *)sReportCache,
493 idx * sizeof(tBTA_HH_RPT_CACHE_ENTRY));
494 BTIF_TRACE_DEBUG("%s() - Saving report; dev=%s, idx=%d", __FUNCTION__, bdstr, idx);
499 /*******************************************************************************
501 ** Function bta_hh_le_co_cache_load
503 ** Description This callout function is to request the application to load the
504 ** cached HOGP report if there is any. When cache reading is completed,
505 ** bta_hh_le_ci_cache_load() is called by the application.
507 ** Parameters remote_bda - remote device address
508 ** p_num_rpt: number of cached report
509 ** app_id - application id
511 ** Returns the acched report array
513 *******************************************************************************/
514 tBTA_HH_RPT_CACHE_ENTRY * bta_hh_le_co_cache_load (BD_ADDR remote_bda,
515 UINT8 *p_num_rpt, UINT8 app_id)
522 sprintf(bdstr, "%02x:%02x:%02x:%02x:%02x:%02x",
523 remote_bda[0], remote_bda[1], remote_bda[2],
524 remote_bda[3], remote_bda[4], remote_bda[5]);
526 size_t len = btif_config_get_bin_length(bdstr, "HidReport");
527 if (!p_num_rpt && len < sizeof(tBTA_HH_RPT_CACHE_ENTRY))
530 if (len > sizeof(sReportCache))
531 len = sizeof(sReportCache);
532 btif_config_get_bin(bdstr, "HidReport", (uint8_t *)sReportCache, &len);
533 *p_num_rpt = len / sizeof(tBTA_HH_RPT_CACHE_ENTRY);
535 BTIF_TRACE_DEBUG("%s() - Loaded %d reports; dev=%s", __FUNCTION__, *p_num_rpt, bdstr);
540 /*******************************************************************************
542 ** Function bta_hh_le_co_reset_rpt_cache
544 ** Description This callout function is to reset the HOGP device cache.
546 ** Parameters remote_bda - remote device address
550 *******************************************************************************/
551 void bta_hh_le_co_reset_rpt_cache (BD_ADDR remote_bda, UINT8 app_id)
556 sprintf(bdstr, "%02x:%02x:%02x:%02x:%02x:%02x",
557 remote_bda[0], remote_bda[1], remote_bda[2],
558 remote_bda[3], remote_bda[4], remote_bda[5]);
559 btif_config_remove(bdstr, "HidReport");
561 BTIF_TRACE_DEBUG("%s() - Reset cache for bda %s", __FUNCTION__, bdstr);
563 #endif /* #if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE) */