OSDN Git Service

resolved conflicts for b8cc54d1 to mnc-dr-dev-plus-aosp
[android-x86/system-bt.git] / service / uuid.cpp
1 //
2 //  Copyright (C) 2015 Google, Inc.
3 //
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:
7 //
8 //  http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 #include "service/uuid.h"
18
19 #include <algorithm>
20 #include <array>
21 #include <stack>
22 #include <string>
23
24 #include <base/rand_util.h>
25
26 namespace bluetooth {
27
28 // static
29 UUID UUID::GetRandom() {
30   UUID128Bit bytes;
31   base::RandBytes(bytes.data(), bytes.size());
32   return UUID(bytes);
33 }
34
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}};
39 }
40
41 UUID::UUID() {
42   InitializeDefault();
43 }
44
45 UUID::UUID(const std::string& uuid) {
46   InitializeDefault();
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);
52   }
53 }
54
55 UUID::UUID(const bt_uuid_t& uuid) {
56   std::reverse_copy(uuid.uu, uuid.uu + sizeof(uuid.uu), id_.begin());
57 }
58
59 UUID::UUID(const UUID16Bit& uuid) {
60   InitializeDefault();
61   std::copy(uuid.begin(), uuid.end(), id_.begin() + kNumBytes16);
62 }
63
64 UUID::UUID(const UUID32Bit& uuid) {
65   InitializeDefault();
66   std::copy(uuid.begin(), uuid.end(), id_.begin());
67 }
68
69 UUID::UUID(const UUID128Bit& uuid) : id_(uuid) {}
70
71 const UUID::UUID128Bit UUID::GetFullBigEndian() const {
72   return id_;
73 }
74
75 const UUID::UUID128Bit UUID::GetFullLittleEndian() const {
76   UUID::UUID128Bit ret;
77   std::reverse_copy(id_.begin(), id_.end(), ret.begin());
78   return ret;
79 }
80
81 const bt_uuid_t UUID::GetBlueDroid() const {
82   bt_uuid_t ret;
83   std::reverse_copy(id_.begin(), id_.end(), ret.uu);
84   return ret;
85 }
86
87 bool UUID::operator<(const UUID& rhs) const {
88   return std::lexicographical_compare(id_.begin(), id_.end(), rhs.id_.begin(),
89                                       rhs.id_.end());
90 }
91
92 bool UUID::operator==(const UUID& rhs) const {
93   return std::equal(id_.begin(), id_.end(), rhs.id_.begin());
94 }
95
96 }  // namespace bluetooth