1 /******************************************************************************
3 * Copyright (C) 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>
23 #include "btcore/include/device_class.h"
24 #include "osi/include/osi.h"
26 typedef struct _bt_device_class_t {
27 uint32_t unused : 2; // LSBs
28 uint32_t minor_device : 6;
29 uint32_t major_device : 5;
30 uint32_t major_service : 11; // MSBs
31 } __attribute__ ((__packed__)) _bt_device_class_t;
33 // Convenience to interpret raw device class bytes.
34 #define DC(x) ((_bt_device_class_t *)x)
36 // Ensure the internal device class implementation and public one
38 COMPILE_ASSERT(sizeof(_bt_device_class_t) == sizeof(bt_device_class_t));
40 // [Major Service Classes](https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband)
42 DC_LIMITED_DISCOVERABLE_MODE = 0x0001,
43 DC_RESERVED14 = 0x0002,
44 DC_RESERVED15 = 0x0004,
45 DC_POSITIONING = 0x0008,
46 DC_NETWORKING = 0x0010,
47 DC_RENDERING = 0x0020,
48 DC_CAPTURING = 0x0040,
49 DC_OBJECT_TRANSFER = 0x0080,
51 DC_TELEPHONY = 0x0200,
52 DC_INFORMATION = 0x0400,
55 static bool device_class_get_major_service_(const bt_device_class_t *dc, int bitmask);
56 static void device_class_clr_major_service_(bt_device_class_t *dc, int bitmask);
57 static void device_class_set_major_service_(bt_device_class_t *dc, int bitmask);
59 void device_class_from_stream(bt_device_class_t *dc, const uint8_t *data) {
62 *dc = *(bt_device_class_t *)data;
65 int device_class_to_stream(const bt_device_class_t *dc, uint8_t *data, size_t len) {
68 assert(len >= sizeof(bt_device_class_t));
69 for (size_t i = 0; i < sizeof(bt_device_class_t); ++i) {
72 return sizeof(bt_device_class_t);
75 void device_class_from_int(bt_device_class_t *dc, int data) {
78 // Careful with endianess.
79 dc->_[0] = data & 0xff;
80 dc->_[1] = (data >> 8) & 0xff;
81 dc->_[2] = (data >> 16) & 0xff;
84 int device_class_to_int(const bt_device_class_t *dc) {
86 // Careful with endianess.
87 return (int)(le32toh(*(int*)dc) & 0xffffff);
90 bool device_class_equals(const bt_device_class_t *p1, const bt_device_class_t *p2) {
93 return (memcmp(p1, p2, sizeof(bt_device_class_t)) == 0);
96 bool device_class_copy(bt_device_class_t *dest, const bt_device_class_t *src) {
99 return (memcpy(dest, src, sizeof(bt_device_class_t)) == dest);
102 int device_class_get_major_device(const bt_device_class_t *dc) {
104 return DC(dc)->major_device;
107 void device_class_set_major_device(bt_device_class_t *dc, int val) {
109 DC(dc)->major_device = val;
112 int device_class_get_minor_device(const bt_device_class_t *dc) {
114 return DC(dc)->minor_device;
117 void device_class_set_minor_device(bt_device_class_t *dc, int val) {
119 DC(dc)->minor_device = val;
122 bool device_class_get_information(const bt_device_class_t *dc) {
124 return device_class_get_major_service_(dc, DC_INFORMATION);
127 void device_class_set_information(bt_device_class_t *dc, bool set) {
130 device_class_set_major_service_(dc, DC_INFORMATION);
132 device_class_clr_major_service_(dc, DC_INFORMATION);
135 bool device_class_get_limited(const bt_device_class_t *dc) {
137 return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
140 void device_class_set_limited(bt_device_class_t *dc, bool set) {
143 device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
145 device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
148 static bool device_class_get_major_service_(const bt_device_class_t *dc, int bitmask) {
149 return (DC(dc)->major_service & bitmask);
152 static void device_class_clr_major_service_(bt_device_class_t *dc, int bitmask) {
153 DC(dc)->major_service &= ~bitmask;
156 static void device_class_set_major_service_(bt_device_class_t *dc, int bitmask) {
157 DC(dc)->major_service |= bitmask;