mediatek: vibrator: Refactor Vibrator HAL
* Clean up code
* Move functions not realted to the vibrator implementation itself
  into a seperate file called VibratorUtils.cpp
* Allow Vibrator effects to be compiled out with TARGET_VIBRATOR_SUPPORTS_EFFECTS.

Signed-off-by: bengris32 <bengris32@protonmail.ch>
Change-Id: I1451427970faf397da00fb8fb4b202e5954cc409
diff --git a/aidl/vibrator/Android.bp b/aidl/vibrator/Android.bp
deleted file mode 100644
index 34f8893..0000000
--- a/aidl/vibrator/Android.bp
+++ /dev/null
@@ -1,30 +0,0 @@
-cc_library_static {
-    name: "android.hardware.vibrator-impl.mediatek",
-    vendor: true,
-    shared_libs: [
-        "libbase",
-        "libbinder_ndk",
-        "android.hardware.vibrator-V2-ndk",
-    ],
-    export_include_dirs: ["include"],
-    srcs: [
-        "Vibrator.cpp",
-    ],
-}
-
-cc_binary {
-    name: "android.hardware.vibrator-service.mediatek",
-    relative_install_path: "hw",
-    init_rc: ["vibrator-mtk.rc"],
-    vintf_fragments: ["vibrator-mtk.xml"],
-    vendor: true,
-    shared_libs: [
-        "libbase",
-        "libbinder_ndk",
-        "android.hardware.vibrator-V2-ndk",
-    ],
-    static_libs: [
-        "android.hardware.vibrator-impl.mediatek",
-    ],
-    srcs: ["main.cpp"],
-}
diff --git a/aidl/vibrator/Android.mk b/aidl/vibrator/Android.mk
new file mode 100644
index 0000000..60a1579
--- /dev/null
+++ b/aidl/vibrator/Android.mk
@@ -0,0 +1,24 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := android.hardware.vibrator-service.mediatek
+LOCAL_VINTF_FRAGMENTS := android.hardware.vibrator.mediatek.xml
+LOCAL_INIT_RC := vibrator-mediatek.rc
+LOCAL_VENDOR_MODULE := true
+LOCAL_MODULE_RELATIVE_PATH := hw
+
+LOCAL_SRC_FILES := \
+    Vibrator.cpp \
+    VibratorUtils.cpp \
+    main.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+    libbase \
+    libbinder_ndk \
+    android.hardware.vibrator-V2-ndk
+
+ifeq ($(TARGET_VIBRATOR_SUPPORTS_EFFECTS),true)
+    LOCAL_CFLAGS += -DVIBRATOR_SUPPORTS_EFFECTS
+endif
+
+include $(BUILD_EXECUTABLE)
diff --git a/aidl/vibrator/Vibrator.cpp b/aidl/vibrator/Vibrator.cpp
index 3295042..0b0aac9 100644
--- a/aidl/vibrator/Vibrator.cpp
+++ b/aidl/vibrator/Vibrator.cpp
@@ -14,50 +14,56 @@
  * limitations under the License.
  */
 
-#include "vibrator-impl/Vibrator.h"
+#include "Vibrator.h"
 
 #include <android-base/logging.h>
 #include <thread>
