Snap for 8626064 from 0ad7df11176eacd42be6540fedd62954f896da48 to mainline-go-adservices-release

Change-Id: I7208741a93aaa38dce2f9cd3e7cde8a947bdfab9
diff --git a/net/netlink_manager.cpp b/net/netlink_manager.cpp
index 22154a7..64db5d8 100644
--- a/net/netlink_manager.cpp
+++ b/net/netlink_manager.cpp
@@ -48,7 +48,7 @@
 // netlink.h suggests NLMSG_GOODSIZE to be at most 8192 bytes.
 constexpr int kReceiveBufferSize = 8 * 1024;
 constexpr uint32_t kBroadcastSequenceNumber = 0;
-constexpr int kMaximumNetlinkMessageWaitMilliSeconds = 300;
+constexpr int kMaximumNetlinkMessageWaitMilliSeconds = 1000;
 uint8_t ReceiveBuffer[kReceiveBufferSize];
 
 void AppendPacket(vector<unique_ptr<const NL80211Packet>>* vec,
@@ -99,7 +99,8 @@
 void NetlinkManager::ReceivePacketAndRunHandler(int fd) {
   ssize_t len = read(fd, ReceiveBuffer, kReceiveBufferSize);
   if (len == -1) {
-    LOG(ERROR) << "Failed to read packet from buffer";
+    LOG(ERROR) << "Failed to read packet from buffer on fd: " << fd;
+    perror(" netlink error ");
     return;
   }
   if (len == 0) {
@@ -307,7 +308,7 @@
                            time_remaining);
 
     if (poll_return == 0) {
-      LOG(ERROR) << "Failed to poll netlink fd: time out ";
+      LOG(ERROR) << "Failed to poll netlink fd:" << sync_netlink_fd_.get() << "time out ";
       message_handlers_.erase(sequence);
       return false;
     } else if (poll_return == -1) {
diff --git a/net/netlink_utils.cpp b/net/netlink_utils.cpp
index 73901ca..dc2e4be 100644
--- a/net/netlink_utils.cpp
+++ b/net/netlink_utils.cpp
@@ -135,8 +135,9 @@
       netlink_manager_->GetSequenceNumber(),
       getpid());
   get_wiphy.AddFlag(NLM_F_DUMP);
+  int ifindex;
   if (!iface_name.empty()) {
-    int ifindex = if_nametoindex(iface_name.c_str());
+    ifindex = if_nametoindex(iface_name.c_str());
     if (ifindex == 0) {
       PLOG(ERROR) << "Can't get " << iface_name << " index";
       return false;
@@ -145,7 +146,8 @@
   }
   vector<unique_ptr<const NL80211Packet>> response;
   if (!netlink_manager_->SendMessageAndGetResponses(get_wiphy, &response))  {
-    LOG(ERROR) << "NL80211_CMD_GET_WIPHY dump failed";
+    LOG(ERROR) << "NL80211_CMD_GET_WIPHY dump failed, ifindex: "
+               << ifindex << " and name: " << iface_name.c_str();
     return false;
   }
   if (response.empty()) {
diff --git a/scanning/scanner_impl.cpp b/scanning/scanner_impl.cpp
index bba3417..c7857df 100644
--- a/scanning/scanner_impl.cpp
+++ b/scanning/scanner_impl.cpp
@@ -225,6 +225,18 @@
   vector<vector<uint8_t>> skipped_match_ssids;
   std::set<int32_t> unique_frequencies;
   int num_networks_no_freqs = 0;
+  // Get the list of supported frequencies
+  const auto band_2g = client_interface_->GetBandInfo().band_2g;
+  const auto band_5g = client_interface_->GetBandInfo().band_5g;
+  const auto band_dfs = client_interface_->GetBandInfo().band_dfs;
+  const auto band_6g = client_interface_->GetBandInfo().band_6g;
+  const auto band_60g = client_interface_->GetBandInfo().band_60g;
+  std::set<uint32_t> all_freqs;
+  all_freqs.insert(band_2g.begin(), band_2g.end());
+  all_freqs.insert(band_5g.begin(), band_5g.end());
+  all_freqs.insert(band_dfs.begin(), band_dfs.end());
+  all_freqs.insert(band_6g.begin(), band_6g.end());
+  all_freqs.insert(band_60g.begin(), band_60g.end());
   for (auto& network : pno_settings.pno_networks_) {
     // Add hidden network ssid.
     if (network.is_hidden_) {
@@ -245,7 +257,11 @@
 
     // build the set of unique frequencies to scan for.
     for (const auto& frequency : network.frequencies_) {
-      unique_frequencies.insert(frequency);
+      if (all_freqs.find(frequency) != all_freqs.end()) {
+        unique_frequencies.insert(frequency);
+      } else {
+        LOG(INFO) << "filtered out invalid frequency " << frequency;
+      }
     }
     if (network.frequencies_.empty()) {
       num_networks_no_freqs++;
@@ -256,14 +272,12 @@
   // networks don't have frequency data.
   if (num_networks_no_freqs * 100 > kPercentNetworksWithFreq * match_ssids->size()) {
     // Filter out frequencies not supported by chip.
-    const auto band_2g = client_interface_->GetBandInfo().band_2g;
     for (const auto frequency : kPnoScanDefaultFreqs2G) {
       if (std::find(band_2g.begin(), band_2g.end(), frequency) != band_2g.end()) {
         unique_frequencies.insert(frequency);
       }
     }
     // Note: kPnoScanDefaultFreqs5G doesn't contain DFS frequencies.
-    const auto band_5g = client_interface_->GetBandInfo().band_5g;
     for (const auto frequency : kPnoScanDefaultFreqs5G) {
       if (std::find(band_5g.begin(), band_5g.end(), frequency) != band_5g.end()) {
         unique_frequencies.insert(frequency);
diff --git a/server.cpp b/server.cpp
index 1c40185..1fd10dd 100644
--- a/server.cpp
+++ b/server.cpp
@@ -488,12 +488,19 @@
 }
 
 void Server::OnRegDomainChanged(uint32_t wiphy_index, std::string& country_code) {
+  string current_country_code;
   if (country_code.empty()) {
-    LOG(INFO) << "Regulatory domain changed";
+    LOG(DEBUG) << "Regulatory domain changed with empty country code (world mode?)";
+    if (!netlink_utils_->GetCountryCode(&current_country_code)) {
+        LOG(ERROR) << "Fail to get country code on wiphy_index:" << wiphy_index;
+    }
   } else {
-    LOG(INFO) << "Regulatory domain changed to country: " << country_code
-              << " on wiphy_index: " << wiphy_index;
-    BroadcastRegDomainChanged(country_code);
+      current_country_code = country_code;
+  }
+  if (!current_country_code.empty()) {
+      LOG(INFO) << "Regulatory domain changed to country: " << current_country_code
+                << " on wiphy_index: " << wiphy_index;
+      BroadcastRegDomainChanged(current_country_code);
   }
   // Sometimes lower layer sends stale wiphy index when there are no
   // interfaces. So update band - wiphy index mapping only if an