1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ANDROID_INCLUDE_BT_HH_H
18 #define ANDROID_INCLUDE_BT_HH_H
19
20 #include <stdint.h>
21
22 #include <string>
23
24 #include "macros.h"
25 #include "types/ble_address_with_type.h"
26 #include "types/raw_address.h"
27
28 __BEGIN_DECLS
29
30 #define BTHH_MAX_DSC_LEN 884
31
32 /* HH connection states */
33 typedef enum {
34 BTHH_CONN_STATE_CONNECTED = 0,
35 BTHH_CONN_STATE_CONNECTING = 1,
36 BTHH_CONN_STATE_DISCONNECTED = 2,
37 BTHH_CONN_STATE_DISCONNECTING = 3,
38 BTHH_CONN_STATE_ACCEPTING = 4,
39 BTHH_CONN_STATE_UNKNOWN = 0xff,
40 } bthh_connection_state_t;
41
42 __END_DECLS
43
bthh_connection_state_text(const bthh_connection_state_t & state)44 inline std::string bthh_connection_state_text(const bthh_connection_state_t& state) {
45 switch (state) {
46 CASE_RETURN_TEXT(BTHH_CONN_STATE_CONNECTED);
47 CASE_RETURN_TEXT(BTHH_CONN_STATE_CONNECTING);
48 CASE_RETURN_TEXT(BTHH_CONN_STATE_DISCONNECTED);
49 CASE_RETURN_TEXT(BTHH_CONN_STATE_DISCONNECTING);
50 CASE_RETURN_TEXT(BTHH_CONN_STATE_ACCEPTING);
51 CASE_RETURN_TEXT(BTHH_CONN_STATE_UNKNOWN);
52 default:
53 return std::format("UNKNOWN[{}]", static_cast<int>(state));
54 }
55 }
56
57 __BEGIN_DECLS
58
59 typedef enum {
60 BTHH_OK = 0,
61 BTHH_HS_HID_NOT_READY, /* handshake error : device not ready */
62 BTHH_HS_INVALID_RPT_ID, /* handshake error : invalid report ID */
63 BTHH_HS_TRANS_NOT_SPT, /* handshake error : transaction not spt */
64 BTHH_HS_INVALID_PARAM, /* handshake error : invalid paremter */
65 BTHH_HS_ERROR, /* handshake error : unspecified HS error */
66 BTHH_ERR, /* general BTA HH error */
67 BTHH_ERR_SDP, /* SDP error */
68 BTHH_ERR_PROTO, /* SET_Protocol error,
69 only used in BTA_HH_OPEN_EVT
70 callback */
71 BTHH_ERR_DB_FULL, /* device database full error, used */
72 BTHH_ERR_TOD_UNSPT, /* type of device not supported */
73 BTHH_ERR_NO_RES, /* out of system resources */
74 BTHH_ERR_AUTH_FAILED, /* authentication fail */
75 BTHH_ERR_HDL
76 } bthh_status_t;
77
78 /* Protocol modes */
79 typedef enum {
80 BTHH_REPORT_MODE = 0x00,
81 BTHH_BOOT_MODE = 0x01,
82 BTHH_UNSUPPORTED_MODE = 0xff
83 } bthh_protocol_mode_t;
84
85 /* Report types */
86 typedef enum { BTHH_INPUT_REPORT = 1, BTHH_OUTPUT_REPORT, BTHH_FEATURE_REPORT } bthh_report_type_t;
87
88 /* Info for which profiles to enable */
89 typedef struct {
90 bool hidp_enabled;
91 bool hogp_enabled;
92 } bthh_profile_enable_t;
93
94 typedef struct {
95 int attr_mask;
96 uint8_t sub_class;
97 uint8_t app_id;
98 int vendor_id;
99 int product_id;
100 int version;
101 uint8_t ctry_code;
102 int dl_len;
103 uint8_t dsc_list[BTHH_MAX_DSC_LEN];
104 } bthh_hid_info_t;
105
106 /** Callback for connection state change.
107 * state will have one of the values from bthh_connection_state_t
108 */
109 typedef void (*bthh_connection_state_callback)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
110 tBT_TRANSPORT transport,
111 bthh_connection_state_t state);
112
113 /** Callback for vitual unplug api.
114 * the status of the vitual unplug
115 */
116 typedef void (*bthh_virtual_unplug_callback)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
117 tBT_TRANSPORT transport, bthh_status_t hh_status);
118
119 /** Callback for get hid info
120 * hid_info will contain attr_mask, sub_class, app_id, vendor_id, product_id,
121 * version, ctry_code, len
122 */
123 typedef void (*bthh_hid_info_callback)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
124 tBT_TRANSPORT transport, bthh_hid_info_t hid_info);
125
126 /** Callback for get protocol api.
127 * the protocol mode is one of the value from bthh_protocol_mode_t
128 */
129 typedef void (*bthh_protocol_mode_callback)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
130 tBT_TRANSPORT transport, bthh_status_t hh_status,
131 bthh_protocol_mode_t mode);
132
133 /** Callback for get/set_idle_time api.
134 */
135 typedef void (*bthh_idle_time_callback)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
136 tBT_TRANSPORT transport, bthh_status_t hh_status,
137 int idle_rate);
138
139 /** Callback for get report api.
140 * if staus is ok rpt_data contains the report data
141 */
142 typedef void (*bthh_get_report_callback)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
143 tBT_TRANSPORT transport, bthh_status_t hh_status,
144 uint8_t* rpt_data, int rpt_size);
145
146 /** Callback for set_report/set_protocol api and if error
147 * occurs for get_report/get_protocol api.
148 */
149 typedef void (*bthh_handshake_callback)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
150 tBT_TRANSPORT transport, bthh_status_t hh_status);
151
152 /** BT-HH callback structure. */
153 typedef struct {
154 /** set to sizeof(BtHfCallbacks) */
155 size_t size;
156 bthh_connection_state_callback connection_state_cb;
157 bthh_hid_info_callback hid_info_cb;
158 bthh_protocol_mode_callback protocol_mode_cb;
159 bthh_idle_time_callback idle_time_cb;
160 bthh_get_report_callback get_report_cb;
161 bthh_virtual_unplug_callback virtual_unplug_cb;
162 bthh_handshake_callback handshake_cb;
163 } bthh_callbacks_t;
164
165 /** Represents the standard BT-HH interface. */
166 typedef struct {
167 /** set to sizeof(BtHhInterface) */
168 size_t size;
169
170 /**
171 * Register the BtHh callbacks
172 */
173 bt_status_t (*init)(bthh_callbacks_t* callbacks);
174
175 /** connect to hid device */
176 bt_status_t (*connect)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport);
177
178 /** dis-connect from hid device */
179 bt_status_t (*disconnect)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
180 bool reconnect_allowed);
181
182 /** Virtual UnPlug (VUP) the specified HID device */
183 bt_status_t (*virtual_unplug)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
184 tBT_TRANSPORT transport);
185
186 /** Set the HID device descriptor for the specified HID device. */
187 bt_status_t (*set_info)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
188 bthh_hid_info_t hid_info);
189
190 /** Get the HID proto mode. */
191 bt_status_t (*get_protocol)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
192 tBT_TRANSPORT transport, bthh_protocol_mode_t protocolMode);
193
194 /** Set the HID proto mode. */
195 bt_status_t (*set_protocol)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
196 tBT_TRANSPORT transport, bthh_protocol_mode_t protocolMode);
197
198 /** Get the HID Idle Time */
199 bt_status_t (*get_idle_time)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
200 tBT_TRANSPORT transport);
201
202 /** Set the HID Idle Time */
203 bt_status_t (*set_idle_time)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
204 tBT_TRANSPORT transport, uint8_t idleTime);
205
206 /** Send a GET_REPORT to HID device. */
207 bt_status_t (*get_report)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
208 bthh_report_type_t reportType, uint8_t reportId, int bufferSize);
209
210 /** Send a GET_REPORT_REPLY to HID driver. */
211 bt_status_t (*get_report_reply)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type,
212 tBT_TRANSPORT transport, bthh_status_t status, char* report,
213 uint16_t size);
214
215 /** Send a SET_REPORT to HID device. */
216 bt_status_t (*set_report)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
217 bthh_report_type_t reportType, char* report);
218
219 /** Send data to HID device. */
220 bt_status_t (*send_data)(RawAddress* bd_addr, tBLE_ADDR_TYPE addr_type, tBT_TRANSPORT transport,
221 char* data);
222
223 /** Closes the interface. */
224 void (*cleanup)(void);
225
226 /** Configure which profiles can be enabled. Affected after re-init */
227 void (*configure_enabled_profiles)(bool enable_hidp, bool enable_hogp);
228 } bthh_interface_t;
229 __END_DECLS
230
231 #if __has_include(<bluetooth/log.h>)
232 #include <bluetooth/log.h>
233
234 namespace std {
235 template <>
236 struct formatter<bthh_connection_state_t> : enum_formatter<bthh_connection_state_t> {};
237 template <>
238 struct formatter<bthh_protocol_mode_t> : enum_formatter<bthh_protocol_mode_t> {};
239 template <>
240 struct formatter<bthh_report_type_t> : enum_formatter<bthh_report_type_t> {};
241 } // namespace std
242
243 #endif // __has_include(<bluetooth/log.h>)
244
245 #endif /* ANDROID_INCLUDE_BT_HH_H */
246