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 | #include "device/bluetooth/bluetooth_uuid.h" |
| 6 | |
avi | 7171ac7 | 2015-12-23 04:26:52 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | #include "base/strings/string_util.h" |
| 11 | |
| 12 | namespace device { |
| 13 | |
| 14 | namespace { |
| 15 | |
thestig | d795491 | 2016-07-14 07:36:34 +0900 | [diff] [blame] | 16 | const char kCommonUuidPostfix[] = "-0000-1000-8000-00805f9b34fb"; |
| 17 | const char kCommonUuidPrefix[] = "0000"; |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 18 | |
| 19 | // Returns the canonical, 128-bit canonical, and the format of the UUID |
| 20 | // in |canonical|, |canonical_128|, and |format| based on |uuid|. |
| 21 | void GetCanonicalUuid(std::string uuid, |
| 22 | std::string* canonical, |
| 23 | std::string* canonical_128, |
| 24 | BluetoothUUID::Format* format) { |
| 25 | // Initialize the values for the failure case. |
| 26 | canonical->clear(); |
| 27 | canonical_128->clear(); |
| 28 | *format = BluetoothUUID::kFormatInvalid; |
| 29 | |
| 30 | if (uuid.empty()) |
| 31 | return; |
| 32 | |
thestig | d795491 | 2016-07-14 07:36:34 +0900 | [diff] [blame] | 33 | if (uuid.size() < 11 && |
| 34 | base::StartsWith(uuid, "0x", base::CompareCase::SENSITIVE)) { |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 35 | uuid = uuid.substr(2); |
thestig | d795491 | 2016-07-14 07:36:34 +0900 | [diff] [blame] | 36 | } |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 37 | |
| 38 | if (!(uuid.size() == 4 || uuid.size() == 8 || uuid.size() == 36)) |
| 39 | return; |
| 40 | |
isherman@chromium.org | 2ce9afc | 2014-05-14 23:22:45 +0900 | [diff] [blame] | 41 | for (size_t i = 0; i < uuid.size(); ++i) { |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 42 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 43 | if (uuid[i] != '-') |
| 44 | return; |
| 45 | } else { |
brettw | 6ff92f6 | 2015-06-24 09:39:02 +0900 | [diff] [blame] | 46 | if (!base::IsHexDigit(uuid[i])) |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 47 | return; |
isherman@chromium.org | a39170e | 2014-05-16 11:01:28 +0900 | [diff] [blame] | 48 | uuid[i] = base::ToLowerASCII(uuid[i]); |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | canonical->assign(uuid); |
isherman@chromium.org | 2ce9afc | 2014-05-14 23:22:45 +0900 | [diff] [blame] | 53 | if (uuid.size() == 4) { |
| 54 | canonical_128->assign(kCommonUuidPrefix + uuid + kCommonUuidPostfix); |
| 55 | *format = BluetoothUUID::kFormat16Bit; |
| 56 | } else if (uuid.size() == 8) { |
| 57 | canonical_128->assign(uuid + kCommonUuidPostfix); |
| 58 | *format = BluetoothUUID::kFormat32Bit; |
| 59 | } else { |
| 60 | canonical_128->assign(uuid); |
| 61 | *format = BluetoothUUID::kFormat128Bit; |
| 62 | } |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | |
| 68 | BluetoothUUID::BluetoothUUID(const std::string& uuid) { |
| 69 | GetCanonicalUuid(uuid, &value_, &canonical_value_, &format_); |
| 70 | } |
| 71 | |
| 72 | BluetoothUUID::BluetoothUUID() : format_(kFormatInvalid) { |
| 73 | } |
| 74 | |
| 75 | BluetoothUUID::~BluetoothUUID() { |
| 76 | } |
| 77 | |
| 78 | bool BluetoothUUID::IsValid() const { |
| 79 | return format_ != kFormatInvalid; |
| 80 | } |
| 81 | |
| 82 | bool BluetoothUUID::operator<(const BluetoothUUID& uuid) const { |
| 83 | return canonical_value_ < uuid.canonical_value_; |
| 84 | } |
| 85 | |
| 86 | bool BluetoothUUID::operator==(const BluetoothUUID& uuid) const { |
| 87 | return canonical_value_ == uuid.canonical_value_; |
| 88 | } |
| 89 | |
| 90 | bool BluetoothUUID::operator!=(const BluetoothUUID& uuid) const { |
| 91 | return canonical_value_ != uuid.canonical_value_; |
| 92 | } |
| 93 | |
armansito@chromium.org | b4b5156 | 2014-04-10 22:32:55 +0900 | [diff] [blame] | 94 | void PrintTo(const BluetoothUUID& uuid, std::ostream* out) { |
| 95 | *out << uuid.canonical_value(); |
| 96 | } |
| 97 | |
armansito@chromium.org | 7d8c5ee | 2014-04-04 13:10:38 +0900 | [diff] [blame] | 98 | } // namespace device |