OSDN Git Service

Use IVibrator hidl service in dumpstate.
authorSteven Moreland <smoreland@google.com>
Tue, 29 Nov 2016 21:20:37 +0000 (13:20 -0800)
committerSteven Moreland <smoreland@google.com>
Tue, 13 Dec 2016 17:25:01 +0000 (09:25 -0800)
Bug: 33067126
Test: Dumpstate vibrator works. dumpstate_test passes.
Change-Id: I9a87755f695829482d072e538e4938f35f393ae4

cmds/dumpstate/Android.mk
cmds/dumpstate/dumpstate.cpp
cmds/dumpstate/dumpstate.h
cmds/dumpstate/utils.cpp

index 17b411f..897cfb1 100644 (file)
@@ -102,7 +102,8 @@ LOCAL_SRC_FILES := $(COMMON_SRC_FILES) \
 
 LOCAL_MODULE := dumpstate
 
-LOCAL_SHARED_LIBRARIES := $(COMMON_SHARED_LIBRARIES)
+LOCAL_SHARED_LIBRARIES := $(COMMON_SHARED_LIBRARIES) \
+    android.hardware.vibrator@1.0
 
 LOCAL_STATIC_LIBRARIES := $(COMMON_STATIC_LIBRARIES)
 
index a0b0426..1e33439 100644 (file)
@@ -43,6 +43,7 @@
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
 #include <android/hardware/dumpstate/1.0/IDumpstateDevice.h>
+#include <android/hardware/vibrator/1.0/IVibrator.h>
 #include <cutils/native_handle.h>
 #include <cutils/properties.h>
 #include <hardware_legacy/power.h>
 #include "DumpstateService.h"
 #include "dumpstate.h"
 
+using ::android::hardware::dumpstate::V1_0::IDumpstateDevice;
+using ::android::hardware::vibrator::V1_0::IVibrator;
+using VibratorStatus = ::android::hardware::vibrator::V1_0::Status;
+
 /* read before root is shed */
 static char cmdline_buf[16384] = "(unknown)";
 static const char *dump_traces_path = NULL;
@@ -1164,8 +1169,8 @@ void Dumpstate::DumpstateBoard() {
     printf("== Board\n");
     printf("========================================================\n");
 
-    android::sp<android::hardware::dumpstate::V1_0::IDumpstateDevice> dumpstate_device(
-        android::hardware::dumpstate::V1_0::IDumpstateDevice::getService("DumpstateDevice"));
+    ::android::sp<IDumpstateDevice> dumpstate_device(
+        IDumpstateDevice::getService("DumpstateDevice"));
     if (dumpstate_device == nullptr) {
         // TODO: temporary workaround until devices on master implement it
         MYLOGE("no IDumpstateDevice implementation; using legacy dumpstate_board()\n");
@@ -1586,12 +1591,21 @@ int main(int argc, char *argv[]) {
         fclose(cmdline);
     }
 
-    /* open the vibrator before dropping root */
-    std::unique_ptr<FILE, int(*)(FILE*)> vibrator(NULL, fclose);
+    ::android::sp<IVibrator> vibrator = nullptr;
     if (do_vibrate) {
-        vibrator.reset(fopen("/sys/class/timed_output/vibrator/enable", "we"));
-        if (vibrator) {
-            vibrate(vibrator.get(), 150);
+        vibrator = IVibrator::getService("vibrator");
+
+        if (vibrator != nullptr) {
+            // cancel previous vibration if any
+            ::android::hardware::Return<VibratorStatus> offStatus = vibrator->off();
+            if (!offStatus.isOk() || offStatus != VibratorStatus::OK) {
+                MYLOGE("Vibrator off failed.");
+            } else {
+                ::android::hardware::Return<VibratorStatus> onStatus = vibrator->on(150);
+                if (!onStatus.isOk() || onStatus != VibratorStatus::OK) {
+                    MYLOGE("Vibrator on failed.");
+                }
+            }
         }
     }
 
@@ -1759,10 +1773,20 @@ int main(int argc, char *argv[]) {
     }
 
     /* vibrate a few but shortly times to let user know it's finished */
-    if (vibrator) {
-        for (int i = 0; i < 3; i++) {
-            vibrate(vibrator.get(), 75);
-            usleep((75 + 50) * 1000);
+    if (vibrator != nullptr) {
+        // in case dumpstate magically completes before the above vibration
+        ::android::hardware::Return<VibratorStatus> offStatus = vibrator->off();
+        if (!offStatus.isOk() || offStatus != VibratorStatus::OK) {
+            MYLOGE("Vibrator off failed.");
+        } else {
+            for (int i = 0; i < 3; i++) {
+                ::android::hardware::Return<VibratorStatus> onStatus = vibrator->on(75);
+                if (!onStatus.isOk() || onStatus != VibratorStatus::OK) {
+                    MYLOGE("Vibrator on failed.");
+                    break;
+                }
+                usleep((75 + 50) * 1000);
+            }
         }
     }
 
index fcf0683..2c11c0c 100644 (file)
@@ -402,9 +402,6 @@ void play_sound(const char *path);
 /* Implemented by libdumpstate_board to dump board-specific info */
 void dumpstate_board();
 
-/* Vibrates for a given durating (in milliseconds). */
-void vibrate(FILE* vibrator, int ms);
-
 /* Checks if a given path is a directory. */
 bool is_dir(const char* pathname);
 
index 5ee00c8..ee88705 100644 (file)
@@ -1061,11 +1061,6 @@ void Dumpstate::TakeScreenshot(const std::string& path) {
     }
 }
 
-void vibrate(FILE* vibrator, int ms) {
-    fprintf(vibrator, "%d\n", ms);
-    fflush(vibrator);
-}
-
 bool is_dir(const char* pathname) {
     struct stat info;
     if (stat(pathname, &info) == -1) {