blob: 1a8fd3bf2c8626f2c3b87a2512d157f00ae98d2d [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
Steven Morelandd44007e2019-10-24 18:12:46 -070030ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) {
31 LOG(INFO) << "Vibrator reporting capabilities";
32 *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK |
Steven Morelandc0b92d52019-11-05 13:31:41 -080033 IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL |
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +090034 IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS |
35 IVibrator::CAP_ALWAYS_ON_CONTROL;
Steven Morelandd44007e2019-10-24 18:12:46 -070036 return ndk::ScopedAStatus::ok();
37}
38
39ndk::ScopedAStatus Vibrator::off() {
40 LOG(INFO) << "Vibrator off";
41 return ndk::ScopedAStatus::ok();
42}
43
44ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs,
45 const std::shared_ptr<IVibratorCallback>& callback) {
46 LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs;
47 if (callback != nullptr) {
48 std::thread([=] {
49 LOG(INFO) << "Starting on on another thread";
50 usleep(timeoutMs * 1000);
51 LOG(INFO) << "Notifying on complete";
Steven Morelandf57ad9b2019-11-27 16:04:52 -080052 if (!callback->onComplete().isOk()) {
53 LOG(ERROR) << "Failed to call onComplete";
54 }
Steven Morelandd44007e2019-10-24 18:12:46 -070055 }).detach();
56 }
57 return ndk::ScopedAStatus::ok();
58}
59
60ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength,
61 const std::shared_ptr<IVibratorCallback>& callback,
62 int32_t* _aidl_return) {
63 LOG(INFO) << "Vibrator perform";
64
65 if (effect != Effect::CLICK && effect != Effect::TICK) {
66 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
67 }
68 if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM &&
69 strength != EffectStrength::STRONG) {
70 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
71 }
72
73 constexpr size_t kEffectMillis = 100;
74
75 if (callback != nullptr) {
76 std::thread([=] {
77 LOG(INFO) << "Starting perform on another thread";
78 usleep(kEffectMillis * 1000);
79 LOG(INFO) << "Notifying perform complete";
80 callback->onComplete();
81 }).detach();
82 }
83
84 *_aidl_return = kEffectMillis;
85 return ndk::ScopedAStatus::ok();
86}
87
Steven Moreland2932b222019-11-05 14:30:17 -080088ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
89 *_aidl_return = {Effect::CLICK, Effect::TICK};
90 return ndk::ScopedAStatus::ok();
91}
92
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090093ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) {
Steven Morelandd44007e2019-10-24 18:12:46 -070094 LOG(INFO) << "Vibrator set amplitude: " << amplitude;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090095 if (amplitude <= 0.0f || amplitude > 1.0f) {
Steven Morelandd44007e2019-10-24 18:12:46 -070096 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
97 }
98 return ndk::ScopedAStatus::ok();
99}
100
101ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) {
102 LOG(INFO) << "Vibrator set external control: " << enabled;
103 return ndk::ScopedAStatus::ok();
104}
105
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900106ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) {
107 *maxDelayMs = kComposeDelayMaxMs;
108 return ndk::ScopedAStatus::ok();
109}
110
111ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) {
112 *maxSize = kComposeSizeMax;
113 return ndk::ScopedAStatus::ok();
114}
115
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900116ndk::ScopedAStatus Vibrator::getSupportedPrimitives(std::vector<CompositePrimitive>* supported) {
117 *supported = {
118 CompositePrimitive::NOOP, CompositePrimitive::CLICK,
119 CompositePrimitive::THUD, CompositePrimitive::SPIN,
120 CompositePrimitive::QUICK_RISE, CompositePrimitive::SLOW_RISE,
Harpreet \"Eli\" Sanghafe5d3982020-01-17 14:46:37 +0900121 CompositePrimitive::QUICK_FALL, CompositePrimitive::LIGHT_TICK,
Harpreet \"Eli\" Sangha523e2962020-01-21 16:15:42 +0900122 };
123 return ndk::ScopedAStatus::ok();
124}
125
126ndk::ScopedAStatus Vibrator::getPrimitiveDuration(CompositePrimitive primitive,
127 int32_t* durationMs) {
128 if (primitive != CompositePrimitive::NOOP) {
129 *durationMs = 100;
130 } else {
131 *durationMs = 0;
132 }
133 return ndk::ScopedAStatus::ok();
134}
135
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900136ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite,
137 const std::shared_ptr<IVibratorCallback>& callback) {
138 if (composite.size() > kComposeSizeMax) {
139 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
140 }
141
Harpreet \"Eli\" Sangha13ef28c2020-01-23 13:22:07 +0900142 std::vector<CompositePrimitive> supported;
143 getSupportedPrimitives(&supported);
144
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900145 for (auto& e : composite) {
146 if (e.delayMs > kComposeDelayMaxMs) {
147 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
148 }
149 if (e.scale <= 0.0f || e.scale > 1.0f) {
150 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
151 }
Harpreet \"Eli\" Sangha13ef28c2020-01-23 13:22:07 +0900152 if (std::find(supported.begin(), supported.end(), e.primitive) == supported.end()) {
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900153 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
154 }
155 }
156
157 std::thread([=] {
158 LOG(INFO) << "Starting compose on another thread";
159
160 for (auto& e : composite) {
161 if (e.delayMs) {
162 usleep(e.delayMs * 1000);
163 }
164 LOG(INFO) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale "
165 << e.scale;
Harpreet \"Eli\" Sanghab075a6a2020-04-10 15:11:58 +0900166
167 int32_t durationMs;
168 getPrimitiveDuration(e.primitive, &durationMs);
169 usleep(durationMs * 1000);
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900170 }
171
172 if (callback != nullptr) {
173 LOG(INFO) << "Notifying perform complete";
174 callback->onComplete();
175 }
176 }).detach();
177
178 return ndk::ScopedAStatus::ok();
179}
180
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900181ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect>* _aidl_return) {
182 return getSupportedEffects(_aidl_return);
183}
184
185ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
186 std::vector<Effect> effects;
187 getSupportedAlwaysOnEffects(&effects);
188
189 if (std::find(effects.begin(), effects.end(), effect) == effects.end()) {
190 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
191 } else {
192 LOG(INFO) << "Enabling always-on ID " << id << " with " << toString(effect) << "/"
193 << toString(strength);
194 return ndk::ScopedAStatus::ok();
195 }
196}
197
198ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t id) {
199 LOG(INFO) << "Disabling always-on ID " << id;
200 return ndk::ScopedAStatus::ok();
201}
202
Steven Morelandd44007e2019-10-24 18:12:46 -0700203} // namespace vibrator
204} // namespace hardware
205} // namespace android
206} // namespace aidl