1 /******************************************************************************
2 *
3 * Copyright 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "btcore/include/device_class.h"
20
21 #include <arpa/inet.h>
22 #include <bluetooth/log.h>
23 #include <endian.h>
24 #include <string.h>
25
26 using namespace bluetooth;
27
28 typedef struct _bt_device_class_t {
29 uint32_t unused : 2; // LSBs
30 uint32_t minor_device : 6;
31 uint32_t major_device : 5;
32 uint32_t major_service : 11; // MSBs
33 } __attribute__((__packed__)) _bt_device_class_t;
34
35 // Convenience to interpret raw device class bytes.
36 #define DC(x) ((_bt_device_class_t*)(x))
37
38 // Ensure the internal device class implementation and public one
39 // have equal size.
40 static_assert(sizeof(_bt_device_class_t) == sizeof(bt_device_class_t),
41 "Internal and external device class implementation should have "
42 "the same size");
43
44 // [Major Service Classes]
45 // (https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband)
46 enum {
47 DC_LIMITED_DISCOVERABLE_MODE = 0x0001,
48 DC_RESERVED14 = 0x0002,
49 DC_RESERVED15 = 0x0004,
50 DC_POSITIONING = 0x0008,
51 DC_NETWORKING = 0x0010,
52 DC_RENDERING = 0x0020,
53 DC_CAPTURING = 0x0040,
54 DC_OBJECT_TRANSFER = 0x0080,
55 DC_AUDIO = 0x0100,
56 DC_TELEPHONY = 0x0200,
57 DC_INFORMATION = 0x0400,
58 };
59
60 static bool device_class_get_major_service_(const bt_device_class_t* dc,
61 int bitmask);
62 static void device_class_clr_major_service_(bt_device_class_t* dc, int bitmask);
63 static void device_class_set_major_service_(bt_device_class_t* dc, int bitmask);
64
device_class_from_stream(bt_device_class_t * dc,const uint8_t * data)65 void device_class_from_stream(bt_device_class_t* dc, const uint8_t* data) {
66 log::assert_that(dc != NULL, "assert failed: dc != NULL");
67 log::assert_that(data != NULL, "assert failed: data != NULL");
68 *dc = *(bt_device_class_t*)data;
69 }
70
device_class_to_stream(const bt_device_class_t * dc,uint8_t * data,size_t len)71 int device_class_to_stream(const bt_device_class_t* dc, uint8_t* data,
72 size_t len) {
73 log::assert_that(dc != NULL, "assert failed: dc != NULL");
74 log::assert_that(data != NULL, "assert failed: data != NULL");
75 log::assert_that(len >= sizeof(bt_device_class_t),
76 "assert failed: len >= sizeof(bt_device_class_t)");
77 for (size_t i = 0; i < sizeof(bt_device_class_t); ++i) {
78 data[i] = dc->_[i];
79 }
80 return sizeof(bt_device_class_t);
81 }
82
device_class_from_int(bt_device_class_t * dc,int data)83 void device_class_from_int(bt_device_class_t* dc, int data) {
84 log::assert_that(dc != NULL, "assert failed: dc != NULL");
85 log::assert_that(data != 0, "assert failed: data != 0");
86 // Careful with endianess.
87 dc->_[0] = data & 0xff;
88 dc->_[1] = (data >> 8) & 0xff;
89 dc->_[2] = (data >> 16) & 0xff;
90 }
91
device_class_to_int(const bt_device_class_t * dc)92 int device_class_to_int(const bt_device_class_t* dc) {
93 log::assert_that(dc != NULL, "assert failed: dc != NULL");
94 // Careful with endianess.
95 int val = 0;
96 memcpy(&val, dc, sizeof(*dc));
97 return static_cast<int>(le32toh(val) & 0xffffff);
98 }
99
device_class_equals(const bt_device_class_t * p1,const bt_device_class_t * p2)100 bool device_class_equals(const bt_device_class_t* p1,
101 const bt_device_class_t* p2) {
102 log::assert_that(p1 != NULL, "assert failed: p1 != NULL");
103 log::assert_that(p2 != NULL, "assert failed: p2 != NULL");
104 return (memcmp(p1, p2, sizeof(bt_device_class_t)) == 0);
105 }
106
device_class_copy(bt_device_class_t * dest,const bt_device_class_t * src)107 bool device_class_copy(bt_device_class_t* dest, const bt_device_class_t* src) {
108 log::assert_that(dest != NULL, "assert failed: dest != NULL");
109 log::assert_that(src != NULL, "assert failed: src != NULL");
110 return (memcpy(dest, src, sizeof(bt_device_class_t)) == dest);
111 }
112
device_class_get_major_device(const bt_device_class_t * dc)113 int device_class_get_major_device(const bt_device_class_t* dc) {
114 log::assert_that(dc != NULL, "assert failed: dc != NULL");
115 return DC(dc)->major_device;
116 }
117
device_class_set_major_device(bt_device_class_t * dc,int val)118 void device_class_set_major_device(bt_device_class_t* dc, int val) {
119 log::assert_that(dc != NULL, "assert failed: dc != NULL");
120 DC(dc)->major_device = val;
121 }
122
device_class_get_minor_device(const bt_device_class_t * dc)123 int device_class_get_minor_device(const bt_device_class_t* dc) {
124 log::assert_that(dc != NULL, "assert failed: dc != NULL");
125 return DC(dc)->minor_device;
126 }
127
device_class_set_minor_device(bt_device_class_t * dc,int val)128 void device_class_set_minor_device(bt_device_class_t* dc, int val) {
129 log::assert_that(dc != NULL, "assert failed: dc != NULL");
130 DC(dc)->minor_device = val;
131 }
132
device_class_get_information(const bt_device_class_t * dc)133 bool device_class_get_information(const bt_device_class_t* dc) {
134 log::assert_that(dc != NULL, "assert failed: dc != NULL");
135 return device_class_get_major_service_(dc, DC_INFORMATION);
136 }
137
device_class_set_information(bt_device_class_t * dc,bool set)138 void device_class_set_information(bt_device_class_t* dc, bool set) {
139 log::assert_that(dc != NULL, "assert failed: dc != NULL");
140 if (set)
141 device_class_set_major_service_(dc, DC_INFORMATION);
142 else
143 device_class_clr_major_service_(dc, DC_INFORMATION);
144 }
145
device_class_get_limited(const bt_device_class_t * dc)146 bool device_class_get_limited(const bt_device_class_t* dc) {
147 log::assert_that(dc != NULL, "assert failed: dc != NULL");
148 return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
149 }
150
device_class_set_limited(bt_device_class_t * dc,bool set)151 void device_class_set_limited(bt_device_class_t* dc, bool set) {
152 log::assert_that(dc != NULL, "assert failed: dc != NULL");
153 if (set)
154 device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
155 else
156 device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
157 }
158
device_class_get_major_service_(const bt_device_class_t * dc,int bitmask)159 static bool device_class_get_major_service_(const bt_device_class_t* dc,
160 int bitmask) {
161 return (DC(dc)->major_service & bitmask);
162 }
163
device_class_clr_major_service_(bt_device_class_t * dc,int bitmask)164 static void device_class_clr_major_service_(bt_device_class_t* dc,
165 int bitmask) {
166 DC(dc)->major_service &= ~bitmask;
167 }
168
device_class_set_major_service_(bt_device_class_t * dc,int bitmask)169 static void device_class_set_major_service_(bt_device_class_t* dc,
170 int bitmask) {
171 DC(dc)->major_service |= bitmask;
172 }
173