OSDN Git Service

Fix build issues in bluedroid on master
[android-x86/system-bt.git] / btif / co / bta_hh_co.c
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 <unistd.h>
31 #include "btif_hh.h"
32 #include "bta_api.h"
33 #include "bta_hh_api.h"
34 #include "btif_util.h"
35 #include "bta_hh_co.h"
36
37 const char *dev_path = "/dev/uhid";
38
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];
43 #endif
44
45 /*Internal function to perform UHID write and error checking*/
46 static int uhid_write(int fd, const struct uhid_event *ev)
47 {
48     ssize_t ret;
49     ret = write(fd, ev, sizeof(*ev));
50     if (ret < 0){
51         int rtn = -errno;
52         APPL_TRACE_ERROR("%s: Cannot write to uhid:%s", __FUNCTION__, strerror(errno));
53         return rtn;
54     } else if (ret != sizeof(*ev)) {
55         APPL_TRACE_ERROR("%s: Wrong size written to uhid: %ld != %lu",
56                                                     __FUNCTION__, ret, sizeof(*ev));
57         return -EFAULT;
58     } else {
59         return 0;
60     }
61 }
62
63 /* Internal function to parse the events received from UHID driver*/
64 static int uhid_event(btif_hh_device_t *p_dev)
65 {
66     struct uhid_event ev;
67     ssize_t ret;
68     memset(&ev, 0, sizeof(ev));
69     if(!p_dev)
70     {
71         APPL_TRACE_ERROR("%s: Device not found",__FUNCTION__)
72         return -1;
73     }
74     ret = read(p_dev->fd, &ev, sizeof(ev));
75     if (ret == 0) {
76         APPL_TRACE_ERROR("%s: Read HUP on uhid-cdev %s", __FUNCTION__,
77                                                  strerror(errno));
78         return -EFAULT;
79     } else if (ret < 0) {
80         APPL_TRACE_ERROR("%s:Cannot read uhid-cdev: %s", __FUNCTION__,
81                                                 strerror(errno));
82         return -errno;
83     } else if (ret != sizeof(ev)) {
84         APPL_TRACE_ERROR("%s:Invalid size read from uhid-dev: %ld != %lu",
85                             __FUNCTION__, ret, sizeof(ev));
86         return -EFAULT;
87     }
88
89     switch (ev.type) {
90     case UHID_START:
91         APPL_TRACE_DEBUG("UHID_START from uhid-dev\n");
92         break;
93     case UHID_STOP:
94         APPL_TRACE_DEBUG("UHID_STOP from uhid-dev\n");
95         break;
96     case UHID_OPEN:
97         APPL_TRACE_DEBUG("UHID_OPEN from uhid-dev\n");
98         break;
99     case UHID_CLOSE:
100         APPL_TRACE_DEBUG("UHID_CLOSE from uhid-dev\n");
101         break;
102     case UHID_OUTPUT:
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);
110         else
111             btif_hh_setreport(p_dev,BTHH_INPUT_REPORT,ev.u.output.size,ev.u.output.data);
112            break;
113     case UHID_OUTPUT_EV:
114         APPL_TRACE_DEBUG("UHID_OUTPUT_EV from uhid-dev\n");
115         break;
116     case UHID_FEATURE:
117         APPL_TRACE_DEBUG("UHID_FEATURE from uhid-dev\n");
118         break;
119     case UHID_FEATURE_ANSWER:
120         APPL_TRACE_DEBUG("UHID_FEATURE_ANSWER from uhid-dev\n");
121         break;
122
123     default:
124         APPL_TRACE_DEBUG("Invalid event from uhid-dev: %u\n", ev.type);
125     }
126
127     return 0;
128 }
129
130 /*******************************************************************************
131 **
132 ** Function create_thread
133 **
134 ** Description creat a select loop
135 **
136 ** Returns pthread_t
137 **
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;
142
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 )
147     {
148         APPL_TRACE_ERROR("pthread_create : %s", strerror(errno));
149         return -1;
150     }
151     APPL_TRACE_DEBUG("create_thread: thread created successfully");
152     return thread_id;
153 }
154
155 /*******************************************************************************
156 **
157 ** Function btif_hh_poll_event_thread
158 **
159 ** Description the polling thread which polls for event from UHID driver
160 **
161 ** Returns void
162 **
163 *******************************************************************************/
164 static void *btif_hh_poll_event_thread(void *arg)
165 {
166
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];
170     int ret;
171     pfds[0].fd = p_dev->fd;
172     pfds[0].events = POLLIN;
173
174     while(p_dev->hh_keep_polling){
175         ret = poll(pfds, 1, 50);
176         if (ret < 0) {
177             APPL_TRACE_ERROR("%s: Cannot poll for fds: %s\n", __FUNCTION__, strerror(errno));
178             break;
179         }
180         if (pfds[0].revents & POLLIN) {
181             APPL_TRACE_DEBUG("btif_hh_poll_event_thread: POLLIN");
182             ret = uhid_event(p_dev);
183             if (ret){
184                 break;
185             }
186         }
187     }
188
189     p_dev->hh_poll_thread_id = -1;
190     return 0;
191 }
192
193 static inline void btif_hh_close_poll_thread(btif_hh_device_t *p_dev)
194 {
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);
199
200     return;
201 }
202
203 void bta_hh_co_destroy(int fd)
204 {
205     struct uhid_event ev;
206     memset(&ev, 0, sizeof(ev));
207     ev.type = UHID_DESTROY;
208     uhid_write(fd, &ev);
209     close(fd);
210 }
211
212 int bta_hh_co_write(int fd, UINT8* rpt, UINT16 len)
213 {
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__);
221         return -1;
222     }
223     memcpy(ev.u.input.data, rpt, len);
224     return uhid_write(fd, &ev);
225
226 }
227
228
229 /*******************************************************************************
230 **
231 ** Function      bta_hh_co_open
232 **
233 ** Description   When connection is opened, this call-out function is executed
234 **               by HH to do platform specific initialization.
235 **
236 ** Returns       void.
237 *******************************************************************************/
238 void bta_hh_co_open(UINT8 dev_handle, UINT8 sub_class, tBTA_HH_ATTR_MASK attr_mask,
239                     UINT8 app_id)
240 {
241     UINT32 i;
242     btif_hh_device_t *p_dev = NULL;
243
244     if (dev_handle == BTA_HH_INVALID_HANDLE) {
245         APPL_TRACE_WARNING("%s: Oops, dev_handle (%d) is invalid...", __FUNCTION__, dev_handle);
246         return;
247     }
248
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__,
255                                                                 p_dev->dev_status);
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);
261
262             if(p_dev->fd<0) {
263                 p_dev->fd = open(dev_path, O_RDWR | O_CLOEXEC);
264                 if (p_dev->fd < 0){
265                     APPL_TRACE_ERROR("%s: Error: failed to open uhid, err:%s",
266                                                                     __FUNCTION__,strerror(errno));
267                 }else
268                     APPL_TRACE_DEBUG("%s: uhid fd = %d", __FUNCTION__, p_dev->fd);
269             }
270             p_dev->hh_keep_polling = 1;
271             p_dev->hh_poll_thread_id = create_thread(btif_hh_poll_event_thread, p_dev);
272             break;
273         }
274         p_dev = NULL;
275     }
276
277     if (p_dev == NULL) {
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;
287
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);
291                 if (p_dev->fd < 0){
292                     APPL_TRACE_ERROR("%s: Error: failed to open uhid, err:%s",
293                                                                     __FUNCTION__,strerror(errno));
294                 }else{
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);
298                 }
299
300
301                 break;
302             }
303         }
304     }
305
306     if (p_dev == NULL) {
307         APPL_TRACE_ERROR("%s: Error: too many HID devices are connected", __FUNCTION__);
308         return;
309     }
310
311     p_dev->dev_status = BTHH_CONN_STATE_CONNECTED;
312     APPL_TRACE_DEBUG("%s: Return device status %d", __FUNCTION__, p_dev->dev_status);
313 }
314
315
316 /*******************************************************************************
317 **
318 ** Function      bta_hh_co_close
319 **
320 ** Description   When connection is closed, this call-out function is executed
321 **               by HH to do platform specific finalization.
322 **
323 ** Parameters    dev_handle  - device handle
324 **                  app_id      - application id
325 **
326 ** Returns          void.
327 *******************************************************************************/
328 void bta_hh_co_close(UINT8 dev_handle, UINT8 app_id)
329 {
330     UINT32 i;
331     btif_hh_device_t *p_dev = NULL;
332
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);
336         return;
337     }
338
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
345                                                         ,p_dev->dev_handle);
346             btif_hh_close_poll_thread(p_dev);
347             break;
348         }
349      }
350 }
351
352
353 /*******************************************************************************
354 **
355 ** Function         bta_hh_co_data
356 **
357 ** Description      This function is executed by BTA when HID host receive a data
358 **                  report.
359 **
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
366 **
367 ** Returns          void
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)
371 {
372     btif_hh_device_t *p_dev;
373     UNUSED(peer_addr);
374
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);
378
379     p_dev = btif_hh_find_connected_dev_by_handle(dev_handle);
380     if (p_dev == NULL) {
381         APPL_TRACE_WARNING("%s: Error: unknown HID device handle %d", __FUNCTION__, dev_handle);
382         return;
383     }
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);
387     }else {
388         APPL_TRACE_WARNING("%s: Error: fd = %d, len = %d", __FUNCTION__, p_dev->fd, len);
389     }
390 }
391
392
393 /*******************************************************************************
394 **
395 ** Function         bta_hh_co_send_hid_info
396 **
397 ** Description      This function is called in btif_hh.c to process DSCP received.
398 **
399 ** Parameters       dev_handle  - device handle
400 **                  dscp_len    - report descriptor length
401 **                  *p_dscp     - report descriptor
402 **
403 ** Returns          void
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)
408 {
409     int result;
410     struct uhid_event ev;
411
412     if (p_dev->fd < 0) {
413         APPL_TRACE_WARNING("%s: Error: fd = %d, dscp_len = %d", __FUNCTION__, p_dev->fd, dscp_len);
414         return;
415     }
416
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,
422                                                                     version, ctry_code);
423
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);
441
442     APPL_TRACE_WARNING("%s: fd = %d, dscp_len = %d, result = %d", __FUNCTION__,
443                                                                     p_dev->fd, dscp_len, result);
444
445     if (result) {
446         APPL_TRACE_WARNING("%s: Error: failed to send DSCP, result = %d", __FUNCTION__, result);
447
448         /* The HID report descriptor is corrupted. Close the driver. */
449         close(p_dev->fd);
450         p_dev->fd = -1;
451     }
452 }
453
454 #if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE)
455 /*******************************************************************************
456 **
457 ** Function         bta_hh_le_co_rpt_info
458 **
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
462 **                  stack reboot.
463 **
464 ** Parameters       remote_bda  - remote device address
465 **                  p_entry     - report entry pointer
466 **                  app_id      - application id
467 **
468 ** Returns          void.
469 **
470 *******************************************************************************/
471 void bta_hh_le_co_rpt_info(BD_ADDR remote_bda, tBTA_HH_RPT_CACHE_ENTRY *p_entry, UINT8 app_id)
472 {
473     UNUSED(app_id);
474
475     unsigned idx = 0;
476
477     bdstr_t bdstr;
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]);
481
482     size_t len = btif_config_get_bin_length(bdstr, "HidReport");
483     if (len >= sizeof(tBTA_HH_RPT_CACHE_ENTRY) && len <= sizeof(sReportCache))
484     {
485         btif_config_get_bin(bdstr, "HidReport", (uint8_t *)sReportCache, &len);
486         idx = len / sizeof(tBTA_HH_RPT_CACHE_ENTRY);
487     }
488
489     if (idx < BTA_HH_NV_LOAD_MAX)
490     {
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);
495     }
496 }
497
498
499 /*******************************************************************************
500 **
501 ** Function         bta_hh_le_co_cache_load
502 **
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.
506 **
507 ** Parameters       remote_bda  - remote device address
508 **                  p_num_rpt: number of cached report
509 **                  app_id      - application id
510 **
511 ** Returns          the acched report array
512 **
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)
516 {
517     UNUSED(app_id);
518
519     unsigned idx = 0;
520
521     bdstr_t bdstr;
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]);
525
526     size_t len = btif_config_get_bin_length(bdstr, "HidReport");
527     if (!p_num_rpt && len < sizeof(tBTA_HH_RPT_CACHE_ENTRY))
528         return NULL;
529
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);
534
535     BTIF_TRACE_DEBUG("%s() - Loaded %d reports; dev=%s", __FUNCTION__, *p_num_rpt, bdstr);
536
537     return sReportCache;
538 }
539
540 /*******************************************************************************
541 **
542 ** Function         bta_hh_le_co_reset_rpt_cache
543 **
544 ** Description      This callout function is to reset the HOGP device cache.
545 **
546 ** Parameters       remote_bda  - remote device address
547 **
548 ** Returns          none
549 **
550 *******************************************************************************/
551 void bta_hh_le_co_reset_rpt_cache (BD_ADDR remote_bda, UINT8 app_id)
552 {
553     UNUSED(app_id);
554
555     bdstr_t bdstr;
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");
560
561     BTIF_TRACE_DEBUG("%s() - Reset cache for bda %s", __FUNCTION__, bdstr);
562 }
563 #endif /* #if (BLE_INCLUDED == TRUE && BTA_HH_LE_INCLUDED == TRUE) */
564