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