repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 1 | // 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 sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 13 | #include <tr1/memory> |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 14 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 15 | #include <base/bind.h> |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 16 | |
| 17 | #include "shill/refptr_types.h" |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 18 | |
| 19 | struct nlattr; |
| 20 | namespace shill { |
| 21 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 22 | class ByteString; |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 23 | class NetlinkAttribute; |
| 24 | class NetlinkRawAttribute; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 25 | |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 26 | class AttributeList : public base::RefCounted<AttributeList> { |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 27 | public: |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 28 | typedef std::tr1::shared_ptr<NetlinkAttribute> AttributePointer; |
| 29 | typedef base::Callback<NetlinkAttribute *(int id)> NewFromIdMethod; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 30 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 31 | // Instantiates an NetlinkAttribute of the appropriate type from |id|, |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 32 | // and adds it to |attributes_|. |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 33 | bool CreateAttribute(int id, NewFromIdMethod factory); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 34 | |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 35 | // Instantiates an NetlinkAttribute of the appropriate type from |id|, |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 36 | // 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 Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 39 | bool CreateAndInitAttribute(int id, const nlattr *data, |
| 40 | AttributeList::NewFromIdMethod factory); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 41 | |
Wade Guthrie | 8e27861 | 2013-02-26 10:32:34 -0800 | [diff] [blame] | 42 | // 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 sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 46 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 47 | // 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 Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 52 | bool CreateU8Attribute(int id, const char *id_string); |
| 53 | bool SetU8AttributeValue(int id, uint8_t value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 54 | bool GetU8AttributeValue(int id, uint8_t *value) const; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 55 | |
| 56 | bool CreateU16Attribute(int id, const char *id_string); |
| 57 | bool SetU16AttributeValue(int id, uint16_t value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 58 | bool GetU16AttributeValue(int id, uint16_t *value) const; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 59 | |
| 60 | bool CreateU32Attribute(int id, const char *id_string); |
| 61 | bool SetU32AttributeValue(int id, uint32_t value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 62 | bool GetU32AttributeValue(int id, uint32_t *value) const; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 63 | |
| 64 | bool CreateU64Attribute(int id, const char *id_string); |
| 65 | bool SetU64AttributeValue(int id, uint64_t value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 66 | bool GetU64AttributeValue(int id, uint64_t *value) const; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 67 | |
| 68 | bool CreateFlagAttribute(int id, const char *id_string); |
| 69 | bool SetFlagAttributeValue(int id, bool value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 70 | 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 Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 75 | |
| 76 | bool CreateStringAttribute(int id, const char *id_string); |
| 77 | bool SetStringAttributeValue(int id, std::string value); |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 78 | bool GetStringAttributeValue(int id, std::string *value) const; |
Wade Guthrie | efe1f0c | 2013-02-26 17:42:01 -0800 | [diff] [blame^] | 79 | |
| 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 sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 85 | |
| 86 | // A raw attribute is a place to store unrecognized attributes when they |
| 87 | // from the kernel. For this reason, only limited support is provided for |
| 88 | // them. |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 89 | bool GetRawAttributeValue(int id, ByteString *output) const; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 90 | // TODO(wdg): |GetRawAttribute| is a stopgap to support various |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 91 | // Nl80211Message::ToString methods and must, once those are re-written, |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 92 | // be destroyed. |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 93 | const NetlinkRawAttribute *GetRawAttribute(int id) const; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 94 | |
| 95 | private: |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 96 | // Using this to get around issues with const and operator[]. |
Wade Guthrie | 68da97c | 2013-02-26 13:09:35 -0800 | [diff] [blame] | 97 | NetlinkAttribute *GetAttribute(int id) const; |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 98 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 99 | // TODO(wdg): This is only used to support |GetRawAttribute|. Delete this |
| 100 | // when that goes away. |
| 101 | bool HasRawAttribute(int id) const; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 102 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 103 | std::map<int, AttributePointer> attributes_; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace shill |
| 107 | |
| 108 | #endif // SHILL_ATTRIBUTE_LIST_H_ |