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