Merge changes from topic "provide events > 1.5  Hz" into rvc-qpr-dev

* changes:
  Add support for variable sampling period
  Add minor improvements in sensors
  Set minDelay and maxDelay to 0 for ON_CHANGE_MODE sensors
  Check if sensorHandle refers to the sensor that is available
  Report the sensors available from goldfish sensor HAL
diff --git a/overlay/frameworks/base/core/res/res/values/config.xml b/overlay/frameworks/base/core/res/res/values/config.xml
index a63f25a..b9f0bab 100644
--- a/overlay/frameworks/base/core/res/res/values/config.xml
+++ b/overlay/frameworks/base/core/res/res/values/config.xml
@@ -41,6 +41,11 @@
          which means to get a larger screen. -->
     <bool name="config_lidControlsDisplayFold">true</bool>
 
+    <!-- Allow testing SoftAP using the simulated interfaces on the emulator. -->
+    <string-array name="config_tether_wifi_regexs">
+      <item>"wlan\\d"</item>
+    </string-array>
+
     <!-- List of biometric sensors on the device, in decreasing strength. Consumed by AuthService
      when registering authenticators with BiometricService. Format must be ID:Modality:Strength,
      where: IDs are unique per device, Modality as defined in BiometricAuthenticator.java,
diff --git a/rro_overlays/TetheringOverlay/Android.bp b/rro_overlays/TetheringOverlay/Android.bp
new file mode 100644
index 0000000..31b0c57
--- /dev/null
+++ b/rro_overlays/TetheringOverlay/Android.bp
@@ -0,0 +1,22 @@
+//
+// Copyright (C) 2020 The Android Open-Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+runtime_resource_overlay {
+    name: "EmulatorTetheringConfigOverlay",
+    resource_dirs: ["res"],
+    product_specific: true,
+    sdk_version: "current",
+}
diff --git a/rro_overlays/TetheringOverlay/AndroidManifest.xml b/rro_overlays/TetheringOverlay/AndroidManifest.xml
new file mode 100644
index 0000000..fc8c8bd
--- /dev/null
+++ b/rro_overlays/TetheringOverlay/AndroidManifest.xml
@@ -0,0 +1,11 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.networkstack.tethering.emulator"
+    android:versionCode="1"
+    android:versionName="1.0">
+    <application android:hasCode="false" />
+    <overlay
+      android:targetPackage="com.android.networkstack.tethering"
+      android:targetName="TetheringConfig"
+      android:isStatic="true"
+      android:priority="0"/>
+</manifest>
diff --git a/overlay/frameworks/base/packages/Tethering/res/values/config.xml b/rro_overlays/TetheringOverlay/res/values/config.xml
similarity index 88%
rename from overlay/frameworks/base/packages/Tethering/res/values/config.xml
rename to rro_overlays/TetheringOverlay/res/values/config.xml
index df8c183..8cb8b40 100644
--- a/overlay/frameworks/base/packages/Tethering/res/values/config.xml
+++ b/rro_overlays/TetheringOverlay/res/values/config.xml
@@ -22,4 +22,7 @@
     <string-array name="config_tether_wifi_regexs">
       <item>"wlan\\d"</item>
     </string-array>
+    <string-array name="config_tether_wifi_p2p_regexs">
+      <item>"p2p-wlan\\d-.*"</item>
+    </string-array>
 </resources>
diff --git a/vendor.mk b/vendor.mk
index 68338f7..4a58872 100644
--- a/vendor.mk
+++ b/vendor.mk
@@ -59,6 +59,7 @@
     local_time.default \
     SdkSetup \
     EmulatorRadioConfig \
+    EmulatorTetheringConfigOverlay \
     libstagefrighthw \
     libstagefright_goldfish_vpxdec \
     libstagefright_goldfish_avcdec \
diff --git a/wifi/wifi_hal/Android.bp b/wifi/wifi_hal/Android.bp
index 88c1e30..6431003 100644
--- a/wifi/wifi_hal/Android.bp
+++ b/wifi/wifi_hal/Android.bp
@@ -28,6 +28,7 @@
         "wifi_hal.cpp",
     ],
     shared_libs: [
+        "libnl",
         "liblog",
         "libcutils",
         "libhardware_legacy",
diff --git a/wifi/wifi_hal/interface.cpp b/wifi/wifi_hal/interface.cpp
index 7c683e5..47d8154 100644
--- a/wifi/wifi_hal/interface.cpp
+++ b/wifi/wifi_hal/interface.cpp
@@ -86,6 +86,9 @@
     return WIFI_SUCCESS;
 }
 
