blob: cedd9cb89c9c332c99b1f17c5650d5d4944df71d [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
116ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite,
117 const std::shared_ptr<IVibratorCallback>& callback) {
118 if (composite.size() > kComposeSizeMax) {
119 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
120 }
121
122 for (auto& e : composite) {
123 if (e.delayMs > kComposeDelayMaxMs) {
124 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
125 }
126 if (e.scale <= 0.0f || e.scale > 1.0f) {
127 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
128 }
129 if (e.primitive < CompositePrimitive::NOOP ||
130 e.primitive > CompositePrimitive::QUICK_FALL) {
131 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
132 }
133 }
134
135 std::thread([=] {
136 LOG(INFO) << "Starting compose on another thread";
137
138 for (auto& e : composite) {
139 if (e.delayMs) {
140 usleep(e.delayMs * 1000);
141 }
142 LOG(INFO) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale "
143 << e.scale;
144 }
145
146 if (callback != nullptr) {
147 LOG(INFO) << "Notifying perform complete";
148 callback->onComplete();
149 }
150 }).detach();
151
152 return ndk::ScopedAStatus::ok();
153}
154
Harpreet \"Eli\" Sangha63624092019-09-09 11:04:54 +0900155ndk::ScopedAStatus Vibrator::getSupportedAlwaysOnEffects(std::vector<Effect>* _aidl_return) {
156 return getSupportedEffects(_aidl_return);
157}
158
159ndk::ScopedAStatus Vibrator::alwaysOnEnable(int32_t id, Effect effect, EffectStrength strength) {
160 std::vector<Effect> effects;
161 getSupportedAlwaysOnEffects(&effects);
162
163 if (std::find(effects.begin(), effects.end(), effect) == effects.end()) {
164 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
165 } else {
166 LOG(INFO) << "Enabling always-on ID " << id << " with " << toString(effect) << "/"
167 << toString(strength);
168 return ndk::ScopedAStatus::ok();
169 }
170}
171
172ndk::ScopedAStatus Vibrator::alwaysOnDisable(int32_t id) {
173 LOG(INFO) << "Disabling always-on ID " << id;
174 return ndk::ScopedAStatus::ok();
175}
176
Steven Morelandd44007e2019-10-24 18:12:46 -0700177} // namespace vibrator
178} // namespace hardware
179} // namespace android
180} // namespace aidl