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