-#include <fstream>
 
 namespace aidl {
 namespace android {
 namespace hardware {
 namespace vibrator {
 
+#ifdef VIBRATOR_SUPPORTS_EFFECTS
 Vibrator::Vibrator() {
-    if (nodeExists(kVibratorStrength)) {
+    if (exists(kVibratorStrength)) {
         mVibratorStrengthSupported = true;
         mVibratorStrengthMax = getNode(kVibratorStrengthMax, 9);
     }
 }
+#endif
 
 ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) {
-    LOG(INFO) << "Vibrator reporting capabilities";
-    *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK;
+    LOG(VERBOSE) << "Vibrator reporting capabilities";
+    *_aidl_return = IVibrator::CAP_ON_CALLBACK;
+
+    #ifdef VIBRATOR_SUPPORTS_EFFECTS
+    *_aidl_return |= IVibrator::CAP_PERFORM_CALLBACK;
+    #endif
 
     return ndk::ScopedAStatus::ok();
 }
 
 ndk::ScopedAStatus Vibrator::off() {
-    LOG(INFO) << "Vibrator off";
-    return activate(0);
+    LOG(VERBOSE) << "Vibrator off";
+    return setNode(kVibratorActivate, 0);
 }
 
 ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs,
                                 const std::shared_ptr<IVibratorCallback>& callback) {
-    LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs;
     ndk::ScopedAStatus status;
-    status = activate(timeoutMs);
 
+    LOG(VERBOSE) << "Vibrator on for timeoutMs: " << timeoutMs;
+
+    status = activate(timeoutMs);
     if (!status.isOk())
         return status;
 
     if (callback != nullptr) {
         std::thread([=] {
-            LOG(INFO) << "Starting on on another thread";
+            LOG(VERBOSE) << "Starting on on another thread";
             usleep(timeoutMs * 1000);
-            LOG(INFO) << "Notifying on complete";
+            LOG(VERBOSE) << "Notifying on complete";
             if (!callback->onComplete().isOk()) {
                 LOG(ERROR) << "Failed to call onComplete";
             }
@@ -66,22 +72,19 @@
     return ndk::ScopedAStatus::ok();
 }
 
+#ifdef VIBRATOR_SUPPORTS_EFFECTS
 ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength,
                                      const std::shared_ptr<IVibratorCallback>& callback,
                                      int32_t* _aidl_return) {
     ndk::ScopedAStatus status;
     int32_t timeoutMs;
 
-    LOG(INFO) << "Vibrator perform";
-
-    if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM &&
-        strength != EffectStrength::STRONG) {
-        return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
-    }
-
     if (vibEffects.find(effect) == vibEffects.end())
         return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
 
+    if (vibStrengths.find(strength) == vibStrengths.end())
+        return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
+
     if (mVibratorStrengthSupported) {
         // Vibration strength is relative to the max strength reported by the kernel.
         int vib_strength = static_cast<int>(mVibratorStrengthMax * vibStrengths[strength] / 10.0 + 0.5);
@@ -107,12 +110,21 @@
 
     *_aidl_return = timeoutMs;
     return ndk::ScopedAStatus::ok();
+#else
+ndk::ScopedAStatus Vibrator::perform(Effect /* effect */, EffectStrength /* strength */,
+                                     const std::shared_ptr<IVibratorCallback>& /* callback */,
+                                     int32_t* /* _aidl_return */) {
+    return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
+#endif
 }
 
+#ifdef VIBRATOR_SUPPORTS_EFFECTS
 ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
     for (auto const& pair : vibEffects)
         _aidl_return->push_back(pair.first);
-
+#else
+ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* /* _aidl_return */) {
+#endif
     return ndk::ScopedAStatus::ok();
 }
 
@@ -195,71 +207,6 @@
     return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
 }
 
-/*
- * Write value to path and close file.
- */
-ndk::ScopedAStatus Vibrator::setNode(const std::string path, const std::string value) {
-    std::ofstream file(path);
-
-    if (!file.is_open()) {
-        LOG(ERROR) << "Failed to write " << value.c_str() << " to " << path.c_str();
-        return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_SERVICE_SPECIFIC));
-    }
-
-    file << value << std::endl;
-
-    return ndk::ScopedAStatus::ok();
-}
-
-ndk::ScopedAStatus Vibrator::setNode(std::string path, const int32_t value) {
-    return setNode(path, std::to_string(value));
-}
-
-/*
- * Read integer from path
- */
-int Vibrator::getNode(const std::string path, const int fallback) {
-    std::ifstream file(path);
-    int value;
-
-    if (!file.is_open()) {
-        LOG(ERROR) << "failed to read from " << path.c_str();
-        return fallback;
-    }
-
-    file >> value;
-    return value;
-}
-
-bool Vibrator::nodeExists(const std::string path) {
-    std::ofstream file(path);
-    return file.is_open();
-}
-
-ndk::ScopedAStatus Vibrator::activate(const int32_t timeoutMs) {
-    ndk::ScopedAStatus status;
-    std::string active = "0";
-
-    if (timeoutMs > 0)
-        active = "1";
-    else
-        goto set_activate;
-
-    status = setNode(kVibratorState, "1");
-    if (!status.isOk())
-        return status;
-
-    status = setNode(kVibratorDuration, timeoutMs);
-    if (!status.isOk())
-        return status;
-
-set_activate:
-    status = setNode(kVibratorActivate, active);
-    if (!status.isOk())
-        return status;
-
-    return ndk::ScopedAStatus::ok();
-}
 }  // namespace vibrator
 }  // namespace hardware
 }  // namespace android
