Chromium Code Reviews| 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" |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "device/bluetooth/bluetooth_advertisement.h" | |
| 11 #include "device/bluetooth/bluetooth_uuid.h" | 13 #include "device/bluetooth/bluetooth_uuid.h" |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 17 // BluetoothUUID helpers. | |
| 15 constexpr size_t kUUIDSize = 16; | 18 constexpr size_t kUUIDSize = 16; |
| 16 | 19 |
| 17 bool IsNonHex(char c) { | 20 bool IsNonHex(char c) { |
| 18 return !isxdigit(c); | 21 return !isxdigit(c); |
| 19 } | 22 } |
| 20 | 23 |
| 21 std::string StripNonHex(const std::string& str) { | 24 std::string StripNonHex(const std::string& str) { |
| 22 std::string result = str; | 25 std::string result = str; |
| 23 result.erase(std::remove_if(result.begin(), result.end(), IsNonHex), | 26 result.erase(std::remove_if(result.begin(), result.end(), IsNonHex), |
| 24 result.end()); | 27 result.end()); |
| 25 | 28 |
| 26 return result; | 29 return result; |
| 27 } | 30 } |
| 28 | 31 |
| 32 // BluetoothAdvertisement helpers. | |
| 33 struct AdvertisementEntry { | |
| 34 virtual void AddTo(device::BluetoothAdvertisement::Data* data) {} | |
| 35 }; | |
| 36 | |
| 37 struct ServiceUUIDEntry : public AdvertisementEntry { | |
| 38 std::vector<device::BluetoothUUID> service_uuids; | |
| 39 | |
| 40 void AddTo(device::BluetoothAdvertisement::Data* data) override { | |
| 41 std::unique_ptr<std::vector<std::string>> string_uuids = | |
|
rickyz (no longer on Chrome)
2016/09/14 20:47:26
nit: Can use auto to avoid specifying the type twi
| |
| 42 base::MakeUnique<std::vector<std::string>>(); | |
| 43 for (const auto& uuid : service_uuids) { | |
| 44 string_uuids->emplace_back(uuid.value()); | |
| 45 } | |
| 46 data->set_service_uuids(std::move(string_uuids)); | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 struct ServiceDataEntry : public AdvertisementEntry { | |
| 51 uint16_t service_uuid; | |
| 52 std::vector<uint8_t> service_data; | |
| 53 | |
| 54 void AddTo(device::BluetoothAdvertisement::Data* data) override { | |
| 55 std::string string_uuid = base::StringPrintf("%04x", service_uuid); | |
| 56 data->set_service_data( | |
| 57 base::WrapUnique(new std::map<std::string, std::vector<uint8_t>>{ | |
| 58 {string_uuid, service_data}})); | |
| 59 } | |
| 60 }; | |
| 61 | |
| 62 struct ManufacturerDataEntry : public AdvertisementEntry { | |
| 63 uint16_t company_id_code; | |
| 64 std::vector<uint8_t> blob; | |
| 65 | |
| 66 void AddTo(device::BluetoothAdvertisement::Data* data) override { | |
| 67 data->set_manufacturer_data(base::WrapUnique( | |
| 68 new std::map<uint16_t, std::vector<uint8_t>>{{company_id_code, blob}})); | |
| 69 } | |
| 70 }; | |
| 71 | |
| 72 uint16_t ExtractCompanyIdentifierCode(std::vector<uint8_t>* blob) { | |
| 73 // The company identifier code is in little-endian. | |
| 74 uint16_t company_id_code = (*blob)[1] << 8 | (*blob)[0]; | |
| 75 blob->erase(blob->begin(), blob->begin() + sizeof(uint16_t)); | |
| 76 return company_id_code; | |
| 77 } | |
| 78 | |
| 29 } // namespace | 79 } // namespace |
| 30 | 80 |
| 31 namespace mojo { | 81 namespace mojo { |
| 32 | 82 |
| 33 // static | 83 // static |
| 34 std::vector<uint8_t> | 84 std::vector<uint8_t> |
| 35 StructTraits<arc::mojom::BluetoothUUIDDataView, device::BluetoothUUID>::uuid( | 85 StructTraits<arc::mojom::BluetoothUUIDDataView, device::BluetoothUUID>::uuid( |
| 36 const device::BluetoothUUID& input) { | 86 const device::BluetoothUUID& input) { |
| 37 std::string uuid_str = StripNonHex(input.canonical_value()); | 87 std::string uuid_str = StripNonHex(input.canonical_value()); |
| 38 | 88 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 60 for (auto pos : kUuidDashPos) | 110 for (auto pos : kUuidDashPos) |
| 61 uuid_str = uuid_str.insert(pos, "-"); | 111 uuid_str = uuid_str.insert(pos, "-"); |
| 62 | 112 |
| 63 device::BluetoothUUID result(uuid_str); | 113 device::BluetoothUUID result(uuid_str); |
| 64 | 114 |
| 65 DCHECK(result.IsValid()); | 115 DCHECK(result.IsValid()); |
| 66 *output = result; | 116 *output = result; |
| 67 return true; | 117 return true; |
| 68 } | 118 } |
| 69 | 119 |
| 120 template <> | |
| 121 struct EnumTraits<arc::mojom::BluetoothAdvertisementType, | |
| 122 device::BluetoothAdvertisement::AdvertisementType> { | |
| 123 static bool FromMojom( | |
| 124 arc::mojom::BluetoothAdvertisementType mojom_type, | |
| 125 device::BluetoothAdvertisement::AdvertisementType* type) { | |
| 126 switch (mojom_type) { | |
| 127 case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_CONNECTABLE: | |
| 128 case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_SCANNABLE: | |
| 129 *type = device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_PERIPHERAL; | |
| 130 return true; | |
| 131 case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_NON_CONNECTABLE: | |
| 132 *type = device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST; | |
| 133 return true; | |
| 134 } | |
| 135 NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(mojom_type); | |
| 136 return false; | |
| 137 } | |
| 138 }; | |
| 139 | |
| 140 template <> | |
| 141 struct StructTraits<arc::mojom::BluetoothServiceDataDataView, | |
| 142 ServiceDataEntry> { | |
| 143 static bool Read(arc::mojom::BluetoothServiceDataDataView data, | |
| 144 ServiceDataEntry* output) { | |
| 145 output->service_uuid = data.uuid_16bit(); | |
| 146 data.ReadData(&output->service_data); | |
| 147 return true; | |
| 148 } | |
| 149 }; | |
| 150 | |
| 151 template <> | |
| 152 struct UnionTraits<arc::mojom::BluetoothAdvertisingDataDataView, | |
| 153 std::unique_ptr<AdvertisementEntry>> { | |
| 154 static bool Read(arc::mojom::BluetoothAdvertisingDataDataView data, | |
| 155 std::unique_ptr<AdvertisementEntry>* output) { | |
| 156 switch (data.tag()) { | |
| 157 case arc::mojom::BluetoothAdvertisingDataDataView::Tag::SERVICE_UUIDS: { | |
| 158 std::unique_ptr<ServiceUUIDEntry> service_uuids = | |
| 159 base::MakeUnique<ServiceUUIDEntry>(); | |
| 160 data.ReadServiceUuids(&service_uuids->service_uuids); | |
| 161 *output = std::move(service_uuids); | |
| 162 break; | |
| 163 } | |
| 164 case arc::mojom::BluetoothAdvertisingDataDataView::Tag::SERVICE_DATA: { | |
| 165 std::unique_ptr<ServiceDataEntry> service_data = | |
| 166 base::MakeUnique<ServiceDataEntry>(); | |
| 167 data.ReadServiceData(service_data.get()); | |
| 168 *output = std::move(service_data); | |
| 169 break; | |
| 170 } | |
| 171 case arc::mojom::BluetoothAdvertisingDataDataView::Tag:: | |
| 172 MANUFACTURER_DATA: { | |
| 173 std::unique_ptr<ManufacturerDataEntry> manufacturer_data = | |
| 174 base::MakeUnique<ManufacturerDataEntry>(); | |
| 175 // We get manufacturer data as a big blob. The first two bytes | |
| 176 // should be a company identifier code and the rest is manufacturer- | |
| 177 // specific. | |
| 178 std::vector<uint8_t> blob; | |
| 179 data.ReadManufacturerData(&blob); | |
| 180 if (blob.size() < sizeof(uint16_t)) { | |
| 181 LOG(WARNING) << "Advertisement had malformed manufacturer data"; | |
| 182 return false; | |
| 183 } | |
| 184 | |
| 185 manufacturer_data->company_id_code = | |
| 186 ExtractCompanyIdentifierCode(&blob); | |
| 187 manufacturer_data->blob = std::move(blob); | |
| 188 *output = std::move(manufacturer_data); | |
| 189 break; | |
| 190 } | |
| 191 default: { | |
| 192 LOG(WARNING) << "Bluetooth advertising data case not implemented"; | |
| 193 // Default AdvertisementEntry does nothing when added to the | |
| 194 // device::BluetoothAdvertisement::AdvertisementData, so data we | |
| 195 // don't know how to handle yet will be dropped but won't cause a | |
| 196 // failure to deserialize. | |
| 197 *output = base::MakeUnique<AdvertisementEntry>(); | |
| 198 break; | |
| 199 } | |
| 200 } | |
| 201 return true; | |
| 202 } | |
| 203 }; | |
| 204 | |
| 205 // static | |
| 206 bool StructTraits<arc::mojom::BluetoothAdvertisementDataView, | |
| 207 std::unique_ptr<device::BluetoothAdvertisement::Data>>:: | |
| 208 Read(arc::mojom::BluetoothAdvertisementDataView advertisement, | |
| 209 std::unique_ptr<device::BluetoothAdvertisement::Data>* output) { | |
| 210 device::BluetoothAdvertisement::AdvertisementType adv_type; | |
| 211 if (!advertisement.ReadType(&adv_type)) | |
| 212 return false; | |
| 213 auto data = base::MakeUnique<device::BluetoothAdvertisement::Data>(adv_type); | |
| 214 | |
| 215 std::vector<std::unique_ptr<AdvertisementEntry>> adv_entries; | |
| 216 if (!advertisement.ReadData(&adv_entries)) | |
| 217 return false; | |
| 218 for (const auto& adv_entry : adv_entries) | |
| 219 adv_entry->AddTo(data.get()); | |
| 220 | |
| 221 data->set_include_tx_power(advertisement.include_tx_power()); | |
| 222 | |
| 223 *output = std::move(data); | |
| 224 return true; | |
| 225 } | |
| 226 | |
| 70 } // namespace mojo | 227 } // namespace mojo |
| OLD | NEW |