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