diff --git a/aidl/vibrator/include/vibrator-impl/Vibrator.h b/aidl/vibrator/Vibrator.h
similarity index 94%
rename from aidl/vibrator/include/vibrator-impl/Vibrator.h
rename to aidl/vibrator/Vibrator.h
index 62fb5ce..32b7e7f 100644
--- a/aidl/vibrator/include/vibrator-impl/Vibrator.h
+++ b/aidl/vibrator/Vibrator.h
@@ -17,7 +17,9 @@
 #pragma once
 
 #include <aidl/android/hardware/vibrator/BnVibrator.h>
+#ifdef VIBRATOR_SUPPORTS_EFFECTS
 #include <map>
+#endif
 
 namespace aidl {
 namespace android {
@@ -27,6 +29,8 @@
 const std::string kVibratorState       = "/sys/class/leds/vibrator/state";
 const std::string kVibratorDuration    = "/sys/class/leds/vibrator/duration";
 const std::string kVibratorActivate    = "/sys/class/leds/vibrator/activate";
+
+#ifdef VIBRATOR_SUPPORTS_EFFECTS
 const std::string kVibratorStrength    = "/sys/kernel/thunderquake_engine/level";
 const std::string kVibratorStrengthMax = "/sys/kernel/thunderquake_engine/max";
 
@@ -41,10 +45,13 @@
     { EffectStrength::MEDIUM, 6},
     { EffectStrength::STRONG, 10}
 };
+#endif
 
 class Vibrator : public BnVibrator {
 public:
+#ifdef VIBRATOR_SUPPORTS_EFFECTS
     Vibrator();
+#endif
     ndk::ScopedAStatus getCapabilities(int32_t* _aidl_return) override;
     ndk::ScopedAStatus off() override;
     ndk::ScopedAStatus on(int32_t timeoutMs,
@@ -75,16 +82,15 @@
     ndk::ScopedAStatus getSupportedBraking(std::vector<Braking>* supported) override;
     ndk::ScopedAStatus composePwle(const std::vector<PrimitivePwle> &composite,
                                    const std::shared_ptr<IVibratorCallback> &callback) override;
-
 private:
-    static ndk::ScopedAStatus setNode(const std::string path, const std::string value);
     static ndk::ScopedAStatus setNode(const std::string path, const int32_t value);
+#ifdef VIBRATOR_SUPPORTS_EFFECTS
+    static bool exists(const std::string path);
     static int getNode(const std::string path, const int fallback);
-    static bool nodeExists(const std::string path);
-
-    ndk::ScopedAStatus activate(const int32_t timeoutMs);
     bool mVibratorStrengthSupported;
     int mVibratorStrengthMax;
+#endif
+    ndk::ScopedAStatus activate(const int32_t timeoutMs);
 };
 
 }  // namespace vibrator
diff --git a/aidl/vibrator/VibratorUtils.cpp b/aidl/vibrator/VibratorUtils.cpp
new file mode 100644
index 0000000..db7bc0a
--- /dev/null
+++ b/aidl/vibrator/VibratorUtils.cpp
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2022 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 "Vibrator.h"
+
+#include <android-base/logging.h>
+#include <fstream>
+
+namespace aidl {
+namespace android {
+namespace hardware {
+namespace vibrator {
+
+ndk::ScopedAStatus Vibrator::setNode(const std::string path, const int32_t value) {
+    std::ofstream file(path);
+
+    if (!file.is_open()) {
+        LOG(ERROR) << "Failed to write " << value << " to " << path;
+        return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_SERVICE_SPECIFIC));
+    }
+
+    file << value << std::endl;
+
+    return ndk::ScopedAStatus::ok();
+}
+
+ndk::ScopedAStatus Vibrator::activate(const int32_t timeoutMs) {
+    ndk::ScopedAStatus status;
+
+    /* timeoutMs under 1 = turn off vibrator */
+    if (timeoutMs < 1) {
+        return off();
+    }
+
+    status = setNode(kVibratorState, 1);
+    if (!status.isOk())
+        return status;
+
+    status = setNode(kVibratorDuration, timeoutMs);
+    if (!status.isOk())
+        return status;
+
+    status = setNode(kVibratorActivate, 1);
+    if (!status.isOk())
+        return status;
+
+    return ndk::ScopedAStatus::ok();
+}
+
+#ifdef VIBRATOR_SUPPORTS_EFFECTS
+bool Vibrator::exists(const std::string path) {
+    std::ofstream file(path);
+    return file.is_open();
+}
+
+int Vibrator::getNode(const std::string path, const int fallback) {
+    std::ifstream file(path);
+    int value;
+
+    if (!file.is_open()) {
+        LOG(ERROR) << "failed to read from " << path.c_str();
+        return fallback;
+    }
+
+    file >> value;
+    return value;
+}
+#endif
+
+}  // namespace vibrator
+}  // namespace hardware
+}  // namespace android
+}  // namespace aidl
diff --git a/aidl/vibrator/vibrator-mtk.xml b/aidl/vibrator/android.hardware.vibrator.mediatek.xml
similarity index 100%
rename from aidl/vibrator/vibrator-mtk.xml
rename to aidl/vibrator/android.hardware.vibrator.mediatek.xml
diff --git a/aidl/vibrator/main.cpp b/aidl/vibrator/main.cpp
index 6482d05..9bd5880 100644
--- a/aidl/vibrator/main.cpp
+++ b/aidl/vibrator/main.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "vibrator-impl/Vibrator.h"
+#include "Vibrator.h"
 
 #include <android-base/logging.h>
 #include <android/binder_manager.h>
