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 #include <string.h>
23 
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(UUID128Bit{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80,
35                                             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) !=
40       0) {
41     return kNumBytes128;
42   }
43 
44   if (uu[0] == 0 && uu[1] == 0) {
45     return kNumBytes16;
46   }
47 
48   return kNumBytes32;
49 }
50 
Is16Bit() const51 bool Uuid::Is16Bit() const { return GetShortestRepresentationSize() == kNumBytes16; }
52 
As16Bit() const53 uint16_t Uuid::As16Bit() const { return (((uint16_t)uu[2]) << 8) + uu[3]; }
54 
As32Bit() const55 uint32_t Uuid::As32Bit() const {
56   return (((uint32_t)uu[0]) << 24) + (((uint32_t)uu[1]) << 16) + (((uint32_t)uu[2]) << 8) + uu[3];
57 }
58 
FromString(const std::string & uuid)59 std::optional<Uuid> Uuid::FromString(const std::string& uuid) {
60   if (uuid.empty()) {
61     return std::nullopt;
62   }
63 
64   Uuid ret = kBase;
65   uint8_t* p = ret.uu.data();
66   if (uuid.size() == kString128BitLen) {
67     if (uuid[8] != '-' || uuid[13] != '-' || uuid[18] != '-' || uuid[23] != '-') {
68       return std::nullopt;
69     }
70 
71     int c;
72     int rc = sscanf(uuid.c_str(),
73                     "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx"
74                     "-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%n",
75                     &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7], &p[8], &p[9], &p[10],
76                     &p[11], &p[12], &p[13], &p[14], &p[15], &c);
77     if (rc != 16) {
78       return std::nullopt;
79     }
80     if (c != kString128BitLen) {
81       return std::nullopt;
82     }
83 
84   } else if (uuid.size() == 8) {
85     int c;
86     int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%02hhx%02hhx%n", &p[0], &p[1], &p[2], &p[3], &c);
87     if (rc != 4) {
88       return std::nullopt;
89     }
90     if (c != 8) {
91       return std::nullopt;
92     }
93 
94   } else if (uuid.size() == 4) {
95     int c;
96     int rc = sscanf(uuid.c_str(), "%02hhx%02hhx%n", &p[2], &p[3], &c);
97     if (rc != 2) {
98       return std::nullopt;
99     }
100     if (c != 4) {
101       return std::nullopt;
102     }
103   } else {
104     return std::nullopt;
105   }
106 
107   return ret;
108 }
109 
FromLegacyConfigString(const std::string & uuid)110 std::optional<Uuid> Uuid::FromLegacyConfigString(const std::string& uuid) {
111   return FromString(uuid);
112 }
113 
From16Bit(uint16_t uuid16)114 Uuid Uuid::From16Bit(uint16_t uuid16) {
115   Uuid u = kBase;
116 
117   u.uu[2] = (uint8_t)((0xFF00 & uuid16) >> 8);
118   u.uu[3] = (uint8_t)(0x00FF & uuid16);
119   return u;
120 }
121 
From32Bit(uint32_t uuid32)122 Uuid Uuid::From32Bit(uint32_t uuid32) {
123   Uuid u = kBase;
124 
125   u.uu[0] = (uint8_t)((0xFF000000 & uuid32) >> 24);
126   u.uu[1] = (uint8_t)((0x00FF0000 & uuid32) >> 16);
127   u.uu[2] = (uint8_t)((0x0000FF00 & uuid32) >> 8);
128   u.uu[3] = (uint8_t)(0x000000FF & uuid32);
129   return u;
130 }
131 
From128BitBE(const uint8_t * uuid)132 Uuid Uuid::From128BitBE(const uint8_t* uuid) {
133   UUID128Bit tmp;
134   memcpy(tmp.data(), uuid, kNumBytes128);
135   return From128BitBE(tmp);
136 }
137 
From128BitLE(const UUID128Bit & uuid)138 Uuid Uuid::From128BitLE(const UUID128Bit& uuid) {
139   Uuid u;
140   std::reverse_copy(uuid.data(), uuid.data() + kNumBytes128, u.uu.begin());
141   return u;
142 }
143 
From128BitLE(const uint8_t * uuid)144 Uuid Uuid::From128BitLE(const uint8_t* uuid) {
145   UUID128Bit tmp;
146   memcpy(tmp.data(), uuid, kNumBytes128);
147   return From128BitLE(tmp);
148 }
149 
To128BitLE() const150 UUID128Bit Uuid::To128BitLE() const {
151   UUID128Bit le;
152   std::reverse_copy(uu.data(), uu.data() + kNumBytes128, le.begin());
153   return le;
154 }
155 
To128BitBE() const156 const UUID128Bit& Uuid::To128BitBE() const { return uu; }
157 
IsEmpty() const158 bool Uuid::IsEmpty() const { return *this == kEmpty; }
159 
operator <(const Uuid & rhs) const160 bool Uuid::operator<(const Uuid& rhs) const {
161   return std::lexicographical_compare(uu.begin(), uu.end(), rhs.uu.begin(), rhs.uu.end());
162 }
163 
operator ==(const Uuid & rhs) const164 bool Uuid::operator==(const Uuid& rhs) const { return uu == rhs.uu; }
165 
operator !=(const Uuid & rhs) const166 bool Uuid::operator!=(const Uuid& rhs) const { return uu != rhs.uu; }
167 
ToString() const168 std::string Uuid::ToString() const {
169   char buf[kString128BitLen + 1] = {};
170   std::snprintf(buf, sizeof(buf),
171                 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", uu[0],
172                 uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], uu[8], uu[9], uu[10], uu[11],
173                 uu[12], uu[13], uu[14], uu[15]);
174   return std::string(buf);
175 }
176 
ToLegacyConfigString() const177 std::string Uuid::ToLegacyConfigString() const { return ToString(); }
178 
179 }  // namespace hci
180 }  // namespace bluetooth
181