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