Move system SDK versions to system manifest am: 1fd1de9c7a

Original change: https://googleplex-android-review.googlesource.com/c/platform/system/libhidl/+/11683215

Change-Id: I343f4e3279c91bdfcc12ad72e219a28c11f1c0cd
diff --git a/MODULE_LICENSE_APACHE2 b/MODULE_LICENSE_APACHE2
deleted file mode 100644
index e69de29..0000000
--- a/MODULE_LICENSE_APACHE2
+++ /dev/null
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 01179d5..22c9d36 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -8,6 +8,9 @@
     },
     {
       "name": "hal_implementation_test"
+    },
+    {
+      "name": "hidl_lazy_test"
     }
   ]
 }
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 1b91c26..1bb38e8 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -342,7 +342,6 @@
         memset(mPad, 0, sizeof(mPad));
     }
 
-    // Note, does not initialize primitive types.
     hidl_vec(size_t size) : hidl_vec() { resize(size); }
 
     hidl_vec(const hidl_vec<T> &other) : hidl_vec() {
@@ -412,7 +411,7 @@
     }
 
     T *releaseData() {
-        if (!mOwnsBuffer && mSize > 0) {
+        if (!mOwnsBuffer && mBuffer != nullptr) {
             resize(mSize);
         }
         mOwnsBuffer = false;
@@ -507,7 +506,7 @@
         return mBuffer[index];
     }
 
-    // Does not initialize primitive types if new size > old size.
+    // Copies over old elements fitting in new size. Value initializes the rest.
     void resize(size_t size) {
         if (size > UINT32_MAX) {
             details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
@@ -1044,6 +1043,9 @@
  */
 template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
 struct hidl_enum_range {
+    // Container-like associated type.
+    using value_type = T;
+
     constexpr auto begin() const { return std::begin(details::hidl_enum_values<T>); }
     constexpr auto cbegin() const { return begin(); }
     constexpr auto rbegin() const { return std::rbegin(details::hidl_enum_values<T>); }
diff --git a/test_main.cpp b/test_main.cpp
index e4cdd29..0a1e97b 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -222,6 +222,24 @@
     EXPECT_ARRAYEQ(v3, array, v3.size());
 }
 
+TEST_F(LibHidlTest, VecReleaseTest) {
+    // this test indicates an inconsistency of behaviors which is undesirable.
+    // Perhaps hidl-vec should always allocate an empty vector whenever it
+    // exposes its data. Alternatively, perhaps it should always free/reject
+    // empty vectors and always return nullptr for this state. While this second
+    // alternative is faster, it makes client code harder to write, and it would
+    // break existing client code.
+    using android::hardware::hidl_vec;
+
+    hidl_vec<int32_t> empty;
+    EXPECT_EQ(nullptr, empty.releaseData());
+
+    empty.resize(0);
+    int32_t* data = empty.releaseData();
+    EXPECT_NE(nullptr, data);
+    delete data;
+}
+
 TEST_F(LibHidlTest, VecIterTest) {
     int32_t array[] = {5, 6, 7};
     android::hardware::hidl_vec<int32_t> hv1 = std::vector<int32_t>(array, array + 3);
diff --git a/transport/HidlLazyUtils.cpp b/transport/HidlLazyUtils.cpp
index 08ed676..b943c99 100644
--- a/transport/HidlLazyUtils.cpp
+++ b/transport/HidlLazyUtils.cpp
@@ -29,15 +29,27 @@
 using ::android::hidl::base::V1_0::IBase;
 
 class ClientCounterCallback : public ::android::hidl::manager::V1_2::IClientCallback {
-   public:
-    ClientCounterCallback() : mNumConnectedServices(0) {}
+  public:
+    ClientCounterCallback() {}
 
     bool addRegisteredService(const sp<IBase>& service, const std::string& name);
 
-   protected:
+  protected:
     Return<void> onClients(const sp<IBase>& service, bool clients) override;
 
-   private:
+  private:
+    struct Service {
+        sp<IBase> service;
+        std::string name;
+        bool clients = false;
+    };
+
+    /**
+     * Looks up service that is guaranteed to be registered (service from
+     * onClients).
+     */
+    Service& assertRegisteredService(const sp<IBase>& service);
+
     /**
      * Registers or re-registers services. Returns whether successful.
      */
@@ -50,28 +62,19 @@
     void tryShutdown();
 
     /**
-     * Counter of the number of services that currently have at least one client.
-     */
-    size_t mNumConnectedServices;
-
-    struct Service {
-        sp<IBase> service;
-        std::string name;
-    };
-    /**
      * Number of services that have been registered.
      */
     std::vector<Service> mRegisteredServices;
 };
 
 class LazyServiceRegistrarImpl {
-   public:
+  public:
     LazyServiceRegistrarImpl() : mClientCallback(new ClientCounterCallback) {}
 
     status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
                              const std::string& name);
 
-   private:
+  private:
     sp<ClientCounterCallback> mClientCallback;
 };
 
@@ -86,6 +89,17 @@
     return success;
 }
 
+ClientCounterCallback::Service& ClientCounterCallback::assertRegisteredService(
+        const sp<IBase>& service) {
+    for (Service& registered : mRegisteredServices) {
+        if (registered.service != service) continue;
+        return registered;
+    }
+    LOG(FATAL) << "Got callback on service " << getDescriptor(service.get())
+               << " which we did not register.";
+    __builtin_unreachable();
+}
+
 bool ClientCounterCallback::registerService(const sp<IBase>& service, const std::string& name) {
     auto manager = hardware::defaultServiceManager1_2();
 
@@ -114,17 +128,24 @@
  */
 Return<void> ClientCounterCallback::onClients(const sp<::android::hidl::base::V1_0::IBase>& service,
                                               bool clients) {
-    if (clients) {
-        mNumConnectedServices++;
-    } else {
-        mNumConnectedServices--;
+    Service& registered = assertRegisteredService(service);
+    if (registered.clients == clients) {
+        LOG(FATAL) << "Process already thought " << getDescriptor(service.get()) << "/"
+                   << registered.name << " had clients: " << registered.clients
+                   << " but hwservicemanager has notified has clients: " << clients;
+    }
+    registered.clients = clients;
+
+    size_t numWithClients = 0;
+    for (const Service& registered : mRegisteredServices) {
+        if (registered.clients) numWithClients++;
     }
 
-    LOG(INFO) << "Process has " << mNumConnectedServices << " (of " << mRegisteredServices.size()
+    LOG(INFO) << "Process has " << numWithClients << " (of " << mRegisteredServices.size()
               << " available) client(s) in use after notification " << getDescriptor(service.get())
-              << " has clients: " << clients;
+              << "/" << registered.name << " has clients: " << clients;
 
-    if (mNumConnectedServices == 0) {
+    if (numWithClients == 0) {
         tryShutdown();
     }
 
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index d7faa6d..a69cf3e 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -192,6 +192,7 @@
     tryShortenProcessName(descriptor);
 }
 
+// only used by prebuilts - should be able to remove
 void onRegistration(const std::string& packageName, const std::string& interfaceName,
                     const std::string& instanceName) {
     return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
diff --git a/transport/allocator/1.0/vts/functional/Android.bp b/transport/allocator/1.0/vts/functional/Android.bp
index 31d9821..ba7aa70 100644
--- a/transport/allocator/1.0/vts/functional/Android.bp
+++ b/transport/allocator/1.0/vts/functional/Android.bp
@@ -27,3 +27,6 @@
     test_suites: ["general-tests", "vts"],
 }
 
+vts_config {
+    name: "VtsHidlAllocatorV1_0Target",
+}
diff --git a/transport/base/1.0/vts/functional/Android.bp b/transport/base/1.0/vts/functional/Android.bp
index 0c814ae..1c6e7ad 100644
--- a/transport/base/1.0/vts/functional/Android.bp
+++ b/transport/base/1.0/vts/functional/Android.bp
@@ -40,3 +40,7 @@
     require_root: true,
     auto_gen_config: true,
 }
+
+vts_config {
+    name: "VtsHalBaseV1_0TargetTest",
+}
diff --git a/transport/base/1.0/vts/functional/Android.mk b/transport/base/1.0/vts/functional/Android.mk
deleted file mode 100644
index 61c6e31..0000000
--- a/transport/base/1.0/vts/functional/Android.mk
+++ /dev/null
@@ -1,22 +0,0 @@
-#
-# Copyright (C) 2017 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.
-#
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := VtsHalBaseV1_0TargetTest
--include test/vts/tools/build/Android.host_config.mk