1 /******************************************************************************
2  *
3  *  Copyright (C) 2002-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 API entry points
22  *
23  ******************************************************************************/
24 
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 
29 #include "common/bt_target.h"
30 #include "osi/allocator.h"
31 #include "stack/bt_types.h"
32 #include "stack/hiddefs.h"
33 #include "stack/hidh_api.h"
34 #include "hid_int.h"
35 #include "stack/btm_api.h"
36 #include "stack/btu.h"
37 #include "btm_int.h"
38 
39 #if (HID_HOST_INCLUDED == TRUE)
40 
41 #if HID_DYNAMIC_MEMORY == FALSE
42 tHID_HOST_CTB hh_cb;
43 #else
44 tHID_HOST_CTB *hidh_cb_ptr = NULL;
45 #endif
46 
47 static void hidh_search_callback (UINT16 sdp_result);
48 
49 /*******************************************************************************
50 **
51 ** Function         HID_HostGetSDPRecord
52 **
53 ** Description      This function reads the device SDP record
54 **
55 ** Returns          tHID_STATUS
56 **
57 *******************************************************************************/
HID_HostGetSDPRecord(BD_ADDR addr,tSDP_DISCOVERY_DB * p_db,UINT32 db_len,tHID_HOST_SDP_CALLBACK * sdp_cback)58 tHID_STATUS HID_HostGetSDPRecord ( BD_ADDR addr, tSDP_DISCOVERY_DB *p_db, UINT32 db_len,
59                                    tHID_HOST_SDP_CALLBACK *sdp_cback )
60 {
61     tSDP_UUID   uuid_list;
62 
63     if ( hh_cb.sdp_busy ) {
64         return HID_ERR_SDP_BUSY;
65     }
66 
67     uuid_list.len = 2;
68     uuid_list.uu.uuid16 = UUID_SERVCLASS_HUMAN_INTERFACE;
69 
70     hh_cb.p_sdp_db = p_db;
71     SDP_InitDiscoveryDb (p_db, db_len, 1, &uuid_list, 0, NULL);
72 
73     if (SDP_ServiceSearchRequest (addr, p_db, hidh_search_callback)) {
74         hh_cb.sdp_cback = sdp_cback ;
75         hh_cb.sdp_busy = TRUE;
76         return HID_SUCCESS;
77     } else {
78         return HID_ERR_NO_RESOURCES;
79     }
80 }
81 
hidh_get_str_attr(tSDP_DISC_REC * p_rec,UINT16 attr_id,UINT16 max_len,char * str)82 void hidh_get_str_attr( tSDP_DISC_REC *p_rec, UINT16 attr_id, UINT16 max_len, char *str )
83 {
84     tSDP_DISC_ATTR          *p_attr;
85     UINT16                  name_len;
86 
87     if ((p_attr = SDP_FindAttributeInRec(p_rec, attr_id)) != NULL) {
88         if ((name_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type)) < max_len ) {
89             memcpy( str, (char *) p_attr->attr_value.v.array, name_len );
90             str[name_len] = '\0';
91         } else {
92             memcpy( str, (char *) p_attr->attr_value.v.array, max_len - 1 );
93             str[max_len - 1] = '\0';
94         }
95     } else {
96         str[0] = '\0';
97     }
98 }
99 
100 
hidh_search_callback(UINT16 sdp_result)101 static void hidh_search_callback (UINT16 sdp_result)
102 {
103     tSDP_DISCOVERY_DB       *p_db = hh_cb.p_sdp_db;
104     tSDP_DISC_REC           *p_rec;
105     tSDP_DISC_ATTR          *p_attr, *p_subattr1, *p_subattr2, *p_repdesc;
106     tBT_UUID                hid_uuid;
107     tHID_DEV_SDP_INFO       *p_nvi = &hh_cb.sdp_rec;
108     UINT16                  attr_mask = 0;
109 
110     hid_uuid.len       = LEN_UUID_16;
111     hid_uuid.uu.uuid16 = UUID_SERVCLASS_HUMAN_INTERFACE;
112 
113     hh_cb.sdp_busy = FALSE;
114 
115     if (sdp_result != SDP_SUCCESS) {
116         hh_cb.sdp_cback(sdp_result, 0, NULL);
117         return;
118     }
119 
120     if ((p_rec = SDP_FindServiceUUIDInDb (p_db, &hid_uuid, NULL)) == NULL) {
121         hh_cb.sdp_cback(HID_SDP_NO_SERV_UUID, 0, NULL);
122         return;
123     }
124 
125     memset (&hh_cb.sdp_rec, 0, sizeof( tHID_DEV_SDP_INFO ));
126 
127     /* First, verify the mandatory fields we care about */
128     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_DESCRIPTOR_LIST)) == NULL)
129             || (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE)
130             || ((p_subattr1 = p_attr->attr_value.v.p_sub_attr) == NULL)
131             || (SDP_DISC_ATTR_TYPE(p_subattr1->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE)
132             || ((p_subattr2 = p_subattr1->attr_value.v.p_sub_attr) == NULL)
133             || ((p_repdesc = p_subattr2->p_next_attr) == NULL)
134             || (SDP_DISC_ATTR_TYPE(p_repdesc->attr_len_type) != TEXT_STR_DESC_TYPE)) {
135         hh_cb.sdp_cback(HID_SDP_MANDATORY_MISSING, 0, NULL);
136         return;
137     }
138 
139     if ((p_nvi->dscp_info.dl_len = SDP_DISC_ATTR_LEN(p_repdesc->attr_len_type)) != 0) {
140         p_nvi->dscp_info.dsc_list = (UINT8 *) &p_repdesc->attr_value;
141     }
142 
143     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_VIRTUAL_CABLE)) != NULL) &&
144             (p_attr->attr_value.v.u8) ) {
145         attr_mask |= HID_VIRTUAL_CABLE;
146     }
147 
148     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_RECONNECT_INITIATE)) != NULL) &&
149             (p_attr->attr_value.v.u8) ) {
150         attr_mask |= HID_RECONN_INIT;
151     }
152 
153     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_NORMALLY_CONNECTABLE)) != NULL) &&
154             (p_attr->attr_value.v.u8) ) {
155         attr_mask |= HID_NORMALLY_CONNECTABLE;
156     }
157 
158     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_SDP_DISABLE)) != NULL) &&
159             (p_attr->attr_value.v.u8) ) {
160         attr_mask |= HID_SDP_DISABLE;
161     }
162 
163     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_BATTERY_POWER)) != NULL) &&
164             (p_attr->attr_value.v.u8) ) {
165         attr_mask |= HID_BATTERY_POWER;
166     }
167 
168     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_REMOTE_WAKE)) != NULL) &&
169             (p_attr->attr_value.v.u8) ) {
170         attr_mask |= HID_REMOTE_WAKE;
171     }
172 
173     hidh_get_str_attr( p_rec, ATTR_ID_SERVICE_NAME, HID_MAX_SVC_NAME_LEN, p_nvi->svc_name );
174     hidh_get_str_attr( p_rec, ATTR_ID_SERVICE_DESCRIPTION, HID_MAX_SVC_DESCR_LEN, p_nvi->svc_descr );
175     hidh_get_str_attr( p_rec, ATTR_ID_PROVIDER_NAME, HID_MAX_PROV_NAME_LEN, p_nvi->prov_name );
176 
177     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_DEVICE_RELNUM)) != NULL)) {
178         p_nvi->rel_num = p_attr->attr_value.v.u16;
179     }
180 
181     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_COUNTRY_CODE)) != NULL)) {
182         p_nvi->ctry_code = p_attr->attr_value.v.u8;
183     }
184 
185     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_DEVICE_SUBCLASS)) != NULL)) {
186         p_nvi->sub_class = p_attr->attr_value.v.u8;
187     }
188 
189     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_PARSER_VERSION)) != NULL)) {
190         p_nvi->hpars_ver = p_attr->attr_value.v.u16;
191     }
192 
193     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_LINK_SUPERVISION_TO)) != NULL)) {
194         attr_mask |= HID_SUP_TOUT_AVLBL;
195         p_nvi->sup_timeout = p_attr->attr_value.v.u16;
196     }
197 
198     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_SSR_HOST_MAX_LAT)) != NULL)) {
199         attr_mask |= HID_SSR_MAX_LATENCY;
200         p_nvi->ssr_max_latency = p_attr->attr_value.v.u16;
201     } else {
202         p_nvi->ssr_max_latency = HID_SSR_PARAM_INVALID;
203     }
204 
205     if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_SSR_HOST_MIN_TOUT)) != NULL)) {
206         attr_mask |= HID_SSR_MIN_TOUT;
207         p_nvi->ssr_min_tout = p_attr->attr_value.v.u16;
208     } else {
209         p_nvi->ssr_min_tout = HID_SSR_PARAM_INVALID;
210     }
211 
212     hh_cb.sdp_rec.p_sdp_layer_rec = p_rec;
213     hh_cb.sdp_cback(SDP_SUCCESS, attr_mask, &hh_cb.sdp_rec);
214 }
215 
216 
217 /*******************************************************************************
218 **
219 ** Function         HID_HostInit
220 **
221 ** Description      This function initializes the control block and trace variable
222 **
223 ** Returns          tHID_STATUS
224 **
225 *******************************************************************************/
HID_HostInit(void)226 tHID_STATUS HID_HostInit (void)
227 {
228 #if (HID_DYNAMIC_MEMORY)
229     if (!hidh_cb_ptr) {
230         hidh_cb_ptr = (tHID_HOST_CTB *)osi_malloc(sizeof(tHID_HOST_CTB));
231         if (!hidh_cb_ptr) {
232             return HID_ERR_NO_RESOURCES;
233         }
234     }
235 #endif /* #if (HID_DYNAMIC_MEMORY) */
236     memset(&hh_cb, 0, sizeof(tHID_HOST_CTB));
237 
238 #if defined(HIDH_INITIAL_TRACE_LEVEL)
239     hh_cb.trace_level = HIDH_INITIAL_TRACE_LEVEL;
240 #else
241     hh_cb.trace_level = BT_TRACE_LEVEL_NONE;
242 #endif
243     return HID_SUCCESS;
244 }
245 
246 /*******************************************************************************
247 **
248 ** Function         HID_HostInit
249 **
250 ** Description      This function deinitializes the control block
251 **
252 ** Returns          void
253 **
254 *******************************************************************************/
HID_HostDeinit(void)255 void HID_HostDeinit (void)
256 {
257 #if (HID_DYNAMIC_MEMORY)
258     if (hidh_cb_ptr) {
259         osi_free(hidh_cb_ptr);
260         hidh_cb_ptr = NULL;
261     }
262 #endif /* #if (HID_DYNAMIC_MEMORY) */
263 }
264 
265 /*******************************************************************************
266 **
267 ** Function         HID_HostSetTraceLevel
268 **
269 ** Description      This function sets the trace level for HID Host. If called with
270 **                  a value of 0xFF, it simply reads the current trace level.
271 **
272 ** Returns          the new (current) trace level
273 **
274 *******************************************************************************/
HID_HostSetTraceLevel(UINT8 new_level)275 UINT8 HID_HostSetTraceLevel (UINT8 new_level)
276 {
277     if (new_level != 0xFF) {
278         hh_cb.trace_level = new_level;
279     }
280 
281     return (hh_cb.trace_level);
282 }
283 
284 /*******************************************************************************
285 **
286 ** Function         HID_HostRegister
287 **
288 ** Description      This function registers HID-Host with lower layers
289 **
290 ** Returns          tHID_STATUS
291 **
292 *******************************************************************************/
HID_HostRegister(tHID_HOST_DEV_CALLBACK * dev_cback)293 tHID_STATUS HID_HostRegister (tHID_HOST_DEV_CALLBACK *dev_cback)
294 {
295     tHID_STATUS st;
296 
297     if ( hh_cb.reg_flag ) {
298         return HID_ERR_ALREADY_REGISTERED;
299     }
300 
301     if ( dev_cback == NULL ) {
302         return HID_ERR_INVALID_PARAM;
303     }
304 
305     /* Register with L2CAP */
306     if ( (st = hidh_conn_reg()) != HID_SUCCESS ) {
307         return st;
308     }
309 
310     hh_cb.callback = dev_cback ;
311     hh_cb.reg_flag = TRUE;
312 
313     return (HID_SUCCESS);
314 }
315 
316 /*******************************************************************************
317 **
318 ** Function         HID_HostDeregister
319 **
320 ** Description      This function is called when the host is about power down.
321 **
322 ** Returns          tHID_STATUS
323 **
324 *******************************************************************************/
HID_HostDeregister(void)325 tHID_STATUS HID_HostDeregister(void)
326 {
327     UINT8 i;
328 
329     if ( !hh_cb.reg_flag ) {
330         return (HID_ERR_NOT_REGISTERED);
331     }
332 
333     for ( i = 0; i < HID_HOST_MAX_DEVICES; i++ ) {
334         HID_HostRemoveDev( i ) ;
335     }
336 
337     hidh_conn_dereg();
338     hh_cb.reg_flag = FALSE;
339 
340     return (HID_SUCCESS) ;
341 }
342 
343 /*******************************************************************************
344 **
345 ** Function         HID_HostAddDev
346 **
347 ** Description      This is called so HID-host may manage this device.
348 **
349 ** Returns          tHID_STATUS
350 **
351 *******************************************************************************/
HID_HostAddDev(BD_ADDR addr,UINT16 attr_mask,UINT8 * handle)352 tHID_STATUS HID_HostAddDev ( BD_ADDR addr, UINT16 attr_mask, UINT8 *handle )
353 {
354     int i;
355     /* Find an entry for this device in hh_cb.devices array */
356     if ( !hh_cb.reg_flag ) {
357         return (HID_ERR_NOT_REGISTERED);
358     }
359 
360     for ( i = 0; i < HID_HOST_MAX_DEVICES; i++) {
361         if ((hh_cb.devices[i].in_use) &&
362                 (!memcmp(addr, hh_cb.devices[i].addr, BD_ADDR_LEN))) {
363             break;
364         }
365     }
366 
367     if (i == HID_HOST_MAX_DEVICES ) {
368         for ( i = 0; i < HID_HOST_MAX_DEVICES; i++) {
369             if ( !hh_cb.devices[i].in_use) {
370                 break;
371             }
372         }
373     }
374 
375     if ( i == HID_HOST_MAX_DEVICES ) {
376         return HID_ERR_NO_RESOURCES;
377     }
378 
379     if (!hh_cb.devices[i].in_use) {
380         hh_cb.devices[i].in_use = TRUE;
381         hh_cb.devices[i].delay_remove = FALSE;
382         memcpy( hh_cb.devices[i].addr, addr, sizeof( BD_ADDR ) ) ;
383         hh_cb.devices[i].state = HID_DEV_NO_CONN;
384         hh_cb.devices[i].conn_tries = 0 ;
385     }
386 
387     if (attr_mask != HID_ATTR_MASK_IGNORE) {
388         hh_cb.devices[i].attr_mask = attr_mask;
389     }
390 
391     *handle = i;
392 
393     return (HID_SUCCESS);
394 }
395 
396 /*******************************************************************************
397 **
398 ** Function         HID_HostGetDev
399 **
400 ** Description      This is called so HID-host can find this device.
401 **
402 ** Returns          tHID_STATUS
403 **
404 *******************************************************************************/
HID_HostGetDev(BD_ADDR addr,UINT8 * handle)405 tHID_STATUS HID_HostGetDev(BD_ADDR addr, UINT8 *handle)
406 {
407     int i;
408     /* Find an entry for this device in hh_cb.devices array */
409     if (!hh_cb.reg_flag) {
410         return (HID_ERR_NOT_REGISTERED);
411     }
412 
413     for (i = 0; i < HID_HOST_MAX_DEVICES; i++) {
414         if ((hh_cb.devices[i].in_use) && (!memcmp(addr, hh_cb.devices[i].addr, BD_ADDR_LEN))) {
415             break;
416         }
417     }
418 
419     if (i == HID_HOST_MAX_DEVICES) {
420         *handle = 0xff;
421     } else {
422         *handle = i;
423     }
424     return (HID_SUCCESS);
425 }
426 
427 /*******************************************************************************
428 **
429 ** Function         HID_HostRemoveDev
430 **
431 ** Description      This removes the device from list devices that host has to manage.
432 **
433 ** Returns          tHID_STATUS
434 **
435 *******************************************************************************/
HID_HostRemoveDev(UINT8 dev_handle)436 tHID_STATUS HID_HostRemoveDev ( UINT8 dev_handle )
437 {
438     if ( !hh_cb.reg_flag ) {
439         return (HID_ERR_NOT_REGISTERED);
440     }
441 
442     if ( (dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use) ) {
443         return HID_ERR_INVALID_PARAM;
444     }
445 
446     HID_HostCloseDev( dev_handle ) ;
447 
448     if (hh_cb.devices[dev_handle].conn.conn_state == HID_CONN_STATE_DISCONNECTING_INTR ||
449         hh_cb.devices[dev_handle].conn.conn_state == HID_CONN_STATE_DISCONNECTING_CTRL) {
450         // delay the remove action, to close the control and the interrupt channel
451         hh_cb.devices[dev_handle].delay_remove = TRUE;
452     } else {
453         HIDH_TRACE_WARNING("%s dev_handle:%d conn_state:%d", __func__, dev_handle,
454                            hh_cb.devices[dev_handle].conn.conn_state);
455         hh_cb.devices[dev_handle].in_use = FALSE;
456         hh_cb.devices[dev_handle].conn.conn_state = HID_CONN_STATE_UNUSED;
457         hh_cb.devices[dev_handle].conn.ctrl_cid = hh_cb.devices[dev_handle].conn.intr_cid = 0;
458         hh_cb.devices[dev_handle].attr_mask = 0;
459     }
460 
461     return HID_SUCCESS;
462 }
463 
464 /*******************************************************************************
465 **
466 ** Function         HID_HostOpenDev
467 **
468 ** Description      This function is called when the user wants to initiate a
469 **                  connection attempt to a device.
470 **
471 ** Returns          void
472 **
473 *******************************************************************************/
HID_HostOpenDev(UINT8 dev_handle)474 tHID_STATUS HID_HostOpenDev ( UINT8 dev_handle )
475 {
476     if ( !hh_cb.reg_flag ) {
477         return (HID_ERR_NOT_REGISTERED);
478     }
479 
480     if ( (dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use) ) {
481         return HID_ERR_INVALID_PARAM;
482     }
483 
484     if ( hh_cb.devices[dev_handle].state != HID_DEV_NO_CONN ) {
485         return HID_ERR_ALREADY_CONN;
486     }
487 
488     hh_cb.devices[dev_handle].conn_tries = 1;
489     return hidh_conn_initiate( dev_handle );
490 }
491 
492 /*******************************************************************************
493 **
494 ** Function         HID_HostWriteDev
495 **
496 ** Description      This function is called when the host has a report to send.
497 **
498 **                  report_id: is only used on GET_REPORT transaction if is specified.
499 **                              only valid when it's a non-zero value.
500 **
501 ** Returns          void
502 **
503 *******************************************************************************/
HID_HostWriteDev(UINT8 dev_handle,UINT8 t_type,UINT8 param,UINT16 data,UINT8 report_id,BT_HDR * pbuf)504 tHID_STATUS HID_HostWriteDev( UINT8 dev_handle, UINT8 t_type,
505                               UINT8 param, UINT16 data, UINT8 report_id, BT_HDR *pbuf  )
506 {
507     tHID_STATUS status = HID_SUCCESS;
508 
509     if ( !hh_cb.reg_flag ) {
510         HIDH_TRACE_ERROR("HID_ERR_NOT_REGISTERED");
511         status = HID_ERR_NOT_REGISTERED;
512     }
513 
514     if ( (dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use) ) {
515         HIDH_TRACE_ERROR("HID_ERR_INVALID_PARAM");
516         status = HID_ERR_INVALID_PARAM;
517     }
518 
519     else if ( hh_cb.devices[dev_handle].state != HID_DEV_CONNECTED ) {
520         HIDH_TRACE_ERROR("HID_ERR_NO_CONNECTION dev_handle %d", dev_handle);
521         status = HID_ERR_NO_CONNECTION;
522     }
523 
524     if (status != HID_SUCCESS) {
525         if (pbuf) {
526             osi_free ((void *)pbuf);
527         }
528     } else {
529         status = hidh_conn_snd_data( dev_handle, t_type, param, data, report_id, pbuf ) ;
530     }
531 
532     return status;
533 }
534 
535 /*******************************************************************************
536 **
537 ** Function         HID_HostCloseDev
538 **
539 ** Description      This function disconnects the device.
540 **
541 ** Returns          void
542 **
543 *******************************************************************************/
HID_HostCloseDev(UINT8 dev_handle)544 tHID_STATUS HID_HostCloseDev( UINT8 dev_handle )
545 {
546     if ( !hh_cb.reg_flag ) {
547         return (HID_ERR_NOT_REGISTERED);
548     }
549 
550     if ( (dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use) ) {
551         return HID_ERR_INVALID_PARAM;
552     }
553 
554     hh_cb.devices[dev_handle].conn_tries = HID_HOST_MAX_CONN_RETRY + 1;
555     btu_stop_timer( &(hh_cb.devices[dev_handle].conn.timer_entry) ) ;
556 
557     if ( hh_cb.devices[dev_handle].state != HID_DEV_CONNECTED ) {
558         return HID_ERR_NO_CONNECTION;
559     }
560 
561     hh_cb.devices[dev_handle].conn_tries = HID_HOST_MAX_CONN_RETRY + 1;
562     return hidh_conn_disconnect( dev_handle );
563 }
564 
HID_HostSetSecurityLevel(char serv_name[],UINT8 sec_lvl)565 tHID_STATUS HID_HostSetSecurityLevel( char serv_name[], UINT8 sec_lvl )
566 {
567     if (!BTM_SetSecurityLevel (FALSE, serv_name, BTM_SEC_SERVICE_HIDH_SEC_CTRL,
568                                sec_lvl, HID_PSM_CONTROL, BTM_SEC_PROTO_HID, HID_SEC_CHN)) {
569         HIDH_TRACE_ERROR ("Security Registration 1 failed");
570         return (HID_ERR_NO_RESOURCES);
571     }
572 
573     if (!BTM_SetSecurityLevel (TRUE, serv_name, BTM_SEC_SERVICE_HIDH_SEC_CTRL,
574                                sec_lvl, HID_PSM_CONTROL, BTM_SEC_PROTO_HID, HID_SEC_CHN)) {
575         HIDH_TRACE_ERROR ("Security Registration 2 failed");
576         return (HID_ERR_NO_RESOURCES);
577     }
578 
579     if (!BTM_SetSecurityLevel (FALSE, serv_name, BTM_SEC_SERVICE_HIDH_NOSEC_CTRL,
580                                BTM_SEC_NONE, HID_PSM_CONTROL, BTM_SEC_PROTO_HID, HID_NOSEC_CHN)) {
581         HIDH_TRACE_ERROR ("Security Registration 3 failed");
582         return (HID_ERR_NO_RESOURCES);
583     }
584 
585     if (!BTM_SetSecurityLevel (TRUE, serv_name, BTM_SEC_SERVICE_HIDH_NOSEC_CTRL,
586                                BTM_SEC_NONE, HID_PSM_CONTROL, BTM_SEC_PROTO_HID, HID_NOSEC_CHN)) {
587         HIDH_TRACE_ERROR ("Security Registration 4 failed");
588         return (HID_ERR_NO_RESOURCES);
589     }
590 
591     if (!BTM_SetSecurityLevel (TRUE, serv_name, BTM_SEC_SERVICE_HIDH_INTR,
592                                BTM_SEC_NONE, HID_PSM_INTERRUPT, BTM_SEC_PROTO_HID, 0)) {
593         HIDH_TRACE_ERROR ("Security Registration 5 failed");
594         return (HID_ERR_NO_RESOURCES);
595     }
596 
597     if (!BTM_SetSecurityLevel (FALSE, serv_name, BTM_SEC_SERVICE_HIDH_INTR,
598                                BTM_SEC_NONE, HID_PSM_INTERRUPT, BTM_SEC_PROTO_HID, 0)) {
599         HIDH_TRACE_ERROR ("Security Registration 6 failed");
600         return (HID_ERR_NO_RESOURCES);
601     }
602 
603     return ( HID_SUCCESS );
604 }
605 
606 /******************************************************************************
607 **
608 ** Function         hid_known_hid_device
609 **
610 ** Description      check if this device is  of type HID Device
611 **
612 ** Returns          TRUE if device is HID Device else FALSE
613 **
614 *******************************************************************************/
hid_known_hid_device(BD_ADDR bd_addr)615 BOOLEAN hid_known_hid_device (BD_ADDR bd_addr)
616 {
617     UINT8 i;
618     tBTM_INQ_INFO *p_inq_info = BTM_InqDbRead(bd_addr);
619 
620     if ( !hh_cb.reg_flag ) {
621         return FALSE;
622     }
623 
624     /* First  check for class of device , if Inq DB has information about this device*/
625     if (p_inq_info != NULL) {
626         /* Check if remote major device class is of type BTM_COD_MAJOR_PERIPHERAL */
627         if ((p_inq_info->results.dev_class[1] & BTM_COD_MAJOR_CLASS_MASK)
628                 == BTM_COD_MAJOR_PERIPHERAL ) {
629             HIDH_TRACE_DEBUG("hid_known_hid_device:dev found in InqDB & COD matches HID dev");
630             return TRUE;
631         }
632     } else {
633         /* Look for this device in security device DB */
634         tBTM_SEC_DEV_REC  *p_dev_rec = btm_find_dev (bd_addr);
635         if ((p_dev_rec != NULL) &&
636                 ((p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK) == BTM_COD_MAJOR_PERIPHERAL )) {
637             HIDH_TRACE_DEBUG("hid_known_hid_device:dev found in SecDevDB & COD matches HID dev");
638             return TRUE;
639         }
640     }
641 
642     /* Find an entry for this device in hh_cb.devices array */
643     for ( i = 0; i < HID_HOST_MAX_DEVICES; i++) {
644         if ((hh_cb.devices[i].in_use) &&
645                 (memcmp(bd_addr, hh_cb.devices[i].addr, BD_ADDR_LEN) == 0)) {
646             return TRUE;
647         }
648     }
649     /* Check if this device is marked as HID Device in IOP Dev */
650     HIDH_TRACE_DEBUG("hid_known_hid_device:remote is not HID device");
651     return FALSE;
652 }
653 
HID_HostConnectOrig(UINT8 dev_handle)654 BOOLEAN HID_HostConnectOrig(UINT8 dev_handle)
655 {
656     BOOLEAN ret = FALSE;
657 
658     do {
659         if (!hh_cb.reg_flag) {
660             break;
661         }
662 
663         if ((dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use)) {
664             break;
665         }
666 
667         ret = hidh_conn_is_orig(dev_handle);
668     } while (0);
669 
670     return ret;
671 }
672 
673 #endif //HID_HOST_INCLUDED
674