Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 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_UTILS_H_ |
| 6 | #define DEVICE_BLUETOOTH_BLUETOOTH_UTILS_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "base/basictypes.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 11 | |
| 12 | namespace device { |
| 13 | namespace bluetooth_utils { |
| 14 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 15 | // Opaque wrapper around a Bluetooth UUID. Instances of UUID represent the |
| 16 | // 128-bit universally unique identifiers (UUIDs) of profiles and attributes |
| 17 | // used in Bluetooth based communication, such as a peripheral's services, |
| 18 | // characteristics, and characteristic descriptors. An instance are |
| 19 | // constructed using a string representing 16, 32, or 128 bit UUID formats. |
| 20 | class UUID { |
| 21 | public: |
| 22 | // Possible representation formats used during construction. |
| 23 | enum Format { |
| 24 | kFormatInvalid, |
| 25 | kFormat16Bit, |
| 26 | kFormat32Bit, |
| 27 | kFormat128Bit |
| 28 | }; |
| 29 | |
| 30 | // Single argument constructor. |uuid| can be a 16, 32, or 128 bit UUID |
| 31 | // represented as a 4, 8, or 36 character string with the following |
| 32 | // formats: |
| 33 | // XXXX |
| 34 | // 0xXXXX |
| 35 | // XXXXXXXX |
| 36 | // 0xXXXXXXXX |
| 37 | // XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX |
| 38 | // |
| 39 | // 16 and 32 bit UUIDs will be internally converted to a 128 bit UUID using |
| 40 | // the base UUID defined in the Bluetooth specification, hence custom UUIDs |
| 41 | // should be provided in the 128-bit format. If |uuid| is in an unsupported |
| 42 | // format, the result might be invalid. Use IsValid to check for validity |
| 43 | // after construction. |
| 44 | explicit UUID(const std::string& uuid); |
| 45 | ~UUID(); |
| 46 | |
| 47 | // Returns true, if the UUID is in a valid canonical format. |
| 48 | bool IsValid() const; |
| 49 | |
| 50 | // Returns the representation format of the UUID. This reflects the format |
| 51 | // that was provided during construction. |
| 52 | Format format() const { return format_; } |
| 53 | |
| 54 | // Returns the value of the UUID as a string. The representation format is |
| 55 | // based on what was passed in during construction. For the supported sizes, |
| 56 | // this representation can have the following formats: |
| 57 | // - 16 bit: XXXX |
| 58 | // - 32 bit: XXXXXXXX |
| 59 | // - 128 bit: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX |
| 60 | // where X is a lowercase hex digit. |
| 61 | const std::string& value() const { return value_; } |
| 62 | |
| 63 | // Returns the underlying 128-bit value as a string in the following format: |
| 64 | // XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX |
| 65 | // where X is a lowercase hex digit. |
| 66 | const std::string& canonical_value() const { return canonical_value_; } |
| 67 | |
| 68 | // Permit sufficient comparison to allow a UUID to be used as a key in a |
| 69 | // std::map. |
| 70 | bool operator<(const UUID& uuid) const; |
| 71 | |
| 72 | // Equality operators. |
| 73 | bool operator==(const UUID& uuid) const; |
| 74 | bool operator!=(const UUID& uuid) const; |
| 75 | |
| 76 | private: |
| 77 | UUID(); |
| 78 | |
| 79 | // String representation of the UUID that was used during construction. For |
| 80 | // the supported sizes, this representation can have the following formats: |
| 81 | // - 16 bit: XXXX |
| 82 | // - 32 bit: XXXXXXXX |
| 83 | // - 128 bit: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX |
| 84 | Format format_; |
| 85 | std::string value_; |
| 86 | |
| 87 | // The 128-bit string representation of the UUID. |
| 88 | std::string canonical_value_; |
| 89 | }; |
| 90 | |
| 91 | // DEPRECATED. Use bluetooth_utils::UUID instead. |
| 92 | // |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 93 | // Takes a 4, 8 or 36 character UUID, validates it and returns it in 36 |
| 94 | // character format with all hex digits lower case. If |uuid| is invalid, the |
| 95 | // empty string is returned. |
| 96 | // |
| 97 | // Valid inputs are: |
| 98 | // XXXX |
| 99 | // 0xXXXX |
| 100 | // XXXXXXXX |
| 101 | // 0xXXXXXXXX |
| 102 | // XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX |
| 103 | std::string CanonicalUuid(std::string uuid); |
| 104 | |
| 105 | } // namespace bluetooth_utils |
| 106 | } // namespace device |
| 107 | |
| 108 | #endif // DEVICE_BLUETOOTH_BLUETOOTH_UTILS_H_ |
| 109 | |