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  ******************************************************************************/
bta_hh_api_enable(tBTA_HH_DATA * p_data)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  ******************************************************************************/
bta_hh_api_disable(void)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  ******************************************************************************/
bta_hh_disc_cmpl(void)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  ******************************************************************************/
bta_hh_sdp_cback(uint16_t result,uint16_t attr_mask,tHID_DEV_SDP_INFO * sdp_rec)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  ******************************************************************************/
bta_hh_di_sdp_cback(uint16_t result)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  ******************************************************************************/
bta_hh_start_sdp(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)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  ******************************************************************************/
bta_hh_sdp_cmpl(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)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_ERROR("%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   p_cb->incoming_conn = false;
474   p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
475   return;
476 }
477 
478 /*******************************************************************************
479  *
480  * Function         bta_hh_api_disc_act
481  *
482  * Description      HID Host initiate a disconnection.
483  *
484  *
485  * Returns          void
486  *
487  ******************************************************************************/
bta_hh_api_disc_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)488 void bta_hh_api_disc_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
489   tBTA_HH_CBDATA disc_dat;
490   tHID_STATUS status;
491 
492 #if (BTA_HH_LE_INCLUDED == TRUE)
493   if (p_cb->is_le_device)
494     bta_hh_le_api_disc_act(p_cb);
495   else
496 #endif
497   {
498     /* found an active connection */
499     disc_dat.handle =
500         p_data ? (uint8_t)p_data->hdr.layer_specific : p_cb->hid_handle;
501     disc_dat.status = BTA_HH_ERR;
502 
503     status = HID_HostCloseDev(disc_dat.handle);
504 
505     if (status) {
506       tBTA_HH bta_hh;
507       bta_hh.dev_status = disc_dat;
508       (*bta_hh_cb.p_cback)(BTA_HH_CLOSE_EVT, &bta_hh);
509     }
510   }
511 
512   return;
513 }
514 /*******************************************************************************
515  *
516  * Function         bta_hh_open_cmpl_act
517  *
518  * Description      HID host connection completed
519  *
520  *
521  * Returns          void
522  *
523  ******************************************************************************/
bta_hh_open_cmpl_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)524 void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
525   tBTA_HH_CONN conn;
526   uint8_t dev_handle =
527       p_data ? (uint8_t)p_data->hid_cback.hdr.layer_specific : p_cb->hid_handle;
528 
529   memset((void*)&conn, 0, sizeof(tBTA_HH_CONN));
530   conn.handle = dev_handle;
531   conn.bda = p_cb->addr;
532 
533   /* increase connection number */
534   bta_hh_cb.cnt_num++;
535 
536   /* initialize device driver */
537   bta_hh_co_open(p_cb->hid_handle, p_cb->sub_class, p_cb->attr_mask,
538                  p_cb->app_id);
539 
540 #if (BTA_HH_LE_INCLUDED == TRUE)
541   conn.status = p_cb->status;
542   conn.le_hid = p_cb->is_le_device;
543   conn.scps_supported = p_cb->scps_supported;
544 
545   if (!p_cb->is_le_device)
546 #endif
547   {
548     /* inform role manager */
549     bta_sys_conn_open(BTA_ID_HH, p_cb->app_id, p_cb->addr);
550   }
551   /* set protocol mode when not default report mode */
552   if (p_cb->mode != BTA_HH_PROTO_RPT_MODE
553 #if (BTA_HH_LE_INCLUDED == TRUE)
554       && !p_cb->is_le_device
555 #endif
556       ) {
557     if ((HID_HostWriteDev(dev_handle, HID_TRANS_SET_PROTOCOL,
558                           HID_PAR_PROTOCOL_BOOT_MODE, 0, 0, NULL)) !=
559         HID_SUCCESS) {
560       /* HID connection is up, while SET_PROTO fail */
561       conn.status = BTA_HH_ERR_PROTO;
562       (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn);
563     } else {
564       conn.status = BTA_HH_OK;
565       p_cb->w4_evt = BTA_HH_OPEN_EVT;
566     }
567   } else
568     (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn);
569 
570   p_cb->incoming_conn = false;
571   p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
572 }
573 /*******************************************************************************
574  *
575  * Function         bta_hh_open_act
576  *
577  * Description      HID host receive HID_OPEN_EVT .
578  *
579  *
580  * Returns          void
581  *
582  ******************************************************************************/
bta_hh_open_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)583 void bta_hh_open_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
584   tBTA_HH_API_CONN conn_data;
585 
586   uint8_t dev_handle =
587       p_data ? (uint8_t)p_data->hid_cback.hdr.layer_specific : p_cb->hid_handle;
588 
589 #if (BTA_HH_DEBUG == TRUE)
590   APPL_TRACE_EVENT("%s:  Device[%d] connected", __func__, dev_handle);
591 #endif
592 
593   /* SDP has been done */
594   if (p_cb->app_id != 0) {
595     bta_hh_sm_execute(p_cb, BTA_HH_OPEN_CMPL_EVT, p_data);
596   } else
597   /*  app_id == 0 indicates an incoming conenction request arrives without SDP
598    *  performed, do it first
599    */
600   {
601     p_cb->incoming_conn = true;
602     /* store the handle here in case sdp fails - need to disconnect */
603     p_cb->incoming_hid_handle = dev_handle;
604 
605     memset(&conn_data, 0, sizeof(tBTA_HH_API_CONN));
606     conn_data.bd_addr = p_cb->addr;
607     bta_hh_start_sdp(p_cb, (tBTA_HH_DATA*)&conn_data);
608   }
609 
610   return;
611 }
612 
613 /*******************************************************************************
614  *
615  * Function         bta_hh_data_act
616  *
617  * Description      HID Host process a data report
618  *
619  *
620  * Returns          void
621  *
622  ******************************************************************************/
bta_hh_data_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)623 void bta_hh_data_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
624   BT_HDR* pdata = p_data->hid_cback.p_data;
625   uint8_t* p_rpt = (uint8_t*)(pdata + 1) + pdata->offset;
626 
627   bta_hh_co_data((uint8_t)p_data->hid_cback.hdr.layer_specific, p_rpt,
628                  pdata->len, p_cb->mode, p_cb->sub_class,
629                  p_cb->dscp_info.ctry_code, p_cb->addr, p_cb->app_id);
630 
631   osi_free_and_reset((void**)&pdata);
632 }
633 
634 /*******************************************************************************
635  *
636  * Function         bta_hh_handsk_act
637  *
638  * Description      HID Host process a handshake acknowledgement.
639  *
640  *
641  * Returns          void
642  *
643  ******************************************************************************/
bta_hh_handsk_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)644 void bta_hh_handsk_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
645 #if (BTA_HH_DEBUG == TRUE)
646   APPL_TRACE_DEBUG("HANDSHAKE received for: event = %s data= %d",
647                    bta_hh_get_w4_event(p_cb->w4_evt), p_data->hid_cback.data);
648 #endif
649 
650   tBTA_HH bta_hh;
651   memset(&bta_hh, 0, sizeof(tBTA_HH));
652 
653   switch (p_cb->w4_evt) {
654     /* GET_ transsaction, handshake indicate unsupported request */
655     case BTA_HH_GET_PROTO_EVT:
656       bta_hh.hs_data.rsp_data.proto_mode = BTA_HH_PROTO_UNKNOWN;
657       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
658     case BTA_HH_GET_RPT_EVT:
659     case BTA_HH_GET_IDLE_EVT:
660       bta_hh.hs_data.handle = p_cb->hid_handle;
661       /* if handshake gives an OK code for these transaction, fill in UNSUPT */
662       bta_hh.hs_data.status = bta_hh_get_trans_status(p_data->hid_cback.data);
663       if (bta_hh.hs_data.status == BTA_HH_OK)
664         bta_hh.hs_data.status = BTA_HH_HS_TRANS_NOT_SPT;
665       if (p_cb->w4_evt == BTA_HH_GET_RPT_EVT)
666         bta_hh_co_get_rpt_rsp(bta_hh.dev_status.handle, bta_hh.hs_data.status,
667                               NULL, 0);
668       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
669       p_cb->w4_evt = 0;
670       break;
671 
672     /* acknoledgement from HID device for SET_ transaction */
673     case BTA_HH_SET_RPT_EVT:
674     case BTA_HH_SET_PROTO_EVT:
675     case BTA_HH_SET_IDLE_EVT:
676       bta_hh.dev_status.handle = p_cb->hid_handle;
677       bta_hh.dev_status.status =
678           bta_hh_get_trans_status(p_data->hid_cback.data);
679       if (p_cb->w4_evt == BTA_HH_SET_RPT_EVT)
680         bta_hh_co_set_rpt_rsp(bta_hh.dev_status.handle,
681                               bta_hh.dev_status.status);
682       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
683       p_cb->w4_evt = 0;
684       break;
685 
686     /* SET_PROTOCOL when open connection */
687     case BTA_HH_OPEN_EVT:
688       bta_hh.conn.status =
689           p_data->hid_cback.data ? BTA_HH_ERR_PROTO : BTA_HH_OK;
690       bta_hh.conn.handle = p_cb->hid_handle;
691       bta_hh.conn.bda = p_cb->addr;
692       (*bta_hh_cb.p_cback)(p_cb->w4_evt, &bta_hh);
693 #if (BTA_HH_DEBUG == TRUE)
694       bta_hh_trace_dev_db();
695 #endif
696       p_cb->w4_evt = 0;
697       break;
698 
699     default:
700       /* unknow transaction handshake response */
701       APPL_TRACE_DEBUG("unknown transaction type");
702       break;
703   }
704 
705   /* transaction achknoledgement received, inform PM for mode change */
706   bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
707   return;
708 }
709 /*******************************************************************************
710  *
711  * Function         bta_hh_ctrl_dat_act
712  *
713  * Description      HID Host process a data report from control channel.
714  *
715  *
716  * Returns          void
717  *
718  ******************************************************************************/
bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)719 void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
720   BT_HDR* pdata = p_data->hid_cback.p_data;
721   uint8_t* data = (uint8_t*)(pdata + 1) + pdata->offset;
722   tBTA_HH_HSDATA hs_data;
723 
724 #if (BTA_HH_DEBUG == TRUE)
725   APPL_TRACE_DEBUG("Ctrl DATA received w4: event[%s]",
726                    bta_hh_get_w4_event(p_cb->w4_evt));
727 #endif
728   if (pdata->len == 0) {
729     android_errorWriteLog(0x534e4554, "116108738");
730     p_cb->w4_evt = 0;
731     osi_free_and_reset((void**)&pdata);
732     return;
733   }
734   hs_data.status = BTA_HH_OK;
735   hs_data.handle = p_cb->hid_handle;
736 
737   switch (p_cb->w4_evt) {
738     case BTA_HH_GET_IDLE_EVT:
739       hs_data.rsp_data.idle_rate = *data;
740       break;
741     case BTA_HH_GET_RPT_EVT:
742       hs_data.rsp_data.p_rpt_data = pdata;
743       bta_hh_co_get_rpt_rsp(hs_data.handle, hs_data.status, pdata->data,
744                             pdata->len);
745       break;
746     case BTA_HH_GET_PROTO_EVT:
747       /* match up BTE/BTA report/boot mode def*/
748       hs_data.rsp_data.proto_mode = ((*data) == HID_PAR_PROTOCOL_REPORT)
749                                         ? BTA_HH_PROTO_RPT_MODE
750                                         : BTA_HH_PROTO_BOOT_MODE;
751 #if (BTA_HH_DEBUG == TRUE)
752       APPL_TRACE_DEBUG("GET_PROTOCOL Mode = [%s]",
753                        (hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE)
754                            ? "Report"
755                            : "Boot");
756 #endif
757       break;
758     /* should not expect control DATA for SET_ transaction */
759     case BTA_HH_SET_PROTO_EVT:
760       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
761     case BTA_HH_SET_RPT_EVT:
762       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
763     case BTA_HH_SET_IDLE_EVT:
764       FALLTHROUGH_INTENDED; /* FALLTHROUGH */
765     default:
766 #if (BTA_HH_DEBUG == TRUE)
767       APPL_TRACE_DEBUG("invalid  transaction type for DATA payload: 4_evt[%s]",
768                        bta_hh_get_w4_event(p_cb->w4_evt));
769 #endif
770       break;
771   }
772 
773   /* inform PM for mode change */
774   bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
775   bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
776 
777   (*bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH*)&hs_data);
778 
779   p_cb->w4_evt = 0;
780   osi_free_and_reset((void**)&pdata);
781 }
782 
783 /*******************************************************************************
784  *
785  * Function         bta_hh_open_failure
786  *
787  * Description      report HID open failure when at wait for connection state
788  *                  and receive device close event.
789  *
790  *
791  * Returns          void
792  *
793  ******************************************************************************/
bta_hh_open_failure(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)794 void bta_hh_open_failure(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
795   tBTA_HH_CONN conn_dat;
796   uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
797 
798   memset(&conn_dat, 0, sizeof(tBTA_HH_CONN));
799   conn_dat.handle = p_cb->hid_handle;
800   conn_dat.status =
801       (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
802   conn_dat.bda = p_cb->addr;
803   HID_HostCloseDev(p_cb->hid_handle);
804 
805   /* Report OPEN fail event */
806   (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
807 
808 #if (BTA_HH_DEBUG == TRUE)
809   bta_hh_trace_dev_db();
810 #endif
811   /* clean up control block, but retain SDP info and device handle */
812   p_cb->vp = false;
813   p_cb->w4_evt = 0;
814 
815   /* if no connection is active and HH disable is signaled, disable service */
816   if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
817     bta_hh_disc_cmpl();
818   }
819 
820   /* Error in opening hid connection, reset flags */
821   p_cb->incoming_conn = false;
822   p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
823 }
824 
825 /*******************************************************************************
826  *
827  * Function         bta_hh_close_act
828  *
829  * Description      HID Host process a close event
830  *
831  *
832  * Returns          void
833  *
834  ******************************************************************************/
bta_hh_close_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)835 void bta_hh_close_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
836   tBTA_HH_CONN conn_dat;
837   tBTA_HH_CBDATA disc_dat = {BTA_HH_OK, 0};
838   uint32_t reason = p_data->hid_cback.data; /* Reason for closing (32-bit) */
839 
840   /* if HID_HDEV_EVT_VC_UNPLUG was received, report BTA_HH_VC_UNPLUG_EVT */
841   uint16_t event = p_cb->vp ? BTA_HH_VC_UNPLUG_EVT : BTA_HH_CLOSE_EVT;
842 
843   disc_dat.handle = p_cb->hid_handle;
844   disc_dat.status = p_data->hid_cback.data;
845 
846   /* Check reason for closing */
847   if ((reason & (HID_L2CAP_CONN_FAIL |
848                  HID_L2CAP_REQ_FAIL)) || /* Failure to initialize connection
849                                             (page timeout or l2cap error) */
850       (reason ==
851        HID_ERR_AUTH_FAILED) || /* Authenication error (while initiating) */
852       (reason == HID_ERR_L2CAP_FAILED)) /* Failure creating l2cap connection */
853   {
854     /* Failure in opening connection */
855     conn_dat.handle = p_cb->hid_handle;
856     conn_dat.status =
857         (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
858     conn_dat.bda = p_cb->addr;
859     HID_HostCloseDev(p_cb->hid_handle);
860 
861     /* Report OPEN fail event */
862     (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH*)&conn_dat);
863 
864 #if (BTA_HH_DEBUG == TRUE)
865     bta_hh_trace_dev_db();
866 #endif
867     return;
868   }
869   /* otherwise report CLOSE/VC_UNPLUG event */
870   else {
871     /* finaliza device driver */
872     bta_hh_co_close(p_cb->hid_handle, p_cb->app_id);
873     /* inform role manager */
874     bta_sys_conn_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
875     /* update total conn number */
876     bta_hh_cb.cnt_num--;
877 
878     if (disc_dat.status) disc_dat.status = BTA_HH_ERR;
879 
880     (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&disc_dat);
881 
882     /* if virtually unplug, remove device */
883     if (p_cb->vp) {
884       HID_HostRemoveDev(p_cb->hid_handle);
885       bta_hh_clean_up_kdev(p_cb);
886     }
887 
888 #if (BTA_HH_DEBUG == TRUE)
889     bta_hh_trace_dev_db();
890 #endif
891   }
892 
893   /* clean up control block, but retain SDP info and device handle */
894   p_cb->vp = false;
895   p_cb->w4_evt = 0;
896 
897   /* if no connection is active and HH disable is signaled, disable service */
898   if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
899     bta_hh_disc_cmpl();
900   }
901 
902   return;
903 }
904 
905 /*******************************************************************************
906  *
907  * Function         bta_hh_get_dscp_act
908  *
909  * Description      Get device report descriptor
910  *
911  *
912  * Returns          void
913  *
914  ******************************************************************************/
bta_hh_get_dscp_act(tBTA_HH_DEV_CB * p_cb,UNUSED_ATTR tBTA_HH_DATA * p_data)915 void bta_hh_get_dscp_act(tBTA_HH_DEV_CB* p_cb,
916                          UNUSED_ATTR tBTA_HH_DATA* p_data) {
917 #if (BTA_HH_LE_INCLUDED == TRUE)
918   if (p_cb->is_le_device) {
919     bta_hh_le_get_dscp_act(p_cb);
920   } else
921 #endif
922     (*bta_hh_cb.p_cback)(BTA_HH_GET_DSCP_EVT, (tBTA_HH*)&p_cb->dscp_info);
923 }
924 
925 /*******************************************************************************
926  *
927  * Function         bta_hh_maint_dev_act
928  *
929  * Description      HID Host maintain device list.
930  *
931  *
932  * Returns          void
933  *
934  ******************************************************************************/
bta_hh_maint_dev_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)935 void bta_hh_maint_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
936   tBTA_HH_MAINT_DEV* p_dev_info = &p_data->api_maintdev;
937   tBTA_HH_DEV_INFO dev_info;
938   uint8_t dev_handle;
939 
940   dev_info.status = BTA_HH_ERR;
941   dev_info.handle = BTA_HH_INVALID_HANDLE;
942 
943   switch (p_dev_info->sub_event) {
944     case BTA_HH_ADD_DEV_EVT: /* add a device */
945       dev_info.bda = p_dev_info->bda;
946       /* initialize callback data */
947       if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
948 #if (BTA_HH_LE_INCLUDED == TRUE)
949         if (bta_hh_is_le_device(p_cb, p_data->api_conn.bd_addr)) {
950           dev_info.handle = bta_hh_le_add_device(p_cb, p_dev_info);
951           if (dev_info.handle != BTA_HH_INVALID_HANDLE)
952             dev_info.status = BTA_HH_OK;
953         } else
954 #endif
955 
956             if (HID_HostAddDev(p_dev_info->bda, p_dev_info->attr_mask,
957                                &dev_handle) == HID_SUCCESS) {
958           dev_info.handle = dev_handle;
959           dev_info.status = BTA_HH_OK;
960 
961 #if (BTA_HH_LE_INCLUDED == TRUE)
962           /* update DI information */
963           bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
964                                 p_dev_info->dscp_info.product_id,
965                                 p_dev_info->dscp_info.version,
966                                 p_dev_info->dscp_info.flag);
967 #else
968           bta_hh_update_di_info(p_cb, p_dev_info->dscp_info.vendor_id,
969                                 p_dev_info->dscp_info.product_id,
970                                 p_dev_info->dscp_info.version, 0);
971 
972 #endif
973           /* add to BTA device list */
974           bta_hh_add_device_to_list(
975               p_cb, dev_handle, p_dev_info->attr_mask,
976               &p_dev_info->dscp_info.descriptor, p_dev_info->sub_class,
977               p_dev_info->dscp_info.ssr_max_latency,
978               p_dev_info->dscp_info.ssr_min_tout, p_dev_info->app_id);
979           /* update cb_index[] map */
980           bta_hh_cb.cb_index[dev_handle] = p_cb->index;
981         }
982       } else /* device already been added */
983       {
984         dev_info.handle = p_cb->hid_handle;
985         dev_info.status = BTA_HH_OK;
986       }
987 #if (BTA_HH_DEBUG == TRUE)
988       bta_hh_trace_dev_db();
989 #endif
990 
991       break;
992     case BTA_HH_RMV_DEV_EVT: /* remove device */
993       dev_info.handle = (uint8_t)p_dev_info->hdr.layer_specific;
994       dev_info.bda = p_cb->addr;
995 
996 #if (BTA_HH_LE_INCLUDED == TRUE)
997       if (p_cb->is_le_device) {
998         bta_hh_le_remove_dev_bg_conn(p_cb);
999         bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
1000         bta_hh_clean_up_kdev(p_cb);
1001       } else
1002 #endif
1003       {
1004         if (HID_HostRemoveDev(dev_info.handle) == HID_SUCCESS) {
1005           dev_info.status = BTA_HH_OK;
1006 
1007           /* remove from known device list in BTA */
1008           bta_hh_clean_up_kdev(p_cb);
1009         }
1010       }
1011       break;
1012 
1013     default:
1014       APPL_TRACE_DEBUG("invalid command");
1015       break;
1016   }
1017 
1018   (*bta_hh_cb.p_cback)(p_dev_info->sub_event, (tBTA_HH*)&dev_info);
1019 }
1020 /*******************************************************************************
1021  *
1022  * Function         bta_hh_write_dev_act
1023  *
1024  * Description      Write device action. can be SET/GET/DATA transaction.
1025  *
1026  * Returns          void
1027  *
1028  ******************************************************************************/
bta_hh_write_dev_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)1029 void bta_hh_write_dev_act(tBTA_HH_DEV_CB* p_cb, tBTA_HH_DATA* p_data) {
1030   tBTA_HH_CBDATA cbdata = {BTA_HH_OK, 0};
1031   uint16_t event = (p_data->api_sndcmd.t_type - BTA_HH_FST_BTE_TRANS_EVT) +
1032                    BTA_HH_FST_TRANS_CB_EVT;
1033 
1034 #if (BTA_HH_LE_INCLUDED == TRUE)
1035   if (p_cb->is_le_device)
1036     bta_hh_le_write_dev_act(p_cb, p_data);
1037   else
1038 #endif
1039   {
1040 
1041     cbdata.handle = p_cb->hid_handle;
1042 
1043     /* match up BTE/BTA report/boot mode def */
1044     if (p_data->api_sndcmd.t_type == HID_TRANS_SET_PROTOCOL) {
1045       p_data->api_sndcmd.param =
1046           (p_data->api_sndcmd.param == BTA_HH_PROTO_RPT_MODE)
1047               ? HID_PAR_PROTOCOL_REPORT
1048               : HID_PAR_PROTOCOL_BOOT_MODE;
1049     }
1050 
1051     if (HID_HostWriteDev(p_cb->hid_handle, p_data->api_sndcmd.t_type,
1052                          p_data->api_sndcmd.param, p_data->api_sndcmd.data,
1053                          p_data->api_sndcmd.rpt_id,
1054                          p_data->api_sndcmd.p_data) != HID_SUCCESS) {
1055       APPL_TRACE_ERROR("HID_HostWriteDev Error ");
1056       cbdata.status = BTA_HH_ERR;
1057 
1058       if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL &&
1059           p_data->api_sndcmd.t_type != HID_TRANS_DATA)
1060         (*bta_hh_cb.p_cback)(event, (tBTA_HH*)&cbdata);
1061       else if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1062         (*bta_hh_cb.p_cback)(BTA_HH_VC_UNPLUG_EVT, (tBTA_HH*)&cbdata);
1063     } else {
1064       switch (p_data->api_sndcmd.t_type) {
1065         case HID_TRANS_SET_PROTOCOL:
1066           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1067         case HID_TRANS_GET_REPORT:
1068           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1069         case HID_TRANS_SET_REPORT:
1070           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1071         case HID_TRANS_GET_PROTOCOL:
1072           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1073         case HID_TRANS_GET_IDLE:
1074           FALLTHROUGH_INTENDED;  /* FALLTHROUGH */
1075         case HID_TRANS_SET_IDLE: /* set w4_handsk event name for callback
1076                                     function use */
1077           p_cb->w4_evt = event;
1078           break;
1079         case HID_TRANS_DATA: /* output report */
1080           FALLTHROUGH_INTENDED; /* FALLTHROUGH */
1081         case HID_TRANS_CONTROL:
1082           /* no handshake event will be generated */
1083           /* if VC_UNPLUG is issued, set flag */
1084           if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG)
1085             p_cb->vp = true;
1086 
1087           break;
1088         /* currently not expected */
1089         case HID_TRANS_DATAC:
1090         default:
1091           APPL_TRACE_DEBUG("%s: cmd type = %d", __func__,
1092                            p_data->api_sndcmd.t_type);
1093           break;
1094       }
1095 
1096       /* if not control type transaction, notify PM for energy control */
1097       if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL) {
1098         /* inform PM for mode change */
1099         bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1100         bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1101       } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_SUSPEND) {
1102         bta_sys_sco_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1103       } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_EXIT_SUSPEND) {
1104         bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1105       }
1106     }
1107   }
1108   return;
1109 }
1110 
1111 /*****************************************************************************
1112  *  Static Function
1113  ****************************************************************************/
1114 /*******************************************************************************
1115  *
1116  * Function         bta_hh_cback
1117  *
1118  * Description      BTA HH callback function.
1119  *
1120  *
1121  * Returns          void
1122  *
1123  ******************************************************************************/
bta_hh_cback(uint8_t dev_handle,const RawAddress & addr,uint8_t event,uint32_t data,BT_HDR * pdata)1124 static void bta_hh_cback(uint8_t dev_handle, const RawAddress& addr,
1125                          uint8_t event, uint32_t data, BT_HDR* pdata) {
1126   uint16_t sm_event = BTA_HH_INVALID_EVT;
1127   uint8_t xx = 0;
1128 
1129 #if (BTA_HH_DEBUG == TRUE)
1130   APPL_TRACE_DEBUG("%s::HID_event [%s]", __func__,
1131                    bta_hh_hid_event_name(event));
1132 #endif
1133 
1134   switch (event) {
1135     case HID_HDEV_EVT_OPEN:
1136       sm_event = BTA_HH_INT_OPEN_EVT;
1137       break;
1138     case HID_HDEV_EVT_CLOSE:
1139       sm_event = BTA_HH_INT_CLOSE_EVT;
1140       break;
1141     case HID_HDEV_EVT_INTR_DATA:
1142       sm_event = BTA_HH_INT_DATA_EVT;
1143       break;
1144     case HID_HDEV_EVT_HANDSHAKE:
1145       sm_event = BTA_HH_INT_HANDSK_EVT;
1146       break;
1147     case HID_HDEV_EVT_CTRL_DATA:
1148       sm_event = BTA_HH_INT_CTRL_DATA;
1149       break;
1150     case HID_HDEV_EVT_RETRYING:
1151       break;
1152     case HID_HDEV_EVT_INTR_DATC:
1153     case HID_HDEV_EVT_CTRL_DATC:
1154       /* Unhandled events: Free buffer for DATAC */
1155       osi_free_and_reset((void**)&pdata);
1156       break;
1157     case HID_HDEV_EVT_VC_UNPLUG:
1158       for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
1159         if (bta_hh_cb.kdev[xx].hid_handle == dev_handle) {
1160           bta_hh_cb.kdev[xx].vp = true;
1161           break;
1162         }
1163       }
1164       break;
1165   }
1166 
1167   if (sm_event != BTA_HH_INVALID_EVT) {
1168     tBTA_HH_CBACK_DATA* p_buf = (tBTA_HH_CBACK_DATA*)osi_malloc(
1169         sizeof(tBTA_HH_CBACK_DATA) + sizeof(BT_HDR));
1170     p_buf->hdr.event = sm_event;
1171     p_buf->hdr.layer_specific = (uint16_t)dev_handle;
1172     p_buf->data = data;
1173     p_buf->addr = addr;
1174     p_buf->p_data = pdata;
1175 
1176     bta_sys_sendmsg(p_buf);
1177   }
1178 }
1179 
1180 /*******************************************************************************
1181  *
1182  * Function         bta_hh_get_trans_status
1183  *
1184  * Description      translate a handshake result code into BTA HH
1185  *                  status code
1186  *
1187  ******************************************************************************/
bta_hh_get_trans_status(uint32_t result)1188 static tBTA_HH_STATUS bta_hh_get_trans_status(uint32_t result) {
1189   switch (result) {
1190     case HID_PAR_HANDSHAKE_RSP_SUCCESS: /*   (0) */
1191       return BTA_HH_OK;
1192     case HID_PAR_HANDSHAKE_RSP_NOT_READY:           /*   (1) */
1193     case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_REP_ID:  /*   (2) */
1194     case HID_PAR_HANDSHAKE_RSP_ERR_UNSUPPORTED_REQ: /*   (3) */
1195     case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_PARAM:   /*   (4) */
1196       return (tBTA_HH_STATUS)result;
1197     case HID_PAR_HANDSHAKE_RSP_ERR_UNKNOWN: /*   (14) */
1198     case HID_PAR_HANDSHAKE_RSP_ERR_FATAL:   /*   (15) */
1199     default:
1200       return BTA_HH_HS_ERROR;
1201       break;
1202   }
1203 }
1204 /*****************************************************************************
1205  *  Debug Functions
1206  ****************************************************************************/
1207 
1208 #if (BTA_HH_DEBUG == TRUE)
bta_hh_get_w4_event(uint16_t event)1209 static const char* bta_hh_get_w4_event(uint16_t event) {
1210   switch (event) {
1211     case BTA_HH_GET_RPT_EVT:
1212       return "BTA_HH_GET_RPT_EVT";
1213     case BTA_HH_SET_RPT_EVT:
1214       return "BTA_HH_SET_RPT_EVT";
1215     case BTA_HH_GET_PROTO_EVT:
1216       return "BTA_HH_GET_PROTO_EVT";
1217     case BTA_HH_SET_PROTO_EVT:
1218       return "BTA_HH_SET_PROTO_EVT";
1219     case BTA_HH_GET_IDLE_EVT:
1220       return "BTA_HH_GET_IDLE_EVT";
1221     case BTA_HH_SET_IDLE_EVT:
1222       return "BTA_HH_SET_IDLE_EVT";
1223     case BTA_HH_OPEN_EVT:
1224       return "BTA_HH_OPEN_EVT";
1225     default:
1226       return "Unknown event";
1227   }
1228 }
1229 
bta_hh_hid_event_name(uint16_t event)1230 static const char* bta_hh_hid_event_name(uint16_t event) {
1231   switch (event) {
1232     case HID_HDEV_EVT_OPEN:
1233       return "HID_HDEV_EVT_OPEN";
1234     case HID_HDEV_EVT_CLOSE:
1235       return "HID_HDEV_EVT_CLOSE";
1236     case HID_HDEV_EVT_RETRYING:
1237       return "HID_HDEV_EVT_RETRYING";
1238     case HID_HDEV_EVT_INTR_DATA:
1239       return "HID_HDEV_EVT_INTR_DATA";
1240     case HID_HDEV_EVT_INTR_DATC:
1241       return "HID_HDEV_EVT_INTR_DATC";
1242     case HID_HDEV_EVT_CTRL_DATA:
1243       return "HID_HDEV_EVT_CTRL_DATA";
1244     case HID_HDEV_EVT_CTRL_DATC:
1245       return "HID_HDEV_EVT_CTRL_DATC";
1246     case HID_HDEV_EVT_HANDSHAKE:
1247       return "HID_HDEV_EVT_HANDSHAKE";
1248     case HID_HDEV_EVT_VC_UNPLUG:
1249       return "HID_HDEV_EVT_VC_UNPLUG";
1250     default:
1251       return "Unknown HID event";
1252   }
1253 }
1254 #endif
1255 #endif /* BTA_HH_INCLUDED */
1256