Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "device/bluetooth/bluetooth_service_record_chromeos.h" | 5 #include "device/bluetooth/bluetooth_service_record_chromeos.h" |
| 6 | 6 |
| 7 #include <bluetooth/bluetooth.h> | 7 #include <bluetooth/bluetooth.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "device/bluetooth/bluetooth_utils.h" | 14 #include "device/bluetooth/bluetooth_utils.h" |
| 15 #include "third_party/libxml/chromium/libxml_utils.h" | 15 #include "third_party/libxml/chromium/libxml_utils.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 static const char* kAttributeNode = "attribute"; | 19 static const char* kAttributeNode = "attribute"; |
| 20 static const char* kIdAttribute = "id"; | 20 static const char* kBooleanNode = "boolean"; |
| 21 static const char* kProtocolDescriptorListId = "0x0004"; | |
| 22 static const char* kRfcommUuid = "0x0003"; | |
| 23 static const char* kSdpNameId = "0x0100"; | |
| 24 static const char* kSequenceNode = "sequence"; | 21 static const char* kSequenceNode = "sequence"; |
| 25 static const char* kTextNode = "text"; | 22 static const char* kTextNode = "text"; |
| 26 static const char* kUint8Node = "uint8"; | 23 static const char* kUint8Node = "uint8"; |
| 27 static const char* kUuidId = "0x0001"; | |
| 28 static const char* kUuidNode = "uuid"; | 24 static const char* kUuidNode = "uuid"; |
| 25 | |
| 26 static const char* kIdAttribute = "id"; | |
| 29 static const char* kValueAttribute = "value"; | 27 static const char* kValueAttribute = "value"; |
| 28 static const char* kValueTrue = "true"; | |
| 29 | |
| 30 static const char* kProtocolDescriptorListId = "0x0004"; | |
| 31 static const char* kSdpNameId = "0x0100"; | |
| 32 static const char* kServiceClassUuidId = "0x0001"; | |
| 33 | |
| 34 static const char* kHidNormallyConnectable = "0x020d"; | |
| 35 static const char* kHidReconnectInitiate = "0x0205"; | |
|
keybuk
2013/02/22 23:50:03
These should be named "Id" to match the others, no
| |
| 36 | |
| 37 static const char* kProtocolRfcommUuid = "0x0003"; | |
| 38 static const char* kProtocolHidpUuid = "0x0011"; | |
| 30 | 39 |
| 31 bool AdvanceToTag(XmlReader* reader, const char* node_type) { | 40 bool AdvanceToTag(XmlReader* reader, const char* node_type) { |
| 32 do { | 41 do { |
| 33 if (!reader->Read()) | 42 if (!reader->Read()) |
| 34 return false; | 43 return false; |
| 35 } while (reader->NodeName() != node_type); | 44 } while (reader->NodeName() != node_type); |
| 36 return true; | 45 return true; |
| 37 } | 46 } |
| 38 | 47 |
| 39 bool ExtractTextValue(XmlReader* reader, std::string* value_out) { | 48 bool ExtractTextValue(XmlReader* reader, std::string* value_out) { |
| 40 if (AdvanceToTag(reader, kTextNode)) { | 49 if (AdvanceToTag(reader, kTextNode)) { |
| 41 reader->NodeAttribute(kValueAttribute, value_out); | 50 reader->NodeAttribute(kValueAttribute, value_out); |
| 42 return true; | 51 return true; |
| 43 } | 52 } |
| 44 return false; | 53 return false; |
| 45 } | 54 } |
| 46 | 55 |
| 56 bool ExtractBooleanValue(XmlReader* reader, bool* value_out) { | |
| 57 if (AdvanceToTag(reader, kBooleanNode)) { | |
| 58 std::string str_value; | |
| 59 if (!reader->NodeAttribute(kValueAttribute, &str_value)) | |
| 60 return false; | |
| 61 *value_out = str_value == kValueTrue; | |
| 62 return true; | |
| 63 } | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 47 } // namespace | 67 } // namespace |
| 48 | 68 |
| 49 namespace chromeos { | 69 namespace chromeos { |
| 50 | 70 |
| 51 BluetoothServiceRecordChromeOS::BluetoothServiceRecordChromeOS( | 71 BluetoothServiceRecordChromeOS::BluetoothServiceRecordChromeOS( |
| 52 const std::string& address, | 72 const std::string& address, |
| 53 const std::string& xml_data) { | 73 const std::string& xml_data) { |
| 54 address_ = address; | 74 address_ = address; |
| 55 supports_rfcomm_ = false; | 75 supports_rfcomm_ = false; |
| 76 supports_hid_ = false; | |
| 77 | |
| 78 // For HID services the default is false when the attribute is not present. | |
| 79 hid_reconnect_initiate_ = false; | |
| 80 hid_normally_connectable_ = false; | |
| 56 | 81 |
| 57 XmlReader reader; | 82 XmlReader reader; |
| 58 if (!reader.Load(xml_data)) | 83 if (!reader.Load(xml_data)) |
| 59 return; | 84 return; |
| 60 | 85 |
| 61 while (AdvanceToTag(&reader, kAttributeNode)) { | 86 while (AdvanceToTag(&reader, kAttributeNode)) { |
| 62 std::string id; | 87 std::string id; |
| 63 if (reader.NodeAttribute(kIdAttribute, &id)) { | 88 if (reader.NodeAttribute(kIdAttribute, &id)) { |
| 64 if (id == kSdpNameId) { | 89 if (id == kSdpNameId) { |
| 65 ExtractTextValue(&reader, &name_); | 90 ExtractTextValue(&reader, &name_); |
| 66 } else if (id == kProtocolDescriptorListId) { | 91 } else if (id == kProtocolDescriptorListId) { |
| 67 if (AdvanceToTag(&reader, kSequenceNode)) { | 92 if (AdvanceToTag(&reader, kSequenceNode)) { |
| 68 ExtractChannels(&reader); | 93 ExtractProtocolDescriptors(&reader); |
| 69 } | 94 } |
| 70 } else if (id == kUuidId) { | 95 } else if (id == kServiceClassUuidId) { |
| 71 if (AdvanceToTag(&reader, kSequenceNode)) { | 96 if (AdvanceToTag(&reader, kSequenceNode)) { |
| 72 ExtractUuid(&reader); | 97 ExtractServiceClassUuid(&reader); |
| 73 } | 98 } |
| 99 } else if (id == kHidNormallyConnectable) { | |
| 100 ExtractBooleanValue(&reader, &hid_normally_connectable_); | |
| 101 } else if (id == kHidReconnectInitiate) { | |
| 102 ExtractBooleanValue(&reader, &hid_reconnect_initiate_); | |
| 74 } | 103 } |
| 75 } | 104 } |
| 76 // We don't care about anything else here, so find the closing tag | 105 // We don't care about anything else here, so find the closing tag |
| 77 AdvanceToTag(&reader, kAttributeNode); | 106 AdvanceToTag(&reader, kAttributeNode); |
| 78 } | 107 } |
| 108 if (!supports_hid_) { | |
| 109 // For non-HID services the default is true. | |
| 110 hid_normally_connectable_ = true; | |
| 111 hid_reconnect_initiate_ = true; | |
| 112 } | |
| 79 } | 113 } |
| 80 | 114 |
| 81 void BluetoothServiceRecordChromeOS::GetBluetoothAddress( | 115 void BluetoothServiceRecordChromeOS::GetBluetoothAddress( |
| 82 bdaddr_t* out_address) const { | 116 bdaddr_t* out_address) const { |
| 83 std::string numbers_only; | 117 std::string numbers_only; |
| 84 for (int i = 0; i < 6; ++i) | 118 for (int i = 0; i < 6; ++i) |
| 85 numbers_only += address_.substr(i * 3, 2); | 119 numbers_only += address_.substr(i * 3, 2); |
| 86 | 120 |
| 87 std::vector<uint8> address_bytes; | 121 std::vector<uint8> address_bytes; |
| 88 base::HexStringToBytes(numbers_only, &address_bytes); | 122 base::HexStringToBytes(numbers_only, &address_bytes); |
| 89 for (int i = 0; i < 6; ++i) | 123 for (int i = 0; i < 6; ++i) |
| 90 out_address->b[5 - i] = address_bytes[i]; | 124 out_address->b[5 - i] = address_bytes[i]; |
| 91 } | 125 } |
| 92 | 126 |
| 93 void BluetoothServiceRecordChromeOS::ExtractChannels(XmlReader* reader) { | 127 void BluetoothServiceRecordChromeOS::ExtractProtocolDescriptors( |
| 128 XmlReader* reader) { | |
| 94 const int start_depth = reader->Depth(); | 129 const int start_depth = reader->Depth(); |
| 130 // The ProtocolDescriptorList can have one or more sequence of sequence of | |
| 131 // stack, where each stack starts with an UUID and the remaining tags (if | |
| 132 // present) are protocol-specific. | |
| 95 do { | 133 do { |
| 96 if (reader->NodeName() == kSequenceNode) { | 134 if (reader->NodeName() == kSequenceNode) { |
| 97 if (AdvanceToTag(reader, kUuidNode)) { | 135 if (AdvanceToTag(reader, kUuidNode)) { |
| 98 std::string type; | 136 std::string protocolUuid; |
| 99 if (reader->NodeAttribute(kValueAttribute, &type) && | 137 if (reader->NodeAttribute(kValueAttribute, &protocolUuid)) { |
| 100 type == kRfcommUuid) { | 138 // Per protocol parameters parsing. |
| 101 if (AdvanceToTag(reader, kUint8Node)) { | 139 if (protocolUuid == kProtocolRfcommUuid) { |
| 102 std::string channel_string; | 140 if (AdvanceToTag(reader, kUint8Node)) { |
| 103 if (reader->NodeAttribute(kValueAttribute, &channel_string)) { | 141 std::string channel_string; |
| 104 std::vector<uint8> channel_bytes; | 142 if (reader->NodeAttribute(kValueAttribute, &channel_string)) { |
| 105 if (base::HexStringToBytes(channel_string.substr(2), | 143 std::vector<uint8> channel_bytes; |
| 106 &channel_bytes)) { | 144 if (base::HexStringToBytes(channel_string.substr(2), |
| 107 if (channel_bytes.size() == 1) { | 145 &channel_bytes)) { |
| 108 rfcomm_channel_ = channel_bytes[0]; | 146 if (channel_bytes.size() == 1) { |
| 109 supports_rfcomm_ = true; | 147 rfcomm_channel_ = channel_bytes[0]; |
| 148 supports_rfcomm_ = true; | |
| 149 } | |
| 110 } | 150 } |
| 111 } | 151 } |
| 112 } | 152 } |
| 153 } else if (protocolUuid == kProtocolHidpUuid) { | |
| 154 supports_hid_ = true; | |
| 113 } | 155 } |
| 114 } | 156 } |
| 115 } | 157 } |
| 116 } | 158 } |
| 117 } while (AdvanceToTag(reader, kSequenceNode) && | 159 } while (AdvanceToTag(reader, kSequenceNode) && |
| 118 reader->Depth() != start_depth); | 160 reader->Depth() != start_depth); |
| 119 } | 161 } |
| 120 | 162 |
| 121 void BluetoothServiceRecordChromeOS::ExtractUuid(XmlReader* reader) { | 163 void BluetoothServiceRecordChromeOS::ExtractServiceClassUuid( |
| 164 XmlReader* reader) { | |
| 122 const int start_depth = reader->Depth(); | 165 const int start_depth = reader->Depth(); |
| 123 do { | 166 do { |
| 124 if (reader->NodeName() == kSequenceNode) { | 167 if (reader->NodeName() == kSequenceNode) { |
| 125 if (AdvanceToTag(reader, kUuidNode)) { | 168 if (AdvanceToTag(reader, kUuidNode)) { |
| 126 if (!reader->NodeAttribute(kValueAttribute, &uuid_)) | 169 if (!reader->NodeAttribute(kValueAttribute, &uuid_)) |
| 127 uuid_.clear(); | 170 uuid_.clear(); |
| 128 } | 171 } |
| 129 } | 172 } |
| 130 } while (AdvanceToTag(reader, kSequenceNode) && | 173 } while (AdvanceToTag(reader, kSequenceNode) && |
| 131 reader->Depth() != start_depth); | 174 reader->Depth() != start_depth); |
| 132 | 175 |
| 133 uuid_ = device::bluetooth_utils::CanonicalUuid(uuid_); | 176 uuid_ = device::bluetooth_utils::CanonicalUuid(uuid_); |
| 134 } | 177 } |
| 135 | 178 |
| 136 } // namespace chromeos | 179 } // namespace chromeos |
| OLD | NEW |