Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Vibrator.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <thread> |
| 21 | |
| 22 | namespace aidl { |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace vibrator { |
| 26 | |
| 27 | ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) { |
| 28 | LOG(INFO) << "Vibrator reporting capabilities"; |
| 29 | *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK | |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame^] | 30 | IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL | |
| 31 | IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 32 | return ndk::ScopedAStatus::ok(); |
| 33 | } |
| 34 | |
| 35 | ndk::ScopedAStatus Vibrator::off() { |
| 36 | LOG(INFO) << "Vibrator off"; |
| 37 | return ndk::ScopedAStatus::ok(); |
| 38 | } |
| 39 | |
| 40 | ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, |
| 41 | const std::shared_ptr<IVibratorCallback>& callback) { |
| 42 | LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs; |
| 43 | if (callback != nullptr) { |
| 44 | std::thread([=] { |
| 45 | LOG(INFO) << "Starting on on another thread"; |
| 46 | usleep(timeoutMs * 1000); |
| 47 | LOG(INFO) << "Notifying on complete"; |
| 48 | callback->onComplete(); |
| 49 | }).detach(); |
| 50 | } |
| 51 | return ndk::ScopedAStatus::ok(); |
| 52 | } |
| 53 | |
| 54 | ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, |
| 55 | const std::shared_ptr<IVibratorCallback>& callback, |
| 56 | int32_t* _aidl_return) { |
| 57 | LOG(INFO) << "Vibrator perform"; |
| 58 | |
| 59 | if (effect != Effect::CLICK && effect != Effect::TICK) { |
| 60 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); |
| 61 | } |
| 62 | if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM && |
| 63 | strength != EffectStrength::STRONG) { |
| 64 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); |
| 65 | } |
| 66 | |
| 67 | constexpr size_t kEffectMillis = 100; |
| 68 | |
| 69 | if (callback != nullptr) { |
| 70 | std::thread([=] { |
| 71 | LOG(INFO) << "Starting perform on another thread"; |
| 72 | usleep(kEffectMillis * 1000); |
| 73 | LOG(INFO) << "Notifying perform complete"; |
| 74 | callback->onComplete(); |
| 75 | }).detach(); |
| 76 | } |
| 77 | |
| 78 | *_aidl_return = kEffectMillis; |
| 79 | return ndk::ScopedAStatus::ok(); |
| 80 | } |
| 81 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 82 | ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) { |
| 83 | *_aidl_return = {Effect::CLICK, Effect::TICK}; |
| 84 | return ndk::ScopedAStatus::ok(); |
| 85 | } |
| 86 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 87 | ndk::ScopedAStatus Vibrator::setAmplitude(int32_t amplitude) { |
| 88 | LOG(INFO) << "Vibrator set amplitude: " << amplitude; |
| 89 | if (amplitude <= 0 || amplitude > 255) { |
| 90 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT)); |
| 91 | } |
| 92 | return ndk::ScopedAStatus::ok(); |
| 93 | } |
| 94 | |
| 95 | ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) { |
| 96 | LOG(INFO) << "Vibrator set external control: " << enabled; |
| 97 | return ndk::ScopedAStatus::ok(); |
| 98 | } |
| 99 | |
| 100 | } // namespace vibrator |
| 101 | } // namespace hardware |
| 102 | } // namespace android |
| 103 | } // namespace aidl |