blob: 274a0cd1371ec1aecb8e2af4c57d4b48486940b4 [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 syncdc085c82012-12-28 08:54:41 -080040 // Returns the attributes as the payload portion of a netlink message
41 // suitable for Sockets::Send. Return value is empty on failure (or if no
42 // attributes exist).
43 ByteString Encode() const;
44
repo sync12cca802012-12-19 17:34:22 -080045 bool GetU8AttributeValue(int id, uint8_t *value) const;
46 bool GetU16AttributeValue(int id, uint16_t *value) const;
47 bool GetU32AttributeValue(int id, uint32_t *value) const;
48 bool GetU64AttributeValue(int id, uint64_t *value) const;
49 bool GetFlagAttributeValue(int id, bool *value) const;
50 // |IsFlagAttributeTrue| returns true if the flag attribute |id| is true. It
51 // retruns false if the attribute does not exist, is not of type kTypeFlag,
52 // or is not true.
53 bool IsFlagAttributeTrue(int id) const;
54 bool GetStringAttributeValue(int id, std::string *value) const;
55 bool GetNestedAttributeValue(int id,
56 base::WeakPtr<AttributeList> *value) const;
repo sync90ee0fa2012-12-18 10:08:08 -080057
58 // A raw attribute is a place to store unrecognized attributes when they
59 // from the kernel. For this reason, only limited support is provided for
60 // them.
repo sync12cca802012-12-19 17:34:22 -080061 bool GetRawAttributeValue(int id, ByteString *output) const;
repo sync90ee0fa2012-12-18 10:08:08 -080062 // TODO(wdg): |GetRawAttribute| is a stopgap to support various
repo syncdc085c82012-12-28 08:54:41 -080063 // Nl80211Message::ToString methods and must, once those are re-written,
repo sync90ee0fa2012-12-18 10:08:08 -080064 // be destroyed.
repo sync12cca802012-12-19 17:34:22 -080065 const Nl80211RawAttribute *GetRawAttribute(int id) const;
repo sync90ee0fa2012-12-18 10:08:08 -080066
67 private:
repo sync12cca802012-12-19 17:34:22 -080068 // The Create*Attribute and Set*Attribute methods are specifically for use
69 // by nested attributes to add their sub-attributes. Classes derived from
70 // Nl80211NestedAttribute should be added, here.
71 friend class Nl80211AttributeCqm;
72 friend class Nl80211AttributeStaInfo;
repo sync90ee0fa2012-12-18 10:08:08 -080073
repo sync12cca802012-12-19 17:34:22 -080074 bool CreateU8Attribute(int id, const char *id_string);
75 bool SetU8AttributeValue(int id, uint8_t value) const;
76
77 bool CreateU16Attribute(int id, const char *id_string);
78 bool SetU16AttributeValue(int id, uint16_t value) const;
79
80 bool CreateU32Attribute(int id, const char *id_string);
81 bool SetU32AttributeValue(int id, uint32_t value) const;
82
83 bool CreateU64Attribute(int id, const char *id_string);
84 bool SetU64AttributeValue(int id, uint64_t value) const;
85
86 bool CreateFlagAttribute(int id, const char *id_string);
87 bool SetFlagAttributeValue(int id, bool value) const;
88
89 bool CreateStringAttribute(int id, const char *id_string);
90 bool SetStringAttributeValue(int id, std::string value) const;
91
92 bool CreateNestedAttribute(int id, const char *id_string);
93 // No |SetNestedAttributeValue| method as it would make no sense.
94
95 // Using this to get around issues with const and operator[].
96 Nl80211Attribute *GetAttribute(int id) const;
97
repo sync1538d442012-12-20 15:24:35 -080098 // TODO(wdg): This is only used to support |GetRawAttribute|. Delete this
99 // when that goes away.
100 bool HasRawAttribute(int id) const;
repo sync90ee0fa2012-12-18 10:08:08 -0800101
repo sync1538d442012-12-20 15:24:35 -0800102 std::map<int, AttributePointer> attributes_;
repo sync90ee0fa2012-12-18 10:08:08 -0800103};
104
105} // namespace shill
106
107#endif // SHILL_ATTRIBUTE_LIST_H_