195489c19Sopenharmony_ci/* 295489c19Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 495489c19Sopenharmony_ci * you may not use this file except in compliance with the License. 595489c19Sopenharmony_ci * You may obtain a copy of the License at 695489c19Sopenharmony_ci * 795489c19Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 895489c19Sopenharmony_ci * 995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and 1395489c19Sopenharmony_ci * limitations under the License. 1495489c19Sopenharmony_ci */ 1595489c19Sopenharmony_ci 1695489c19Sopenharmony_ci#ifndef BT_UUID_H 1795489c19Sopenharmony_ci#define BT_UUID_H 1895489c19Sopenharmony_ci 1995489c19Sopenharmony_ci#include <cstddef> 2095489c19Sopenharmony_ci#include <cstdint> 2195489c19Sopenharmony_ci#include <array> 2295489c19Sopenharmony_ci#include "cstdint" 2395489c19Sopenharmony_ci#include "iosfwd" 2495489c19Sopenharmony_ci#include <string> 2595489c19Sopenharmony_ci 2695489c19Sopenharmony_ci/** 2795489c19Sopenharmony_ci * @brief The bluetooth subsystem. 2895489c19Sopenharmony_ci */ 2995489c19Sopenharmony_cinamespace OHOS { 3095489c19Sopenharmony_cinamespace bluetooth { 3195489c19Sopenharmony_ci/** 3295489c19Sopenharmony_ci * @brief This class provides service uuid. 3395489c19Sopenharmony_ci * 3495489c19Sopenharmony_ci * @since 6 3595489c19Sopenharmony_ci */ 3695489c19Sopenharmony_ciclass Uuid { 3795489c19Sopenharmony_cipublic: 3895489c19Sopenharmony_ci // 128 bits uuid length type 3995489c19Sopenharmony_ci const static int UUID128_BYTES_TYPE = 16; 4095489c19Sopenharmony_ci // 32 bits uuid length 4195489c19Sopenharmony_ci const static int UUID32_BYTES_TYPE = 4; 4295489c19Sopenharmony_ci // 16 bits uuid length 4395489c19Sopenharmony_ci const static int UUID16_BYTES_TYPE = 2; 4495489c19Sopenharmony_ci using UUID128Bit = std::array<uint8_t, UUID128_BYTES_TYPE>; 4595489c19Sopenharmony_ci 4695489c19Sopenharmony_ci const static int UUID_TIME_LOW_FIRST_BYTE = 0; 4795489c19Sopenharmony_ci const static int UUID_TIME_LOW_SECEND_BYTE = 1; 4895489c19Sopenharmony_ci const static int UUID_TIME_LOW_THIRD_BYTE = 2; 4995489c19Sopenharmony_ci const static int UUID_TIME_LOW_FOURTH_BYTE = 3; 5095489c19Sopenharmony_ci const static int UUID_TIME_MID_FIRST_BYTE = 4; 5195489c19Sopenharmony_ci const static int UUID_TIME_MID_SECEND_BYTE = 5; 5295489c19Sopenharmony_ci const static int UUID_VERSION = 6; 5395489c19Sopenharmony_ci const static int UUID_TIME_HIGH = 7; 5495489c19Sopenharmony_ci const static int UUID_VARIANT = 8; 5595489c19Sopenharmony_ci const static int UUID_CLOCK_SEQ = 9; 5695489c19Sopenharmony_ci const static int UUID_NODE_FIRST_BYTE = 10; 5795489c19Sopenharmony_ci const static int UUID_NODE_SECEND_BYTE = 11; 5895489c19Sopenharmony_ci const static int UUID_NODE_THIRD_BYTE = 12; 5995489c19Sopenharmony_ci const static int UUID_NODE_FOURTH_BYTE = 13; 6095489c19Sopenharmony_ci const static int UUID_NODE_FIFTH_BYTE = 14; 6195489c19Sopenharmony_ci const static int UUID_NODE_SIXTH_BYTE = 15; 6295489c19Sopenharmony_ci 6395489c19Sopenharmony_ci const static int BASE_BIT_OPT_SIZE = 8; 6495489c19Sopenharmony_ci const static int BIT_OPT_TWO_BYTE = 2; 6595489c19Sopenharmony_ci const static int BIT_OPT_THREE_BYTE = 3; 6695489c19Sopenharmony_ci const static int BIT_OPT_FOUR_BYTE = 4; 6795489c19Sopenharmony_ci const static int BIT_OPT_FIVE_BYTE = 5; 6895489c19Sopenharmony_ci const static int BIT_OPT_SIX_BYTE = 6; 6995489c19Sopenharmony_ci const static int BIT_OPT_SEVEN_BYTE = 7; 7095489c19Sopenharmony_ci 7195489c19Sopenharmony_ci const static int SIZE_STRING_TO_INT = 2; 7295489c19Sopenharmony_ci /** 7395489c19Sopenharmony_ci * @brief A constructor used to create an <b>UUID</b> instance. 7495489c19Sopenharmony_ci * 7595489c19Sopenharmony_ci * @since 6 7695489c19Sopenharmony_ci */ 7795489c19Sopenharmony_ci Uuid() = default; 7895489c19Sopenharmony_ci 7995489c19Sopenharmony_ci /** 8095489c19Sopenharmony_ci * @brief A constructor used to create an <b>UUID</b> instance. 8195489c19Sopenharmony_ci * 8295489c19Sopenharmony_ci * @param other Other uuid to create an <b>UUID</b> instance. 8395489c19Sopenharmony_ci * @since 6 8495489c19Sopenharmony_ci */ 8595489c19Sopenharmony_ci Uuid(const Uuid &other) = default; 8695489c19Sopenharmony_ci 8795489c19Sopenharmony_ci /** 8895489c19Sopenharmony_ci * @brief The assignment constructor. 8995489c19Sopenharmony_ci * 9095489c19Sopenharmony_ci * @param other Other uuid object. 9195489c19Sopenharmony_ci * @return Returns the reference of Uuid. 9295489c19Sopenharmony_ci * @since 6 9395489c19Sopenharmony_ci */ 9495489c19Sopenharmony_ci Uuid &operator=(const Uuid &other) = default; 9595489c19Sopenharmony_ci 9695489c19Sopenharmony_ci /** 9795489c19Sopenharmony_ci * @brief A destructor used to delete the <b>UUID</b> instance. 9895489c19Sopenharmony_ci * 9995489c19Sopenharmony_ci * @since 6 10095489c19Sopenharmony_ci */ 10195489c19Sopenharmony_ci ~Uuid() = default; 10295489c19Sopenharmony_ci 10395489c19Sopenharmony_ci /** 10495489c19Sopenharmony_ci * @brief Create a random uuid. 10595489c19Sopenharmony_ci * 10695489c19Sopenharmony_ci * @return @c Uuid : The function return a random uuid. 10795489c19Sopenharmony_ci */ 10895489c19Sopenharmony_ci static Uuid Random(); 10995489c19Sopenharmony_ci 11095489c19Sopenharmony_ci /** 11195489c19Sopenharmony_ci * @brief Constructor a new Uuid from string. 11295489c19Sopenharmony_ci * 11395489c19Sopenharmony_ci * @param name The value of string to create Uuid. 11495489c19Sopenharmony_ci * for example : "00000000-0000-1000-8000-00805F9B34FB" 11595489c19Sopenharmony_ci * @return Returns a specified Uuid. 11695489c19Sopenharmony_ci * @since 6 11795489c19Sopenharmony_ci */ 11895489c19Sopenharmony_ci static Uuid ConvertFromString(const std::string &name); 11995489c19Sopenharmony_ci 12095489c19Sopenharmony_ci /** 12195489c19Sopenharmony_ci * @brief Create a new uuid from uint16_t. 12295489c19Sopenharmony_ci * 12395489c19Sopenharmony_ci * @param uuid The 16 bits is a part of 128 bits. 12495489c19Sopenharmony_ci * @return Returns a new uuid. 12595489c19Sopenharmony_ci * @since 6 12695489c19Sopenharmony_ci */ 12795489c19Sopenharmony_ci static Uuid ConvertFrom16Bits(uint16_t uuid); 12895489c19Sopenharmony_ci 12995489c19Sopenharmony_ci /** 13095489c19Sopenharmony_ci * @brief Create a new uuid from uint32_t. 13195489c19Sopenharmony_ci * 13295489c19Sopenharmony_ci * @param uuid The 32 bits is a part of 128 bits. 13395489c19Sopenharmony_ci * @return Return a new uuid. 13495489c19Sopenharmony_ci * @since 6 13595489c19Sopenharmony_ci */ 13695489c19Sopenharmony_ci static Uuid ConvertFrom32Bits(uint32_t uuid); 13795489c19Sopenharmony_ci 13895489c19Sopenharmony_ci /** 13995489c19Sopenharmony_ci * @brief Create a new uuid from little endian bytes. 14095489c19Sopenharmony_ci * 14195489c19Sopenharmony_ci * @param uuid The 128 bits value for a uuid. 14295489c19Sopenharmony_ci * @return Returns a new uuid. 14395489c19Sopenharmony_ci * @since 6 14495489c19Sopenharmony_ci */ 14595489c19Sopenharmony_ci static Uuid ConvertFromBytesLE(const uint8_t *uuid, const size_t size = 16); 14695489c19Sopenharmony_ci 14795489c19Sopenharmony_ci /** 14895489c19Sopenharmony_ci * @brief Create a new uuid by most significant 64 bits and least signigicant 64 bits. 14995489c19Sopenharmony_ci * 15095489c19Sopenharmony_ci * @param uuid The 128 bits value for a uuid. 15195489c19Sopenharmony_ci * @return Returns a new uuid. 15295489c19Sopenharmony_ci * @since 6 15395489c19Sopenharmony_ci */ 15495489c19Sopenharmony_ci static Uuid ConvertFromMostAndLeastBit(uint64_t mostSigBits, uint64_t leastSigBits); 15595489c19Sopenharmony_ci 15695489c19Sopenharmony_ci /** 15795489c19Sopenharmony_ci * @brief Create a new uuid from uint8_t array. 15895489c19Sopenharmony_ci * 15995489c19Sopenharmony_ci * @param uuid The 128 bits value for a uuid. 16095489c19Sopenharmony_ci * @return Returns a uuid from uint8_t array. 16195489c19Sopenharmony_ci * @since 6 16295489c19Sopenharmony_ci */ 16395489c19Sopenharmony_ci static Uuid ConvertFrom128Bits(const UUID128Bit &uuid); 16495489c19Sopenharmony_ci 16595489c19Sopenharmony_ci /** 16695489c19Sopenharmony_ci * @brief Convert uuid to 16 bits. 16795489c19Sopenharmony_ci * 16895489c19Sopenharmony_ci * @return Returns a uint16_t value from uuid. 16995489c19Sopenharmony_ci * @since 6 17095489c19Sopenharmony_ci */ 17195489c19Sopenharmony_ci uint16_t ConvertTo16Bits() const; 17295489c19Sopenharmony_ci 17395489c19Sopenharmony_ci /** 17495489c19Sopenharmony_ci * @brief Convert uuid to 32 bits. 17595489c19Sopenharmony_ci * 17695489c19Sopenharmony_ci * @return Returns a uint32_t value from uuid. 17795489c19Sopenharmony_ci * @since 6 17895489c19Sopenharmony_ci */ 17995489c19Sopenharmony_ci uint32_t ConvertTo32Bits() const; 18095489c19Sopenharmony_ci 18195489c19Sopenharmony_ci /** 18295489c19Sopenharmony_ci * @brief Convert uuid to uint8_t* with little endian. 18395489c19Sopenharmony_ci * 18495489c19Sopenharmony_ci * @param[in] value : The 128 bits value for a uuid. 18595489c19Sopenharmony_ci * @return Returns <b>true</b> if the operation is successful; 18695489c19Sopenharmony_ci * returns <b>false</b> if the operation fails. 18795489c19Sopenharmony_ci * @since 6 18895489c19Sopenharmony_ci */ 18995489c19Sopenharmony_ci bool ConvertToBytesLE(uint8_t *value, const size_t size = 16) const; 19095489c19Sopenharmony_ci 19195489c19Sopenharmony_ci /** 19295489c19Sopenharmony_ci * @brief Convert uuid to uint8_t array. 19395489c19Sopenharmony_ci * 19495489c19Sopenharmony_ci * @return Returns a new uuid. 19595489c19Sopenharmony_ci * @since 6 19695489c19Sopenharmony_ci */ 19795489c19Sopenharmony_ci UUID128Bit ConvertTo128Bits() const; 19895489c19Sopenharmony_ci 19995489c19Sopenharmony_ci /** 20095489c19Sopenharmony_ci * @brief Get UUID type: 16 bits or 32 bits or 128 bits 20195489c19Sopenharmony_ci * 20295489c19Sopenharmony_ci * @return Returns uuid type. 20395489c19Sopenharmony_ci * UUID128_BYTES_TYPE : 128 bits uuid. 20495489c19Sopenharmony_ci * UUID32_BYTES_TYPE : 32 bits uuid. 20595489c19Sopenharmony_ci * UUID16_BYTES_TYPE : 16 bits uuid. 20695489c19Sopenharmony_ci * @since 6 20795489c19Sopenharmony_ci */ 20895489c19Sopenharmony_ci int GetUuidType() const; 20995489c19Sopenharmony_ci 21095489c19Sopenharmony_ci /** 21195489c19Sopenharmony_ci * @brief Compare two uuid whether are same or not. 21295489c19Sopenharmony_ci * 21395489c19Sopenharmony_ci * @param rhs Compared UUID instance. 21495489c19Sopenharmony_ci * @return Returns <b>true</b> if this UUID is the same as compared UUID; 21595489c19Sopenharmony_ci * returns <b>false</b> if this UUID is not the same as compared UUID. 21695489c19Sopenharmony_ci * @since 6 21795489c19Sopenharmony_ci */ 21895489c19Sopenharmony_ci bool operator==(const Uuid &rhs) const; 21995489c19Sopenharmony_ci 22095489c19Sopenharmony_ci /** 22195489c19Sopenharmony_ci * @brief Compare two uuid whether are different or not. 22295489c19Sopenharmony_ci * 22395489c19Sopenharmony_ci * @param rhs Compared UUID instance. 22495489c19Sopenharmony_ci * @return Returns <b>true</b> if this UUID is different as compared UUID; 22595489c19Sopenharmony_ci * returns <b>false</b> if this UUID is not different as compared UUID. 22695489c19Sopenharmony_ci * @since 6 22795489c19Sopenharmony_ci */ 22895489c19Sopenharmony_ci bool operator!=(const Uuid &rhs) const; 22995489c19Sopenharmony_ci 23095489c19Sopenharmony_ci /** 23195489c19Sopenharmony_ci * @brief In order to use the object key in the map object, overload the operator <. 23295489c19Sopenharmony_ci * @param[in] uuid : Uuid object. 23395489c19Sopenharmony_ci * @return @c bool : If the object uuid is the same, return true, otherwise return false. 23495489c19Sopenharmony_ci */ 23595489c19Sopenharmony_ci bool operator<(const Uuid &uuid) const 23695489c19Sopenharmony_ci { 23795489c19Sopenharmony_ci return *this != uuid; 23895489c19Sopenharmony_ci } 23995489c19Sopenharmony_ci 24095489c19Sopenharmony_ci /** 24195489c19Sopenharmony_ci * @brief Convert UUID to string. 24295489c19Sopenharmony_ci * 24395489c19Sopenharmony_ci * @return Returns a String object representing this UUID. 24495489c19Sopenharmony_ci * @since 6 24595489c19Sopenharmony_ci */ 24695489c19Sopenharmony_ci std::string ToString() const 24795489c19Sopenharmony_ci { 24895489c19Sopenharmony_ci std::string tmp = ""; 24995489c19Sopenharmony_ci std::string ret = ""; 25095489c19Sopenharmony_ci static const char *hex = "0123456789ABCDEF"; 25195489c19Sopenharmony_ci const uint8_t size4 = 4; 25295489c19Sopenharmony_ci const uint8_t pos8 = 8; 25395489c19Sopenharmony_ci const uint8_t pos12 = 12; 25495489c19Sopenharmony_ci const uint8_t pos16 = 16; 25595489c19Sopenharmony_ci const uint8_t pos20 = 20; 25695489c19Sopenharmony_ci 25795489c19Sopenharmony_ci for (auto it = this->uuid_.begin(); it != this->uuid_.end(); ++it) { 25895489c19Sopenharmony_ci tmp.push_back(hex[(((*it) >> size4) & 0xF)]); 25995489c19Sopenharmony_ci tmp.push_back(hex[(*it) & 0xF]); 26095489c19Sopenharmony_ci } 26195489c19Sopenharmony_ci // 00000000-0000-1000-8000-00805F9B34FB 26295489c19Sopenharmony_ci ret = tmp.substr(0, pos8) + "-" + tmp.substr(pos8, size4) + "-" + tmp.substr(pos12, size4) + "-" + 26395489c19Sopenharmony_ci tmp.substr(pos16, size4) + "-" + tmp.substr(pos20); 26495489c19Sopenharmony_ci 26595489c19Sopenharmony_ci return ret; 26695489c19Sopenharmony_ci } 26795489c19Sopenharmony_ci 26895489c19Sopenharmony_ciprotected: 26995489c19Sopenharmony_ci /** 27095489c19Sopenharmony_ci * @brief Constructor. 27195489c19Sopenharmony_ci */ 27295489c19Sopenharmony_ci explicit Uuid(const UUID128Bit uuid) : uuid_(uuid) {}; 27395489c19Sopenharmony_ci 27495489c19Sopenharmony_ci // base uuid value 27595489c19Sopenharmony_ci std::array<uint8_t, UUID128_BYTES_TYPE> BASE_UUID = { 27695489c19Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 27795489c19Sopenharmony_ci 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB 27895489c19Sopenharmony_ci }; 27995489c19Sopenharmony_ci 28095489c19Sopenharmony_ci std::array<uint8_t, UUID128_BYTES_TYPE> uuid_ = BASE_UUID; 28195489c19Sopenharmony_ci}; 28295489c19Sopenharmony_ci} // namespace bluetooth 28395489c19Sopenharmony_ci} // namespace OHOS 28495489c19Sopenharmony_ci 28595489c19Sopenharmony_ci#endif // BT_UUID_H