| 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..d5deb36cb65ba231e722a2de5a6d5f7d2c81db15 100644
|
| --- a/components/arc/bluetooth/bluetooth_type_converters.cc
|
| +++ b/components/arc/bluetooth/bluetooth_type_converters.cc
|
| @@ -6,6 +6,9 @@
|
| #include <cctype>
|
| #include <iomanip>
|
| #include <ios>
|
| +#include <map>
|
| +#include <memory>
|
| +#include <set>
|
| #include <sstream>
|
| #include <string>
|
| #include <vector>
|
| @@ -19,6 +22,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 = 0x0100;
|
| +
|
| bool IsNonHex(char c) {
|
| return !isxdigit(c);
|
| }
|
| @@ -142,4 +152,277 @@ 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 = base::MakeUnique<
|
| + std::vector<bluez::BluetoothServiceAttributeValueBlueZ>>();
|
| +
|
| + 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());
|
| +
|
| + for (auto it : v) {
|
| + switch (it) {
|
| + case kServiceClassIDList:
|
| + result->attrs[kServiceClassIDList] =
|
| + arc::mojom::BluetoothSdpServiceAttr::From(
|
| + rcd_bluez.GetAttributeValue(kServiceClassIDList));
|
| + break;
|
| + case kProtocolDescriptorList:
|
| + result->attrs[kProtocolDescriptorList] =
|
| + arc::mojom::BluetoothSdpServiceAttr::From(
|
| + rcd_bluez.GetAttributeValue(kProtocolDescriptorList));
|
| + break;
|
| + case kBrowseGroupList:
|
| + result->attrs[kBrowseGroupList] =
|
| + arc::mojom::BluetoothSdpServiceAttr::From(
|
| + rcd_bluez.GetAttributeValue(kBrowseGroupList));
|
| + break;
|
| + case kBluetoothProfileDescriptorList:
|
| + result->attrs[kBluetoothProfileDescriptorList] =
|
| + arc::mojom::BluetoothSdpServiceAttr::From(
|
| + rcd_bluez.GetAttributeValue(kBluetoothProfileDescriptorList));
|
| + break;
|
| + case kServiceName:
|
| + result->attrs[kServiceName] = arc::mojom::BluetoothSdpServiceAttr::From(
|
| + rcd_bluez.GetAttributeValue(kServiceName));
|
| + break;
|
| + default:
|
| + break;
|
| + }
|
| + }
|
| +
|
| + 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;
|
| + std::map<uint16_t, arc::mojom::BluetoothSdpServiceAttrPtr>::const_iterator it;
|
| +
|
| + for (it = rcd->attrs.begin(); it != rcd->attrs.end(); it++) {
|
| + switch (it->first) {
|
| + case kServiceClassIDList:
|
| + *attr_bluez =
|
| + mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>(
|
| + it->second);
|
| + if (attr_bluez->size() != 0)
|
| + rcd_bluez.AddRecordEntry(kServiceClassIDList, *attr_bluez);
|
| + break;
|
| + case kProtocolDescriptorList:
|
| + *attr_bluez =
|
| + mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>(
|
| + it->second);
|
| + if (attr_bluez->size() != 0)
|
| + rcd_bluez.AddRecordEntry(kProtocolDescriptorList, *attr_bluez);
|
| + break;
|
| + case kBrowseGroupList:
|
| + *attr_bluez =
|
| + mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>(
|
| + it->second);
|
| + if (attr_bluez->size() != 0)
|
| + rcd_bluez.AddRecordEntry(kBrowseGroupList, *attr_bluez);
|
| + break;
|
| + case kBluetoothProfileDescriptorList:
|
| + *attr_bluez =
|
| + mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>(
|
| + it->second);
|
| + if (attr_bluez->size() != 0)
|
| + rcd_bluez.AddRecordEntry(kBluetoothProfileDescriptorList,
|
| + *attr_bluez);
|
| + break;
|
| + case kServiceName:
|
| + *attr_bluez =
|
| + mojo::ConvertTo<bluez::BluetoothServiceAttributeValueBlueZ>(
|
| + it->second);
|
| + if (attr_bluez->size() != 0)
|
| + rcd_bluez.AddRecordEntry(kServiceName, *attr_bluez);
|
| + break;
|
| + default:
|
| + break;
|
| + }
|
| + }
|
| +
|
| + return rcd_bluez;
|
| +}
|
| +
|
| } // namespace mojo
|
|
|