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