apmanager: support for WiFi devices and interfaces

Added DeviceInfo for enumerating WiFi devices (PHYs) and detecting
WiFi interfaces. Also Created a Device class for abstracting WiFi PHYs,
which will maintain the device's capabilities and manage interfaces
created on the device.

Integrate Device with Manager and Config to allow user to start
an AP service without providing interface information.

Next up, will focus on parsing HT/VHT capabilities for each band and
integrate it into ap configuration. Also adding shill support for
claim/release interface.

BUG=chromium:431763
TEST=unittest

Change-Id: I62f4251064483b57e264d1278fde68022a737aea
Reviewed-on: https://chromium-review.googlesource.com/231257
Reviewed-by: Peter Qiu <zqiu@chromium.org>
Commit-Queue: Peter Qiu <zqiu@chromium.org>
Tested-by: Peter Qiu <zqiu@chromium.org>
diff --git a/config.cc b/config.cc
index ef8620f..8f70c80 100644
--- a/config.cc
+++ b/config.cc
@@ -7,6 +7,9 @@
 #include <base/strings/stringprintf.h>
 #include <chromeos/dbus/service_constants.h>
 
+#include "apmanager/device.h"
+#include "apmanager/manager.h"
+
 using chromeos::dbus_utils::AsyncEventSequencer;
 using chromeos::dbus_utils::ExportedObjectManager;
 using chromeos::ErrorPtr;
@@ -53,8 +56,9 @@
 // RTS threshold: disabled.
 const int Config::kHostapdDefaultRtsThreshold = 2347;
 
-Config::Config(const string& service_path)
+Config::Config(Manager* manager, const string& service_path)
     : org::chromium::apmanager::ConfigAdaptor(this),
+      manager_(manager),
       dbus_path_(dbus::ObjectPath(
           base::StringPrintf("%s/config", service_path.c_str()))) {
   // Initialize default configuration values.
@@ -120,6 +124,22 @@
   return true;
 }
 
+bool Config::ClaimDevice() {
+  if (!device_) {
+    LOG(ERROR) << "Failed to claim device: device doesn't exist.";
+    return false;
+  }
+  return device_->ClaimDevice();
+}
+
+bool Config::ReleaseDevice() {
+  if (!device_) {
+    LOG(ERROR) << "Failed to release device: device doesn't exist.";
+    return false;
+  }
+  return device_->ReleaseDevice();
+}
+
 bool Config::AppendHwMode(ErrorPtr* error, std::string* config_str) {
   string hw_mode = GetHwMode();
   string hostapd_hw_mode;
@@ -187,10 +207,29 @@
                              std::string* config_str) {
   string interface = GetInterfaceName();
   if (interface.empty()) {
-    // TODO(zqiu): Ask manager for available ap mode interface.
-    return false;
+    // Ask manager for unused ap capable device.
+    device_ = manager_->GetAvailableDevice();
+    if (!device_) {
+      chromeos::Error::AddTo(
+          error, FROM_HERE, chromeos::errors::dbus::kDomain, kConfigError,
+          "No device available");
+      return false;
+    }
+  } else {
+    device_ = manager_->GetDeviceFromInterfaceName(interface);
+    if (device_->GetInUsed()) {
+      chromeos::Error::AddToPrintf(
+          error, FROM_HERE, chromeos::errors::dbus::kDomain, kConfigError,
+          "Device [%s] for interface [%s] already in use",
+          device_->GetDeviceName().c_str(),
+          interface.c_str());
+      return false;
+    }
   }
 
+  // Use the preferred AP interface from the device.
+  interface = device_->GetPreferredApInterface();
+
   base::StringAppendF(
       config_str, "%s=%s\n", kHostapdConfigKeyInterface, interface.c_str());
   return true;