blob: 8487f6a2da552b10a289b70d6f0980e56661b373 [file] [log] [blame]
armansito@chromium.org7d8c5ee2014-04-04 13:10:38 +09001// 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
scheib2a849792014-12-07 01:02:04 +090010#include "device/bluetooth/bluetooth_export.h"
11
armansito@chromium.org7d8c5ee2014-04-04 13:10:38 +090012namespace device {
13
14// Opaque wrapper around a Bluetooth UUID. Instances of UUID represent the
15// 128-bit universally unique identifiers (UUIDs) of profiles and attributes
16// used in Bluetooth based communication, such as a peripheral's services,
17// characteristics, and characteristic descriptors. An instance are
18// constructed using a string representing 16, 32, or 128 bit UUID formats.
scheib2a849792014-12-07 01:02:04 +090019class DEVICE_BLUETOOTH_EXPORT BluetoothUUID {
armansito@chromium.org7d8c5ee2014-04-04 13:10:38 +090020 public:
21 // Possible representation formats used during construction.
22 enum Format {
23 kFormatInvalid,
24 kFormat16Bit,
25 kFormat32Bit,
26 kFormat128Bit
27 };
28
29 // Single argument constructor. |uuid| can be a 16, 32, or 128 bit UUID
30 // represented as a 4, 8, or 36 character string with the following
31 // formats:
isherman@chromium.org2ce9afc2014-05-14 23:22:45 +090032 // xxxx
33 // 0xxxxx
34 // xxxxxxxx
35 // 0xxxxxxxxx
36 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
armansito@chromium.org7d8c5ee2014-04-04 13:10:38 +090037 //
38 // 16 and 32 bit UUIDs will be internally converted to a 128 bit UUID using
39 // the base UUID defined in the Bluetooth specification, hence custom UUIDs
40 // should be provided in the 128-bit format. If |uuid| is in an unsupported
41 // format, the result might be invalid. Use IsValid to check for validity
42 // after construction.
43 explicit BluetoothUUID(const std::string& uuid);
44
45 // Default constructor does nothing. Since BluetoothUUID is copyable, this
46 // constructor is useful for initializing member variables and assigning a
47 // value to them later. The default constructor will initialize an invalid
48 // UUID by definition and the string accessors will return an empty string.
49 BluetoothUUID();
50 virtual ~BluetoothUUID();
51
52 // Returns true, if the UUID is in a valid canonical format.
53 bool IsValid() const;
54
55 // Returns the representation format of the UUID. This reflects the format
56 // that was provided during construction.
57 Format format() const { return format_; }
58
59 // Returns the value of the UUID as a string. The representation format is
60 // based on what was passed in during construction. For the supported sizes,
61 // this representation can have the following formats:
isherman@chromium.org2ce9afc2014-05-14 23:22:45 +090062 // - 16 bit: xxxx
63 // - 32 bit: xxxxxxxx
64 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
65 // where x is a lowercase hex digit.
armansito@chromium.org7d8c5ee2014-04-04 13:10:38 +090066 const std::string& value() const { return value_; }
67
68 // Returns the underlying 128-bit value as a string in the following format:
isherman@chromium.org2ce9afc2014-05-14 23:22:45 +090069 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
70 // where x is a lowercase hex digit.
armansito@chromium.org7d8c5ee2014-04-04 13:10:38 +090071 const std::string& canonical_value() const { return canonical_value_; }
72
73 // Permit sufficient comparison to allow a UUID to be used as a key in a
74 // std::map.
75 bool operator<(const BluetoothUUID& uuid) const;
76
77 // Equality operators.
78 bool operator==(const BluetoothUUID& uuid) const;
79 bool operator!=(const BluetoothUUID& uuid) const;
80
81 private:
82 // String representation of the UUID that was used during construction. For
83 // the supported sizes, this representation can have the following formats:
isherman@chromium.org2ce9afc2014-05-14 23:22:45 +090084 // - 16 bit: xxxx
85 // - 32 bit: xxxxxxxx
86 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
armansito@chromium.org7d8c5ee2014-04-04 13:10:38 +090087 Format format_;
88 std::string value_;
89
90 // The 128-bit string representation of the UUID.
91 std::string canonical_value_;
92};
93
armansito@chromium.orgb4b51562014-04-10 22:32:55 +090094// This is required by gtest to print a readable output on test failures.
scheib2a849792014-12-07 01:02:04 +090095void DEVICE_BLUETOOTH_EXPORT
96PrintTo(const BluetoothUUID& uuid, std::ostream* out);
armansito@chromium.orgb4b51562014-04-10 22:32:55 +090097
ortunob6a8a9c2016-06-11 05:15:07 +090098struct BluetoothUUIDHash {
99 size_t operator()(const device::BluetoothUUID& uuid) const {
100 return std::hash<std::string>()(uuid.canonical_value());
101 }
jyasskin68f5d7d2015-06-25 08:53:52 +0900102};
103
ortunob6a8a9c2016-06-11 05:15:07 +0900104} // namespace device
jyasskin68f5d7d2015-06-25 08:53:52 +0900105
armansito@chromium.org7d8c5ee2014-04-04 13:10:38 +0900106#endif // DEVICE_BLUETOOTH_BLUETOOTH_UUID_H_