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..0716fcd85446a8fca0706bee83f387176ad9963f 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> |
| @@ -31,6 +32,19 @@ std::string StripNonHex(const std::string& str) { |
| return result; |
| } |
| +void* bytes_to_value(const mojo::Array<uint8_t>& bytes) { |
| + if (bytes.empty()) |
| + return nullptr; |
| + |
| + int size = static_cast<int>(bytes.size()); |
| + uint8_t* value = (uint8_t*)malloc(sizeof(uint8_t) * size); |
|
Luis Héctor Chávez
2016/07/14 23:13:14
avoid using malloc/raw pointers at all costs. Let'
Miao
2016/07/15 08:39:21
I'd be more than happy to use the base::Value mars
Luis Héctor Chávez
2016/07/18 15:42:02
On it :) Also, my apologies, the snippet should ha
Miao
2016/07/26 07:20:45
Done.
|
| + if (!value) |
| + return nullptr; |
| + |
| + std::copy(bytes.begin(), bytes.end(), value); |
| + return value; |
| +} |
| + |
| } // namespace |
| namespace mojo { |
| @@ -142,4 +156,262 @@ 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()); |
| + int32_t n; |
|
Luis Héctor Chávez
2016/07/14 23:13:14
Avoid this by adding braces to each case and decla
Miao
2016/07/15 08:39:21
Done.
|
| + uint8_t* val; |
| + uint8_t v8; |
| + uint16_t v16; |
| + uint32_t v32; |
| + bool b; |
| + std::vector<uint8_t> data; |
| + std::string str; |
| + |
| + switch(attr_bluez.type()) { |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::NULLTYPE: |
| + break; |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::UINT: |
| + // Fall through |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::INT: |
| + if (!attr_bluez.value().GetAsInteger(&n)) break; |
| + switch (attr_bluez.size()) { |
| + case 1: |
| + v8 = static_cast<uint8_t>(n); |
| + val = reinterpret_cast<uint8_t*>(&v8); |
| + data.insert(data.begin(), val, val + attr_bluez.size()); |
| + break; |
| + case 2: |
| + v16 = static_cast<uint16_t>(n); |
| + val = reinterpret_cast<uint8_t*>(&v16); |
| + data.insert(data.begin(), val, val + attr_bluez.size()); |
| + break; |
| + case 4: |
| + 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: |
| + if (!attr_bluez.value().GetAsString(&str)) break; |
| + std::copy(str.begin(), str.end(), result->value.begin()); |
| + break; |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::BOOL: |
| + if (!attr_bluez.value().GetAsBoolean(&b)) break; |
| + v8 = b ? 1 : 0; |
| + result->value.push_back(v8); |
| + result->type_size = result->value.size(); |
| + break; |
| + case bluez::BluetoothServiceAttributeValueBlueZ::Type::SEQUENCE: |
| + for (unsigned int i; i < attr_bluez.sequence().size(); i++) |
| + result->sequence.push_back( |
| + std::move(ConvertTo<arc::mojom::BluetoothSdpServiceAttrPtr>( |
| + attr_bluez.sequence().at(i)))); |
| + result->type_size = result->sequence.size(); |
| + break; |
| + default: |
| + break; |
| + } |
| + |
| + return std::move(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; |
| + |
| + void* v; |
| + uint8_t* v8; |
| + uint16_t* v16; |
| + uint32_t* v32; |
| + std::string str; |
| + std::unique_ptr<std::vector<bluez::BluetoothServiceAttributeValueBlueZ>> seq; |
| + |
| + 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; |
| + v = bytes_to_value(attr->value); |
| + if (!v) break; |
| + |
| + switch(attr->type_size) { |
| + case 1: |
| + v8 = static_cast<uint8_t*>(v); |
| + value = base::MakeUnique<base::FundamentalValue>( |
| + static_cast<int32_t>(*v8)); |
| + break; |
| + case 2: |
| + v16 = static_cast<uint16_t*>(v); |
| + value = base::MakeUnique<base::FundamentalValue>( |
| + static_cast<int32_t>(*v16)); |
| + break; |
| + case 4: |
| + v32 = static_cast<uint32_t*>(v); |
| + value = base::MakeUnique<base::FundamentalValue>( |
| + static_cast<int32_t>(*v32)); |
| + break; |
| + default: |
| + value = base::Value::CreateNullValue(); |
| + break; |
| + } |
| + |
| + free(v); |
| + 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: |
| + 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: |
| + 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(); |
| + |
| + // 0x0001 (ServiceClassIDList), 0x0004 (ProtocolDescriptorList), |
| + // 0x0005 (BrowseGroupList), 0x0009 (BluetoothProfileDescriptorList), |
| + // 0x0010 (ServiceName) |
| + uint16_t target_ids[] = {0x0001, 0x0004, 0x0005, 0x0009, 0x0100}; |
|
Luis Héctor Chávez
2016/07/14 23:13:14
this is not being used as an array, so it's better
Miao
2016/07/15 08:39:21
Done.
|
| + std::vector<uint16_t> v = rcd_bluez.GetAttributeIds(); |
| + std::set<uint16_t> ids (v.begin(), v.end()); |
| + |
| + if (ids.find(target_ids[0]) != ids.end()) |
| + result->service_class_id_list = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(target_ids[0])); |
| + else |
| + result->service_class_id_list = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + if (ids.find(target_ids[1]) != ids.end()) |
| + result->protocol_desc_list = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(target_ids[1])); |
| + else |
| + result->protocol_desc_list = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + if (ids.find(target_ids[2]) != ids.end()) |
| + result->browse_group_list = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(target_ids[2])); |
| + else |
| + result->browse_group_list = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + if (ids.find(target_ids[3]) != ids.end()) |
| + result->profile_desc_list = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(target_ids[3])); |
| + else |
| + result->profile_desc_list = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + if (ids.find(target_ids[4]) != ids.end()) |
| + result->name = arc::mojom::BluetoothSdpServiceAttr::From( |
| + rcd_bluez.GetAttributeValue(target_ids[4])); |
| + else |
| + result->name = arc::mojom::BluetoothSdpServiceAttr::New(); |
| + |
| + return std::move(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; |
| + |
| + // 0x0001 (ServiceClassIDList), 0x0004 (ProtocolDescriptorList), |
| + // 0x0005 (BrowseGroupList), 0x0009 (BluetoothProfileDescriptorList), |
| + // 0x0010 (ServiceName) |
| + uint16_t target_ids[] = {0x0001, 0x0004, 0x0005, 0x0009, 0x0100}; |
| + *attr_bluez = mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + rcd->service_class_id_list); |
| + if (attr_bluez->type() != |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type::NULLTYPE) |
| + rcd_bluez.AddRecordEntry(target_ids[0], *attr_bluez); |
| + |
| + *attr_bluez = mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + rcd->protocol_desc_list); |
| + if (attr_bluez->type() != |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type::NULLTYPE) |
| + rcd_bluez.AddRecordEntry(target_ids[1], *attr_bluez); |
| + |
| + *attr_bluez = mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + rcd->browse_group_list); |
| + if (attr_bluez->type() != |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type::NULLTYPE) |
| + rcd_bluez.AddRecordEntry(target_ids[2], *attr_bluez); |
| + |
| + *attr_bluez = mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>( |
| + rcd->profile_desc_list); |
| + if (attr_bluez->type() != |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type::NULLTYPE) |
| + rcd_bluez.AddRecordEntry(target_ids[3], *attr_bluez); |
| + |
| + *attr_bluez = |
| + mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>(rcd->name); |
| + if (attr_bluez->type() != |
| + bluez::BluetoothServiceAttributeValueBlueZ::Type::NULLTYPE) |
| + rcd_bluez.AddRecordEntry(target_ids[4], *attr_bluez); |
| + |
| + return rcd_bluez; |
| +} |
| + |
| } // namespace mojo |