Add default/android.hardware.dumpstate@1.1-service.example

Bug: 152067221
Test: VtsHalDumpstateV1_1TargetTest
Signed-off-by: Roman Kiryanov <rkir@google.com>
Change-Id: I7f4e50846e2dc4393563c602145afb5617b2d48f
diff --git a/dumpstate/1.1/default/Android.bp b/dumpstate/1.1/default/Android.bp
new file mode 100644
index 0000000..bd92075
--- /dev/null
+++ b/dumpstate/1.1/default/Android.bp
@@ -0,0 +1,26 @@
+cc_binary {
+    name: "android.hardware.dumpstate@1.1-service.example",
+    vendor: true,
+    relative_install_path: "hw",
+    defaults: ["hidl_defaults"],
+    init_rc: [
+        "android.hardware.dumpstate@1.1-service.example.rc",
+    ],
+    vintf_fragments: [
+        "android.hardware.dumpstate@1.1-service.example.xml",
+    ],
+    srcs: ["main.cpp"],
+    shared_libs: [
+        "android.hardware.dumpstate@1.0",
+        "android.hardware.dumpstate@1.1",
+        "libbase",
+        "libcutils",
+        "libdumpstateutil",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+    ],
+    cflags: [
+        "-DLOG_TAG=\"android.hardware.dumpstate@1.1-service.example\"",
+    ],
+}
diff --git a/dumpstate/1.1/default/android.hardware.dumpstate@1.1-service.example.rc b/dumpstate/1.1/default/android.hardware.dumpstate@1.1-service.example.rc
new file mode 100644
index 0000000..3a1e06a
--- /dev/null
+++ b/dumpstate/1.1/default/android.hardware.dumpstate@1.1-service.example.rc
@@ -0,0 +1,8 @@
+service dumpstate-1-1 /vendor/bin/hw/android.hardware.dumpstate@1.1-service.example
+    class hal
+    user nobody
+    group nobody
+    interface android.hardware.dumpstate@1.0::IDumpstateDevice default
+    interface android.hardware.dumpstate@1.1::IDumpstateDevice default
+    oneshot
+    disabled
diff --git a/dumpstate/1.1/default/android.hardware.dumpstate@1.1-service.example.xml b/dumpstate/1.1/default/android.hardware.dumpstate@1.1-service.example.xml
new file mode 100644
index 0000000..775e8c8
--- /dev/null
+++ b/dumpstate/1.1/default/android.hardware.dumpstate@1.1-service.example.xml
@@ -0,0 +1,11 @@
+<manifest version="1.0" type="device">
+    <hal format="hidl">
+        <name>android.hardware.dumpstate</name>
+        <transport>hwbinder</transport>
+        <version>1.1</version>
+        <interface>
+            <name>IDumpstateDevice</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+</manifest>
diff --git a/dumpstate/1.1/default/main.cpp b/dumpstate/1.1/default/main.cpp
new file mode 100644
index 0000000..4510f63
--- /dev/null
+++ b/dumpstate/1.1/default/main.cpp
@@ -0,0 +1,129 @@
+/*
+ * 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.
+ */
+
+#include <android-base/properties.h>
+#include <android/hardware/dumpstate/1.1/IDumpstateDevice.h>
+#include <android/hardware/dumpstate/1.1/types.h>
+#include <hidl/HidlLazyUtils.h>
+#include <hidl/HidlSupport.h>
+#include <hidl/HidlTransportSupport.h>
+#include <log/log.h>
+
+#include "DumpstateUtil.h"
+
+namespace {
+using ::android::hardware::hidl_handle;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+
+using ::android::hardware::dumpstate::V1_1::DumpstateMode;
+using ::android::hardware::dumpstate::V1_1::DumpstateStatus;
+using ::android::hardware::dumpstate::V1_1::IDumpstateDevice;
+
+using ::android::os::dumpstate::DumpFileToFd;
+
+const char kVerboseLoggingProperty[] = "persist.dumpstate.verbose_logging.enabled";
+
+struct DumpstateDevice : public IDumpstateDevice {
+    // 1.1
+    Return<DumpstateStatus> dumpstateBoard_1_1(const hidl_handle& handle, const DumpstateMode mode,
+                                               uint64_t /*timeoutMillis*/) override {
+        if (handle == nullptr || handle->numFds < 1) {
+            ALOGE("no FDs\n");
+            return DumpstateStatus::ILLEGAL_ARGUMENT;
+        }
+
+        int fd = handle->data[0];
+        if (fd < 0) {
+            ALOGE("invalid FD: %d\n", fd);
+            return DumpstateStatus::ILLEGAL_ARGUMENT;
+        }
+
+        switch (mode) {
+            case DumpstateMode::FULL:
+                return dumpstateBoardImpl(fd, true);
+
+            case DumpstateMode::DEFAULT:
+                return dumpstateBoardImpl(fd, false);
+
+            case DumpstateMode::INTERACTIVE:
+            case DumpstateMode::REMOTE:
+            case DumpstateMode::WEAR:
+            case DumpstateMode::CONNECTIVITY:
+            case DumpstateMode::WIFI:
+            case DumpstateMode::PROTO:
+                ALOGE("The requested mode is not supported: %s\n", toString(mode).c_str());
+                return DumpstateStatus::UNSUPPORTED_MODE;
+
+            default:
+                ALOGE("The requested mode is invalid: %s\n", toString(mode).c_str());
+                return DumpstateStatus::ILLEGAL_ARGUMENT;
+        }
+    }
+
+    Return<void> setVerboseLoggingEnabled(bool enable) override {
+        ::android::base::SetProperty(kVerboseLoggingProperty, enable ? "true" : "false");
+        return Void();
+    }
+
+    Return<bool> getVerboseLoggingEnabled() override { return getVerboseLoggingEnabledImpl(); }
+
+    // 1.0
+    Return<void> dumpstateBoard(const hidl_handle& h) override {
+        dumpstateBoard_1_1(h, DumpstateMode::DEFAULT, 0);
+        return Void();
+    }
+
+    DumpstateStatus dumpstateBoardImpl(const int fd, const bool full) {
+        ALOGD("DumpstateDevice::dumpstateBoard() FD: %d\n", fd);
+        ALOGI("Dumpstate HIDL not provided by device\n");
+
+        dprintf(fd, "verbose logging: %s\n",
+                getVerboseLoggingEnabledImpl() ? "enabled" : "disabled");
+
+        dprintf(fd, "[%s] %s\n", (full ? "full" : "default"), "Hello, world!");
+
+        // Shows an example on how to use the libdumpstateutil API.
+        DumpFileToFd(fd, "cmdline", "/proc/self/cmdline");
+
+        return DumpstateStatus::OK;
+    }
+
+    static bool getVerboseLoggingEnabledImpl() {
+        return ::android::base::GetBoolProperty(kVerboseLoggingProperty, false);
+    }
+};
+}  // namespace
+
+int main(int, char**) {
+    using ::android::sp;
+    using ::android::hardware::configureRpcThreadpool;
+    using ::android::hardware::joinRpcThreadpool;
+    using ::android::hardware::LazyServiceRegistrar;
+
+    configureRpcThreadpool(1, true);
+
+    auto dumpstate = sp<DumpstateDevice>::make();
+    auto serviceRegistrar = LazyServiceRegistrar::getInstance();
+
+    if (serviceRegistrar.registerService(dumpstate) != ::android::OK) {
+        ALOGE("Could not register service.");
+        return 1;
+    }
+
+    joinRpcThreadpool();
+    return 0;
+}