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