Chromium Code Reviews| 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 | 8 |
| 9 #include "base/logging.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 9 #include "chrome/common/libxml_utils.h" | 11 #include "chrome/common/libxml_utils.h" |
| 10 | 12 |
| 11 namespace { | 13 namespace { |
| 12 | 14 |
| 13 static const char* kAttributeNode = "attribute"; | 15 static const char* kAttributeNode = "attribute"; |
| 14 static const char* kIdAttribute = "id"; | 16 static const char* kIdAttribute = "id"; |
| 17 static const char* kProtocolDescriptorListId = "0x0004"; | |
| 18 static const char* kRfcommUuid = "0x0003"; | |
| 15 static const char* kSdpNameId = "0x0100"; | 19 static const char* kSdpNameId = "0x0100"; |
| 20 static const char* kSequenceNode = "sequence"; | |
| 16 static const char* kTextNode = "text"; | 21 static const char* kTextNode = "text"; |
| 22 static const char* kUint8Node = "uint8"; | |
| 23 static const char* kUuidNode = "uuid"; | |
| 17 static const char* kValueAttribute = "value"; | 24 static const char* kValueAttribute = "value"; |
| 18 | 25 |
| 19 bool advanceToTag(XmlReader* reader, const char* node_type) { | 26 bool advanceToTag(XmlReader* reader, const char* node_type) { |
| 20 do { | 27 do { |
| 21 if (!reader->Read()) | 28 if (!reader->Read()) |
| 22 return false; | 29 return false; |
| 23 } while (reader->NodeName() != node_type); | 30 } while (reader->NodeName() != node_type); |
| 24 return true; | 31 return true; |
| 25 } | 32 } |
| 26 | 33 |
| 27 } // namespace | 34 } // namespace |
| 28 | 35 |
| 29 namespace chromeos { | 36 namespace chromeos { |
| 30 | 37 |
| 31 bool BluetoothServiceRecord::Init(const std::string& xml_data) { | 38 bool BluetoothServiceRecord::Init(const std::string& xml_data) { |
| 32 name_.clear(); | 39 name_.clear(); |
| 40 supports_rfcomm_ = false; | |
|
keybuk
2012/04/19 01:05:40
Isn't this in the constructor now
bryeung
2012/04/19 19:42:43
Yep :-)
| |
| 33 | 41 |
| 34 XmlReader reader; | 42 XmlReader reader; |
| 35 if (!reader.Load(xml_data)) | 43 if (!reader.Load(xml_data)) |
| 36 return false; | 44 return false; |
| 37 | 45 |
| 38 while (advanceToTag(&reader, kAttributeNode)) { | 46 while (advanceToTag(&reader, kAttributeNode)) { |
| 39 std::string id; | 47 std::string id; |
| 40 if (reader.NodeAttribute(kIdAttribute, &id)) { | 48 if (reader.NodeAttribute(kIdAttribute, &id)) { |
| 41 if (id == kSdpNameId) { | 49 if (id == kSdpNameId) { |
| 42 if (advanceToTag(&reader, kTextNode)) { | 50 if (advanceToTag(&reader, kTextNode)) |
| 43 std::string value; | 51 reader.NodeAttribute(kValueAttribute, &name_); |
| 44 if (reader.NodeAttribute(kValueAttribute, &value)) | 52 } else if (id == kProtocolDescriptorListId) { |
| 45 name_ = value; | 53 if (advanceToTag(&reader, kSequenceNode)) { |
| 54 std::string sequence_content; | |
| 55 if (reader.ReadElementContent(&sequence_content)) { | |
| 56 ExtractChannels(sequence_content); | |
| 57 } | |
|
keybuk
2012/04/19 01:05:40
may as well add L2CAP + L2CAP PSM while you're in
bryeung
2012/04/19 19:42:43
future CL?
| |
| 46 } | 58 } |
| 47 } | 59 } |
| 48 } | 60 } |
| 49 // We don't care about anything else here, so find the closing tag | 61 // We don't care about anything else here, so find the closing tag |
| 50 advanceToTag(&reader, kAttributeNode); | 62 advanceToTag(&reader, kAttributeNode); |
| 51 } | 63 } |
| 52 | 64 |
| 53 return true; | 65 return true; |
| 54 } | 66 } |
| 55 | 67 |
| 68 void BluetoothServiceRecord::ExtractChannels(const std::string& xml_data) { | |
| 69 XmlReader reader; | |
| 70 if (!reader.Load(xml_data)) | |
| 71 return; | |
| 72 | |
| 73 do { | |
| 74 if (reader.NodeName() == kSequenceNode) { | |
| 75 if (advanceToTag(&reader, kUuidNode)) { | |
| 76 std::string type; | |
| 77 if (reader.NodeAttribute(kValueAttribute, &type) && | |
| 78 type == kRfcommUuid) { | |
| 79 if (advanceToTag(&reader, kUint8Node)) { | |
| 80 std::string channel_string; | |
| 81 if (reader.NodeAttribute(kValueAttribute, &channel_string)) { | |
| 82 unsigned channel; | |
| 83 if (base::StringToUint(channel_string, &channel)) { | |
| 84 if ((channel & 0xFF) == channel) { | |
| 85 rfcomm_channel_ = static_cast<uint8_t>(channel); | |
| 86 supports_rfcomm_ = true; | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 } while (advanceToTag(&reader, kSequenceNode)); | |
| 95 } | |
| 96 | |
| 56 } // namespace chromeos | 97 } // namespace chromeos |
| OLD | NEW |