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 | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 34 | IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS | |
| 35 | IVibrator::CAP_ALWAYS_ON_CONTROL; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 36 | return ndk::ScopedAStatus::ok(); |
| 37 | } |
| 38 | |
| 39 | ndk::ScopedAStatus Vibrator::off() { |
| 40 | LOG(INFO) << "Vibrator off"; |
| 41 | return ndk::ScopedAStatus::ok(); |
| 42 | } |
| 43 | |
| 44 | ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, |
| 45 | const std::shared_ptr<IVibratorCallback>& callback) { |
| 46 | LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs; |
| 47 | if (callback != nullptr) { |
| 48 | std::thread([=] { |
| 49 | LOG(INFO) << "Starting on on another thread"; |
| 50 | usleep(timeoutMs * 1000); |
| 51 | LOG(INFO) << "Notifying on complete"; |
Steven Moreland | f57ad9b | 2019-11-27 16:04:52 -0800 | [diff] [blame] | 52 | if (!callback->onComplete().isOk()) { |
| 53 | LOG(ERROR) << "Failed to call onComplete"; |
| 54 | } |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 55 | }).detach(); |
| 56 | } |
| 57 | return ndk::ScopedAStatus::ok(); |
| 58 | } |
| 59 | |
| 60 | ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, |
| 61 | const std::shared_ptr<IVibratorCallback>& callback, |
| 62 | int32_t* _aidl_return) { |
| 63 | LOG(INFO) << "Vibrator perform"; |
| 64 | |
| 65 | if (effect != Effect::CLICK && effect != Effect::TICK) { |
| 66 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); |
| 67 | } |
| 68 | if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM && |
| 69 | strength != EffectStrength::STRONG) { |
| 70 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); |
| 71 | } |
| 72 | |
| 73 | constexpr size_t kEffectMillis = 100; |
| 74 | |
| 75 | if (callback != nullptr) { |
| 76 | std::thread([=] { |
| 77 | LOG(INFO) << "Starting perform on another thread"; |
| 78 | usleep(kEffectMillis * 1000); |
| 79 | LOG(INFO) << "Notifying perform complete"; |
| 80 | callback->onComplete(); |
| 81 | }).detach(); |
| 82 | } |
| 83 | |
| 84 | *_aidl_return = kEffectMillis; |
| 85 | return ndk::ScopedAStatus::ok(); |
| 86 | } |
| 87 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 88 | ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) { |
| 89 | *_aidl_return = {Effect::CLICK, Effect::TICK}; |
| 90 | return ndk::ScopedAStatus::ok(); |
| 91 | } |
| 92 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 93 | ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 94 | LOG(INFO) << "Vibrator set amplitude: " << amplitude; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 95 | if (amplitude <= 0.0f || amplitude > 1.0f) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 96 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT)); |
| 97 | } |
| 98 | return ndk::ScopedAStatus::ok(); |
| 99 | } |
| 100 | |
| 101 | ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) { |
| 102 | LOG(INFO) << "Vibrator set external control: " << enabled; |
| 103 | return ndk::ScopedAStatus::ok(); |
| 104 | } |
| 105 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 106 | ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) { |
| 107 | *maxDelayMs = kComposeDelayMaxMs; |
| 108 | return ndk::ScopedAStatus::ok(); |
| 109 | } |
| 110 | |
| 111 | ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) { |
| 112 | *maxSize = kComposeSizeMax; |
| 113 | return ndk::ScopedAStatus::ok(); |
| 114 | } |
| 115 | |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 116 | ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector<CompositePrimitive>* supported) { |
| 117 | *supported = { |
| 118 | CompositePrimitive::NOOP, CompositePrimitive::CLICK, |
| 119 | CompositePrimitive::THUD, CompositePrimitive::SPIN, |
| 120 | CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE, |
Harpreet \"Eli\" Sangha | fe5d398 | 2020-01-17 14:46:37 +0900 | [diff] [blame] | 121 | CompositePrimitive::QUICK_FALL, CompositePrimitive::LIGHT_TICK, |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 122 | }; |
| 123 | return ndk::ScopedAStatus::ok(); |
| 124 | } |
| 125 | |
| 126 | ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive primitive, |
| 127 | int32_t* durationMs) { |
| 128 | if (primitive != CompositePrimitive::NOOP) { |
| 129 | *durationMs = 100; |
| 130 | } else { |
| 131 | *durationMs = 0; |
| 132 | } |
| 133 | return ndk::ScopedAStatus::ok(); |
| 134 | } |
| 135 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 136 | ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite, |
| 137 | const std::shared_ptr<IVibratorCallback>& callback) { |
| 138 | if (composite.size() > kComposeSizeMax) { |
| 139 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 140 | } |
| 141 | |
Harpreet \"Eli\" Sangha | 13ef28c | 2020-01-23 13:22:07 +0900 | [diff] [blame] | 142 | std::vector<CompositePrimitive> supported; |
| 143 | getSupportedPrimitives(&supported); |
| 144 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 145 | for (auto& e : composite) { |
| 146 | if (e.delayMs > kComposeDelayMaxMs) { |
| 147 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 148 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame^] | 149 | if (e.scale < 0.0f || e.scale > 1.0f) { |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 150 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 151 | } |
Harpreet \"Eli\" Sangha | 13ef28c | 2020-01-23 13:22:07 +0900 | [diff] [blame] | 152 | if (std::find(supported.begin(), supported.end(), e.primitive) == supported.end()) { |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 153 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | std::thread([=] { |
| 158 | LOG(INFO) << "Starting compose on another thread"; |
| 159 | |
| 160 | for (auto& e : composite) { |
| 161 | if (e.delayMs) { |
| 162 | usleep(e.delayMs * 1000); |
| 163 | } |
| 164 | LOG(INFO) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale " |
| 165 | << e.scale; |
| 166 | } |
| 167 | |
| 168 | if (callback != nullptr) { |
| 169 | LOG(INFO) << "Notifying perform complete"; |
| 170 | callback->onComplete(); |
| 171 | } |
| 172 | }).detach(); |
| 173 | |
| 174 | return ndk::ScopedAStatus::ok(); |
| 175 | } |
| 176 | |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 177 | ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect>* _aidl_return) { |
| 178 | return getSupportedEffects(_aidl_return); |
| 179 | } |
| 180 | |
| 181 | ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { |
| 182 | std::vector<Effect> effects; |
| 183 | getSupportedAlwaysOnEffects(&effects); |
| 184 | |
| 185 | if (std::find(effects.begin(), effects.end(), effect) == effects.end()) { |
| 186 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 187 | } else { |
| 188 | LOG(INFO) << "Enabling always-on ID " << id << " with " << toString(effect) << "/" |
| 189 | << toString(strength); |
| 190 | return ndk::ScopedAStatus::ok(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t id) { |
| 195 | LOG(INFO) << "Disabling always-on ID " << id; |
| 196 | return ndk::ScopedAStatus::ok(); |
| 197 | } |
| 198 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 199 | } // namespace vibrator |
| 200 | } // namespace hardware |
| 201 | } // namespace android |
| 202 | } // namespace aidl |