| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <cctype> | 6 #include <cctype> |
| 7 #include <iomanip> | 7 #include <iomanip> |
| 8 #include <ios> | 8 #include <ios> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/arc/bluetooth/bluetooth_type_converters.h" | 15 #include "components/arc/bluetooth/bluetooth_type_converters.h" |
| 16 #include "device/bluetooth/bluetooth_gatt_service.h" | 16 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 17 #include "device/bluetooth/bluetooth_uuid.h" | 17 #include "device/bluetooth/bluetooth_uuid.h" |
| 18 #include "mojo/public/cpp/bindings/array.h" | 18 #include "mojo/public/cpp/bindings/array.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 constexpr size_t kAddressSize = 6; |
| 23 constexpr size_t kUUIDSize = 16; |
| 24 constexpr char kInvalidAddress[] = "00:00:00:00:00:00"; |
| 25 |
| 22 bool IsNonHex(char c) { | 26 bool IsNonHex(char c) { |
| 23 return !isxdigit(c); | 27 return !isxdigit(c); |
| 24 } | 28 } |
| 25 | 29 |
| 26 std::string StripNonHex(const std::string& str) { | 30 std::string StripNonHex(const std::string& str) { |
| 27 std::string result = str; | 31 std::string result = str; |
| 28 result.erase(std::remove_if(result.begin(), result.end(), IsNonHex), | 32 result.erase(std::remove_if(result.begin(), result.end(), IsNonHex), |
| 29 result.end()); | 33 result.end()); |
| 30 | 34 |
| 31 return result; | 35 return result; |
| 32 } | 36 } |
| 33 | 37 |
| 34 } // namespace | 38 } // namespace |
| 35 | 39 |
| 36 namespace mojo { | 40 namespace mojo { |
| 37 | 41 |
| 38 // TODO(smbarber): Add unit tests for Bluetooth type converters. | |
| 39 | |
| 40 // static | 42 // static |
| 41 arc::mojom::BluetoothAddressPtr | 43 arc::mojom::BluetoothAddressPtr |
| 42 TypeConverter<arc::mojom::BluetoothAddressPtr, std::string>::Convert( | 44 TypeConverter<arc::mojom::BluetoothAddressPtr, std::string>::Convert( |
| 43 const std::string& address) { | 45 const std::string& address) { |
| 44 std::string stripped = StripNonHex(address); | 46 std::string stripped = StripNonHex(address); |
| 45 | 47 |
| 46 std::vector<uint8_t> address_bytes; | 48 std::vector<uint8_t> address_bytes; |
| 47 base::HexStringToBytes(stripped, &address_bytes); | 49 base::HexStringToBytes(stripped, &address_bytes); |
| 48 | 50 |
| 49 arc::mojom::BluetoothAddressPtr mojo_addr = | 51 arc::mojom::BluetoothAddressPtr mojo_addr = |
| 50 arc::mojom::BluetoothAddress::New(); | 52 arc::mojom::BluetoothAddress::New(); |
| 51 mojo_addr->address = mojo::Array<uint8_t>::From(address_bytes); | 53 mojo_addr->address = mojo::Array<uint8_t>::From(address_bytes); |
| 52 | 54 |
| 53 return mojo_addr; | 55 return mojo_addr; |
| 54 } | 56 } |
| 55 | 57 |
| 56 // static | 58 // static |
| 57 std::string TypeConverter<std::string, arc::mojom::BluetoothAddress>::Convert( | 59 std::string TypeConverter<std::string, arc::mojom::BluetoothAddress>::Convert( |
| 58 const arc::mojom::BluetoothAddress& address) { | 60 const arc::mojom::BluetoothAddress& address) { |
| 59 std::ostringstream addr_stream; | 61 std::ostringstream addr_stream; |
| 60 addr_stream << std::setfill('0') << std::hex << std::uppercase; | 62 addr_stream << std::setfill('0') << std::hex << std::uppercase; |
| 61 | 63 |
| 62 const mojo::Array<uint8_t>& bytes = address.address; | 64 const mojo::Array<uint8_t>& bytes = address.address; |
| 63 | 65 |
| 66 if (address.address.size() != kAddressSize) |
| 67 return std::string(kInvalidAddress); |
| 68 |
| 64 for (size_t k = 0; k < bytes.size(); k++) { | 69 for (size_t k = 0; k < bytes.size(); k++) { |
| 65 addr_stream << std::setw(2) << (unsigned int)bytes[k]; | 70 addr_stream << std::setw(2) << (unsigned int)bytes[k]; |
| 66 addr_stream << ((k == bytes.size() - 1) ? "" : ":"); | 71 addr_stream << ((k == bytes.size() - 1) ? "" : ":"); |
| 67 } | 72 } |
| 68 | 73 |
| 69 return addr_stream.str(); | 74 return addr_stream.str(); |
| 70 } | 75 } |
| 71 | 76 |
| 72 // static | 77 // static |
| 73 arc::mojom::BluetoothUUIDPtr | 78 arc::mojom::BluetoothUUIDPtr |
| 74 TypeConverter<arc::mojom::BluetoothUUIDPtr, device::BluetoothUUID>::Convert( | 79 TypeConverter<arc::mojom::BluetoothUUIDPtr, device::BluetoothUUID>::Convert( |
| 75 const device::BluetoothUUID& uuid) { | 80 const device::BluetoothUUID& uuid) { |
| 76 std::string uuid_str = StripNonHex(uuid.canonical_value()); | 81 std::string uuid_str = StripNonHex(uuid.canonical_value()); |
| 77 | 82 |
| 78 std::vector<uint8_t> address_bytes; | 83 std::vector<uint8_t> address_bytes; |
| 79 base::HexStringToBytes(uuid_str, &address_bytes); | 84 base::HexStringToBytes(uuid_str, &address_bytes); |
| 80 | 85 |
| 81 arc::mojom::BluetoothUUIDPtr uuidp = arc::mojom::BluetoothUUID::New(); | 86 arc::mojom::BluetoothUUIDPtr uuidp = arc::mojom::BluetoothUUID::New(); |
| 82 uuidp->uuid = mojo::Array<uint8_t>::From(address_bytes); | 87 uuidp->uuid = mojo::Array<uint8_t>::From(address_bytes); |
| 83 | 88 |
| 84 return uuidp; | 89 return uuidp; |
| 85 } | 90 } |
| 86 | 91 |
| 87 // static | 92 // static |
| 88 device::BluetoothUUID | 93 device::BluetoothUUID |
| 89 TypeConverter<device::BluetoothUUID, arc::mojom::BluetoothUUIDPtr>::Convert( | 94 TypeConverter<device::BluetoothUUID, arc::mojom::BluetoothUUIDPtr>::Convert( |
| 90 const arc::mojom::BluetoothUUIDPtr& uuid) { | 95 const arc::mojom::BluetoothUUIDPtr& uuid) { |
| 91 std::vector<uint8_t> address_bytes = uuid->uuid.To<std::vector<uint8_t>>(); | 96 std::vector<uint8_t> address_bytes = uuid->uuid.To<std::vector<uint8_t>>(); |
| 92 | 97 |
| 98 if (address_bytes.size() != kUUIDSize) |
| 99 return device::BluetoothUUID(); |
| 100 |
| 93 // BluetoothUUID expects the format below with the dashes inserted. | 101 // BluetoothUUID expects the format below with the dashes inserted. |
| 94 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | 102 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 95 std::string uuid_str = | 103 std::string uuid_str = |
| 96 base::HexEncode(address_bytes.data(), address_bytes.size()); | 104 base::HexEncode(address_bytes.data(), address_bytes.size()); |
| 97 const size_t uuid_dash_pos[] = {8, 13, 18, 23}; | 105 const size_t uuid_dash_pos[] = {8, 13, 18, 23}; |
| 98 for (auto pos : uuid_dash_pos) | 106 for (auto pos : uuid_dash_pos) |
| 99 uuid_str = uuid_str.insert(pos, "-"); | 107 uuid_str = uuid_str.insert(pos, "-"); |
| 100 | 108 |
| 101 device::BluetoothUUID result(uuid_str); | 109 device::BluetoothUUID result(uuid_str); |
| 102 | 110 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 break; | 144 break; |
| 137 | 145 |
| 138 default: | 146 default: |
| 139 ret = arc::mojom::BluetoothGattStatus::GATT_FAILURE; | 147 ret = arc::mojom::BluetoothGattStatus::GATT_FAILURE; |
| 140 break; | 148 break; |
| 141 } | 149 } |
| 142 return ret; | 150 return ret; |
| 143 } | 151 } |
| 144 | 152 |
| 145 } // namespace mojo | 153 } // namespace mojo |
| OLD | NEW |