• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef __cplusplus
20 #include <cstdint>
21 #else
22 #include <stdint.h>
23 #endif  // __cplusplus
24 
25 #define DEV_CLASS_LEN 3
26 typedef uint8_t DEV_CLASS[DEV_CLASS_LEN]; /* Device class */
27 
28 #ifdef __cplusplus
29 inline constexpr DEV_CLASS kDevClassEmpty = {};
30 #endif  // __cplusplus
31 
32 /* 0x00 is used as unclassified for all minor device classes */
33 #define BTM_COD_MINOR_UNCLASSIFIED 0x00
34 #define BTM_COD_MINOR_WEARABLE_HEADSET 0x04
35 #define BTM_COD_MINOR_CONFM_HANDSFREE 0x08
36 #define BTM_COD_MINOR_CAR_AUDIO 0x20
37 #define BTM_COD_MINOR_SET_TOP_BOX 0x24
38 
39 /* minor device class field for Peripheral Major Class */
40 /* Bits 6-7 independently specify mouse, keyboard, or combo mouse/keyboard */
41 #define BTM_COD_MINOR_KEYBOARD 0x40
42 #define BTM_COD_MINOR_POINTING 0x80
43 /* Bits 2-5 OR'd with selection from bits 6-7 */
44 /* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
45 #define BTM_COD_MINOR_JOYSTICK 0x04
46 #define BTM_COD_MINOR_GAMEPAD 0x08
47 #define BTM_COD_MINOR_REMOTE_CONTROL 0x0C
48 #define BTM_COD_MINOR_DIGITIZING_TABLET 0x14
49 #define BTM_COD_MINOR_CARD_READER 0x18 /* e.g. SIM card reader */
50 #define BTM_COD_MINOR_DIGITAL_PAN 0x1C
51 
52 /* minor device class field for Imaging Major Class */
53 /* Bits 5-7 independently specify display, camera, scanner, or printer */
54 #define BTM_COD_MINOR_DISPLAY 0x10
55 /* Bits 2-3 Reserved */
56 /* #define BTM_COD_MINOR_UNCLASSIFIED       0x00    */
57 
58 /* minor device class field for Wearable Major Class */
59 /* Bits 2-7 meaningful    */
60 #define BTM_COD_MINOR_WRIST_WATCH 0x04
61 #define BTM_COD_MINOR_GLASSES 0x14
62 
63 /* minor device class field for Health Major Class */
64 /* Bits 2-7 meaningful    */
65 #define BTM_COD_MINOR_BLOOD_MONITOR 0x04
66 #define BTM_COD_MINOR_THERMOMETER 0x08
67 #define BTM_COD_MINOR_WEIGHING_SCALE 0x0C
68 #define BTM_COD_MINOR_GLUCOSE_METER 0x10
69 #define BTM_COD_MINOR_PULSE_OXIMETER 0x14
70 #define BTM_COD_MINOR_HEART_PULSE_MONITOR 0x18
71 #define BTM_COD_MINOR_STEP_COUNTER 0x20
72 
73 /***************************
74  * major device class field
75  ***************************/
76 #define BTM_COD_MAJOR_COMPUTER 0x01
77 #define BTM_COD_MAJOR_PHONE 0x02
78 #define BTM_COD_MAJOR_AUDIO 0x04
79 #define BTM_COD_MAJOR_PERIPHERAL 0x05
80 #define BTM_COD_MAJOR_IMAGING 0x06
81 #define BTM_COD_MAJOR_WEARABLE 0x07
82 #define BTM_COD_MAJOR_HEALTH 0x09
83 #define BTM_COD_MAJOR_UNCLASSIFIED 0x1F
84 
85 /***************************
86  * service class fields
87  ***************************/
88 #define BTM_COD_SERVICE_LMTD_DISCOVER 0x0020
89 #define BTM_COD_SERVICE_LE_AUDIO 0x0040
90 #define BTM_COD_SERVICE_POSITIONING 0x0100
91 #define BTM_COD_SERVICE_NETWORKING 0x0200
92 #define BTM_COD_SERVICE_RENDERING 0x0400
93 #define BTM_COD_SERVICE_CAPTURING 0x0800
94 #define BTM_COD_SERVICE_OBJ_TRANSFER 0x1000
95 #define BTM_COD_SERVICE_AUDIO 0x2000
96 #define BTM_COD_SERVICE_TELEPHONY 0x4000
97 #define BTM_COD_SERVICE_INFORMATION 0x8000
98 
99 /* the COD masks */
100 #define BTM_COD_MINOR_CLASS_MASK 0xFC
101 #define BTM_COD_MAJOR_CLASS_MASK 0x1F
102 #define BTM_COD_SERVICE_CLASS_LO_B 0x00E0
103 #define BTM_COD_SERVICE_CLASS_MASK 0xFFE0
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 
125 #ifdef __cplusplus
126 #include <sstream>
dev_class_text(const DEV_CLASS & dev_class)127 inline std::string dev_class_text(const DEV_CLASS& dev_class) {
128   std::ostringstream oss;
129   uint8_t mj, mn;
130   uint16_t sv;
131   BTM_COD_MINOR_CLASS(mn, dev_class);
132   BTM_COD_MAJOR_CLASS(mj, dev_class);
133   BTM_COD_SERVICE_CLASS(sv, dev_class);
134   oss << std::to_string(mj) << "-" << std::to_string(mn) << "-"
135       << std::to_string(sv);
136   return oss.str();
137 }
138 #endif  // __cplusplus
139 
140 #define DEVCLASS_TO_STREAM(p, a)                      \
141   {                                                   \
142     int ijk;                                          \
143     for (ijk = 0; ijk < DEV_CLASS_LEN; ijk++)         \
144       *(p)++ = (uint8_t)(a)[DEV_CLASS_LEN - 1 - ijk]; \
145   }
146 
147 #define STREAM_TO_DEVCLASS(a, p)                               \
148   {                                                            \
149     int ijk;                                                   \
150     uint8_t* _pa = (uint8_t*)(a) + DEV_CLASS_LEN - 1;          \
151     for (ijk = 0; ijk < DEV_CLASS_LEN; ijk++) *_pa-- = *(p)++; \
152   }
153