repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/attribute_list.h" |
| 6 | |
| 7 | #include <ctype.h> |
| 8 | #include <linux/nl80211.h> |
| 9 | #include <netlink/attr.h> |
| 10 | #include <netlink/netlink.h> |
| 11 | |
| 12 | #include <iomanip> |
| 13 | #include <map> |
| 14 | #include <string> |
| 15 | |
| 16 | #include <base/stl_util.h> |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 17 | #include <base/stringprintf.h> |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 18 | |
| 19 | #include "shill/logging.h" |
Wade Guthrie | f162f8b | 2013-02-27 14:13:55 -0800 | [diff] [blame] | 20 | #include "shill/netlink_attribute.h" |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 21 | #include "shill/scope_logger.h" |
| 22 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 23 | using base::StringAppendF; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 24 | using base::WeakPtr; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 25 | using std::map; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 26 | using std::string; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 27 | |
| 28 | namespace shill { |
| 29 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 30 | bool AttributeList::CreateAttribute( |
| 31 | int id, AttributeList::NewFromIdMethod factory) { |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 32 | if (ContainsKey(attributes_, id)) { |
Wade Guthrie | f788a0a | 2013-06-10 09:50:26 -0700 | [diff] [blame] | 33 | SLOG(WiFi, 7) << "Trying to re-add attribute " << id << ", not overwriting"; |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 34 | return true; |
| 35 | } |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 36 | attributes_[id] = AttributePointer(factory.Run(id)); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 37 | return true; |
| 38 | } |
| 39 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 40 | bool AttributeList::CreateAndInitAttribute( |
| 41 | int id, const nlattr *data, AttributeList::NewFromIdMethod factory) { |
| 42 | if (!CreateAttribute(id, factory)) { |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 43 | return false; |
| 44 | } |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 45 | return attributes_[id]->InitFromNlAttr(data); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 48 | void AttributeList::Print(int log_level, int indent) const { |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 49 | map<int, AttributePointer>::const_iterator i; |
| 50 | |
| 51 | for (i = attributes_.begin(); i != attributes_.end(); ++i) { |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 52 | i->second->Print(log_level, indent); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 53 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 54 | } |
| 55 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 56 | ByteString AttributeList::Encode() const { |
| 57 | ByteString result; |
| 58 | map<int, AttributePointer>::const_iterator i; |
| 59 | |
| 60 | for (i = attributes_.begin(); i != attributes_.end(); ++i) { |
| 61 | result.Append(i->second->Encode()); |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 66 | // U8 Attribute. |
| 67 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 68 | bool AttributeList::GetU8AttributeValue(int id, uint8_t *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 69 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 70 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 71 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 72 | return attribute->GetU8Value(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 73 | } |
| 74 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 75 | bool AttributeList::CreateU8Attribute(int id, const char *id_string) { |
| 76 | if (ContainsKey(attributes_, id)) { |
| 77 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 78 | return false; |
| 79 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 80 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 81 | new NetlinkU8Attribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame] | 85 | bool AttributeList::SetU8AttributeValue(int id, uint8_t value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 86 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 87 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 88 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 89 | return attribute->SetU8Value(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 93 | // U16 Attribute. |
| 94 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 95 | bool AttributeList::GetU16AttributeValue(int id, uint16_t *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 96 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 97 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 98 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 99 | return attribute->GetU16Value(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 100 | } |
| 101 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 102 | bool AttributeList::CreateU16Attribute(int id, const char *id_string) { |
| 103 | if (ContainsKey(attributes_, id)) { |
| 104 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 105 | return false; |
| 106 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 107 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 108 | new NetlinkU16Attribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame] | 112 | bool AttributeList::SetU16AttributeValue(int id, uint16_t value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 113 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 114 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 115 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 116 | return attribute->SetU16Value(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 117 | } |
| 118 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 119 | // U32 Attribute. |
| 120 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 121 | bool AttributeList::GetU32AttributeValue(int id, uint32_t *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 122 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 123 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 124 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 125 | return attribute->GetU32Value(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 126 | } |
| 127 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 128 | bool AttributeList::CreateU32Attribute(int id, const char *id_string) { |
| 129 | if (ContainsKey(attributes_, id)) { |
| 130 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 131 | return false; |
| 132 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 133 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 134 | new NetlinkU32Attribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame] | 138 | bool AttributeList::SetU32AttributeValue(int id, uint32_t value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 139 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 140 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 141 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 142 | return attribute->SetU32Value(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 143 | } |
| 144 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 145 | // U64 Attribute. |
| 146 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 147 | bool AttributeList::GetU64AttributeValue(int id, uint64_t *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 148 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 149 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 150 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 151 | return attribute->GetU64Value(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 152 | } |
| 153 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 154 | bool AttributeList::CreateU64Attribute(int id, const char *id_string) { |
| 155 | if (ContainsKey(attributes_, id)) { |
| 156 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 157 | return false; |
| 158 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 159 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 160 | new NetlinkU64Attribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame] | 164 | bool AttributeList::SetU64AttributeValue(int id, uint64_t value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 165 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 166 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 167 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 168 | return attribute->SetU64Value(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 169 | } |
| 170 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 171 | // Flag Attribute. |
| 172 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 173 | bool AttributeList::GetFlagAttributeValue(int id, bool *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 174 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 175 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 176 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 177 | return attribute->GetFlagValue(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 178 | } |
| 179 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 180 | bool AttributeList::CreateFlagAttribute(int id, const char *id_string) { |
| 181 | if (ContainsKey(attributes_, id)) { |
| 182 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 183 | return false; |
| 184 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 185 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 186 | new NetlinkFlagAttribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 187 | return true; |
| 188 | } |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 189 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame] | 190 | bool AttributeList::SetFlagAttributeValue(int id, bool value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 191 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 192 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 193 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 194 | return attribute->SetFlagValue(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | bool AttributeList::IsFlagAttributeTrue(int id) const { |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 198 | bool flag; |
| 199 | if (!GetFlagAttributeValue(id, &flag)) { |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | return flag; |
| 203 | } |
| 204 | |
| 205 | // String Attribute. |
| 206 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 207 | bool AttributeList::GetStringAttributeValue(int id, string *value) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 208 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 209 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 210 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 211 | return attribute->GetStringValue(value); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 212 | } |
| 213 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 214 | bool AttributeList::CreateStringAttribute(int id, const char *id_string) { |
| 215 | if (ContainsKey(attributes_, id)) { |
| 216 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 217 | return false; |
| 218 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 219 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 220 | new NetlinkStringAttribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
Wade Guthrie | f54872f | 2013-04-11 15:11:50 -0700 | [diff] [blame] | 224 | bool AttributeList::CreateSsidAttribute(int id, const char *id_string) { |
| 225 | if (ContainsKey(attributes_, id)) { |
| 226 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 227 | return false; |
| 228 | } |
| 229 | attributes_[id] = AttributePointer( |
| 230 | new NetlinkSsidAttribute(id, id_string)); |
| 231 | return true; |
| 232 | } |
| 233 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame] | 234 | bool AttributeList::SetStringAttributeValue(int id, string value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 235 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 236 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 237 | return false; |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 238 | return attribute->SetStringValue(value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Nested Attribute. |
| 242 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame] | 243 | bool AttributeList::GetNestedAttributeList(int id, |
| 244 | AttributeListRefPtr *value) { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 245 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 246 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 247 | return false; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame] | 248 | return attribute->GetNestedAttributeList(value); |
| 249 | } |
| 250 | |
| 251 | bool AttributeList::ConstGetNestedAttributeList( |
| 252 | int id, AttributeListConstRefPtr *value) const { |
| 253 | NetlinkAttribute *attribute = GetAttribute(id); |
| 254 | if (!attribute) |
| 255 | return false; |
| 256 | return attribute->ConstGetNestedAttributeList(value); |
| 257 | } |
| 258 | |
| 259 | bool AttributeList::SetNestedAttributeHasAValue(int id) { |
| 260 | NetlinkAttribute *attribute = GetAttribute(id); |
| 261 | if (!attribute) |
| 262 | return false; |
| 263 | return attribute->SetNestedHasAValue(); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | bool AttributeList::CreateNestedAttribute(int id, const char *id_string) { |
| 267 | if (ContainsKey(attributes_, id)) { |
| 268 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 269 | return false; |
| 270 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 271 | attributes_[id] = AttributePointer( |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 272 | new NetlinkNestedAttribute(id, id_string)); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 276 | // Raw Attribute. |
| 277 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 278 | bool AttributeList::GetRawAttributeValue(int id, |
| 279 | ByteString *output) const { |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 280 | NetlinkAttribute *attribute = GetAttribute(id); |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 281 | if (!attribute) |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 282 | return false; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 283 | |
| 284 | ByteString raw_value; |
| 285 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 286 | if (!attribute->GetRawValue(&raw_value)) |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 287 | return false; |
| 288 | |
| 289 | if (output) { |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 290 | *output = raw_value; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 291 | } |
| 292 | return true; |
| 293 | } |
| 294 | |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 295 | bool AttributeList::SetRawAttributeValue(int id, ByteString value) { |
| 296 | NetlinkAttribute *attribute = GetAttribute(id); |
| 297 | if (!attribute) |
| 298 | return false; |
| 299 | return attribute->SetRawValue(value); |
| 300 | } |
| 301 | |
| 302 | bool AttributeList::CreateRawAttribute(int id, const char *id_string) { |
| 303 | if (ContainsKey(attributes_, id)) { |
| 304 | LOG(ERROR) << "Trying to re-add attribute: " << id; |
| 305 | return false; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 306 | } |
Wade Guthrie | 71cb0a7 | 2013-02-27 10:27:18 -0800 | [diff] [blame] | 307 | attributes_[id] = AttributePointer(new NetlinkRawAttribute(id, id_string)); |
| 308 | return true; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Wade Guthrie | 2193b23 | 2013-06-05 17:09:07 -0700 | [diff] [blame] | 311 | bool AttributeList::GetAttributeAsString(int id, std::string *value) const { |
| 312 | NetlinkAttribute *attribute = GetAttribute(id); |
| 313 | if (!attribute) |
| 314 | return false; |
| 315 | |
| 316 | return attribute->ToString(value); |
| 317 | } |
| 318 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 319 | NetlinkAttribute *AttributeList::GetAttribute(int id) const { |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 320 | map<int, AttributePointer>::const_iterator i; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 321 | i = attributes_.find(id); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 322 | if (i == attributes_.end()) { |
| 323 | return NULL; |
| 324 | } |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 325 | return i->second.get(); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 326 | } |
| 327 | |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 328 | } // namespace shill |