• 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 <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 "btu.h"
33 #include "hiddefs.h"
34 #include "hidh_api.h"
35 #include "hidh_int.h"
36 #include "stack/btm/btm_dev.h"
37 #include "stack/btm/btm_sec.h"
38 
39 using bluetooth::Uuid;
40 
41 tHID_HOST_CTB hh_cb;
42 
43 static void hidh_search_callback(tSDP_RESULT sdp_result);
44 
45 /*******************************************************************************
46  *
47  * Function         HID_HostGetSDPRecord
48  *
49  * Description      This function reads the device SDP record
50  *
51  * Returns          tHID_STATUS
52  *
53  ******************************************************************************/
HID_HostGetSDPRecord(const RawAddress & addr,tSDP_DISCOVERY_DB * p_db,uint32_t db_len,tHID_HOST_SDP_CALLBACK * sdp_cback)54 tHID_STATUS HID_HostGetSDPRecord(const RawAddress& addr,
55                                  tSDP_DISCOVERY_DB* p_db, uint32_t db_len,
56                                  tHID_HOST_SDP_CALLBACK* sdp_cback) {
57 
58   if (hh_cb.sdp_busy) return HID_ERR_SDP_BUSY;
59 
60   hh_cb.p_sdp_db = p_db;
61   Uuid uuid_list = Uuid::From16Bit(UUID_SERVCLASS_HUMAN_INTERFACE);
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 
hidh_get_str_attr(tSDP_DISC_REC * p_rec,uint16_t attr_id,uint16_t max_len,char * str)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 
hidh_search_callback(tSDP_RESULT sdp_result)91 static void hidh_search_callback(tSDP_RESULT 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   tHID_DEV_SDP_INFO* p_nvi = &hh_cb.sdp_rec;
96   uint16_t attr_mask = 0;
97 
98 
99   hh_cb.sdp_busy = false;
100 
101   if (sdp_result != SDP_SUCCESS) {
102     hh_cb.sdp_cback(sdp_result, 0, NULL);
103     return;
104   }
105 
106   Uuid hid_uuid = Uuid::From16Bit(UUID_SERVCLASS_HUMAN_INTERFACE);
107   p_rec = SDP_FindServiceUUIDInDb(p_db, hid_uuid, NULL);
108   if (p_rec == NULL) {
109     hh_cb.sdp_cback(HID_SDP_NO_SERV_UUID, 0, NULL);
110     return;
111   }
112 
113   memset(&hh_cb.sdp_rec, 0, sizeof(tHID_DEV_SDP_INFO));
114 
115   /* First, verify the mandatory fields we care about */
116   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_DESCRIPTOR_LIST)) ==
117        NULL) ||
118       (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE) ||
119       ((p_subattr1 = p_attr->attr_value.v.p_sub_attr) == NULL) ||
120       (SDP_DISC_ATTR_TYPE(p_subattr1->attr_len_type) !=
121        DATA_ELE_SEQ_DESC_TYPE) ||
122       ((p_subattr2 = p_subattr1->attr_value.v.p_sub_attr) == NULL) ||
123       ((p_repdesc = p_subattr2->p_next_attr) == NULL) ||
124       (SDP_DISC_ATTR_TYPE(p_repdesc->attr_len_type) != TEXT_STR_DESC_TYPE)) {
125     hh_cb.sdp_cback(HID_SDP_MANDATORY_MISSING, 0, NULL);
126     return;
127   }
128 
129   p_nvi->dscp_info.dl_len = SDP_DISC_ATTR_LEN(p_repdesc->attr_len_type);
130   if (p_nvi->dscp_info.dl_len != 0)
131     p_nvi->dscp_info.dsc_list = (uint8_t*)&p_repdesc->attr_value;
132 
133   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_VIRTUAL_CABLE)) !=
134        NULL) &&
135       (p_attr->attr_value.v.u8)) {
136     attr_mask |= HID_VIRTUAL_CABLE;
137   }
138 
139   if (((p_attr = SDP_FindAttributeInRec(
140             p_rec, ATTR_ID_HID_RECONNECT_INITIATE)) != NULL) &&
141       (p_attr->attr_value.v.u8)) {
142     attr_mask |= HID_RECONN_INIT;
143   }
144 
145   if (((p_attr = SDP_FindAttributeInRec(
146             p_rec, ATTR_ID_HID_NORMALLY_CONNECTABLE)) != NULL) &&
147       (p_attr->attr_value.v.u8)) {
148     attr_mask |= HID_NORMALLY_CONNECTABLE;
149   }
150 
151   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_SDP_DISABLE)) !=
152        NULL) &&
153       (p_attr->attr_value.v.u8)) {
154     attr_mask |= HID_SDP_DISABLE;
155   }
156 
157   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_BATTERY_POWER)) !=
158        NULL) &&
159       (p_attr->attr_value.v.u8)) {
160     attr_mask |= HID_BATTERY_POWER;
161   }
162 
163   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_REMOTE_WAKE)) !=
164        NULL) &&
165       (p_attr->attr_value.v.u8)) {
166     attr_mask |= HID_REMOTE_WAKE;
167   }
168 
169   hidh_get_str_attr(p_rec, ATTR_ID_SERVICE_NAME, HID_MAX_SVC_NAME_LEN,
170                     p_nvi->svc_name);
171   hidh_get_str_attr(p_rec, ATTR_ID_SERVICE_DESCRIPTION, HID_MAX_SVC_DESCR_LEN,
172                     p_nvi->svc_descr);
173   hidh_get_str_attr(p_rec, ATTR_ID_PROVIDER_NAME, HID_MAX_PROV_NAME_LEN,
174                     p_nvi->prov_name);
175 
176   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_DEVICE_RELNUM)) !=
177        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)) !=
182        NULL)) {
183     p_nvi->ctry_code = p_attr->attr_value.v.u8;
184   }
185 
186   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_DEVICE_SUBCLASS)) !=
187        NULL)) {
188     p_nvi->sub_class = p_attr->attr_value.v.u8;
189   }
190 
191   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_PARSER_VERSION)) !=
192        NULL)) {
193     p_nvi->hpars_ver = p_attr->attr_value.v.u16;
194   }
195 
196   if (((p_attr = SDP_FindAttributeInRec(
197             p_rec, ATTR_ID_HID_LINK_SUPERVISION_TO)) != NULL)) {
198     attr_mask |= HID_SUP_TOUT_AVLBL;
199     p_nvi->sup_timeout = p_attr->attr_value.v.u16;
200   }
201 
202   if (((p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_HID_SSR_HOST_MAX_LAT)) !=
203        NULL)) {
204     attr_mask |= HID_SSR_MAX_LATENCY;
205     p_nvi->ssr_max_latency = p_attr->attr_value.v.u16;
206   } else
207     p_nvi->ssr_max_latency = HID_SSR_PARAM_INVALID;
208 
209   if (((p_attr = SDP_FindAttributeInRec(
210             p_rec, ATTR_ID_HID_SSR_HOST_MIN_TOUT)) != NULL)) {
211     attr_mask |= HID_SSR_MIN_TOUT;
212     p_nvi->ssr_min_tout = p_attr->attr_value.v.u16;
213   } else
214     p_nvi->ssr_min_tout = HID_SSR_PARAM_INVALID;
215 
216   hh_cb.sdp_rec.p_sdp_layer_rec = p_rec;
217   hh_cb.sdp_cback(SDP_SUCCESS, attr_mask, &hh_cb.sdp_rec);
218 }
219 
220 /*******************************************************************************
221  *
222  * Function         HID_HostInit
223  *
224  * Description      This function initializes the control block and trace
225  *                  variable
226  *
227  * Returns          void
228  *
229  ******************************************************************************/
HID_HostInit(void)230 void HID_HostInit(void) {
231   uint8_t log_level = hh_cb.trace_level;
232   memset(&hh_cb, 0, sizeof(tHID_HOST_CTB));
233   hh_cb.trace_level = log_level;
234 }
235 
236 /*******************************************************************************
237  *
238  * Function         HID_HostSetTraceLevel
239  *
240  * Description      This function sets the trace level for HID Host. If called
241  *                  with 0xFF, it simply reads the current trace level.
242  *
243  * Returns          the new (current) trace level
244  *
245  ******************************************************************************/
HID_HostSetTraceLevel(uint8_t new_level)246 uint8_t HID_HostSetTraceLevel(uint8_t new_level) {
247   if (new_level != 0xFF) hh_cb.trace_level = new_level;
248 
249   return (hh_cb.trace_level);
250 }
251 
252 /*******************************************************************************
253  *
254  * Function         HID_HostRegister
255  *
256  * Description      This function registers HID-Host with lower layers
257  *
258  * Returns          tHID_STATUS
259  *
260  ******************************************************************************/
HID_HostRegister(tHID_HOST_DEV_CALLBACK * dev_cback)261 tHID_STATUS HID_HostRegister(tHID_HOST_DEV_CALLBACK* dev_cback) {
262   tHID_STATUS st;
263 
264   if (hh_cb.reg_flag) return HID_ERR_ALREADY_REGISTERED;
265 
266   if (dev_cback == NULL) return HID_ERR_INVALID_PARAM;
267 
268   /* Register with L2CAP */
269   st = hidh_conn_reg();
270   if (st != HID_SUCCESS) {
271     return st;
272   }
273 
274   hh_cb.callback = dev_cback;
275   hh_cb.reg_flag = true;
276 
277   for (size_t i = 0; i < HID_HOST_MAX_DEVICES; i++) {
278     hh_cb.devices[i].conn.process_repage_timer =
279         alarm_new("hid_devices_conn.process_repage_timer");
280   }
281   return (HID_SUCCESS);
282 }
283 
284 /*******************************************************************************
285  *
286  * Function         HID_HostDeregister
287  *
288  * Description      This function is called when the host is about power down.
289  *
290  * Returns          tHID_STATUS
291  *
292  ******************************************************************************/
HID_HostDeregister(void)293 tHID_STATUS HID_HostDeregister(void) {
294   uint8_t i;
295 
296   if (!hh_cb.reg_flag) return (HID_ERR_NOT_REGISTERED);
297 
298   for (i = 0; i < HID_HOST_MAX_DEVICES; i++) {
299     HID_HostRemoveDev(i);
300     alarm_free(hh_cb.devices[i].conn.process_repage_timer);
301   }
302 
303   hidh_conn_dereg();
304   hh_cb.reg_flag = false;
305 
306   return (HID_SUCCESS);
307 }
308 
309 /*******************************************************************************
310  *
311  * Function         HID_HostAddDev
312  *
313  * Description      This is called so HID-host may manage this device.
314  *
315  * Returns          tHID_STATUS
316  *
317  ******************************************************************************/
HID_HostAddDev(const RawAddress & addr,uint16_t attr_mask,uint8_t * handle)318 tHID_STATUS HID_HostAddDev(const RawAddress& addr, uint16_t attr_mask,
319                            uint8_t* handle) {
320   int i;
321   /* Find an entry for this device in hh_cb.devices array */
322   if (!hh_cb.reg_flag) return (HID_ERR_NOT_REGISTERED);
323 
324   for (i = 0; i < HID_HOST_MAX_DEVICES; i++) {
325     if ((hh_cb.devices[i].in_use) && addr == hh_cb.devices[i].addr) break;
326   }
327 
328   if (i == HID_HOST_MAX_DEVICES) {
329     for (i = 0; i < HID_HOST_MAX_DEVICES; i++) {
330       if (!hh_cb.devices[i].in_use) break;
331     }
332   }
333 
334   if (i == HID_HOST_MAX_DEVICES) return HID_ERR_NO_RESOURCES;
335 
336   if (!hh_cb.devices[i].in_use) {
337     hh_cb.devices[i].in_use = true;
338     hh_cb.devices[i].addr = addr;
339     hh_cb.devices[i].state = HID_DEV_NO_CONN;
340     hh_cb.devices[i].conn_tries = 0;
341   }
342 
343   if (attr_mask != HID_ATTR_MASK_IGNORE) hh_cb.devices[i].attr_mask = attr_mask;
344 
345   *handle = i;
346 
347   return (HID_SUCCESS);
348 }
349 
350 /*******************************************************************************
351  *
352  * Function         HID_HostRemoveDev
353  *
354  * Description      This removes the device from the list of devices that the
355  *                  host has to manage.
356  *
357  * Returns          tHID_STATUS
358  *
359  ******************************************************************************/
HID_HostRemoveDev(uint8_t dev_handle)360 tHID_STATUS HID_HostRemoveDev(uint8_t dev_handle) {
361   if (!hh_cb.reg_flag) return (HID_ERR_NOT_REGISTERED);
362 
363   if ((dev_handle >= HID_HOST_MAX_DEVICES) ||
364       (!hh_cb.devices[dev_handle].in_use))
365     return HID_ERR_INVALID_PARAM;
366 
367   HID_HostCloseDev(dev_handle);
368   hh_cb.devices[dev_handle].in_use = false;
369   hh_cb.devices[dev_handle].conn.conn_state = HID_CONN_STATE_UNUSED;
370   hh_cb.devices[dev_handle].conn.ctrl_cid =
371       hh_cb.devices[dev_handle].conn.intr_cid = 0;
372   hh_cb.devices[dev_handle].attr_mask = 0;
373   return HID_SUCCESS;
374 }
375 
376 /*******************************************************************************
377  *
378  * Function         HID_HostOpenDev
379  *
380  * Description      This function is called when the user wants to initiate a
381  *                  connection attempt to a device.
382  *
383  * Returns          void
384  *
385  ******************************************************************************/
HID_HostOpenDev(uint8_t dev_handle)386 tHID_STATUS HID_HostOpenDev(uint8_t dev_handle) {
387   if (!hh_cb.reg_flag) return (HID_ERR_NOT_REGISTERED);
388 
389   if ((dev_handle >= HID_HOST_MAX_DEVICES) ||
390       (!hh_cb.devices[dev_handle].in_use))
391     return HID_ERR_INVALID_PARAM;
392 
393   if (hh_cb.devices[dev_handle].state != HID_DEV_NO_CONN)
394     return HID_ERR_ALREADY_CONN;
395 
396   hh_cb.devices[dev_handle].conn_tries = 1;
397   return hidh_conn_initiate(dev_handle);
398 }
399 
400 /*******************************************************************************
401  *
402  * Function         HID_HostWriteDev
403  *
404  * Description      This function is called when the host has a report to send.
405  *
406  *                  report_id: is only used on GET_REPORT transaction if is
407  *                              specified. only valid when it is non-zero.
408  *
409  * Returns          void
410  *
411  ******************************************************************************/
HID_HostWriteDev(uint8_t dev_handle,uint8_t t_type,uint8_t param,uint16_t data,uint8_t report_id,BT_HDR * pbuf)412 tHID_STATUS HID_HostWriteDev(uint8_t dev_handle, uint8_t t_type, uint8_t param,
413                              uint16_t data, uint8_t report_id, BT_HDR* pbuf) {
414   tHID_STATUS status = HID_SUCCESS;
415 
416   if (!hh_cb.reg_flag) {
417     HIDH_TRACE_ERROR("HID_ERR_NOT_REGISTERED");
418     status = HID_ERR_NOT_REGISTERED;
419   }
420 
421   if ((dev_handle >= HID_HOST_MAX_DEVICES) ||
422       (!hh_cb.devices[dev_handle].in_use)) {
423     HIDH_TRACE_ERROR("HID_ERR_INVALID_PARAM");
424     status = HID_ERR_INVALID_PARAM;
425   }
426 
427   else if (hh_cb.devices[dev_handle].state != HID_DEV_CONNECTED) {
428     HIDH_TRACE_ERROR("HID_ERR_NO_CONNECTION dev_handle %d", dev_handle);
429     status = HID_ERR_NO_CONNECTION;
430   }
431 
432   if (status != HID_SUCCESS)
433     osi_free(pbuf);
434   else
435     status =
436         hidh_conn_snd_data(dev_handle, t_type, param, data, report_id, pbuf);
437 
438   return status;
439 }
440 
441 /*******************************************************************************
442  *
443  * Function         HID_HostCloseDev
444  *
445  * Description      This function disconnects the device.
446  *
447  * Returns          void
448  *
449  ******************************************************************************/
HID_HostCloseDev(uint8_t dev_handle)450 tHID_STATUS HID_HostCloseDev(uint8_t dev_handle) {
451   if (!hh_cb.reg_flag) return (HID_ERR_NOT_REGISTERED);
452 
453   if ((dev_handle >= HID_HOST_MAX_DEVICES) ||
454       (!hh_cb.devices[dev_handle].in_use))
455     return HID_ERR_INVALID_PARAM;
456 
457   if (hh_cb.devices[dev_handle].state != HID_DEV_CONNECTED)
458     return HID_ERR_NO_CONNECTION;
459 
460   alarm_cancel(hh_cb.devices[dev_handle].conn.process_repage_timer);
461   hh_cb.devices[dev_handle].conn_tries = HID_HOST_MAX_CONN_RETRY + 1;
462   return hidh_conn_disconnect(dev_handle);
463 }
464