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