blob: 58ad37b373988fc2cae0be4dec01f960ac25459f [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
Wade Guthrie68da97c2013-02-26 13:09:35 -080015#include <base/bind.h>
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080016
17#include "shill/refptr_types.h"
repo sync90ee0fa2012-12-18 10:08:08 -080018
19struct nlattr;
20namespace shill {
21
repo sync12cca802012-12-19 17:34:22 -080022class ByteString;
Wade Guthrie68da97c2013-02-26 13:09:35 -080023class NetlinkAttribute;
24class NetlinkRawAttribute;
repo sync90ee0fa2012-12-18 10:08:08 -080025
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080026class AttributeList : public base::RefCounted<AttributeList> {
repo sync90ee0fa2012-12-18 10:08:08 -080027 public:
Wade Guthrie68da97c2013-02-26 13:09:35 -080028 typedef std::tr1::shared_ptr<NetlinkAttribute> AttributePointer;
29 typedef base::Callback<NetlinkAttribute *(int id)> NewFromIdMethod;
repo sync90ee0fa2012-12-18 10:08:08 -080030
Wade Guthrie68da97c2013-02-26 13:09:35 -080031 // Instantiates an NetlinkAttribute of the appropriate type from |id|,
repo sync90ee0fa2012-12-18 10:08:08 -080032 // and adds it to |attributes_|.
Wade Guthrie68da97c2013-02-26 13:09:35 -080033 bool CreateAttribute(int id, NewFromIdMethod factory);
repo sync90ee0fa2012-12-18 10:08:08 -080034
Wade Guthrie68da97c2013-02-26 13:09:35 -080035 // Instantiates an NetlinkAttribute of the appropriate type from |id|,
repo sync90ee0fa2012-12-18 10:08:08 -080036 // initializes it from |data|, and adds it to |attributes_|.
37 // TODO(wdg): This is a stop-gap for use before message constructors add
38 // their attributes as message templates.
Wade Guthrie68da97c2013-02-26 13:09:35 -080039 bool CreateAndInitAttribute(int id, const nlattr *data,
40 AttributeList::NewFromIdMethod factory);
repo sync90ee0fa2012-12-18 10:08:08 -080041
Wade Guthrie8e278612013-02-26 10:32:34 -080042 // Prints the attribute list with each attribute using no less than 1 line.
43 // |indent| indicates the amout of leading spaces to be printed (useful for
44 // nested attributes).
45 void Print(int log_level, int indent) const;
repo sync1538d442012-12-20 15:24:35 -080046
repo syncdc085c82012-12-28 08:54:41 -080047 // Returns the attributes as the payload portion of a netlink message
48 // suitable for Sockets::Send. Return value is empty on failure (or if no
49 // attributes exist).
50 ByteString Encode() const;
51
Wade Guthrief54872f2013-04-11 15:11:50 -070052 // Create, get, and set attributes of the given types. Attributes are
53 // accessed via an integer |id|. |id_string| is a string used to describe
54 // the attribute in debug output.
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080055 bool CreateU8Attribute(int id, const char *id_string);
56 bool SetU8AttributeValue(int id, uint8_t value);
repo sync12cca802012-12-19 17:34:22 -080057 bool GetU8AttributeValue(int id, uint8_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080058
59 bool CreateU16Attribute(int id, const char *id_string);
60 bool SetU16AttributeValue(int id, uint16_t value);
repo sync12cca802012-12-19 17:34:22 -080061 bool GetU16AttributeValue(int id, uint16_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080062
63 bool CreateU32Attribute(int id, const char *id_string);
64 bool SetU32AttributeValue(int id, uint32_t value);
repo sync12cca802012-12-19 17:34:22 -080065 bool GetU32AttributeValue(int id, uint32_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080066
67 bool CreateU64Attribute(int id, const char *id_string);
68 bool SetU64AttributeValue(int id, uint64_t value);
repo sync12cca802012-12-19 17:34:22 -080069 bool GetU64AttributeValue(int id, uint64_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080070
71 bool CreateFlagAttribute(int id, const char *id_string);
72 bool SetFlagAttributeValue(int id, bool value);
repo sync12cca802012-12-19 17:34:22 -080073 bool GetFlagAttributeValue(int id, bool *value) const;
74 // |IsFlagAttributeTrue| returns true if the flag attribute |id| is true. It
75 // retruns false if the attribute does not exist, is not of type kTypeFlag,
76 // or is not true.
77 bool IsFlagAttributeTrue(int id) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080078
79 bool CreateStringAttribute(int id, const char *id_string);
Wade Guthrief54872f2013-04-11 15:11:50 -070080 // SSID attributes are derived from string attributes.
81 bool CreateSsidAttribute(int id, const char *id_string);
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080082 bool SetStringAttributeValue(int id, std::string value);
repo sync12cca802012-12-19 17:34:22 -080083 bool GetStringAttributeValue(int id, std::string *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080084
85 bool CreateNestedAttribute(int id, const char *id_string);
86 bool SetNestedAttributeHasAValue(int id);
87 bool GetNestedAttributeList(int id, AttributeListRefPtr *value);
88 bool ConstGetNestedAttributeList(int id,
89 AttributeListConstRefPtr *value) const;
repo sync90ee0fa2012-12-18 10:08:08 -080090
Wade Guthrie71cb0a72013-02-27 10:27:18 -080091 bool CreateRawAttribute(int id, const char *id_string);
92 // |value| should point to the data (after the |nlattr| header, if there is
93 // one).
94 bool SetRawAttributeValue(int id, ByteString value);
repo sync12cca802012-12-19 17:34:22 -080095 bool GetRawAttributeValue(int id, ByteString *output) const;
Wade Guthrie71cb0a72013-02-27 10:27:18 -080096
97 protected:
98 friend class base::RefCounted<AttributeList>;
99 virtual ~AttributeList() {}
repo sync90ee0fa2012-12-18 10:08:08 -0800100
101 private:
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800102 friend class NetlinkNestedAttribute;
103
repo sync12cca802012-12-19 17:34:22 -0800104 // Using this to get around issues with const and operator[].
Wade Guthrie68da97c2013-02-26 13:09:35 -0800105 NetlinkAttribute *GetAttribute(int id) const;
repo sync12cca802012-12-19 17:34:22 -0800106
repo sync1538d442012-12-20 15:24:35 -0800107 std::map<int, AttributePointer> attributes_;
repo sync90ee0fa2012-12-18 10:08:08 -0800108};
109
110} // namespace shill
111
112#endif // SHILL_ATTRIBUTE_LIST_H_