• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <raw_address.h>
22 #include <stdint.h>
23 
24 #include <string>
25 
26 __BEGIN_DECLS
27 
28 #define BTHH_MAX_DSC_LEN 884
29 
30 /* HH connection states */
31 typedef enum {
32   BTHH_CONN_STATE_CONNECTED = 0,
33   BTHH_CONN_STATE_CONNECTING = 1,
34   BTHH_CONN_STATE_DISCONNECTED = 2,
35   BTHH_CONN_STATE_DISCONNECTING = 3,
36   BTHH_CONN_STATE_UNKNOWN = 0xff,
37 } bthh_connection_state_t;
38 
39 __END_DECLS
40 #define CASE_RETURN_TEXT(code) \
41   case code:                   \
42     return #code
43 
bthh_connection_state_text(const bthh_connection_state_t & state)44 inline std::string bthh_connection_state_text(
45     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_UNKNOWN);
52     default:
53       return base::StringPrintf("UNKNOWN[%d]", state);
54   }
55 }
56 #undef CASE_RETURN_TEXT
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 {
87   BTHH_INPUT_REPORT = 1,
88   BTHH_OUTPUT_REPORT,
89   BTHH_FEATURE_REPORT
90 } bthh_report_type_t;
91 
92 typedef struct {
93   int attr_mask;
94   uint8_t sub_class;
95   uint8_t app_id;
96   int vendor_id;
97   int product_id;
98   int version;
99   uint8_t ctry_code;
100   int dl_len;
101   uint8_t dsc_list[BTHH_MAX_DSC_LEN];
102 } bthh_hid_info_t;
103 
104 /** Callback for connection state change.
105  *  state will have one of the values from bthh_connection_state_t
106  */
107 typedef void (*bthh_connection_state_callback)(RawAddress* bd_addr,
108                                                bthh_connection_state_t state);
109 
110 /** Callback for vitual unplug api.
111  *  the status of the vitual unplug
112  */
113 typedef void (*bthh_virtual_unplug_callback)(RawAddress* bd_addr,
114                                              bthh_status_t hh_status);
115 
116 /** Callback for get hid info
117  *  hid_info will contain attr_mask, sub_class, app_id, vendor_id, product_id,
118  * version, ctry_code, len
119  */
120 typedef void (*bthh_hid_info_callback)(RawAddress* bd_addr,
121                                        bthh_hid_info_t hid_info);
122 
123 /** Callback for get protocol api.
124  *  the protocol mode is one of the value from bthh_protocol_mode_t
125  */
126 typedef void (*bthh_protocol_mode_callback)(RawAddress* bd_addr,
127                                             bthh_status_t hh_status,
128                                             bthh_protocol_mode_t mode);
129 
130 /** Callback for get/set_idle_time api.
131  */
132 typedef void (*bthh_idle_time_callback)(RawAddress* bd_addr,
133                                         bthh_status_t hh_status, int idle_rate);
134 
135 /** Callback for get report api.
136  *  if staus is ok rpt_data contains the report data
137  */
138 typedef void (*bthh_get_report_callback)(RawAddress* bd_addr,
139                                          bthh_status_t hh_status,
140                                          uint8_t* rpt_data, int rpt_size);
141 
142 /** Callback for set_report/set_protocol api and if error
143  *  occurs for get_report/get_protocol api.
144  */
145 typedef void (*bthh_handshake_callback)(RawAddress* bd_addr,
146                                         bthh_status_t hh_status);
147 
148 /** BT-HH callback structure. */
149 typedef struct {
150   /** set to sizeof(BtHfCallbacks) */
151   size_t size;
152   bthh_connection_state_callback connection_state_cb;
153   bthh_hid_info_callback hid_info_cb;
154   bthh_protocol_mode_callback protocol_mode_cb;
155   bthh_idle_time_callback idle_time_cb;
156   bthh_get_report_callback get_report_cb;
157   bthh_virtual_unplug_callback virtual_unplug_cb;
158   bthh_handshake_callback handshake_cb;
159 
160 } bthh_callbacks_t;
161 
162 /** Represents the standard BT-HH interface. */
163 typedef struct {
164   /** set to sizeof(BtHhInterface) */
165   size_t size;
166 
167   /**
168    * Register the BtHh callbacks
169    */
170   bt_status_t (*init)(bthh_callbacks_t* callbacks);
171 
172   /** connect to hid device */
173   bt_status_t (*connect)(RawAddress* bd_addr);
174 
175   /** dis-connect from hid device */
176   bt_status_t (*disconnect)(RawAddress* bd_addr);
177 
178   /** Virtual UnPlug (VUP) the specified HID device */
179   bt_status_t (*virtual_unplug)(RawAddress* bd_addr);
180 
181   /** Set the HID device descriptor for the specified HID device. */
182   bt_status_t (*set_info)(RawAddress* bd_addr, bthh_hid_info_t hid_info);
183 
184   /** Get the HID proto mode. */
185   bt_status_t (*get_protocol)(RawAddress* bd_addr,
186                               bthh_protocol_mode_t protocolMode);
187 
188   /** Set the HID proto mode. */
189   bt_status_t (*set_protocol)(RawAddress* bd_addr,
190                               bthh_protocol_mode_t protocolMode);
191 
192   /** Get the HID Idle Time */
193   bt_status_t (*get_idle_time)(RawAddress* bd_addr);
194 
195   /** Set the HID Idle Time */
196   bt_status_t (*set_idle_time)(RawAddress* bd_addr, uint8_t idleTime);
197 
198   /** Send a GET_REPORT to HID device. */
199   bt_status_t (*get_report)(RawAddress* bd_addr, bthh_report_type_t reportType,
200                             uint8_t reportId, int bufferSize);
201 
202   /** Send a SET_REPORT to HID device. */
203   bt_status_t (*set_report)(RawAddress* bd_addr, bthh_report_type_t reportType,
204                             char* report);
205 
206   /** Send data to HID device. */
207   bt_status_t (*send_data)(RawAddress* bd_addr, char* data);
208 
209   /** Closes the interface. */
210   void (*cleanup)(void);
211 
212 } bthh_interface_t;
213 __END_DECLS
214 
215 #endif /* ANDROID_INCLUDE_BT_HH_H */
216