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