Chromium Code Reviews| Index: components/arc/bluetooth/bluetooth_type_converters.cc |
| diff --git a/components/arc/bluetooth/bluetooth_type_converters.cc b/components/arc/bluetooth/bluetooth_type_converters.cc |
| index f91139d0434d9b248e64f0b3e9456cfd2ea3c44a..1e6d14af6f10fae43cb6f7680c03b39aef82be9b 100644 |
| --- a/components/arc/bluetooth/bluetooth_type_converters.cc |
| +++ b/components/arc/bluetooth/bluetooth_type_converters.cc |
| @@ -11,6 +11,7 @@ |
| #include <vector> |
| #include "base/strings/string_number_conversions.h" |
| +#include "base/strings/stringprintf.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "components/arc/bluetooth/bluetooth_type_converters.h" |
| #include "device/bluetooth/bluetooth_gatt_service.h" |
| @@ -150,4 +151,75 @@ TypeConverter<arc::mojom::BluetoothGattStatus, |
| return ret; |
| } |
| +// static |
| +device::BluetoothAdvertisement::AdvertisementType |
| +TypeConverter<device::BluetoothAdvertisement::AdvertisementType, |
| + arc::mojom::BluetoothAdvertisementType>:: |
| + Convert(const arc::mojom::BluetoothAdvertisementType& type) { |
| + switch (type) { |
| + case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_CONNECTABLE: |
| + case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_SCANNABLE: |
| + return device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_PERIPHERAL; |
| + case arc::mojom::BluetoothAdvertisementType::ADV_TYPE_NON_CONNECTABLE: |
| + return device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST; |
| + default: |
| + NOTREACHED() << "Invalid type: " << static_cast<uint32_t>(type); |
| + return device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST; |
| + } |
| +} |
| + |
| +// static |
| +std::unique_ptr<device::BluetoothAdvertisement::Data> |
| +TypeConverter<std::unique_ptr<device::BluetoothAdvertisement::Data>, |
| + arc::mojom::BluetoothAdvertisementPtr>:: |
| + Convert(const arc::mojom::BluetoothAdvertisementPtr& advertisement) { |
| + auto data = base::MakeUnique<device::BluetoothAdvertisement::Data>( |
| + ConvertTo<device::BluetoothAdvertisement::AdvertisementType>( |
| + advertisement->type)); |
| + |
| + for (const auto& adv_entry : advertisement->data) { |
| + if (adv_entry->is_service_uuids()) { |
| + std::vector<device::BluetoothUUID> adv_uuids = |
| + adv_entry->get_service_uuids() |
| + .To<std::vector<device::BluetoothUUID>>(); |
| + |
| + std::unique_ptr<std::vector<std::string>> uuid_list = |
| + base::MakeUnique<std::vector<std::string>>(); |
| + for (const auto& uuid : adv_uuids) { |
| + uuid_list->push_back(uuid.value()); |
| + } |
| + data->set_service_uuids(std::move(uuid_list)); |
| + } else if (adv_entry->is_service_data()) { |
| + std::string service_uuid = |
| + base::StringPrintf("%04x", adv_entry->get_service_data()->uuid_16bit); |
| + std::vector<uint8_t> service_data = |
| + adv_entry->get_service_data()->data.To<std::vector<uint8_t>>(); |
| + |
| + data->set_service_data( |
| + base::WrapUnique(new std::map<std::string, std::vector<uint8_t>>{ |
| + {service_uuid, service_data}})); |
| + } else if (adv_entry->is_manufacturer_data()) { |
| + // We get manufacturer data as a big blob. The first two bytes |
| + // should be a company identifier code and the rest is manufacturer- |
| + // specific. |
| + std::vector<uint8_t> blob = |
| + adv_entry->get_manufacturer_data().To<std::vector<uint8_t>>(); |
| + if (blob.size() < sizeof(uint16_t)) { |
| + LOG(WARNING) << "Advertisement had malformed manufacturer data"; |
| + return std::unique_ptr<device::BluetoothAdvertisement::Data>(); |
| + } |
| + |
| + // The company identifier code is in little-endian. |
| + uint16_t cic = blob[1] << 8 | blob[0]; |
| + blob.erase(blob.begin(), blob.begin() + sizeof(uint16_t)); |
| + data->set_manufacturer_data(base::WrapUnique( |
| + new std::map<uint16_t, std::vector<uint8_t>>{{cic, blob}})); |
| + } |
|
puthik_chromium
2016/08/19 23:43:24
else NOTREACHED();
Eric Caruso
2016/08/20 00:33:17
Not sure we need NOTREACHED() here. We might have
|
| + } |
| + |
| + data->set_include_tx_power(advertisement->include_tx_power); |
| + |
| + return data; |
| +} |
| + |
| } // namespace mojo |