Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 872d84e3 authored by Lais Andrade's avatar Lais Andrade Committed by Android (Google) Code Review
Browse files

Merge "Remove IVibrator HIDL dependencies from idlcli" into main

parents 87feddcf 3efb014d
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -25,13 +25,8 @@ cc_defaults {
    name: "idlcli-defaults",
    shared_libs: [
        "android.hardware.vibrator-V3-ndk",
        "android.hardware.vibrator@1.0",
        "android.hardware.vibrator@1.1",
        "android.hardware.vibrator@1.2",
        "android.hardware.vibrator@1.3",
        "libbase",
        "libbinder_ndk",
        "libhidlbase",
        "liblog",
        "libutils",
    ],
+0 −1
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@
#define FRAMEWORK_NATIVE_CMDS_IDLCLI_UTILS_H_

#include <android/binder_enums.h>
#include <hidl/HidlSupport.h>

#include <iomanip>
#include <iostream>
+6 −78
Original line number Diff line number Diff line
@@ -22,102 +22,30 @@
#include <aidl/android/hardware/vibrator/IVibratorManager.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include <android/hardware/vibrator/1.3/IVibrator.h>

#include "IdlCli.h"
#include "utils.h"

namespace android {

using hardware::Return;
using ::aidl::android::hardware::vibrator::IVibrator;
using idlcli::IdlCli;

static constexpr int NUM_TRIES = 2;

// Creates a Return<R> with STATUS::EX_NULL_POINTER.
template <class R>
inline R NullptrStatus() {
    using ::android::hardware::Status;
    return Status::fromExceptionCode(Status::EX_NULL_POINTER);
}

template <>
inline ndk::ScopedAStatus NullptrStatus() {
    return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_NULL_POINTER));
}

template <typename I>
inline auto getService(std::string name) {
    const auto instance = std::string() + I::descriptor + "/" + name;
    const auto instance = std::string() + IVibrator::descriptor + "/" + name;
    auto vibBinder = ndk::SpAIBinder(AServiceManager_checkService(instance.c_str()));
    return I::fromBinder(vibBinder);
}

template <>
inline auto getService<android::hardware::vibrator::V1_0::IVibrator>(std::string name) {
    return android::hardware::vibrator::V1_0::IVibrator::getService(name);
}

template <>
inline auto getService<android::hardware::vibrator::V1_1::IVibrator>(std::string name) {
    return android::hardware::vibrator::V1_1::IVibrator::getService(name);
}

template <>
inline auto getService<android::hardware::vibrator::V1_2::IVibrator>(std::string name) {
    return android::hardware::vibrator::V1_2::IVibrator::getService(name);
}

template <>
inline auto getService<android::hardware::vibrator::V1_3::IVibrator>(std::string name) {
    return android::hardware::vibrator::V1_3::IVibrator::getService(name);
}

template <typename I>
using shared_ptr = std::invoke_result_t<decltype(getService<I>)&, std::string>;

template <typename I>
class HalWrapper {
public:
    static std::unique_ptr<HalWrapper> Create() {
        // Assume that if getService returns a nullptr, HAL is not available on the
        // device.
        const auto name = IdlCli::Get().getName();
        auto hal = getService<I>(name.empty() ? "default" : name);
        return hal ? std::unique_ptr<HalWrapper>(new HalWrapper(std::move(hal))) : nullptr;
    return IVibrator::fromBinder(vibBinder);
}

    template <class R, class... Args0, class... Args1>
    R call(R (I::*fn)(Args0...), Args1&&... args1) {
        return (*mHal.*fn)(std::forward<Args1>(args1)...);
    }

private:
    HalWrapper(shared_ptr<I>&& hal) : mHal(std::move(hal)) {}

private:
    shared_ptr<I> mHal;
};

template <typename I>
static auto getHal() {
    static auto sHalWrapper = HalWrapper<I>::Create();
    return sHalWrapper.get();
}

template <class R, class I, class... Args0, class... Args1>
R halCall(R (I::*fn)(Args0...), Args1&&... args1) {
    auto hal = getHal<I>();
    return hal ? hal->call(fn, std::forward<Args1>(args1)...) : NullptrStatus<R>();
    // Assume that if getService returns a nullptr, HAL is not available on the device.
    const auto name = IdlCli::Get().getName();
    return getService(name.empty() ? "default" : name);
}

namespace idlcli {
namespace vibrator {

namespace V1_0 = ::android::hardware::vibrator::V1_0;
namespace V1_1 = ::android::hardware::vibrator::V1_1;
namespace V1_2 = ::android::hardware::vibrator::V1_2;
namespace V1_3 = ::android::hardware::vibrator::V1_3;
namespace aidl = ::aidl::android::hardware::vibrator;

class VibratorCallback : public aidl::BnVibratorCallback {
+6 −10
Original line number Diff line number Diff line
@@ -51,21 +51,17 @@ class CommandAlwaysOnDisable : public Command {
    }

    Status doMain(Args && /*args*/) override {
        std::string statusStr;
        Status ret;
        auto hal = getHal();

        if (auto hal = getHal<aidl::IVibrator>()) {
            auto status = hal->call(&aidl::IVibrator::alwaysOnDisable, mId);

            statusStr = status.getDescription();
            ret = status.isOk() ? OK : ERROR;
        } else {
        if (!hal) {
            return UNAVAILABLE;
        }

        std::cout << "Status: " << statusStr << std::endl;
        auto status = hal->alwaysOnDisable(mId);

        std::cout << "Status: " << status.getDescription() << std::endl;

        return ret;
        return status.isOk() ? OK : ERROR;
    }

    int32_t mId;
+6 −10
Original line number Diff line number Diff line
@@ -72,21 +72,17 @@ class CommandAlwaysOnEnable : public Command {
    }

    Status doMain(Args && /*args*/) override {
        std::string statusStr;
        Status ret;
        auto hal = getHal();

        if (auto hal = getHal<aidl::IVibrator>()) {
            auto status = hal->call(&aidl::IVibrator::alwaysOnEnable, mId, mEffect, mStrength);

            statusStr = status.getDescription();
            ret = status.isOk() ? OK : ERROR;
        } else {
        if (!hal) {
            return UNAVAILABLE;
        }

        std::cout << "Status: " << statusStr << std::endl;
        auto status = hal->alwaysOnEnable(mId, mEffect, mStrength);

        std::cout << "Status: " << status.getDescription() << std::endl;

        return ret;
        return status.isOk() ? OK : ERROR;
    }

    int32_t mId;
Loading