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