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 | |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 27 | static constexpr int32_t COMPOSE_DELAY_MAX_MS = 1000; |
| 28 | static constexpr int32_t COMPOSE_SIZE_MAX = 256; |
| 29 | static constexpr int32_t COMPOSE_PWLE_SIZE_MAX = 127; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 30 | |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 31 | static constexpr float Q_FACTOR = 11.0; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 32 | static constexpr int32_t COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS = 16383; |
| 33 | static constexpr float PWLE_LEVEL_MIN = 0.0; |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 34 | static constexpr float PWLE_LEVEL_MAX = 1.0; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 35 | static constexpr float PWLE_FREQUENCY_RESOLUTION_HZ = 1.0; |
| 36 | static constexpr float PWLE_FREQUENCY_MIN_HZ = 140.0; |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 37 | static constexpr float RESONANT_FREQUENCY_HZ = 150.0; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 38 | static constexpr float PWLE_FREQUENCY_MAX_HZ = 160.0; |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 39 | static constexpr float PWLE_BW_MAP_SIZE = |
| 40 | 1 + ((PWLE_FREQUENCY_MAX_HZ - PWLE_FREQUENCY_MIN_HZ) / PWLE_FREQUENCY_RESOLUTION_HZ); |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 41 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 42 | ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 43 | LOG(VERBOSE) << "Vibrator reporting capabilities"; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 44 | *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK | |
Steven Moreland | c0b92d5 | 2019-11-05 13:31:41 -0800 | [diff] [blame] | 45 | IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL | |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 46 | IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS | |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 47 | IVibrator::CAP_ALWAYS_ON_CONTROL | IVibrator::CAP_GET_RESONANT_FREQUENCY | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 48 | IVibrator::CAP_GET_Q_FACTOR | IVibrator::CAP_FREQUENCY_CONTROL | |
| 49 | IVibrator::CAP_COMPOSE_PWLE_EFFECTS; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 50 | return ndk::ScopedAStatus::ok(); |
| 51 | } |
| 52 | |
| 53 | ndk::ScopedAStatus Vibrator::off() { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 54 | LOG(VERBOSE) << "Vibrator off"; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 55 | return ndk::ScopedAStatus::ok(); |
| 56 | } |
| 57 | |
| 58 | ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs, |
| 59 | const std::shared_ptr<IVibratorCallback>& callback) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 60 | LOG(VERBOSE) << "Vibrator on for timeoutMs: " << timeoutMs; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 61 | if (callback != nullptr) { |
Simon Bowden | 608655b | 2022-06-01 12:09:41 +0000 | [diff] [blame] | 62 | // Note that thread lambdas aren't using implicit capture [=], to avoid capturing "this", |
| 63 | // which may be asynchronously destructed. |
| 64 | // If "this" is needed, use [sharedThis = this->ref<Vibrator>()]. |
| 65 | std::thread([timeoutMs, callback] { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 66 | LOG(VERBOSE) << "Starting on on another thread"; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 67 | usleep(timeoutMs * 1000); |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 68 | LOG(VERBOSE) << "Notifying on complete"; |
Steven Moreland | f57ad9b | 2019-11-27 16:04:52 -0800 | [diff] [blame] | 69 | if (!callback->onComplete().isOk()) { |
| 70 | LOG(ERROR) << "Failed to call onComplete"; |
| 71 | } |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 72 | }).detach(); |
| 73 | } |
| 74 | return ndk::ScopedAStatus::ok(); |
| 75 | } |
| 76 | |
| 77 | ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength, |
| 78 | const std::shared_ptr<IVibratorCallback>& callback, |
| 79 | int32_t* _aidl_return) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 80 | LOG(VERBOSE) << "Vibrator perform"; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 81 | |
| 82 | if (effect != Effect::CLICK && effect != Effect::TICK) { |
| 83 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); |
| 84 | } |
| 85 | if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM && |
| 86 | strength != EffectStrength::STRONG) { |
| 87 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION)); |
| 88 | } |
| 89 | |
| 90 | constexpr size_t kEffectMillis = 100; |
| 91 | |
| 92 | if (callback != nullptr) { |
Simon Bowden | 608655b | 2022-06-01 12:09:41 +0000 | [diff] [blame] | 93 | std::thread([callback] { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 94 | LOG(VERBOSE) << "Starting perform on another thread"; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 95 | usleep(kEffectMillis * 1000); |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 96 | LOG(VERBOSE) << "Notifying perform complete"; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 97 | callback->onComplete(); |
| 98 | }).detach(); |
| 99 | } |
| 100 | |
| 101 | *_aidl_return = kEffectMillis; |
| 102 | return ndk::ScopedAStatus::ok(); |
| 103 | } |
| 104 | |
Steven Moreland | 2932b22 | 2019-11-05 14:30:17 -0800 | [diff] [blame] | 105 | ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) { |
| 106 | *_aidl_return = {Effect::CLICK, Effect::TICK}; |
| 107 | return ndk::ScopedAStatus::ok(); |
| 108 | } |
| 109 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 110 | ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 111 | LOG(VERBOSE) << "Vibrator set amplitude: " << amplitude; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 112 | if (amplitude <= 0.0f || amplitude > 1.0f) { |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 113 | return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT)); |
| 114 | } |
| 115 | return ndk::ScopedAStatus::ok(); |
| 116 | } |
| 117 | |
| 118 | ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 119 | LOG(VERBOSE) << "Vibrator set external control: " << enabled; |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 120 | return ndk::ScopedAStatus::ok(); |
| 121 | } |
| 122 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 123 | ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 124 | *maxDelayMs = COMPOSE_DELAY_MAX_MS; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 125 | return ndk::ScopedAStatus::ok(); |
| 126 | } |
| 127 | |
| 128 | ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 129 | *maxSize = COMPOSE_SIZE_MAX; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 130 | return ndk::ScopedAStatus::ok(); |
| 131 | } |
| 132 | |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 133 | ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector<CompositePrimitive>* supported) { |
| 134 | *supported = { |
| 135 | CompositePrimitive::NOOP, CompositePrimitive::CLICK, |
| 136 | CompositePrimitive::THUD, CompositePrimitive::SPIN, |
| 137 | CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE, |
Harpreet \"Eli\" Sangha | fe5d398 | 2020-01-17 14:46:37 +0900 | [diff] [blame] | 138 | CompositePrimitive::QUICK_FALL, CompositePrimitive::LIGHT_TICK, |
Vince Leung | deb46ec | 2020-12-22 00:03:16 +0000 | [diff] [blame] | 139 | CompositePrimitive::LOW_TICK, |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 140 | }; |
| 141 | return ndk::ScopedAStatus::ok(); |
| 142 | } |
| 143 | |
| 144 | ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive primitive, |
| 145 | int32_t* durationMs) { |
Lais Andrade | 3c7f0d9 | 2021-06-16 10:20:09 +0000 | [diff] [blame] | 146 | std::vector<CompositePrimitive> supported; |
| 147 | getSupportedPrimitives(&supported); |
| 148 | if (std::find(supported.begin(), supported.end(), primitive) == supported.end()) { |
| 149 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 150 | } |
Harpreet \"Eli\" Sangha | 523e296 | 2020-01-21 16:15:42 +0900 | [diff] [blame] | 151 | if (primitive != CompositePrimitive::NOOP) { |
| 152 | *durationMs = 100; |
| 153 | } else { |
| 154 | *durationMs = 0; |
| 155 | } |
| 156 | return ndk::ScopedAStatus::ok(); |
| 157 | } |
| 158 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 159 | ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite, |
| 160 | const std::shared_ptr<IVibratorCallback>& callback) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 161 | if (composite.size() > COMPOSE_SIZE_MAX) { |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 162 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 163 | } |
| 164 | |
Harpreet \"Eli\" Sangha | 13ef28c | 2020-01-23 13:22:07 +0900 | [diff] [blame] | 165 | std::vector<CompositePrimitive> supported; |
| 166 | getSupportedPrimitives(&supported); |
| 167 | |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 168 | for (auto& e : composite) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 169 | if (e.delayMs > COMPOSE_DELAY_MAX_MS) { |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 170 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 171 | } |
Harpreet \"Eli\" Sangha | 7aec502 | 2020-03-11 06:00:55 +0900 | [diff] [blame] | 172 | if (e.scale < 0.0f || e.scale > 1.0f) { |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 173 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 174 | } |
Harpreet \"Eli\" Sangha | 13ef28c | 2020-01-23 13:22:07 +0900 | [diff] [blame] | 175 | if (std::find(supported.begin(), supported.end(), e.primitive) == supported.end()) { |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 176 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 177 | } |
| 178 | } |
| 179 | |
Simon Bowden | 608655b | 2022-06-01 12:09:41 +0000 | [diff] [blame] | 180 | // The thread may theoretically outlive the vibrator, so take a proper reference to it. |
| 181 | std::thread([sharedThis = this->ref<Vibrator>(), composite, callback] { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 182 | LOG(VERBOSE) << "Starting compose on another thread"; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 183 | |
| 184 | for (auto& e : composite) { |
| 185 | if (e.delayMs) { |
| 186 | usleep(e.delayMs * 1000); |
| 187 | } |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 188 | LOG(VERBOSE) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale " |
| 189 | << e.scale; |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 190 | |
| 191 | int32_t durationMs; |
Simon Bowden | 608655b | 2022-06-01 12:09:41 +0000 | [diff] [blame] | 192 | sharedThis->getPrimitiveDuration(e.primitive, &durationMs); |
Harpreet \"Eli\" Sangha | b075a6a | 2020-04-10 15:11:58 +0900 | [diff] [blame] | 193 | usleep(durationMs * 1000); |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | if (callback != nullptr) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 197 | LOG(VERBOSE) << "Notifying perform complete"; |
Harpreet \"Eli\" Sangha | f4de5b0 | 2019-10-23 09:25:52 +0900 | [diff] [blame] | 198 | callback->onComplete(); |
| 199 | } |
| 200 | }).detach(); |
| 201 | |
| 202 | return ndk::ScopedAStatus::ok(); |
| 203 | } |
| 204 | |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 205 | ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect>* _aidl_return) { |
| 206 | return getSupportedEffects(_aidl_return); |
| 207 | } |
| 208 | |
| 209 | ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) { |
| 210 | std::vector<Effect> effects; |
| 211 | getSupportedAlwaysOnEffects(&effects); |
| 212 | |
| 213 | if (std::find(effects.begin(), effects.end(), effect) == effects.end()) { |
| 214 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 215 | } else { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 216 | LOG(VERBOSE) << "Enabling always-on ID " << id << " with " << toString(effect) << "/" |
| 217 | << toString(strength); |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 218 | return ndk::ScopedAStatus::ok(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t id) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 223 | LOG(VERBOSE) << "Disabling always-on ID " << id; |
Harpreet \"Eli\" Sangha | 6362409 | 2019-09-09 11:04:54 +0900 | [diff] [blame] | 224 | return ndk::ScopedAStatus::ok(); |
| 225 | } |
| 226 | |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 227 | ndk::ScopedAStatus Vibrator::getResonantFrequency(float *resonantFreqHz) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 228 | *resonantFreqHz = RESONANT_FREQUENCY_HZ; |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 229 | return ndk::ScopedAStatus::ok(); |
| 230 | } |
| 231 | |
| 232 | ndk::ScopedAStatus Vibrator::getQFactor(float *qFactor) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 233 | *qFactor = Q_FACTOR; |
Vince Leung | 4bae4f9 | 2021-02-03 06:21:58 +0000 | [diff] [blame] | 234 | return ndk::ScopedAStatus::ok(); |
| 235 | } |
| 236 | |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 237 | ndk::ScopedAStatus Vibrator::getFrequencyResolution(float *freqResolutionHz) { |
| 238 | *freqResolutionHz = PWLE_FREQUENCY_RESOLUTION_HZ; |
| 239 | return ndk::ScopedAStatus::ok(); |
| 240 | } |
| 241 | |
| 242 | ndk::ScopedAStatus Vibrator::getFrequencyMinimum(float *freqMinimumHz) { |
| 243 | *freqMinimumHz = PWLE_FREQUENCY_MIN_HZ; |
| 244 | return ndk::ScopedAStatus::ok(); |
| 245 | } |
| 246 | |
| 247 | ndk::ScopedAStatus Vibrator::getBandwidthAmplitudeMap(std::vector<float> *_aidl_return) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 248 | // The output BandwidthAmplitudeMap will be as below and the maximum |
| 249 | // amplitude 1.0 will be set on RESONANT_FREQUENCY_HZ |
| 250 | // {0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1, 0.99, 0.98, 0.97, |
| 251 | // 0.96, 0.95, 0.94, 0.93, 0.92, 0.91, 0.9} |
| 252 | int32_t capabilities = 0; |
| 253 | int halfMapSize = PWLE_BW_MAP_SIZE / 2; |
| 254 | Vibrator::getCapabilities(&capabilities); |
| 255 | if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) { |
| 256 | std::vector<float> bandwidthAmplitudeMap(PWLE_BW_MAP_SIZE, PWLE_LEVEL_MAX); |
| 257 | for (int i = 0; i < halfMapSize; ++i) { |
| 258 | bandwidthAmplitudeMap[halfMapSize + i + 1] = |
| 259 | bandwidthAmplitudeMap[halfMapSize + i] - 0.01; |
| 260 | bandwidthAmplitudeMap[halfMapSize - i - 1] = |
| 261 | bandwidthAmplitudeMap[halfMapSize - i] - 0.01; |
| 262 | } |
| 263 | *_aidl_return = bandwidthAmplitudeMap; |
| 264 | return ndk::ScopedAStatus::ok(); |
| 265 | } else { |
| 266 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 267 | } |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | ndk::ScopedAStatus Vibrator::getPwlePrimitiveDurationMax(int32_t *durationMs) { |
| 271 | *durationMs = COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS; |
| 272 | return ndk::ScopedAStatus::ok(); |
| 273 | } |
| 274 | |
| 275 | ndk::ScopedAStatus Vibrator::getPwleCompositionSizeMax(int32_t *maxSize) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 276 | *maxSize = COMPOSE_PWLE_SIZE_MAX; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 277 | return ndk::ScopedAStatus::ok(); |
| 278 | } |
| 279 | |
| 280 | ndk::ScopedAStatus Vibrator::getSupportedBraking(std::vector<Braking> *supported) { |
| 281 | *supported = { |
| 282 | Braking::NONE, |
| 283 | Braking::CLAB, |
| 284 | }; |
| 285 | return ndk::ScopedAStatus::ok(); |
| 286 | } |
| 287 | |
| 288 | void resetPreviousEndAmplitudeEndFrequency(float &prevEndAmplitude, float &prevEndFrequency) { |
| 289 | const float reset = -1.0; |
| 290 | prevEndAmplitude = reset; |
| 291 | prevEndFrequency = reset; |
| 292 | } |
| 293 | |
| 294 | void incrementIndex(int &index) { |
| 295 | index += 1; |
| 296 | } |
| 297 | |
| 298 | void constructActiveDefaults(std::ostringstream &pwleBuilder, const int &segmentIdx) { |
| 299 | pwleBuilder << ",C" << segmentIdx << ":1"; |
| 300 | pwleBuilder << ",B" << segmentIdx << ":0"; |
| 301 | pwleBuilder << ",AR" << segmentIdx << ":0"; |
| 302 | pwleBuilder << ",V" << segmentIdx << ":0"; |
| 303 | } |
| 304 | |
| 305 | void constructActiveSegment(std::ostringstream &pwleBuilder, const int &segmentIdx, int duration, |
| 306 | float amplitude, float frequency) { |
| 307 | pwleBuilder << ",T" << segmentIdx << ":" << duration; |
| 308 | pwleBuilder << ",L" << segmentIdx << ":" << amplitude; |
| 309 | pwleBuilder << ",F" << segmentIdx << ":" << frequency; |
| 310 | constructActiveDefaults(pwleBuilder, segmentIdx); |
| 311 | } |
| 312 | |
| 313 | void constructBrakingSegment(std::ostringstream &pwleBuilder, const int &segmentIdx, int duration, |
| 314 | Braking brakingType) { |
| 315 | pwleBuilder << ",T" << segmentIdx << ":" << duration; |
| 316 | pwleBuilder << ",L" << segmentIdx << ":" << 0; |
| 317 | pwleBuilder << ",F" << segmentIdx << ":" << 0; |
| 318 | pwleBuilder << ",C" << segmentIdx << ":0"; |
| 319 | pwleBuilder << ",B" << segmentIdx << ":" |
| 320 | << static_cast<std::underlying_type<Braking>::type>(brakingType); |
| 321 | pwleBuilder << ",AR" << segmentIdx << ":0"; |
| 322 | pwleBuilder << ",V" << segmentIdx << ":0"; |
| 323 | } |
| 324 | |
| 325 | ndk::ScopedAStatus Vibrator::composePwle(const std::vector<PrimitivePwle> &composite, |
| 326 | const std::shared_ptr<IVibratorCallback> &callback) { |
| 327 | std::ostringstream pwleBuilder; |
| 328 | std::string pwleQueue; |
| 329 | |
| 330 | int compositionSizeMax; |
| 331 | getPwleCompositionSizeMax(&compositionSizeMax); |
| 332 | if (composite.size() <= 0 || composite.size() > compositionSizeMax) { |
| 333 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 334 | } |
| 335 | |
| 336 | float prevEndAmplitude; |
| 337 | float prevEndFrequency; |
| 338 | resetPreviousEndAmplitudeEndFrequency(prevEndAmplitude, prevEndFrequency); |
| 339 | |
| 340 | int segmentIdx = 0; |
| 341 | uint32_t totalDuration = 0; |
| 342 | |
| 343 | pwleBuilder << "S:0,WF:4,RP:0,WT:0"; |
| 344 | |
| 345 | for (auto &e : composite) { |
| 346 | switch (e.getTag()) { |
| 347 | case PrimitivePwle::active: { |
| 348 | auto active = e.get<PrimitivePwle::active>(); |
| 349 | if (active.duration < 0 || |
| 350 | active.duration > COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS) { |
| 351 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 352 | } |
| 353 | if (active.startAmplitude < PWLE_LEVEL_MIN || |
| 354 | active.startAmplitude > PWLE_LEVEL_MAX || |
| 355 | active.endAmplitude < PWLE_LEVEL_MIN || active.endAmplitude > PWLE_LEVEL_MAX) { |
| 356 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 357 | } |
| 358 | if (active.startFrequency < PWLE_FREQUENCY_MIN_HZ || |
| 359 | active.startFrequency > PWLE_FREQUENCY_MAX_HZ || |
| 360 | active.endFrequency < PWLE_FREQUENCY_MIN_HZ || |
| 361 | active.endFrequency > PWLE_FREQUENCY_MAX_HZ) { |
| 362 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 363 | } |
| 364 | |
| 365 | if (!((active.startAmplitude == prevEndAmplitude) && |
| 366 | (active.startFrequency == prevEndFrequency))) { |
| 367 | constructActiveSegment(pwleBuilder, segmentIdx, 0, active.startAmplitude, |
| 368 | active.startFrequency); |
| 369 | incrementIndex(segmentIdx); |
| 370 | } |
| 371 | |
| 372 | constructActiveSegment(pwleBuilder, segmentIdx, active.duration, |
| 373 | active.endAmplitude, active.endFrequency); |
| 374 | incrementIndex(segmentIdx); |
| 375 | |
| 376 | prevEndAmplitude = active.endAmplitude; |
| 377 | prevEndFrequency = active.endFrequency; |
| 378 | totalDuration += active.duration; |
| 379 | break; |
| 380 | } |
| 381 | case PrimitivePwle::braking: { |
| 382 | auto braking = e.get<PrimitivePwle::braking>(); |
| 383 | if (braking.braking > Braking::CLAB) { |
| 384 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 385 | } |
| 386 | if (braking.duration > COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS) { |
| 387 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 388 | } |
| 389 | |
| 390 | constructBrakingSegment(pwleBuilder, segmentIdx, 0, braking.braking); |
| 391 | incrementIndex(segmentIdx); |
| 392 | |
| 393 | constructBrakingSegment(pwleBuilder, segmentIdx, braking.duration, braking.braking); |
| 394 | incrementIndex(segmentIdx); |
| 395 | |
| 396 | resetPreviousEndAmplitudeEndFrequency(prevEndAmplitude, prevEndFrequency); |
| 397 | totalDuration += braking.duration; |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
Simon Bowden | 608655b | 2022-06-01 12:09:41 +0000 | [diff] [blame] | 403 | std::thread([totalDuration, callback] { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 404 | LOG(VERBOSE) << "Starting composePwle on another thread"; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 405 | usleep(totalDuration * 1000); |
| 406 | if (callback != nullptr) { |
chasewu | 8af5e84 | 2022-03-22 01:42:24 +0800 | [diff] [blame] | 407 | LOG(VERBOSE) << "Notifying compose PWLE complete"; |
Vince Leung | 823cf5f | 2021-02-11 02:21:57 +0000 | [diff] [blame] | 408 | callback->onComplete(); |
| 409 | } |
| 410 | }).detach(); |
| 411 | |
| 412 | return ndk::ScopedAStatus::ok(); |
| 413 | } |
| 414 | |
Steven Moreland | d44007e | 2019-10-24 18:12:46 -0700 | [diff] [blame] | 415 | } // namespace vibrator |
| 416 | } // namespace hardware |
| 417 | } // namespace android |
| 418 | } // namespace aidl |