• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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;
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.
38 COMPILE_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)
42 enum {
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 
56 static bool device_class_get_major_service_(const bt_device_class_t* dc,
57                                             int bitmask);
58 static void device_class_clr_major_service_(bt_device_class_t* dc, int bitmask);
59 static void device_class_set_major_service_(bt_device_class_t* dc, int bitmask);
60 
device_class_from_stream(bt_device_class_t * dc,const uint8_t * data)61 void 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 
device_class_to_stream(const bt_device_class_t * dc,uint8_t * data,size_t len)67 int 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 
device_class_from_int(bt_device_class_t * dc,int data)78 void 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 
device_class_to_int(const bt_device_class_t * dc)87 int device_class_to_int(const bt_device_class_t* dc) {
88   CHECK(dc != NULL);
89   // Careful with endianess.
90   int val = 0;
91   memcpy(&val, dc, sizeof(*dc));
92   return static_cast<int>(le32toh(val) & 0xffffff);
93 }
94 
device_class_equals(const bt_device_class_t * p1,const bt_device_class_t * p2)95 bool device_class_equals(const bt_device_class_t* p1,
96                          const bt_device_class_t* p2) {
97   CHECK(p1 != NULL);
98   CHECK(p2 != NULL);
99   return (memcmp(p1, p2, sizeof(bt_device_class_t)) == 0);
100 }
101 
device_class_copy(bt_device_class_t * dest,const bt_device_class_t * src)102 bool device_class_copy(bt_device_class_t* dest, const bt_device_class_t* src) {
103   CHECK(dest != NULL);
104   CHECK(src != NULL);
105   return (memcpy(dest, src, sizeof(bt_device_class_t)) == dest);
106 }
107 
device_class_get_major_device(const bt_device_class_t * dc)108 int device_class_get_major_device(const bt_device_class_t* dc) {
109   CHECK(dc != NULL);
110   return DC(dc)->major_device;
111 }
112 
device_class_set_major_device(bt_device_class_t * dc,int val)113 void device_class_set_major_device(bt_device_class_t* dc, int val) {
114   CHECK(dc != NULL);
115   DC(dc)->major_device = val;
116 }
117 
device_class_get_minor_device(const bt_device_class_t * dc)118 int device_class_get_minor_device(const bt_device_class_t* dc) {
119   CHECK(dc != NULL);
120   return DC(dc)->minor_device;
121 }
122 
device_class_set_minor_device(bt_device_class_t * dc,int val)123 void device_class_set_minor_device(bt_device_class_t* dc, int val) {
124   CHECK(dc != NULL);
125   DC(dc)->minor_device = val;
126 }
127 
device_class_get_information(const bt_device_class_t * dc)128 bool device_class_get_information(const bt_device_class_t* dc) {
129   CHECK(dc != NULL);
130   return device_class_get_major_service_(dc, DC_INFORMATION);
131 }
132 
device_class_set_information(bt_device_class_t * dc,bool set)133 void device_class_set_information(bt_device_class_t* dc, bool set) {
134   CHECK(dc != NULL);
135   if (set)
136     device_class_set_major_service_(dc, DC_INFORMATION);
137   else
138     device_class_clr_major_service_(dc, DC_INFORMATION);
139 }
140 
device_class_get_limited(const bt_device_class_t * dc)141 bool device_class_get_limited(const bt_device_class_t* dc) {
142   CHECK(dc != NULL);
143   return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
144 }
145 
device_class_set_limited(bt_device_class_t * dc,bool set)146 void device_class_set_limited(bt_device_class_t* dc, bool set) {
147   CHECK(dc != NULL);
148   if (set)
149     device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
150   else
151     device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE);
152 }
153 
device_class_get_major_service_(const bt_device_class_t * dc,int bitmask)154 static bool device_class_get_major_service_(const bt_device_class_t* dc,
155                                             int bitmask) {
156   return (DC(dc)->major_service & bitmask);
157 }
158 
device_class_clr_major_service_(bt_device_class_t * dc,int bitmask)159 static void device_class_clr_major_service_(bt_device_class_t* dc,
160                                             int bitmask) {
161   DC(dc)->major_service &= ~bitmask;
162 }
163 
device_class_set_major_service_(bt_device_class_t * dc,int bitmask)164 static void device_class_set_major_service_(bt_device_class_t* dc,
165                                             int bitmask) {
166   DC(dc)->major_service |= bitmask;
167 }
168