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 | |
Steven Moreland | 8ba8c03 | 2019-11-18 16:23:39 -0800 | [diff] [blame] | 17 | #include "vibrator-impl/Vibrator.h" |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <thread> |
| 21 | |
| 22 | namespace aidl { |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace vibrator { |
| 26 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame^] | 27 | static constexpr int32_t kComposeDelayMaxMs = 1000; |
| 28 | static constexpr int32_t kComposeSizeMax = 256; |
| 29 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 30 | ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) { |
| 31 | LOG(INFO) << "Vibrator reporting capabilities"; |
| 32 | *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK | |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 33 | IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame^] | 34 | IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 35 | return ndk::ScopedAStatus::ok(); |
| 36 | } |
| 37 | |
| 38 | ndk::ScopedAStatus Vibrator::off() { |
| 39 | LOG(INFO) << "Vibrator off"; |
| 40 | return ndk::ScopedAStatus::ok(); |
| 41 | } |
| 42 | |
| 43 | ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, |
| 44 | const std::shared_ptr<IVibratorCallback>& callback) { |
| 45 | LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs; |
| 46 | if (callback != nullptr) { |
| 47 | std::thread([=] { |
| 48 | LOG(INFO) << "Starting on on another thread"; |
| 49 | usleep(timeoutMs * 1000); |
| 50 | LOG(INFO) << "Notifying on complete"; |
| 51 | callback->onComplete(); |
| 52 | }).detach(); |
| 53 | } |
| 54 | return ndk::ScopedAStatus::ok(); |
| 55 | } |
| 56 | |
| 57 | ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, |
| 58 | const std::shared_ptr<IVibratorCallback>& callback, |
| 59 | int32_t* _aidl_return) { |
| 60 | LOG(INFO) << "Vibrator perform"; |
| 61 | |
| 62 | if (effect != Effect::CLICK && effect != Effect::TICK) { |
| 63 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); |
| 64 | } |
| 65 | if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM && |
| 66 | strength != EffectStrength::STRONG) { |
| 67 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); |
| 68 | } |
| 69 | |
| 70 | constexpr size_t kEffectMillis = 100; |
| 71 | |
| 72 | if (callback != nullptr) { |
| 73 | std::thread([=] { |
| 74 | LOG(INFO) << "Starting perform on another thread"; |
| 75 | usleep(kEffectMillis * 1000); |
| 76 | LOG(INFO) << "Notifying perform complete"; |
| 77 | callback->onComplete(); |
| 78 | }).detach(); |
| 79 | } |
| 80 | |
| 81 | *_aidl_return = kEffectMillis; |
| 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 85 | ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) { |
| 86 | *_aidl_return = {Effect::CLICK, Effect::TICK}; |
| 87 | return ndk::ScopedAStatus::ok(); |
| 88 | } |
| 89 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame^] | 90 | ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 91 | LOG(INFO) << "Vibrator set amplitude: " << amplitude; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame^] | 92 | if (amplitude <= 0.0f || amplitude > 1.0f) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 93 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT)); |
| 94 | } |
| 95 | return ndk::ScopedAStatus::ok(); |
| 96 | } |
| 97 | |
| 98 | ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) { |
| 99 | LOG(INFO) << "Vibrator set external control: " << enabled; |
| 100 | return ndk::ScopedAStatus::ok(); |
| 101 | } |
| 102 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame^] | 103 | ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) { |
| 104 | *maxDelayMs = kComposeDelayMaxMs; |
| 105 | return ndk::ScopedAStatus::ok(); |
| 106 | } |
| 107 | |
| 108 | ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) { |
| 109 | *maxSize = kComposeSizeMax; |
| 110 | return ndk::ScopedAStatus::ok(); |
| 111 | } |
| 112 | |
| 113 | ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite, |
| 114 | const std::shared_ptr<IVibratorCallback>& callback) { |
| 115 | if (composite.size() > kComposeSizeMax) { |
| 116 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 117 | } |
| 118 | |
| 119 | for (auto& e : composite) { |
| 120 | if (e.delayMs > kComposeDelayMaxMs) { |
| 121 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 122 | } |
| 123 | if (e.scale <= 0.0f || e.scale > 1.0f) { |
| 124 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 125 | } |
| 126 | if (e.primitive < CompositePrimitive::NOOP || |
| 127 | e.primitive > CompositePrimitive::QUICK_FALL) { |
| 128 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | std::thread([=] { |
| 133 | LOG(INFO) << "Starting compose on another thread"; |
| 134 | |
| 135 | for (auto& e : composite) { |
| 136 | if (e.delayMs) { |
| 137 | usleep(e.delayMs * 1000); |
| 138 | } |
| 139 | LOG(INFO) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale " |
| 140 | << e.scale; |
| 141 | } |
| 142 | |
| 143 | if (callback != nullptr) { |
| 144 | LOG(INFO) << "Notifying perform complete"; |
| 145 | callback->onComplete(); |
| 146 | } |
| 147 | }).detach(); |
| 148 | |
| 149 | return ndk::ScopedAStatus::ok(); |
| 150 | } |
| 151 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 152 | } // namespace vibrator |
| 153 | } // namespace hardware |
| 154 | } // namespace android |
| 155 | } // namespace aidl |