1 /******************************************************************************
2  *
3  *  Copyright 2009-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 #ifndef BTIF_HH_H
20 #define BTIF_HH_H
21 
22 #include <base/strings/stringprintf.h>
23 #include <bluetooth/log.h>
24 #include <hardware/bluetooth.h>
25 #include <hardware/bt_hh.h>
26 #include <pthread.h>
27 #include <stdint.h>
28 
29 #include "bta/include/bta_hh_api.h"
30 #include "macros.h"
31 #include "osi/include/alarm.h"
32 #include "osi/include/fixed_queue.h"
33 #include "types/ble_address_with_type.h"
34 #include "types/raw_address.h"
35 
36 /*******************************************************************************
37  *  Constants & Macros
38  ******************************************************************************/
39 
40 #define BTIF_HH_MAX_HID 8
41 #define BTIF_HH_MAX_ADDED_DEV 32
42 
43 #define BTIF_HH_MAX_KEYSTATES 3
44 #define BTIF_HH_KEYSTATE_MASK_NUMLOCK 0x01
45 #define BTIF_HH_KEYSTATE_MASK_CAPSLOCK 0x02
46 #define BTIF_HH_KEYSTATE_MASK_SCROLLLOCK 0x04
47 
48 #define BTIF_HH_MAX_POLLING_ATTEMPTS 10
49 #define BTIF_HH_POLLING_SLEEP_DURATION_US 5000
50 
51 #ifndef ENABLE_UHID_SET_REPORT
52 #if defined(__ANDROID__) || defined(TARGET_FLOSS)
53 #define ENABLE_UHID_SET_REPORT 1
54 #else
55 #define ENABLE_UHID_SET_REPORT 0
56 #endif
57 #endif
58 
59 /*******************************************************************************
60  *  Type definitions and return values
61  ******************************************************************************/
62 
63 typedef enum : unsigned {
64   BTIF_HH_DISABLED = 0,
65   BTIF_HH_ENABLED,
66   BTIF_HH_DISABLING,
67   BTIF_HH_DEV_UNKNOWN,
68   BTIF_HH_DEV_CONNECTING,
69   BTIF_HH_DEV_CONNECTED,
70   BTIF_HH_DEV_DISCONNECTED
71 } BTIF_HH_STATUS;
72 
btif_hh_status_text(const BTIF_HH_STATUS & status)73 inline std::string btif_hh_status_text(const BTIF_HH_STATUS& status) {
74   switch (status) {
75     CASE_RETURN_TEXT(BTIF_HH_DISABLED);
76     CASE_RETURN_TEXT(BTIF_HH_ENABLED);
77     CASE_RETURN_TEXT(BTIF_HH_DISABLING);
78     CASE_RETURN_TEXT(BTIF_HH_DEV_UNKNOWN);
79     CASE_RETURN_TEXT(BTIF_HH_DEV_CONNECTING);
80     CASE_RETURN_TEXT(BTIF_HH_DEV_CONNECTED);
81     CASE_RETURN_TEXT(BTIF_HH_DEV_DISCONNECTED);
82     default:
83       return base::StringPrintf("UNKNOWN[%u]", status);
84   }
85 }
86 
87 /* Supposedly is exclusive to uhid thread, but now is still accessed by btif. */
88 /* TODO: remove btif_hh_uhid_t from btif_hh_device_t. */
89 typedef struct {
90   int fd;
91   uint8_t dev_handle;
92   tAclLinkSpec link_spec;
93   uint8_t hh_keep_polling;
94   bool ready_for_data;
95   fixed_queue_t* get_rpt_id_queue;
96 #if ENABLE_UHID_SET_REPORT
97   fixed_queue_t* set_rpt_id_queue;
98 #endif  // ENABLE_UHID_SET_REPORT
99 } btif_hh_uhid_t;
100 
101 /* Control block to maintain properties of devices */
102 typedef struct {
103   bthh_connection_state_t dev_status;
104   uint8_t dev_handle;
105   tAclLinkSpec link_spec;
106   tBTA_HH_ATTR_MASK attr_mask;
107   uint8_t sub_class;
108   uint8_t app_id;
109   pthread_t hh_poll_thread_id;
110   alarm_t* vup_timer;
111   bool local_vup;  // Indicated locally initiated VUP
112   btif_hh_uhid_t uhid;
113 } btif_hh_device_t;
114 
115 /* Control block to maintain properties of devices */
116 typedef struct {
117   uint8_t dev_handle;
118   tAclLinkSpec link_spec;
119   tBTA_HH_ATTR_MASK attr_mask;
120   bool reconnect_allowed;  // Connection policy
121 } btif_hh_added_device_t;
122 
123 /**
124  * BTIF-HH control block to maintain added devices and currently
125  * connected hid devices
126  */
127 typedef struct {
128   BTIF_HH_STATUS status;
129   btif_hh_device_t devices[BTIF_HH_MAX_HID];
130   uint32_t device_num;
131   btif_hh_added_device_t added_devices[BTIF_HH_MAX_ADDED_DEV];
132   bool service_dereg_active;
133   tAclLinkSpec pending_link_spec;
134 } btif_hh_cb_t;
135 
136 /*******************************************************************************
137  *  Functions
138  ******************************************************************************/
139 
140 extern btif_hh_cb_t btif_hh_cb;
141 
142 btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle);
143 btif_hh_device_t* btif_hh_find_dev_by_handle(uint8_t handle);
144 btif_hh_device_t* btif_hh_find_empty_dev(void);
145 bt_status_t btif_hh_connect(const tAclLinkSpec& link_spec);
146 bt_status_t btif_hh_virtual_unplug(const tAclLinkSpec& link_spec);
147 void btif_hh_remove_device(const tAclLinkSpec& link_spec);
148 void btif_hh_setreport(btif_hh_uhid_t* p_uhid, bthh_report_type_t r_type,
149                        uint16_t size, uint8_t* report);
150 void btif_hh_senddata(btif_hh_uhid_t* p_uhid, uint16_t size, uint8_t* report);
151 void btif_hh_getreport(btif_hh_uhid_t* p_uhid, bthh_report_type_t r_type,
152                        uint8_t reportId, uint16_t bufferSize);
153 void btif_hh_service_registration(bool enable);
154 
155 void btif_hh_load_bonded_dev(const tAclLinkSpec& link_spec,
156                              tBTA_HH_ATTR_MASK attr_mask, uint8_t sub_class,
157                              uint8_t app_id, tBTA_HH_DEV_DSCP_INFO dscp_info,
158                              bool reconnect_allowed);
159 
160 void DumpsysHid(int fd);
161 
162 namespace fmt {
163 template <>
164 struct formatter<BTIF_HH_STATUS> : enum_formatter<BTIF_HH_STATUS> {};
165 }  // namespace fmt
166 
167 #endif
168