1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
58
59
60
61
62
63
66
67
68
69
70
71
72
73
80
81
82
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
119
120
124
125
126
127
128
129
130
131
132
133
134
138
139
142
143
144
147
148
149
152
153
154
157
158
159
162
163
164
167
168
169
172
173
174
175
176
177
180
181
184
185
188
189
192
193
197
198
204
205
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
264
265
266
267
268
269
270
271
272
273
274
275
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
300
301
304
305
306
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
332
333
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
359
360
366
367
374
375
378
379
386
387
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
412
413
418
419
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
441
442
445
446
447
448
449
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
479
480
483
484
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
513
514
518
519
523
524
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
549
550
553
554
555
556
557
560
561
562
563
564
565
566
567
568
572
573
574
578
579
580
584
585
586
590
591
592
596
597
598
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
623
624
625
641
642
643
649
650
651
652
653
654
655
656
657
658
669
670
671
672
673
/* ... */
/* ... */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "common/bt_target.h"
#include "osi/allocator.h"
#include "stack/bt_types.h"
#include "stack/hiddefs.h"
#include "stack/hidh_api.h"
#include "hid_int.h"
#include "stack/btm_api.h"
#include "stack/btu.h"
#include "btm_int.h"12 includes
#if (HID_HOST_INCLUDED == TRUE)
#if HID_DYNAMIC_MEMORY == FALSE
tHID_HOST_CTB hh_cb;
#else
tHID_HOST_CTB *hidh_cb_ptr = NULL;
#endif
static void hidh_search_callback (UINT16 sdp_result);
/* ... */
tHID_STATUS HID_HostGetSDPRecord ( BD_ADDR addr, tSDP_DISCOVERY_DB *p_db, UINT32 db_len,
tHID_HOST_SDP_CALLBACK *sdp_cback )
{
tSDP_UUID uuid_list;
if ( hh_cb.sdp_busy ) {
return HID_ERR_SDP_BUSY;
}{...}
uuid_list.len = 2;
uuid_list.uu.uuid16 = UUID_SERVCLASS_HUMAN_INTERFACE;
hh_cb.p_sdp_db = p_db;
SDP_InitDiscoveryDb (p_db, db_len, 1, &uuid_list, 0, NULL);
if (SDP_ServiceSearchRequest (addr, p_db, hidh_search_callback)) {
hh_cb.sdp_cback = sdp_cback ;
hh_cb.sdp_busy = TRUE;
return HID_SUCCESS;
}{...} else {
return HID_ERR_NO_RESOURCES;
}{...}
}{...}
void hidh_get_str_attr( tSDP_DISC_REC *p_rec, UINT16 attr_id, UINT16 max_len, char *str )
{
tSDP_DISC_ATTR *p_attr;
UINT16 name_len;
if ((p_attr = SDP_FindAttributeInRec(p_rec, attr_id)) != NULL) {
if ((name_len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type)) < max_len ) {
memcpy( str, (char *) p_attr->attr_value.v.array, name_len );
str[name_len] = '\0';
}{...} else {
memcpy( str, (char *) p_attr->attr_value.v.array, max_len - 1 );
str[max_len - 1] = '\0';
}{...}
}{...} else {
str[0] = '\0';
}{...}
}{...}
static void hidh_search_callback (UINT16 sdp_result)
{
tSDP_DISCOVERY_DB *p_db = hh_cb.p_sdp_db;
tSDP_DISC_REC *p_rec;
tSDP_DISC_ATTR *p_attr, *p_subattr1, *p_subattr2, *p_repdesc;
tBT_UUID hid_uuid;
tHID_DEV_SDP_INFO *p_nvi = &hh_cb.sdp_rec;
UINT16 attr_mask = 0;
hid_uuid.len = LEN_UUID_16;
hid_uuid.uu.uuid16 = UUID_SERVCLASS_HUMAN_INTERFACE;
hh_cb.sdp_busy = FALSE;
if (sdp_result != SDP_SUCCESS) {
hh_cb.sdp_cback(sdp_result, 0, NULL);
return;
}{...}
if ((p_rec = SDP_FindServiceUUIDInDb (p_db, &hid_uuid, NULL)) == NULL) {
hh_cb.sdp_cback(HID_SDP_NO_SERV_UUID, 0, NULL);
return;
}{...}
memset (&hh_cb.sdp_rec, 0, sizeof( tHID_DEV_SDP_INFO ));
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_DESCRIPTOR_LIST)) == NULL)
|| (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE)
|| ((p_subattr1 = p_attr->attr_value.v.p_sub_attr) == NULL)
|| (SDP_DISC_ATTR_TYPE(p_subattr1->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE)
|| ((p_subattr2 = p_subattr1->attr_value.v.p_sub_attr) == NULL)
|| ((p_repdesc = p_subattr2->p_next_attr) == NULL)
|| (SDP_DISC_ATTR_TYPE(p_repdesc->attr_len_type) != TEXT_STR_DESC_TYPE)) {
hh_cb.sdp_cback(HID_SDP_MANDATORY_MISSING, 0, NULL);
return;
}{...}
if ((p_nvi->dscp_info.dl_len = SDP_DISC_ATTR_LEN(p_repdesc->attr_len_type)) != 0) {
p_nvi->dscp_info.dsc_list = (UINT8 *) &p_repdesc->attr_value;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_VIRTUAL_CABLE)) != NULL) &&
(p_attr->attr_value.v.u8) ) {
attr_mask |= HID_VIRTUAL_CABLE;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_RECONNECT_INITIATE)) != NULL) &&
(p_attr->attr_value.v.u8) ) {
attr_mask |= HID_RECONN_INIT;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_NORMALLY_CONNECTABLE)) != NULL) &&
(p_attr->attr_value.v.u8) ) {
attr_mask |= HID_NORMALLY_CONNECTABLE;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_SDP_DISABLE)) != NULL) &&
(p_attr->attr_value.v.u8) ) {
attr_mask |= HID_SDP_DISABLE;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_BATTERY_POWER)) != NULL) &&
(p_attr->attr_value.v.u8) ) {
attr_mask |= HID_BATTERY_POWER;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_REMOTE_WAKE)) != NULL) &&
(p_attr->attr_value.v.u8) ) {
attr_mask |= HID_REMOTE_WAKE;
}{...}
hidh_get_str_attr( p_rec, ATTR_ID_SERVICE_NAME, HID_MAX_SVC_NAME_LEN, p_nvi->svc_name );
hidh_get_str_attr( p_rec, ATTR_ID_SERVICE_DESCRIPTION, HID_MAX_SVC_DESCR_LEN, p_nvi->svc_descr );
hidh_get_str_attr( p_rec, ATTR_ID_PROVIDER_NAME, HID_MAX_PROV_NAME_LEN, p_nvi->prov_name );
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_DEVICE_RELNUM)) != NULL)) {
p_nvi->rel_num = p_attr->attr_value.v.u16;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_COUNTRY_CODE)) != NULL)) {
p_nvi->ctry_code = p_attr->attr_value.v.u8;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_DEVICE_SUBCLASS)) != NULL)) {
p_nvi->sub_class = p_attr->attr_value.v.u8;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_PARSER_VERSION)) != NULL)) {
p_nvi->hpars_ver = p_attr->attr_value.v.u16;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_LINK_SUPERVISION_TO)) != NULL)) {
attr_mask |= HID_SUP_TOUT_AVLBL;
p_nvi->sup_timeout = p_attr->attr_value.v.u16;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_SSR_HOST_MAX_LAT)) != NULL)) {
attr_mask |= HID_SSR_MAX_LATENCY;
p_nvi->ssr_max_latency = p_attr->attr_value.v.u16;
}{...} else {
p_nvi->ssr_max_latency = HID_SSR_PARAM_INVALID;
}{...}
if (((p_attr = SDP_FindAttributeInRec (p_rec, ATTR_ID_HID_SSR_HOST_MIN_TOUT)) != NULL)) {
attr_mask |= HID_SSR_MIN_TOUT;
p_nvi->ssr_min_tout = p_attr->attr_value.v.u16;
}{...} else {
p_nvi->ssr_min_tout = HID_SSR_PARAM_INVALID;
}{...}
hh_cb.sdp_rec.p_sdp_layer_rec = p_rec;
hh_cb.sdp_cback(SDP_SUCCESS, attr_mask, &hh_cb.sdp_rec);
}{...}
/* ... */
tHID_STATUS HID_HostInit (void)
{
#if (HID_DYNAMIC_MEMORY)
if (!hidh_cb_ptr) {
hidh_cb_ptr = (tHID_HOST_CTB *)osi_malloc(sizeof(tHID_HOST_CTB));
if (!hidh_cb_ptr) {
return HID_ERR_NO_RESOURCES;
}{...}
}{...}
/* ... */#endif
memset(&hh_cb, 0, sizeof(tHID_HOST_CTB));
#if defined(HIDH_INITIAL_TRACE_LEVEL)
hh_cb.trace_level = HIDH_INITIAL_TRACE_LEVEL;
#else
hh_cb.trace_level = BT_TRACE_LEVEL_NONE;
#endif
return HID_SUCCESS;
}{...}
/* ... */
void HID_HostDeinit (void)
{
#if (HID_DYNAMIC_MEMORY)
if (hidh_cb_ptr) {
osi_free(hidh_cb_ptr);
hidh_cb_ptr = NULL;
}{...}
/* ... */#endif
}{...}
/* ... */
UINT8 HID_HostSetTraceLevel (UINT8 new_level)
{
if (new_level != 0xFF) {
hh_cb.trace_level = new_level;
}{...}
return (hh_cb.trace_level);
}{...}
/* ... */
tHID_STATUS HID_HostRegister (tHID_HOST_DEV_CALLBACK *dev_cback)
{
tHID_STATUS st;
if ( hh_cb.reg_flag ) {
return HID_ERR_ALREADY_REGISTERED;
}{...}
if ( dev_cback == NULL ) {
return HID_ERR_INVALID_PARAM;
}{...}
if ( (st = hidh_conn_reg()) != HID_SUCCESS ) {
return st;
}{...}
hh_cb.callback = dev_cback ;
hh_cb.reg_flag = TRUE;
return (HID_SUCCESS);
}{...}
/* ... */
tHID_STATUS HID_HostDeregister(void)
{
UINT8 i;
if ( !hh_cb.reg_flag ) {
return (HID_ERR_NOT_REGISTERED);
}{...}
for ( i = 0; i < HID_HOST_MAX_DEVICES; i++ ) {
HID_HostRemoveDev( i ) ;
}{...}
hidh_conn_dereg();
hh_cb.reg_flag = FALSE;
return (HID_SUCCESS) ;
}{...}
/* ... */
tHID_STATUS HID_HostAddDev ( BD_ADDR addr, UINT16 attr_mask, UINT8 *handle )
{
int i;
if ( !hh_cb.reg_flag ) {
return (HID_ERR_NOT_REGISTERED);
}{...}
for ( i = 0; i < HID_HOST_MAX_DEVICES; i++) {
if ((hh_cb.devices[i].in_use) &&
(!memcmp(addr, hh_cb.devices[i].addr, BD_ADDR_LEN))) {
break;
}{...}
}{...}
if (i == HID_HOST_MAX_DEVICES ) {
for ( i = 0; i < HID_HOST_MAX_DEVICES; i++) {
if ( !hh_cb.devices[i].in_use) {
break;
}{...}
}{...}
}{...}
if ( i == HID_HOST_MAX_DEVICES ) {
return HID_ERR_NO_RESOURCES;
}{...}
if (!hh_cb.devices[i].in_use) {
hh_cb.devices[i].in_use = TRUE;
hh_cb.devices[i].delay_remove = FALSE;
memcpy( hh_cb.devices[i].addr, addr, sizeof( BD_ADDR ) ) ;
hh_cb.devices[i].state = HID_DEV_NO_CONN;
hh_cb.devices[i].conn_tries = 0 ;
}{...}
if (attr_mask != HID_ATTR_MASK_IGNORE) {
hh_cb.devices[i].attr_mask = attr_mask;
}{...}
*handle = i;
return (HID_SUCCESS);
}{...}
/* ... */
tHID_STATUS HID_HostGetDev(BD_ADDR addr, UINT8 *handle)
{
int i;
if (!hh_cb.reg_flag) {
return (HID_ERR_NOT_REGISTERED);
}{...}
for (i = 0; i < HID_HOST_MAX_DEVICES; i++) {
if ((hh_cb.devices[i].in_use) && (!memcmp(addr, hh_cb.devices[i].addr, BD_ADDR_LEN))) {
break;
}{...}
}{...}
if (i == HID_HOST_MAX_DEVICES) {
*handle = 0xff;
}{...} else {
*handle = i;
}{...}
return (HID_SUCCESS);
}{...}
/* ... */
tHID_STATUS HID_HostRemoveDev ( UINT8 dev_handle )
{
if ( !hh_cb.reg_flag ) {
return (HID_ERR_NOT_REGISTERED);
}{...}
if ( (dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use) ) {
return HID_ERR_INVALID_PARAM;
}{...}
HID_HostCloseDev( dev_handle ) ;
if (hh_cb.devices[dev_handle].conn.conn_state == HID_CONN_STATE_DISCONNECTING_INTR ||
hh_cb.devices[dev_handle].conn.conn_state == HID_CONN_STATE_DISCONNECTING_CTRL) {
hh_cb.devices[dev_handle].delay_remove = TRUE;
}{...} else {
HIDH_TRACE_WARNING("%s dev_handle:%d conn_state:%d", __func__, dev_handle,
hh_cb.devices[dev_handle].conn.conn_state);
hh_cb.devices[dev_handle].in_use = FALSE;
hh_cb.devices[dev_handle].conn.conn_state = HID_CONN_STATE_UNUSED;
hh_cb.devices[dev_handle].conn.ctrl_cid = hh_cb.devices[dev_handle].conn.intr_cid = 0;
hh_cb.devices[dev_handle].attr_mask = 0;
}{...}
return HID_SUCCESS;
}{...}
/* ... */
tHID_STATUS HID_HostOpenDev ( UINT8 dev_handle )
{
if ( !hh_cb.reg_flag ) {
return (HID_ERR_NOT_REGISTERED);
}{...}
if ( (dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use) ) {
return HID_ERR_INVALID_PARAM;
}{...}
if ( hh_cb.devices[dev_handle].state != HID_DEV_NO_CONN ) {
return HID_ERR_ALREADY_CONN;
}{...}
hh_cb.devices[dev_handle].conn_tries = 1;
return hidh_conn_initiate( dev_handle );
}{...}
/* ... */
tHID_STATUS HID_HostWriteDev( UINT8 dev_handle, UINT8 t_type,
UINT8 param, UINT16 data, UINT8 report_id, BT_HDR *pbuf )
{
tHID_STATUS status = HID_SUCCESS;
if ( !hh_cb.reg_flag ) {
HIDH_TRACE_ERROR("HID_ERR_NOT_REGISTERED");
status = HID_ERR_NOT_REGISTERED;
}{...}
if ( (dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use) ) {
HIDH_TRACE_ERROR("HID_ERR_INVALID_PARAM");
status = HID_ERR_INVALID_PARAM;
}{...}
else if ( hh_cb.devices[dev_handle].state != HID_DEV_CONNECTED ) {
HIDH_TRACE_ERROR("HID_ERR_NO_CONNECTION dev_handle %d", dev_handle);
status = HID_ERR_NO_CONNECTION;
}{...}
if (status != HID_SUCCESS) {
if (pbuf) {
osi_free ((void *)pbuf);
}{...}
}{...} else {
status = hidh_conn_snd_data( dev_handle, t_type, param, data, report_id, pbuf ) ;
}{...}
return status;
}{...}
/* ... */
tHID_STATUS HID_HostCloseDev( UINT8 dev_handle )
{
if ( !hh_cb.reg_flag ) {
return (HID_ERR_NOT_REGISTERED);
}{...}
if ( (dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use) ) {
return HID_ERR_INVALID_PARAM;
}{...}
hh_cb.devices[dev_handle].conn_tries = HID_HOST_MAX_CONN_RETRY + 1;
btu_stop_timer( &(hh_cb.devices[dev_handle].conn.timer_entry) ) ;
if ( hh_cb.devices[dev_handle].state != HID_DEV_CONNECTED ) {
return HID_ERR_NO_CONNECTION;
}{...}
hh_cb.devices[dev_handle].conn_tries = HID_HOST_MAX_CONN_RETRY + 1;
return hidh_conn_disconnect( dev_handle );
}{...}
tHID_STATUS HID_HostSetSecurityLevel( char serv_name[], UINT8 sec_lvl )
{
if (!BTM_SetSecurityLevel (FALSE, serv_name, BTM_SEC_SERVICE_HIDH_SEC_CTRL,
sec_lvl, HID_PSM_CONTROL, BTM_SEC_PROTO_HID, HID_SEC_CHN)) {
HIDH_TRACE_ERROR ("Security Registration 1 failed");
return (HID_ERR_NO_RESOURCES);
}{...}
if (!BTM_SetSecurityLevel (TRUE, serv_name, BTM_SEC_SERVICE_HIDH_SEC_CTRL,
sec_lvl, HID_PSM_CONTROL, BTM_SEC_PROTO_HID, HID_SEC_CHN)) {
HIDH_TRACE_ERROR ("Security Registration 2 failed");
return (HID_ERR_NO_RESOURCES);
}{...}
if (!BTM_SetSecurityLevel (FALSE, serv_name, BTM_SEC_SERVICE_HIDH_NOSEC_CTRL,
BTM_SEC_NONE, HID_PSM_CONTROL, BTM_SEC_PROTO_HID, HID_NOSEC_CHN)) {
HIDH_TRACE_ERROR ("Security Registration 3 failed");
return (HID_ERR_NO_RESOURCES);
}{...}
if (!BTM_SetSecurityLevel (TRUE, serv_name, BTM_SEC_SERVICE_HIDH_NOSEC_CTRL,
BTM_SEC_NONE, HID_PSM_CONTROL, BTM_SEC_PROTO_HID, HID_NOSEC_CHN)) {
HIDH_TRACE_ERROR ("Security Registration 4 failed");
return (HID_ERR_NO_RESOURCES);
}{...}
if (!BTM_SetSecurityLevel (TRUE, serv_name, BTM_SEC_SERVICE_HIDH_INTR,
BTM_SEC_NONE, HID_PSM_INTERRUPT, BTM_SEC_PROTO_HID, 0)) {
HIDH_TRACE_ERROR ("Security Registration 5 failed");
return (HID_ERR_NO_RESOURCES);
}{...}
if (!BTM_SetSecurityLevel (FALSE, serv_name, BTM_SEC_SERVICE_HIDH_INTR,
BTM_SEC_NONE, HID_PSM_INTERRUPT, BTM_SEC_PROTO_HID, 0)) {
HIDH_TRACE_ERROR ("Security Registration 6 failed");
return (HID_ERR_NO_RESOURCES);
}{...}
return ( HID_SUCCESS );
}{...}
/* ... */
BOOLEAN hid_known_hid_device (BD_ADDR bd_addr)
{
UINT8 i;
tBTM_INQ_INFO *p_inq_info = BTM_InqDbRead(bd_addr);
if ( !hh_cb.reg_flag ) {
return FALSE;
}{...}
if (p_inq_info != NULL) {
if ((p_inq_info->results.dev_class[1] & BTM_COD_MAJOR_CLASS_MASK)
== BTM_COD_MAJOR_PERIPHERAL ) {
HIDH_TRACE_DEBUG("hid_known_hid_device:dev found in InqDB & COD matches HID dev");
return TRUE;
}{...}
}{...} else {
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
if ((p_dev_rec != NULL) &&
((p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK) == BTM_COD_MAJOR_PERIPHERAL )) {
HIDH_TRACE_DEBUG("hid_known_hid_device:dev found in SecDevDB & COD matches HID dev");
return TRUE;
}{...}
}{...}
for ( i = 0; i < HID_HOST_MAX_DEVICES; i++) {
if ((hh_cb.devices[i].in_use) &&
(memcmp(bd_addr, hh_cb.devices[i].addr, BD_ADDR_LEN) == 0)) {
return TRUE;
}{...}
}{...}
HIDH_TRACE_DEBUG("hid_known_hid_device:remote is not HID device");
return FALSE;
}{...}
BOOLEAN HID_HostConnectOrig(UINT8 dev_handle)
{
BOOLEAN ret = FALSE;
do {
if (!hh_cb.reg_flag) {
break;
}{...}
if ((dev_handle >= HID_HOST_MAX_DEVICES) || (!hh_cb.devices[dev_handle].in_use)) {
break;
}{...}
ret = hidh_conn_is_orig(dev_handle);
}{...} while (0);
return ret;
}{...}
/* ... */
#endif