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.
25 void Uuid::InitializeDefault() {
26 // Initialize to base bluetooth UUID.
27 id_ = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
28 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
35 Uuid::Uuid(const std::string& uuid) {
37 const int start_index = uuid.size() == 4 ? 2 : 0;
38 const size_t copy_size = std::min(id_.size(), uuid.size() / 2);
39 for (size_t i = 0; i < copy_size; ++i) {
40 std::string octet_text(uuid, i * 2, 2);
41 id_[start_index + i] = std::stoul(octet_text, 0, 16);
45 Uuid::Uuid(const bt_uuid_t& uuid) {
46 std::reverse_copy(uuid.uu, uuid.uu + sizeof(uuid.uu), id_.begin());
49 Uuid::Uuid(const Uuid::Uuid16Bit& uuid) {
51 std::copy(uuid.begin(), uuid.end(), id_.begin() + kUuid16Octets);
54 Uuid::Uuid(const Uuid::Uuid32Bit& uuid) {
56 std::copy(uuid.begin(), uuid.end(), id_.begin());
59 Uuid::Uuid(const Uuid::Uuid128Bit& uuid) : id_(uuid) {}
61 const Uuid::Uuid128Bit Uuid::GetFullBigEndian() const {
65 const Uuid::Uuid128Bit Uuid::GetFullLittleEndian() const {
67 std::reverse_copy(id_.begin(), id_.end(), ret.begin());
71 const bt_uuid_t Uuid::GetBlueDroid() const {
73 std::reverse_copy(id_.begin(), id_.end(), ret.uu);
77 bool Uuid::operator<(const Uuid& rhs) const {
78 return std::lexicographical_compare(id_.begin(), id_.end(), rhs.id_.begin(),
82 bool Uuid::operator==(const Uuid& rhs) const {
83 return std::equal(id_.begin(), id_.end(), rhs.id_.begin());
86 } // namespace bluetooth