blob: bf61bfec916436da55d0b6e5cb9bdfe98ab8ffdd [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;
29
Vince Leung4bae4f92021-02-03 06:21:58 +000030static constexpr float kResonantFrequency = 150.0;
31static constexpr float kQFactor = 11.0;
32
Steven Morelandd44007e2019-10-24 18:12:46 -070033ndk::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 Morelandc0b92d52019-11-05 13:31:41 -080036 IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL |
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +090037 IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS |
Vince Leung4bae4f92021-02-03 06:21:58 +000038 IVibrator::CAP_ALWAYS_ON_CONTROL | IVibrator::CAP_GET_RESONANT_FREQUENCY |
39 IVibrator::CAP_GET_Q_FACTOR;
Steven Morelandd44007e2019-10-24 18:12:46 -070040 return ndk::ScopedAStatus::ok();
41}
42
43ndk::ScopedAStatus Vibrator::off() {
44 LOG(INFO) << "Vibrator off";
45 return ndk::ScopedAStatus::ok();
46}
47
48ndk::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 Morelandf57ad9b2019-11-27 16:04:52 -080056 if (!callback->onComplete().isOk()) {
57 LOG(ERROR) << "Failed to call onComplete";
58 }
Steven Morelandd44007e2019-10-24 18:12:46 -070059 }).detach();
60 }
61 return ndk::ScopedAStatus::ok();
62}
63
64ndk::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 Moreland2932b222019-11-05 14:30:17 -080092ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
93 *_aidl_return = {Effect::CLICK, Effect::TICK};
94 return ndk::ScopedAStatus::ok();
95}
96
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090097ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) {
Steven Morelandd44007e2019-10-24 18:12:46 -070098 LOG(INFO) << "Vibrator set amplitude: " << amplitude;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090099 if (amplitude <= 0.0f || amplitude > 1.0f) {
Steven Morelandd44007e2019-10-24 18:12:46 -0700100 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
101 }
102 return ndk::ScopedAStatus::ok();
103}
104
105ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) {
106 LOG(INFO) << "Vibrator set external control: " << enabled;
107 return ndk::ScopedAStatus::ok();
108}
109
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900110ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) {
111 *maxDelayMs = kComposeDelayMaxMs;
112 return ndk::ScopedAStatus::ok();
113}
114
115ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) {
116 *maxSize = kComposeSizeMax;
117 return ndk::ScopedAStatus::ok();
118}
119
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900120ndk::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\" Sanghafe5d3982020-01-17 14:46:37 +0900125 CompositePrimitive::QUICK_FALL, CompositePrimitive::LIGHT_TICK,
Vince Leungdeb46ec2020-12-22 00:03:16 +0000126 CompositePrimitive::LOW_TICK,
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900127 };
128 return ndk::ScopedAStatus::ok();
129}
130
131ndk::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\" Sanghaf4de5b02019-10-23 09:25:52 +0900141ndk::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\" Sangha13ef28c2020-01-23 13:22:07 +0900147 std::vector<CompositePrimitive> supported;
148 getSupportedPrimitives(&supported);
149
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900150 for (auto& e : composite) {
151 if (e.delayMs > kComposeDelayMaxMs) {
152 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
153 }
Harpreet \"Eli\" Sangha7aec5022020-03-11 06:00:55 +0900154 if (e.scale < 0.0f || e.scale > 1.0f) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900155 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
156 }
Harpreet \"Eli\" Sangha13ef28c2020-01-23 13:22:07 +0900157 if (std::find(supported.begin(), supported.end(), e.primitive) == supported.end()) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900158 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\" Sanghab075a6a2020-04-10 15:11:58 +0900171
172 int32_t durationMs;
173 getPrimitiveDuration(e.primitive, &durationMs);
174 usleep(durationMs * 1000);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900175 }
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\" Sangha63624092019-09-09 11:04:54 +0900186ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect>* _aidl_return) {
187 return getSupportedEffects(_aidl_return);
188}
189
190ndk::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
203ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t id) {
204 LOG(INFO) << "Disabling always-on ID " << id;
205 return ndk::ScopedAStatus::ok();
206}
207
Vince Leung4bae4f92021-02-03 06:21:58 +0000208ndk::ScopedAStatus Vibrator::getResonantFrequency(float *resonantFreqHz) {
209 *resonantFreqHz = kResonantFrequency;
210 return ndk::ScopedAStatus::ok();
211}
212
213ndk::ScopedAStatus Vibrator::getQFactor(float *qFactor) {
214 *qFactor = kQFactor;
215 return ndk::ScopedAStatus::ok();
216}
217
Steven Morelandd44007e2019-10-24 18:12:46 -0700218} // namespace vibrator
219} // namespace hardware
220} // namespace android
221} // namespace aidl