Merge "Dumpstate: convert to hidl implementation."
diff --git a/BoardConfig.mk b/BoardConfig.mk
index dd47842..8c988a2 100644
--- a/BoardConfig.mk
+++ b/BoardConfig.mk
@@ -49,9 +49,6 @@
 BOARD_KERNEL_CMDLINE += snd_pcm.maximum_substreams=8
 BOARD_KERNEL_CMDLINE += intel_soc_pmu.enable_s3=0
 
-# Custom dumpstate library to add board specific stuff to bugreport
-BOARD_HAL_STATIC_LIBRARIES := libdumpstate.fugu
-
 # Binder API version
 TARGET_USES_64_BIT_BINDER := true
 
diff --git a/device.mk b/device.mk
index c1f49e5..4d8dbb3 100644
--- a/device.mk
+++ b/device.mk
@@ -83,6 +83,10 @@
     audio.r_submix.default \
     libaudio-resampler
 
+# Dumpstate HAL
+PRODUCT_PACKAGES += \
+    android.hardware.dumpstate@1.0-service.fugu
+
 USE_CUSTOM_AUDIO_POLICY := 1
 
 # specific management of audio_policy.conf
diff --git a/dumpstate/Android.mk b/dumpstate/Android.mk
index 14228fc..0cfb2d9 100644
--- a/dumpstate/Android.mk
+++ b/dumpstate/Android.mk
@@ -1,4 +1,4 @@
-# Copyright (C) 2014 The Android Open Source Project
+# Copyright (C) 2016 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.
@@ -12,15 +12,28 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-LOCAL_PATH:= $(call my-dir)
+LOCAL_PATH := $(call my-dir)
+
 include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.dumpstate@1.0-service.fugu
+LOCAL_INIT_RC := android.hardware.dumpstate@1.0-service.fugu.rc
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_SRC_FILES := \
+    DumpstateDevice.cpp \
+    service.cpp
 
-LOCAL_STATIC_LIBRARIES := libdumpstateheaders
-
-LOCAL_SRC_FILES := dumpstate.cpp
-
-LOCAL_MODULE := libdumpstate.fugu
+LOCAL_SHARED_LIBRARIES := \
+    android.hardware.dumpstate@1.0 \
+    libbase \
+    libcutils \
+    libdumpstateutil \
+    libhidlbase \
+    libhidltransport \
+    libhwbinder \
+    liblog \
+    libutils
 
 LOCAL_MODULE_TAGS := optional
+LOCAL_PROPRIETARY_MODULE := true
 
