Ashmemd HIDL Client Lib am: eb4e9b38d4
am: 0b584e7e0c

Change-Id: I61730ff66633d01a19c6969a65260a8e5c5c0773
diff --git a/Android.bp b/Android.bp
index bff956e..1cb86b7 100644
--- a/Android.bp
+++ b/Android.bp
@@ -23,8 +23,6 @@
 cc_defaults {
     name: "ashmemd_defaults",
     shared_libs: [
-        "android.system.ashmem@1.0",
-        "ashmemd_aidl_interface-cpp",
         "libbase",
         "libbinder",
         "libcutils",
@@ -42,16 +40,37 @@
     name: "ashmemd",
     defaults: ["ashmemd_defaults"],
     srcs: ["ashmemd.cpp"],
+    shared_libs: [
+        "android.system.ashmem@1.0",
+        "ashmemd_aidl_interface-cpp",
+    ],
     init_rc: ["ashmemd.rc"],
     vintf_fragments: ["android.system.ashmem@1.0.xml"],
 }
 
 // This library is used to communicate with ashmemd using dlopen/dlsym. We do
-// this to avoid shared library dependecy cycles.
+// this to avoid shared library dependency cycles.
 cc_library {
     name: "libashmemd_client",
     defaults: ["ashmemd_defaults"],
     srcs: ["ashmemd_client.cpp"],
+    shared_libs: ["ashmemd_aidl_interface-cpp"],
+    cflags: [
+        "-Wexit-time-destructors",
+        "-fno-c++-static-destructors",
+    ],
+}
+
+// This library is used to communicate with ashmemd using dlopen/dlsym. We do
+// this to avoid shared library dependency cycles.
+cc_library {
+    name: "libashmemd_hidl_client",
+    vendor: true,
+    defaults: ["ashmemd_defaults"],
+    srcs: ["ashmemd_hidl_client.cpp"],
+    shared_libs: [
+        "android.system.ashmem@1.0",
+    ],
     cflags: [
         "-Wexit-time-destructors",
         "-fno-c++-static-destructors",
@@ -62,5 +81,18 @@
     name: "ashmemd_test",
     defaults: ["ashmemd_defaults"],
     srcs: ["tests/ashmemd_test.cpp"],
+    shared_libs: [
+        "android.system.ashmem@1.0",
+        "ashmemd_aidl_interface-cpp",
+    ],
+    test_suites: ["device-tests"],
+}
+
+cc_test {
+    name: "ashmemd_hidl_client_test",
+    vendor: true,
+    defaults: ["ashmemd_defaults"],
+    srcs: ["tests/ashmemd_hidl_client_test.cpp"],
+    shared_libs: ["android.system.ashmem@1.0"],
     test_suites: ["device-tests"],
 }
diff --git a/ashmemd_hidl_client.cpp b/ashmemd_hidl_client.cpp
new file mode 100644
index 0000000..9b8219f
--- /dev/null
+++ b/ashmemd_hidl_client.cpp
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+
+#include <android-base/logging.h>
+#include <android/system/ashmem/1.0/IAshmem.h>
+
+using android::sp;
+using android::hardware::hidl_handle;
+using android::system::ashmem::V1_0::IAshmem;
+
+namespace android {
+namespace ashmemd {
+
+extern "C" int openAshmemdFd() {
+    static sp<IAshmem> ashmemService = IAshmem::getService();
+    if (!ashmemService) {
+        LOG(ERROR) << "Failed to get IAshmem service.";
+        return -1;
+    }
+    int fd = -1;
+    ashmemService->open([&fd](hidl_handle handle) {
+        if (handle == nullptr || handle->numFds < 1 || handle->data[0] < 0) {
+            LOG(ERROR) << "IAshmem.open() failed";
+        } else {
+            fd = dup(handle->data[0]);
+        }
+    });
+    return fd;
+}
+
+} // namespace ashmemd
+} // namespace android
diff --git a/tests/ashmemd_hidl_client_test.cpp b/tests/ashmemd_hidl_client_test.cpp
new file mode 100644
index 0000000..e5155ce
--- /dev/null
+++ b/tests/ashmemd_hidl_client_test.cpp
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+
+#include <dlfcn.h>
+#include <gtest/gtest.h>
+
+namespace android {
+namespace ashmemd {
+
+TEST(AshmemdHidlClientTest, OpenFd) {
+    const char *clientLib = "libashmemd_hidl_client.so";
+
+    void *handle = dlopen(clientLib, RTLD_NOW);
+    ASSERT_NE(handle, nullptr) << "Failed to dlopen() " << clientLib << ": " << dlerror();
+
+    auto function = (int (*)())dlsym(handle, "openAshmemdFd");
+    ASSERT_NE(function, nullptr) << "Failed to dlsym() openAshmemdFd() function: " << dlerror();
+
+    int fd = function();
+    ASSERT_GE(fd, 0) << "Failed to open /dev/ashmem";
+}
+
+}  // namespace ashmemd
+}  // namespace android