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 #include "build/build_config.h" 11 #include "device/bluetooth/bluetooth_export.h" 12 13 #if defined(OS_WIN) 14 #include <rpc.h> 15 16 #include "base/strings/string_piece_forward.h" 17 #endif // defined(OS_WIN) 18 19 namespace device { 20 21 // Opaque wrapper around a Bluetooth UUID. Instances of UUID represent the 22 // 128-bit universally unique identifiers (UUIDs) of profiles and attributes 23 // used in Bluetooth based communication, such as a peripheral's services, 24 // characteristics, and characteristic descriptors. An instance are 25 // constructed using a string representing 16, 32, or 128 bit UUID formats. 26 class DEVICE_BLUETOOTH_EXPORT BluetoothUUID { 27 public: 28 // Possible representation formats used during construction. 29 enum Format { 30 kFormatInvalid, 31 kFormat16Bit, 32 kFormat32Bit, 33 kFormat128Bit 34 }; 35 36 // Single argument constructor. |uuid| can be a 16, 32, or 128 bit UUID 37 // represented as a 4, 8, or 36 character string with the following 38 // formats: 39 // xxxx 40 // 0xxxxx 41 // xxxxxxxx 42 // 0xxxxxxxxx 43 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 44 // 45 // 16 and 32 bit UUIDs will be internally converted to a 128 bit UUID using 46 // the base UUID defined in the Bluetooth specification, hence custom UUIDs 47 // should be provided in the 128-bit format. If |uuid| is in an unsupported 48 // format, the result might be invalid. Use IsValid to check for validity 49 // after construction. 50 explicit BluetoothUUID(const std::string& uuid); 51 52 #if defined(OS_WIN) 53 // Windows exclusive constructor converting a GUID structure to a 54 // BluetoothUUID. This will always result in a 128 bit Format. 55 explicit BluetoothUUID(GUID uuid); 56 #endif // defined(OS_WIN) 57 58 // Default constructor does nothing. Since BluetoothUUID is copyable, this 59 // constructor is useful for initializing member variables and assigning a 60 // value to them later. The default constructor will initialize an invalid 61 // UUID by definition and the string accessors will return an empty string. 62 BluetoothUUID(); 63 virtual ~BluetoothUUID(); 64 65 #if defined(OS_WIN) 66 // The canonical UUID string format is device::BluetoothUUID.value(). 67 static GUID GetCanonicalValueAsGUID(base::StringPiece uuid); 68 #endif // defined(OS_WIN) 69 70 // Returns true, if the UUID is in a valid canonical format. 71 bool IsValid() const; 72 73 // Returns the representation format of the UUID. This reflects the format 74 // that was provided during construction. format()75 Format format() const { return format_; } 76 77 // Returns the value of the UUID as a string. The representation format is 78 // based on what was passed in during construction. For the supported sizes, 79 // this representation can have the following formats: 80 // - 16 bit: xxxx 81 // - 32 bit: xxxxxxxx 82 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 83 // where x is a lowercase hex digit. value()84 const std::string& value() const { return value_; } 85 86 // Returns the underlying 128-bit value as a string in the following format: 87 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 88 // where x is a lowercase hex digit. canonical_value()89 const std::string& canonical_value() const { return canonical_value_; } 90 91 // Permit sufficient comparison to allow a UUID to be used as a key in a 92 // std::map. 93 bool operator<(const BluetoothUUID& uuid) const; 94 95 // Equality operators. 96 bool operator==(const BluetoothUUID& uuid) const; 97 bool operator!=(const BluetoothUUID& uuid) const; 98 99 private: 100 // String representation of the UUID that was used during construction. For 101 // the supported sizes, this representation can have the following formats: 102 // - 16 bit: xxxx 103 // - 32 bit: xxxxxxxx 104 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 105 Format format_; 106 std::string value_; 107 108 // The 128-bit string representation of the UUID. 109 std::string canonical_value_; 110 }; 111 112 // This is required by gtest to print a readable output on test failures. 113 void DEVICE_BLUETOOTH_EXPORT 114 PrintTo(const BluetoothUUID& uuid, std::ostream* out); 115 116 struct BluetoothUUIDHash { operatorBluetoothUUIDHash117 size_t operator()(const device::BluetoothUUID& uuid) const { 118 return std::hash<std::string>()(uuid.canonical_value()); 119 } 120 }; 121 122 } // namespace device 123 124 #endif // DEVICE_BLUETOOTH_BLUETOOTH_UUID_H_ 125