-include $(BUILD_STATIC_LIBRARY)
+include $(BUILD_EXECUTABLE)
diff --git a/dumpstate/DumpstateDevice.cpp b/dumpstate/DumpstateDevice.cpp
new file mode 100644
index 0000000..571d368
--- /dev/null
+++ b/dumpstate/DumpstateDevice.cpp
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2016 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 "dumpstate"
+
+#include "DumpstateDevice.h"
+
+#include "DumpstateUtil.h"
+
+#include <errno.h>
+#include <log/log.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+static const char base64[] =
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+static const char pad64 = '=';
+
+using android::os::dumpstate::CommandOptions;
+using android::os::dumpstate::DumpFileToFd;
+using android::os::dumpstate::RunCommandToFd;
+
+namespace android {
+namespace hardware {
+namespace dumpstate {
+namespace V1_0 {
+namespace implementation {
+
+static void base64_output3(int out_fd, const unsigned char *src, int len)
+{
+    dprintf(out_fd, "%c", base64[src[0] >> 2]);
+    dprintf(out_fd, "%c", base64[((src[0] & 0x03) << 4) | (src[1] >> 4)]);
+    if (len == 1) {
+        dprintf(out_fd, "==");
+        return;
+    }
+    dprintf(out_fd, "%c", base64[((src[1] & 0x0F) << 2) | (src[2] >> 6)]);
+    if (len == 2) {
+        dprintf(out_fd, "=");
+        return;
+    }
+    dprintf(out_fd, "%c", base64[src[2] & 0x3F]);
+}
+
+static void fugu_dump_base64(int out_fd, const char *path)
+{
+
+    dprintf(out_fd, "------ (%s) ------\n", path);
+    int fd = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
+    if (fd < 0) {
+        dprintf(out_fd, "*** %s: %s\n\n", path, strerror(errno));
+        return;
+    }
+
+    /* buffer size multiple of 3 for ease of use */
+    unsigned char buffer[1200];
+    int left = 0;
+    int count = 0;
+    for (;;) {
+        int ret = read(fd, &buffer[left], sizeof(buffer) - left);
+        if (ret <= 0) {
+            break;
+        }
+        left += ret;
+        int ofs = 0;
+        while (left > 2) {
+            base64_output3(out_fd, &buffer[ofs], 3);
+            left -= 3;
+            ofs += 3;
+            count += 4;
+            if (count > 72) {
+                dprintf(out_fd, "\n");
+                count = 0;
+            }
+        }
+        if (left) {
+            memmove(buffer, &buffer[ofs], left);
+        }
+    }
+    close(fd);
+
+    if (!left) {
+        dprintf(out_fd, "\n------ end ------\n");
+        return;
+    }
+
+    /* finish padding */
+    count = left;
+    while (count < 3) {
+        buffer[count++] = 0;
+    }
+    base64_output3(out_fd, buffer, left);
+
+    dprintf(out_fd, "\n------ end ------\n");
+}
+
+// Methods from ::android::hardware::dumpstate::V1_0::IDumpstateDevice follow.
+Return<void> DumpstateDevice::dumpstateBoard(const hidl_handle& handle) {
+    if (handle->numFds < 1) {
+        ALOGE("no FDs\n");
+        return Void();
+    }
+
+    int fd = handle->data[0];
+    if (fd < 0) {
+        ALOGE("invalid FD: %d\n", handle->data[0]);
+        return Void();
+    }
+
+    DumpFileToFd(fd, "INTERRUPTS", "/proc/interrupts");
+    DumpFileToFd(fd, "last ipanic_console", "/data/dontpanic/ipanic_console");
+    DumpFileToFd(fd, "last ipanic_threads", "/data/dontpanic/ipanic_threads");
+
+    fugu_dump_base64(fd, "/dev/snd_atvr_mSBC");
+    fugu_dump_base64(fd, "/dev/snd_atvr_pcm");
+
+    return Void();
+}
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace dumpstate
+}  // namespace hardware
+}  // namespace android
diff --git a/dumpstate/DumpstateDevice.h b/dumpstate/DumpstateDevice.h
new file mode 100644
index 0000000..f8585f5
--- /dev/null
+++ b/dumpstate/DumpstateDevice.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2016 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 ANDROID_HARDWARE_DUMPSTATE_V1_0_DUMPSTATEDEVICE_H
+#define ANDROID_HARDWARE_DUMPSTATE_V1_0_DUMPSTATEDEVICE_H
+
+#include <android/hardware/dumpstate/1.0/IDumpstateDevice.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+
+namespace android {
+namespace hardware {
+namespace dumpstate {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
+using ::android::hardware::hidl_array;
+using ::android::hardware::hidl_handle;
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::sp;
+
+struct DumpstateDevice : public IDumpstateDevice {
+    // Methods from ::android::hardware::dumpstate::V1_0::IDumpstateDevice follow.
+    Return<void> dumpstateBoard(const hidl_handle& h) override;
+
+};
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace dumpstate
+}  // namespace hardware
+}  // namespace android
+
+#endif  // ANDROID_HARDWARE_DUMPSTATE_V1_0_DUMPSTATEDEVICE_H
diff --git a/dumpstate/NOTICE b/dumpstate/NOTICE
index e2ca067..e48dd6b 100644
--- a/dumpstate/NOTICE
+++ b/dumpstate/NOTICE
@@ -1,5 +1,5 @@
 
-   Copyright (C) 2014 The Android Open Source Project
+   Copyright (C) 2016 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.
diff --git a/dumpstate/android.hardware.dumpstate@1.0-service.fugu.rc b/dumpstate/android.hardware.dumpstate@1.0-service.fugu.rc
new file mode 100644
index 0000000..49cb711
--- /dev/null
+++ b/dumpstate/android.hardware.dumpstate@1.0-service.fugu.rc
@@ -0,0 +1,4 @@
+service dumpstate-1-0 /vendor/bin/hw/android.hardware.dumpstate@1.0-service.guppy
+    class hal
+    user system
+    group system
diff --git a/dumpstate/dumpstate.cpp b/dumpstate/dumpstate.cpp
deleted file mode 100644
index 112ec0a..0000000
--- a/dumpstate/dumpstate.cpp
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Copyright 2014 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 <dumpstate.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-static const char base64[] =
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-static const char pad64 = '=';
-
-static void base64_output3(const unsigned char *src, int len)
-{
-    printf("%c", base64[src[0] >> 2]);
-    printf("%c", base64[((src[0] & 0x03) << 4) | (src[1] >> 4)]);
-    if (len == 1) {
-        printf("==");
-        return;
-    }
-    printf("%c", base64[((src[1] & 0x0F) << 2) | (src[2] >> 6)]);
-    if (len == 2) {
-        printf("=");
-        return;
-    }
-    printf("%c", base64[src[2] & 0x3F]);
-}
-
-static void fugu_dump_base64(const char *path)
-{
-
-    printf("------ (%s) ------\n", path);
-    int fd = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
-    if (fd < 0) {
-        printf("*** %s: %s\n\n", path, strerror(errno));
-        return;
-    }
-
-    /* buffer size multiple of 3 for ease of use */
-    unsigned char buffer[1200];
-    int left = 0;
-    int count = 0;
-    for (;;) {
-        int ret = read(fd, &buffer[left], sizeof(buffer) - left);
-        if (ret <= 0) {
-            break;
-        }
-        left += ret;
-        int ofs = 0;
-        while (left > 2) {
-            base64_output3(&buffer[ofs], 3);
-            left -= 3;
-            ofs += 3;
-            count += 4;
-            if (count > 72) {
-                printf("\n");
-                count = 0;
-            }
-        }
-        if (left) {
-            memmove(buffer, &buffer[ofs], left);
-        }
-    }
-    close(fd);
-
-    if (!left) {
-        printf("\n------ end ------\n");
-        return;
-    }
-
-    /* finish padding */
-    count = left;
-    while (count < 3) {
-        buffer[count++] = 0;
-    }
-    base64_output3(buffer, left);
-
-    printf("\n------ end ------\n");
-}
-
-void dumpstate_board()
-{
-    Dumpstate& ds = Dumpstate::GetInstance();
-
-    ds.DumpFile("INTERRUPTS", "/proc/interrupts");
-    ds.DumpFile("last ipanic_console", "/data/dontpanic/ipanic_console");
-    ds.DumpFile("last ipanic_threads", "/data/dontpanic/ipanic_threads");
-    fugu_dump_base64("/dev/snd_atvr_mSBC");
-    fugu_dump_base64("/dev/snd_atvr_pcm");
-};
diff --git a/dumpstate/service.cpp b/dumpstate/service.cpp
new file mode 100644
index 0000000..eaaa5ab
--- /dev/null
+++ b/dumpstate/service.cpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2016 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 "android.hardware.dumpstate@1.0-service.fugu"
+
+#include <hidl/HidlSupport.h>
+#include <hidl/HidlTransportSupport.h>
+
+#include "DumpstateDevice.h"
+
+using ::android::hardware::configureRpcThreadpool;
+using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
+using ::android::hardware::dumpstate::V1_0::implementation::DumpstateDevice;
+using ::android::hardware::joinRpcThreadpool;
+using ::android::sp;
+
+
+int main(int /* argc */, char* /* argv */ []) {
+  sp<IDumpstateDevice> dumpstate = new DumpstateDevice;
+  configureRpcThreadpool(1, true);
+  dumpstate->registerAsService("dumpstate");
+  joinRpcThreadpool();
+}
diff --git a/sepolicy/dumpstate.te b/sepolicy/dumpstate.te
index a3c754f..36aaa9c 100644
--- a/sepolicy/dumpstate.te
+++ b/sepolicy/dumpstate.te
@@ -1,6 +1,2 @@
-userdebug_or_eng(`
-        allow dumpstate atvr_device:chr_file r_file_perms;
-')
-
 # GPU dynamic fbc feature
 allow dumpstate surfaceflinger:fifo_file rw_file_perms;
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index 8f9ef45..9cceec5 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -9,6 +9,8 @@
 
 /system/bin/BtFwLoader       u:object_r:btfwloader_exec:s0
 
+/vendor/bin/hw/android\.hardware\.dumpstate@1\.0-service.fugu      u:object_r:hal_dumpstate_impl_exec:s0
+
 /sys/devices/pci0000:00/0000:00:01\.3/mmc_host/mmc2/mmc2:0001/mmc2:0001:2/ieee80211/phy[0-9]+/rfkill[0-9]+/type  u:object_r:sysfs_bluetooth:s0
 /sys/devices/pci0000:00/0000:00:01\.3/mmc_host/mmc2/mmc2:0001/mmc2:0001:2/rfkill/rfkill[0-9]+/type  u:object_r:sysfs_bluetooth:s0
 /sys/devices/platform/bcm_bt_lpm.0/rfkill/rfkill[0-9]/state     u:object_r:sysfs_bluetooth_writable:s0
diff --git a/sepolicy/hal_dumpstate_impl.te b/sepolicy/hal_dumpstate_impl.te
new file mode 100644
index 0000000..1e34f09
--- /dev/null
+++ b/sepolicy/hal_dumpstate_impl.te
@@ -0,0 +1,8 @@
+type hal_dumpstate_impl, domain, hal_dumpstate;
+type hal_dumpstate_impl_exec, exec_type, file_type;
+
+init_daemon_domain(hal_dumpstate_impl)
+
+userdebug_or_eng(`
+        allow hal_dumpstate_impl atvr_device:chr_file r_file_perms;
+')
\ No newline at end of file