blob: e733d01cba1c3c0939316fb4b41f60b1067fb088 [file] [log] [blame]
repo sync90ee0fa2012-12-18 10:08:08 -08001// 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#ifndef SHILL_ATTRIBUTE_LIST_H_
6#define SHILL_ATTRIBUTE_LIST_H_
7
8#include <linux/nl80211.h>
9#include <netlink/netlink.h>
10
11#include <map>
12#include <string>
repo sync1538d442012-12-20 15:24:35 -080013#include <tr1/memory>
repo sync90ee0fa2012-12-18 10:08:08 -080014
repo sync12cca802012-12-19 17:34:22 -080015#include <base/memory/weak_ptr.h>
repo sync90ee0fa2012-12-18 10:08:08 -080016
17struct nlattr;
18namespace shill {
19
repo sync12cca802012-12-19 17:34:22 -080020class ByteString;
21class Nl80211Attribute;
repo sync90ee0fa2012-12-18 10:08:08 -080022class Nl80211RawAttribute;
23
repo sync12cca802012-12-19 17:34:22 -080024class AttributeList : public base::SupportsWeakPtr<AttributeList> {
repo sync90ee0fa2012-12-18 10:08:08 -080025 public:
repo sync1538d442012-12-20 15:24:35 -080026 typedef std::tr1::shared_ptr<Nl80211Attribute> AttributePointer;
repo sync90ee0fa2012-12-18 10:08:08 -080027
repo sync12cca802012-12-19 17:34:22 -080028 // Instantiates an Nl80211Attribute of the appropriate type from |id|,
repo sync90ee0fa2012-12-18 10:08:08 -080029 // and adds it to |attributes_|.
repo sync12cca802012-12-19 17:34:22 -080030 bool CreateAttribute(nl80211_attrs id);
repo sync90ee0fa2012-12-18 10:08:08 -080031
repo sync12cca802012-12-19 17:34:22 -080032 // Instantiates an Nl80211Attribute of the appropriate type from |id|,
repo sync90ee0fa2012-12-18 10:08:08 -080033 // initializes it from |data|, and adds it to |attributes_|.
34 // TODO(wdg): This is a stop-gap for use before message constructors add
35 // their attributes as message templates.
repo sync12cca802012-12-19 17:34:22 -080036 bool CreateAndInitFromNlAttr(nl80211_attrs id, const nlattr *data);
repo sync90ee0fa2012-12-18 10:08:08 -080037
repo sync1538d442012-12-20 15:24:35 -080038 std::string ToString() const;
39
repo sync12cca802012-12-19 17:34:22 -080040 bool GetU8AttributeValue(int id, uint8_t *value) const;
41 bool GetU16AttributeValue(int id, uint16_t *value) const;
42 bool GetU32AttributeValue(int id, uint32_t *value) const;
43 bool GetU64AttributeValue(int id, uint64_t *value) const;
44 bool GetFlagAttributeValue(int id, bool *value) const;
45 // |IsFlagAttributeTrue| returns true if the flag attribute |id| is true. It
46 // retruns false if the attribute does not exist, is not of type kTypeFlag,
47 // or is not true.
48 bool IsFlagAttributeTrue(int id) const;
49 bool GetStringAttributeValue(int id, std::string *value) const;
50 bool GetNestedAttributeValue(int id,
51 base::WeakPtr<AttributeList> *value) const;
repo sync90ee0fa2012-12-18 10:08:08 -080052
53 // A raw attribute is a place to store unrecognized attributes when they
54 // from the kernel. For this reason, only limited support is provided for
55 // them.
repo sync12cca802012-12-19 17:34:22 -080056 bool GetRawAttributeValue(int id, ByteString *output) const;
repo sync90ee0fa2012-12-18 10:08:08 -080057 // TODO(wdg): |GetRawAttribute| is a stopgap to support various
58 // UserBoundNlMessage::ToString methods and must, once those are re-written,
59 // be destroyed.
repo sync12cca802012-12-19 17:34:22 -080060 const Nl80211RawAttribute *GetRawAttribute(int id) const;
repo sync90ee0fa2012-12-18 10:08:08 -080061
62 private:
repo sync12cca802012-12-19 17:34:22 -080063 // The Create*Attribute and Set*Attribute methods are specifically for use
64 // by nested attributes to add their sub-attributes. Classes derived from
65 // Nl80211NestedAttribute should be added, here.
66 friend class Nl80211AttributeCqm;
67 friend class Nl80211AttributeStaInfo;
repo sync90ee0fa2012-12-18 10:08:08 -080068
repo sync12cca802012-12-19 17:34:22 -080069 bool CreateU8Attribute(int id, const char *id_string);
70 bool SetU8AttributeValue(int id, uint8_t value) const;
71
72 bool CreateU16Attribute(int id, const char *id_string);
73 bool SetU16AttributeValue(int id, uint16_t value) const;
74
75 bool CreateU32Attribute(int id, const char *id_string);
76 bool SetU32AttributeValue(int id, uint32_t value) const;
77
78 bool CreateU64Attribute(int id, const char *id_string);
79 bool SetU64AttributeValue(int id, uint64_t value) const;
80
81 bool CreateFlagAttribute(int id, const char *id_string);
82 bool SetFlagAttributeValue(int id, bool value) const;
83
84 bool CreateStringAttribute(int id, const char *id_string);
85 bool SetStringAttributeValue(int id, std::string value) const;
86
87 bool CreateNestedAttribute(int id, const char *id_string);
88 // No |SetNestedAttributeValue| method as it would make no sense.
89
90 // Using this to get around issues with const and operator[].
91 Nl80211Attribute *GetAttribute(int id) const;
92
repo sync1538d442012-12-20 15:24:35 -080093 // TODO(wdg): This is only used to support |GetRawAttribute|. Delete this
94 // when that goes away.
95 bool HasRawAttribute(int id) const;
repo sync90ee0fa2012-12-18 10:08:08 -080096
repo sync1538d442012-12-20 15:24:35 -080097 std::map<int, AttributePointer> attributes_;
repo sync90ee0fa2012-12-18 10:08:08 -080098};
99
100} // namespace shill
101
102#endif // SHILL_ATTRIBUTE_LIST_H_