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 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 16 | |
| 17 | struct nlattr; |
| 18 | namespace shill { |
| 19 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 20 | class ByteString; |
| 21 | class Nl80211Attribute; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 22 | class Nl80211RawAttribute; |
| 23 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 24 | class AttributeList : public base::SupportsWeakPtr<AttributeList> { |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 25 | public: |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 26 | typedef std::tr1::shared_ptr<Nl80211Attribute> AttributePointer; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 27 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 28 | // Instantiates an Nl80211Attribute of the appropriate type from |id|, |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 29 | // and adds it to |attributes_|. |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 30 | bool CreateAttribute(nl80211_attrs id); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 31 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 32 | // Instantiates an Nl80211Attribute of the appropriate type from |id|, |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 33 | // 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 sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 36 | bool CreateAndInitFromNlAttr(nl80211_attrs id, const nlattr *data); |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 37 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 38 | std::string ToString() const; |
| 39 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 40 | // 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 sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 45 | 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 sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 57 | |
| 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 sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 61 | bool GetRawAttributeValue(int id, ByteString *output) const; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 62 | // TODO(wdg): |GetRawAttribute| is a stopgap to support various |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 63 | // Nl80211Message::ToString methods and must, once those are re-written, |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 64 | // be destroyed. |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 65 | const Nl80211RawAttribute *GetRawAttribute(int id) const; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 66 | |
| 67 | private: |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 68 | // 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 sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 73 | |
repo sync | 12cca80 | 2012-12-19 17:34:22 -0800 | [diff] [blame] | 74 | 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 sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 98 | // TODO(wdg): This is only used to support |GetRawAttribute|. Delete this |
| 99 | // when that goes away. |
| 100 | bool HasRawAttribute(int id) const; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 101 | |
repo sync | 1538d44 | 2012-12-20 15:24:35 -0800 | [diff] [blame] | 102 | std::map<int, AttributePointer> attributes_; |
repo sync | 90ee0fa | 2012-12-18 10:08:08 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace shill |
| 106 | |
| 107 | #endif // SHILL_ATTRIBUTE_LIST_H_ |