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