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