OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" | 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
| 8 #include <vector> |
8 | 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/string_number_conversions.h" |
9 #include "chrome/common/libxml_utils.h" | 12 #include "chrome/common/libxml_utils.h" |
10 | 13 |
11 namespace { | 14 namespace { |
12 | 15 |
13 static const char* kAttributeNode = "attribute"; | 16 static const char* kAttributeNode = "attribute"; |
14 static const char* kIdAttribute = "id"; | 17 static const char* kIdAttribute = "id"; |
| 18 static const char* kProtocolDescriptorListId = "0x0004"; |
| 19 static const char* kRfcommUuid = "0x0003"; |
15 static const char* kSdpNameId = "0x0100"; | 20 static const char* kSdpNameId = "0x0100"; |
| 21 static const char* kSequenceNode = "sequence"; |
16 static const char* kTextNode = "text"; | 22 static const char* kTextNode = "text"; |
| 23 static const char* kUint8Node = "uint8"; |
| 24 static const char* kUuidNode = "uuid"; |
17 static const char* kValueAttribute = "value"; | 25 static const char* kValueAttribute = "value"; |
18 | 26 |
19 bool advanceToTag(XmlReader* reader, const char* node_type) { | 27 bool AdvanceToTag(XmlReader* reader, const char* node_type) { |
20 do { | 28 do { |
21 if (!reader->Read()) | 29 if (!reader->Read()) |
22 return false; | 30 return false; |
23 } while (reader->NodeName() != node_type); | 31 } while (reader->NodeName() != node_type); |
24 return true; | 32 return true; |
25 } | 33 } |
26 | 34 |
| 35 bool ExtractTextValue(XmlReader* reader, std::string* value_out) { |
| 36 if (AdvanceToTag(reader, kTextNode)) { |
| 37 reader->NodeAttribute(kValueAttribute, value_out); |
| 38 return true; |
| 39 } |
| 40 return false; |
| 41 } |
| 42 |
27 } // namespace | 43 } // namespace |
28 | 44 |
29 namespace chromeos { | 45 namespace chromeos { |
30 | 46 |
31 BluetoothServiceRecord::BluetoothServiceRecord(const std::string& xml_data) { | 47 BluetoothServiceRecord::BluetoothServiceRecord( |
| 48 const std::string& address, |
| 49 const std::string& xml_data) |
| 50 : address_(address), |
| 51 supports_rfcomm_(false) { |
| 52 |
32 XmlReader reader; | 53 XmlReader reader; |
33 if (!reader.Load(xml_data)) | 54 if (!reader.Load(xml_data)) |
34 return; | 55 return; |
35 | 56 |
36 while (advanceToTag(&reader, kAttributeNode)) { | 57 while (AdvanceToTag(&reader, kAttributeNode)) { |
37 std::string id; | 58 std::string id; |
38 if (reader.NodeAttribute(kIdAttribute, &id)) { | 59 if (reader.NodeAttribute(kIdAttribute, &id)) { |
39 if (id == kSdpNameId) { | 60 if (id == kSdpNameId) { |
40 if (advanceToTag(&reader, kTextNode)) { | 61 ExtractTextValue(&reader, &name_); |
41 std::string value; | 62 } else if (id == kProtocolDescriptorListId) { |
42 if (reader.NodeAttribute(kValueAttribute, &value)) | 63 if (AdvanceToTag(&reader, kSequenceNode)) { |
43 name_ = value; | 64 ExtractChannels(&reader); |
44 } | 65 } |
45 } | 66 } |
46 } | 67 } |
47 // We don't care about anything else here, so find the closing tag | 68 // We don't care about anything else here, so find the closing tag |
48 advanceToTag(&reader, kAttributeNode); | 69 AdvanceToTag(&reader, kAttributeNode); |
49 } | 70 } |
50 } | 71 } |
51 | 72 |
| 73 void BluetoothServiceRecord::ExtractChannels(XmlReader* reader) { |
| 74 const int start_depth = reader->Depth(); |
| 75 do { |
| 76 if (reader->NodeName() == kSequenceNode) { |
| 77 if (AdvanceToTag(reader, kUuidNode)) { |
| 78 std::string type; |
| 79 if (reader->NodeAttribute(kValueAttribute, &type) && |
| 80 type == kRfcommUuid) { |
| 81 if (AdvanceToTag(reader, kUint8Node)) { |
| 82 std::string channel_string; |
| 83 if (reader->NodeAttribute(kValueAttribute, &channel_string)) { |
| 84 std::vector<uint8> channel_bytes; |
| 85 if (base::HexStringToBytes(channel_string.substr(2), |
| 86 &channel_bytes)) { |
| 87 if (channel_bytes.size() == 1) { |
| 88 rfcomm_channel_ = channel_bytes[0]; |
| 89 supports_rfcomm_ = true; |
| 90 } |
| 91 } |
| 92 } |
| 93 } |
| 94 } |
| 95 } |
| 96 } |
| 97 } while (AdvanceToTag(reader, kSequenceNode) && |
| 98 reader->Depth() != start_depth); |
| 99 } |
| 100 |
52 } // namespace chromeos | 101 } // namespace chromeos |
OLD | NEW |