Home | History | Annotate | Download | only in bluetooth
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_UUID_H_
      6 #define DEVICE_BLUETOOTH_BLUETOOTH_UUID_H_
      7 
      8 #include <string>
      9 
     10 namespace device {
     11 
     12 // Opaque wrapper around a Bluetooth UUID. Instances of UUID represent the
     13 // 128-bit universally unique identifiers (UUIDs) of profiles and attributes
     14 // used in Bluetooth based communication, such as a peripheral's services,
     15 // characteristics, and characteristic descriptors. An instance are
     16 // constructed using a string representing 16, 32, or 128 bit UUID formats.
     17 class BluetoothUUID {
     18  public:
     19   // Possible representation formats used during construction.
     20   enum Format {
     21     kFormatInvalid,
     22     kFormat16Bit,
     23     kFormat32Bit,
     24     kFormat128Bit
     25   };
     26 
     27   // Single argument constructor. |uuid| can be a 16, 32, or 128 bit UUID
     28   // represented as a 4, 8, or 36 character string with the following
     29   // formats:
     30   //   xxxx
     31   //   0xxxxx
     32   //   xxxxxxxx
     33   //   0xxxxxxxxx
     34   //   xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
     35   //
     36   // 16 and 32 bit UUIDs will be internally converted to a 128 bit UUID using
     37   // the base UUID defined in the Bluetooth specification, hence custom UUIDs
     38   // should be provided in the 128-bit format. If |uuid| is in an unsupported
     39   // format, the result might be invalid. Use IsValid to check for validity
     40   // after construction.
     41   explicit BluetoothUUID(const std::string& uuid);
     42 
     43   // Default constructor does nothing. Since BluetoothUUID is copyable, this
     44   // constructor is useful for initializing member variables and assigning a
     45   // value to them later. The default constructor will initialize an invalid
     46   // UUID by definition and the string accessors will return an empty string.
     47   BluetoothUUID();
     48   virtual ~BluetoothUUID();
     49 
     50   // Returns true, if the UUID is in a valid canonical format.
     51   bool IsValid() const;
     52 
     53   // Returns the representation format of the UUID. This reflects the format
     54   // that was provided during construction.
     55   Format format() const { return format_; }
     56 
     57   // Returns the value of the UUID as a string. The representation format is
     58   // based on what was passed in during construction. For the supported sizes,
     59   // this representation can have the following formats:
     60   //   - 16 bit:  xxxx
     61   //   - 32 bit:  xxxxxxxx
     62   //   - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
     63   // where x is a lowercase hex digit.
     64   const std::string& value() const { return value_; }
     65 
     66   // Returns the underlying 128-bit value as a string in the following format:
     67   //   xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
     68   // where x is a lowercase hex digit.
     69   const std::string& canonical_value() const { return canonical_value_; }
     70 
     71   // Permit sufficient comparison to allow a UUID to be used as a key in a
     72   // std::map.
     73   bool operator<(const BluetoothUUID& uuid) const;
     74 
     75   // Equality operators.
     76   bool operator==(const BluetoothUUID& uuid) const;
     77   bool operator!=(const BluetoothUUID& uuid) const;
     78 
     79  private:
     80   // String representation of the UUID that was used during construction. For
     81   // the supported sizes, this representation can have the following formats:
     82   //   - 16 bit:  xxxx
     83   //   - 32 bit:  xxxxxxxx
     84   //   - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
     85   Format format_;
     86   std::string value_;
     87 
     88   // The 128-bit string representation of the UUID.
     89   std::string canonical_value_;
     90 };
     91 
     92 // This is required by gtest to print a readable output on test failures.
     93 void PrintTo(const BluetoothUUID& uuid, std::ostream* out);
     94 
     95 }  // namespace device
     96 
     97 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_UUID_H_
     98