Home | History | Annotate | Download | only in bluetooth
      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   virtual ~UUID() = default;
     50 
     51   // BlueDroid constructor.
     52   explicit UUID(const bt_uuid_t& uuid);
     53 
     54   // String constructor. Only hex ASCII accepted.
     55   explicit UUID(std::string uuid);
     56 
     57   // std::array variants constructors.
     58   explicit UUID(const UUID16Bit& uuid);
     59   explicit UUID(const UUID32Bit& uuid);
     60   explicit UUID(const UUID128Bit& uuid);
     61 
     62   // Provide the full network-byte-ordered blob.
     63   UUID128Bit GetFullBigEndian() const;
     64 
     65   // Provide blob in Little endian (BlueDroid expects this).
     66   UUID128Bit GetFullLittleEndian() const;
     67 
     68   // Helper for bluedroid LE type.
     69   bt_uuid_t GetBlueDroid() const;
     70 
     71   // Returns a string representation for the UUID.
     72   std::string ToString() const;
     73 
     74   // Returns whether or not this UUID was initialized correctly.
     75   bool is_valid() const { return is_valid_; }
     76 
     77   // Returns the shortest possible representation of this UUID in bytes.
     78   size_t GetShortestRepresentationSize() const;
     79 
     80   bool operator<(const UUID& rhs) const;
     81   bool operator==(const UUID& rhs) const;
     82   inline bool operator!=(const UUID& rhs) const { return !(*this == rhs); }
     83 
     84  protected:
     85   void InitializeDefault();
     86 
     87   // Network-byte-ordered ID.
     88   UUID128Bit id_;
     89 
     90   // True if this UUID was initialized with a correct representation.
     91   bool is_valid_;
     92 };
     93 
     94 }  // namespace bluetooth
     95 
     96 // Custom std::hash specialization so that bluetooth::UUID can be used as a key
     97 // in std::unordered_map.
     98 namespace std {
     99 
    100 template <>
    101 struct hash<bluetooth::UUID> {
    102   std::size_t operator()(const bluetooth::UUID& key) const {
    103     const auto& uuid_bytes = key.GetFullBigEndian();
    104     std::hash<std::string> hash_fn;
    105     return hash_fn(std::string((char*)uuid_bytes.data(), uuid_bytes.size()));
    106   }
    107 };
    108 
    109 }  // namespace std
    110