| Index: device/bluetooth/bluetooth_device.cc
|
| diff --git a/device/bluetooth/bluetooth_device.cc b/device/bluetooth/bluetooth_device.cc
|
| index e29e34f7d5c0e6f9f89bcebe676a9f1a4761f627..e95fa3003507859175fbcd33ca3a90ebbaf114ec 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 normalized = address;
|
| + if (address.size() == 12) {
|
| + // Might be an address in the format "1A2B3C4D5E6F". Add separators.
|
| + for (size_t i = 2; i < normalized.size(); i += 3) {
|
| + normalized.insert(i, ":");
|
| + }
|
| + }
|
| +
|
| + // Verify that the length matches the canonical format "1A:2B:3C:4D:5E:6F".
|
| + const size_t kCanonicalAddressLength = 17;
|
| + if (normalized.size() != kCanonicalAddressLength)
|
| + return std::string();
|
| +
|
| + const char separator = normalized[2];
|
| + for (size_t i = 0; i < normalized.size(); ++i) {
|
| + bool is_separator = (i + 1) % 3 == 0;
|
| + if (is_separator) {
|
| + // All separators in the input |address| should be consistent.
|
| + if (normalized[i] != separator)
|
| + return std::string();
|
| +
|
| + normalized[i] = ':';
|
| + } else {
|
| + if (!IsHexDigit(normalized[i]))
|
| + return std::string();
|
| +
|
| + normalized[i] = base::ToUpperASCII(normalized[i]);
|
| + }
|
| + }
|
| +
|
| + return normalized;
|
| +}
|
| +
|
| } // namespace device
|
|
|