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 <arpa/inet.h>
20 #include <base/logging.h>
21 #include <string.h>
22
23 #include "btcore/include/device_class.h"
24
25 typedef struct _bt_device_class_t {
26 uint32_t unused : 2; // LSBs
27 uint32_t minor_device : 6;
28 uint32_t major_device : 5;
29 uint32_t major_service : 11; // MSBs
30 } __attribute__((__packed__)) _bt_device_class_t;
31
32 // Convenience to interpret raw device class bytes.
33 #define DC(x) ((_bt_device_class_t*)(x))
34
35 // Ensure the internal device class implementation and public one
36 // have equal size.
37 static_assert(sizeof(_bt_device_class_t) == sizeof(bt_device_class_t),
38 "Internal and external device class implementation should have "
39 "the same size");
40
41 // [Major Service Classes]
42 // (https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband)
43 enum {
44 DC_LIMITED_DISCOVERABLE_MODE = 0x0001,
45 DC_RESERVED14 = 0x0002,
46 DC_RESERVED15 = 0x0004,
47 DC_POSITIONING = 0x0008,
48 DC_NETWORKING = 0x0010,
49 DC_RENDERING = 0x0020,
50 DC_CAPTURING = 0x0040,
51 DC_OBJECT_TRANSFER = 0x0080,
52 DC_AUDIO = 0x0100,
53 DC_TELEPHONY = 0x0200,
54 DC_INFORMATION = 0x0400,
55 };
56
57 static bool device_class_get_major_service_(const bt_device_class_t* dc,
58 int bitmask);
59 static void device_class_clr_major_service_(bt_device_class_t* dc, int bitmask);
60 static void device_class_set_major_service_(bt_device_class_t* dc, int bitmask);
61
device_class_from_stream(bt_device_class_t * dc,const uint8_t * data)62 void device_class_from_stream(bt_device_class_t* dc, const uint8_t* data) {
63 CHECK(dc != NULL);
64 CHECK(data != NULL);
65 *dc = *(bt_device_class_t*)data;
66 }
67
device_class_to_stream(const bt_device_class_t * dc,uint8_t * data,size_t len)68 int device_class_to_stream(const bt_device_class_t* dc, uint8_t* data,
69 size_t len) {
70 CHECK(dc != NULL);
71 CHECK(data != NULL);
72 CHECK(len >= sizeof(bt_device_class_t));
73 for (size_t i = 0; i < sizeof(bt_device_class_t); ++i) {
74 data[i] = dc->_[i];
75 }
76 return sizeof(bt_device_class_t);
77 }
78
device_class_from_int(bt_device_class_t * dc,int data)79 void device_class_from_int(bt_device_class_t* dc, int data) {
80 CHECK(dc != NULL);
81 CHECK(data != 0);
82 // Careful with endianess.
83 dc->_[0] = data & 0xff;
84 dc->_[1] = (data >> 8) & 0xff;
85 dc->_[2] = (data >> 16) & 0xff;
86 }
87
device_class_to_int(const bt_device_class_t * dc)88 int device_class_to_int(const bt_device_class_t* dc) {
89 CHECK(dc != NULL);
90 // Careful with endianess.
91 return (int)(le32toh(*(int*)dc) & 0xffffff);
92 }
93
device_class_equals(const bt_device_class_t * p1,const bt_device_class_t * p2)94 bool device_class_equals(const bt_device_class_t* p1,
95 const bt_device_class_t* p2) {
96 CHECK(p1 != NULL);
97 CHECK(p2 != NULL);
98 return (memcmp(p1, p2, sizeof(bt_device_class_t)) == 0);
99 }
100
device_class_copy(bt_device_class_t * dest,const bt_device_class_t * src)101 bool device_class_copy(bt_device_class_t* dest, const bt_device_class_t* src) {
102 CHECK(dest != NULL);
103 CHECK(src != NULL);
104 return (memcpy(dest, src, sizeof(bt_device_class_t)) == dest);
105 }
106
device_class_get_major_device(const bt_device_class_t * dc)107 int device_class_get_major_device(const bt_device_class_t* dc) {
108 CHECK(dc != NULL);
109 return DC(dc)->major_device;
110 }
111
device_class_set_major_device(bt_device_class_t * dc,int val)112 void device_class_set_major_device(bt_device_class_t* dc, int val) {
113 CHECK(dc != NULL);
114 DC(dc)->major_device = val;
115 }
116
device_class_get_minor_device(const bt_device_class_t * dc)117 int device_class_get_minor_device(const bt_device_class_t* dc) {
118 CHECK(dc != NULL);
119 return DC(dc)->minor_device;
120 }
121
device_class_set_minor_device(bt_device_class_t * dc,int val)122 void device_class_set_minor_device(bt_device_class_t* dc, int val) {
123 CHECK(dc != NULL);
124 DC(dc)->minor_device = val;
125 }
126
device_class_get_information(const bt_device_class_t * dc)127 bool device_class_get_information(const bt_device_class_t* dc) {
128 CHECK(dc != NULL);
129 return device_class_get_major_service_(dc, DC_INFORMATION);
130 }
131
device_class_set_information(bt_device_class_t * dc,bool set)132 void device_class_set_information(bt_device_class_t* dc, bool set) {
133 CHECK(dc != NULL);
134 if (set)
135 device_class_set_major_service_(dc, DC_INFORMATION);
136 else
137 device_class_clr_major_service_(dc, DC_INFORMATION);
138 }
139
device_class_get_limited(const bt_device_class_t * dc)140 bool device_class_get_limited(const bt_device_class_t* dc) {
141 CHECK(dc != NULL);
142 return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
143 }
144
device_class_set_limited(bt_device_class_t * dc,bool set)145 void device_class_set_limited(bt_device_class_t* dc, bool set) {
146 CHECK(dc != NULL);
147 if (set)
148 device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
149 else
150 device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
151 }
152
device_class_get_major_service_(const bt_device_class_t * dc,int bitmask)153 static bool device_class_get_major_service_(const bt_device_class_t* dc,
154 int bitmask) {
155 return (DC(dc)->major_service & bitmask);
156 }
157
device_class_clr_major_service_(bt_device_class_t * dc,int bitmask)158 static void device_class_clr_major_service_(bt_device_class_t* dc,
159 int bitmask) {
160 DC(dc)->major_service &= ~bitmask;
161 }
162
device_class_set_major_service_(bt_device_class_t * dc,int bitmask)163 static void device_class_set_major_service_(bt_device_class_t* dc,
164 int bitmask) {
165 DC(dc)->major_service |= bitmask;
166 }
167