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 | #include "device/bluetooth/bluetooth_utils.h" |
| 6 | |
| 7 | #include <vector> |
| 8 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 9 | #include "base/basictypes.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "base/logging.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 12 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | namespace device { |
| 14 | namespace bluetooth_utils { |
| 15 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | const char* kCommonUuidPostfix = "-0000-1000-8000-00805f9b34fb"; |
| 19 | const char* kCommonUuidPrefix = "0000"; |
| 20 | const int kUuidSize = 36; |
| 21 | |
| 22 | // Returns the canonical, 128-bit canonical, and the format of the UUID |
| 23 | // in |canonical|, |canonical_128|, and |format| based on |uuid|. |
| 24 | void GetCanonicalUuid(std::string uuid, |
| 25 | std::string* canonical, |
| 26 | std::string* canonical_128, |
| 27 | UUID::Format* format) { |
| 28 | // Initialize the values for the failure case. |
| 29 | canonical->clear(); |
| 30 | canonical_128->clear(); |
| 31 | *format = UUID::kFormatInvalid; |
| 32 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 33 | if (uuid.empty()) |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 34 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 35 | |
| 36 | if (uuid.size() < 11 && uuid.find("0x") == 0) |
| 37 | uuid = uuid.substr(2); |
| 38 | |
| 39 | if (!(uuid.size() == 4 || uuid.size() == 8 || uuid.size() == 36)) |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 40 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 41 | |
| 42 | if (uuid.size() == 4 || uuid.size() == 8) { |
| 43 | for (size_t i = 0; i < uuid.size(); ++i) { |
| 44 | if (!IsHexDigit(uuid[i])) |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 45 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 46 | } |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 47 | if (uuid.size() == 4) { |
| 48 | canonical->assign(uuid); |
| 49 | canonical_128->assign(kCommonUuidPrefix + uuid + kCommonUuidPostfix); |
| 50 | *format = UUID::kFormat16Bit; |
| 51 | return; |
| 52 | } |
| 53 | canonical->assign(uuid); |
| 54 | canonical_128->assign(uuid + kCommonUuidPostfix); |
| 55 | *format = UUID::kFormat32Bit; |
| 56 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 59 | for (int i = 0; i < kUuidSize; ++i) { |
| 60 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 61 | if (uuid[i] != '-') |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 62 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 63 | } else { |
| 64 | if (!IsHexDigit(uuid[i])) |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 65 | return; |
| 66 | uuid[i] = tolower(uuid[i]); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 69 | |
| 70 | canonical->assign(uuid); |
| 71 | canonical_128->assign(uuid); |
| 72 | *format = UUID::kFormat128Bit; |
| 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | |
| 78 | UUID::UUID(const std::string& uuid) { |
| 79 | GetCanonicalUuid(uuid, &value_, &canonical_value_, &format_); |
| 80 | } |
| 81 | |
| 82 | UUID::UUID() : format_(kFormatInvalid) { |
| 83 | } |
| 84 | |
| 85 | UUID::~UUID() { |
| 86 | } |
| 87 | |
| 88 | bool UUID::IsValid() const { |
| 89 | return format_ != kFormatInvalid; |
| 90 | } |
| 91 | |
| 92 | bool UUID::operator<(const UUID& uuid) const { |
| 93 | return canonical_value_ < uuid.canonical_value_; |
| 94 | } |
| 95 | |
| 96 | bool UUID::operator==(const UUID& uuid) const { |
| 97 | return canonical_value_ == uuid.canonical_value_; |
| 98 | } |
| 99 | |
| 100 | bool UUID::operator!=(const UUID& uuid) const { |
| 101 | return canonical_value_ != uuid.canonical_value_; |
| 102 | } |
| 103 | |
| 104 | std::string CanonicalUuid(std::string uuid) { |
| 105 | std::string value; |
| 106 | std::string canonical_value; |
| 107 | UUID::Format format; |
| 108 | GetCanonicalUuid(uuid, &value, &canonical_value, &format); |
| 109 | return canonical_value; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace bluetooth_utils |
| 113 | } // namespace device |