Use Radio ID to register for hwsim notifications

Change-Id: I9af6a71923d34ab3cc39fc3dae7c74ba4247e266
Merged-In: I9af6a71923d34ab3cc39fc3dae7c74ba4247e266
(cherry picked from commit 60ed57718c6ee96a8ee9b8cf5905e99913fe0941)
diff --git a/common/libs/wifi/packet_switch.cc b/common/libs/wifi/packet_switch.cc
index 5dc58fe..f365c29 100644
--- a/common/libs/wifi/packet_switch.cc
+++ b/common/libs/wifi/packet_switch.cc
@@ -45,11 +45,13 @@
   shm_xchg_.reset(new std::thread([this] {
       size_t maxlen = getpagesize();
       std::unique_ptr<uint8_t[]> msg(new uint8_t[maxlen]);
+      auto hdr = reinterpret_cast<nlmsghdr*>(msg.get());
+      std::unique_ptr<nl_msg, void (*)(nl_msg*)> nlm(nullptr, nlmsg_free);
+
+      int item = 0;
       while (started_) {
-        // TODO(ender): how to trigger (periodic?) exit from this call?
         auto len = shm_wifi_.Recv(msg.get(), maxlen);
-        std::unique_ptr<nl_msg, void (*)(nl_msg*)> nlm(
-            nlmsg_convert(reinterpret_cast<nlmsghdr*>(msg.get())), nlmsg_free);
+        nlm.reset(nlmsg_convert(hdr));
         ProcessPacket(nlm.get(), true);
       }
     }));
@@ -100,7 +102,7 @@
         }
       }
     } else {
-      shm_wifi_.Send(&header, header->nlmsg_len);
+      shm_wifi_.Send(header, header->nlmsg_len);
     }
   }
 }
diff --git a/common/libs/wifi/packet_switch.h b/common/libs/wifi/packet_switch.h
index c20e681..0fbe213 100644
--- a/common/libs/wifi/packet_switch.h
+++ b/common/libs/wifi/packet_switch.h
@@ -42,7 +42,7 @@
   std::mutex op_mutex_;
   // Started is referenced by all threads created by PacketSwitch to determine
   // whether to carry on working, or terminate.
-  bool started_;
+  bool started_ = false;
 
   std::unique_ptr<std::thread> shm_xchg_;
   vsoc::wifi::WifiExchangeView shm_wifi_;
diff --git a/common/libs/wifi/virtual_wifi.cc b/common/libs/wifi/virtual_wifi.cc
index 365209e..140ff77 100644
--- a/common/libs/wifi/virtual_wifi.cc
+++ b/common/libs/wifi/virtual_wifi.cc
@@ -189,12 +189,12 @@
   return -1;
 }
 
-bool RegisterForRouterNotifications(Netlink* nl, uint8_t* mac_addr) {
+bool RegisterForRouterNotifications(Netlink* nl, int hwsim_id) {
   Cmd msg;
 
   if (!genlmsg_put(msg.Msg(), NL_AUTO_PID, NL_AUTO_SEQ, 0, 0,
                    NLM_F_REQUEST, WIFIROUTER_CMD_REGISTER, 0) ||
-      nla_put(msg.Msg(), WIFIROUTER_ATTR_MAC, MAX_ADDR_LEN, mac_addr)) {
+      nla_put_u32(msg.Msg(), WIFIROUTER_ATTR_HWSIM_ID, hwsim_id)) {
     LOG(ERROR) << "Could not create wifirouter register message.";
     return false;
   }
@@ -284,7 +284,7 @@
 
   // 5. Register with wifi router.
   LOG(INFO) << "Registering for notifications for: " << addr_;
-  if (!RegisterForRouterNotifications(nl_, mac_addr_)) {
+  if (!RegisterForRouterNotifications(nl_, hwsim_number_)) {
     LOG(ERROR) << "Could not register with wifi router.";
     return false;
   }