2 // Copyright (C) 2015 Google, Inc.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at:
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include "service/uuid.h"
24 #include <base/rand_util.h>
29 UUID UUID::GetRandom() {
31 base::RandBytes(bytes.data(), bytes.size());
35 void UUID::InitializeDefault() {
36 // Initialize to base bluetooth UUID.
37 id_ = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
38 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}};
45 UUID::UUID(const std::string& uuid) {
47 const int start_index = uuid.size() == 4 ? 2 : 0;
48 const size_t copy_size = std::min(id_.size(), uuid.size() / 2);
49 for (size_t i = 0; i < copy_size; ++i) {
50 std::string octet_text(uuid, i * 2, 2);
51 id_[start_index + i] = std::stoul(octet_text, 0, 16);
55 UUID::UUID(const bt_uuid_t& uuid) {
56 std::reverse_copy(uuid.uu, uuid.uu + sizeof(uuid.uu), id_.begin());
59 UUID::UUID(const UUID16Bit& uuid) {
61 std::copy(uuid.begin(), uuid.end(), id_.begin() + kNumBytes16);
64 UUID::UUID(const UUID32Bit& uuid) {
66 std::copy(uuid.begin(), uuid.end(), id_.begin());
69 UUID::UUID(const UUID128Bit& uuid) : id_(uuid) {}
71 const UUID::UUID128Bit UUID::GetFullBigEndian() const {
75 const UUID::UUID128Bit UUID::GetFullLittleEndian() const {
77 std::reverse_copy(id_.begin(), id_.end(), ret.begin());
81 const bt_uuid_t UUID::GetBlueDroid() const {
83 std::reverse_copy(id_.begin(), id_.end(), ret.uu);
87 bool UUID::operator<(const UUID& rhs) const {
88 return std::lexicographical_compare(id_.begin(), id_.end(), rhs.id_.begin(),
92 bool UUID::operator==(const UUID& rhs) const {
93 return std::equal(id_.begin(), id_.end(), rhs.id_.begin());
96 } // namespace bluetooth