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