| 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 "components/arc/bluetooth/bluetooth_struct_traits.h" | 5 #include "components/arc/bluetooth/bluetooth_struct_traits.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 } | 27 } |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 namespace mojo { | 31 namespace mojo { |
| 32 | 32 |
| 33 // static | 33 // static |
| 34 std::vector<uint8_t> | 34 std::vector<uint8_t> |
| 35 StructTraits<arc::mojom::BluetoothUUIDDataView, device::BluetoothUUID>::uuid( | 35 StructTraits<arc::mojom::BluetoothUUIDDataView, device::BluetoothUUID>::uuid( |
| 36 const device::BluetoothUUID& input) { | 36 const device::BluetoothUUID& input) { |
| 37 // TODO(dcheng): Figure out what to do here, this is called twice on |
| 38 // serialization. Building a vector is a little inefficient. |
| 37 std::string uuid_str = StripNonHex(input.canonical_value()); | 39 std::string uuid_str = StripNonHex(input.canonical_value()); |
| 38 | 40 |
| 39 std::vector<uint8_t> address_bytes; | 41 std::vector<uint8_t> address_bytes; |
| 40 base::HexStringToBytes(uuid_str, &address_bytes); | 42 base::HexStringToBytes(uuid_str, &address_bytes); |
| 41 return address_bytes; | 43 return address_bytes; |
| 42 } | 44 } |
| 43 | 45 |
| 44 // static | 46 // static |
| 45 bool StructTraits<arc::mojom::BluetoothUUIDDataView, | 47 bool StructTraits<arc::mojom::BluetoothUUIDDataView, |
| 46 device::BluetoothUUID>::Read( | 48 device::BluetoothUUID>::Read( |
| 47 arc::mojom::BluetoothUUIDDataView data, | 49 arc::mojom::BluetoothUUIDDataView data, |
| 48 device::BluetoothUUID* output) { | 50 device::BluetoothUUID* output) { |
| 49 std::vector<uint8_t> address_bytes; | 51 std::vector<uint8_t> address_bytes; |
| 50 data.ReadUuid(&address_bytes); | 52 if (!data.ReadUuid(&address_bytes)) |
| 53 return false; |
| 51 | 54 |
| 52 if (address_bytes.size() != kUUIDSize) | 55 if (address_bytes.size() != kUUIDSize) |
| 53 return false; | 56 return false; |
| 54 | 57 |
| 55 // BluetoothUUID expects the format below with the dashes inserted. | 58 // BluetoothUUID expects the format below with the dashes inserted. |
| 56 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | 59 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 57 std::string uuid_str = | 60 std::string uuid_str = |
| 58 base::HexEncode(address_bytes.data(), address_bytes.size()); | 61 base::HexEncode(address_bytes.data(), address_bytes.size()); |
| 59 constexpr size_t kUuidDashPos[] = {8, 13, 18, 23}; | 62 constexpr size_t kUuidDashPos[] = {8, 13, 18, 23}; |
| 60 for (auto pos : kUuidDashPos) | 63 for (auto pos : kUuidDashPos) |
| 61 uuid_str = uuid_str.insert(pos, "-"); | 64 uuid_str = uuid_str.insert(pos, "-"); |
| 62 | 65 |
| 63 device::BluetoothUUID result(uuid_str); | 66 device::BluetoothUUID result(uuid_str); |
| 64 | 67 |
| 65 DCHECK(result.IsValid()); | 68 DCHECK(result.IsValid()); |
| 66 *output = result; | 69 *output = result; |
| 67 return true; | 70 return true; |
| 68 } | 71 } |
| 69 | 72 |
| 70 } // namespace mojo | 73 } // namespace mojo |
| OLD | NEW |