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 record 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     p_cb->new_mode  = p_data->api_conn.mode;
327     bta_hh_cb.p_cur = p_cb;
328 
329 #if (BTA_HH_LE_INCLUDED == TRUE)
330     if (bta_hh_is_le_device(p_cb, p_data->api_conn.bd_addr)) {
331         bta_hh_le_open_conn(p_cb, p_data->api_conn.bd_addr);
332         return;
333     }
334 #endif
335 
336     /* if previously virtually cabled device, skip SDP */
337     if (p_cb->app_id) {
338         status = BTA_HH_OK;
339 #if BTA_HH_DEBUG
340         APPL_TRACE_DEBUG("bta_hh_start_sdp:: skip SDP for known devices");
341 #endif
342         if (p_cb->hid_handle == BTA_HH_INVALID_HANDLE) {
343             if (HID_HostAddDev (p_cb->addr, p_cb->attr_mask, &hdl) \
344                     == HID_SUCCESS) {
345                 /* update device CB with newly register device handle */
346                 bta_hh_add_device_to_list(p_cb,  hdl, p_cb->attr_mask, NULL,
347                                           p_cb->sub_class,
348                                           p_cb->dscp_info.ssr_max_latency,
349                                           p_cb->dscp_info.ssr_min_tout,
350                                           p_cb->app_id);
351                 /* update cb_index[] map */
352                 bta_hh_cb.cb_index[hdl] = p_cb->index;
353             } else {
354                 status = BTA_HH_ERR_NO_RES;
355             }
356         }
357         bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, (tBTA_HH_DATA *)&status);
358 
359         return;
360     }
361     /* GetSDPRecord. at one time only one SDP procedure can be active */
362     else if (!bta_hh_cb.p_disc_db) {
363         bta_hh_cb.p_disc_db = (tSDP_DISCOVERY_DB *) osi_malloc(p_bta_hh_cfg->sdp_db_size);
364 
365         if (bta_hh_cb.p_disc_db == NULL) {
366             status = BTA_HH_ERR_NO_RES;
367         } else {
368             bta_hh_cb.p_cur = p_cb;
369             /* do DI discovery first */
370             if (SDP_DiDiscover(p_data->api_conn.bd_addr,
371                                bta_hh_cb.p_disc_db,
372                                p_bta_hh_cfg->sdp_db_size,
373                                bta_hh_di_sdp_cback) != SDP_SUCCESS) {
374 #if BTA_HH_DEBUG
375                 APPL_TRACE_DEBUG ("bta_hh_start_sdp:  SDP_DiDiscover failed: \
376                     Status 0x%2X", status);
377 #endif
378                 status = BTA_HH_ERR_SDP;
379                 utl_freebuf((void **)&bta_hh_cb.p_disc_db);
380             } else {
381                 status = BTA_HH_OK;
382             }
383         }
384     }
385 
386     if (status != BTA_HH_OK) {
387         bta_hh_sm_execute(p_cb, BTA_HH_SDP_CMPL_EVT, (tBTA_HH_DATA *)&status);
388     }
389 
390     return;
391 
392 }
393 /*******************************************************************************
394 **
395 ** Function         bta_hh_sdp_cmpl
396 **
397 ** Description      When SDP completed, initiate a connection or report error depend
398 **                  on SDP result.
399 **
400 **
401 ** Returns          void
402 **
403 *******************************************************************************/
bta_hh_sdp_cmpl(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)404 void bta_hh_sdp_cmpl(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
405 {
406     tBTA_HH_CONN            conn_dat;
407     tBTA_HH_STATUS          status = p_data->status;
408 
409 #if BTA_HH_DEBUG
410     APPL_TRACE_DEBUG ("bta_hh_sdp_cmpl:  status 0x%2X", p_data->status);
411 #endif
412 
413     /* initialize call back data */
414     memset((void *)&conn_dat, 0, sizeof(tBTA_HH_CONN));
415     conn_dat.handle = p_cb->hid_handle;
416     bdcpy(conn_dat.bda, p_cb->addr);
417 
418     /* if SDP compl success */
419     if ( status == BTA_HH_OK) {
420         /* not incoming connection doing SDP, initiate a HID connection */
421         if (!p_cb->incoming_conn) {
422             tHID_STATUS ret;
423             /* set security level */
424             HID_HostSetSecurityLevel("", p_cb->sec_mask);
425 
426             /* open HID connection */
427             if ((ret = HID_HostOpenDev (p_cb->hid_handle)) != HID_SUCCESS) {
428 #if BTA_HH_DEBUG
429                 APPL_TRACE_DEBUG ("bta_hh_sdp_cmpl:  HID_HostOpenDev failed: \
430                     Status 0x%2X", ret);
431 #endif
432                 conn_dat.is_orig = HID_HostConnectOrig(p_cb->hid_handle);
433                 /* open fail, remove device from management device list */
434                 HID_HostRemoveDev( p_cb->hid_handle);
435                 status = BTA_HH_ERR;
436             } else {
437                 status = BTA_HH_OK;
438             }
439         } else { /* incoming connection SDP finish */
440             bta_hh_sm_execute(p_cb, BTA_HH_OPEN_CMPL_EVT, NULL);
441         }
442     }
443 
444     if (status != BTA_HH_OK) {
445         /* Check if this was incoming connection request  from an unknown device
446            **and connection failed due to missing HID Device SDP UUID
447            **In above condition, disconnect the link as well as remove the
448            **device from list of HID devices*/
449         if ((status == BTA_HH_ERR_SDP) &&
450                 (p_cb->incoming_conn) && (p_cb->app_id == 0)) {
451             APPL_TRACE_DEBUG ("bta_hh_sdp_cmpl:SDP failed for  incoming conn :hndl %d",
452                               p_cb->incoming_hid_handle);
453             HID_HostRemoveDev( p_cb->incoming_hid_handle);
454         }
455         conn_dat.status = status;
456         (* bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH *)&conn_dat);
457 
458         /* move state machine W4_CONN ->IDLE */
459         bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
460 
461         if (p_cb->app_id == 0) {
462             /* clean up device control block */
463             bta_hh_clean_up_kdev(p_cb);
464         }
465 #if BTA_HH_DEBUG
466         bta_hh_trace_dev_db();
467 #endif
468     }
469     return;
470 }
471 
472 /*******************************************************************************
473 **
474 ** Function         bta_hh_api_disc_act
475 **
476 ** Description      HID Host initiate a disconnection.
477 **
478 **
479 ** Returns          void
480 **
481 *******************************************************************************/
bta_hh_api_disc_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)482 void bta_hh_api_disc_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
483 {
484     tBTA_HH_CBDATA    disc_dat;
485     tHID_STATUS     status;
486 
487 #if BTA_HH_LE_INCLUDED == TRUE
488     if (p_cb->is_le_device) {
489         bta_hh_le_api_disc_act(p_cb);
490     } else
491 #endif
492     {
493         /* found an active connection */
494         disc_dat.handle = p_data ? (UINT8)p_data->hdr.layer_specific : p_cb->hid_handle;
495         disc_dat.status = BTA_HH_ERR;
496 
497         status = HID_HostCloseDev(disc_dat.handle);
498 
499         if (status) {
500             (* bta_hh_cb.p_cback)(BTA_HH_CLOSE_EVT, (tBTA_HH *)&disc_dat);
501         }
502     }
503 
504     return;
505 
506 }
507 /*******************************************************************************
508 **
509 ** Function         bta_hh_open_cmpl_act
510 **
511 ** Description      HID host connection completed
512 **
513 **
514 ** Returns          void
515 **
516 *******************************************************************************/
bta_hh_open_cmpl_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)517 void bta_hh_open_cmpl_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
518 {
519     tBTA_HH_CONN        conn ;
520     UINT8   dev_handle = p_data ? (UINT8)p_data->hid_cback.hdr.layer_specific : \
521                          p_cb->hid_handle;
522 
523     memset((void *)&conn, 0, sizeof (tBTA_HH_CONN));
524     conn.handle = dev_handle;
525     conn.is_orig = HID_HostConnectOrig(dev_handle);
526     bdcpy(conn.bda, p_cb->addr);
527 
528     /* increase connection number */
529     bta_hh_cb.cnt_num ++;
530 
531     /* initialize device driver */
532     bta_hh_co_open(p_cb->hid_handle, p_cb->sub_class,
533                    p_cb->attr_mask,  p_cb->app_id);
534 
535 #if (BTA_HH_LE_INCLUDED == TRUE)
536     conn.status = p_cb->status;
537     conn.le_hid = p_cb->is_le_device;
538     conn.scps_supported = p_cb->scps_supported;
539 
540     if (!p_cb->is_le_device)
541 #endif
542     {
543         /* inform role manager */
544         bta_sys_conn_open( BTA_ID_HH , p_cb->app_id, p_cb->addr);
545     }
546     /* set protocol mode when not default report mode */
547     if ( p_cb->mode != BTA_HH_PROTO_RPT_MODE
548 #if (BTA_HH_LE_INCLUDED == TRUE)
549             && !p_cb->is_le_device
550 #endif
551        ) {
552         if ((HID_HostWriteDev(dev_handle,
553                               HID_TRANS_SET_PROTOCOL, HID_PAR_PROTOCOL_BOOT_MODE,
554                               0,
555                               0, NULL)) != HID_SUCCESS) {
556             /* HID connection is up, while SET_PROTO fail */
557             conn.status = BTA_HH_ERR_PROTO;
558             (* bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH *)&conn);
559         } else {
560             conn.status = BTA_HH_OK;
561             p_cb->w4_evt = BTA_HH_OPEN_EVT;
562         }
563     } else {
564         (* bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH *)&conn);
565     }
566 
567     p_cb->incoming_conn = FALSE;
568     p_cb->incoming_hid_handle = BTA_HH_INVALID_HANDLE;
569 
570 }
571 /*******************************************************************************
572 **
573 ** Function         bta_hh_open_act
574 **
575 ** Description      HID host receive HID_OPEN_EVT .
576 **
577 **
578 ** Returns          void
579 **
580 *******************************************************************************/
bta_hh_open_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)581 void bta_hh_open_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
582 {
583     tBTA_HH_API_CONN    conn_data;
584 
585     UINT8   dev_handle = p_data ? (UINT8)p_data->hid_cback.hdr.layer_specific : \
586                          p_cb->hid_handle;
587 
588 #if BTA_HH_DEBUG
589     APPL_TRACE_EVENT ("bta_hh_open_act:  Device[%d] connected", dev_handle);
590 #endif
591 
592     p_cb->incoming_conn = TRUE;
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 connection request arrives without SDP
598             performed, do it first */
599     {
600         /* store the handle here in case sdp fails - need to disconnect */
601         p_cb->incoming_hid_handle = dev_handle;
602 
603         memset(&conn_data, 0, sizeof(tBTA_HH_API_CONN));
604         bdcpy(conn_data.bd_addr, p_cb->addr);
605         bta_hh_start_sdp(p_cb, (tBTA_HH_DATA *)&conn_data);
606     }
607 
608     return;
609 }
610 
611 
612 /*******************************************************************************
613 **
614 ** Function         bta_hh_data_act
615 **
616 ** Description      HID Host process a data report
617 **
618 **
619 ** Returns          void
620 **
621 *******************************************************************************/
bta_hh_data_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)622 void bta_hh_data_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
623 {
624     BT_HDR  *pdata = p_data->hid_cback.p_data;
625     UINT8   *p_rpt = (UINT8 *)(pdata + 1) + pdata->offset;
626 
627     bta_hh_co_data((UINT8)p_data->hid_cback.hdr.layer_specific, p_rpt, pdata->len,
628                    p_cb->mode, p_cb->sub_class, p_cb->dscp_info.ctry_code, p_cb->addr, p_cb->app_id);
629 
630     utl_freebuf((void **)&pdata);
631 }
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 {
646     tBTA_HH_CBDATA  cback_data ;
647     tBTA_HH_HSDATA  hs_data;
648     tBTA_HH_CONN    conn ;
649 
650 #if BTA_HH_DEBUG
651     APPL_TRACE_DEBUG("HANDSHAKE received for: event = %s data= %d",
652                      bta_hh_get_w4_event(p_cb->w4_evt), p_data->hid_cback.data);
653 #endif
654 
655     memset(&hs_data, 0, sizeof(tBTA_HH_HSDATA));
656     memset(&cback_data, 0, sizeof(tBTA_HH_CBDATA));
657 
658     switch (p_cb->w4_evt) {
659     /* GET_ transsaction, handshake indicate unsupported request */
660     case BTA_HH_GET_PROTO_EVT:
661         hs_data.rsp_data.proto_mode = BTA_HH_PROTO_UNKNOWN;
662     /* fall through */
663     case BTA_HH_GET_RPT_EVT:
664     case BTA_HH_GET_IDLE_EVT :
665         hs_data.handle = p_cb->hid_handle;
666         /* if handshake gives an OK code for these transaction, fill in UNSUPT */
667         if ((hs_data.status = bta_hh_get_trans_status(p_data->hid_cback.data)) == BTA_HH_OK) {
668             hs_data.status = BTA_HH_HS_TRANS_NOT_SPT;
669         }
670 
671         (* bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH *)&hs_data);
672         p_cb->w4_evt = 0;
673         break;
674 
675     /* acknowledgement from HID device for SET_ transaction */
676     case BTA_HH_SET_RPT_EVT:
677     case BTA_HH_SET_PROTO_EVT:
678     case BTA_HH_SET_IDLE_EVT :
679         cback_data.handle  = p_cb->hid_handle;
680         cback_data.status = bta_hh_get_trans_status(p_data->hid_cback.data);
681         if (cback_data.status == BTA_HH_OK) {
682             p_cb->mode = p_cb->new_mode;
683         } else {
684             p_cb->new_mode = p_cb->mode;
685         }
686         (* bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH *)&cback_data);
687         p_cb->w4_evt = 0;
688         break;
689 
690     /* SET_PROTOCOL when open connection */
691     case BTA_HH_OPEN_EVT:
692         conn.status = p_data->hid_cback.data ? BTA_HH_ERR_PROTO : BTA_HH_OK;
693         conn.handle = p_cb->hid_handle;
694         conn.is_orig = HID_HostConnectOrig(p_cb->hid_handle);
695         bdcpy(conn.bda, p_cb->addr);
696         (* bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH *)&conn);
697 #if BTA_HH_DEBUG
698         bta_hh_trace_dev_db();
699 #endif
700         p_cb->w4_evt = 0;
701         break;
702 
703     default:
704         /* unknown transaction handshake response */
705         APPL_TRACE_DEBUG("unknown transaction type");
706         break;
707     }
708 
709     /* transaction acknowledgement received, inform PM for mode change */
710     bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
711     return;
712 }
713 /*******************************************************************************
714 **
715 ** Function         bta_hh_ctrl_dat_act
716 **
717 ** Description      HID Host process a data report from control channel.
718 **
719 **
720 ** Returns          void
721 **
722 *******************************************************************************/
bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)723 void bta_hh_ctrl_dat_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
724 {
725     BT_HDR          *pdata = p_data->hid_cback.p_data;
726     UINT8           *data = (UINT8 *)(pdata + 1) + pdata->offset;
727     tBTA_HH_HSDATA    hs_data;
728 
729 #if BTA_HH_DEBUG
730     APPL_TRACE_DEBUG("Ctrl DATA received w4: event[%s]",
731                      bta_hh_get_w4_event(p_cb->w4_evt));
732 #endif
733     hs_data.status  = BTA_HH_OK;
734     hs_data.handle  = p_cb->hid_handle;
735 
736     switch (p_cb->w4_evt) {
737     case BTA_HH_GET_IDLE_EVT:
738         hs_data.rsp_data.idle_rate = *data;
739         break;
740     case BTA_HH_GET_RPT_EVT:
741         hs_data.rsp_data.p_rpt_data = pdata;
742         break;
743     case BTA_HH_GET_PROTO_EVT:
744         /* match up BTE/BTA report/boot mode def*/
745         hs_data.rsp_data.proto_mode = ((*data) == HID_PAR_PROTOCOL_REPORT) ? \
746                                       BTA_HH_PROTO_RPT_MODE : BTA_HH_PROTO_BOOT_MODE;
747 #if BTA_HH_DEBUG
748         APPL_TRACE_DEBUG("GET_PROTOCOL Mode = [%s]",
749                          (hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE) ? "Report" : "Boot");
750 #endif
751         break;
752     /* should not expect control DATA for SET_ transaction */
753     case BTA_HH_SET_PROTO_EVT:
754     /* fall through */
755     case BTA_HH_SET_RPT_EVT:
756     /* fall through */
757     case BTA_HH_SET_IDLE_EVT :
758     /* fall through */
759     default:
760 #if BTA_HH_DEBUG
761         APPL_TRACE_DEBUG("invalid  transaction type for DATA payload: 4_evt[%s]",
762                          bta_hh_get_w4_event(p_cb->w4_evt));
763 #endif
764         break;
765     }
766 
767     /* inform PM for mode change */
768     bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
769     bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
770 
771     (* bta_hh_cb.p_cback)(p_cb->w4_evt, (tBTA_HH *)&hs_data);
772 
773     p_cb->w4_evt = 0;
774     utl_freebuf((void **)&pdata);
775 
776 }
777 
778 /*******************************************************************************
779 **
780 ** Function         bta_hh_open_failure
781 **
782 ** Description      report HID open failure when at wait for connection state and receive
783 **                  device close event.
784 **
785 **
786 ** Returns          void
787 **
788 *******************************************************************************/
bta_hh_open_failure(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)789 void bta_hh_open_failure(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
790 {
791     tBTA_HH_CONN            conn_dat ;
792     UINT32                  reason = p_data->hid_cback.data;    /* Reason for closing (32-bit) */
793 
794     memset(&conn_dat, 0, sizeof(tBTA_HH_CONN));
795     conn_dat.handle = p_cb->hid_handle;
796     conn_dat.status = (reason == HID_ERR_AUTH_FAILED) ?
797                       BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
798     /* check if host initiate the connection*/
799     conn_dat.is_orig = HID_HostConnectOrig(p_cb->hid_handle);
800     bdcpy(conn_dat.bda, p_cb->addr);
801     HID_HostCloseDev(p_cb->hid_handle);
802 
803     /* Report OPEN fail event */
804     (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH *)&conn_dat);
805 
806 #if BTA_HH_DEBUG
807     bta_hh_trace_dev_db();
808 #endif
809     /* clean up control block, but retain SDP info and device handle */
810     p_cb->vp            = FALSE;
811     p_cb->w4_evt        = 0;
812 
813     /* if no connection is active and HH disable is signaled, disable service */
814     if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
815         bta_hh_disc_cmpl();
816     }
817 
818 }
819 
820 /*******************************************************************************
821 **
822 ** Function         bta_hh_close_act
823 **
824 ** Description      HID Host process a close event
825 **
826 **
827 ** Returns          void
828 **
829 *******************************************************************************/
bta_hh_close_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)830 void bta_hh_close_act (tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
831 {
832     tBTA_HH_CONN            conn_dat ;
833     tBTA_HH_CBDATA          disc_dat = {BTA_HH_OK, 0};
834     UINT32                  reason = p_data->hid_cback.data;    /* Reason for closing (32-bit) */
835 
836     /* if HID_HDEV_EVT_VC_UNPLUG was received, report BTA_HH_VC_UNPLUG_EVT */
837     UINT16     event = p_cb->vp ? BTA_HH_VC_UNPLUG_EVT : BTA_HH_CLOSE_EVT;
838 
839     disc_dat.handle = p_cb->hid_handle;
840     disc_dat.status = p_data->hid_cback.data;
841 
842     /* Check reason for closing */
843     if ((reason & (HID_L2CAP_CONN_FAIL | HID_L2CAP_REQ_FAIL)) || /* Failure to initialize connection (page timeout or l2cap error) */
844             (reason == HID_ERR_AUTH_FAILED) ||                      /* Authentication error (while initiating) */
845             (reason == HID_ERR_L2CAP_FAILED)) {                     /* Failure creating l2cap connection */
846         /* Failure in opening connection */
847         conn_dat.handle = p_cb->hid_handle;
848         conn_dat.status = (reason == HID_ERR_AUTH_FAILED) ? BTA_HH_ERR_AUTH_FAILED : BTA_HH_ERR;
849         /* check if host initiate the connection*/
850         conn_dat.is_orig = HID_HostConnectOrig(p_cb->hid_handle);
851         bdcpy(conn_dat.bda, p_cb->addr);
852         HID_HostCloseDev(p_cb->hid_handle);
853 
854         /* Report OPEN fail event */
855         (*bta_hh_cb.p_cback)(BTA_HH_OPEN_EVT, (tBTA_HH *)&conn_dat);
856 
857 #if BTA_HH_DEBUG
858         bta_hh_trace_dev_db();
859 #endif
860         return;
861     }
862     /* otherwise report CLOSE/VC_UNPLUG event */
863     else {
864         /* finaliza device driver */
865         bta_hh_co_close(p_cb->hid_handle, p_cb->app_id);
866         /* inform role manager */
867         bta_sys_conn_close( BTA_ID_HH , p_cb->app_id, p_cb->addr);
868         /* update total conn number */
869         bta_hh_cb.cnt_num --;
870 
871         if (disc_dat.status) {
872             disc_dat.status = BTA_HH_ERR;
873         }
874 
875         (*bta_hh_cb.p_cback)(event, (tBTA_HH *)&disc_dat);
876 
877         /* if virtually unplug, remove device */
878         if (p_cb->vp ) {
879             HID_HostRemoveDev( p_cb->hid_handle);
880             bta_hh_clean_up_kdev(p_cb);
881         }
882 
883 #if BTA_HH_DEBUG
884         bta_hh_trace_dev_db();
885 #endif
886     }
887 
888     /* clean up control block, but retain SDP info and device handle */
889     p_cb->vp            = FALSE;
890     p_cb->w4_evt        = 0;
891 
892     /* if no connection is active and HH disable is signaled, disable service */
893     if (bta_hh_cb.cnt_num == 0 && bta_hh_cb.w4_disable) {
894         bta_hh_disc_cmpl();
895     }
896 
897     return;
898 }
899 
900 /*******************************************************************************
901 **
902 ** Function         bta_hh_get_dscp_act
903 **
904 ** Description      Get device report descriptor
905 **
906 **
907 ** Returns          void
908 **
909 *******************************************************************************/
bta_hh_get_dscp_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)910 void bta_hh_get_dscp_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
911 {
912     UNUSED(p_data);
913 
914 #if (BTA_HH_LE_INCLUDED == TRUE)
915     if (p_cb->is_le_device) {
916         bta_hh_le_get_dscp_act(p_cb);
917     } else
918 #endif
919     {
920         (*bta_hh_cb.p_cback)(BTA_HH_GET_DSCP_EVT, (tBTA_HH *)&p_cb->dscp_info);
921     }
922 }
923 
924 /*******************************************************************************
925 **
926 ** Function         bta_hh_maint_dev_act
927 **
928 ** Description      HID Host maintain device list.
929 **
930 **
931 ** Returns          void
932 **
933 *******************************************************************************/
bta_hh_maint_dev_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)934 void bta_hh_maint_dev_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
935 {
936     tBTA_HH_MAINT_DEV       *p_dev_info = &p_data->api_maintdev;
937     tBTA_HH_DEV_INFO        dev_info ;
938     UINT8                   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         bdcpy(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                 dev_info.status   = BTA_HH_OK;
952             } else
953 #endif
954             {
955                 if (HID_HostAddDev(p_dev_info->bda, p_dev_info->attr_mask, &dev_handle)\
956                         == HID_SUCCESS) {
957                     dev_info.handle   = dev_handle;
958 
959 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
960                     /* update DI information */
961                     bta_hh_update_di_info(p_cb,
962                                           p_dev_info->dscp_info.vendor_id,
963                                           p_dev_info->dscp_info.product_id,
964                                           p_dev_info->dscp_info.version,
965                                           p_dev_info->dscp_info.flag);
966 #else
967                     bta_hh_update_di_info(p_cb,
968                                           p_dev_info->dscp_info.vendor_id,
969                                           p_dev_info->dscp_info.product_id,
970                                           p_dev_info->dscp_info.version,
971                                           0);
972 
973 #endif
974                     /* add to BTA device list */
975                     bta_hh_add_device_to_list(p_cb, dev_handle,
976                                               p_dev_info->attr_mask,
977                                               &p_dev_info->dscp_info.descriptor,
978                                               p_dev_info->sub_class,
979                                               p_dev_info->dscp_info.ssr_max_latency,
980                                               p_dev_info->dscp_info.ssr_min_tout,
981                                               p_dev_info->app_id);
982                     /* update cb_index[] map */
983                     bta_hh_cb.cb_index[dev_handle] = p_cb->index;
984                 }
985             }
986         } else { /* device already been added */
987             dev_info.handle = p_cb->hid_handle;
988             dev_info.status = BTA_HH_OK;
989         }
990 #if BTA_HH_DEBUG
991         bta_hh_trace_dev_db();
992 #endif
993 
994         break;
995     case BTA_HH_RMV_DEV_EVT:    /* remove device */
996         dev_info.handle = (UINT8)p_dev_info->hdr.layer_specific;
997         bdcpy(dev_info.bda, p_cb->addr);
998 
999 #if BTA_HH_LE_INCLUDED == TRUE
1000         if (p_cb->is_le_device) {
1001             bta_hh_le_remove_dev_bg_conn(p_cb);
1002             bta_hh_sm_execute(p_cb, BTA_HH_API_CLOSE_EVT, NULL);
1003             bta_hh_clean_up_kdev(p_cb);
1004         } else
1005 #endif
1006         {
1007             if (HID_HostRemoveDev( dev_info.handle ) == HID_SUCCESS) {
1008                 dev_info.status  = BTA_HH_OK;
1009 
1010                 /* remove from known device list in BTA */
1011                 bta_hh_clean_up_kdev(p_cb);
1012             }
1013         }
1014         break;
1015 
1016     default:
1017         APPL_TRACE_DEBUG("invalid command");
1018         break;
1019     }
1020 
1021     (* bta_hh_cb.p_cback)(p_dev_info->sub_event, (tBTA_HH *)&dev_info);
1022 }
1023 /*******************************************************************************
1024 **
1025 ** Function         bta_hh_write_dev_act
1026 **
1027 ** Description      Write device action. can be SET/GET/DATA transaction.
1028 **
1029 ** Returns          void
1030 **
1031 *******************************************************************************/
bta_hh_write_dev_act(tBTA_HH_DEV_CB * p_cb,tBTA_HH_DATA * p_data)1032 void bta_hh_write_dev_act(tBTA_HH_DEV_CB *p_cb, tBTA_HH_DATA *p_data)
1033 {
1034     tHID_STATUS status;
1035     tBTA_HH_CBDATA     cbdata = {BTA_HH_OK, 0};
1036     tBTA_HH_API_SENDDATA send_data = {BTA_HH_OK, 0, 0};
1037     UINT16  event = (p_data->api_sndcmd.t_type - BTA_HH_FST_BTE_TRANS_EVT) +
1038                     BTA_HH_FST_TRANS_CB_EVT;
1039 
1040 #if BTA_HH_LE_INCLUDED == TRUE
1041     if (p_cb->is_le_device) {
1042         bta_hh_le_write_dev_act(p_cb, p_data);
1043     } else
1044 #endif
1045     {
1046 
1047         cbdata.handle = p_cb->hid_handle;
1048         send_data.handle = p_cb->hid_handle;
1049 
1050         /* match up BTE/BTA report/boot mode def */
1051         if (p_data->api_sndcmd.t_type == HID_TRANS_SET_PROTOCOL) {
1052             p_cb->new_mode = p_data->api_sndcmd.param;
1053             p_data->api_sndcmd.param = ( p_data->api_sndcmd.param == BTA_HH_PROTO_RPT_MODE) ? \
1054                                        HID_PAR_PROTOCOL_REPORT : HID_PAR_PROTOCOL_BOOT_MODE;
1055         }
1056 
1057         status = HID_HostWriteDev(p_cb->hid_handle, p_data->api_sndcmd.t_type, p_data->api_sndcmd.param,
1058                                   p_data->api_sndcmd.data, p_data->api_sndcmd.rpt_id, p_data->api_sndcmd.p_data);
1059         if (status != HID_SUCCESS) {
1060             APPL_TRACE_ERROR("HID_HostWriteDev status:%d", status);
1061             cbdata.status = BTA_HH_ERR;
1062             send_data.status = BTA_HH_ERR;
1063 
1064             if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL) {
1065                 switch (p_data->api_sndcmd.t_type) {
1066                 case HID_TRANS_DATA:
1067                     event = BTA_HH_DATA_EVT;
1068                     send_data.reason = status;
1069                     (*bta_hh_cb.p_cback)(event, (tBTA_HH *)&send_data);
1070                     break;
1071                 default:
1072                     (*bta_hh_cb.p_cback)(event, (tBTA_HH *)&cbdata);
1073                     break;
1074                 }
1075             } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG) {
1076                 (* bta_hh_cb.p_cback)(BTA_HH_VC_UNPLUG_EVT, (tBTA_HH *)&cbdata);
1077             }
1078         } else {
1079 
1080             switch (p_data->api_sndcmd.t_type) {
1081             case HID_TRANS_SET_PROTOCOL:
1082             /* fall through */
1083             case HID_TRANS_GET_REPORT:
1084             /* fall through */
1085             case HID_TRANS_SET_REPORT:
1086             /* fall through */
1087             case HID_TRANS_GET_PROTOCOL:
1088             /* fall through */
1089             case HID_TRANS_GET_IDLE:
1090             /* fall through */
1091             case HID_TRANS_SET_IDLE:/* set w4_handsk event name for callback function use */
1092                 p_cb->w4_evt = event;
1093                 break;
1094             case HID_TRANS_DATA:  /* output report */
1095                 (*bta_hh_cb.p_cback)(BTA_HH_DATA_EVT, (tBTA_HH *)&send_data);
1096             /* fall through */
1097             case HID_TRANS_CONTROL:
1098                 /* no handshake event will be generated */
1099                 /* if VC_UNPLUG is issued, set flag */
1100                 if (p_data->api_sndcmd.param == BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG) {
1101                     p_cb->vp = TRUE;
1102                 }
1103 
1104                 break;
1105             /* currently not expected */
1106             case HID_TRANS_DATAC:
1107             default:
1108                 APPL_TRACE_DEBUG("bta_hh_write_dev_act:: cmd type = %d",
1109                                  p_data->api_sndcmd.t_type);
1110                 break;
1111             }
1112 
1113             /* if not control type transaction, notify PM for energy control */
1114             if (p_data->api_sndcmd.t_type != HID_TRANS_CONTROL) {
1115                 /* inform PM for mode change */
1116                 bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1117                 bta_sys_idle(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1118             } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_SUSPEND) {
1119                 bta_sys_sco_close(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1120             } else if (p_data->api_sndcmd.param == BTA_HH_CTRL_EXIT_SUSPEND) {
1121                 bta_sys_busy(BTA_ID_HH, p_cb->app_id, p_cb->addr);
1122             }
1123         }
1124     }
1125     return;
1126 }
1127 
1128 /*****************************************************************************
1129 **  Static Function
1130 *****************************************************************************/
1131 /*******************************************************************************
1132 **
1133 ** Function         bta_hh_cback
1134 **
1135 ** Description      BTA HH callback function.
1136 **
1137 **
1138 ** Returns          void
1139 **
1140 *******************************************************************************/
bta_hh_cback(UINT8 dev_handle,BD_ADDR addr,UINT8 event,UINT32 data,BT_HDR * pdata)1141 static void bta_hh_cback (UINT8 dev_handle, BD_ADDR addr, UINT8 event,
1142                           UINT32 data, BT_HDR *pdata)
1143 {
1144     tBTA_HH_CBACK_DATA    *p_buf = NULL;
1145     UINT16  sm_event = BTA_HH_INVALID_EVT;
1146     UINT8   xx = 0;
1147 
1148 #if BTA_HH_DEBUG
1149     APPL_TRACE_DEBUG("bta_hh_cback::HID_event [%s]", bta_hh_hid_event_name(event));
1150 #endif
1151 
1152     switch (event) {
1153     case HID_HDEV_EVT_OPEN:
1154         sm_event = BTA_HH_INT_OPEN_EVT;
1155         break;
1156     case HID_HDEV_EVT_CLOSE:
1157         sm_event = BTA_HH_INT_CLOSE_EVT;
1158         break;
1159     case HID_HDEV_EVT_INTR_DATA:
1160         sm_event = BTA_HH_INT_DATA_EVT;
1161         break;
1162     case HID_HDEV_EVT_HANDSHAKE:
1163         sm_event = BTA_HH_INT_HANDSK_EVT;
1164         break;
1165     case HID_HDEV_EVT_CTRL_DATA:
1166         sm_event = BTA_HH_INT_CTRL_DATA;
1167         break;
1168     case HID_HDEV_EVT_RETRYING:
1169         break;
1170     case HID_HDEV_EVT_INTR_DATC:
1171     case HID_HDEV_EVT_CTRL_DATC:
1172         /* Unhandled events: Free buffer for DATAC */
1173         utl_freebuf((void **)&pdata);
1174         break;
1175     case HID_HDEV_EVT_VC_UNPLUG:
1176         for (xx = 0; xx < BTA_HH_MAX_DEVICE; xx++) {
1177             if (bta_hh_cb.kdev[xx].hid_handle == dev_handle) {
1178                 bta_hh_cb.kdev[xx].vp = TRUE;
1179                 break;
1180             }
1181         }
1182         break;
1183     }
1184 
1185     if (sm_event != BTA_HH_INVALID_EVT &&
1186             (p_buf = (tBTA_HH_CBACK_DATA *)osi_malloc(sizeof(tBTA_HH_CBACK_DATA) +
1187                      sizeof(BT_HDR))) != NULL) {
1188         p_buf->hdr.event  = sm_event;
1189         p_buf->hdr.layer_specific = (UINT16)dev_handle;
1190         p_buf->data       = data;
1191         bdcpy(p_buf->addr, addr);
1192         p_buf->p_data     = pdata;
1193 
1194         bta_sys_sendmsg(p_buf);
1195     }
1196 
1197 }
1198 /*******************************************************************************
1199 **
1200 ** Function         bta_hh_get_trans_status
1201 **
1202 ** Description      translate a handshake result code into BTA HH
1203 **                  status code
1204 **
1205 *******************************************************************************/
bta_hh_get_trans_status(UINT32 result)1206 static tBTA_HH_STATUS bta_hh_get_trans_status(UINT32 result)
1207 {
1208     switch (result) {
1209     case HID_PAR_HANDSHAKE_RSP_SUCCESS :                /*   (0) */
1210         return BTA_HH_OK;
1211     case HID_PAR_HANDSHAKE_RSP_NOT_READY :              /*   (1) */
1212     case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_REP_ID:      /*   (2) */
1213     case HID_PAR_HANDSHAKE_RSP_ERR_UNSUPPORTED_REQ :    /*   (3) */
1214     case HID_PAR_HANDSHAKE_RSP_ERR_INVALID_PARAM :      /*   (4) */
1215         return (tBTA_HH_STATUS)result;
1216     case HID_PAR_HANDSHAKE_RSP_ERR_UNKNOWN :            /*   (14) */
1217     case HID_PAR_HANDSHAKE_RSP_ERR_FATAL  :             /*   (15) */
1218     default:
1219         return BTA_HH_HS_ERROR;
1220         break;
1221     }
1222 }
1223 /*****************************************************************************
1224 **  Debug Functions
1225 *****************************************************************************/
1226 
1227 #if (defined BTA_HH_DEBUG && BTA_HH_DEBUG == TRUE)
bta_hh_get_w4_event(UINT16 event)1228 static char *bta_hh_get_w4_event(UINT16 event)
1229 {
1230     switch (event) {
1231     case BTA_HH_GET_RPT_EVT:
1232         return "BTA_HH_GET_RPT_EVT";
1233     case BTA_HH_SET_RPT_EVT:
1234         return "BTA_HH_SET_RPT_EVT";
1235     case BTA_HH_GET_PROTO_EVT:
1236         return "BTA_HH_GET_PROTO_EVT";
1237     case BTA_HH_SET_PROTO_EVT:
1238         return "BTA_HH_SET_PROTO_EVT";
1239     case BTA_HH_GET_IDLE_EVT:
1240         return "BTA_HH_GET_IDLE_EVT";
1241     case BTA_HH_SET_IDLE_EVT:
1242         return "BTA_HH_SET_IDLE_EVT";
1243     case BTA_HH_OPEN_EVT:
1244         return "BTA_HH_OPEN_EVT";
1245     default:
1246         return "Unknown event";
1247     }
1248 
1249 }
1250 
bta_hh_hid_event_name(UINT16 event)1251 static char *bta_hh_hid_event_name(UINT16 event)
1252 {
1253     switch (event) {
1254     case HID_HDEV_EVT_OPEN:
1255         return "HID_HDEV_EVT_OPEN";
1256     case HID_HDEV_EVT_CLOSE:
1257         return "HID_HDEV_EVT_CLOSE";
1258     case HID_HDEV_EVT_RETRYING:
1259         return "HID_HDEV_EVT_RETRYING";
1260     case HID_HDEV_EVT_INTR_DATA:
1261         return "HID_HDEV_EVT_INTR_DATA";
1262     case HID_HDEV_EVT_INTR_DATC:
1263         return "HID_HDEV_EVT_INTR_DATC";
1264     case HID_HDEV_EVT_CTRL_DATA:
1265         return "HID_HDEV_EVT_CTRL_DATA";
1266     case HID_HDEV_EVT_CTRL_DATC:
1267         return "HID_HDEV_EVT_CTRL_DATC";
1268     case HID_HDEV_EVT_HANDSHAKE:
1269         return "HID_HDEV_EVT_HANDSHAKE";
1270     case HID_HDEV_EVT_VC_UNPLUG:
1271         return "HID_HDEV_EVT_VC_UNPLUG";
1272     default:
1273         return "Unknown HID event";
1274     }
1275 }
1276 #endif
1277 #endif /* BTA_HH_INCLUDED */
1278