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 inline constexpr DEV_CLASS kDevClassEmpty = {};
28 
29 /* 0x00 is used as unclassified for all minor device classes */
30 #define BTM_COD_MINOR_UNCLASSIFIED 0x00
31 #define BTM_COD_MINOR_WEARABLE_HEADSET 0x04
32 #define BTM_COD_MINOR_CONFM_HANDSFREE 0x08
33 #define BTM_COD_MINOR_CAR_AUDIO 0x20
34 #define BTM_COD_MINOR_SET_TOP_BOX 0x24
35 
36 /* minor device class field for Peripheral Major Class */
37 /* Bits 6-7 independently specify mouse, keyboard, or combo mouse/keyboard */
38 #define BTM_COD_MINOR_KEYBOARD 0x40
39 #define BTM_COD_MINOR_POINTING 0x80
40 /* Bits 2-5 OR'd with selection from bits 6-7 */
41 /* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
42 #define BTM_COD_MINOR_JOYSTICK 0x04
43 #define BTM_COD_MINOR_GAMEPAD 0x08
44 #define BTM_COD_MINOR_REMOTE_CONTROL 0x0C
45 #define BTM_COD_MINOR_DIGITIZING_TABLET 0x14
46 #define BTM_COD_MINOR_CARD_READER 0x18 /* e.g. SIM card reader */
47 #define BTM_COD_MINOR_DIGITAL_PAN 0x1C
48 
49 /* minor device class field for Imaging Major Class */
50 /* Bits 5-7 independently specify display, camera, scanner, or printer */
51 #define BTM_COD_MINOR_DISPLAY 0x10
52 /* Bits 2-3 Reserved */
53 /* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
54 
55 /* minor device class field for Wearable Major Class */
56 /* Bits 2-7 meaningful    */
57 #define BTM_COD_MINOR_WRIST_WATCH 0x04
58 #define BTM_COD_MINOR_GLASSES 0x14
59 
60 /* minor device class field for Health Major Class */
61 /* Bits 2-7 meaningful    */
62 #define BTM_COD_MINOR_BLOOD_MONITOR 0x04
63 #define BTM_COD_MINOR_THERMOMETER 0x08
64 #define BTM_COD_MINOR_WEIGHING_SCALE 0x0C
65 #define BTM_COD_MINOR_GLUCOSE_METER 0x10
66 #define BTM_COD_MINOR_PULSE_OXIMETER 0x14
67 #define BTM_COD_MINOR_HEART_PULSE_MONITOR 0x18
68 #define BTM_COD_MINOR_STEP_COUNTER 0x20
69 
70 /***************************
71  * major device class field
72  ***************************/
73 #define BTM_COD_MAJOR_COMPUTER 0x01
74 #define BTM_COD_MAJOR_PHONE 0x02
75 #define BTM_COD_MAJOR_AUDIO 0x04
76 #define BTM_COD_MAJOR_PERIPHERAL 0x05
77 #define BTM_COD_MAJOR_IMAGING 0x06
78 #define BTM_COD_MAJOR_WEARABLE 0x07
79 #define BTM_COD_MAJOR_HEALTH 0x09
80 #define BTM_COD_MAJOR_UNCLASSIFIED 0x1F
81 
82 /***************************
83  * service class fields
84  ***************************/
85 #define BTM_COD_SERVICE_LMTD_DISCOVER 0x0020
86 #define BTM_COD_SERVICE_LE_AUDIO 0x0040
87 #define BTM_COD_SERVICE_POSITIONING 0x0100
88 #define BTM_COD_SERVICE_NETWORKING 0x0200
89 #define BTM_COD_SERVICE_RENDERING 0x0400
90 #define BTM_COD_SERVICE_CAPTURING 0x0800
91 #define BTM_COD_SERVICE_OBJ_TRANSFER 0x1000
92 #define BTM_COD_SERVICE_AUDIO 0x2000
93 #define BTM_COD_SERVICE_TELEPHONY 0x4000
94 #define BTM_COD_SERVICE_INFORMATION 0x8000
95 
96 /* the COD masks */
97 #define BTM_COD_MINOR_CLASS_MASK 0xFC
98 #define BTM_COD_MAJOR_CLASS_MASK 0x1F
99 #define BTM_COD_SERVICE_CLASS_LO_B 0x00E0
100 #define BTM_COD_SERVICE_CLASS_MASK 0xFFE0
101 
102 inline constexpr DEV_CLASS kDevClassUnclassified = {
103     0x00, BTM_COD_MAJOR_UNCLASSIFIED, BTM_COD_MINOR_UNCLASSIFIED};
104 
105 /* class of device field macros */
106 #define BTM_COD_MINOR_CLASS(u8, pd) \
107   { (u8) = (pd)[2] & BTM_COD_MINOR_CLASS_MASK; }
108 #define BTM_COD_MAJOR_CLASS(u8, pd) \
109   { (u8) = (pd)[1] & BTM_COD_MAJOR_CLASS_MASK; }
110 #define BTM_COD_SERVICE_CLASS(u16, pd) \
111   {                                    \
112     (u16) = (pd)[0];                   \
113     (u16) <<= 8;                       \
114     (u16) += (pd)[1] & 0xE0;           \
115   }
116 
117 /* to set the fields (assumes that format type is always 0) */
118 #define FIELDS_TO_COD(pd, mn, mj, sv)                   \
119   {                                                     \
120     (pd)[2] = mn;                                       \
121     (pd)[1] = (mj) + ((sv)&BTM_COD_SERVICE_CLASS_LO_B); \
122     (pd)[0] = (sv) >> 8;                                \
123   }
124 
dev_class_text(const DEV_CLASS & dev_class)125 inline std::string dev_class_text(const DEV_CLASS& dev_class) {
126   std::ostringstream oss;
127   uint16_t sv;
128   uint8_t mj, mn;
129   BTM_COD_SERVICE_CLASS(sv, dev_class);
130   BTM_COD_MAJOR_CLASS(mj, dev_class);
131   BTM_COD_MINOR_CLASS(mn, dev_class);
132   oss << std::hex << (int)sv << "-" << (int)mj << "-" << (int)mn;
133   return oss.str();
134 }
135 
136 #define DEVCLASS_TO_STREAM(p, a)                \
137   {                                             \
138     size_t ijk;                                 \
139     for (ijk = 0; ijk < kDevClassLength; ijk++) \
140       *(p)++ = (a)[kDevClassLength - 1 - ijk];  \
141   }
142 
143 #define STREAM_TO_DEVCLASS(a, p)                                 \
144   {                                                              \
145     size_t ijk;                                                  \
146     uint8_t* _pa = a.data() + kDevClassLength - 1;               \
147     for (ijk = 0; ijk < kDevClassLength; ijk++) *_pa-- = *(p)++; \
148   }
149