Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3257)

Unified Diff: device/bluetooth/bluetooth_device.cc

Issue 288903003: [Bluetooth] Standardize Bluetooth device address format to XX:XX:XX:XX:XX:XX. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: git add unittest Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698