@@ -29,7 +29,7 @@
     auto vib = ndk::SharedRefBase::make<Vibrator>();
     const std::string vibName = std::string() + Vibrator::descriptor + "/default";
     binder_status_t status = AServiceManager_addService(vib->asBinder().get(), vibName.c_str());
-    CHECK(status == STATUS_OK);
+    CHECK_EQ(status, STATUS_OK);
 
     ABinderProcess_joinThreadPool();
     return EXIT_FAILURE;  // should not reach
diff --git a/aidl/vibrator/vibrator-mediatek.rc b/aidl/vibrator/vibrator-mediatek.rc
new file mode 100644
index 0000000..f895bca
--- /dev/null
+++ b/aidl/vibrator/vibrator-mediatek.rc
@@ -0,0 +1,4 @@
+service vendor.vibrator-mediatek /vendor/bin/hw/android.hardware.vibrator-service.mediatek
+    class hal
+    user system
+    group system
diff --git a/aidl/vibrator/vibrator-mtk.rc b/aidl/vibrator/vibrator-mtk.rc
deleted file mode 100644
index 71b3d21..0000000
--- a/aidl/vibrator/vibrator-mtk.rc
+++ /dev/null
@@ -1,8 +0,0 @@
-on init
-    chown system system /sys/kernel/thunderquake_engine/level
-    chown 644 /sys/kernel/thunderquake_engine/level
-
-service vendor.vibrator-mtk /vendor/bin/hw/android.hardware.vibrator-service.mediatek
-    class hal
-    user system
-    group system