1 /******************************************************************************
3 * Copyright 2014 Google, Inc.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 #include <arpa/inet.h>
20 #include <base/logging.h>
23 #include "btcore/include/device_class.h"
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;
32 // Convenience to interpret raw device class bytes.
33 #define DC(x) ((_bt_device_class_t*)(x))
35 // Ensure the internal device class implementation and public one
37 static_assert(sizeof(_bt_device_class_t) == sizeof(bt_device_class_t),
38 "Internal and external device class implementation should have "
41 // [Major Service Classes]
42 // (https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband)
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,
53 DC_TELEPHONY = 0x0200,
54 DC_INFORMATION = 0x0400,
57 static bool device_class_get_major_service_(const bt_device_class_t* dc,
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);
62 void device_class_from_stream(bt_device_class_t* dc, const uint8_t* data) {
65 *dc = *(bt_device_class_t*)data;
68 int device_class_to_stream(const bt_device_class_t* dc, uint8_t* data,
72 CHECK(len >= sizeof(bt_device_class_t));
73 for (size_t i = 0; i < sizeof(bt_device_class_t); ++i) {
76 return sizeof(bt_device_class_t);
79 void device_class_from_int(bt_device_class_t* dc, int data) {
82 // Careful with endianess.
83 dc->_[0] = data & 0xff;
84 dc->_[1] = (data >> 8) & 0xff;
85 dc->_[2] = (data >> 16) & 0xff;
88 int device_class_to_int(const bt_device_class_t* dc) {
90 // Careful with endianess.
92 memcpy(&val, dc, sizeof(*dc));
93 return static_cast<int>(le32toh(val) & 0xffffff);
96 bool device_class_equals(const bt_device_class_t* p1,
97 const bt_device_class_t* p2) {
100 return (memcmp(p1, p2, sizeof(bt_device_class_t)) == 0);
103 bool device_class_copy(bt_device_class_t* dest, const bt_device_class_t* src) {
106 return (memcpy(dest, src, sizeof(bt_device_class_t)) == dest);
109 int device_class_get_major_device(const bt_device_class_t* dc) {
111 return DC(dc)->major_device;
114 void device_class_set_major_device(bt_device_class_t* dc, int val) {
116 DC(dc)->major_device = val;
119 int device_class_get_minor_device(const bt_device_class_t* dc) {
121 return DC(dc)->minor_device;
124 void device_class_set_minor_device(bt_device_class_t* dc, int val) {
126 DC(dc)->minor_device = val;
129 bool device_class_get_information(const bt_device_class_t* dc) {
131 return device_class_get_major_service_(dc, DC_INFORMATION);
134 void device_class_set_information(bt_device_class_t* dc, bool set) {
137 device_class_set_major_service_(dc, DC_INFORMATION);
139 device_class_clr_major_service_(dc, DC_INFORMATION);
142 bool device_class_get_limited(const bt_device_class_t* dc) {
144 return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
147 void device_class_set_limited(bt_device_class_t* dc, bool set) {
150 device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
152 device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
155 static bool device_class_get_major_service_(const bt_device_class_t* dc,
157 return (DC(dc)->major_service & bitmask);
160 static void device_class_clr_major_service_(bt_device_class_t* dc,
162 DC(dc)->major_service &= ~bitmask;
165 static void device_class_set_major_service_(bt_device_class_t* dc,
167 DC(dc)->major_service |= bitmask;