OSDN Git Service

am aaf56636: am 1eeb7ae0: am ccd37fa0: Enable enhanced connection complete event
[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 #include "uuid.h"
17
18 #include <algorithm>
19 #include <array>
20 #include <stack>
21 #include <string>
22
23 namespace bluetooth {
24
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};
29 }
30
31 Uuid::Uuid() {
32   InitializeDefault();
33 }
34
35 Uuid::Uuid(const std::string& uuid) {
36   InitializeDefault();
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);
42   }
43 }
44
45 Uuid::Uuid(const bt_uuid_t& uuid) {
46   std::reverse_copy(uuid.uu, uuid.uu + sizeof(uuid.uu), id_.begin());
47 }
48
49 Uuid::Uuid(const Uuid::Uuid16Bit& uuid) {
50   InitializeDefault();
51   std::copy(uuid.begin(), uuid.end(), id_.begin() + kUuid16Octets);
52 }
53
54 Uuid::Uuid(const Uuid::Uuid32Bit& uuid) {
55   InitializeDefault();
56   std::copy(uuid.begin(), uuid.end(), id_.begin());
57 }
58
59 Uuid::Uuid(const Uuid::Uuid128Bit& uuid) : id_(uuid) {}
60
61 const Uuid::Uuid128Bit Uuid::GetFullBigEndian() const {
62   return id_;
63 }
64
65 const Uuid::Uuid128Bit Uuid::GetFullLittleEndian() const {
66   Uuid::Uuid128Bit ret;
67   std::reverse_copy(id_.begin(), id_.end(), ret.begin());
68   return ret;
69 }
70
71 const bt_uuid_t Uuid::GetBlueDroid() const {
72   bt_uuid_t ret;
73   std::reverse_copy(id_.begin(), id_.end(), ret.uu);
74   return ret;
75 }
76
77 bool Uuid::operator<(const Uuid& rhs) const {
78   return std::lexicographical_compare(id_.begin(), id_.end(), rhs.id_.begin(),
79                                       rhs.id_.end());
80 }
81
82 bool Uuid::operator==(const Uuid& rhs) const {
83   return std::equal(id_.begin(), id_.end(), rhs.id_.begin());
84 }
85
86 }  // namespace bluetooth