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 <list>
30 
31 #include "bta/include/bta_hh_api.h"
32 #include "macros.h"
33 #include "osi/include/alarm.h"
34 #include "osi/include/fixed_queue.h"
35 #include "types/ble_address_with_type.h"
36 #include "types/raw_address.h"
37 
38 /*******************************************************************************
39  *  Constants & Macros
40  ******************************************************************************/
41 
42 #define BTIF_HH_MAX_HID 8
43 #define BTIF_HH_MAX_ADDED_DEV 32
44 
45 #define BTIF_HH_MAX_KEYSTATES 3
46 #define BTIF_HH_KEYSTATE_MASK_NUMLOCK 0x01
47 #define BTIF_HH_KEYSTATE_MASK_CAPSLOCK 0x02
48 #define BTIF_HH_KEYSTATE_MASK_SCROLLLOCK 0x04
49 
50 #define BTIF_HH_MAX_POLLING_ATTEMPTS 10
51 #define BTIF_HH_POLLING_SLEEP_DURATION_US 5000
52 
53 #ifndef ENABLE_UHID_SET_REPORT
54 #if defined(__ANDROID__) || defined(TARGET_FLOSS)
55 #define ENABLE_UHID_SET_REPORT 1
56 #else
57 #define ENABLE_UHID_SET_REPORT 0
58 #endif
59 #endif
60 
61 /*******************************************************************************
62  *  Type definitions and return values
63  ******************************************************************************/
64 
65 typedef enum : unsigned {
66   BTIF_HH_DISABLED = 0,
67   BTIF_HH_ENABLED,
68   BTIF_HH_DISABLING,
69 } BTIF_HH_STATUS;
70 
btif_hh_status_text(const BTIF_HH_STATUS & status)71 inline std::string btif_hh_status_text(const BTIF_HH_STATUS& status) {
72   switch (status) {
73     CASE_RETURN_TEXT(BTIF_HH_DISABLED);
74     CASE_RETURN_TEXT(BTIF_HH_ENABLED);
75     CASE_RETURN_TEXT(BTIF_HH_DISABLING);
76     default:
77       return base::StringPrintf("UNKNOWN[%u]", status);
78   }
79 }
80 
81 /* Uhid thread has exclusive access to this block. */
82 typedef struct {
83   int fd;                // for interfacing with uhid
84   int internal_recv_fd;  // for receiving internal events in uhid thread
85   int internal_send_fd;  // for passing to other threads so they can send
86                          // internal events
87   uint8_t dev_handle;
88   tAclLinkSpec link_spec;
89   uint8_t hh_keep_polling;  // Deprecated with the aflags hid_report_queuing.
90                             // TODO: remove after launching the aflag.
91   bool ready_for_data;
92   fixed_queue_t* get_rpt_id_queue;
93 #if ENABLE_UHID_SET_REPORT
94   fixed_queue_t* set_rpt_id_queue;
95 #endif  // ENABLE_UHID_SET_REPORT
96   fixed_queue_t* input_queue;  // to store the inputs before uhid is ready.
97   alarm_t* delayed_ready_timer;  // to delay marking a device as ready, give input chance to listen.
98   alarm_t* ready_disconn_timer;  // to disconnect device if still not ready after some time.
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   int internal_send_fd;  // for sending internal events from btif
110   pthread_t hh_poll_thread_id;
111   alarm_t* vup_timer;
112   bool local_vup;  // Indicated locally initiated VUP
113   btif_hh_uhid_t uhid;  // Deprecated with the aflags hid_report_queuing.
114                         // TODO: remove after launching the aflag.
115 } btif_hh_device_t;
116 
117 /* Control block to maintain properties of devices */
118 typedef struct {
119   uint8_t dev_handle;
120   tAclLinkSpec link_spec;
121   tBTA_HH_ATTR_MASK attr_mask;
122   bool reconnect_allowed;  // Connection policy
123 } btif_hh_added_device_t;
124 
125 /**
126  * BTIF-HH control block to maintain added devices and currently
127  * connected hid devices
128  */
129 typedef struct {
130   BTIF_HH_STATUS status;
131   btif_hh_device_t devices[BTIF_HH_MAX_HID];
132   uint32_t device_num;
133   btif_hh_added_device_t added_devices[BTIF_HH_MAX_ADDED_DEV];
134   bool service_dereg_active;
135 
136   std::list<tAclLinkSpec> pending_connections;
137 } btif_hh_cb_t;
138 
139 /*******************************************************************************
140  *  Functions
141  ******************************************************************************/
142 
143 extern btif_hh_cb_t btif_hh_cb;
144 
145 const bthh_interface_t* btif_hh_get_interface();
146 bt_status_t btif_hh_execute_service(bool b_enable);
147 btif_hh_device_t* btif_hh_find_connected_dev_by_handle(uint8_t handle);
148 btif_hh_device_t* btif_hh_find_dev_by_handle(uint8_t handle);
149 btif_hh_device_t* btif_hh_find_empty_dev(void);
150 bt_status_t btif_hh_virtual_unplug(const tAclLinkSpec& link_spec);
151 bt_status_t btif_hh_connect(const tAclLinkSpec& link_spec);
152 void btif_hh_remove_device(const tAclLinkSpec& link_spec);
153 void btif_hh_setreport(btif_hh_uhid_t* p_uhid, bthh_report_type_t r_type, uint16_t size,
154                        uint8_t* report);
155 void btif_hh_senddata(btif_hh_uhid_t* p_uhid, uint16_t size, uint8_t* report);
156 void btif_hh_getreport(btif_hh_uhid_t* p_uhid, bthh_report_type_t r_type, uint8_t reportId,
157                        uint16_t bufferSize);
158 void btif_hh_service_registration(bool enable);
159 
160 void btif_hh_load_bonded_dev(const tAclLinkSpec& link_spec, tBTA_HH_ATTR_MASK attr_mask,
161                              uint8_t sub_class, uint8_t app_id, tBTA_HH_DEV_DSCP_INFO dscp_info,
162                              bool reconnect_allowed);
163 
164 int bta_hh_co_write(int fd, uint8_t* rpt, uint16_t len);
165 void bta_hh_co_close(btif_hh_device_t* p_dev);
166 void bta_hh_co_send_hid_info(btif_hh_device_t* p_dev, const char* dev_name, uint16_t vendor_id,
167                              uint16_t product_id, uint16_t version, uint8_t ctry_code,
168                              uint16_t dscp_len, uint8_t* p_dscp);
169 
170 void DumpsysHid(int fd);
171 
172 namespace bluetooth::legacy::testing {
173 void bte_hh_evt(tBTA_HH_EVT event, tBTA_HH* p_data);
174 }  // namespace bluetooth::legacy::testing
175 
176 namespace std {
177 template <>
178 struct formatter<BTIF_HH_STATUS> : enum_formatter<BTIF_HH_STATUS> {};
179 }  // namespace std
180 
181 #endif
182