Add template listener for getting OEM netd

It is a template OemNetdListener that OEM could extend their
binder interface based on it.

Bug: 120251561
Test: built, flashed, booted
        system/netd/tests/runtests.sh pass

Change-Id: Ida29af3f970f65fafc733617ac091747fe10eb11
diff --git a/server/Android.bp b/server/Android.bp
index 43e6cc4..7083f47 100644
--- a/server/Android.bp
+++ b/server/Android.bp
@@ -45,6 +45,14 @@
     ],
 }
 
+aidl_interface {
+    name: "oemnetd_aidl_interface",
+    local_include_dir: "binder",
+    srcs: [
+        "binder/com/android/internal/net/IOemNetd.aidl",
+    ],
+}
+
 // Modules common to both netd and netd_unit_test
 cc_library_static {
     name: "libnetd_server",
@@ -133,6 +141,7 @@
         "libutils",
         "netd_aidl_interface-cpp",
         "netd_event_listener_interface-cpp",
+        "oemnetd_aidl_interface-cpp",
     ],
     static_libs: [
         "libnetd_server",
@@ -152,6 +161,7 @@
         "NetlinkHandler.cpp",
         "Network.cpp",
         "NetworkController.cpp",
+        "OemNetdListener.cpp",
         "PhysicalNetwork.cpp",
         "PppController.cpp",
         "Process.cpp",
diff --git a/server/NetdNativeService.cpp b/server/NetdNativeService.cpp
index ab64973..fe575df 100644
--- a/server/NetdNativeService.cpp
+++ b/server/NetdNativeService.cpp
@@ -42,6 +42,7 @@
 #include "NetdConstants.h"  // SHA256_SIZE
 #include "NetdNativeService.h"
 #include "NetdPermissions.h"
+#include "OemNetdListener.h"
 #include "Permission.h"
 #include "Process.h"
 #include "RouteController.h"
@@ -1256,7 +1257,8 @@
 
 binder::Status NetdNativeService::getOemNetd(android::sp<android::IBinder>* listener) {
     ENFORCE_NETWORK_STACK_PERMISSIONS();
-    *listener = nullptr;
+    *listener = com::android::internal::net::OemNetdListener::getListener();
+
     return binder::Status::ok();
 }
 
diff --git a/server/OemNetdListener.cpp b/server/OemNetdListener.cpp
new file mode 100644
index 0000000..dddfef0
--- /dev/null
+++ b/server/OemNetdListener.cpp
@@ -0,0 +1,54 @@
+/**
+ * Copyright (c) 2019, 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 "OemNetd"
+
+#include <log/log.h>
+#include <utils/Errors.h>
+
+#include <binder/IPCThreadState.h>
+
+#include "OemNetdListener.h"
+
+#include "com/android/internal/net/BnOemNetd.h"
+
+namespace com {
+namespace android {
+namespace internal {
+namespace net {
+
+::android::sp<::android::IBinder> OemNetdListener::getListener() {
+    static OemNetdListener listener;
+    return listener.getIBinder();
+}
+
+::android::sp<::android::IBinder> OemNetdListener::getIBinder() {
+    std::lock_guard lock(mMutex);
+    if (mIBinder == nullptr) {
+        mIBinder = ::android::IInterface::asBinder(this);
+    }
+    return mIBinder;
+}
+
+::android::binder::Status OemNetdListener::isAlive(bool* alive) {
+    *alive = true;
+    return ::android::binder::Status::ok();
+}
+
+}  // namespace net
+}  // namespace internal
+}  // namespace android
+}  // namespace com
diff --git a/server/OemNetdListener.h b/server/OemNetdListener.h
new file mode 100644
index 0000000..a17039c
--- /dev/null
+++ b/server/OemNetdListener.h
@@ -0,0 +1,47 @@
+/**
+ * Copyright (c) 2019, 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.
+ */
+
+#ifndef NETD_SERVER_OEM_NETD_LISTENER_H
+#define NETD_SERVER_OEM_NETD_LISTENER_H
+
+#include "com/android/internal/net/BnOemNetd.h"
+
+namespace com {
+namespace android {
+namespace internal {
+namespace net {
+
+class OemNetdListener : public BnOemNetd {
+  public:
+    OemNetdListener() = default;
+    ~OemNetdListener() = default;
+    static ::android::sp<::android::IBinder> getListener();
+
+    ::android::binder::Status isAlive(bool* alive) override;
+
+  private:
+    std::mutex mMutex;
+    ::android::sp<::android::IBinder> mIBinder GUARDED_BY(mMutex);
+
+    ::android::sp<::android::IBinder> getIBinder() EXCLUDES(mMutex);
+};
+
+}  // namespace net
+}  // namespace internal
+}  // namespace android
+}  // namespace com
+
+#endif  // NETD_SERVER_OEM_NETD_LISTENER_H
\ No newline at end of file
diff --git a/server/binder/com/android/internal/net/IOemNetd.aidl b/server/binder/com/android/internal/net/IOemNetd.aidl
new file mode 100644
index 0000000..7b22389
--- /dev/null
+++ b/server/binder/com/android/internal/net/IOemNetd.aidl
@@ -0,0 +1,25 @@
+/**
+ * Copyright (c) 2019, 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 com.android.internal.net;
+
+/** {@hide} */
+interface IOemNetd {
+    /**
+     * Returns true if the service is responding.
+     */
+    boolean isAlive();
+}