shill: Moves some netlink-attribute-specific code out of messages.

Just a little cleanup.

BUG=None
TEST=unittest

Change-Id: If26b5b226ef253b835d6b009c5cff87d7c529238
Reviewed-on: https://gerrit.chromium.org/gerrit/57798
Reviewed-by: Wade Guthrie <wdg@chromium.org>
Tested-by: Wade Guthrie <wdg@chromium.org>
Commit-Queue: Wade Guthrie <wdg@chromium.org>
diff --git a/attribute_list.cc b/attribute_list.cc
index a0843ea..8d35f44 100644
--- a/attribute_list.cc
+++ b/attribute_list.cc
@@ -308,6 +308,14 @@
   return true;
 }
 
+bool AttributeList::GetAttributeAsString(int id, std::string *value) const {
+  NetlinkAttribute *attribute = GetAttribute(id);
+  if (!attribute)
+    return false;
+
+  return attribute->ToString(value);
+}
+
 NetlinkAttribute *AttributeList::GetAttribute(int id) const {
   map<int, AttributePointer>::const_iterator i;
   i = attributes_.find(id);
diff --git a/attribute_list.h b/attribute_list.h
index 080d5e0..c8186ce 100644
--- a/attribute_list.h
+++ b/attribute_list.h
@@ -96,6 +96,9 @@
   bool SetRawAttributeValue(int id, ByteString value);
   bool GetRawAttributeValue(int id, ByteString *output) const;
 
+  // This retrieves a string from any kind of attribute.
+  bool GetAttributeAsString(int id, std::string *value) const;
+
  protected:
   friend class base::RefCounted<AttributeList>;
   virtual ~AttributeList() {}
diff --git a/netlink_message_unittest.cc b/netlink_message_unittest.cc
index 93b9a04..9432e90 100644
--- a/netlink_message_unittest.cc
+++ b/netlink_message_unittest.cc
@@ -504,7 +504,8 @@
 
   {
     string value;
-    EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
+    EXPECT_TRUE(message->const_attributes()->GetAttributeAsString(
+        NL80211_ATTR_MAC, &value));
     EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
   }
 
@@ -623,7 +624,8 @@
 
   {
     string value;
-    EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
+    EXPECT_TRUE(message->const_attributes()->GetAttributeAsString(
+        NL80211_ATTR_MAC, &value));
     EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
   }
 
@@ -789,7 +791,8 @@
 
   {
     string value;
-    EXPECT_TRUE(message->GetMacAttributeString(NL80211_ATTR_MAC, &value));
+    EXPECT_TRUE(message->const_attributes()->GetAttributeAsString(
+        NL80211_ATTR_MAC, &value));
     EXPECT_EQ(0, strncmp(value.c_str(), kExpectedMacAddress, value.length()));
   }
 
diff --git a/nl80211_attribute.cc b/nl80211_attribute.cc
index 089df3c..2041181 100644
--- a/nl80211_attribute.cc
+++ b/nl80211_attribute.cc
@@ -15,6 +15,7 @@
 #include "shill/logging.h"
 
 using base::Bind;
+using base::StringAppendF;
 using base::StringPrintf;
 using std::string;
 
@@ -278,6 +279,30 @@
 const int Nl80211AttributeMac::kName = NL80211_ATTR_MAC;
 const char Nl80211AttributeMac::kNameString[] = "NL80211_ATTR_MAC";
 
+bool Nl80211AttributeMac::ToString(std::string *value) const {
+  if (!value) {
+    LOG(ERROR) << "Null |value| parameter";
+    return false;
+  }
+  *value = StringFromMacAddress(data_.GetConstData());
+  return true;
+}
+
+// static
+string Nl80211AttributeMac::StringFromMacAddress(const uint8_t *arg) {
+  string output;
+
+  if (!arg) {
+    static const char kBogusMacAddress[] = "XX:XX:XX:XX:XX:XX";
+    output = kBogusMacAddress;
+    LOG(ERROR) << "|arg| parameter is NULL.";
+  } else {
+    output = StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x",
+                          arg[0], arg[1], arg[2], arg[3], arg[4], arg[5]);
+  }
+  return output;
+}
+
 const int Nl80211AttributeMaxMatchSets::kName = NL80211_ATTR_MAX_MATCH_SETS;
 const char Nl80211AttributeMaxMatchSets::kNameString[] =
     "NL80211_ATTR_MAX_MATCH_SETS";
diff --git a/nl80211_attribute.h b/nl80211_attribute.h
index e74fef4..fa5148d 100644
--- a/nl80211_attribute.h
+++ b/nl80211_attribute.h
@@ -617,6 +617,11 @@
   static const int kName;
   static const char kNameString[];
   Nl80211AttributeMac() : NetlinkRawAttribute(kName, kNameString) {}
+  virtual bool ToString(std::string *value) const;
+
+  // Stringizes the MAC address found in 'arg'.  If there are problems (such
+  // as a NULL |arg|), |value| is set to a bogus MAC address.
+  static std::string StringFromMacAddress(const uint8_t *arg);
 
  private:
   DISALLOW_COPY_AND_ASSIGN(Nl80211AttributeMac);
