/* * 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 "vibrator-impl/Vibrator.h" #include #include namespace aidl { namespace android { namespace hardware { namespace vibrator { static constexpr int32_t kComposeDelayMaxMs = 1000; static constexpr int32_t kComposeSizeMax = 256; ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) { LOG(INFO) << "Vibrator reporting capabilities"; *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK | IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL | IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS | IVibrator::CAP_ALWAYS_ON_CONTROL; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::off() { LOG(INFO) << "Vibrator off"; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, const std::shared_ptr& callback) { LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs; if (callback != nullptr) { std::thread([=] { LOG(INFO) << "Starting on on another thread"; usleep(timeoutMs * 1000); LOG(INFO) << "Notifying on complete"; if (!callback->onComplete().isOk()) { LOG(ERROR) << "Failed to call onComplete"; } }).detach(); } return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, const std::shared_ptr& callback, int32_t* _aidl_return) { LOG(INFO) << "Vibrator perform"; if (effect != Effect::CLICK && effect != Effect::TICK) { return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); } if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM && strength != EffectStrength::STRONG) { return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); } constexpr size_t kEffectMillis = 100; if (callback != nullptr) { std::thread([=] { LOG(INFO) << "Starting perform on another thread"; usleep(kEffectMillis * 1000); LOG(INFO) << "Notifying perform complete"; callback->onComplete(); }).detach(); } *_aidl_return = kEffectMillis; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector* _aidl_return) { *_aidl_return = {Effect::CLICK, Effect::TICK}; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) { LOG(INFO) << "Vibrator set amplitude: " << amplitude; if (amplitude <= 0.0f || amplitude > 1.0f) { return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT)); } return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) { LOG(INFO) << "Vibrator set external control: " << enabled; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) { *maxDelayMs = kComposeDelayMaxMs; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) { *maxSize = kComposeSizeMax; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector* supported) { *supported = { CompositePrimitive::NOOP, CompositePrimitive::CLICK, CompositePrimitive::THUD, CompositePrimitive::SPIN, CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE, CompositePrimitive::QUICK_FALL, CompositePrimitive::LIGHT_TICK, }; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive primitive, int32_t* durationMs) { if (primitive != CompositePrimitive::NOOP) { *durationMs = 100; } else { *durationMs = 0; } return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::compose(const std::vector& composite, const std::shared_ptr& callback) { if (composite.size() > kComposeSizeMax) { return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); } std::vector supported; getSupportedPrimitives(&supported); for (auto& e : composite) { if (e.delayMs > kComposeDelayMaxMs) { return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); } if (e.scale < 0.0f || e.scale > 1.0f) { return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); } if (std::find(supported.begin(), supported.end(), e.primitive) == supported.end()) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } } std::thread([=] { LOG(INFO) << "Starting compose on another thread"; for (auto& e : composite) { if (e.delayMs) { usleep(e.delayMs * 1000); } LOG(INFO) << "triggering primitive " << static_cast(e.primitive) << " @ scale " << e.scale; int32_t durationMs; getPrimitiveDuration(e.primitive, &durationMs); usleep(durationMs * 1000); } if (callback != nullptr) { LOG(INFO) << "Notifying perform complete"; callback->onComplete(); } }).detach(); return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector* _aidl_return) { return getSupportedEffects(_aidl_return); } ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { std::vector effects; getSupportedAlwaysOnEffects(&effects); if (std::find(effects.begin(), effects.end(), effect) == effects.end()) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } else { LOG(INFO) << "Enabling always-on ID " << id << " with " << toString(effect) << "/" << toString(strength); return ndk::ScopedAStatus::ok(); } } ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t id) { LOG(INFO) << "Disabling always-on ID " << id; return ndk::ScopedAStatus::ok(); } } // namespace vibrator } // namespace hardware } // namespace android } // namespace aidl