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 af2e08b33ca1f97508d528d0c968156dc09906fb..5b9ce7bcd3ffb13c3b7d89bd14469e6b882193bd 100644 |
| --- a/components/arc/bluetooth/bluetooth_type_converters.cc |
| +++ b/components/arc/bluetooth/bluetooth_type_converters.cc |
| @@ -6,6 +6,7 @@ |
| #include <cctype> |
| #include <iomanip> |
| #include <ios> |
| +#include <set> |
| #include <sstream> |
| #include <string> |
| #include <vector> |
| @@ -19,6 +20,13 @@ |
| namespace { |
| +// SDP Service attribute IDs. |
| +constexpr uint16_t kServiceClassIDList = 0x0001; |
| +constexpr uint16_t kProtocolDescriptorList = 0x0004; |
| +constexpr uint16_t kBrowseGroupList = 0x0005; |
| +constexpr uint16_t kBluetoothProfileDescriptorList = 0x0009; |
| +constexpr uint16_t kServiceName = 0x0010; |
| + |
| bool IsNonHex(char c) { |
| return !isxdigit(c); |
| } |
| @@ -142,4 +150,256 @@ TypeConverter<arc::mojom::BluetoothGattStatus, |
| return ret; |
| } |
| +// static |
| +arc::mojom::BluetoothSdpServiceAttrPtr |
| +TypeConverter<arc::mojom::BluetoothSdpServiceAttrPtr, |
| + bluez::BluetoothServiceAttributeValueBlueZ>:: |
| + Convert(const bluez::BluetoothServiceAttributeValueBlueZ& attr_bluez) { |
| + arc::mojom::BluetoothSdpServiceAttrPtr result = |
| + arc::mojom::BluetoothSdpServiceAttr::New(); |
| + result->type = |
| + static_cast<arc::mojom::BluetoothSdpAttrType>(attr_bluez.type()); |
| + |
| + switch (attr_bluez.type()) { |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::NULLTYPE: |
| + break; |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::UINT: |
| + // Fall through |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::INT: { |
| + int32_t n; |
| + uint8_t* val; |
| + std::vector<uint8_t> data; |
| + if (!attr_bluez.value().GetAsInteger(&n)) break; |
| + switch (attr_bluez.size()) { |
| + case 1: { |
| + uint8_t v8 = static_cast<uint8_t>(n); |
| + val = reinterpret_cast<uint8_t*>(&v8); |
| + data.insert(data.begin(), val, val + attr_bluez.size()); |
| + break; |
| + } |
| + case 2: { |
| + uint16_t v16 = static_cast<uint16_t>(n); |
| + val = reinterpret_cast<uint8_t*>(&v16); |
| + data.insert(data.begin(), val, val + attr_bluez.size()); |
| + break; |
| + } |
| + case 4: { |
| + uint32_t v32 = static_cast<uint32_t>(n); |
| + val = reinterpret_cast<uint8_t*>(&v32); |
| + data.insert(data.begin(), val, val + attr_bluez.size()); |
| + break; |
| + } |
| + default: |
| + break; |
| + } |
| + result->value.Swap(&data); |
| + result->type_size = attr_bluez.size(); |
| + break; |
| + } |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::UUID: |
| + // Fall through |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::STRING: |
| + // Fall through |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::URL: { |
| + std::string str; |
| + if (!attr_bluez.value().GetAsString(&str)) break; |
| + std::copy(str.begin(), str.end(), result->value.begin()); |
| + break; |
| + } |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::BOOL: { |
| + bool b; |
| + if (!attr_bluez.value().GetAsBoolean(&b)) break; |
| + uint8_t v = b ? 1 : 0; |
| + result->value.push_back(v); |
| + result->type_size = result->value.size(); |
| + break; |
| + } |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::SEQUENCE: |
| + for (unsigned int i = 0; i < attr_bluez.sequence().size(); i++) { |
| + result->sequence.push_back( |
| + ConvertTo<arc::mojom::BluetoothSdpServiceAttrPtr>( |
| + attr_bluez.sequence().at(i))); |
| + } |
| + result->type_size = result->sequence.size(); |
| + break; |
| + default: |
| + break; |
| + } |
| + |
| + return result; |
| +} |
| + |
| +// static |
| +bluez::BluetoothServiceAttributeValueBlueZ |
| +TypeConverter<bluez::BluetoothServiceAttributeValueBlueZ, |
| + arc::mojom::BluetoothSdpServiceAttrPtr>:: |
| + Convert(const arc::mojom::BluetoothSdpServiceAttrPtr& attr) { |
| + uint32_t size = attr->type_size; |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type type = |
| + static_cast<bluez::BluetoothServiceAttributeValueBlueZ::Type>(attr->type); |
| + std::unique_ptr<base::Value> value; |
| + |
| + switch (attr->type) { |
| + case arc::mojom::BluetoothSdpAttrType::NULLTYPE: |
| + break; |
| + case arc::mojom::BluetoothSdpAttrType::UINT: |
| + // Fall through |
| + case arc::mojom::BluetoothSdpAttrType::INT: { |
| + if (size != attr->value.size()) break; |
| + |
| + void* v = attr->value.To<std::vector<uint8_t>>().data(); |
| + if (!v) break; |
| + |
| + switch (attr->type_size) { |
| + case 1: { |
| + uint8_t* v8 = static_cast<uint8_t*>(v); |
| + value = base::MakeUnique<base::FundamentalValue>( |
| + static_cast<int32_t>(*v8)); |
| + break; |
| + } |
| + case 2: { |
| + uint16_t* v16 = static_cast<uint16_t*>(v); |
| + value = base::MakeUnique<base::FundamentalValue>( |
| + static_cast<int32_t>(*v16)); |
| + break; |
| + } |
| + case 4: { |
| + uint32_t* v32 = static_cast<uint32_t*>(v); |
| + value = base::MakeUnique<base::FundamentalValue>( |
| + static_cast<int32_t>(*v32)); |
| + break; |
| + } |
| + default: |
| + value = base::Value::CreateNullValue(); |
| + break; |
| + } |
| + |
| + return bluez::BluetoothServiceAttributeValueBlueZ(type, size, |
| + std::move(value)); |
| + } |
| + case arc::mojom::BluetoothSdpAttrType::BOOL: |
| + if (size != attr->value.size() || attr->value.size() != 1) break; |
| + |
| + if (attr->value[0]) |
| + value = base::MakeUnique<base::FundamentalValue>(true); |
| + else |
| + value = base::MakeUnique<base::FundamentalValue>(false); |
| + |
| + return bluez::BluetoothServiceAttributeValueBlueZ(type, size, |
| + std::move(value)); |
| + case arc::mojom::BluetoothSdpAttrType::SEQUENCE: { |
| + std::unique_ptr<std::vector<bluez::BluetoothServiceAttributeValueBlueZ>> |
| + seq; |
|
Luis Héctor Chávez
2016/07/18 15:42:02
seq = base::MakeUnique<...>();
Miao
2016/07/26 07:20:45
Done.
|
| + |
| + for (unsigned int i = 0; i < attr->sequence.size(); i++) { |
| + bluez::BluetoothServiceAttributeValueBlueZ attr_bluez = |
| + mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + attr->sequence[i]); |
| + if (attr_bluez.type() != |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type::NULLTYPE) |
| + seq->push_back(attr_bluez); |
| + } |
| + return bluez::BluetoothServiceAttributeValueBlueZ(std::move(seq)); |
| + } |
| + case arc::mojom::BluetoothSdpAttrType::UUID: |
| + // Fall through |
| + case arc::mojom::BluetoothSdpAttrType::STRING: |
| + // Fall through |
| + case arc::mojom::BluetoothSdpAttrType::URL: { |
| + std::string str; |
| + if (size != attr->value.size()) break; |
| + |
| + std::copy(attr->value.begin(), attr->value.begin(), str.begin()); |
| + value = base::MakeUnique<base::StringValue>(str); |
| + return bluez::BluetoothServiceAttributeValueBlueZ(type, size, |
| + std::move(value)); |
| + break; |
| + } |
| + default: |
| + break; |
| + } |
| + return bluez::BluetoothServiceAttributeValueBlueZ( |
| + type, 0, base::Value::CreateNullValue()); |
| +} |
| + |
| +// static |
| +arc::mojom::BluetoothSdpRecordPtr |
| +TypeConverter<arc::mojom::BluetoothSdpRecordPtr, |
| + bluez::BluetoothServiceRecordBlueZ>:: |
| + Convert(const bluez::BluetoothServiceRecordBlueZ& rcd_bluez) { |
| + arc::mojom::BluetoothSdpRecordPtr result = |
| + arc::mojom::BluetoothSdpRecord::New(); |
| + |
| + std::vector<uint16_t> v = rcd_bluez.GetAttributeIds(); |
| + std::set<uint16_t> ids(v.begin(), v.end()); |
| + |
| + if (ids.find(kServiceClassIDList) != ids.end()) |
|
Luis Héctor Chávez
2016/07/18 15:42:02
What happens if you do
for (uint16_t id : ids) {
Miao
2016/07/26 07:20:45
Yes, I have changed Android side so that nulls wil
|
| + result->service_class_id_list = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(kServiceClassIDList)); |
| + else |
| + result->service_class_id_list = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + if (ids.find(kProtocolDescriptorList) != ids.end()) |
| + result->protocol_desc_list = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(kProtocolDescriptorList)); |
| + else |
| + result->protocol_desc_list = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + if (ids.find(kBrowseGroupList) != ids.end()) |
| + result->browse_group_list = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(kBrowseGroupList)); |
| + else |
| + result->browse_group_list = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + if (ids.find(kBluetoothProfileDescriptorList) != ids.end()) |
| + result->profile_desc_list = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(kBluetoothProfileDescriptorList)); |
| + else |
| + result->profile_desc_list = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + if (ids.find(kServiceName) != ids.end()) |
| + result->name = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(kServiceName)); |
| + else |
| + result->name = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + return result; |
| +} |
| + |
| +// static |
| +bluez::BluetoothServiceRecordBlueZ |
| +TypeConverter<bluez::BluetoothServiceRecordBlueZ, |
| + arc::mojom::BluetoothSdpRecordPtr>:: |
| + Convert(const arc::mojom::BluetoothSdpRecordPtr& rcd) { |
| + bluez::BluetoothServiceRecordBlueZ rcd_bluez; |
| + std::unique_ptr<bluez::BluetoothServiceAttributeValueBlueZ> attr_bluez; |
| + |
| + *attr_bluez = mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + rcd->service_class_id_list); |
| + if (attr_bluez->size() != 0) |
| + rcd_bluez.AddRecordEntry(kServiceClassIDList, *attr_bluez); |
| + |
| + *attr_bluez = mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + rcd->protocol_desc_list); |
| + if (attr_bluez->size() != 0) |
| + rcd_bluez.AddRecordEntry(kProtocolDescriptorList, *attr_bluez); |
| + |
| + *attr_bluez = mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + rcd->browse_group_list); |
| + if (attr_bluez->size() != 0) |
| + rcd_bluez.AddRecordEntry(kBrowseGroupList, *attr_bluez); |
| + |
| + *attr_bluez = mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + rcd->profile_desc_list); |
| + if (attr_bluez->size() != 0) |
| + rcd_bluez.AddRecordEntry(kBluetoothProfileDescriptorList, *attr_bluez); |
| + |
| + *attr_bluez = |
| + mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>(rcd->name); |
| + if (attr_bluez->size() != 0) |
| + rcd_bluez.AddRecordEntry(kServiceName, *attr_bluez); |
| + |
| + return rcd_bluez; |
| +} |
| + |
| } // namespace mojo |