Index: device/bluetooth/bluetooth_device.cc |
diff --git a/device/bluetooth/bluetooth_device.cc b/device/bluetooth/bluetooth_device.cc |
index e29e34f7d5c0e6f9f89bcebe676a9f1a4761f627..5abff605e2fbbaeff0a6599c611b103f144c8cf6 100644 |
--- a/device/bluetooth/bluetooth_device.cc |
+++ b/device/bluetooth/bluetooth_device.cc |
@@ -6,6 +6,7 @@ |
#include <string> |
+#include "base/strings/string_util.h" |
#include "base/strings/utf_string_conversions.h" |
#include "device/bluetooth/bluetooth_gatt_service.h" |
#include "grit/device_bluetooth_strings.h" |
@@ -187,4 +188,39 @@ BluetoothGattService* BluetoothDevice::GetGattService( |
return NULL; |
} |
+// static |
+std::string BluetoothDevice::CanonicalizeAddress(const std::string& address) { |
+ std::string canonicalized = address; |
+ if (address.size() == 12) { |
+ // Might be an address in the format "1A2B3C4D5E6F". Add separators. |
+ for (size_t i = 2; i < canonicalized.size(); i += 3) { |
+ canonicalized.insert(i, ":"); |
+ } |
+ } |
+ |
+ // Verify that the length matches the canonical format "1A:2B:3C:4D:5E:6F". |
+ const size_t kCanonicalAddressLength = 17; |
+ if (canonicalized.size() != kCanonicalAddressLength) |
+ return std::string(); |
+ |
+ const char separator = canonicalized[2]; |
+ for (size_t i = 0; i < canonicalized.size(); ++i) { |
+ bool is_separator = (i + 1) % 3 == 0; |
+ if (is_separator) { |
+ // All separators in the input |address| should be consistent. |
+ if (canonicalized[i] != separator) |
+ return std::string(); |
+ |
+ canonicalized[i] = ':'; |
+ } else { |
+ if (!IsHexDigit(canonicalized[i])) |
+ return std::string(); |
+ |
+ canonicalized[i] = base::ToUpperASCII(canonicalized[i]); |
+ } |
+ } |
+ |
+ return canonicalized; |
+} |
+ |
} // namespace device |