OSDN Git Service

Fetch the mac address in GetInterfaceNameAndIndex
authorRoshan Pius <rpius@google.com>
Tue, 23 Aug 2016 18:26:51 +0000 (11:26 -0700)
committerRoshan Pius <rpius@google.com>
Tue, 23 Aug 2016 20:15:08 +0000 (13:15 -0700)
Rename the |GetInterfaceNameAndIndex| function to
|GetInterfaceInfo| and fetch the mac address assigned to the interface
along with the other info being fetched.

BUG: 31038272
TEST: Modified existing tests
TEST: Manual tests on bullhead to see if the correct mac address is
fetched.

Change-Id: I89c11e2227ef9bdf90dfd681df09d8b8116cd53f

client_interface_impl.cpp
client_interface_impl.h
net/netlink_utils.cpp
net/netlink_utils.h
server.cpp
server.h
tests/mock_netlink_utils.h
tests/netlink_utils_unittest.cpp
tests/server_unittest.cpp

index 998daee..653a57e 100644 (file)
@@ -35,11 +35,14 @@ using std::vector;
 namespace android {
 namespace wificond {
 
-ClientInterfaceImpl::ClientInterfaceImpl(const std::string& interface_name,
-                                         uint32_t interface_index,
-                                         ScanUtils* scan_utils)
+ClientInterfaceImpl::ClientInterfaceImpl(
+    const std::string& interface_name,
+    uint32_t interface_index,
+    const std::vector<uint8_t>& interface_mac_addr,
+    ScanUtils* scan_utils)
     : interface_name_(interface_name),
       interface_index_(interface_index),
+      interface_mac_addr_(interface_mac_addr),
       scan_utils_(scan_utils),
       binder_(new ClientInterfaceBinder(this)) {
   scan_utils_->SubscribeScanResultNotification(
index 88448d7..a60f8c5 100644 (file)
@@ -40,6 +40,7 @@ class ClientInterfaceImpl {
  public:
   ClientInterfaceImpl(const std::string& interface_name,
                       uint32_t interface_index,
+                      const std::vector<uint8_t>& interface_mac_addr,
                       ScanUtils* scan_utils);
   ~ClientInterfaceImpl();
 
@@ -56,6 +57,7 @@ class ClientInterfaceImpl {
 
   const std::string interface_name_;
   const uint32_t interface_index_;
+  const std::vector<uint8_t> interface_mac_addr_;
   ScanUtils* const scan_utils_;
   const android::sp<ClientInterfaceBinder> binder_;
 
index aabd436..fc0935c 100644 (file)
@@ -82,9 +82,10 @@ bool NetlinkUtils::GetWiphyIndex(uint32_t* out_wiphy_index) {
   return true;
 }
 
-bool NetlinkUtils::GetInterfaceNameAndIndex(uint32_t wiphy_index,
-                                            string* interface_name,
-                                            uint32_t* interface_index) {
+bool NetlinkUtils::GetInterfaceInfo(uint32_t wiphy_index,
+                                    string* name,
+                                    uint32_t* index,
+                                    vector<uint8_t>* mac_addr) {
   NL80211Packet get_interface(
       netlink_manager_->GetFamilyId(),
       NL80211_CMD_GET_INTERFACE,
@@ -144,8 +145,16 @@ bool NetlinkUtils::GetInterfaceNameAndIndex(uint32_t wiphy_index,
       LOG(DEBUG) << "Failed to get interface index";
       continue;
     }
-    *interface_name = if_name;
-    *interface_index = if_index;
+
+    vector<uint8_t> if_mac_addr;
+    if (!packet->GetAttributeValue(NL80211_ATTR_MAC, &if_mac_addr)) {
+      LOG(DEBUG) << "Failed to get interface mac address";
+      continue;
+    }
+
+    *name = if_name;
+    *index = if_index;
+    *mac_addr = if_mac_addr;
     return true;
   }
 
index 450c409..991e762 100644 (file)
@@ -37,12 +37,13 @@ class NetlinkUtils {
   // Returns true on success.
   virtual bool GetWiphyIndex(uint32_t* out_wiphy_index);
 
-  // Get wifi interface name from kernel.
+  // Get wifi interface info from kernel.
   // |wiphy_index| is the wiphy index we get using GetWiphyIndex().
   // Returns true on success.
-  virtual bool GetInterfaceNameAndIndex(uint32_t wiphy_index,
-                                        std::string* interface_name,
-                                        uint32_t* interface_index);
+  virtual bool GetInterfaceInfo(uint32_t wiphy_index,
+                                std::string* name,
+                                uint32_t* index,
+                                std::vector<uint8_t>* mac_addr);
 
  private:
   NetlinkManager* netlink_manager_;
index 48f63ef..2630112 100644 (file)
@@ -52,9 +52,11 @@ Server::Server(unique_ptr<HalTool> hal_tool,
 Status Server::createApInterface(sp<IApInterface>* created_interface) {
   string interface_name;
   uint32_t interface_index;
+  vector<uint8_t> interface_mac_addr;
   if (!SetupInterfaceForMode(DriverTool::kFirmwareModeAp,
                              &interface_name,
-                             &interface_index)) {
+                             &interface_index,
+                             &interface_mac_addr)) {
     return Status::ok();  // Logging was done internally
   }
 
@@ -70,15 +72,18 @@ Status Server::createApInterface(sp<IApInterface>* created_interface) {
 Status Server::createClientInterface(sp<IClientInterface>* created_interface) {
   string interface_name;
   uint32_t interface_index;
+  vector<uint8_t> interface_mac_addr;
   if (!SetupInterfaceForMode(DriverTool::kFirmwareModeSta,
                              &interface_name,
-                             &interface_index)) {
+                             &interface_index,
+                             &interface_mac_addr)) {
     return Status::ok();  // Logging was done internally
   }
 
   unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
       interface_name,
       interface_index,
+      interface_mac_addr,
       scan_utils_));
   *created_interface = client_interface->GetBinder();
   client_interfaces_.push_back(std::move(client_interface));
@@ -96,7 +101,8 @@ Status Server::tearDownInterfaces() {
 
 bool Server::SetupInterfaceForMode(int mode,
                                    string* interface_name,
-                                   uint32_t* interface_index) {
+                                   uint32_t* interface_index,
+                                   vector<uint8_t>* interface_mac_addr) {
   if (!ap_interfaces_.empty() || !client_interfaces_.empty()) {
     // In the future we may support multiple interfaces at once.  However,
     // today, we support just one.
@@ -118,9 +124,10 @@ bool Server::SetupInterfaceForMode(int mode,
     return false;
   }
 
-  if (!netlink_utils_->GetInterfaceNameAndIndex(wiphy_index_,
-                                                interface_name,
-                                                interface_index)) {
+  if (!netlink_utils_->GetInterfaceInfo(wiphy_index_,
+                                        interface_name,
+                                        interface_index,
+                                        interface_mac_addr)) {
     return false;
   }
 
index 5ceac41..0ab7e8a 100644 (file)
--- a/server.h
+++ b/server.h
@@ -69,7 +69,8 @@ class Server : public android::net::wifi::BnWificond {
   // Returns true on success, false otherwise.
   bool SetupInterfaceForMode(int mode,
                              std::string* interface_name,
-                             uint32_t* interface_index);
+                             uint32_t* interface_index,
+                             std::vector<uint8_t>* interface_mac_addr);
   bool RefreshWiphyIndex();
 
   const std::unique_ptr<wifi_system::HalTool> hal_tool_;
index 21d543e..806da7f 100644 (file)
@@ -30,9 +30,11 @@ class MockNetlinkUtils : public NetlinkUtils {
   ~MockNetlinkUtils() override = default;
 
   MOCK_METHOD1(GetWiphyIndex, bool(uint32_t* out_wiphy_index));
-  MOCK_METHOD3(GetInterfaceNameAndIndex, bool(uint32_t wiphy_index,
-                                              std::string* interface_name,
-                                              uint32_t* interface_index));
+  MOCK_METHOD4(GetInterfaceInfo,
+               bool(uint32_t wiphy_index,
+                    std::string* name,
+                    uint32_t* index,
+                    std::vector<uint8_t>* mac_address));
 
 };  // class MockNetlinkUtils
 
index 44eb360..029b989 100644 (file)
@@ -45,6 +45,7 @@ constexpr uint16_t kFakeWiphyIndex = 8;
 constexpr int kFakeErrorCode = EIO;
 const char kFakeInterfaceName[] = "testif0";
 const uint32_t kFakeInterfaceIndex = 34;
+const uint8_t kFakeInterfaceMacAddress[] = {0x45, 0x54, 0xad, 0x67, 0x98, 0xf6};
 
 // Currently, control messages are only created by the kernel and sent to us.
 // Therefore NL80211Packet doesn't have corresponding constructor.
@@ -122,7 +123,7 @@ TEST_F(NetlinkUtilsTest, CanHandleGetWiphyIndexError) {
   EXPECT_FALSE(netlink_utils_->GetWiphyIndex(&wiphy_index));
 }
 
-TEST_F(NetlinkUtilsTest, CanGetInterfaceNameAndIndex) {
+TEST_F(NetlinkUtilsTest, CanGetInterfaceInfo) {
   NL80211Packet new_interface(
       netlink_manager_->GetFamilyId(),
       NL80211_CMD_NEW_INTERFACE,
@@ -134,6 +135,13 @@ TEST_F(NetlinkUtilsTest, CanGetInterfaceNameAndIndex) {
   // Insert interface index attribute.
   NL80211Attr<uint32_t> if_index_attr(NL80211_ATTR_IFINDEX, kFakeInterfaceIndex);
   new_interface.AddAttribute(if_index_attr);
+  // Insert mac address attribute.
+  std::vector<uint8_t> if_mac_addr;
+  if_mac_addr.assign(
+      kFakeInterfaceMacAddress,
+      kFakeInterfaceMacAddress + sizeof(kFakeInterfaceMacAddress));
+  NL80211Attr<vector<uint8_t>> if_mac_attr(NL80211_ATTR_MAC, if_mac_addr);
+  new_interface.AddAttribute(if_mac_attr);
 
   // Mock a valid response from kernel.
   vector<NL80211Packet> response = {new_interface};
@@ -143,14 +151,17 @@ TEST_F(NetlinkUtilsTest, CanGetInterfaceNameAndIndex) {
 
   string interface_name;
   uint32_t interface_index;
-  EXPECT_TRUE(netlink_utils_->GetInterfaceNameAndIndex(kFakeWiphyIndex,
-                                                       &interface_name,
-                                                       &interface_index));
+  vector<uint8_t> interface_mac_addr;
+  EXPECT_TRUE(netlink_utils_->GetInterfaceInfo(kFakeWiphyIndex,
+                                               &interface_name,
+                                               &interface_index,
+                                               &interface_mac_addr));
   EXPECT_EQ(string(kFakeInterfaceName), interface_name);
   EXPECT_EQ(kFakeInterfaceIndex, interface_index);
+  EXPECT_EQ(if_mac_addr, interface_mac_addr);
 }
 
-TEST_F(NetlinkUtilsTest, HandlesPseudoDevicesInInterfaceNameAndIndexQuery) {
+TEST_F(NetlinkUtilsTest, HandlesPseudoDevicesInInterfaceInfoQuery) {
   // Some kernels will have extra responses ahead of the expected packet.
   NL80211Packet psuedo_interface(
       netlink_manager_->GetFamilyId(),
@@ -170,6 +181,12 @@ TEST_F(NetlinkUtilsTest, HandlesPseudoDevicesInInterfaceNameAndIndexQuery) {
       NL80211_ATTR_IFNAME, string(kFakeInterfaceName)));
   expected_interface.AddAttribute(NL80211Attr<uint32_t>(
       NL80211_ATTR_IFINDEX, kFakeInterfaceIndex));
+  std::vector<uint8_t> if_mac_addr;
+  if_mac_addr.assign(
+      kFakeInterfaceMacAddress,
+      kFakeInterfaceMacAddress + sizeof(kFakeInterfaceMacAddress));
+  expected_interface.AddAttribute(
+      NL80211Attr<vector<uint8_t>>(NL80211_ATTR_MAC, if_mac_addr));
 
   // Kernel can send us the pseduo interface packet first
   vector<NL80211Packet> response = {psuedo_interface, expected_interface};
@@ -179,14 +196,15 @@ TEST_F(NetlinkUtilsTest, HandlesPseudoDevicesInInterfaceNameAndIndexQuery) {
 
   string interface_name;
   uint32_t interface_index;
-  EXPECT_TRUE(netlink_utils_->GetInterfaceNameAndIndex(kFakeWiphyIndex,
-                                                       &interface_name,
-                                                       &interface_index));
+  vector<uint8_t> interface_mac_addr;
+  EXPECT_TRUE(netlink_utils_->GetInterfaceInfo(
+      kFakeWiphyIndex, &interface_name, &interface_index, &interface_mac_addr));
   EXPECT_EQ(string(kFakeInterfaceName), interface_name);
   EXPECT_EQ(kFakeInterfaceIndex, interface_index);
+  EXPECT_EQ(if_mac_addr, interface_mac_addr);
 }
 
-TEST_F(NetlinkUtilsTest, HandleP2p0WhenGetInterfaceNameAndIndex) {
+TEST_F(NetlinkUtilsTest, HandleP2p0WhenGetInterfaceInfo) {
   NL80211Packet new_interface(
       netlink_manager_->GetFamilyId(),
       NL80211_CMD_NEW_INTERFACE,
@@ -198,6 +216,13 @@ TEST_F(NetlinkUtilsTest, HandleP2p0WhenGetInterfaceNameAndIndex) {
   // Insert interface index attribute.
   NL80211Attr<uint32_t> if_index_attr(NL80211_ATTR_IFINDEX, kFakeInterfaceIndex);
   new_interface.AddAttribute(if_index_attr);
+  // Insert mac address attribute.
+  std::vector<uint8_t> if_mac_addr;
+  if_mac_addr.assign(
+      kFakeInterfaceMacAddress,
+      kFakeInterfaceMacAddress + sizeof(kFakeInterfaceMacAddress));
+  NL80211Attr<vector<uint8_t>> if_mac_attr(NL80211_ATTR_MAC, if_mac_addr);
+  new_interface.AddAttribute(if_mac_attr);
 
   // Create a new interface packet for p2p0.
   NL80211Packet new_interface_p2p0(
@@ -215,14 +240,17 @@ TEST_F(NetlinkUtilsTest, HandleP2p0WhenGetInterfaceNameAndIndex) {
 
   string interface_name;
   uint32_t interface_index;
-  EXPECT_TRUE(netlink_utils_->GetInterfaceNameAndIndex(kFakeWiphyIndex,
-                                                       &interface_name,
-                                                       &interface_index));
+  vector<uint8_t> interface_mac_addr;
+  EXPECT_TRUE(netlink_utils_->GetInterfaceInfo(kFakeWiphyIndex,
+                                               &interface_name,
+                                               &interface_index,
+                                               &interface_mac_addr));
   EXPECT_EQ(string(kFakeInterfaceName), interface_name);
   EXPECT_EQ(kFakeInterfaceIndex, interface_index);
+  EXPECT_EQ(if_mac_addr, interface_mac_addr);
 }
 
-TEST_F(NetlinkUtilsTest, CanHandleGetInterfaceNameAndIndexError) {
+TEST_F(NetlinkUtilsTest, CanHandleGetInterfaceInfoError) {
   // Mock an error response from kernel.
   vector<NL80211Packet> response = {CreateControlMessageError(kFakeErrorCode)};
 
@@ -231,9 +259,11 @@ TEST_F(NetlinkUtilsTest, CanHandleGetInterfaceNameAndIndexError) {
 
   string interface_name;
   uint32_t interface_index;
-  EXPECT_FALSE(netlink_utils_->GetInterfaceNameAndIndex(kFakeWiphyIndex,
-                                                        &interface_name,
-                                                        &interface_index));
+  vector<uint8_t> interface_mac_addr;
+  EXPECT_FALSE(netlink_utils_->GetInterfaceInfo(kFakeWiphyIndex,
+                                                &interface_name,
+                                                &interface_index,
+                                                &interface_mac_addr));
 }
 
 }  // namespace wificond
index dcb2a6a..cc00ac9 100644 (file)
@@ -53,8 +53,8 @@ class ServerTest : public ::testing::Test {
     ON_CALL(*driver_tool_, ChangeFirmwareMode(_)).WillByDefault(Return(true));
     ON_CALL(*if_tool_, SetWifiUpState(_)).WillByDefault(Return(true));
     ON_CALL(*netlink_utils_, GetWiphyIndex(_)).WillByDefault(Return(true));
-    ON_CALL(*netlink_utils_, GetInterfaceNameAndIndex(_, _, _)).
-        WillByDefault(Return(true));
+    ON_CALL(*netlink_utils_, GetInterfaceInfo(_, _, _, _))
+        .WillByDefault(Return(true));
   }
 
   NiceMock<MockHalTool>* hal_tool_ = new NiceMock<MockHalTool>;
@@ -91,7 +91,7 @@ TEST_F(ServerTest, CanSetUpApInterface) {
   EXPECT_CALL(*netlink_utils_, GetWiphyIndex(_))
       .InSequence(sequence)
       .WillOnce(Return(true));
-  EXPECT_CALL(*netlink_utils_, GetInterfaceNameAndIndex(_, _, _))
+  EXPECT_CALL(*netlink_utils_, GetInterfaceInfo(_, _, _, _))
       .InSequence(sequence)
       .WillOnce(Return(true));
 
@@ -102,7 +102,7 @@ TEST_F(ServerTest, CanSetUpApInterface) {
 TEST_F(ServerTest, DoesNotSupportMultipleInterfaces) {
   sp<IApInterface> ap_if;
   EXPECT_CALL(*netlink_utils_, GetWiphyIndex(_)).Times(1);
-  EXPECT_CALL(*netlink_utils_, GetInterfaceNameAndIndex(_, _, _)).Times(1);
+  EXPECT_CALL(*netlink_utils_, GetInterfaceInfo(_, _, _, _)).Times(1);
 
   EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());
   EXPECT_NE(nullptr, ap_if.get());
@@ -117,7 +117,7 @@ TEST_F(ServerTest, DoesNotSupportMultipleInterfaces) {
 TEST_F(ServerTest, CanDestroyInterfaces) {
   sp<IApInterface> ap_if;
   EXPECT_CALL(*netlink_utils_, GetWiphyIndex(_)).Times(2);
-  EXPECT_CALL(*netlink_utils_, GetInterfaceNameAndIndex(_, _, _)).Times(2);
+  EXPECT_CALL(*netlink_utils_, GetInterfaceInfo(_, _, _, _)).Times(2);
   EXPECT_CALL(*driver_tool_, UnloadDriver()).Times(0);
 
   EXPECT_TRUE(server_.createApInterface(&ap_if).isOk());