1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HID_HOST_DEFINES_H
17 #define HID_HOST_DEFINES_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include "gatt_data.h"
23 #include "log.h"
24 #include "l2cap_if.h"
25 #include "packet.h"
26 
27 namespace OHOS {
28 namespace bluetooth {
29 static constexpr int HID_HOST_STATE_DISCONNECTED = 0;
30 static constexpr int HID_HOST_STATE_CONNECTING = 1;
31 static constexpr int HID_HOST_STATE_DISCONNECTING = 2;
32 static constexpr int HID_HOST_STATE_CONNECTED = 3;
33 
34 static constexpr int HID_HOST_SUCCESS = 0;
35 static constexpr int HID_HOST_FAILURE = 1;
36 
37 static constexpr int HID_HOST_DEVICE_TYPE_UNKNOWN = 0;
38 static constexpr int HID_HOST_DEVICE_TYPE_BREDR = 1;
39 static constexpr int HID_HOST_DEVICE_TYPE_BLE = 2;
40 
41 static constexpr int HID_HOST_HOGP_STATE_UNUSED = 0;
42 static constexpr int HID_HOST_HOGP_STATE_CONNECTING = 1;
43 static constexpr int HID_HOST_HOGP_STATE_DISCOVERING = 2;
44 static constexpr int HID_HOST_HOGP_STATE_CONNECTED = 3;
45 static constexpr int HID_HOST_HOGP_STATE_DISCONNECTING = 4;
46 
47 // Hid host event
48 static constexpr int HID_HOST_INVALID_EVT = 0;
49 // Service start/stop
50 static constexpr int HID_HOST_SERVICE_STARTUP_EVT = 1;
51 static constexpr int HID_HOST_SERVICE_SHUTDOWN_EVT = 2;
52 // Device open/close
53 static constexpr int HID_HOST_API_OPEN_EVT = 3;
54 static constexpr int HID_HOST_API_CLOSE_EVT = 4;
55 static constexpr int HID_HOST_INT_OPEN_EVT = 5;
56 static constexpr int HID_HOST_INT_CLOSE_EVT = 6;
57 static constexpr int HID_HOST_INT_DATA_EVT = 7;
58 static constexpr int HID_HOST_INT_CTRL_DATA = 8;
59 static constexpr int HID_HOST_INT_HANDSK_EVT = 9;
60 // SDP completes
61 static constexpr int HID_HOST_SDP_CMPL_EVT = 10;
62 // Write device action. can be SET/GET/DATA transaction.
63 static constexpr int HID_HOST_API_WRITE_DEV_EVT = 11;
64 static constexpr int HID_HOST_OPEN_CMPL_EVT = 12;
65 // Remove state machine
66 static constexpr int HID_HOST_REMOVE_STATE_MACHINE_EVT = 13;
67 // Connection/disconnection timeout
68 static constexpr int HID_HOST_CONNECTION_TIMEOUT_EVT = 14;
69 static constexpr int HID_HOST_DISCONNECTION_TIMEOUT_EVT = 15;
70 // L2cap event
71 static constexpr int HID_HOST_L2CAP_START_EVT = 100;
72 static constexpr int HID_HOST_L2CAP_CONNECT_REQ_EVT = 101;
73 static constexpr int HID_HOST_L2CAP_CONNECT_RSP_EVT = 102;
74 static constexpr int HID_HOST_L2CAP_CONFIG_REQ_EVT = 103;
75 static constexpr int HID_HOST_L2CAP_CONFIG_RSP_EVT = 104;
76 static constexpr int HID_HOST_L2CAP_DISCONNECT_REQ_EVT = 105;
77 static constexpr int HID_HOST_L2CAP_DISCONNECT_RSP_EVT = 106;
78 static constexpr int HID_HOST_L2CAP_DISCONNECT_ABNORMAL_EVT = 107;
79 static constexpr int HID_HOST_L2CAP_DATA_EVT = 108;
80 static constexpr int HID_HOST_L2CAP_REMOTE_BUSY_EVT = 109;
81 static constexpr int HID_HOST_L2CAP_SECURITY_RESULT_EVT = 110;
82 static constexpr int HID_HOST_L2CAP_IND_SECURITY_RESULT_EVT = 111;
83 static constexpr int HID_HOST_L2CAP_END_EVT = 112;
84 // Hogp event
85 static constexpr int HID_HOST_HOGP_START_EVT = 200;
86 static constexpr int HID_HOST_HOGP_CONNECTION_STATE_CHANGED_EVT = 201;
87 static constexpr int HID_HOST_HOGP_SERVICES_DISCOVERED_EVT = 202;
88 static constexpr int HID_HOST_HOGP_STOP_THREAD_EVT = 203;
89 static constexpr int HID_HOST_HOGP_END_EVT = 204;
90 
91 static constexpr int HID_HOST_CONN_STATE_UNUSED = 0;
92 static constexpr int HID_HOST_CONN_STATE_CONNECTING_CTRL = 1;
93 static constexpr int HID_HOST_CONN_STATE_CONNECTING_INTR = 2;
94 static constexpr int HID_HOST_CONN_STATE_CONFIG = 3;
95 static constexpr int HID_HOST_CONN_STATE_CONNECTED = 4;
96 static constexpr int HID_HOST_CONN_STATE_DISCONNECTING = 5;
97 static constexpr int HID_HOST_CONN_STATE_SECURITY = 6;
98 
99 static constexpr int HID_HOST_RESERVED_REPORT = 0;
100 static constexpr int HID_HOST_INPUT_REPORT = 1;
101 static constexpr int HID_HOST_OUTPUT_REPORT = 2;
102 static constexpr int HID_HOST_FEATURE_REPORT = 3;
103 
104 struct HidHostL2capConnectionInfo {
105     uint16_t lpsm = 0;
106     uint16_t lcid = 0;
107     int result = 0;
108     uint8_t id = 0;
109     L2capConfigInfo cfg;
110     uint8_t reason = 0;
111     uint8_t isBusy = 0;
112 };
113 
114 struct SendHidData {
115     uint8_t type = 0;
116     uint8_t param = 0;
117     uint16_t data = 0;
118     uint8_t reportId = 0;
119 };
120 
121 struct PnpInformation {
122     uint16_t vendorId = 0;
123     uint16_t productId = 0;
124     uint16_t version = 0;
125 };
126 
127 static constexpr uint16_t HID_HOST_INVALID = 0xffff;
128 
129 struct HidInformation {
130     uint16_t attrMask = 0;
131 
132     uint16_t supTimeout = 0;
133     uint16_t ssrMaxLatency = HID_HOST_INVALID;
134     uint16_t ssrMinTout = HID_HOST_INVALID;
135     uint16_t relNum = 0;
136     uint8_t ctryCode = 0;
137     uint8_t subClass = 0;
138     uint16_t hparsVer = 0;
139     std::string serviceName = "";
140     std::string serviceDescription = "";
141     std::string providerName = "";
142     uint16_t descLength = 0;
143     std::unique_ptr<uint8_t[]> descInfo = nullptr;
144 };
145 
146 struct HogpReport {
147     uint8_t reportId = 0;
148     uint8_t reportType = HID_HOST_RESERVED_REPORT;
149     std::unique_ptr<Characteristic> character = nullptr;
150     std::unique_ptr<Descriptor> config = nullptr;
151 };
152 
153 static constexpr uint16_t SDP_ATTRIBUTE_VENDOR_ID = 0x0201;
154 static constexpr uint16_t SDP_ATTRIBUTE_PRODUCT_ID = 0x0202;
155 static constexpr uint16_t SDP_ATTRIBUTE_VERSION = 0x0203;
156 
157 static constexpr uint16_t ATTR_ID_HID_DEVICE_RELNUM = 0x0200;
158 static constexpr uint16_t ATTR_ID_HID_PARSER_VERSION = 0x0201;
159 static constexpr uint16_t ATTR_ID_HID_DEVICE_SUBCLASS = 0x0202;
160 static constexpr uint16_t ATTR_ID_HID_COUNTRY_CODE = 0x0203;
161 static constexpr uint16_t ATTR_ID_HID_VIRTUAL_CABLE = 0x0204;
162 static constexpr uint16_t ATTR_ID_HID_RECONNECT_INITIATE = 0x0205;
163 static constexpr uint16_t ATTR_ID_HID_DESCRIPTOR_LIST = 0x0206;
164 static constexpr uint16_t ATTR_ID_HID_LANGUAGE_ID_BASE = 0x0207;
165 static constexpr uint16_t ATTR_ID_HID_SDP_DISABLE = 0x0208;
166 static constexpr uint16_t ATTR_ID_HID_BATTERY_POWER = 0x0209;
167 static constexpr uint16_t ATTR_ID_HID_REMOTE_WAKE = 0x020A;
168 static constexpr uint16_t ATTR_ID_HID_PROFILE_VERSION = 0x020B;
169 static constexpr uint16_t ATTR_ID_HID_LINK_SUPERVISION_TO = 0x020C;
170 static constexpr uint16_t ATTR_ID_HID_NORMALLY_CONNECTABLE = 0x020D;
171 static constexpr uint16_t ATTR_ID_HID_BOOT_DEVICE = 0x020E;
172 static constexpr uint16_t ATTR_ID_HID_SSR_HOST_MAX_LAT = 0x020F;
173 static constexpr uint16_t ATTR_ID_HID_SSR_HOST_MIN_TOUT = 0x0210;
174 
175 static constexpr uint16_t HID_VIRTUAL_CABLE = 0x0001;
176 static constexpr uint16_t HID_NORMALLY_CONNECTABLE = 0x0002;
177 static constexpr uint16_t HID_RECONN_INIT = 0x0004;
178 static constexpr uint16_t HID_SDP_DISABLE = 0x0008;
179 static constexpr uint16_t HID_BATTERY_POWER = 0x0010;
180 static constexpr uint16_t HID_REMOTE_WAKE = 0x0020;
181 static constexpr uint16_t HID_SUP_TOUT_AVLBL = 0x0040;
182 static constexpr uint16_t HID_SSR_MAX_LATENCY = 0x0080;
183 static constexpr uint16_t HID_SSR_MIN_TOUT = 0x0100;
184 
185 static constexpr uint8_t DP_TYPE_DES = 6;
186 static constexpr uint8_t SDP_DE_TYPE_STRING = 4;
187 static constexpr uint8_t HID_SDP_DESCRIPTOR_SIZE_BIT = 3;
188 static constexpr uint8_t SDP_SIZE_MASK = 0x07;
189 static constexpr uint8_t SDP_DE_SIZE_VAR_8 = 5;
190 static constexpr uint8_t SDP_DE_SIZE_VAR_16 = 6;
191 static constexpr uint8_t SDP_UINT8_LENGTH = 1;
192 static constexpr uint8_t SDP_UINT16_LENGTH = 2;
193 
194 static constexpr uint8_t DESCRIPTOR_LIST_SDP_HEAD_LENGTH = 6;
195 static constexpr uint8_t DESCRIPTOR_LIST_SDP_OFFSET = 4;
196 
197 static constexpr uint8_t ONE_BYTE_OFFSET = 8;
198 
199 static constexpr int HID_HOST_SDP_SUCCESS = 0;
200 static constexpr int HID_HOST_SDP_FAILD = 1;
201 
202 /* Define PSMs for HID host */
203 static constexpr uint16_t HID_CONTROL_PSM = 0x0011;
204 static constexpr uint16_t HID_INTERRUPT_PSM = 0x0013;
205 
206 static constexpr uint16_t HID_HOST_MTU = 640;
207 
208 static constexpr uint8_t HID_HOST_CONN_FLAGS_IS_ORIG = 0x01;
209 static constexpr uint8_t HID_HOST_CONN_FLAGS_HIS_CTRL_CFG_DONE = 0x02;
210 static constexpr uint8_t HID_HOST_CONN_FLAGS_MY_CTRL_CFG_DONE = 0x04;
211 static constexpr uint8_t HID_HOST_CONN_FLAGS_HIS_INTR_CFG_DONE = 0x08;
212 static constexpr uint8_t HID_HOST_CONN_FLAGS_MY_INTR_CFG_DONE = 0x10;
213 static constexpr uint8_t HID_HOST_CONN_FLAGS_ALL_CONFIGURED = 0x1E;
214 
215 static constexpr uint8_t HID_HOST_PDU_HEADER_LENGTH = 1;
216 
217 static constexpr uint16_t HID_HOST_UUID_SERVCLASS_PNP = 0x1200;
218 static constexpr uint16_t HID_HOST_UUID_SERVCLASS_HID = 0x1124;
219 static constexpr uint16_t HID_HOST_UUID_SERVCLASS_LE_HID = 0x1812;
220 static constexpr uint16_t HID_HOST_UUID_GATT_PNP_ID = 0x2A50;
221 static constexpr uint16_t HID_HOST_UUID_GATT_HID_INFORMATION = 0x2A4A;
222 static constexpr uint16_t HID_HOST_UUID_GATT_HID_REPORT_MAP = 0x2A4B;
223 static constexpr uint16_t HID_HOST_UUID_GATT_HID_REPORT = 0x2A4D;
224 static constexpr uint16_t HID_HOST_UUID_GATT_CLIENT_CHAR_CONFIG = 0x2902;
225 static constexpr uint16_t HID_HOST_UUID_GATT_REPORT_REFERENCE = 0x2908;
226 
227 // For PTS(HOGP/RH/HGRF/BV-02-I and HOGP/RH/HGRF/BV-10-I)
228 static constexpr uint16_t HID_HOST_UUID_BATTERY_SERVICE = 0x180F;
229 static constexpr uint16_t HID_HOST_UUID_GATT_BATTERY_LEVEL = 0x2A19;
230 static constexpr uint16_t HID_HOST_UUID_GATT_EXT_RPT_REF = 0x2907;
231 static constexpr uint16_t HID_HOST_UUID_GATT_EXT_CFG_REF = 0x2902;
232 
233 static constexpr int HID_HOST_PNP_ID_SIZE = 7;
234 static constexpr int HID_HOST_HID_INFORMATION_SIZE = 4;
235 static constexpr int HID_HOST_REPORT_REFERENCE_SIZE = 2;
236 static constexpr int HID_HOST_CLIENT_CHAR_CONFIG_SIZE = 2;
237 
238 static constexpr uint8_t HID_HOST_CTRY_CODE_OFFSET = 2;
239 
240 static constexpr int HOGP_GATT_THREAD_WAIT_TIMEOUT = 5;
241 
242 inline static constexpr uint16_t HID_HOST_CLASSID_NUM = 1;
243 inline static constexpr int HID_HOST_ATTRIBUTE_LENGTH_UINT8 = 1;
244 inline static constexpr int HID_HOST_ATTRIBUTE_LENGTH_UINT16 = 2;
245 
246 /* HID data types
247 */
248 static constexpr uint8_t HID_HOST_DATA_TYPE_HANDSHAKE = 0;
249 static constexpr uint8_t HID_HOST_DATA_TYPE_CONTROL = 1;
250 static constexpr uint8_t HID_HOST_DATA_TYPE_GET_REPORT = 4;
251 static constexpr uint8_t HID_HOST_DATA_TYPE_SET_REPORT = 5;
252 static constexpr uint8_t HID_HOST_DATA_TYPE_GET_PROTOCOL = 6;
253 static constexpr uint8_t HID_HOST_DATA_TYPE_SET_PROTOCOL = 7;
254 static constexpr uint8_t HID_HOST_DATA_TYPE_GET_IDLE = 8;
255 static constexpr uint8_t HID_HOST_DATA_TYPE_SET_IDLE = 9;
256 static constexpr uint8_t HID_HOST_DATA_TYPE_DATA = 10;
257 static constexpr uint8_t HID_HOST_DATA_TYPE_DATAC = 11;
258 
259 static constexpr uint8_t HID_HOST_HANDSHAKE_ERROR = 5;
260 
261 /* HID shift operation */
262 static constexpr uint8_t HID_HOST_SHIFT_OPRATURN_4 = 4;
263 static constexpr uint8_t HID_HOST_SHIFT_OPRATURN_8 = 8;
264 
265 static const char UHID_DEVICE_PATH[] = { "/dev/uhid" };
266 }  // namespace bluetooth
267 }  // namespace OHOS
268 #endif  // HID_HOST_DEFINES_H
269