blob: 080d5e0a3f8ab8e9884b2f5c140b00d6666ea003 [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 Guthrie480aada2013-04-25 15:16:34 -070031 AttributeList() {}
32
Wade Guthrie68da97c2013-02-26 13:09:35 -080033 // Instantiates an NetlinkAttribute of the appropriate type from |id|,
repo sync90ee0fa2012-12-18 10:08:08 -080034 // and adds it to |attributes_|.
Wade Guthrie68da97c2013-02-26 13:09:35 -080035 bool CreateAttribute(int id, NewFromIdMethod factory);
repo sync90ee0fa2012-12-18 10:08:08 -080036
Wade Guthrie68da97c2013-02-26 13:09:35 -080037 // Instantiates an NetlinkAttribute of the appropriate type from |id|,
repo sync90ee0fa2012-12-18 10:08:08 -080038 // initializes it from |data|, and adds it to |attributes_|.
39 // TODO(wdg): This is a stop-gap for use before message constructors add
40 // their attributes as message templates.
Wade Guthrie68da97c2013-02-26 13:09:35 -080041 bool CreateAndInitAttribute(int id, const nlattr *data,
42 AttributeList::NewFromIdMethod factory);
repo sync90ee0fa2012-12-18 10:08:08 -080043
Wade Guthrie8e278612013-02-26 10:32:34 -080044 // Prints the attribute list with each attribute using no less than 1 line.
45 // |indent| indicates the amout of leading spaces to be printed (useful for
46 // nested attributes).
47 void Print(int log_level, int indent) const;
repo sync1538d442012-12-20 15:24:35 -080048
repo syncdc085c82012-12-28 08:54:41 -080049 // Returns the attributes as the payload portion of a netlink message
50 // suitable for Sockets::Send. Return value is empty on failure (or if no
51 // attributes exist).
52 ByteString Encode() const;
53
Wade Guthrief54872f2013-04-11 15:11:50 -070054 // Create, get, and set attributes of the given types. Attributes are
55 // accessed via an integer |id|. |id_string| is a string used to describe
56 // the attribute in debug output.
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080057 bool CreateU8Attribute(int id, const char *id_string);
58 bool SetU8AttributeValue(int id, uint8_t value);
repo sync12cca802012-12-19 17:34:22 -080059 bool GetU8AttributeValue(int id, uint8_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080060
61 bool CreateU16Attribute(int id, const char *id_string);
62 bool SetU16AttributeValue(int id, uint16_t value);
repo sync12cca802012-12-19 17:34:22 -080063 bool GetU16AttributeValue(int id, uint16_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080064
65 bool CreateU32Attribute(int id, const char *id_string);
66 bool SetU32AttributeValue(int id, uint32_t value);
repo sync12cca802012-12-19 17:34:22 -080067 bool GetU32AttributeValue(int id, uint32_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080068
69 bool CreateU64Attribute(int id, const char *id_string);
70 bool SetU64AttributeValue(int id, uint64_t value);
repo sync12cca802012-12-19 17:34:22 -080071 bool GetU64AttributeValue(int id, uint64_t *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080072
73 bool CreateFlagAttribute(int id, const char *id_string);
74 bool SetFlagAttributeValue(int id, bool value);
repo sync12cca802012-12-19 17:34:22 -080075 bool GetFlagAttributeValue(int id, bool *value) const;
76 // |IsFlagAttributeTrue| returns true if the flag attribute |id| is true. It
77 // retruns false if the attribute does not exist, is not of type kTypeFlag,
78 // or is not true.
79 bool IsFlagAttributeTrue(int id) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080080
81 bool CreateStringAttribute(int id, const char *id_string);
Wade Guthrief54872f2013-04-11 15:11:50 -070082 // SSID attributes are derived from string attributes.
83 bool CreateSsidAttribute(int id, const char *id_string);
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080084 bool SetStringAttributeValue(int id, std::string value);
repo sync12cca802012-12-19 17:34:22 -080085 bool GetStringAttributeValue(int id, std::string *value) const;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080086
87 bool CreateNestedAttribute(int id, const char *id_string);
88 bool SetNestedAttributeHasAValue(int id);
89 bool GetNestedAttributeList(int id, AttributeListRefPtr *value);
90 bool ConstGetNestedAttributeList(int id,
91 AttributeListConstRefPtr *value) const;
repo sync90ee0fa2012-12-18 10:08:08 -080092
Wade Guthrie71cb0a72013-02-27 10:27:18 -080093 bool CreateRawAttribute(int id, const char *id_string);
94 // |value| should point to the data (after the |nlattr| header, if there is
95 // one).
96 bool SetRawAttributeValue(int id, ByteString value);
repo sync12cca802012-12-19 17:34:22 -080097 bool GetRawAttributeValue(int id, ByteString *output) const;
Wade Guthrie71cb0a72013-02-27 10:27:18 -080098
99 protected:
100 friend class base::RefCounted<AttributeList>;
101 virtual ~AttributeList() {}
repo sync90ee0fa2012-12-18 10:08:08 -0800102
103 private:
Wade Guthrie480aada2013-04-25 15:16:34 -0700104 typedef std::map<int, AttributePointer> AttributeMap;
105 friend class AttributeIdIterator;
Wade Guthried3dfd6c2013-02-28 17:40:36 -0800106 friend class NetlinkNestedAttribute;
107
repo sync12cca802012-12-19 17:34:22 -0800108 // Using this to get around issues with const and operator[].
Wade Guthrie68da97c2013-02-26 13:09:35 -0800109 NetlinkAttribute *GetAttribute(int id) const;
repo sync12cca802012-12-19 17:34:22 -0800110
Wade Guthrie480aada2013-04-25 15:16:34 -0700111 AttributeMap attributes_;
112
113 DISALLOW_COPY_AND_ASSIGN(AttributeList);
114};
115
116// Provides a mechanism to iterate through the ids of all of the attributes
117// in an |AttributeList|. This class is really only useful if the caller
118// knows the type of each attribute in advance (such as with a nested array).
119class AttributeIdIterator {
120 public:
121 explicit AttributeIdIterator(const AttributeList &list)
122 : iter_(list.attributes_.begin()),
123 end_(list.attributes_.end()) {
124 }
125 void Advance() { ++iter_; }
126 bool AtEnd() const { return iter_ == end_; }
127 int GetId() const { return iter_->first; }
128
129 private:
130 AttributeList::AttributeMap::const_iterator iter_;
131 const AttributeList::AttributeMap::const_iterator end_;
132
133 DISALLOW_COPY_AND_ASSIGN(AttributeIdIterator);
repo sync90ee0fa2012-12-18 10:08:08 -0800134};
135
136} // namespace shill
137
138#endif // SHILL_ATTRIBUTE_LIST_H_