blob: 1e6503d2bb34ebaeadf60db1fd5ca75e320bfbc8 [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 Guthrieefe1f0c2013-02-26 17:42:01 -080052 bool CreateU8Attribute(int id, const char *id_string);
53 bool SetU8AttributeValue(int id, uint8_t value);
repo sync12cca802012-12-19 17:34:22 -080054 bool GetU8AttributeValue(int id, uint8_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080055
56 bool CreateU16Attribute(int id, const char *id_string);
57 bool SetU16AttributeValue(int id, uint16_t value);
repo sync12cca802012-12-19 17:34:22 -080058 bool GetU16AttributeValue(int id, uint16_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080059
60 bool CreateU32Attribute(int id, const char *id_string);
61 bool SetU32AttributeValue(int id, uint32_t value);
repo sync12cca802012-12-19 17:34:22 -080062 bool GetU32AttributeValue(int id, uint32_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080063
64 bool CreateU64Attribute(int id, const char *id_string);
65 bool SetU64AttributeValue(int id, uint64_t value);
repo sync12cca802012-12-19 17:34:22 -080066 bool GetU64AttributeValue(int id, uint64_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080067
68 bool CreateFlagAttribute(int id, const char *id_string);
69 bool SetFlagAttributeValue(int id, bool value);
repo sync12cca802012-12-19 17:34:22 -080070 bool GetFlagAttributeValue(int id, bool *value) const;
71 // |IsFlagAttributeTrue| returns true if the flag attribute |id| is true. It
72 // retruns false if the attribute does not exist, is not of type kTypeFlag,
73 // or is not true.
74 bool IsFlagAttributeTrue(int id) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080075
76 bool CreateStringAttribute(int id, const char *id_string);
77 bool SetStringAttributeValue(int id, std::string value);
repo sync12cca802012-12-19 17:34:22 -080078 bool GetStringAttributeValue(int id, std::string *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080079
80 bool CreateNestedAttribute(int id, const char *id_string);
81 bool SetNestedAttributeHasAValue(int id);
82 bool GetNestedAttributeList(int id, AttributeListRefPtr *value);
83 bool ConstGetNestedAttributeList(int id,
84 AttributeListConstRefPtr *value) const;
repo sync90ee0fa2012-12-18 10:08:08 -080085
Wade Guthrie71cb0a72013-02-27 10:27:18 -080086 bool CreateRawAttribute(int id, const char *id_string);
87 // |value| should point to the data (after the |nlattr| header, if there is
88 // one).
89 bool SetRawAttributeValue(int id, ByteString value);
repo sync12cca802012-12-19 17:34:22 -080090 bool GetRawAttributeValue(int id, ByteString *output) const;
Wade Guthrie71cb0a72013-02-27 10:27:18 -080091
92 protected:
93 friend class base::RefCounted<AttributeList>;
94 virtual ~AttributeList() {}
repo sync90ee0fa2012-12-18 10:08:08 -080095
96 private:
Wade Guthried3dfd6c2013-02-28 17:40:36 -080097 friend class NetlinkNestedAttribute;
98
repo sync12cca802012-12-19 17:34:22 -080099 // Using this to get around issues with const and operator[].
Wade Guthrie68da97c2013-02-26 13:09:35 -0800100 NetlinkAttribute *GetAttribute(int id) const;
repo sync12cca802012-12-19 17:34:22 -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_