1 /******************************************************************************
2  *
3  *  Copyright (C) 2017 The Android Open Source Project
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 "hci/uuid.h"
20 
21 #include <openssl/rand.h>
22 
23 #include <string.h>
24 #include <algorithm>
25 
26 namespace bluetooth {
27 namespace hci {
28 
29 using UUID128Bit = Uuid::UUID128Bit;
30 
31 const Uuid Uuid::kEmpty = Uuid::From128BitBE(UUID128Bit{{0x00}});
32 
33 namespace {
34 Uuid kBase = Uuid::From128BitBE(
35     UUID128Bit{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}});
36 }  // namespace
37 
GetShortestRepresentationSize() const38 size_t Uuid::GetShortestRepresentationSize() const {
39   if (memcmp(uu.data() + kNumBytes32, kBase.uu.data() + kNumBytes32, kNumBytes128 - kNumBytes32) != 0) {
40     return kNumBytes128;
41   }
42 
43   if (uu[0] == 0 && uu[1] == 0) {
44     return kNumBytes16;
45   }
46 
47   return kNumBytes32;
48 }
49 
Is16Bit() const50 bool Uuid::Is16Bit() const {
51   return GetShortestRepresentationSize() == kNumBytes16;
52 }
53 
As16Bit() const54 uint16_t Uuid::As16Bit() const {
55   return (((uint16_t)uu[2]) << 8) + uu[3];
56 }
57 
As32Bit() const58 uint32_t Uuid::As32Bit() const {
59   return (((uint32_t)uu[0]) << 24) + (((uint32_t)uu[1]) << 16) + (((uint32_t)uu[2]) << 8) + uu[3];
60 }
61 
FromString(const std::string & uuid)62 std::optional<Uuid> Uuid::FromString(const std::string& uuid) {
63   if (uuid.empty()) {
64     return std::nullopt;
65   }
66 
67   Uuid ret = kBase;
68   uint8_t* p = ret.uu.data();
69   if (uuid.size() == kString128BitLen) {
70     if (uuid[8] != '-' || uuid[13] != '-' || uuid[18] != '-' || uuid[23] != '-') {
71       return std::nullopt;
72     }
73 
74     int c;
75     int rc = sscanf(
76         uuid.c_str(),
77         "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx"
78         "-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%n",
79         &p[0],
80         &p[1],
81         &p[2],
82         &p[3],
83         &p[4],
84         &p[5],
85         &p[6],
86         &p[7],
87         &p[8],
88         &p[9],
89         &p[10],
90         &p[11],
91         &p[12],
92         &p[13],
93         &p[14],
94         &p[15],
95         &c);
96     if (rc != 16) {
97       return std::nullopt;
98     }
99     if (c != kString128BitLen) {
100       return std::nullopt;
101     }
102 
103   } else if (uuid.size() == 8) {
104     int c;
105     int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%02hhx%02hhx%n", &p[0], &p[1], &p[2], &p[3], &c);
106     if (rc != 4) {
107       return std::nullopt;
108     }
109     if (c != 8) {
110       return std::nullopt;
111     }
112 
113   } else if (uuid.size() == 4) {
114     int c;
115     int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%n", &p[2], &p[3], &c);
116     if (rc != 2) {
117       return std::nullopt;
118     }
119     if (c != 4) {
120       return std::nullopt;
121     }
122   } else {
123     return std::nullopt;
124   }
125 
126   return ret;
127 }
128 
FromLegacyConfigString(const std::string & uuid)129 std::optional<Uuid> Uuid::FromLegacyConfigString(const std::string& uuid) {
130   return FromString(uuid);
131 }
132 
From16Bit(uint16_t uuid16)133 Uuid Uuid::From16Bit(uint16_t uuid16) {
134   Uuid u = kBase;
135 
136   u.uu[2] = (uint8_t)((0xFF00 & uuid16) >> 8);
137   u.uu[3] = (uint8_t)(0x00FF & uuid16);
138   return u;
139 }
140 
From32Bit(uint32_t uuid32)141 Uuid Uuid::From32Bit(uint32_t uuid32) {
142   Uuid u = kBase;
143 
144   u.uu[0] = (uint8_t)((0xFF000000 & uuid32) >> 24);
145   u.uu[1] = (uint8_t)((0x00FF0000 & uuid32) >> 16);
146   u.uu[2] = (uint8_t)((0x0000FF00 & uuid32) >> 8);
147   u.uu[3] = (uint8_t)(0x000000FF & uuid32);
148   return u;
149 }
150 
From128BitBE(const uint8_t * uuid)151 Uuid Uuid::From128BitBE(const uint8_t* uuid) {
152   UUID128Bit tmp;
153   memcpy(tmp.data(), uuid, kNumBytes128);
154   return From128BitBE(tmp);
155 }
156 
From128BitLE(const UUID128Bit & uuid)157 Uuid Uuid::From128BitLE(const UUID128Bit& uuid) {
158   Uuid u;
159   std::reverse_copy(uuid.data(), uuid.data() + kNumBytes128, u.uu.begin());
160   return u;
161 }
162 
From128BitLE(const uint8_t * uuid)163 Uuid Uuid::From128BitLE(const uint8_t* uuid) {
164   UUID128Bit tmp;
165   memcpy(tmp.data(), uuid, kNumBytes128);
166   return From128BitLE(tmp);
167 }
168 
To128BitLE() const169 UUID128Bit Uuid::To128BitLE() const {
170   UUID128Bit le;
171   std::reverse_copy(uu.data(), uu.data() + kNumBytes128, le.begin());
172   return le;
173 }
174 
To128BitBE() const175 const UUID128Bit& Uuid::To128BitBE() const {
176   return uu;
177 }
178 
GetRandom()179 Uuid Uuid::GetRandom() {
180   Uuid uuid;
181   RAND_bytes(uuid.uu.data(), uuid.uu.size());
182   return uuid;
183 }
184 
IsEmpty() const185 bool Uuid::IsEmpty() const {
186   return *this == kEmpty;
187 }
188 
operator <(const Uuid & rhs) const189 bool Uuid::operator<(const Uuid& rhs) const {
190   return std::lexicographical_compare(uu.begin(), uu.end(), rhs.uu.begin(), rhs.uu.end());
191 }
192 
operator ==(const Uuid & rhs) const193 bool Uuid::operator==(const Uuid& rhs) const {
194   return uu == rhs.uu;
195 }
196 
operator !=(const Uuid & rhs) const197 bool Uuid::operator!=(const Uuid& rhs) const {
198   return uu != rhs.uu;
199 }
200 
ToString() const201 std::string Uuid::ToString() const {
202   char buf[kString128BitLen + 1] = {};
203   std::snprintf(
204       buf,
205       sizeof(buf),
206       "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
207       uu[0],
208       uu[1],
209       uu[2],
210       uu[3],
211       uu[4],
212       uu[5],
213       uu[6],
214       uu[7],
215       uu[8],
216       uu[9],
217       uu[10],
218       uu[11],
219       uu[12],
220       uu[13],
221       uu[14],
222       uu[15]);
223   return std::string(buf);
224 }
225 
ToLegacyConfigString() const226 std::string Uuid::ToLegacyConfigString() const {
227   return ToString();
228 }
229 
230 }  // namespace hci
231 }  // namespace bluetooth
232