OSDN Git Service

Merge "HH: Check parameter length in bta_hh_ctrl_dat_act" into pi-dev
[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 acknowledgement.
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       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
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       if (p_cb->w4_evt == BTA_HH_GET_RPT_EVT)
664         bta_hh_co_get_rpt_rsp(bta_hh.dev_status.handle, bta_hh.hs_data.status,
665                               NULL, 0);
666       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
667       p_cb->w4_evt = 0;
668       break;
669
670     /* acknoledgement from HID device for SET_ transaction */
671     case BTA_HH_SET_RPT_EVT:
672     case BTA_HH_SET_PROTO_EVT:
673     case BTA_HH_SET_IDLE_EVT:
674       bta_hh.dev_status.handle = p_cb->hid_handle;
675       bta_hh.dev_status.status =
676           bta_hh_get_trans_status(p_data->hid_cback.data);
677       if (p_cb->w4_evt == BTA_HH_SET_RPT_EVT)
678         bta_hh_co_set_rpt_rsp(bta_hh.dev_status.handle,
679                               bta_hh.dev_status.status);
680       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
681       p_cb->w4_evt = 0;
682       break;
683
684     /* SET_PROTOCOL when open connection */
685     case BTA_HH_OPEN_EVT:
686       bta_hh.conn.status =
687           p_data->hid_cback.data ? BTA_HH_ERR_PROTO : BTA_HH_OK;
688       bta_hh.conn.handle = p_cb->hid_handle;
689       bta_hh.conn.bda = p_cb->addr;
690       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
691 #if (BTA_HH_DEBUG == TRUE)
692       bta_hh_trace_dev_db();
693 #endif
694       p_cb->w4_evt = 0;
695       break;
696
697     default:
698       /* unknow transaction handshake response */
699       APPL_TRACE_DEBUG("unknown transaction type");
700       break;
701   }
702
703   /* transaction achknoledgement received, inform PM for mode change */
704   bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
705   return;
706 }
707 /*******************************************************************************
708  *
709  * Function         bta_hh_ctrl_dat_act
710  *
711  * Description      HID Host process a data report from control channel.
712  *
713  *
714  * Returns          void
715  *
716  ******************************************************************************/
717 void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
718   BT_HDR* pdata = p_data->hid_cback.p_data;
719   uint8_t* data = (uint8_t*)(pdata + 1) + pdata->offset;
720   tBTA_HH_HSDATA hs_data;
721
722 #if (BTA_HH_DEBUG == TRUE)
723   APPL_TRACE_DEBUG("Ctrl DATA received w4: event[%s]",
724                    bta_hh_get_w4_event(p_cb->w4_evt));
725 #endif
726   if (pdata->len == 0) {
727     android_errorWriteLog(0x534e4554, "116108738");
728     p_cb->w4_evt = 0;
729     osi_free_and_reset((void**)&pdata);
730     return;
731   }
732   hs_data.status = BTA_HH_OK;
733   hs_data.handle = p_cb->hid_handle;
734
735   switch (p_cb->w4_evt) {
736     case BTA_HH_GET_IDLE_EVT:
737       hs_data.rsp_data.idle_rate = *data;
738       break;
739     case BTA_HH_GET_RPT_EVT:
740       hs_data.rsp_data.p_rpt_data = pdata;
741       bta_hh_co_get_rpt_rsp(hs_data.handle, hs_data.status, pdata->data,
742                             pdata->len);
743       break;
744     case BTA_HH_GET_PROTO_EVT:
745       /* match up BTE/BTA report/boot mode def*/
746       hs_data.rsp_data.proto_mode = ((*data) == HID_PAR_PROTOCOL_REPORT)
747                                         ? BTA_HH_PROTO_RPT_MODE
748                                         : BTA_HH_PROTO_BOOT_MODE;
749 #if (BTA_HH_DEBUG == TRUE)
750       APPL_TRACE_DEBUG("GET_PROTOCOL Mode = [%s]",
751                        (hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE)
752                            ? "Report"
753                            : "Boot");
754 #endif
755       break;
756     /* should not expect control DATA for SET_ transaction */
757     case BTA_HH_SET_PROTO_EVT:
758       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
759     case BTA_HH_SET_RPT_EVT:
760       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
761     case BTA_HH_SET_IDLE_EVT:
762       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
763     default:
764 #if (BTA_HH_DEBUG == TRUE)
765       APPL_TRACE_DEBUG("invalid  transaction type for DATA payload: 4_evt[%s]",
766                        bta_hh_get_w4_event(p_cb->w4_evt));
767 #endif
768       break;
769   }
770
771   /* inform PM for mode change */
772   bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
773   bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
774
775   (*bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH*)&hs_data);
776
777   p_cb->w4_evt = 0;
778   osi_free_and_reset((void**)&pdata);
779 }
780
781 /*******************************************************************************
782  *
783  * Function         bta_hh_open_failure
784  *
785  * Description      report HID open failure when at wait for connection state
786  *                  and receive device close event.
787  *
788  *
789  * Returns          void
790  *
791  ******************************************************************************/
792 void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
793   tBTA_HH_CONN conn_dat;
794   uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
795
796   memset(&conn_dat, 0, sizeof(tBTA_HH_CONN));
797   conn_dat.handle = p_cb->hid_handle;
798   conn_dat.status =
799       (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
800   conn_dat.bda = p_cb->addr;
801   HID_HostCloseDev(p_cb->hid_handle);
802
803   /* Report OPEN fail event */
804   (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
805
806 #if (BTA_HH_DEBUG == TRUE)
807   bta_hh_trace_dev_db();
808 #endif
809   /* clean up control block, but retain SDP info and device handle */
810   p_cb->vp = false;
811   p_cb->w4_evt = 0;
812
813   /* if no connection is active and HH disable is signaled, disable service */
814   if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
815     bta_hh_disc_cmpl();
816   }
817
818   /* Error in opening hid connection, reset flags */
819   p_cb->incoming_conn = false;
820   p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
821 }
822
823 /*******************************************************************************
824  *
825  * Function         bta_hh_close_act
826  *
827  * Description      HID Host process a close event
828  *
829  *
830  * Returns          void
831  *
832  ******************************************************************************/
833 void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
834   tBTA_HH_CONN conn_dat;
835   tBTA_HH_CBDATA disc_dat = {BTA_HH_OK, 0};
836   uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
837
838   /* if HID_HDEV_EVT_VC_UNPLUG was received, report BTA_HH_VC_UNPLUG_EVT */
839   uint16_t event = p_cb->vp ? BTA_HH_VC_UNPLUG_EVT : BTA_HH_CLOSE_EVT;
840
841   disc_dat.handle = p_cb->hid_handle;
842   disc_dat.status = p_data->hid_cback.data;
843
844   /* Check reason for closing */
845   if ((reason & (HID_L2CAP_CONN_FAIL |
846                  HID_L2CAP_REQ_FAIL)) || /* Failure to initialize connection
847                                             (page timeout or l2cap error) */
848       (reason ==
849        HID_ERR_AUTH_FAILED) || /* Authenication error (while initiating) */
850       (reason == HID_ERR_L2CAP_FAILED)) /* Failure creating l2cap connection */
851   {
852     /* Failure in opening connection */
853     conn_dat.handle = p_cb->hid_handle;
854     conn_dat.status =
855         (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
856     conn_dat.bda = p_cb->addr;
857     HID_HostCloseDev(p_cb->hid_handle);
858
859     /* Report OPEN fail event */
860     (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
861
862 #if (BTA_HH_DEBUG == TRUE)
863     bta_hh_trace_dev_db();
864 #endif
865     return;
866   }
867   /* otherwise report CLOSE/VC_UNPLUG event */
868   else {
869     /* finaliza device driver */
870     bta_hh_co_close(p_cb->hid_handle, p_cb->app_id);
871     /* inform role manager */
872     bta_sys_conn_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
873     /* update total conn number */
874     bta_hh_cb.cnt_num--;
875
876     if (disc_dat.status) disc_dat.status = BTA_HH_ERR;
877
878     (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&disc_dat);
879
880     /* if virtually unplug, remove device */
881     if (p_cb->vp) {
882       HID_HostRemoveDev(p_cb->hid_handle);
883       bta_hh_clean_up_kdev(p_cb);
884     }
885
886 #if (BTA_HH_DEBUG == TRUE)
887     bta_hh_trace_dev_db();
888 #endif
889   }
890
891   /* clean up control block, but retain SDP info and device handle */
892   p_cb->vp = false;
893   p_cb->w4_evt = 0;
894
895   /* if no connection is active and HH disable is signaled, disable service */
896   if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
897     bta_hh_disc_cmpl();
898   }
899
900   return;
901 }
902
903 /*******************************************************************************
904  *
905  * Function         bta_hh_get_dscp_act
906  *
907  * Description      Get device report descriptor
908  *
909  *
910  * Returns          void
911  *
912  ******************************************************************************/
913 void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb,
914                          UNUSED_ATTR tBTA_HH_DATA* p_data) {
915 #if (BTA_HH_LE_INCLUDED == TRUE)
916   if (p_cb->is_le_device) {
917     bta_hh_le_get_dscp_act(p_cb);
918   } else
919 #endif
920     (*bta_hh_cb.p_cback)(BTA_HH_GET_DSCP_EVT, (tBTA_HH*)&p_cb->dscp_info);
921 }
922
923 /*******************************************************************************
924  *
925  * Function         bta_hh_maint_dev_act
926  *
927  * Description      HID Host maintain device list.
928  *
929  *
930  * Returns          void
931  *
932  ******************************************************************************/
933 void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
934   tBTA_HH_MAINT_DEV* p_dev_info = &p_data->api_maintdev;
935   tBTA_HH_DEV_INFO dev_info;
936   uint8_t dev_handle;
937
938   dev_info.status = BTA_HH_ERR;
939   dev_info.handle = BTA_HH_INVALID_HANDLE;
940
941   switch (p_dev_info->sub_event) {
942     case BTA_HH_ADD_DEV_EVT: /* add a device */
943       dev_info.bda = p_dev_info->bda;
944       /* initialize callback data */
945       if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
946 #if (BTA_HH_LE_INCLUDED == TRUE)
947         if (bta_hh_is_le_device(p_cb, p_data->api_conn.bd_addr)) {
948           dev_info.handle = bta_hh_le_add_device(p_cb, p_dev_info);
949           if (dev_info.handle != BTA_HH_INVALID_HANDLE)
950             dev_info.status = BTA_HH_OK;
951         } else
952 #endif
953
954             if (HID_HostAddDev(p_dev_info->bda, p_dev_info->attr_mask,
955                                &dev_handle) == HID_SUCCESS) {
956           dev_info.handle = dev_handle;
957           dev_info.status = BTA_HH_OK;
958
959 #if (BTA_HH_LE_INCLUDED == TRUE)
960           /* update DI information */
961           bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
962                                 p_dev_info->dscp_info.product_id,
963                                 p_dev_info->dscp_info.version,
964                                 p_dev_info->dscp_info.flag);
965 #else
966           bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
967                                 p_dev_info->dscp_info.product_id,
968                                 p_dev_info->dscp_info.version, 0);
969
970 #endif
971           /* add to BTA device list */
972           bta_hh_add_device_to_list(
973               p_cb, dev_handle, p_dev_info->attr_mask,
974               &p_dev_info->dscp_info.descriptor, p_dev_info->sub_class,
975               p_dev_info->dscp_info.ssr_max_latency,
976               p_dev_info->dscp_info.ssr_min_tout, p_dev_info->app_id);
977           /* update cb_index[] map */
978           bta_hh_cb.cb_index[dev_handle] = p_cb->index;
979         }
980       } else /* device already been added */
981       {
982         dev_info.handle = p_cb->hid_handle;
983         dev_info.status = BTA_HH_OK;
984       }
985 #if (BTA_HH_DEBUG == TRUE)
986       bta_hh_trace_dev_db();
987 #endif
988
989       break;
990     case BTA_HH_RMV_DEV_EVT: /* remove device */
991       dev_info.handle = (uint8_t)p_dev_info->hdr.layer_specific;
992       dev_info.bda = p_cb->addr;
993
994 #if (BTA_HH_LE_INCLUDED == TRUE)
995       if (p_cb->is_le_device) {
996         bta_hh_le_remove_dev_bg_conn(p_cb);
997         bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
998         bta_hh_clean_up_kdev(p_cb);
999       } else
1000 #endif
1001       {
1002         if (HID_HostRemoveDev(dev_info.handle) == HID_SUCCESS) {
1003           dev_info.status = BTA_HH_OK;
1004
1005           /* remove from known device list in BTA */
1006           bta_hh_clean_up_kdev(p_cb);
1007         }
1008       }
1009       break;
1010
1011     default:
1012       APPL_TRACE_DEBUG("invalid command");
1013       break;
1014   }
1015
1016   (*bta_hh_cb.p_cback)(p_dev_info->sub_event, (tBTA_HH*)&dev_info);
1017 }
1018 /*******************************************************************************
1019  *
1020  * Function         bta_hh_write_dev_act
1021  *
1022  * Description      Write device action. can be SET/GET/DATA transaction.
1023  *
1024  * Returns          void
1025  *
1026  ******************************************************************************/
1027 void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
1028   tBTA_HH_CBDATA cbdata = {BTA_HH_OK, 0};
1029   uint16_t event = (p_data->api_sndcmd.t_type - BTA_HH_FST_BTE_TRANS_EVT) +
1030                    BTA_HH_FST_TRANS_CB_EVT;
1031
1032 #if (BTA_HH_LE_INCLUDED == TRUE)
1033   if (p_cb->is_le_device)
1034     bta_hh_le_write_dev_act(p_cb, p_data);
1035   else
1036 #endif
1037   {
1038
1039     cbdata.handle = p_cb->hid_handle;
1040
1041     /* match up BTE/BTA report/boot mode def */
1042     if (p_data->api_sndcmd.t_type == HID_TRANS_SET_PROTOCOL) {
1043       p_data->api_sndcmd.param =
1044           (p_data->api_sndcmd.param == BTA_HH_PROTO_RPT_MODE)
1045               ? HID_PAR_PROTOCOL_REPORT
1046               : HID_PAR_PROTOCOL_BOOT_MODE;
1047     }
1048
1049     if (HID_HostWriteDev(p_cb->hid_handle, p_data->api_sndcmd.t_type,
1050                          p_data->api_sndcmd.param, p_data->api_sndcmd.data,
1051                          p_data->api_sndcmd.rpt_id,
1052                          p_data->api_sndcmd.p_data) != HID_SUCCESS) {
1053       APPL_TRACE_ERROR("HID_HostWriteDev Error ");
1054       cbdata.status = BTA_HH_ERR;
1055
1056       if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL &&
1057           p_data->api_sndcmd.t_type != HID_TRANS_DATA)
1058         (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&cbdata);
1059       else if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1060         (*bta_hh_cb.p_cback)(BTA_HH_VC_UNPLUG_EVT, (tBTA_HH*)&cbdata);
1061     } else {
1062       switch (p_data->api_sndcmd.t_type) {
1063         case HID_TRANS_SET_PROTOCOL:
1064           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1065         case HID_TRANS_GET_REPORT:
1066           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1067         case HID_TRANS_SET_REPORT:
1068           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1069         case HID_TRANS_GET_PROTOCOL:
1070           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1071         case HID_TRANS_GET_IDLE:
1072           FALLTHROUGH_INTENDED;  /* FALLTHROUGH */
1073         case HID_TRANS_SET_IDLE: /* set w4_handsk event name for callback
1074                                     function use */
1075           p_cb->w4_evt = event;
1076           break;
1077         case HID_TRANS_DATA: /* output report */
1078           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1079         case HID_TRANS_CONTROL:
1080           /* no handshake event will be generated */
1081           /* if VC_UNPLUG is issued, set flag */
1082           if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1083             p_cb->vp = true;
1084
1085           break;
1086         /* currently not expected */
1087         case HID_TRANS_DATAC:
1088         default:
1089           APPL_TRACE_DEBUG("%s: cmd type = %d", __func__,
1090                            p_data->api_sndcmd.t_type);
1091           break;
1092       }
1093
1094       /* if not control type transaction, notify PM for energy control */
1095       if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL) {
1096         /* inform PM for mode change */
1097         bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1098         bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1099       } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_SUSPEND) {
1100         bta_sys_sco_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1101       } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_EXIT_SUSPEND) {
1102         bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1103       }
1104     }
1105   }
1106   return;
1107 }
1108
1109 /*****************************************************************************
1110  *  Static Function
1111  ****************************************************************************/
1112 /*******************************************************************************
1113  *
1114  * Function         bta_hh_cback
1115  *
1116  * Description      BTA HH callback function.
1117  *
1118  *
1119  * Returns          void
1120  *
1121  ******************************************************************************/
1122 static void bta_hh_cback(uint8_t dev_handle, const RawAddress& addr,
1123                          uint8_t event, uint32_t data, BT_HDR* pdata) {
1124   uint16_t sm_event = BTA_HH_INVALID_EVT;
1125   uint8_t xx = 0;
1126
1127 #if (BTA_HH_DEBUG == TRUE)
1128   APPL_TRACE_DEBUG("%s::HID_event [%s]", __func__,
1129                    bta_hh_hid_event_name(event));
1130 #endif
1131
1132   switch (event) {
1133     case HID_HDEV_EVT_OPEN:
1134       sm_event = BTA_HH_INT_OPEN_EVT;
1135       break;
1136     case HID_HDEV_EVT_CLOSE:
1137       sm_event = BTA_HH_INT_CLOSE_EVT;
1138       break;
1139     case HID_HDEV_EVT_INTR_DATA:
1140       sm_event = BTA_HH_INT_DATA_EVT;
1141       break;
1142     case HID_HDEV_EVT_HANDSHAKE:
1143       sm_event = BTA_HH_INT_HANDSK_EVT;
1144       break;
1145     case HID_HDEV_EVT_CTRL_DATA:
1146       sm_event = BTA_HH_INT_CTRL_DATA;
1147       break;
1148     case HID_HDEV_EVT_RETRYING:
1149       break;
1150     case HID_HDEV_EVT_INTR_DATC:
1151     case HID_HDEV_EVT_CTRL_DATC:
1152       /* Unhandled events: Free buffer for DATAC */
1153       osi_free_and_reset((void**)&pdata);
1154       break;
1155     case HID_HDEV_EVT_VC_UNPLUG:
1156       for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
1157         if (bta_hh_cb.kdev[xx].hid_handle == dev_handle) {
1158           bta_hh_cb.kdev[xx].vp = true;
1159           break;
1160         }
1161       }
1162       break;
1163   }
1164
1165   if (sm_event != BTA_HH_INVALID_EVT) {
1166     tBTA_HH_CBACK_DATA* p_buf = (tBTA_HH_CBACK_DATA*)osi_malloc(
1167         sizeof(tBTA_HH_CBACK_DATA) + sizeof(BT_HDR));
1168     p_buf->hdr.event = sm_event;
1169     p_buf->hdr.layer_specific = (uint16_t)dev_handle;
1170     p_buf->data = data;
1171     p_buf->addr = addr;
1172     p_buf->p_data = pdata;
1173
1174     bta_sys_sendmsg(p_buf);
1175   }
1176 }
1177
1178 /*******************************************************************************
1179  *
1180  * Function         bta_hh_get_trans_status
1181  *
1182  * Description      translate a handshake result code into BTA HH
1183  *                  status code
1184  *
1185  ******************************************************************************/
1186 static tBTA_HH_STATUS bta_hh_get_trans_status(uint32_t result) {
1187   switch (result) {
1188     case HID_PAR_HANDSHAKE_RSP_SUCCESS: /*   (0) */
1189       return BTA_HH_OK;
1190     case HID_PAR_HANDSHAKE_RSP_NOT_READY:           /*   (1) */
1191     case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_REP_ID:  /*   (2) */
1192     case HID_PAR_HANDSHAKE_RSP_ERR_UNSUPPORTED_REQ: /*   (3) */
1193     case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_PARAM:   /*   (4) */
1194       return (tBTA_HH_STATUS)result;
1195     case HID_PAR_HANDSHAKE_RSP_ERR_UNKNOWN: /*   (14) */
1196     case HID_PAR_HANDSHAKE_RSP_ERR_FATAL:   /*   (15) */
1197     default:
1198       return BTA_HH_HS_ERROR;
1199       break;
1200   }
1201 }
1202 /*****************************************************************************
1203  *  Debug Functions
1204  ****************************************************************************/
1205
1206 #if (BTA_HH_DEBUG == TRUE)
1207 static const char* bta_hh_get_w4_event(uint16_t event) {
1208   switch (event) {
1209     case BTA_HH_GET_RPT_EVT:
1210       return "BTA_HH_GET_RPT_EVT";
1211     case BTA_HH_SET_RPT_EVT:
1212       return "BTA_HH_SET_RPT_EVT";
1213     case BTA_HH_GET_PROTO_EVT:
1214       return "BTA_HH_GET_PROTO_EVT";
1215     case BTA_HH_SET_PROTO_EVT:
1216       return "BTA_HH_SET_PROTO_EVT";
1217     case BTA_HH_GET_IDLE_EVT:
1218       return "BTA_HH_GET_IDLE_EVT";
1219     case BTA_HH_SET_IDLE_EVT:
1220       return "BTA_HH_SET_IDLE_EVT";
1221     case BTA_HH_OPEN_EVT:
1222       return "BTA_HH_OPEN_EVT";
1223     default:
1224       return "Unknown event";
1225   }
1226 }
1227
1228 static const char* bta_hh_hid_event_name(uint16_t event) {
1229   switch (event) {
1230     case HID_HDEV_EVT_OPEN:
1231       return "HID_HDEV_EVT_OPEN";
1232     case HID_HDEV_EVT_CLOSE:
1233       return "HID_HDEV_EVT_CLOSE";
1234     case HID_HDEV_EVT_RETRYING:
1235       return "HID_HDEV_EVT_RETRYING";
1236     case HID_HDEV_EVT_INTR_DATA:
1237       return "HID_HDEV_EVT_INTR_DATA";
1238     case HID_HDEV_EVT_INTR_DATC:
1239       return "HID_HDEV_EVT_INTR_DATC";
1240     case HID_HDEV_EVT_CTRL_DATA:
1241       return "HID_HDEV_EVT_CTRL_DATA";
1242     case HID_HDEV_EVT_CTRL_DATC:
1243       return "HID_HDEV_EVT_CTRL_DATC";
1244     case HID_HDEV_EVT_HANDSHAKE:
1245       return "HID_HDEV_EVT_HANDSHAKE";
1246     case HID_HDEV_EVT_VC_UNPLUG:
1247       return "HID_HDEV_EVT_VC_UNPLUG";
1248     default:
1249       return "Unknown HID event";
1250   }
1251 }
1252 #endif
1253 #endif /* BTA_HH_INCLUDED */