1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup Bluetooth 18 * @{ 19 * 20 * @brief Defines uuid for framework. 21 * 22 * @since 6 23 */ 24 25 /** 26 * @file uuid.h 27 * 28 * @brief framework uuid interface. 29 * 30 * @since 6 31 */ 32 33 #ifndef DUMMY_UUID_H 34 #define DUMMY_UUID_H 35 36 #include "sys/time.h" 37 #include <string> 38 #include <array> 39 #include <ctime> 40 #include <regex> 41 42 namespace OHOS { 43 namespace Bluetooth { 44 const std::regex uuidRegex("^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$"); 45 46 /** 47 * @brief This class provides framework uuid. 48 * 49 * @since 6 50 */ 51 class UUID { 52 public: 53 //128 bits uuid length 54 const static int UUID128_BYTES_LEN = 16; 55 56 /** 57 * @brief A constructor used to create an <b>UUID</b> instance. 58 * 59 * @since 6 60 */ UUID()61 UUID(){}; 62 63 /** 64 * @brief A destructor used to delete the <b>UUID</b> instance. 65 * 66 * @since 6 67 */ ~UUID()68 ~UUID(){}; 69 70 /** 71 * @brief A constructor used to create an <b>UUID</b> instance. Constructor a new UUID using most significant 64 72 * bits and least significant 64 bits. 73 * 74 * @param[in] mostSigBits : The most significant 64 bits of UUID. 75 * @param[in] leastSigBits : The least significant 64 bits of UUID. 76 * @since 6 77 */ UUID(const long mostSigBits,const long leastSigBits)78 UUID(const long mostSigBits, const long leastSigBits) 79 { 80 this->uuid_[15] = (uint8_t)(leastSigBits & 0x00000000000000FF); 81 this->uuid_[14] = (uint8_t)((leastSigBits & 0x000000000000FF00) >> 8); 82 this->uuid_[13] = (uint8_t)((leastSigBits & 0x0000000000FF0000) >> 16); 83 this->uuid_[12] = (uint8_t)((leastSigBits & 0x00000000FF000000) >> 24); 84 this->uuid_[11] = (uint8_t)((leastSigBits & 0x000000FF00000000) >> 32); 85 this->uuid_[10] = (uint8_t)((leastSigBits & 0x0000FF0000000000) >> 40); 86 this->uuid_[9] = (uint8_t)((leastSigBits & 0x00FF000000000000) >> 48); 87 this->uuid_[8] = (uint8_t)((leastSigBits & 0xFF00000000000000) >> 56); 88 this->uuid_[7] = (uint8_t)(mostSigBits & 0x00000000000000FF); 89 this->uuid_[6] = (uint8_t)((mostSigBits & 0x000000000000FF00) >> 8); 90 this->uuid_[5] = (uint8_t)((mostSigBits & 0x0000000000FF0000) >> 16); 91 this->uuid_[4] = (uint8_t)((mostSigBits & 0x00000000FF000000) >> 24); 92 this->uuid_[3] = (uint8_t)((mostSigBits & 0x000000FF00000000) >> 32); 93 this->uuid_[2] = (uint8_t)((mostSigBits & 0x0000FF0000000000) >> 40); 94 this->uuid_[1] = (uint8_t)((mostSigBits & 0x00FF000000000000) >> 48); 95 this->uuid_[0] = (uint8_t)((mostSigBits & 0xFF00000000000000) >> 56); 96 } 97 98 /** 99 * @brief A constructor used to create an <b>UUID</b> instance. Constructor a new UUID from string. 100 * 101 * @param[in] name : The value of string to create UUID. 102 * for example : "00000000-0000-1000-8000-00805F9B34FB" 103 * @return Returns a specified UUID. 104 * @since 6 105 */ FromString(const std::string & name)106 static UUID FromString(const std::string &name) 107 { 108 UUID ret; 109 std::string tmp = name; 110 std::size_t pos = tmp.find("-"); 111 112 while (pos != std::string::npos) { 113 tmp.replace(pos, 1, ""); 114 pos = tmp.find("-"); 115 } 116 117 for (std::size_t i = 0; (i + 1) < tmp.length();) { 118 ret.uuid_[i / 2] = std::stoi(tmp.substr(i, 2), nullptr, 16); 119 i += 2; 120 } 121 122 return ret; 123 } 124 125 /** 126 * @brief A constructor used to create an <b>UUID</b> instance. Constructor a new random UUID. 127 * 128 * @return Returns a random UUID. 129 * @since 6 130 */ RandomUUID()131 static UUID RandomUUID() 132 { 133 UUID random; 134 135 struct timeval tv; 136 struct timezone tz; 137 struct tm randomTime; 138 unsigned int randNum = 0; 139 140 rand_r(&randNum); 141 gettimeofday(&tv, &tz); 142 localtime_r(&tv.tv_sec, &randomTime); 143 144 random.uuid_[15] = (uint8_t)(tv.tv_usec & 0x00000000000000FF); 145 random.uuid_[14] = (uint8_t)((tv.tv_usec & 0x000000000000FF00) >> 8); 146 random.uuid_[13] = (uint8_t)((tv.tv_usec & 0x0000000000FF0000) >> 16); 147 random.uuid_[12] = (uint8_t)((tv.tv_usec & 0x00000000FF000000) >> 24); 148 random.uuid_[10] = (uint8_t)((tv.tv_usec & 0x000000FF00000000) >> 32); 149 random.uuid_[9] = (uint8_t)((tv.tv_usec & 0x0000FF0000000000) >> 40); 150 random.uuid_[8] = (uint8_t)((tv.tv_usec & 0x00FF000000000000) >> 48); 151 random.uuid_[7] = (uint8_t)((tv.tv_usec & 0xFF00000000000000) >> 56); 152 random.uuid_[6] = (uint8_t)((randomTime.tm_sec + randNum) & 0xFF); 153 random.uuid_[5] = (uint8_t)((randomTime.tm_min + (randNum >> 8)) & 0xFF); 154 random.uuid_[4] = (uint8_t)((randomTime.tm_hour + (randNum >> 16)) & 0xFF); 155 random.uuid_[3] = (uint8_t)((randomTime.tm_mday + (randNum >> 24)) & 0xFF); 156 random.uuid_[2] = (uint8_t)(randomTime.tm_mon & 0xFF); 157 random.uuid_[1] = (uint8_t)(randomTime.tm_year & 0xFF); 158 random.uuid_[0] = (uint8_t)((randomTime.tm_year & 0xFF00) >> 8); 159 160 return random; 161 } 162 163 /** 164 * @brief Convert UUID to string. 165 * 166 * @return Returns a String object representing this UUID. 167 * @since 6 168 */ ToString()169 std::string ToString() const 170 { 171 std::string tmp = ""; 172 std::string ret = ""; 173 static const char *hex = "0123456789ABCDEF"; 174 175 for (auto it = this->uuid_.begin(); it != this->uuid_.end(); it++) { 176 tmp.push_back(hex[((*it) >> 4 & 0xF)]); 177 tmp.push_back(hex[(*it) & 0xF]); 178 } 179 180 ret = tmp.substr(0, 8) + "-" + tmp.substr(8, 4) + "-" + tmp.substr(12, 4) + "-" + tmp.substr(16, 4) + "-" + 181 tmp.substr(20); 182 183 return ret; 184 } 185 186 /** 187 * @brief Compares this UUID with the specified UUID. 188 * 189 * @param[in] val : UUID which this UUID is to be compared. 190 * @return Returns <b> <0 </b> if this UUID is less than compared UUID; 191 * returns <b> =0 </b> if this UUID is equal to compared UUID; 192 * returns <b> >0 </b> if this UUID is greater than compared UUID. 193 * @since 6 194 */ CompareTo(const UUID & val)195 int CompareTo(const UUID &val) const 196 { 197 UUID tmp = val; 198 return this->ToString().compare(tmp.ToString()); 199 } 200 201 /** 202 * @brief Compares this object to the specified object. 203 * 204 * @param[in] val : UUID which this UUID is to be compared. 205 * @return Returns <b>true</b> if this UUID is the same as compared UUID; 206 * returns <b>false</b> if this UUID is not the same as compared UUID. 207 * @since 6 208 */ Equals(const UUID & val)209 bool Equals(const UUID &val) const 210 { 211 for (int i = 0; i < UUID128_BYTES_LEN; i++) { 212 if (this->uuid_[i] != val.uuid_[i]) { 213 return false; 214 } 215 } 216 return true; 217 } 218 219 /** 220 * @brief Returns the least significant 64 bits of this UUID's 128 bit value. 221 * 222 * @return Retruns the least significant 64 bits of this UUID's 128 bit value. 223 * @since 6 224 */ GetLeastSignificantBits()225 uint64_t GetLeastSignificantBits() const 226 { 227 uint64_t leastSigBits = 0; 228 for (int i = UUID128_BYTES_LEN / 2; i < UUID128_BYTES_LEN; i++) { 229 leastSigBits = (leastSigBits << 8) | (uuid_[i] & 0xFF); 230 } 231 return leastSigBits; 232 } 233 234 /** 235 * @brief Returns the most significant 64 bits of this UUID's 128 bit value. 236 * 237 * @return Returns the most significant 64 bits of this UUID's 128 bit value. 238 * @since 6 239 */ GetMostSignificantBits()240 uint64_t GetMostSignificantBits() const 241 { 242 uint64_t mostSigBits = 0; 243 for (int i = 0 / 2; i < UUID128_BYTES_LEN / 2; i++) { 244 mostSigBits = (mostSigBits << 8) | (uuid_[i] & 0xFF); 245 } 246 return mostSigBits; 247 } 248 249 /** 250 * @brief Constructor a new UUID from uint8_t array. 251 * 252 * @param[in] name : The 128 bits value for a UUID. 253 * @return Returns a specified UUID. 254 * @since 6 255 */ ConvertFrom128Bits(const std::array<uint8_t,UUID128_BYTES_LEN> & name)256 static UUID ConvertFrom128Bits(const std::array<uint8_t, UUID128_BYTES_LEN> &name) 257 { 258 UUID tmp; 259 for (int i = 0; i < UUID128_BYTES_LEN; i++) { 260 tmp.uuid_[i] = name[i]; 261 } 262 return tmp; 263 } 264 265 /** 266 * @brief Returns uint8_t array from UUID. 267 * 268 * @return returns a specified array. 269 * @since 6 270 */ ConvertTo128Bits()271 std::array<uint8_t, UUID128_BYTES_LEN> ConvertTo128Bits() const 272 { 273 std::array<uint8_t, UUID128_BYTES_LEN> uuid; 274 275 for (int i = 0; i < UUID128_BYTES_LEN; i++) { 276 uuid[i] = uuid_[i]; 277 } 278 279 return uuid; 280 } 281 282 /** 283 * @brief In order to use the object key in the map object, overload the operator <. 284 * @param[in] uuid : UUID object. 285 * @return @c bool : If the object uuid is the same, return true, otherwise return false. 286 */ 287 bool operator<(const UUID &uuid) const 288 { 289 return !Equals(uuid); 290 } 291 292 private: 293 std::array<uint8_t, UUID128_BYTES_LEN> uuid_ = {0x00}; 294 }; 295 296 /** 297 * @brief This class provides framework ParcelUuid. 298 * 299 * @since 6 300 */ 301 using ParcelUuid = UUID; 302 303 } // namespace Bluetooth 304 } // namespace OHOS 305 306 #endif //DUMMY_UUID_H