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