blob: acf7d34266072e2df9bd6c6e8155998b37df12ed [file] [log] [blame]
Steven Morelandd44007e2019-10-24 18:12:46 -07001/*
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 Moreland8ba8c032019-11-18 16:23:39 -080017#include "vibrator-impl/Vibrator.h"
Steven Morelandd44007e2019-10-24 18:12:46 -070018
19#include <android-base/logging.h>
20#include <thread>
21
22namespace aidl {
23namespace android {
24namespace hardware {
25namespace vibrator {
26
chasewu8af5e842022-03-22 01:42:24 +080027static constexpr int32_t COMPOSE_DELAY_MAX_MS = 1000;
28static constexpr int32_t COMPOSE_SIZE_MAX = 256;
29static constexpr int32_t COMPOSE_PWLE_SIZE_MAX = 127;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090030
chasewu8af5e842022-03-22 01:42:24 +080031static constexpr float Q_FACTOR = 11.0;
Vince Leung823cf5f2021-02-11 02:21:57 +000032static constexpr int32_t COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS = 16383;
33static constexpr float PWLE_LEVEL_MIN = 0.0;
chasewu8af5e842022-03-22 01:42:24 +080034static constexpr float PWLE_LEVEL_MAX = 1.0;
Vince Leung823cf5f2021-02-11 02:21:57 +000035static constexpr float PWLE_FREQUENCY_RESOLUTION_HZ = 1.0;
36static constexpr float PWLE_FREQUENCY_MIN_HZ = 140.0;
chasewu8af5e842022-03-22 01:42:24 +080037static constexpr float RESONANT_FREQUENCY_HZ = 150.0;
Vince Leung823cf5f2021-02-11 02:21:57 +000038static constexpr float PWLE_FREQUENCY_MAX_HZ = 160.0;
chasewu8af5e842022-03-22 01:42:24 +080039static constexpr float PWLE_BW_MAP_SIZE =
40 1 + ((PWLE_FREQUENCY_MAX_HZ - PWLE_FREQUENCY_MIN_HZ) / PWLE_FREQUENCY_RESOLUTION_HZ);
Vince Leung4bae4f92021-02-03 06:21:58 +000041
Lais Andradea3c332f2024-06-20 10:46:06 +010042// Service specific error code used for vendor vibration effects.
43static constexpr int32_t ERROR_CODE_INVALID_DURATION = 1;
44
Steven Morelandd44007e2019-10-24 18:12:46 -070045ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) {
chasewu8af5e842022-03-22 01:42:24 +080046 LOG(VERBOSE) << "Vibrator reporting capabilities";
Steven Morelandd44007e2019-10-24 18:12:46 -070047 *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK |
Steven Morelandc0b92d52019-11-05 13:31:41 -080048 IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL |
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +090049 IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS |
Vince Leung4bae4f92021-02-03 06:21:58 +000050 IVibrator::CAP_ALWAYS_ON_CONTROL | IVibrator::CAP_GET_RESONANT_FREQUENCY |
Vince Leung823cf5f2021-02-11 02:21:57 +000051 IVibrator::CAP_GET_Q_FACTOR | IVibrator::CAP_FREQUENCY_CONTROL |
Lais Andradea3c332f2024-06-20 10:46:06 +010052 IVibrator::CAP_COMPOSE_PWLE_EFFECTS | IVibrator::CAP_PERFORM_VENDOR_EFFECTS;
Steven Morelandd44007e2019-10-24 18:12:46 -070053 return ndk::ScopedAStatus::ok();
54}
55
56ndk::ScopedAStatus Vibrator::off() {
chasewu8af5e842022-03-22 01:42:24 +080057 LOG(VERBOSE) << "Vibrator off";
Steven Morelandd44007e2019-10-24 18:12:46 -070058 return ndk::ScopedAStatus::ok();
59}
60
61ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs,
62 const std::shared_ptr<IVibratorCallback>& callback) {
chasewu8af5e842022-03-22 01:42:24 +080063 LOG(VERBOSE) << "Vibrator on for timeoutMs: " << timeoutMs;
Steven Morelandd44007e2019-10-24 18:12:46 -070064 if (callback != nullptr) {
Simon Bowden608655b2022-06-01 12:09:41 +000065 // Note that thread lambdas aren't using implicit capture [=], to avoid capturing "this",
66 // which may be asynchronously destructed.
67 // If "this" is needed, use [sharedThis = this->ref<Vibrator>()].
68 std::thread([timeoutMs, callback] {
chasewu8af5e842022-03-22 01:42:24 +080069 LOG(VERBOSE) << "Starting on on another thread";
Steven Morelandd44007e2019-10-24 18:12:46 -070070 usleep(timeoutMs * 1000);
chasewu8af5e842022-03-22 01:42:24 +080071 LOG(VERBOSE) << "Notifying on complete";
Steven Morelandf57ad9b2019-11-27 16:04:52 -080072 if (!callback->onComplete().isOk()) {
73 LOG(ERROR) << "Failed to call onComplete";
74 }
Steven Morelandd44007e2019-10-24 18:12:46 -070075 }).detach();
76 }
77 return ndk::ScopedAStatus::ok();
78}
79
80ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength,
81 const std::shared_ptr<IVibratorCallback>& callback,
82 int32_t* _aidl_return) {
chasewu8af5e842022-03-22 01:42:24 +080083 LOG(VERBOSE) << "Vibrator perform";
Steven Morelandd44007e2019-10-24 18:12:46 -070084
85 if (effect != Effect::CLICK && effect != Effect::TICK) {
86 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
87 }
88 if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM &&
89 strength != EffectStrength::STRONG) {
90 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
91 }
92
93 constexpr size_t kEffectMillis = 100;
94
95 if (callback != nullptr) {
Simon Bowden608655b2022-06-01 12:09:41 +000096 std::thread([callback] {
chasewu8af5e842022-03-22 01:42:24 +080097 LOG(VERBOSE) << "Starting perform on another thread";
Steven Morelandd44007e2019-10-24 18:12:46 -070098 usleep(kEffectMillis * 1000);
chasewu8af5e842022-03-22 01:42:24 +080099 LOG(VERBOSE) << "Notifying perform complete";
Steven Morelandd44007e2019-10-24 18:12:46 -0700100 callback->onComplete();
101 }).detach();
102 }
103
104 *_aidl_return = kEffectMillis;
105 return ndk::ScopedAStatus::ok();
106}
107
Lais Andradea3c332f2024-06-20 10:46:06 +0100108ndk::ScopedAStatus Vibrator::performVendorEffect(
109 const VendorEffect& effect, const std::shared_ptr<IVibratorCallback>& callback) {
110 LOG(VERBOSE) << "Vibrator perform vendor effect";
111 EffectStrength strength = effect.strength;
112 if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM &&
113 strength != EffectStrength::STRONG) {
114 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
115 }
116 float scale = effect.scale;
117 if (scale <= 0) {
118 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
119 }
120
121 int32_t durationMs = 0;
122 if (!effect.vendorData.getInt("DURATION_MS", &durationMs) || durationMs <= 0) {
123 return ndk::ScopedAStatus::fromServiceSpecificError(ERROR_CODE_INVALID_DURATION);
124 }
125
126 if (callback != nullptr) {
127 std::thread([callback, durationMs] {
128 LOG(VERBOSE) << "Starting perform on another thread for durationMs:" << durationMs;
129 usleep(durationMs * 1000);
130 LOG(VERBOSE) << "Notifying perform vendor effect complete";
131 callback->onComplete();
132 }).detach();
133 }
134
135 return ndk::ScopedAStatus::ok();
136}
137
Steven Moreland2932b222019-11-05 14:30:17 -0800138ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
139 *_aidl_return = {Effect::CLICK, Effect::TICK};
140 return ndk::ScopedAStatus::ok();
141}
142
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900143ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) {
chasewu8af5e842022-03-22 01:42:24 +0800144 LOG(VERBOSE) << "Vibrator set amplitude: " << amplitude;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900145 if (amplitude <= 0.0f || amplitude > 1.0f) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700146 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
147 }
148 return ndk::ScopedAStatus::ok();
149}
150
151ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) {
chasewu8af5e842022-03-22 01:42:24 +0800152 LOG(VERBOSE) << "Vibrator set external control: " << enabled;
Steven Morelandd44007e2019-10-24 18:12:46 -0700153 return ndk::ScopedAStatus::ok();
154}
155
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900156ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) {
chasewu8af5e842022-03-22 01:42:24 +0800157 *maxDelayMs = COMPOSE_DELAY_MAX_MS;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900158 return ndk::ScopedAStatus::ok();
159}
160
161ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) {
chasewu8af5e842022-03-22 01:42:24 +0800162 *maxSize = COMPOSE_SIZE_MAX;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900163 return ndk::ScopedAStatus::ok();
164}
165
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900166ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector<CompositePrimitive>* supported) {
167 *supported = {
168 CompositePrimitive::NOOP, CompositePrimitive::CLICK,
169 CompositePrimitive::THUD, CompositePrimitive::SPIN,
170 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
Harpreet \"Eli\" Sanghafe5d3982020-01-17 14:46:37 +0900171 CompositePrimitive::QUICK_FALL, CompositePrimitive::LIGHT_TICK,
Vince Leungdeb46ec2020-12-22 00:03:16 +0000172 CompositePrimitive::LOW_TICK,
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900173 };
174 return ndk::ScopedAStatus::ok();
175}
176
177ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive primitive,
178 int32_t* durationMs) {
Lais Andrade3c7f0d92021-06-16 10:20:09 +0000179 std::vector<CompositePrimitive> supported;
180 getSupportedPrimitives(&supported);
181 if (std::find(supported.begin(), supported.end(), primitive) == supported.end()) {
182 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
183 }
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900184 if (primitive != CompositePrimitive::NOOP) {
185 *durationMs = 100;
186 } else {
187 *durationMs = 0;
188 }
189 return ndk::ScopedAStatus::ok();
190}
191
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900192ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite,
193 const std::shared_ptr<IVibratorCallback>& callback) {
chasewu8af5e842022-03-22 01:42:24 +0800194 if (composite.size() > COMPOSE_SIZE_MAX) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900195 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
196 }
197
Harpreet \"Eli\" Sangha13ef28c2020-01-23 13:22:07 +0900198 std::vector<CompositePrimitive> supported;
199 getSupportedPrimitives(&supported);
200
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900201 for (auto& e : composite) {
chasewu8af5e842022-03-22 01:42:24 +0800202 if (e.delayMs > COMPOSE_DELAY_MAX_MS) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900203 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
204 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900205 if (e.scale < 0.0f || e.scale > 1.0f) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900206 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
207 }
Harpreet \"Eli\" Sangha13ef28c2020-01-23 13:22:07 +0900208 if (std::find(supported.begin(), supported.end(), e.primitive) == supported.end()) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900209 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
210 }
211 }
212
Simon Bowden608655b2022-06-01 12:09:41 +0000213 // The thread may theoretically outlive the vibrator, so take a proper reference to it.
214 std::thread([sharedThis = this->ref<Vibrator>(), composite, callback] {
chasewu8af5e842022-03-22 01:42:24 +0800215 LOG(VERBOSE) << "Starting compose on another thread";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900216
217 for (auto& e : composite) {
218 if (e.delayMs) {
219 usleep(e.delayMs * 1000);
220 }
chasewu8af5e842022-03-22 01:42:24 +0800221 LOG(VERBOSE) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale "
222 << e.scale;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900223
224 int32_t durationMs;
Simon Bowden608655b2022-06-01 12:09:41 +0000225 sharedThis->getPrimitiveDuration(e.primitive, &durationMs);
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900226 usleep(durationMs * 1000);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900227 }
228
229 if (callback != nullptr) {
chasewu8af5e842022-03-22 01:42:24 +0800230 LOG(VERBOSE) << "Notifying perform complete";
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900231 callback->onComplete();
232 }
233 }).detach();
234
235 return ndk::ScopedAStatus::ok();
236}
237
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900238ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect>* _aidl_return) {
239 return getSupportedEffects(_aidl_return);
240}
241
242ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
243 std::vector<Effect> effects;
244 getSupportedAlwaysOnEffects(&effects);
245
246 if (std::find(effects.begin(), effects.end(), effect) == effects.end()) {
247 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
248 } else {
chasewu8af5e842022-03-22 01:42:24 +0800249 LOG(VERBOSE) << "Enabling always-on ID " << id << " with " << toString(effect) << "/"
250 << toString(strength);
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900251 return ndk::ScopedAStatus::ok();
252 }
253}
254
255ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t id) {
chasewu8af5e842022-03-22 01:42:24 +0800256 LOG(VERBOSE) << "Disabling always-on ID " << id;
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900257 return ndk::ScopedAStatus::ok();
258}
259
Vince Leung4bae4f92021-02-03 06:21:58 +0000260ndk::ScopedAStatus Vibrator::getResonantFrequency(float *resonantFreqHz) {
chasewu8af5e842022-03-22 01:42:24 +0800261 *resonantFreqHz = RESONANT_FREQUENCY_HZ;
Vince Leung4bae4f92021-02-03 06:21:58 +0000262 return ndk::ScopedAStatus::ok();
263}
264
265ndk::ScopedAStatus Vibrator::getQFactor(float *qFactor) {
chasewu8af5e842022-03-22 01:42:24 +0800266 *qFactor = Q_FACTOR;
Vince Leung4bae4f92021-02-03 06:21:58 +0000267 return ndk::ScopedAStatus::ok();
268}
269
Vince Leung823cf5f2021-02-11 02:21:57 +0000270ndk::ScopedAStatus Vibrator::getFrequencyResolution(float *freqResolutionHz) {
271 *freqResolutionHz = PWLE_FREQUENCY_RESOLUTION_HZ;
272 return ndk::ScopedAStatus::ok();
273}
274
275ndk::ScopedAStatus Vibrator::getFrequencyMinimum(float *freqMinimumHz) {
276 *freqMinimumHz = PWLE_FREQUENCY_MIN_HZ;
277 return ndk::ScopedAStatus::ok();
278}
279
280ndk::ScopedAStatus Vibrator::getBandwidthAmplitudeMap(std::vector<float> *_aidl_return) {
chasewu8af5e842022-03-22 01:42:24 +0800281 // The output BandwidthAmplitudeMap will be as below and the maximum
282 // amplitude 1.0 will be set on RESONANT_FREQUENCY_HZ
283 // {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,
284 // 0.96, 0.95, 0.94, 0.93, 0.92, 0.91, 0.9}
285 int32_t capabilities = 0;
286 int halfMapSize = PWLE_BW_MAP_SIZE / 2;
287 Vibrator::getCapabilities(&capabilities);
288 if (capabilities & IVibrator::CAP_FREQUENCY_CONTROL) {
289 std::vector<float> bandwidthAmplitudeMap(PWLE_BW_MAP_SIZE, PWLE_LEVEL_MAX);
290 for (int i = 0; i < halfMapSize; ++i) {
291 bandwidthAmplitudeMap[halfMapSize + i + 1] =
292 bandwidthAmplitudeMap[halfMapSize + i] - 0.01;
293 bandwidthAmplitudeMap[halfMapSize - i - 1] =
294 bandwidthAmplitudeMap[halfMapSize - i] - 0.01;
295 }
296 *_aidl_return = bandwidthAmplitudeMap;
297 return ndk::ScopedAStatus::ok();
298 } else {
299 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
300 }
Vince Leung823cf5f2021-02-11 02:21:57 +0000301}
302
303ndk::ScopedAStatus Vibrator::getPwlePrimitiveDurationMax(int32_t *durationMs) {
304 *durationMs = COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS;
305 return ndk::ScopedAStatus::ok();
306}
307
308ndk::ScopedAStatus Vibrator::getPwleCompositionSizeMax(int32_t *maxSize) {
chasewu8af5e842022-03-22 01:42:24 +0800309 *maxSize = COMPOSE_PWLE_SIZE_MAX;
Vince Leung823cf5f2021-02-11 02:21:57 +0000310 return ndk::ScopedAStatus::ok();
311}
312
313ndk::ScopedAStatus Vibrator::getSupportedBraking(std::vector<Braking> *supported) {
314 *supported = {
315 Braking::NONE,
316 Braking::CLAB,
317 };
318 return ndk::ScopedAStatus::ok();
319}
320
321void resetPreviousEndAmplitudeEndFrequency(float &prevEndAmplitude, float &prevEndFrequency) {
322 const float reset = -1.0;
323 prevEndAmplitude = reset;
324 prevEndFrequency = reset;
325}
326
327void incrementIndex(int &index) {
328 index += 1;
329}
330
331void constructActiveDefaults(std::ostringstream &pwleBuilder, const int &segmentIdx) {
332 pwleBuilder << ",C" << segmentIdx << ":1";
333 pwleBuilder << ",B" << segmentIdx << ":0";
334 pwleBuilder << ",AR" << segmentIdx << ":0";
335 pwleBuilder << ",V" << segmentIdx << ":0";
336}
337
338void constructActiveSegment(std::ostringstream &pwleBuilder, const int &segmentIdx, int duration,
339 float amplitude, float frequency) {
340 pwleBuilder << ",T" << segmentIdx << ":" << duration;
341 pwleBuilder << ",L" << segmentIdx << ":" << amplitude;
342 pwleBuilder << ",F" << segmentIdx << ":" << frequency;
343 constructActiveDefaults(pwleBuilder, segmentIdx);
344}
345
346void constructBrakingSegment(std::ostringstream &pwleBuilder, const int &segmentIdx, int duration,
347 Braking brakingType) {
348 pwleBuilder << ",T" << segmentIdx << ":" << duration;
349 pwleBuilder << ",L" << segmentIdx << ":" << 0;
350 pwleBuilder << ",F" << segmentIdx << ":" << 0;
351 pwleBuilder << ",C" << segmentIdx << ":0";
352 pwleBuilder << ",B" << segmentIdx << ":"
353 << static_cast<std::underlying_type<Braking>::type>(brakingType);
354 pwleBuilder << ",AR" << segmentIdx << ":0";
355 pwleBuilder << ",V" << segmentIdx << ":0";
356}
357
358ndk::ScopedAStatus Vibrator::composePwle(const std::vector<PrimitivePwle> &composite,
359 const std::shared_ptr<IVibratorCallback> &callback) {
360 std::ostringstream pwleBuilder;
361 std::string pwleQueue;
362
363 int compositionSizeMax;
364 getPwleCompositionSizeMax(&compositionSizeMax);
365 if (composite.size() <= 0 || composite.size() > compositionSizeMax) {
366 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
367 }
368
369 float prevEndAmplitude;
370 float prevEndFrequency;
371 resetPreviousEndAmplitudeEndFrequency(prevEndAmplitude, prevEndFrequency);
372
373 int segmentIdx = 0;
374 uint32_t totalDuration = 0;
375
376 pwleBuilder << "S:0,WF:4,RP:0,WT:0";
377
378 for (auto &e : composite) {
379 switch (e.getTag()) {
380 case PrimitivePwle::active: {
381 auto active = e.get<PrimitivePwle::active>();
382 if (active.duration < 0 ||
383 active.duration > COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS) {
384 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
385 }
386 if (active.startAmplitude < PWLE_LEVEL_MIN ||
387 active.startAmplitude > PWLE_LEVEL_MAX ||
388 active.endAmplitude < PWLE_LEVEL_MIN || active.endAmplitude > PWLE_LEVEL_MAX) {
389 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
390 }
391 if (active.startFrequency < PWLE_FREQUENCY_MIN_HZ ||
392 active.startFrequency > PWLE_FREQUENCY_MAX_HZ ||
393 active.endFrequency < PWLE_FREQUENCY_MIN_HZ ||
394 active.endFrequency > PWLE_FREQUENCY_MAX_HZ) {
395 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
396 }
397
398 if (!((active.startAmplitude == prevEndAmplitude) &&
399 (active.startFrequency == prevEndFrequency))) {
400 constructActiveSegment(pwleBuilder, segmentIdx, 0, active.startAmplitude,
401 active.startFrequency);
402 incrementIndex(segmentIdx);
403 }
404
405 constructActiveSegment(pwleBuilder, segmentIdx, active.duration,
406 active.endAmplitude, active.endFrequency);
407 incrementIndex(segmentIdx);
408
409 prevEndAmplitude = active.endAmplitude;
410 prevEndFrequency = active.endFrequency;
411 totalDuration += active.duration;
412 break;
413 }
414 case PrimitivePwle::braking: {
415 auto braking = e.get<PrimitivePwle::braking>();
416 if (braking.braking > Braking::CLAB) {
417 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
418 }
419 if (braking.duration > COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS) {
420 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
421 }
422
423 constructBrakingSegment(pwleBuilder, segmentIdx, 0, braking.braking);
424 incrementIndex(segmentIdx);
425
426 constructBrakingSegment(pwleBuilder, segmentIdx, braking.duration, braking.braking);
427 incrementIndex(segmentIdx);
428
429 resetPreviousEndAmplitudeEndFrequency(prevEndAmplitude, prevEndFrequency);
430 totalDuration += braking.duration;
431 break;
432 }
433 }
434 }
435
Simon Bowden608655b2022-06-01 12:09:41 +0000436 std::thread([totalDuration, callback] {
chasewu8af5e842022-03-22 01:42:24 +0800437 LOG(VERBOSE) << "Starting composePwle on another thread";
Vince Leung823cf5f2021-02-11 02:21:57 +0000438 usleep(totalDuration * 1000);
439 if (callback != nullptr) {
chasewu8af5e842022-03-22 01:42:24 +0800440 LOG(VERBOSE) << "Notifying compose PWLE complete";
Vince Leung823cf5f2021-02-11 02:21:57 +0000441 callback->onComplete();
442 }
443 }).detach();
444
445 return ndk::ScopedAStatus::ok();
446}
447
Steven Morelandd44007e2019-10-24 18:12:46 -0700448} // namespace vibrator
449} // namespace hardware
450} // namespace android
451} // namespace aidl