diff --git a/nl80211_message.cc b/nl80211_message.cc
index ee43e11..97d5419 100644
--- a/nl80211_message.cc
+++ b/nl80211_message.cc
@@ -39,6 +39,7 @@
 #include "shill/ieee80211.h"
 #include "shill/logging.h"
 #include "shill/netlink_attribute.h"
+#include "shill/nl80211_attribute.h"  // For Nl80211AttributeMac
 #include "shill/refptr_types.h"
 
 using base::Bind;
@@ -58,8 +59,6 @@
 const uint8_t Nl80211Frame::kMinimumFrameByteCount = 26;
 const uint8_t Nl80211Frame::kFrameTypeMask = 0xfc;
 
-const char Nl80211Message::kBogusMacAddress[] = "XX:XX:XX:XX:XX:XX";
-const unsigned int Nl80211Message::kEthernetAddressBytes = 6;
 const char Nl80211Message::kMessageTypeString[] = "nl80211";
 map<uint16_t, string> *Nl80211Message::reason_code_string_ = NULL;
 map<uint16_t, string> *Nl80211Message::status_code_string_ = NULL;
@@ -304,23 +303,6 @@
   return true;
 }
 
-// Helper function to provide a string for a MAC address.
-bool Nl80211Message::GetMacAttributeString(int id, string *value) const {
-  if (!value) {
-    LOG(ERROR) << "Null |value| parameter";
-    return false;
-  }
-
-  ByteString data;
-  if (!const_attributes()->GetRawAttributeValue(id, &data)) {
-    value->assign(kBogusMacAddress);
-    return false;
-  }
-  value->assign(StringFromMacAddress(data.GetConstData()));
-
-  return true;
-}
-
 // Helper function to provide a string for NL80211_ATTR_SCAN_FREQUENCIES.
 bool Nl80211Message::GetScanFrequenciesAttribute(
     int id, vector<uint32_t> *value) const {
@@ -374,23 +356,6 @@
 }
 
 // static
-string Nl80211Message::StringFromMacAddress(const uint8_t *arg) {
-  string output;
-
-  if (!arg) {
-    output = kBogusMacAddress;
-    LOG(ERROR) << "|arg| parameter is NULL.";
-  } else {
-    StringAppendF(&output, "%02x", arg[0]);
-
-    for (unsigned int i = 1; i < kEthernetAddressBytes ; ++i) {
-      StringAppendF(&output, ":%02x", arg[i]);
-    }
-  }
-  return output;
-}
-
-// static
 string Nl80211Message::StringFromReason(uint16_t status) {
   map<uint16_t, string>::const_iterator match;
   match = reason_code_string_->find(status);
@@ -436,8 +401,8 @@
   // Now, let's populate the other stuff.
   if (frame_.GetLength() >= kMinimumFrameByteCount) {
     mac_from_ =
-        Nl80211Message::StringFromMacAddress(&frame->destination_mac[0]);
-    mac_to_ = Nl80211Message::StringFromMacAddress(&frame->source_mac[0]);
+        Nl80211AttributeMac::StringFromMacAddress(&frame->destination_mac[0]);
+    mac_to_ = Nl80211AttributeMac::StringFromMacAddress(&frame->source_mac[0]);
     frame_type_ = frame->frame_control & kFrameTypeMask;
 
     switch (frame_type_) {
diff --git a/nl80211_message.h b/nl80211_message.h
index 1eb080f..31e9723 100644
--- a/nl80211_message.h
+++ b/nl80211_message.h
@@ -23,8 +23,6 @@
 class Nl80211Message : public GenericNetlinkMessage {
  public:
   static const char kMessageTypeString[];
-  static const unsigned int kEthernetAddressBytes;
-  static const char kBogusMacAddress[];
 
   Nl80211Message(uint8 command, const char *command_string)
       : GenericNetlinkMessage(nl80211_message_type_, command, command_string) {}
@@ -44,12 +42,6 @@
   uint32_t sequence_number() const { return sequence_number_; }
   void set_sequence_number(uint32_t seq) { sequence_number_ = seq; }
 
-  // TODO(wdg): This needs to be moved to AttributeMac.
-  // Helper function to provide a string for a MAC address.  If no attribute
-  // is found, this method returns 'false'.  On any error with a non-NULL
-  // |value|, this method sets |value| to a bogus MAC address.
-  bool GetMacAttributeString(int id, std::string *value) const;
-
   // TODO(wdg): This needs to be moved to AttributeScanFrequencies.
   // Helper function to provide a vector of scan frequencies for attributes
   // that contain them (such as NL80211_ATTR_SCAN_FREQUENCIES).
@@ -60,11 +52,6 @@
   // them (such as NL80211_ATTR_SCAN_SSIDS).
   bool GetScanSsidsAttribute(int id, std::vector<std::string> *value) const;
 
-  // TODO(wdg): This needs to be moved to AttributeMac.
-  // Stringizes the MAC address found in 'arg'.  If there are problems (such
-  // as a NULL |arg|), |value| is set to a bogus MAC address.
-  static std::string StringFromMacAddress(const uint8_t *arg);
-
   // Returns a string representing the passed-in |status| or |reason|, the
   // value of which has been acquired from libnl (for example, from the
   // NL80211_ATTR_STATUS_CODE or NL80211_ATTR_REASON_CODE attribute).