1 /*
2 * Copyright 2021 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 #pragma once
18
19 #include <array>
20 #include <cstddef>
21 #include <cstdint>
22 #include <sstream>
23
24 constexpr size_t kDevClassLength = 3;
25 typedef std::array<uint8_t, kDevClassLength> DEV_CLASS; /* Device class */
26
27 /* major class mask */
28 #define PHONE_COD_MAJOR_CLASS_MASK 0x1F00
29
30 /***************************
31 * major device class field
32 * Note: All values are deduced by basing BIT_X to BIT_8, values as per
33 * BT-spec assigned-numbers.
34 ***************************/
35 #define COD_MAJOR_MISC 0x00
36 #define COD_MAJOR_COMPUTER 0x01 // BIT8
37 #define COD_MAJOR_PHONE 0x02 // BIT9
38 #define COD_MAJOR_LAN_NAP 0x03 // BIT8 | BIT9
39 #define COD_MAJOR_AUDIO 0x04 // BIT10
40 #define COD_MAJOR_PERIPHERAL 0x05 // BIT8 | BIT10
41 #define COD_MAJOR_IMAGING 0x06 // BIT9 | BIT10
42 #define COD_MAJOR_WEARABLE 0x07 // BIT8 | BIT9 | BIT10
43 #define COD_MAJOR_TOY 0x08 // BIT11
44 #define COD_MAJOR_HEALTH 0x09 // BIT8 | BIT11
45 #define COD_MAJOR_UNCLASSIFIED 0x1F // BIT8 | BIT9 | BIT10 | BIT11 | BIT12
46
47 /***************************
48 * service class fields
49 * Note: All values are deduced by basing BIT_X to BIT_8, values as per
50 * BT-spec assigned-numbers.
51 ***************************/
52 #define COD_SERVICE_NA 0x0000
53 #define COD_SERVICE_LMTD_DISCOVER 0x0020 // BIT13 (eg. 13-8 = BIT5 = 0x0020)
54 #define COD_SERVICE_LE_AUDIO 0x0040 // BIT14
55 #define COD_SERVICE_POSITIONING 0x0100 // BIT16
56 #define COD_SERVICE_NETWORKING 0x0200 // BIT17
57 #define COD_SERVICE_RENDERING 0x0400 // BIT18
58 #define COD_SERVICE_CAPTURING 0x0800 // BIT19
59 #define COD_SERVICE_OBJ_TRANSFER 0x1000 // BIT20
60 #define COD_SERVICE_AUDIO 0x2000 // BIT21
61 #define COD_SERVICE_TELEPHONY 0x4000 // BIT22
62 #define COD_SERVICE_INFORMATION 0x8000 // BIT23
63
64 /***************************
65 * minor device class field
66 * Note: LSB[1:0] (2 bits) is don't care for minor device class.
67 ***************************/
68 /* Minor Device class field - Computer Major Class (COD_MAJOR_COMPUTER) */
69 #define COD_MAJOR_COMPUTER_MINOR_UNCATEGORIZED 0x00
70 #define COD_MAJOR_COMPUTER_MINOR_DESKTOP_WORKSTATION 0x04 // BIT2
71 #define COD_MAJOR_COMPUTER_MINOR_SERVER_CLASS_COMPUTER 0x08 // BIT3
72 #define COD_MAJOR_COMPUTER_MINOR_LAPTOP 0x0C // BIT2 | BIT3
73 #define COD_MAJOR_COMPUTER_MINOR_HANDHELD_PC_PDA 0x10 // BIT4
74 #define COD_MAJOR_COMPUTER_MINOR_PALM_SIZE_PC_PDA 0x14 // BIT2 | BIT4
75 #define COD_MAJOR_COMPUTER_MINOR_WEARABLE_COMPUTER_WATCH_SIZE 0x18 // BIT3 | BIT4
76 #define COD_MAJOR_COMPUTER_MINOR_TABLET 0x1C // BIT2 | BIT3 | BIT4
77
78 /* Minor Device class field - Phone Major Class (COD_MAJOR_PHONE) */
79 #define COD_MAJOR_PHONE_MINOR_UNCATEGORIZED 0x00
80 #define COD_MAJOR_PHONE_MINOR_CELLULAR 0x04 // BIT2
81 #define COD_MAJOR_PHONE_MINOR_CORDLESS 0x08 // BIT3
82 #define COD_MAJOR_PHONE_MINOR_SMARTPHONE 0x0C // BIT2 | BIT3
83 #define COD_MAJOR_PHONE_MINOR_WIRED_MODEM_OR_VOICE_GATEWAY 0x10 // BIT4
84 #define COD_MAJOR_PHONE_MINOR_COMMON_ISDN_ACCESS 0x14 // BIT2 | BIT4
85
86 /*
87 * Minor Device class field -
88 * LAN/Network Access Point Major Class (COD_MAJOR_LAN_NAP)
89 */
90 #define COD_MAJOR_LAN_NAP_MINOR_FULLY_AVAILABLE 0x00
91 #define COD_MAJOR_LAN_NAP_MINOR_1_TO_17_PER_UTILIZED 0x20 // BIT5
92 #define COD_MAJOR_LAN_NAP_MINOR_17_TO_33_PER_UTILIZED 0x40 // BIT6
93 #define COD_MAJOR_LAN_NAP_MINOR_33_TO_50_PER_UTILIZED 0x60 // BIT5 | BIT6
94 #define COD_MAJOR_LAN_NAP_MINOR_50_TO_67_PER_UTILIZED 0x80 // BIT7
95 #define COD_MAJOR_LAN_NAP_MINOR_67_TO_83_PER_UTILIZED 0xA0 // BIT5 | BIT7
96 #define COD_MAJOR_LAN_NAP_MINOR_83_TO_99_PER_UTILIZED 0xC0 // BIT6 | BIT7
97 #define COD_MAJOR_LAN_NAP_MINOR_NO_SERVICE_AVAILABLE 0xE0 // BIT5 | BIT6 | BIT7
98
99 /* Minor Device class field - Audio/Video Major Class (COD_MAJOR_AUDIO) */
100 /* 0x00 is used as unclassified for all minor device classes */
101 #define COD_MINOR_UNCATEGORIZED 0x00
102 #define COD_MAJOR_AUDIO_MINOR_WEARABLE_HEADSET 0x04 // BIT2
103 #define COD_MAJOR_AUDIO_MINOR_CONFM_HANDSFREE 0x08 // BIT3
104 #define COD_MAJOR_AUDIO_MINOR_MICROPHONE 0x10 // BIT4
105 #define COD_MAJOR_AUDIO_MINOR_LOUDSPEAKER 0x14 // BIT2 | BIT4
106 #define COD_MAJOR_AUDIO_MINOR_HEADPHONES 0x18 // BIT3 | BIT4
107 #define COD_MAJOR_AUDIO_MINOR_PORTABLE_AUDIO 0x1C // BIT2 | BIT3 | BIT4
108 #define COD_MAJOR_AUDIO_MINOR_CAR_AUDIO 0x20 // BIT5
109 #define COD_MAJOR_AUDIO_MINOR_SET_TOP_BOX 0x24 // BIT2 | BIT5
110 #define COD_MAJOR_AUDIO_MINOR_HIFI_AUDIO 0x28 // BIT3 | BIT5
111 #define COD_MAJOR_AUDIO_MINOR_VCR 0x2C // BIT2 | BIT3 | BIT5
112 #define COD_MAJOR_AUDIO_MINOR_VIDEO_CAMERA 0x30 // BIT4 | BIT5
113 #define COD_MAJOR_AUDIO_MINOR_CAMCORDER 0x34 // BIT2 | BIT4 | BIT5
114 #define COD_MAJOR_AUDIO_MINOR_VIDEO_MONITOR 0x38 // BIT3 | BIT4 | BIT5
115 #define COD_MAJOR_AUDIO_MINOR_VIDEO_DISPLAY_AND_LOUDSPEAKER 0x3C // BIT2 |
116 // BIT3 | BIT4 | BIT5
117 #define COD_MAJOR_AUDIO_MINOR_VIDEO_CONFERENCING 0x40 // BIT6
118 #define COD_MAJOR_AUDIO_MINOR_GAMING_OR_TOY 0x48 // BIT3 | BIT6
119
120 /* Minor Device class field - Peripheral Major Class (COD_MAJOR_PERIPHERAL) */
121 /* Bits 6-7 independently specify mouse, keyboard, or combo mouse/keyboard */
122 #define COD_MAJOR_PERIPH_MINOR_UNCATEGORIZED 0x00
123 #define COD_MAJOR_PERIPH_MINOR_KEYBOARD 0x40 // BIT6
124 #define COD_MAJOR_PERIPH_MINOR_POINTING 0x80 // BIT7
125 #define COD_MAJOR_PERIPH_MINOR_KEYBOARD_AND_POINTING_DEVICE 0xC0 // BIT6 | BIT7
126
127 /* Bits 2-5 OR'd with selection from bits 6-7 */
128 #define COD_MAJOR_PERIPH_MINOR_JOYSTICK 0x04 // BIT2
129 #define COD_MAJOR_PERIPH_MINOR_GAMEPAD 0x08 // BIT3
130 #define COD_MAJOR_PERIPH_MINOR_REMOTE_CONTROL 0x0C // BIT2 | BIT3
131 #define COD_MAJOR_PERIPH_MINOR_SENSING_DEVICE 0x10 // BIT4
132 #define COD_MAJOR_PERIPH_MINOR_DIGITIZING_TABLET 0x14 // BIT2 | BIT4
133 #define COD_MAJOR_PERIPH_MINOR_CARD_READER 0x18 /* e.g. SIM card reader, BIT3 | BIT4 */
134 #define COD_MAJOR_PERIPH_MINOR_DIGITAL_PEN 0x1C // Pen, BIT2 | BIT3 | BIT4
135 #define COD_MAJOR_PERIPH_MINOR_HANDHELD_SCANNER 0x20 // e.g. Barcode, RFID, BIT5
136 #define COD_MAJOR_PERIPH_MINOR_HANDHELD_GESTURAL_INP_DEVICE 0x24
137 // e.g. "wand" form factor, BIT2 | BIT5
138
139 /* Minor Device class field - Imaging Major Class (COD_MAJOR_IMAGING)
140 *
141 * Bits 5-7 independently specify display, camera, scanner, or printer
142 * Note: Apart from the set bit, all other bits are don't care.
143 */
144 #define COD_MAJOR_IMAGING_MINOR_DISPLAY 0x10 // BIT4
145 #define COD_MAJOR_IMAGING_MINOR_CAMERA 0x20 // BIT5
146 #define COD_MAJOR_IMAGING_MINOR_SCANNER 0x40 // BIT6
147 #define COD_MAJOR_IMAGING_MINOR_PRINTER 0x80 // BIT7
148
149 /* Minor Device class field - Wearable Major Class (COD_MAJOR_WEARABLE) */
150 #define COD_MAJOR_WEARABLE_MINOR_WRIST_WATCH 0x04 // BIT2
151 #define COD_MAJOR_WEARABLE_MINOR_PAGER 0x08 // BIT3
152 #define COD_MJAOR_WEARABLE_MINOR_JACKET 0x0C // BIT2 | BIT3
153 #define COD_MAJOR_WEARABLE_MINOR_HELMET 0x10 // BIT4
154 #define COD_MAJOR_WEARABLE_MINOR_GLASSES 0x14 // BIT2 | BIT4
155 #define COD_MAJOR_WEARABLE_MINOR_PIN 0x18
156 // e.g. Label pin, broach, badge BIT3 | BIT4
157
158 /* Minor Device class field - Toy Major Class (COD_MAJOR_TOY) */
159 #define COD_MAJOR_TOY_MINOR_ROBOT 0x04 // BIT2
160 #define COD_MAJOR_TOY_MINOR_VEHICLE 0x08 // BIT3
161 #define COD_MAJOR_TOY_MINOR_DOLL_OR_ACTION_FIGURE 0x0C // BIT2 | BIT3
162 #define COD_MAJOR_TOY_MINOR_CONTROLLER 0x10 // BIT4
163 #define COD_MAJOR_TOY_MINOR_GAME 0x14 // BIT2 | BIT4
164
165 /* Minor Device class field - Health Major Class (COD_MAJOR_HEALTH) */
166 #define COD_MAJOR_HEALTH_MINOR_BLOOD_MONITOR 0x04 // Blood pressure monitor, BIT2
167 #define COD_MAJOR_HEALTH_MINOR_THERMOMETER 0x08 // BIT3
168 #define COD_MAJOR_HEALTH_MINOR_WEIGHING_SCALE 0x0C // BIT2 | BIT3
169 #define COD_MAJOR_HEALTH_MINOR_GLUCOSE_METER 0x10 // BIT4
170 #define COD_MAJOR_HEALTH_MINOR_PULSE_OXIMETER 0x14 // BIT2 | BIT4
171 #define COD_MAJOR_HEALTH_MINOR_HEART_PULSE_MONITOR 0x18 // BIT3 | BIT4
172 #define COD_MAJOR_HEALTH_MINOR_HEALTH_DATA_DISPLAY 0x1C // BIT2 | BIT3 | BIT4
173 #define COD_MAJOR_HEALTH_MINOR_STEP_COUNTER 0x20 // BIT5
174 #define COD_MAJOR_HEALTH_MINOR_BODY_COMPOSITION_ANALYZER 0x24 // BIT2 | BIT5
175 #define COD_MAJOR_HEALTH_MINOR_PEAK_FLOW_MONITOR 0x28 // BIT3 | BIT5
176 #define COD_MAJOR_HEALTH_MINOR_MEDICATION_MONITOR 0x2C // BIT2 | BIT3 | BIT5
177 #define COD_MAJOR_HEALTH_MINOR_KNEE_PROSTHESIS 0x30 // BIT4 | BIT5
178 #define COD_MAJOR_HEALTH_MINOR_ANKLE_PROSTHESIS 0x34 // BIT3 | BIT4 | BIT5
179 #define COD_MAJOR_HEALTH_MINOR_GENERIC_HEALTH_MANAGER 0x38 // BIT2 | BIT3 | BIT4 | BIT5
180 #define COD_MAJOR_HEALTH_MINOR_PERSONAL_MOBILITY_DEVICE 0x3C // BIT4 | BIT5
181
182 /* 0x00 is used as unclassified for all minor device classes */
183 #define BTM_COD_MINOR_UNCLASSIFIED COD_MINOR_UNCATEGORIZED
184 #define BTM_COD_MINOR_WEARABLE_HEADSET COD_MAJOR_AUDIO_MINOR_WEARABLE_HEADSET
185 #define BTM_COD_MINOR_CONFM_HANDSFREE COD_MAJOR_AUDIO_MINOR_CONFM_HANDSFREE
186 #define BTM_COD_MINOR_CAR_AUDIO COD_MAJOR_AUDIO_MINOR_CAR_AUDIO
187 #define BTM_COD_MINOR_SET_TOP_BOX COD_MAJOR_AUDIO_MINOR_SET_TOP_BOX
188
189 /* minor device class field for Peripheral Major Class */
190 /* Bits 6-7 independently specify mouse, keyboard, or combo mouse/keyboard */
191 #define BTM_COD_MINOR_KEYBOARD COD_MAJOR_PERIPH_MINOR_KEYBOARD
192 #define BTM_COD_MINOR_POINTING COD_MAJOR_PERIPH_MINOR_POINTING
193 /* Bits 2-5 OR'd with selection from bits 6-7 */
194 /* #define BTM_COD_MINOR_UNCLASSIFIED 0x00 */
195 #define BTM_COD_MINOR_JOYSTICK COD_MAJOR_PERIPH_MINOR_JOYSTICK
196 #define BTM_COD_MINOR_GAMEPAD COD_MAJOR_PERIPH_MINOR_GAMEPAD
197 #define BTM_COD_MINOR_REMOTE_CONTROL COD_MAJOR_PERIPH_MINOR_REMOTE_CONTROL
198 #define BTM_COD_MINOR_DIGITIZING_TABLET COD_MAJOR_PERIPH_MINOR_DIGITIZING_TABLET
199 #define BTM_COD_MINOR_CARD_READER COD_MAJOR_PERIPH_MINOR_CARD_READER
200 #define BTM_COD_MINOR_DIGITAL_PAN COD_MAJOR_PERIPH_MINOR_DIGITAL_PEN
201
202 /* minor device class field for Imaging Major Class */
203 /* Bits 5-7 independently specify display, camera, scanner, or printer */
204 #define BTM_COD_MINOR_DISPLAY COD_MAJOR_IMAGING_MINOR_DISPLAY
205 /* Bits 2-3 Reserved */
206 /* #define BTM_COD_MINOR_UNCLASSIFIED 0x00 */
207
208 /* minor device class field for Wearable Major Class */
209 /* Bits 2-7 meaningful */
210 #define BTM_COD_MINOR_WRIST_WATCH COD_MAJOR_WEARABLE_MINOR_WRIST_WATCH
211 #define BTM_COD_MINOR_GLASSES COD_MAJOR_WEARABLE_MINOR_GLASSES
212
213 /* minor device class field for Health Major Class */
214 /* Bits 2-7 meaningful */
215 #define BTM_COD_MINOR_BLOOD_MONITOR COD_MAJOR_HEALTH_MINOR_BLOOD_MONITOR
216 #define BTM_COD_MINOR_THERMOMETER COD_MAJOR_HEALTH_MINOR_THERMOMETER
217 #define BTM_COD_MINOR_WEIGHING_SCALE COD_MAJOR_HEALTH_MINOR_WEIGHING_SCALE
218 #define BTM_COD_MINOR_GLUCOSE_METER COD_MAJOR_HEALTH_MINOR_GLUCOSE_METER
219 #define BTM_COD_MINOR_PULSE_OXIMETER COD_MAJOR_HEALTH_MINOR_PULSE_OXIMETER
220 #define BTM_COD_MINOR_HEART_PULSE_MONITOR COD_MAJOR_HEALTH_MINOR_HEART_PULSE_MONITOR
221 #define BTM_COD_MINOR_STEP_COUNTER COD_MAJOR_HEALTH_MINOR_STEP_COUNTER
222
223 /***************************
224 * major device class field
225 ***************************/
226 #define BTM_COD_MAJOR_COMPUTER COD_MAJOR_COMPUTER
227 #define BTM_COD_MAJOR_PHONE COD_MAJOR_PHONE
228 #define BTM_COD_MAJOR_AUDIO COD_MAJOR_AUDIO
229 #define BTM_COD_MAJOR_PERIPHERAL COD_MAJOR_PERIPHERAL
230 #define BTM_COD_MAJOR_IMAGING COD_MAJOR_IMAGING
231 #define BTM_COD_MAJOR_WEARABLE COD_MAJOR_WEARABLE
232 #define BTM_COD_MAJOR_HEALTH COD_MAJOR_HEALTH
233 #define BTM_COD_MAJOR_UNCLASSIFIED COD_MAJOR_UNCLASSIFIED
234
235 /***************************
236 * service class fields
237 ***************************/
238 #define BTM_COD_SERVICE_LMTD_DISCOVER COD_SERVICE_LMTD_DISCOVER
239 #define BTM_COD_SERVICE_LE_AUDIO COD_SERVICE_LE_AUDIO
240 #define BTM_COD_SERVICE_POSITIONING COD_SERVICE_POSITIONING
241 #define BTM_COD_SERVICE_NETWORKING COD_SERVICE_NETWORKING
242 #define BTM_COD_SERVICE_RENDERING COD_SERVICE_RENDERING
243 #define BTM_COD_SERVICE_CAPTURING COD_SERVICE_CAPTURING
244 #define BTM_COD_SERVICE_OBJ_TRANSFER COD_SERVICE_OBJ_TRANSFER
245 #define BTM_COD_SERVICE_AUDIO COD_SERVICE_AUDIO
246 #define BTM_COD_SERVICE_TELEPHONY COD_SERVICE_TELEPHONY
247 #define BTM_COD_SERVICE_INFORMATION COD_SERVICE_INFORMATION
248
249 /* the COD masks */
250 #define BTM_COD_MINOR_CLASS_MASK 0xFC
251 #define BTM_COD_MAJOR_CLASS_MASK 0x1F
252 #define BTM_COD_SERVICE_CLASS_LO_B 0x00E0
253 #define BTM_COD_SERVICE_CLASS_MASK 0xFFE0
254
255 inline constexpr DEV_CLASS kDevClassEmpty = {COD_SERVICE_NA, COD_MAJOR_MISC,
256 COD_MINOR_UNCATEGORIZED};
257 inline constexpr DEV_CLASS kDevClassUnclassified = {0x00, BTM_COD_MAJOR_UNCLASSIFIED,
258 BTM_COD_MINOR_UNCLASSIFIED};
259
260 /* class of device field macros */
261 #define BTM_COD_MINOR_CLASS(u8, pd) \
262 { (u8) = (pd)[2] & BTM_COD_MINOR_CLASS_MASK; }
263 #define BTM_COD_MAJOR_CLASS(u8, pd) \
264 { (u8) = (pd)[1] & BTM_COD_MAJOR_CLASS_MASK; }
265 #define BTM_COD_SERVICE_CLASS(u16, pd) \
266 { \
267 (u16) = (pd)[0]; \
268 (u16) <<= 8; \
269 (u16) += (pd)[1] & 0xE0; \
270 }
271
272 /* to set the fields (assumes that format type is always 0) */
273 #define FIELDS_TO_COD(pd, mn, mj, sv) \
274 { \
275 (pd)[2] = mn; \
276 (pd)[1] = (mj) + ((sv) & BTM_COD_SERVICE_CLASS_LO_B); \
277 (pd)[0] = (sv) >> 8; \
278 }
279
dev_class_text(const DEV_CLASS & dev_class)280 inline std::string dev_class_text(const DEV_CLASS& dev_class) {
281 std::ostringstream oss;
282 uint16_t sv;
283 uint8_t mj, mn;
284 BTM_COD_SERVICE_CLASS(sv, dev_class);
285 BTM_COD_MAJOR_CLASS(mj, dev_class);
286 BTM_COD_MINOR_CLASS(mn, dev_class);
287 oss << std::hex << (int)sv << "-" << (int)mj << "-" << (int)mn;
288 return oss.str();
289 }
290
291 #define DEVCLASS_TO_STREAM(p, a) \
292 { \
293 size_t ijk; \
294 for (ijk = 0; ijk < kDevClassLength; ijk++) \
295 *(p)++ = (a)[kDevClassLength - 1 - ijk]; \
296 }
297
298 #define STREAM_TO_DEVCLASS(a, p) \
299 { \
300 size_t ijk; \
301 uint8_t* _pa = a.data() + kDevClassLength - 1; \
302 for (ijk = 0; ijk < kDevClassLength; ijk++) \
303 *_pa-- = *(p)++; \
304 }
305