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/manager_unittest.cc b/manager_unittest.cc
index 06e359e..a3c6b33 100644
--- a/manager_unittest.cc
+++ b/manager_unittest.cc
@@ -6,20 +6,70 @@
 
 #include <gtest/gtest.h>
 
+#include "apmanager/mock_device.h"
+
+using ::testing::_;
+using ::testing::Return;
+
 namespace apmanager {
 
 class ManagerTest : public testing::Test {
  public:
   ManagerTest() : manager_() {}
 
+  void RegisterDevice(scoped_refptr<Device> device) {
+    manager_.devices_.push_back(device);
+  }
+
  protected:
   Manager manager_;
 };
 
-TEST_F(ManagerTest, CreateService) {
+TEST_F(ManagerTest, GetAvailableDevice) {
+  // Register two devices.
+  scoped_refptr<MockDevice> device0 = new MockDevice();
+  scoped_refptr<MockDevice> device1 = new MockDevice();
+  RegisterDevice(device0);
+  RegisterDevice(device1);
+
+  // Both devices are idle by default, should return the first added device.
+  EXPECT_EQ(device0, manager_.GetAvailableDevice());
+
+  // Set first one to be in used, should return the non-used device.
+  device0->SetInUsed(true);
+  EXPECT_EQ(device1, manager_.GetAvailableDevice());
+
+  // Both devices are in used, should return a nullptr.
+  device1->SetInUsed(true);
+  EXPECT_EQ(nullptr, manager_.GetAvailableDevice());
 }
 
-TEST_F(ManagerTest, RemoveService) {
+TEST_F(ManagerTest, GetDeviceFromInterfaceName) {
+  // Register two devices
+  scoped_refptr<MockDevice> device0 = new MockDevice();
+  scoped_refptr<MockDevice> device1 = new MockDevice();
+  RegisterDevice(device0);
+  RegisterDevice(device1);
+
+  const char kTestInterface0[] = "test-interface0";
+  const char kTestInterface1[] = "test-interface1";
+
+  // interface0 belongs to device0.
+  EXPECT_CALL(*device0.get(), InterfaceExists(kTestInterface0))
+      .WillOnce(Return(true));
+  EXPECT_EQ(device0, manager_.GetDeviceFromInterfaceName(kTestInterface0));
+
+  // interface1 belongs to device1.
+  EXPECT_CALL(*device0.get(), InterfaceExists(_))
+      .WillRepeatedly(Return(false));
+  EXPECT_CALL(*device1.get(), InterfaceExists(kTestInterface1))
+      .WillOnce(Return(true));
+  EXPECT_EQ(device1, manager_.GetDeviceFromInterfaceName(kTestInterface1));
+
+  // "random" interface is not found.
+  EXPECT_CALL(*device1.get(), InterfaceExists(_))
+      .WillRepeatedly(Return(false));
+  EXPECT_EQ(nullptr, manager_.GetDeviceFromInterfaceName("random"));
 }
 
 }  // namespace apmanager