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 #pragma once
18 
19 #include <array>
20 #include <string>
21 
22 // TODO: Find places that break include what you use and remove this.
23 #include <base/logging.h>
24 #include <hardware/bluetooth.h>
25 
26 namespace bluetooth {
27 
28 class UUID {
29  public:
30   static constexpr size_t kNumBytes128 = 16;
31   static constexpr size_t kNumBytes32 = 4;
32   static constexpr size_t kNumBytes16 = 2;
33 
34   typedef std::array<uint8_t, kNumBytes16> UUID16Bit;
35   typedef std::array<uint8_t, kNumBytes32> UUID32Bit;
36   typedef std::array<uint8_t, kNumBytes128> UUID128Bit;
37 
38   // Creates and returns a random 128-bit UUID.
39   static UUID GetRandom();
40 
41   // Creates and returns a UUID in which all 128 bits are equal to 0.
42   static UUID GetNil();
43 
44   // Creates and returns a UUID in which all 128 bits are equal to 1.
45   static UUID GetMax();
46 
47   // Construct a Bluetooth 'base' UUID.
48   UUID();
49 
50   // BlueDroid constructor.
51   explicit UUID(const bt_uuid_t& uuid);
52 
53   // String constructor. Only hex ASCII accepted.
54   explicit UUID(std::string uuid);
55 
56   // std::array variants constructors.
57   explicit UUID(const UUID16Bit& uuid);
58   explicit UUID(const UUID32Bit& uuid);
59   explicit UUID(const UUID128Bit& uuid);
60 
61   // Provide the full network-byte-ordered blob.
62   UUID128Bit GetFullBigEndian() const;
63 
64   // Provide blob in Little endian (BlueDroid expects this).
65   UUID128Bit GetFullLittleEndian() const;
66 
67   // Helper for bluedroid LE type.
68   bt_uuid_t GetBlueDroid() const;
69 
70   // Returns a string representation for the UUID.
71   std::string ToString() const;
72 
73   // Returns whether or not this UUID was initialized correctly.
is_valid()74   bool is_valid() const { return is_valid_; }
75 
76   // Returns the shortest possible representation of this UUID in bytes.
77   size_t GetShortestRepresentationSize() const;
78 
79   bool operator<(const UUID& rhs) const;
80   bool operator==(const UUID& rhs) const;
81   inline bool operator!=(const UUID& rhs) const {
82     return !(*this == rhs);
83   }
84 
85  private:
86   void InitializeDefault();
87 
88   // Network-byte-ordered ID.
89   UUID128Bit id_;
90 
91   // True if this UUID was initialized with a correct representation.
92   bool is_valid_;
93 };
94 
95 }  // namespace bluetooth
96 
97 // Custom std::hash specialization so that bluetooth::UUID can be used as a key
98 // in std::unordered_map.
99 namespace std {
100 
101 template<>
102 struct hash<bluetooth::UUID> {
103   std::size_t operator()(const bluetooth::UUID& key) const {
104     const auto& uuid_bytes = key.GetFullBigEndian();
105     std::hash<std::string> hash_fn;
106     return hash_fn(std::string((char *)uuid_bytes.data(), uuid_bytes.size()));
107   }
108 };
109 
110 }  // namespace std
111