• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   int val = 0;
92   memcpy(&val, dc, sizeof(*dc));
93   return static_cast<int>(le32toh(val) & 0xffffff);
94 }
95 
device_class_equals(const bt_device_class_t * p1,const bt_device_class_t * p2)96 bool device_class_equals(const bt_device_class_t* p1,
97                          const bt_device_class_t* p2) {
98   CHECK(p1 != NULL);
99   CHECK(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   CHECK(dest != NULL);
105   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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   CHECK(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 
device_class_get_limited(const bt_device_class_t * dc)142 bool device_class_get_limited(const bt_device_class_t* dc) {
143   CHECK(dc != NULL);
144   return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
145 }
146 
device_class_set_limited(bt_device_class_t * dc,bool set)147 void device_class_set_limited(bt_device_class_t* dc, bool set) {
148   CHECK(dc != NULL);
149   if (set)
150     device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
151   else
152     device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
153 }
154 
device_class_get_major_service_(const bt_device_class_t * dc,int bitmask)155 static bool device_class_get_major_service_(const bt_device_class_t* dc,
156                                             int bitmask) {
157   return (DC(dc)->major_service & bitmask);
158 }
159 
device_class_clr_major_service_(bt_device_class_t * dc,int bitmask)160 static void device_class_clr_major_service_(bt_device_class_t* dc,
161                                             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,
166                                             int bitmask) {
167   DC(dc)->major_service |= bitmask;
168 }
169