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

Commit 6cea68f9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "idlcli: Support AIDL Vibrator"

parents 781e508f cb350b82
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -20,9 +20,11 @@ cc_defaults {
        "android.hardware.vibrator@1.2",
        "android.hardware.vibrator@1.3",
        "libbase",
        "libbinder",
        "libhidlbase",
        "liblog",
        "libutils",
        "vintf-vibrator-cpp",
    ],
    cflags: [
        "-DLOG_TAG=\"idlcli\"",
@@ -34,6 +36,7 @@ cc_library {
    defaults: ["idlcli-defaults"],
    srcs: [
        "CommandVibrator.cpp",
        "vibrator/CommandGetCapabilities.cpp",
        "vibrator/CommandOff.cpp",
        "vibrator/CommandOn.cpp",
        "vibrator/CommandPerform.cpp",
+24 −5
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
#define FRAMEWORK_NATIVE_CMDS_IDLCLI_VIBRATOR_H_

#include <android/hardware/vibrator/1.3/IVibrator.h>
#include <android/hardware/vibrator/IVibrator.h>
#include <binder/IServiceManager.h>

#include "utils.h"

@@ -31,9 +33,25 @@ static constexpr int NUM_TRIES = 2;

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

template <>
inline binder::Status NullptrStatus() {
    using binder::Status;
    return Status::fromExceptionCode(Status::EX_NULL_POINTER);
}

template <typename I>
inline sp<I> getService() {
    return I::getService();
}

template <>
inline sp<hardware::vibrator::IVibrator> getService() {
    return waitForVintfService<hardware::vibrator::IVibrator>();
}

template <typename I>
@@ -42,12 +60,12 @@ public:
    static std::unique_ptr<HalWrapper> Create() {
        // Assume that if getService returns a nullptr, HAL is not available on the
        // device.
        auto hal = I::getService();
        auto hal = getService<I>();
        return hal ? std::unique_ptr<HalWrapper>(new HalWrapper(std::move(hal))) : nullptr;
    }

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

@@ -65,7 +83,7 @@ static auto getHal() {
}

template <class R, class I, class... Args0, class... Args1>
Return<R> halCall(Return<R> (I::*fn)(Args0...), Args1&&... args1) {
R halCall(R (I::*fn)(Args0...), Args1&&... args1) {
    auto hal = getHal<I>();
    return hal ? hal->call(fn, std::forward<Args1>(args1)...) : NullptrStatus<R>();
}
@@ -77,6 +95,7 @@ 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 = ::android::hardware::vibrator;

} // namespace vibrator
} // namespace idlcli
+69 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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 "utils.h"
#include "vibrator.h"

namespace android {
namespace idlcli {

class CommandVibrator;

namespace vibrator {

class CommandGetCapabilities : public Command {
    std::string getDescription() const override { return "Retrieves vibrator capabilities."; }

    std::string getUsageSummary() const override { return ""; }

    UsageDetails getUsageDetails() const override {
        UsageDetails details{};
        return details;
    }

    Status doArgs(Args &args) override {
        if (!args.empty()) {
            std::cerr << "Unexpected Arguments!" << std::endl;
            return USAGE;
        }
        return OK;
    }

    Status doMain(Args && /*args*/) override {
        std::string statusStr;
        int32_t cap;
        Status ret;

        if (auto hal = getHal<aidl::IVibrator>()) {
            auto status = hal->call(&aidl::IVibrator::getCapabilities, &cap);
            statusStr = status.toString8();
            ret = status.isOk() ? OK : ERROR;
        } else {
            return UNAVAILABLE;
        }

        std::cout << "Status: " << statusStr << std::endl;
        std::cout << "Capabilities: " << std::bitset<32>(cap) << std::endl;

        return ret;
    }
};

static const auto Command =
        CommandRegistry<CommandVibrator>::Register<CommandGetCapabilities>("getCapabilities");

} // namespace vibrator
} // namespace idlcli
} // namespace android
+13 −4
Original line number Diff line number Diff line
@@ -42,15 +42,24 @@ class CommandOff : public Command {
    }

    Status doMain(Args && /*args*/) override {
        auto ret = halCall(&V1_0::IVibrator::off);
        std::string statusStr;
        Status ret;

        if (!ret.isOk()) {
        if (auto hal = getHal<aidl::IVibrator>()) {
            auto status = hal->call(&aidl::IVibrator::off);
            statusStr = status.toString8();
            ret = status.isOk() ? OK : ERROR;
        } else if (auto hal = getHal<V1_0::IVibrator>()) {
            auto status = hal->call(&V1_0::IVibrator::off);
            statusStr = toString(status);
            ret = status.isOk() && status == V1_0::Status::OK ? OK : ERROR;
        } else {
            return UNAVAILABLE;
        }

        std::cout << "Status: " << toString(ret) << std::endl;
        std::cout << "Status: " << statusStr << std::endl;

        return ret == V1_0::Status::OK ? OK : ERROR;
        return ret;
    }
};

+13 −4
Original line number Diff line number Diff line
@@ -50,15 +50,24 @@ class CommandOn : public Command {
    }

    Status doMain(Args && /*args*/) override {
        auto ret = halCall(&V1_0::IVibrator::on, mDuration);
        std::string statusStr;
        Status ret;

        if (!ret.isOk()) {
        if (auto hal = getHal<aidl::IVibrator>()) {
            auto status = hal->call(&aidl::IVibrator::on, mDuration, nullptr);
            statusStr = status.toString8();
            ret = status.isOk() ? OK : ERROR;
        } else if (auto hal = getHal<V1_0::IVibrator>()) {
            auto status = hal->call(&V1_0::IVibrator::on, mDuration);
            statusStr = toString(status);
            ret = status.isOk() && status == V1_0::Status::OK ? OK : ERROR;
        } else {
            return UNAVAILABLE;
        }

        std::cout << "Status: " << toString(ret) << std::endl;
        std::cout << "Status: " << statusStr << std::endl;

        return ret == V1_0::Status::OK ? OK : ERROR;
        return ret;
    }

    uint32_t mDuration;
Loading