+// Wifi legacy HAL implicitly assumes getLinkStats is blocking and
+// handler will be set to nullptr immediately after invocation.
+// Therefore, this function will wait until onLinkStatsReply is called.
 wifi_error Interface::getLinkStats(wifi_request_id requestId,
                                    wifi_stats_result_handler handler) {
     NetlinkMessage message(RTM_GETLINK, mNetlink.getSequenceNumber());
@@ -97,12 +100,21 @@
     info->ifi_flags = 0;
     info->ifi_change = 0xFFFFFFFF;
 
-    bool success = mNetlink.sendMessage(message,
-                                        std::bind(&Interface::onLinkStatsReply,
-                                                  this,
-                                                  requestId,
-                                                  handler,
-                                                  std::placeholders::_1));
+    std::condition_variable condition;
+    std::mutex mutex;
+    std::unique_lock<std::mutex> lock(mutex);
+    bool stopped = false;
+    auto callback = [this, requestId, &handler,
+        &mutex, &condition, &stopped] (const NetlinkMessage& message) {
+        stopped = true;
+        std::unique_lock<std::mutex> lock(mutex);
+        onLinkStatsReply(requestId, handler, message);
+        condition.notify_all();
+    };
+    bool success = mNetlink.sendMessage(message, callback);
+    while (!stopped) {
+        condition.wait(lock);
+    }
     return success ? WIFI_SUCCESS : WIFI_ERROR_UNKNOWN;
 }
 
diff --git a/wifi/wifi_hal/netlinkmessage.cpp b/wifi/wifi_hal/netlinkmessage.cpp
index baf5800..06bb743 100644
--- a/wifi/wifi_hal/netlinkmessage.cpp
+++ b/wifi/wifi_hal/netlinkmessage.cpp
@@ -22,6 +22,7 @@
 #include <linux/rtnetlink.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <netlink/msg.h>
 
 size_t getSpaceForMessageType(uint16_t type) {
     switch (type) {
@@ -51,13 +52,12 @@
 
 bool NetlinkMessage::getAttribute(int attributeId, void* data, size_t size) const {
     const void* value = nullptr;
-    uint16_t attrSize = 0;
-    if (!findAttribute(attributeId, &value, &attrSize)) {
+    const auto attr = nlmsg_find_attr((struct nlmsghdr*)mData.data(), sizeof(ifinfomsg), attributeId);
+    if (!attr) {
         return false;
     }
-    if (size > attrSize) {
-        return false;
-    }
+    value = (const uint8_t*) attr + NLA_HDRLEN;
+    size = attr->nla_len;
     memcpy(data, value, size);
     return true;
 }
@@ -71,30 +71,3 @@
     auto header = reinterpret_cast<const nlmsghdr*>(mData.data());
     return header->nlmsg_seq;
 }
-
-bool NetlinkMessage::findAttribute(int attributeId,
-                                   const void** value,
-                                   uint16_t* size) const {
-    const uint8_t* end = mData.data() + mData.size();
-    size_t attrOffset = getSpaceForMessageType(type());
-    if (attrOffset == 0) {
-        return false;
-    }
-    const uint8_t* attribute = mData.data() + attrOffset;
-    while (attribute < end) {
-        auto header = reinterpret_cast<const nlattr*>(attribute);
-        if (header->nla_len == 0) {
-            // The length should include the header so the length should always
-            // be greater than zero. If it doesn't we're going to end up looping
-            // forever so ignore this.
-            return false;
-        }
-        if (header->nla_type == attributeId) {
-            *value = attribute + NLA_HDRLEN;
-            *size = header->nla_len;
-            return true;
-        }
-        attribute += header->nla_len;
-    }
-    return false;
-}
diff --git a/wifi/wifi_hal/netlinkmessage.h b/wifi/wifi_hal/netlinkmessage.h
index 45fd7cd..5e1a3b8 100644
--- a/wifi/wifi_hal/netlinkmessage.h
+++ b/wifi/wifi_hal/netlinkmessage.h
@@ -60,9 +60,6 @@
     NetlinkMessage& operator=(const NetlinkMessage&) = delete;
 
     bool getAttribute(int attributeId, void* data, size_t size) const;
-    bool findAttribute(int attributeId,
-                       const void** value,
-                       uint16_t* size) const;
 
     std::vector<uint8_t> mData;
 };