blob: 5755ce55b60d85b9cdf480c1eb87a76efa6af563 [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
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090027static constexpr int32_t kComposeDelayMaxMs = 1000;
28static constexpr int32_t kComposeSizeMax = 256;
Vince Leung823cf5f2021-02-11 02:21:57 +000029static constexpr int32_t kComposePwleSizeMax = 127;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090030
Vince Leung4bae4f92021-02-03 06:21:58 +000031static constexpr float kResonantFrequency = 150.0;
32static constexpr float kQFactor = 11.0;
Vince Leung823cf5f2021-02-11 02:21:57 +000033static constexpr int32_t COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS = 16383;
34static constexpr float PWLE_LEVEL_MIN = 0.0;
35static constexpr float PWLE_LEVEL_MAX = 0.98256;
36static constexpr float PWLE_FREQUENCY_RESOLUTION_HZ = 1.0;
37static constexpr float PWLE_FREQUENCY_MIN_HZ = 140.0;
38static constexpr float PWLE_FREQUENCY_MAX_HZ = 160.0;
Vince Leung4bae4f92021-02-03 06:21:58 +000039
Steven Morelandd44007e2019-10-24 18:12:46 -070040ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) {
41 LOG(INFO) << "Vibrator reporting capabilities";
42 *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK |
Steven Morelandc0b92d52019-11-05 13:31:41 -080043 IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL |
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +090044 IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS |
Vince Leung4bae4f92021-02-03 06:21:58 +000045 IVibrator::CAP_ALWAYS_ON_CONTROL | IVibrator::CAP_GET_RESONANT_FREQUENCY |
Vince Leung823cf5f2021-02-11 02:21:57 +000046 IVibrator::CAP_GET_Q_FACTOR | IVibrator::CAP_FREQUENCY_CONTROL |
47 IVibrator::CAP_COMPOSE_PWLE_EFFECTS;
Steven Morelandd44007e2019-10-24 18:12:46 -070048 return ndk::ScopedAStatus::ok();
49}
50
51ndk::ScopedAStatus Vibrator::off() {
52 LOG(INFO) << "Vibrator off";
53 return ndk::ScopedAStatus::ok();
54}
55
56ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs,
57 const std::shared_ptr<IVibratorCallback>& callback) {
58 LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs;
59 if (callback != nullptr) {
60 std::thread([=] {
61 LOG(INFO) << "Starting on on another thread";
62 usleep(timeoutMs * 1000);
63 LOG(INFO) << "Notifying on complete";
Steven Morelandf57ad9b2019-11-27 16:04:52 -080064 if (!callback->onComplete().isOk()) {
65 LOG(ERROR) << "Failed to call onComplete";
66 }
Steven Morelandd44007e2019-10-24 18:12:46 -070067 }).detach();
68 }
69 return ndk::ScopedAStatus::ok();
70}
71
72ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength,
73 const std::shared_ptr<IVibratorCallback>& callback,
74 int32_t* _aidl_return) {
75 LOG(INFO) << "Vibrator perform";
76
77 if (effect != Effect::CLICK && effect != Effect::TICK) {
78 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
79 }
80 if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM &&
81 strength != EffectStrength::STRONG) {
82 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
83 }
84
85 constexpr size_t kEffectMillis = 100;
86
87 if (callback != nullptr) {
88 std::thread([=] {
89 LOG(INFO) << "Starting perform on another thread";
90 usleep(kEffectMillis * 1000);
91 LOG(INFO) << "Notifying perform complete";
92 callback->onComplete();
93 }).detach();
94 }
95
96 *_aidl_return = kEffectMillis;
97 return ndk::ScopedAStatus::ok();
98}
99
Steven Moreland2932b222019-11-05 14:30:17 -0800100ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
101 *_aidl_return = {Effect::CLICK, Effect::TICK};
102 return ndk::ScopedAStatus::ok();
103}
104
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900105ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700106 LOG(INFO) << "Vibrator set amplitude: " << amplitude;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900107 if (amplitude <= 0.0f || amplitude > 1.0f) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700108 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
109 }
110 return ndk::ScopedAStatus::ok();
111}
112
113ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) {
114 LOG(INFO) << "Vibrator set external control: " << enabled;
115 return ndk::ScopedAStatus::ok();
116}
117
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900118ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) {
119 *maxDelayMs = kComposeDelayMaxMs;
120 return ndk::ScopedAStatus::ok();
121}
122
123ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) {
124 *maxSize = kComposeSizeMax;
125 return ndk::ScopedAStatus::ok();
126}
127
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900128ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector<CompositePrimitive>* supported) {
129 *supported = {
130 CompositePrimitive::NOOP, CompositePrimitive::CLICK,
131 CompositePrimitive::THUD, CompositePrimitive::SPIN,
132 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
Harpreet \"Eli\" Sanghafe5d3982020-01-17 14:46:37 +0900133 CompositePrimitive::QUICK_FALL, CompositePrimitive::LIGHT_TICK,
Vince Leungdeb46ec2020-12-22 00:03:16 +0000134 CompositePrimitive::LOW_TICK,
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900135 };
136 return ndk::ScopedAStatus::ok();
137}
138
139ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive primitive,
140 int32_t* durationMs) {
Lais Andrade3c7f0d92021-06-16 10:20:09 +0000141 std::vector<CompositePrimitive> supported;
142 getSupportedPrimitives(&supported);
143 if (std::find(supported.begin(), supported.end(), primitive) == supported.end()) {
144 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
145 }
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900146 if (primitive != CompositePrimitive::NOOP) {
147 *durationMs = 100;
148 } else {
149 *durationMs = 0;
150 }
151 return ndk::ScopedAStatus::ok();
152}
153
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900154ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite,
155 const std::shared_ptr<IVibratorCallback>& callback) {
156 if (composite.size() > kComposeSizeMax) {
157 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
158 }
159
Harpreet \"Eli\" Sangha13ef28c2020-01-23 13:22:07 +0900160 std::vector<CompositePrimitive> supported;
161 getSupportedPrimitives(&supported);
162
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900163 for (auto& e : composite) {
164 if (e.delayMs > kComposeDelayMaxMs) {
165 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
166 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900167 if (e.scale < 0.0f || e.scale > 1.0f) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900168 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
169 }
Harpreet \"Eli\" Sangha13ef28c2020-01-23 13:22:07 +0900170 if (std::find(supported.begin(), supported.end(), e.primitive) == supported.end()) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900171 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
172 }
173 }
174
175 std::thread([=] {
176 LOG(INFO) << "Starting compose on another thread";
177
178 for (auto& e : composite) {
179 if (e.delayMs) {
180 usleep(e.delayMs * 1000);
181 }
182 LOG(INFO) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale "
183 << e.scale;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900184
185 int32_t durationMs;
186 getPrimitiveDuration(e.primitive, &durationMs);
187 usleep(durationMs * 1000);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900188 }
189
190 if (callback != nullptr) {
191 LOG(INFO) << "Notifying perform complete";
192 callback->onComplete();
193 }
194 }).detach();
195
196 return ndk::ScopedAStatus::ok();
197}
198
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900199ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect>* _aidl_return) {
200 return getSupportedEffects(_aidl_return);
201}
202
203ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
204 std::vector<Effect> effects;
205 getSupportedAlwaysOnEffects(&effects);
206
207 if (std::find(effects.begin(), effects.end(), effect) == effects.end()) {
208 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
209 } else {
210 LOG(INFO) << "Enabling always-on ID " << id << " with " << toString(effect) << "/"
211 << toString(strength);
212 return ndk::ScopedAStatus::ok();
213 }
214}
215
216ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t id) {
217 LOG(INFO) << "Disabling always-on ID " << id;
218 return ndk::ScopedAStatus::ok();
219}
220
Vince Leung4bae4f92021-02-03 06:21:58 +0000221ndk::ScopedAStatus Vibrator::getResonantFrequency(float *resonantFreqHz) {
222 *resonantFreqHz = kResonantFrequency;
223 return ndk::ScopedAStatus::ok();
224}
225
226ndk::ScopedAStatus Vibrator::getQFactor(float *qFactor) {
227 *qFactor = kQFactor;
228 return ndk::ScopedAStatus::ok();
229}
230
Vince Leung823cf5f2021-02-11 02:21:57 +0000231ndk::ScopedAStatus Vibrator::getFrequencyResolution(float *freqResolutionHz) {
232 *freqResolutionHz = PWLE_FREQUENCY_RESOLUTION_HZ;
233 return ndk::ScopedAStatus::ok();
234}
235
236ndk::ScopedAStatus Vibrator::getFrequencyMinimum(float *freqMinimumHz) {
237 *freqMinimumHz = PWLE_FREQUENCY_MIN_HZ;
238 return ndk::ScopedAStatus::ok();
239}
240
241ndk::ScopedAStatus Vibrator::getBandwidthAmplitudeMap(std::vector<float> *_aidl_return) {
242 // A valid array should be of size:
243 // (PWLE_FREQUENCY_MAX_HZ - PWLE_FREQUENCY_MIN_HZ) / PWLE_FREQUENCY_RESOLUTION_HZ
244 *_aidl_return = {0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.10,
245 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20};
246 return ndk::ScopedAStatus::ok();
247}
248
249ndk::ScopedAStatus Vibrator::getPwlePrimitiveDurationMax(int32_t *durationMs) {
250 *durationMs = COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS;
251 return ndk::ScopedAStatus::ok();
252}
253
254ndk::ScopedAStatus Vibrator::getPwleCompositionSizeMax(int32_t *maxSize) {
255 *maxSize = kComposePwleSizeMax;
256 return ndk::ScopedAStatus::ok();
257}
258
259ndk::ScopedAStatus Vibrator::getSupportedBraking(std::vector<Braking> *supported) {
260 *supported = {
261 Braking::NONE,
262 Braking::CLAB,
263 };
264 return ndk::ScopedAStatus::ok();
265}
266
267void resetPreviousEndAmplitudeEndFrequency(float &prevEndAmplitude, float &prevEndFrequency) {
268 const float reset = -1.0;
269 prevEndAmplitude = reset;
270 prevEndFrequency = reset;
271}
272
273void incrementIndex(int &index) {
274 index += 1;
275}
276
277void constructActiveDefaults(std::ostringstream &pwleBuilder, const int &segmentIdx) {
278 pwleBuilder << ",C" << segmentIdx << ":1";
279 pwleBuilder << ",B" << segmentIdx << ":0";
280 pwleBuilder << ",AR" << segmentIdx << ":0";
281 pwleBuilder << ",V" << segmentIdx << ":0";
282}
283
284void constructActiveSegment(std::ostringstream &pwleBuilder, const int &segmentIdx, int duration,
285 float amplitude, float frequency) {
286 pwleBuilder << ",T" << segmentIdx << ":" << duration;
287 pwleBuilder << ",L" << segmentIdx << ":" << amplitude;
288 pwleBuilder << ",F" << segmentIdx << ":" << frequency;
289 constructActiveDefaults(pwleBuilder, segmentIdx);
290}
291
292void constructBrakingSegment(std::ostringstream &pwleBuilder, const int &segmentIdx, int duration,
293 Braking brakingType) {
294 pwleBuilder << ",T" << segmentIdx << ":" << duration;
295 pwleBuilder << ",L" << segmentIdx << ":" << 0;
296 pwleBuilder << ",F" << segmentIdx << ":" << 0;
297 pwleBuilder << ",C" << segmentIdx << ":0";
298 pwleBuilder << ",B" << segmentIdx << ":"
299 << static_cast<std::underlying_type<Braking>::type>(brakingType);
300 pwleBuilder << ",AR" << segmentIdx << ":0";
301 pwleBuilder << ",V" << segmentIdx << ":0";
302}
303
304ndk::ScopedAStatus Vibrator::composePwle(const std::vector<PrimitivePwle> &composite,
305 const std::shared_ptr<IVibratorCallback> &callback) {
306 std::ostringstream pwleBuilder;
307 std::string pwleQueue;
308
309 int compositionSizeMax;
310 getPwleCompositionSizeMax(&compositionSizeMax);
311 if (composite.size() <= 0 || composite.size() > compositionSizeMax) {
312 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
313 }
314
315 float prevEndAmplitude;
316 float prevEndFrequency;
317 resetPreviousEndAmplitudeEndFrequency(prevEndAmplitude, prevEndFrequency);
318
319 int segmentIdx = 0;
320 uint32_t totalDuration = 0;
321
322 pwleBuilder << "S:0,WF:4,RP:0,WT:0";
323
324 for (auto &e : composite) {
325 switch (e.getTag()) {
326 case PrimitivePwle::active: {
327 auto active = e.get<PrimitivePwle::active>();
328 if (active.duration < 0 ||
329 active.duration > COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS) {
330 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
331 }
332 if (active.startAmplitude < PWLE_LEVEL_MIN ||
333 active.startAmplitude > PWLE_LEVEL_MAX ||
334 active.endAmplitude < PWLE_LEVEL_MIN || active.endAmplitude > PWLE_LEVEL_MAX) {
335 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
336 }
337 if (active.startFrequency < PWLE_FREQUENCY_MIN_HZ ||
338 active.startFrequency > PWLE_FREQUENCY_MAX_HZ ||
339 active.endFrequency < PWLE_FREQUENCY_MIN_HZ ||
340 active.endFrequency > PWLE_FREQUENCY_MAX_HZ) {
341 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
342 }
343
344 if (!((active.startAmplitude == prevEndAmplitude) &&
345 (active.startFrequency == prevEndFrequency))) {
346 constructActiveSegment(pwleBuilder, segmentIdx, 0, active.startAmplitude,
347 active.startFrequency);
348 incrementIndex(segmentIdx);
349 }
350
351 constructActiveSegment(pwleBuilder, segmentIdx, active.duration,
352 active.endAmplitude, active.endFrequency);
353 incrementIndex(segmentIdx);
354
355 prevEndAmplitude = active.endAmplitude;
356 prevEndFrequency = active.endFrequency;
357 totalDuration += active.duration;
358 break;
359 }
360 case PrimitivePwle::braking: {
361 auto braking = e.get<PrimitivePwle::braking>();
362 if (braking.braking > Braking::CLAB) {
363 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
364 }
365 if (braking.duration > COMPOSE_PWLE_PRIMITIVE_DURATION_MAX_MS) {
366 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
367 }
368
369 constructBrakingSegment(pwleBuilder, segmentIdx, 0, braking.braking);
370 incrementIndex(segmentIdx);
371
372 constructBrakingSegment(pwleBuilder, segmentIdx, braking.duration, braking.braking);
373 incrementIndex(segmentIdx);
374
375 resetPreviousEndAmplitudeEndFrequency(prevEndAmplitude, prevEndFrequency);
376 totalDuration += braking.duration;
377 break;
378 }
379 }
380 }
381
382 std::thread([=] {
383 LOG(INFO) << "Starting composePwle on another thread";
384 usleep(totalDuration * 1000);
385 if (callback != nullptr) {
386 LOG(INFO) << "Notifying compose PWLE complete";
387 callback->onComplete();
388 }
389 }).detach();
390
391 return ndk::ScopedAStatus::ok();
392}
393
Steven Morelandd44007e2019-10-24 18:12:46 -0700394} // namespace vibrator
395} // namespace hardware
396} // namespace android
397} // namespace aidl