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