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