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(const std::string& address, |
48 const std::string& xml_data) | |
keybuk
2012/04/19 23:30:11
nit: style guide requires that the first arg be on
bryeung
2012/04/20 00:17:56
Done.
| |
49 : address_(address), | |
50 supports_rfcomm_(false) { | |
51 | |
32 XmlReader reader; | 52 XmlReader reader; |
33 if (!reader.Load(xml_data)) | 53 if (!reader.Load(xml_data)) |
34 return; | 54 return; |
35 | 55 |
36 while (advanceToTag(&reader, kAttributeNode)) { | 56 while (AdvanceToTag(&reader, kAttributeNode)) { |
37 std::string id; | 57 std::string id; |
38 if (reader.NodeAttribute(kIdAttribute, &id)) { | 58 if (reader.NodeAttribute(kIdAttribute, &id)) { |
39 if (id == kSdpNameId) { | 59 if (id == kSdpNameId) { |
40 if (advanceToTag(&reader, kTextNode)) { | 60 ExtractTextValue(&reader, &name_); |
41 std::string value; | 61 } else if (id == kProtocolDescriptorListId) { |
42 if (reader.NodeAttribute(kValueAttribute, &value)) | 62 if (AdvanceToTag(&reader, kSequenceNode)) { |
43 name_ = value; | 63 ExtractChannels(&reader); |
44 } | 64 } |
45 } | 65 } |
46 } | 66 } |
47 // We don't care about anything else here, so find the closing tag | 67 // We don't care about anything else here, so find the closing tag |
48 advanceToTag(&reader, kAttributeNode); | 68 AdvanceToTag(&reader, kAttributeNode); |
49 } | 69 } |
50 } | 70 } |
51 | 71 |
72 void BluetoothServiceRecord::ExtractChannels(XmlReader* reader) { | |
73 const int start_depth = reader->Depth(); | |
74 do { | |
75 if (reader->NodeName() == kSequenceNode) { | |
76 if (AdvanceToTag(reader, kUuidNode)) { | |
77 std::string type; | |
78 if (reader->NodeAttribute(kValueAttribute, &type) && | |
79 type == kRfcommUuid) { | |
80 if (AdvanceToTag(reader, kUint8Node)) { | |
81 std::string channel_string; | |
82 if (reader->NodeAttribute(kValueAttribute, &channel_string)) { | |
83 std::vector<uint8> channel_bytes; | |
84 if (base::HexStringToBytes(channel_string.substr(2), | |
85 &channel_bytes)) { | |
86 if (channel_bytes.size() == 1) { | |
87 rfcomm_channel_ = channel_bytes[0]; | |
88 supports_rfcomm_ = true; | |
89 } | |
90 } | |
91 } | |
92 } | |
93 } | |
94 } | |
95 } | |
96 } while (AdvanceToTag(reader, kSequenceNode) && | |
97 reader->Depth() != start_depth); | |
98 } | |
99 | |
52 } // namespace chromeos | 100 } // namespace chromeos |
OLD | NEW |