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