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 the same size");
42
43 // [Major Service Classes]
44 // (https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband)
45 enum {
46 DC_LIMITED_DISCOVERABLE_MODE = 0x0001,
47 DC_RESERVED14 = 0x0002,
48 DC_RESERVED15 = 0x0004,
49 DC_POSITIONING = 0x0008,
50 DC_NETWORKING = 0x0010,
51 DC_RENDERING = 0x0020,
52 DC_CAPTURING = 0x0040,
53 DC_OBJECT_TRANSFER = 0x0080,
54 DC_AUDIO = 0x0100,
55 DC_TELEPHONY = 0x0200,
56 DC_INFORMATION = 0x0400,
57 };
58
59 static bool device_class_get_major_service_(const bt_device_class_t* dc, int bitmask);
60 static void device_class_clr_major_service_(bt_device_class_t* dc, int bitmask);
61 static void device_class_set_major_service_(bt_device_class_t* dc, int bitmask);
62
device_class_from_stream(bt_device_class_t * dc,const uint8_t * data)63 void device_class_from_stream(bt_device_class_t* dc, const uint8_t* data) {
64 log::assert_that(dc != NULL, "assert failed: dc != NULL");
65 log::assert_that(data != NULL, "assert failed: data != NULL");
66 *dc = *(bt_device_class_t*)data;
67 }
68
device_class_to_stream(const bt_device_class_t * dc,uint8_t * data,size_t len)69 int device_class_to_stream(const bt_device_class_t* dc, uint8_t* data, size_t len) {
70 log::assert_that(dc != NULL, "assert failed: dc != NULL");
71 log::assert_that(data != NULL, "assert failed: data != NULL");
72 log::assert_that(len >= sizeof(bt_device_class_t),
73 "assert failed: len >= sizeof(bt_device_class_t)");
74 for (size_t i = 0; i < sizeof(bt_device_class_t); ++i) {
75 data[i] = dc->_[i];
76 }
77 return sizeof(bt_device_class_t);
78 }
79
device_class_from_int(bt_device_class_t * dc,int data)80 void device_class_from_int(bt_device_class_t* dc, int data) {
81 log::assert_that(dc != NULL, "assert failed: dc != NULL");
82 log::assert_that(data != 0, "assert failed: data != 0");
83 // Careful with endianess.
84 dc->_[0] = data & 0xff;
85 dc->_[1] = (data >> 8) & 0xff;
86 dc->_[2] = (data >> 16) & 0xff;
87 }
88
device_class_to_int(const bt_device_class_t * dc)89 int device_class_to_int(const bt_device_class_t* dc) {
90 log::assert_that(dc != NULL, "assert failed: dc != NULL");
91 // Careful with endianess.
92 int val = 0;
93 memcpy(&val, dc, sizeof(*dc));
94 return static_cast<int>(le32toh(val) & 0xffffff);
95 }
96
device_class_equals(const bt_device_class_t * p1,const bt_device_class_t * p2)97 bool device_class_equals(const bt_device_class_t* p1, const bt_device_class_t* p2) {
98 log::assert_that(p1 != NULL, "assert failed: p1 != NULL");
99 log::assert_that(p2 != NULL, "assert failed: p2 != NULL");
100 return memcmp(p1, p2, sizeof(bt_device_class_t)) == 0;
101 }
102
device_class_copy(bt_device_class_t * dest,const bt_device_class_t * src)103 bool device_class_copy(bt_device_class_t* dest, const bt_device_class_t* src) {
104 log::assert_that(dest != NULL, "assert failed: dest != NULL");
105 log::assert_that(src != NULL, "assert failed: src != NULL");
106 return memcpy(dest, src, sizeof(bt_device_class_t)) == dest;
107 }
108
device_class_get_major_device(const bt_device_class_t * dc)109 int device_class_get_major_device(const bt_device_class_t* dc) {
110 log::assert_that(dc != NULL, "assert failed: dc != NULL");
111 return DC(dc)->major_device;
112 }
113
device_class_set_major_device(bt_device_class_t * dc,int val)114 void device_class_set_major_device(bt_device_class_t* dc, int val) {
115 log::assert_that(dc != NULL, "assert failed: dc != NULL");
116 DC(dc)->major_device = val;
117 }
118
device_class_get_minor_device(const bt_device_class_t * dc)119 int device_class_get_minor_device(const bt_device_class_t* dc) {
120 log::assert_that(dc != NULL, "assert failed: dc != NULL");
121 return DC(dc)->minor_device;
122 }
123
device_class_set_minor_device(bt_device_class_t * dc,int val)124 void device_class_set_minor_device(bt_device_class_t* dc, int val) {
125 log::assert_that(dc != NULL, "assert failed: dc != NULL");
126 DC(dc)->minor_device = val;
127 }
128
device_class_get_information(const bt_device_class_t * dc)129 bool device_class_get_information(const bt_device_class_t* dc) {
130 log::assert_that(dc != NULL, "assert failed: dc != NULL");
131 return device_class_get_major_service_(dc, DC_INFORMATION);
132 }
133
device_class_set_information(bt_device_class_t * dc,bool set)134 void device_class_set_information(bt_device_class_t* dc, bool set) {
135 log::assert_that(dc != NULL, "assert failed: dc != NULL");
136 if (set) {
137 device_class_set_major_service_(dc, DC_INFORMATION);
138 } else {
139 device_class_clr_major_service_(dc, DC_INFORMATION);
140 }
141 }
142
device_class_get_limited(const bt_device_class_t * dc)143 bool device_class_get_limited(const bt_device_class_t* dc) {
144 log::assert_that(dc != NULL, "assert failed: dc != NULL");
145 return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
146 }
147
device_class_set_limited(bt_device_class_t * dc,bool set)148 void device_class_set_limited(bt_device_class_t* dc, bool set) {
149 log::assert_that(dc != NULL, "assert failed: dc != NULL");
150 if (set) {
151 device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
152 } else {
153 device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
154 }
155 }
156
device_class_get_major_service_(const bt_device_class_t * dc,int bitmask)157 static bool device_class_get_major_service_(const bt_device_class_t* dc, int bitmask) {
158 return DC(dc)->major_service & bitmask;
159 }
160
device_class_clr_major_service_(bt_device_class_t * dc,int bitmask)161 static void device_class_clr_major_service_(bt_device_class_t* dc, int bitmask) {
162 DC(dc)->major_service &= ~bitmask;
163 }
164
device_class_set_major_service_(bt_device_class_t * dc,int bitmask)165 static void device_class_set_major_service_(bt_device_class_t* dc, int bitmask) {
166 DC(dc)->major_service |= bitmask;
167 }
168