OSDN Git Service

a7bdc9c7687b9163dcd10230eb869ad10269ecb3
[android-x86/system-bt.git] / bta / hh / bta_hh_act.cc
1 /******************************************************************************
2  *
3  *  Copyright 2005-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 /******************************************************************************
20  *
21  *  This file contains the HID host action functions.
22  *
23  ******************************************************************************/
24
25 #include "bt_target.h"
26
27 #if (BTA_HH_INCLUDED == TRUE)
28
29 #include <log/log.h>
30 #include <string.h>
31
32 #include "bta_hh_co.h"
33 #include "bta_hh_int.h"
34 #include "bta_sys.h"
35 #include "btm_api.h"
36 #include "l2c_api.h"
37 #include "osi/include/osi.h"
38 #include "utl.h"
39
40 /*****************************************************************************
41  *  Constants
42  ****************************************************************************/
43
44 /*****************************************************************************
45  *  Local Function prototypes
46  ****************************************************************************/
47 static void bta_hh_cback(uint8_t dev_handle, const RawAddress& addr,
48                          uint8_t event, uint32_t data, BT_HDR* pdata);
49 static tBTA_HH_STATUS bta_hh_get_trans_status(uint32_t result);
50
51 #if (BTA_HH_DEBUG == TRUE)
52 static const char* bta_hh_get_w4_event(uint16_t event);
53 static const char* bta_hh_hid_event_name(uint16_t event);
54 #endif
55
56 /*****************************************************************************
57  *  Action Functions
58  ****************************************************************************/
59 /*******************************************************************************
60  *
61  * Function         bta_hh_api_enable
62  *
63  * Description      Perform necessary operations to enable HID host.
64  *
65  *
66  * Returns          void
67  *
68  ******************************************************************************/
69 void bta_hh_api_enable(tBTA_HH_DATA* p_data) {
70   tBTA_HH_STATUS status = BTA_HH_ERR;
71   uint8_t xx;
72
73   /* initialize BTE HID */
74   HID_HostInit();
75
76   memset(&bta_hh_cb, 0, sizeof(tBTA_HH_CB));
77
78   HID_HostSetSecurityLevel("", p_data->api_enable.sec_mask);
79
80   /* Register with L2CAP */
81   if (HID_HostRegister(bta_hh_cback) == HID_SUCCESS) {
82     /* store parameters */
83     bta_hh_cb.p_cback = p_data->api_enable.p_cback;
84
85     status = BTA_HH_OK;
86     /* initialize device CB */
87     for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
88       bta_hh_cb.kdev[xx].state = BTA_HH_IDLE_ST;
89       bta_hh_cb.kdev[xx].hid_handle = BTA_HH_INVALID_HANDLE;
90       bta_hh_cb.kdev[xx].index = xx;
91     }
92
93     /* initialize control block map */
94     for (xx = 0; xx < BTA_HH_MAX_KNOWN; xx++)
95       bta_hh_cb.cb_index[xx] = BTA_HH_IDX_INVALID;
96   }
97
98 #if (BTA_HH_LE_INCLUDED == TRUE)
99   if (status == BTA_HH_OK) {
100     bta_hh_le_enable();
101   } else
102 #endif
103   {
104     /* signal BTA call back event */
105     tBTA_HH bta_hh;
106     bta_hh.status = status;
107     (*bta_hh_cb.p_cback)(BTA_HH_ENABLE_EVT, &bta_hh);
108   }
109 }
110 /*******************************************************************************
111  *
112  * Function         bta_hh_api_disable
113  *
114  * Description      Perform necessary operations to disable HID host.
115  *
116  *
117  * Returns          void
118  *
119  ******************************************************************************/
120 void bta_hh_api_disable(void) {
121   uint8_t xx;
122
123   /* service is not enabled */
124   if (bta_hh_cb.p_cback == NULL) return;
125
126   /* no live connection, signal DISC_CMPL_EVT directly */
127   if (!bta_hh_cb.cnt_num) {
128     bta_hh_disc_cmpl();
129   } else /* otherwise, disconnect all live connections */
130   {
131     bta_hh_cb.w4_disable = true;
132
133     for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
134       /* send API_CLOSE event to every connected device */
135       if (bta_hh_cb.kdev[xx].state == BTA_HH_CONN_ST) {
136         /* disconnect all connected devices */
137         bta_hh_sm_execute(&bta_hh_cb.kdev[xx], BTA_HH_API_CLOSE_EVT, NULL);
138       }
139     }
140   }
141
142   return;
143 }
144
145 /*******************************************************************************
146  *
147  * Function         bta_hh_disc_cmpl
148  *
149  * Description      All connections have been closed, disable service.
150  *
151  *
152  * Returns          void
153  *
154  ******************************************************************************/
155 void bta_hh_disc_cmpl(void) {
156 #if (BTA_HH_LE_INCLUDED == TRUE)
157   HID_HostDeregister();
158   bta_hh_le_deregister();
159 #else
160   tBTA_HH_STATUS status = BTA_HH_OK;
161
162   /* Deregister with lower layer */
163   if (HID_HostDeregister() != HID_SUCCESS) status = BTA_HH_ERR;
164
165   bta_hh_cleanup_disable(status);
166 #endif
167 }
168
169 /*******************************************************************************
170  *
171  * Function         bta_hh_sdp_cback
172  *
173  * Description      SDP callback function.
174  *
175  * Returns          void
176  *
177  ******************************************************************************/
178 static void bta_hh_sdp_cback(uint16_t result, uint16_t attr_mask,
179                              tHID_DEV_SDP_INFO* sdp_rec) {
180   tBTA_HH_DEV_CB* p_cb = bta_hh_cb.p_cur;
181   uint8_t hdl = 0;
182   tBTA_HH_STATUS status = BTA_HH_ERR_SDP;
183
184   /* make sure sdp succeeded and hh has not been disabled */
185   if ((result == SDP_SUCCESS) && (p_cb != NULL)) {
186     /* security is required for the connection, add attr_mask bit*/
187     if (p_cb->sec_mask) attr_mask |= HID_SEC_REQUIRED;
188
189 #if (BTA_HH_DEBUG == TRUE)
190     APPL_TRACE_EVENT("%s: p_cb: %d result 0x%02x, attr_mask 0x%02x, handle %x",
191                      __func__, p_cb, result, attr_mask, p_cb->hid_handle);
192 #endif
193
194     /* check to see type of device is supported , and should not been added
195      * before */
196     if (bta_hh_tod_spt(p_cb, sdp_rec->sub_class)) {
197       /* if not added before */
198       if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
199         /*  add device/update attr_mask information */
200         if (HID_HostAddDev(p_cb->addr, attr_mask, &hdl) == HID_SUCCESS) {
201           status = BTA_HH_OK;
202           /* update cb_index[] map */
203           bta_hh_cb.cb_index[hdl] = p_cb->index;
204         } else {
205           p_cb->app_id = 0;
206         }
207       } else {
208         hdl = p_cb->hid_handle;
209       }
210       /* else : incoming connection after SDP should update the SDP information
211        * as well */
212
213       if (p_cb->app_id != 0) {
214         /* update cb information with attr_mask, dscp_info etc. */
215         bta_hh_add_device_to_list(p_cb, hdl, attr_mask, &sdp_rec->dscp_info,
216                                   sdp_rec->sub_class, sdp_rec->ssr_max_latency,
217                                   sdp_rec->ssr_min_tout, p_cb->app_id);
218
219         p_cb->dscp_info.ctry_code = sdp_rec->ctry_code;
220
221         status = BTA_HH_OK;
222       }
223
224     } else /* type of device is not supported */
225       status = BTA_HH_ERR_TOD_UNSPT;
226   }
227
228   /* free disc_db when SDP is completed */
229   osi_free_and_reset((void**)&bta_hh_cb.p_disc_db);
230
231   /* send SDP_CMPL_EVT into state machine */
232   tBTA_HH_DATA bta_hh_data;
233   bta_hh_data.status = status;
234   bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, &bta_hh_data);
235
236   return;
237 }
238 /*******************************************************************************
239  *
240  * Function         bta_hh_di_sdp_cback
241  *
242  * Description      SDP DI callback function.
243  *
244  * Returns          void
245  *
246  ******************************************************************************/
247 static void bta_hh_di_sdp_cback(uint16_t result) {
248   tBTA_HH_DEV_CB* p_cb = bta_hh_cb.p_cur;
249   tBTA_HH_STATUS status = BTA_HH_ERR_SDP;
250   tSDP_DI_GET_RECORD di_rec;
251   tHID_STATUS ret;
252 #if (BTA_HH_DEBUG == TRUE)
253   APPL_TRACE_EVENT("%s: p_cb: %d result 0x%02x", __func__, p_cb, result);
254 #endif
255
256   /* if DI record does not exist on remote device, vendor_id in
257    * tBTA_HH_DEV_DSCP_INFO will be set to 0xffff and we will allow the
258    * connection to go through. Spec mandates that DI record be set, but many
259    * HID devices do not set this. So for IOP purposes, we allow the connection
260    * to go through and update the DI record to invalid DI entry.
261    */
262   if (((result == SDP_SUCCESS) || (result == SDP_NO_RECS_MATCH)) &&
263       (p_cb != NULL)) {
264     if (result == SDP_SUCCESS &&
265         SDP_GetNumDiRecords(bta_hh_cb.p_disc_db) != 0) {
266       /* always update information with primary DI record */
267       if (SDP_GetDiRecord(1, &di_rec, bta_hh_cb.p_disc_db) == SDP_SUCCESS) {
268         bta_hh_update_di_info(p_cb, di_rec.rec.vendor, di_rec.rec.product,
269                               di_rec.rec.version, 0);
270       }
271
272     } else /* no DI recrod available */
273     {
274       bta_hh_update_di_info(p_cb, BTA_HH_VENDOR_ID_INVALID, 0, 0, 0);
275     }
276
277     ret = HID_HostGetSDPRecord(p_cb->addr, bta_hh_cb.p_disc_db,
278                                p_bta_hh_cfg->sdp_db_size, bta_hh_sdp_cback);
279     if (ret == HID_SUCCESS) {
280       status = BTA_HH_OK;
281     } else {
282 #if (BTA_HH_DEBUG == TRUE)
283       APPL_TRACE_DEBUG("%s:  HID_HostGetSDPRecord failed: Status 0x%2x",
284                        __func__, ret);
285 #endif
286     }
287   }
288
289   if (status != BTA_HH_OK) {
290     osi_free_and_reset((void**)&bta_hh_cb.p_disc_db);
291     /* send SDP_CMPL_EVT into state machine */
292     tBTA_HH_DATA bta_hh_data;
293     bta_hh_data.status = status;
294     bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, &bta_hh_data);
295   }
296   return;
297 }
298
299 /*******************************************************************************
300  *
301  * Function         bta_hh_start_sdp
302  *
303  * Description      Start SDP service search, and obtain necessary SDP records.
304  *                  Only one SDP service search request is allowed at the same
305  *                  time. For every BTA_HhOpen API call, do SDP first unless SDP
306  *                  has been done previously.
307  *
308  * Returns          void
309  *
310  ******************************************************************************/
311 void bta_hh_start_sdp(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
312   tBTA_HH_STATUS status = BTA_HH_ERR_SDP;
313   uint8_t hdl;
314
315   p_cb->sec_mask = p_data->api_conn.sec_mask;
316   p_cb->mode = p_data->api_conn.mode;
317   bta_hh_cb.p_cur = p_cb;
318
319 #if (BTA_HH_LE_INCLUDED == TRUE)
320   if (bta_hh_is_le_device(p_cb, p_data->api_conn.bd_addr)) {
321     bta_hh_le_open_conn(p_cb, p_data->api_conn.bd_addr);
322     return;
323   }
324 #endif
325
326   /* if previously virtually cabled device, skip SDP */
327   if (p_cb->app_id) {
328     status = BTA_HH_OK;
329 #if (BTA_HH_DEBUG == TRUE)
330     APPL_TRACE_DEBUG("%s: skip SDP for known devices", __func__);
331 #endif
332     if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
333       if (HID_HostAddDev(p_cb->addr, p_cb->attr_mask, &hdl) == HID_SUCCESS) {
334         /* update device CB with newly register device handle */
335         bta_hh_add_device_to_list(p_cb, hdl, p_cb->attr_mask, NULL,
336                                   p_cb->sub_class,
337                                   p_cb->dscp_info.ssr_max_latency,
338                                   p_cb->dscp_info.ssr_min_tout, p_cb->app_id);
339         /* update cb_index[] map */
340         bta_hh_cb.cb_index[hdl] = p_cb->index;
341       } else
342         status = BTA_HH_ERR_NO_RES;
343     }
344     tBTA_HH_DATA bta_hh_data;
345     bta_hh_data.status = status;
346     bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, &bta_hh_data);
347
348     return;
349   }
350   /* GetSDPRecord. at one time only one SDP precedure can be active */
351   else if (!bta_hh_cb.p_disc_db) {
352     bta_hh_cb.p_disc_db =
353         (tSDP_DISCOVERY_DB*)osi_malloc(p_bta_hh_cfg->sdp_db_size);
354     bta_hh_cb.p_cur = p_cb;
355     /* do DI discovery first */
356     if (SDP_DiDiscover(p_data->api_conn.bd_addr, bta_hh_cb.p_disc_db,
357                        p_bta_hh_cfg->sdp_db_size,
358                        bta_hh_di_sdp_cback) != SDP_SUCCESS) {
359 #if (BTA_HH_DEBUG == TRUE)
360       APPL_TRACE_DEBUG("%s:  SDP_DiDiscover failed: Status 0x%2X", __func__,
361                        status);
362 #endif
363       status = BTA_HH_ERR_SDP;
364       osi_free_and_reset((void**)&bta_hh_cb.p_disc_db);
365     } else {
366       status = BTA_HH_OK;
367     }
368   } else if (bta_hh_cb.p_disc_db) {
369     /* It is possible that there is incoming/outgoing collision case. DUT
370      * initiated
371      * HID connection at same time remote has connected L2CAP for HID control,
372      * so SDP would be in progress, when this flow reaches here. Just do nothing
373      * when the code reaches here, and ongoing SDP completion or failure will
374      * handle this case.
375      */
376     APPL_TRACE_DEBUG("%s: ignoring as SDP already in progress", __func__);
377     return;
378   }
379
380   if (status != BTA_HH_OK) {
381     tBTA_HH_DATA bta_hh_data;
382     bta_hh_data.status = status;
383     bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, &bta_hh_data);
384   }
385
386   return;
387 }
388 /*******************************************************************************
389  *
390  * Function         bta_hh_sdp_cmpl
391  *
392  * Description      When SDP completes, initiate a connection or report an error
393  *                  depending on the SDP result.
394  *
395  *
396  * Returns          void
397  *
398  ******************************************************************************/
399 void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
400   tBTA_HH_CONN conn_dat;
401   tBTA_HH_STATUS status = p_data->status;
402
403 #if (BTA_HH_DEBUG == TRUE)
404   APPL_TRACE_DEBUG("%s:  status 0x%2X", __func__, p_data->status);
405 #endif
406
407   /* initialize call back data */
408   memset((void*)&conn_dat, 0, sizeof(tBTA_HH_CONN));
409   conn_dat.handle = p_cb->hid_handle;
410   conn_dat.bda = p_cb->addr;
411
412   /* if SDP compl success */
413   if (status == BTA_HH_OK) {
414     /* not incoming connection doing SDP, initiate a HID connection */
415     if (!p_cb->incoming_conn) {
416       tHID_STATUS ret;
417       /* set security level */
418       HID_HostSetSecurityLevel("", p_cb->sec_mask);
419
420       /* open HID connection */
421       ret = HID_HostOpenDev(p_cb->hid_handle);
422       APPL_TRACE_DEBUG("%s: HID_HostOpenDev returned=%d", __func__, ret);
423       if (ret == HID_SUCCESS || ret == HID_ERR_ALREADY_CONN) {
424         status = BTA_HH_OK;
425       } else if (ret == HID_ERR_CONN_IN_PROCESS) {
426         /* Connection already in progress, return from here, SDP
427          * will be performed after connection is completed.
428          */
429         APPL_TRACE_DEBUG("%s: connection already in progress", __func__);
430         return;
431       } else {
432 #if (BTA_HH_DEBUG == TRUE)
433         APPL_TRACE_DEBUG("%s: HID_HostOpenDev failed: Status 0x%2X", __func__,
434                          ret);
435 #endif
436         /* open fail, remove device from management device list */
437         HID_HostRemoveDev(p_cb->hid_handle);
438         status = BTA_HH_ERR;
439       }
440     } else /* incoming connection SDP finish */
441     {
442       bta_hh_sm_execute(p_cb, BTA_HH_OPEN_CMPL_EVT, NULL);
443     }
444   }
445
446   if (status != BTA_HH_OK) {
447     /* Check if this was incoming connection request  from an unknown device
448      * and connection failed due to missing HID Device SDP UUID
449      * In above condition, disconnect the link as well as remove the
450      * device from list of HID devices
451      */
452     if ((status == BTA_HH_ERR_SDP) && (p_cb->incoming_conn) &&
453         (p_cb->app_id == 0)) {
454       APPL_TRACE_DEBUG("%s: SDP failed for  incoming conn :hndl %d", __func__,
455                        p_cb->incoming_hid_handle);
456       HID_HostRemoveDev(p_cb->incoming_hid_handle);
457     }
458     conn_dat.status = status;
459     (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
460
461     /* move state machine W4_CONN ->IDLE */
462     bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
463
464     /* if this is an outgoing connection to an unknown device, clean up cb */
465     if (p_cb->app_id == 0 && !p_cb->incoming_conn) {
466       /* clean up device control block */
467       bta_hh_clean_up_kdev(p_cb);
468     }
469 #if (BTA_HH_DEBUG == TRUE)
470     bta_hh_trace_dev_db();
471 #endif
472   }
473   return;
474 }
475
476 /*******************************************************************************
477  *
478  * Function         bta_hh_api_disc_act
479  *
480  * Description      HID Host initiate a disconnection.
481  *
482  *
483  * Returns          void
484  *
485  ******************************************************************************/
486 void bta_hh_api_disc_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
487   tBTA_HH_CBDATA disc_dat;
488   tHID_STATUS status;
489
490 #if (BTA_HH_LE_INCLUDED == TRUE)
491   if (p_cb->is_le_device)
492     bta_hh_le_api_disc_act(p_cb);
493   else
494 #endif
495   {
496     /* found an active connection */
497     disc_dat.handle =
498         p_data ? (uint8_t)p_data->hdr.layer_specific : p_cb->hid_handle;
499     disc_dat.status = BTA_HH_ERR;
500
501     status = HID_HostCloseDev(disc_dat.handle);
502
503     if (status) {
504       tBTA_HH bta_hh;
505       bta_hh.dev_status = disc_dat;
506       (*bta_hh_cb.p_cback)(BTA_HH_CLOSE_EVT, &bta_hh);
507     }
508   }
509
510   return;
511 }
512 /*******************************************************************************
513  *
514  * Function         bta_hh_open_cmpl_act
515  *
516  * Description      HID host connection completed
517  *
518  *
519  * Returns          void
520  *
521  ******************************************************************************/
522 void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
523   tBTA_HH_CONN conn;
524   uint8_t dev_handle =
525       p_data ? (uint8_t)p_data->hid_cback.hdr.layer_specific : p_cb->hid_handle;
526
527   memset((void*)&conn, 0, sizeof(tBTA_HH_CONN));
528   conn.handle = dev_handle;
529   conn.bda = p_cb->addr;
530
531   /* increase connection number */
532   bta_hh_cb.cnt_num++;
533
534   /* initialize device driver */
535   bta_hh_co_open(p_cb->hid_handle, p_cb->sub_class, p_cb->attr_mask,
536                  p_cb->app_id);
537
538 #if (BTA_HH_LE_INCLUDED == TRUE)
539   conn.status = p_cb->status;
540   conn.le_hid = p_cb->is_le_device;
541   conn.scps_supported = p_cb->scps_supported;
542
543   if (!p_cb->is_le_device)
544 #endif
545   {
546     /* inform role manager */
547     bta_sys_conn_open(BTA_ID_HH, p_cb->app_id, p_cb->addr);
548   }
549   /* set protocol mode when not default report mode */
550   if (p_cb->mode != BTA_HH_PROTO_RPT_MODE
551 #if (BTA_HH_LE_INCLUDED == TRUE)
552       && !p_cb->is_le_device
553 #endif
554       ) {
555     if ((HID_HostWriteDev(dev_handle, HID_TRANS_SET_PROTOCOL,
556                           HID_PAR_PROTOCOL_BOOT_MODE, 0, 0, NULL)) !=
557         HID_SUCCESS) {
558       /* HID connection is up, while SET_PROTO fail */
559       conn.status = BTA_HH_ERR_PROTO;
560       (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn);
561     } else {
562       conn.status = BTA_HH_OK;
563       p_cb->w4_evt = BTA_HH_OPEN_EVT;
564     }
565   } else
566     (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn);
567
568   p_cb->incoming_conn = false;
569   p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
570 }
571 /*******************************************************************************
572  *
573  * Function         bta_hh_open_act
574  *
575  * Description      HID host receive HID_OPEN_EVT .
576  *
577  *
578  * Returns          void
579  *
580  ******************************************************************************/
581 void bta_hh_open_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
582   tBTA_HH_API_CONN conn_data;
583
584   uint8_t dev_handle =
585       p_data ? (uint8_t)p_data->hid_cback.hdr.layer_specific : p_cb->hid_handle;
586
587 #if (BTA_HH_DEBUG == TRUE)
588   APPL_TRACE_EVENT("%s:  Device[%d] connected", __func__, dev_handle);
589 #endif
590
591   /* SDP has been done */
592   if (p_cb->app_id != 0) {
593     bta_hh_sm_execute(p_cb, BTA_HH_OPEN_CMPL_EVT, p_data);
594   } else
595   /*  app_id == 0 indicates an incoming conenction request arrives without SDP
596    *  performed, do it first
597    */
598   {
599     p_cb->incoming_conn = true;
600     /* store the handle here in case sdp fails - need to disconnect */
601     p_cb->incoming_hid_handle = dev_handle;
602
603     memset(&conn_data, 0, sizeof(tBTA_HH_API_CONN));
604     conn_data.bd_addr = p_cb->addr;
605     bta_hh_start_sdp(p_cb, (tBTA_HH_DATA*)&conn_data);
606   }
607
608   return;
609 }
610
611 /*******************************************************************************
612  *
613  * Function         bta_hh_data_act
614  *
615  * Description      HID Host process a data report
616  *
617  *
618  * Returns          void
619  *
620  ******************************************************************************/
621 void bta_hh_data_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
622   BT_HDR* pdata = p_data->hid_cback.p_data;
623   uint8_t* p_rpt = (uint8_t*)(pdata + 1) + pdata->offset;
624
625   bta_hh_co_data((uint8_t)p_data->hid_cback.hdr.layer_specific, p_rpt,
626                  pdata->len, p_cb->mode, p_cb->sub_class,
627                  p_cb->dscp_info.ctry_code, p_cb->addr, p_cb->app_id);
628
629   osi_free_and_reset((void**)&pdata);
630 }
631
632 /*******************************************************************************
633  *
634  * Function         bta_hh_handsk_act
635  *
636  * Description      HID Host process a handshake acknoledgement.
637  *
638  *
639  * Returns          void
640  *
641  ******************************************************************************/
642 void bta_hh_handsk_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
643 #if (BTA_HH_DEBUG == TRUE)
644   APPL_TRACE_DEBUG("HANDSHAKE received for: event = %s data= %d",
645                    bta_hh_get_w4_event(p_cb->w4_evt), p_data->hid_cback.data);
646 #endif
647
648   tBTA_HH bta_hh;
649   memset(&bta_hh, 0, sizeof(tBTA_HH));
650
651   switch (p_cb->w4_evt) {
652     /* GET_ transsaction, handshake indicate unsupported request */
653     case BTA_HH_GET_PROTO_EVT:
654       bta_hh.hs_data.rsp_data.proto_mode = BTA_HH_PROTO_UNKNOWN;
655     /* fall through */
656     case BTA_HH_GET_RPT_EVT:
657     case BTA_HH_GET_IDLE_EVT:
658       bta_hh.hs_data.handle = p_cb->hid_handle;
659       /* if handshake gives an OK code for these transaction, fill in UNSUPT */
660       bta_hh.hs_data.status = bta_hh_get_trans_status(p_data->hid_cback.data);
661       if (bta_hh.hs_data.status == BTA_HH_OK)
662         bta_hh.hs_data.status = BTA_HH_HS_TRANS_NOT_SPT;
663
664       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
665       p_cb->w4_evt = 0;
666       break;
667
668     /* acknoledgement from HID device for SET_ transaction */
669     case BTA_HH_SET_RPT_EVT:
670     case BTA_HH_SET_PROTO_EVT:
671     case BTA_HH_SET_IDLE_EVT:
672       bta_hh.dev_status.handle = p_cb->hid_handle;
673       bta_hh.dev_status.status =
674           bta_hh_get_trans_status(p_data->hid_cback.data);
675       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
676       p_cb->w4_evt = 0;
677       break;
678
679     /* SET_PROTOCOL when open connection */
680     case BTA_HH_OPEN_EVT:
681       bta_hh.conn.status =
682           p_data->hid_cback.data ? BTA_HH_ERR_PROTO : BTA_HH_OK;
683       bta_hh.conn.handle = p_cb->hid_handle;
684       bta_hh.conn.bda = p_cb->addr;
685       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
686 #if (BTA_HH_DEBUG == TRUE)
687       bta_hh_trace_dev_db();
688 #endif
689       p_cb->w4_evt = 0;
690       break;
691
692     default:
693       /* unknow transaction handshake response */
694       APPL_TRACE_DEBUG("unknown transaction type");
695       break;
696   }
697
698   /* transaction achknoledgement received, inform PM for mode change */
699   bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
700   return;
701 }
702 /*******************************************************************************
703  *
704  * Function         bta_hh_ctrl_dat_act
705  *
706  * Description      HID Host process a data report from control channel.
707  *
708  *
709  * Returns          void
710  *
711  ******************************************************************************/
712 void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
713   BT_HDR* pdata = p_data->hid_cback.p_data;
714   uint8_t* data = (uint8_t*)(pdata + 1) + pdata->offset;
715   tBTA_HH_HSDATA hs_data;
716
717 #if (BTA_HH_DEBUG == TRUE)
718   APPL_TRACE_DEBUG("Ctrl DATA received w4: event[%s]",
719                    bta_hh_get_w4_event(p_cb->w4_evt));
720 #endif
721   if (pdata->len == 0) {
722     android_errorWriteLog(0x534e4554, "116108738");
723     p_cb->w4_evt = 0;
724     osi_free_and_reset((void**)&pdata);
725     return;
726   }
727   hs_data.status = BTA_HH_OK;
728   hs_data.handle = p_cb->hid_handle;
729
730   switch (p_cb->w4_evt) {
731     case BTA_HH_GET_IDLE_EVT:
732       hs_data.rsp_data.idle_rate = *data;
733       break;
734     case BTA_HH_GET_RPT_EVT:
735       hs_data.rsp_data.p_rpt_data = pdata;
736       break;
737     case BTA_HH_GET_PROTO_EVT:
738       /* match up BTE/BTA report/boot mode def*/
739       hs_data.rsp_data.proto_mode = ((*data) == HID_PAR_PROTOCOL_REPORT)
740                                         ? BTA_HH_PROTO_RPT_MODE
741                                         : BTA_HH_PROTO_BOOT_MODE;
742 #if (BTA_HH_DEBUG == TRUE)
743       APPL_TRACE_DEBUG("GET_PROTOCOL Mode = [%s]",
744                        (hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE)
745                            ? "Report"
746                            : "Boot");
747 #endif
748       break;
749     /* should not expect control DATA for SET_ transaction */
750     case BTA_HH_SET_PROTO_EVT:
751     /* fall through */
752     case BTA_HH_SET_RPT_EVT:
753     /* fall through */
754     case BTA_HH_SET_IDLE_EVT:
755     /* fall through */
756     default:
757 #if (BTA_HH_DEBUG == TRUE)
758       APPL_TRACE_DEBUG("invalid  transaction type for DATA payload: 4_evt[%s]",
759                        bta_hh_get_w4_event(p_cb->w4_evt));
760 #endif
761       break;
762   }
763
764   /* inform PM for mode change */
765   bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
766   bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
767
768   (*bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH*)&hs_data);
769
770   p_cb->w4_evt = 0;
771   osi_free_and_reset((void**)&pdata);
772 }
773
774 /*******************************************************************************
775  *
776  * Function         bta_hh_open_failure
777  *
778  * Description      report HID open failure when at wait for connection state
779  *                  and receive device close event.
780  *
781  *
782  * Returns          void
783  *
784  ******************************************************************************/
785 void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
786   tBTA_HH_CONN conn_dat;
787   uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
788
789   memset(&conn_dat, 0, sizeof(tBTA_HH_CONN));
790   conn_dat.handle = p_cb->hid_handle;
791   conn_dat.status =
792       (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
793   conn_dat.bda = p_cb->addr;
794   HID_HostCloseDev(p_cb->hid_handle);
795
796   /* Report OPEN fail event */
797   (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
798
799 #if (BTA_HH_DEBUG == TRUE)
800   bta_hh_trace_dev_db();
801 #endif
802   /* clean up control block, but retain SDP info and device handle */
803   p_cb->vp = false;
804   p_cb->w4_evt = 0;
805
806   /* if no connection is active and HH disable is signaled, disable service */
807   if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
808     bta_hh_disc_cmpl();
809   }
810
811   /* Error in opening hid connection, reset flags */
812   p_cb->incoming_conn = false;
813   p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
814 }
815
816 /*******************************************************************************
817  *
818  * Function         bta_hh_close_act
819  *
820  * Description      HID Host process a close event
821  *
822  *
823  * Returns          void
824  *
825  ******************************************************************************/
826 void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
827   tBTA_HH_CONN conn_dat;
828   tBTA_HH_CBDATA disc_dat = {BTA_HH_OK, 0};
829   uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
830
831   /* if HID_HDEV_EVT_VC_UNPLUG was received, report BTA_HH_VC_UNPLUG_EVT */
832   uint16_t event = p_cb->vp ? BTA_HH_VC_UNPLUG_EVT : BTA_HH_CLOSE_EVT;
833
834   disc_dat.handle = p_cb->hid_handle;
835   disc_dat.status = p_data->hid_cback.data;
836
837   /* Check reason for closing */
838   if ((reason & (HID_L2CAP_CONN_FAIL |
839                  HID_L2CAP_REQ_FAIL)) || /* Failure to initialize connection
840                                             (page timeout or l2cap error) */
841       (reason ==
842        HID_ERR_AUTH_FAILED) || /* Authenication error (while initiating) */
843       (reason == HID_ERR_L2CAP_FAILED)) /* Failure creating l2cap connection */
844   {
845     /* Failure in opening connection */
846     conn_dat.handle = p_cb->hid_handle;
847     conn_dat.status =
848         (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
849     conn_dat.bda = p_cb->addr;
850     HID_HostCloseDev(p_cb->hid_handle);
851
852     /* Report OPEN fail event */
853     (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
854
855 #if (BTA_HH_DEBUG == TRUE)
856     bta_hh_trace_dev_db();
857 #endif
858     return;
859   }
860   /* otherwise report CLOSE/VC_UNPLUG event */
861   else {
862     /* finaliza device driver */
863     bta_hh_co_close(p_cb->hid_handle, p_cb->app_id);
864     /* inform role manager */
865     bta_sys_conn_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
866     /* update total conn number */
867     bta_hh_cb.cnt_num--;
868
869     if (disc_dat.status) disc_dat.status = BTA_HH_ERR;
870
871     (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&disc_dat);
872
873     /* if virtually unplug, remove device */
874     if (p_cb->vp) {
875       HID_HostRemoveDev(p_cb->hid_handle);
876       bta_hh_clean_up_kdev(p_cb);
877     }
878
879 #if (BTA_HH_DEBUG == TRUE)
880     bta_hh_trace_dev_db();
881 #endif
882   }
883
884   /* clean up control block, but retain SDP info and device handle */
885   p_cb->vp = false;
886   p_cb->w4_evt = 0;
887
888   /* if no connection is active and HH disable is signaled, disable service */
889   if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
890     bta_hh_disc_cmpl();
891   }
892
893   return;
894 }
895
896 /*******************************************************************************
897  *
898  * Function         bta_hh_get_dscp_act
899  *
900  * Description      Get device report descriptor
901  *
902  *
903  * Returns          void
904  *
905  ******************************************************************************/
906 void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb,
907                          UNUSED_ATTR tBTA_HH_DATA* p_data) {
908 #if (BTA_HH_LE_INCLUDED == TRUE)
909   if (p_cb->is_le_device) {
910     bta_hh_le_get_dscp_act(p_cb);
911   } else
912 #endif
913     (*bta_hh_cb.p_cback)(BTA_HH_GET_DSCP_EVT, (tBTA_HH*)&p_cb->dscp_info);
914 }
915
916 /*******************************************************************************
917  *
918  * Function         bta_hh_maint_dev_act
919  *
920  * Description      HID Host maintain device list.
921  *
922  *
923  * Returns          void
924  *
925  ******************************************************************************/
926 void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
927   tBTA_HH_MAINT_DEV* p_dev_info = &p_data->api_maintdev;
928   tBTA_HH_DEV_INFO dev_info;
929   uint8_t dev_handle;
930
931   dev_info.status = BTA_HH_ERR;
932   dev_info.handle = BTA_HH_INVALID_HANDLE;
933
934   switch (p_dev_info->sub_event) {
935     case BTA_HH_ADD_DEV_EVT: /* add a device */
936       dev_info.bda = p_dev_info->bda;
937       /* initialize callback data */
938       if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
939 #if (BTA_HH_LE_INCLUDED == TRUE)
940         if (bta_hh_is_le_device(p_cb, p_data->api_conn.bd_addr)) {
941           dev_info.handle = bta_hh_le_add_device(p_cb, p_dev_info);
942           if (dev_info.handle != BTA_HH_INVALID_HANDLE)
943             dev_info.status = BTA_HH_OK;
944         } else
945 #endif
946
947             if (HID_HostAddDev(p_dev_info->bda, p_dev_info->attr_mask,
948                                &dev_handle) == HID_SUCCESS) {
949           dev_info.handle = dev_handle;
950           dev_info.status = BTA_HH_OK;
951
952 #if (BTA_HH_LE_INCLUDED == TRUE)
953           /* update DI information */
954           bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
955                                 p_dev_info->dscp_info.product_id,
956                                 p_dev_info->dscp_info.version,
957                                 p_dev_info->dscp_info.flag);
958 #else
959           bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
960                                 p_dev_info->dscp_info.product_id,
961                                 p_dev_info->dscp_info.version, 0);
962
963 #endif
964           /* add to BTA device list */
965           bta_hh_add_device_to_list(
966               p_cb, dev_handle, p_dev_info->attr_mask,
967               &p_dev_info->dscp_info.descriptor, p_dev_info->sub_class,
968               p_dev_info->dscp_info.ssr_max_latency,
969               p_dev_info->dscp_info.ssr_min_tout, p_dev_info->app_id);
970           /* update cb_index[] map */
971           bta_hh_cb.cb_index[dev_handle] = p_cb->index;
972         }
973       } else /* device already been added */
974       {
975         dev_info.handle = p_cb->hid_handle;
976         dev_info.status = BTA_HH_OK;
977       }
978 #if (BTA_HH_DEBUG == TRUE)
979       bta_hh_trace_dev_db();
980 #endif
981
982       break;
983     case BTA_HH_RMV_DEV_EVT: /* remove device */
984       dev_info.handle = (uint8_t)p_dev_info->hdr.layer_specific;
985       dev_info.bda = p_cb->addr;
986
987 #if (BTA_HH_LE_INCLUDED == TRUE)
988       if (p_cb->is_le_device) {
989         bta_hh_le_remove_dev_bg_conn(p_cb);
990         bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
991         bta_hh_clean_up_kdev(p_cb);
992       } else
993 #endif
994       {
995         if (HID_HostRemoveDev(dev_info.handle) == HID_SUCCESS) {
996           dev_info.status = BTA_HH_OK;
997
998           /* remove from known device list in BTA */
999           bta_hh_clean_up_kdev(p_cb);
1000         }
1001       }
1002       break;
1003
1004     default:
1005       APPL_TRACE_DEBUG("invalid command");
1006       break;
1007   }
1008
1009   (*bta_hh_cb.p_cback)(p_dev_info->sub_event, (tBTA_HH*)&dev_info);
1010 }
1011 /*******************************************************************************
1012  *
1013  * Function         bta_hh_write_dev_act
1014  *
1015  * Description      Write device action. can be SET/GET/DATA transaction.
1016  *
1017  * Returns          void
1018  *
1019  ******************************************************************************/
1020 void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
1021   tBTA_HH_CBDATA cbdata = {BTA_HH_OK, 0};
1022   uint16_t event = (p_data->api_sndcmd.t_type - BTA_HH_FST_BTE_TRANS_EVT) +
1023                    BTA_HH_FST_TRANS_CB_EVT;
1024
1025 #if (BTA_HH_LE_INCLUDED == TRUE)
1026   if (p_cb->is_le_device)
1027     bta_hh_le_write_dev_act(p_cb, p_data);
1028   else
1029 #endif
1030   {
1031
1032     cbdata.handle = p_cb->hid_handle;
1033
1034     /* match up BTE/BTA report/boot mode def */
1035     if (p_data->api_sndcmd.t_type == HID_TRANS_SET_PROTOCOL) {
1036       p_data->api_sndcmd.param =
1037           (p_data->api_sndcmd.param == BTA_HH_PROTO_RPT_MODE)
1038               ? HID_PAR_PROTOCOL_REPORT
1039               : HID_PAR_PROTOCOL_BOOT_MODE;
1040     }
1041
1042     if (HID_HostWriteDev(p_cb->hid_handle, p_data->api_sndcmd.t_type,
1043                          p_data->api_sndcmd.param, p_data->api_sndcmd.data,
1044                          p_data->api_sndcmd.rpt_id,
1045                          p_data->api_sndcmd.p_data) != HID_SUCCESS) {
1046       APPL_TRACE_ERROR("HID_HostWriteDev Error ");
1047       cbdata.status = BTA_HH_ERR;
1048
1049       if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL &&
1050           p_data->api_sndcmd.t_type != HID_TRANS_DATA)
1051         (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&cbdata);
1052       else if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1053         (*bta_hh_cb.p_cback)(BTA_HH_VC_UNPLUG_EVT, (tBTA_HH*)&cbdata);
1054     } else {
1055       switch (p_data->api_sndcmd.t_type) {
1056         case HID_TRANS_SET_PROTOCOL:
1057         /* fall through */
1058         case HID_TRANS_GET_REPORT:
1059         /* fall through */
1060         case HID_TRANS_SET_REPORT:
1061         /* fall through */
1062         case HID_TRANS_GET_PROTOCOL:
1063         /* fall through */
1064         case HID_TRANS_GET_IDLE:
1065         /* fall through */
1066         case HID_TRANS_SET_IDLE: /* set w4_handsk event name for callback
1067                                     function use */
1068           p_cb->w4_evt = event;
1069           break;
1070         case HID_TRANS_DATA: /* output report */
1071                              /* fall through */
1072         case HID_TRANS_CONTROL:
1073           /* no handshake event will be generated */
1074           /* if VC_UNPLUG is issued, set flag */
1075           if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1076             p_cb->vp = true;
1077
1078           break;
1079         /* currently not expected */
1080         case HID_TRANS_DATAC:
1081         default:
1082           APPL_TRACE_DEBUG("%s: cmd type = %d", __func__,
1083                            p_data->api_sndcmd.t_type);
1084           break;
1085       }
1086
1087       /* if not control type transaction, notify PM for energy control */
1088       if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL) {
1089         /* inform PM for mode change */
1090         bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1091         bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1092       } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_SUSPEND) {
1093         bta_sys_sco_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1094       } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_EXIT_SUSPEND) {
1095         bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1096       }
1097     }
1098   }
1099   return;
1100 }
1101
1102 /*****************************************************************************
1103  *  Static Function
1104  ****************************************************************************/
1105 /*******************************************************************************
1106  *
1107  * Function         bta_hh_cback
1108  *
1109  * Description      BTA HH callback function.
1110  *
1111  *
1112  * Returns          void
1113  *
1114  ******************************************************************************/
1115 static void bta_hh_cback(uint8_t dev_handle, const RawAddress& addr,
1116                          uint8_t event, uint32_t data, BT_HDR* pdata) {
1117   uint16_t sm_event = BTA_HH_INVALID_EVT;
1118   uint8_t xx = 0;
1119
1120 #if (BTA_HH_DEBUG == TRUE)
1121   APPL_TRACE_DEBUG("%s::HID_event [%s]", __func__,
1122                    bta_hh_hid_event_name(event));
1123 #endif
1124
1125   switch (event) {
1126     case HID_HDEV_EVT_OPEN:
1127       sm_event = BTA_HH_INT_OPEN_EVT;
1128       break;
1129     case HID_HDEV_EVT_CLOSE:
1130       sm_event = BTA_HH_INT_CLOSE_EVT;
1131       break;
1132     case HID_HDEV_EVT_INTR_DATA:
1133       sm_event = BTA_HH_INT_DATA_EVT;
1134       break;
1135     case HID_HDEV_EVT_HANDSHAKE:
1136       sm_event = BTA_HH_INT_HANDSK_EVT;
1137       break;
1138     case HID_HDEV_EVT_CTRL_DATA:
1139       sm_event = BTA_HH_INT_CTRL_DATA;
1140       break;
1141     case HID_HDEV_EVT_RETRYING:
1142       break;
1143     case HID_HDEV_EVT_INTR_DATC:
1144     case HID_HDEV_EVT_CTRL_DATC:
1145       /* Unhandled events: Free buffer for DATAC */
1146       osi_free_and_reset((void**)&pdata);
1147       break;
1148     case HID_HDEV_EVT_VC_UNPLUG:
1149       for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
1150         if (bta_hh_cb.kdev[xx].hid_handle == dev_handle) {
1151           bta_hh_cb.kdev[xx].vp = true;
1152           break;
1153         }
1154       }
1155       break;
1156   }
1157
1158   if (sm_event != BTA_HH_INVALID_EVT) {
1159     tBTA_HH_CBACK_DATA* p_buf = (tBTA_HH_CBACK_DATA*)osi_malloc(
1160         sizeof(tBTA_HH_CBACK_DATA) + sizeof(BT_HDR));
1161     p_buf->hdr.event = sm_event;
1162     p_buf->hdr.layer_specific = (uint16_t)dev_handle;
1163     p_buf->data = data;
1164     p_buf->addr = addr;
1165     p_buf->p_data = pdata;
1166
1167     bta_sys_sendmsg(p_buf);
1168   }
1169 }
1170
1171 /*******************************************************************************
1172  *
1173  * Function         bta_hh_get_trans_status
1174  *
1175  * Description      translate a handshake result code into BTA HH
1176  *                  status code
1177  *
1178  ******************************************************************************/
1179 static tBTA_HH_STATUS bta_hh_get_trans_status(uint32_t result) {
1180   switch (result) {
1181     case HID_PAR_HANDSHAKE_RSP_SUCCESS: /*   (0) */
1182       return BTA_HH_OK;
1183     case HID_PAR_HANDSHAKE_RSP_NOT_READY:           /*   (1) */
1184     case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_REP_ID:  /*   (2) */
1185     case HID_PAR_HANDSHAKE_RSP_ERR_UNSUPPORTED_REQ: /*   (3) */
1186     case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_PARAM:   /*   (4) */
1187       return (tBTA_HH_STATUS)result;
1188     case HID_PAR_HANDSHAKE_RSP_ERR_UNKNOWN: /*   (14) */
1189     case HID_PAR_HANDSHAKE_RSP_ERR_FATAL:   /*   (15) */
1190     default:
1191       return BTA_HH_HS_ERROR;
1192       break;
1193   }
1194 }
1195 /*****************************************************************************
1196  *  Debug Functions
1197  ****************************************************************************/
1198
1199 #if (BTA_HH_DEBUG == TRUE)
1200 static const char* bta_hh_get_w4_event(uint16_t event) {
1201   switch (event) {
1202     case BTA_HH_GET_RPT_EVT:
1203       return "BTA_HH_GET_RPT_EVT";
1204     case BTA_HH_SET_RPT_EVT:
1205       return "BTA_HH_SET_RPT_EVT";
1206     case BTA_HH_GET_PROTO_EVT:
1207       return "BTA_HH_GET_PROTO_EVT";
1208     case BTA_HH_SET_PROTO_EVT:
1209       return "BTA_HH_SET_PROTO_EVT";
1210     case BTA_HH_GET_IDLE_EVT:
1211       return "BTA_HH_GET_IDLE_EVT";
1212     case BTA_HH_SET_IDLE_EVT:
1213       return "BTA_HH_SET_IDLE_EVT";
1214     case BTA_HH_OPEN_EVT:
1215       return "BTA_HH_OPEN_EVT";
1216     default:
1217       return "Unknown event";
1218   }
1219 }
1220
1221 static const char* bta_hh_hid_event_name(uint16_t event) {
1222   switch (event) {
1223     case HID_HDEV_EVT_OPEN:
1224       return "HID_HDEV_EVT_OPEN";
1225     case HID_HDEV_EVT_CLOSE:
1226       return "HID_HDEV_EVT_CLOSE";
1227     case HID_HDEV_EVT_RETRYING:
1228       return "HID_HDEV_EVT_RETRYING";
1229     case HID_HDEV_EVT_INTR_DATA:
1230       return "HID_HDEV_EVT_INTR_DATA";
1231     case HID_HDEV_EVT_INTR_DATC:
1232       return "HID_HDEV_EVT_INTR_DATC";
1233     case HID_HDEV_EVT_CTRL_DATA:
1234       return "HID_HDEV_EVT_CTRL_DATA";
1235     case HID_HDEV_EVT_CTRL_DATC:
1236       return "HID_HDEV_EVT_CTRL_DATC";
1237     case HID_HDEV_EVT_HANDSHAKE:
1238       return "HID_HDEV_EVT_HANDSHAKE";
1239     case HID_HDEV_EVT_VC_UNPLUG:
1240       return "HID_HDEV_EVT_VC_UNPLUG";
1241     default:
1242       return "Unknown HID event";
1243   }
1244 }
1245 #endif
1246 #endif /* BTA_HH_INCLUDED */