Reland "create minimal HAL for NETD"

This reverts commit 29ae9622a1cd2109c4ed5226b1729f846015acf1.

Reason for revert: reland with less merge conflicts

Test: as follows
    - built
    - flashed
    - booted
Bug: 36682246
Change-Id: Ibb06cd65969b5765482b62abec9051207366e0b8
(cherry picked from commit 5e94f2ca0b8304cc24a1e58d14f23ef02289a5a4)
diff --git a/Android.bp b/Android.bp
index ebf99c2..64fdde4 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,5 +1,7 @@
 // This is an autogenerated file, do not edit.
 subdirs = [
+    "net/netd/1.0",
+    "net/netd/1.0/vts/functional",
     "wifi/keystore/1.0",
     "wifi/keystore/1.0/vts/functional",
 ]
diff --git a/net/netd/1.0/Android.bp b/net/netd/1.0/Android.bp
new file mode 100644
index 0000000..a585d20
--- /dev/null
+++ b/net/netd/1.0/Android.bp
@@ -0,0 +1,59 @@
+// This file is autogenerated by hidl-gen. Do not edit manually.
+
+filegroup {
+    name: "android.system.net.netd@1.0_hal",
+    srcs: [
+        "INetd.hal",
+    ],
+}
+
+genrule {
+    name: "android.system.net.netd@1.0_genc++",
+    tools: ["hidl-gen"],
+    cmd: "$(location hidl-gen) -o $(genDir) -Lc++-sources -randroid.hidl:system/libhidl/transport -randroid.system:system/hardware/interfaces android.system.net.netd@1.0",
+    srcs: [
+        ":android.system.net.netd@1.0_hal",
+    ],
+    out: [
+        "android/system/net/netd/1.0/NetdAll.cpp",
+    ],
+}
+
+genrule {
+    name: "android.system.net.netd@1.0_genc++_headers",
+    tools: ["hidl-gen"],
+    cmd: "$(location hidl-gen) -o $(genDir) -Lc++-headers -randroid.hidl:system/libhidl/transport -randroid.system:system/hardware/interfaces android.system.net.netd@1.0",
+    srcs: [
+        ":android.system.net.netd@1.0_hal",
+    ],
+    out: [
+        "android/system/net/netd/1.0/INetd.h",
+        "android/system/net/netd/1.0/IHwNetd.h",
+        "android/system/net/netd/1.0/BnHwNetd.h",
+        "android/system/net/netd/1.0/BpHwNetd.h",
+        "android/system/net/netd/1.0/BsNetd.h",
+    ],
+}
+
+cc_library_shared {
+    name: "android.system.net.netd@1.0",
+    defaults: ["hidl-module-defaults"],
+    generated_sources: ["android.system.net.netd@1.0_genc++"],
+    generated_headers: ["android.system.net.netd@1.0_genc++_headers"],
+    export_generated_headers: ["android.system.net.netd@1.0_genc++_headers"],
+    vendor_available: true,
+    shared_libs: [
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "liblog",
+        "libutils",
+        "libcutils",
+    ],
+    export_shared_lib_headers: [
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "libutils",
+    ],
+}
diff --git a/net/netd/1.0/INetd.hal b/net/netd/1.0/INetd.hal
new file mode 100644
index 0000000..3f69892
--- /dev/null
+++ b/net/netd/1.0/INetd.hal
@@ -0,0 +1,58 @@
+/*
+ * Copyright 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.
+ */
+
+package android.system.net.netd@1.0;
+
+/**
+ * This is the root of the HAL module and is the interface returned when
+ * loading an implementation of the INetd HAL.
+ */
+interface INetd {
+    /**
+     * Return values for INetd requests
+     */
+    enum StatusCode : int32_t {
+        OK,
+        INVALID_ARGUMENTS,
+        NO_NETWORK,
+        ALREADY_EXISTS,
+        PERMISSION_DENIED,
+        UNKNOWN_ERROR
+    };
+
+    /**
+     * Creates a physical network to be used for interfaces managed by the OEM
+     *
+     * @return networkHandle a handle to the OEM network. Zero implies
+     *             no networks are available and created
+     * @return packetMark The packet mark that vendor network stack configuration code must use when
+     *             routing packets to this network. No applications may use this mark. They must
+     *             instead pass the networkHandle to the android_set*network LL-NDK APIs.
+     * @return status operation status
+     */
+    @entry
+    @callflow(next={"*"})
+    createOemNetwork() generates (uint64_t networkHandle, uint32_t packetMark, StatusCode status);
+
+    /**
+     * Destroys the specified network previously created using createOemNetwork()
+     *
+     * @return status operation status
+     */
+    @exit
+    @callflow(next="createOemNetwork")
+    destroyOemNetwork(uint64_t networkHandle) generates (StatusCode status);
+};
diff --git a/net/netd/1.0/vts/functional/Android.bp b/net/netd/1.0/vts/functional/Android.bp
new file mode 100644
index 0000000..61e612a
--- /dev/null
+++ b/net/netd/1.0/vts/functional/Android.bp
@@ -0,0 +1,18 @@
+cc_test {
+    name: "VtsHalNetNetdV1_0TargetTest",
+    srcs: [
+        "VtsHalNetNetdV1_0TargetTest.cpp",
+    ],
+    shared_libs: [
+        "liblog",
+        "libhidlbase",
+        "libhidltransport",
+        "libutils",
+        "android.system.net.netd@1.0",
+    ],
+    static_libs: ["VtsHalHidlTargetTestBase"],
+    cflags: [
+        "-O0",
+        "-g",
+    ],
+}
diff --git a/net/netd/1.0/vts/functional/VtsHalNetNetdV1_0TargetTest.cpp b/net/netd/1.0/vts/functional/VtsHalNetNetdV1_0TargetTest.cpp
new file mode 100644
index 0000000..c332c10
--- /dev/null
+++ b/net/netd/1.0/vts/functional/VtsHalNetNetdV1_0TargetTest.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright 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.
+ */
+
+#define LOG_TAG "netd_hidl_test"
+
+#include <VtsHalHidlTargetTestBase.h>
+#include <android/system/net/netd/1.0/INetd.h>
+#include <log/log.h>
+
+using ::android::system::net::netd::V1_0::INetd;
+using ::android::hardware::Return;
+using ::android::sp;
+
+class NetdHidlTest : public ::testing::VtsHalHidlTargetTestBase {
+   public:
+    virtual void SetUp() override {
+        netd = ::testing::VtsHalHidlTargetTestBase::getService<INetd>();
+        ASSERT_NE(nullptr, netd.get()) << "Could not get HIDL instance";
+    }
+
+    sp<INetd> netd;
+};
+
+// positive test. Ensure netd creates an oem network and returns valid netHandle, and destroys it.
+TEST_F(NetdHidlTest, TestCreateAndDestroyOemNetworkOk) {
+    auto cb = [this](uint64_t netHandle, uint32_t packetMark, INetd::StatusCode status) {
+        ASSERT_EQ(INetd::StatusCode::OK, status);
+        ASSERT_NE((uint64_t)0, netHandle);
+        ASSERT_NE((uint32_t)0, packetMark);
+
+        Return<INetd::StatusCode> retStatus = netd->destroyOemNetwork(netHandle);
+        ASSERT_EQ(INetd::StatusCode::OK, retStatus);
+    };
+
+    Return<void> ret = netd->createOemNetwork(cb);
+    ASSERT_TRUE(ret.isOk());
+}
+
+// negative test. Ensure destroy for invalid OEM network fails appropriately
+TEST_F(NetdHidlTest, TestDestroyOemNetworkInvalid) {
+    const uint64_t nh = 0x6600FACADE;
+
+    Return<INetd::StatusCode> retStatus = netd->destroyOemNetwork(nh);
+    ASSERT_EQ(INetd::StatusCode::INVALID_ARGUMENTS, retStatus);
+}
+
+int main(int argc, char** argv) {
+    ::testing::InitGoogleTest(&argc, argv);
+    int status = RUN_ALL_TESTS();
+    ALOGE("Test result with status=%d", status);
+    return status;
+}