blob: a77c49a0a27dad1955e02297f231af09deaea7e2 [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\" Sanghaf4de5b02019-10-23 09:25:52 +090034 IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL | IVibrator::CAP_COMPOSE_EFFECTS;
Steven Morelandd44007e2019-10-24 18:12:46 -070035 return ndk::ScopedAStatus::ok();
36}
37
38ndk::ScopedAStatus Vibrator::off() {
39 LOG(INFO) << "Vibrator off";
40 return ndk::ScopedAStatus::ok();
41}
42
43ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs,
44 const std::shared_ptr<IVibratorCallback>& callback) {
45 LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs;
46 if (callback != nullptr) {
47 std::thread([=] {
48 LOG(INFO) << "Starting on on another thread";
49 usleep(timeoutMs * 1000);
50 LOG(INFO) << "Notifying on complete";
51 callback->onComplete();
52 }).detach();
53 }
54 return ndk::ScopedAStatus::ok();
55}
56
57ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength,
58 const std::shared_ptr<IVibratorCallback>& callback,
59 int32_t* _aidl_return) {
60 LOG(INFO) << "Vibrator perform";
61
62 if (effect != Effect::CLICK && effect != Effect::TICK) {
63 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
64 }
65 if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM &&
66 strength != EffectStrength::STRONG) {
67 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
68 }
69
70 constexpr size_t kEffectMillis = 100;
71
72 if (callback != nullptr) {
73 std::thread([=] {
74 LOG(INFO) << "Starting perform on another thread";
75 usleep(kEffectMillis * 1000);
76 LOG(INFO) << "Notifying perform complete";
77 callback->onComplete();
78 }).detach();
79 }
80
81 *_aidl_return = kEffectMillis;
82 return ndk::ScopedAStatus::ok();
83}
84
Steven Moreland2932b222019-11-05 14:30:17 -080085ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
86 *_aidl_return = {Effect::CLICK, Effect::TICK};
87 return ndk::ScopedAStatus::ok();
88}
89
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090090ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) {
Steven Morelandd44007e2019-10-24 18:12:46 -070091 LOG(INFO) << "Vibrator set amplitude: " << amplitude;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090092 if (amplitude <= 0.0f || amplitude > 1.0f) {
Steven Morelandd44007e2019-10-24 18:12:46 -070093 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
94 }
95 return ndk::ScopedAStatus::ok();
96}
97
98ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) {
99 LOG(INFO) << "Vibrator set external control: " << enabled;
100 return ndk::ScopedAStatus::ok();
101}
102
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900103ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) {
104 *maxDelayMs = kComposeDelayMaxMs;
105 return ndk::ScopedAStatus::ok();
106}
107
108ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) {
109 *maxSize = kComposeSizeMax;
110 return ndk::ScopedAStatus::ok();
111}
112
113ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite,
114 const std::shared_ptr<IVibratorCallback>& callback) {
115 if (composite.size() > kComposeSizeMax) {
116 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
117 }
118
119 for (auto& e : composite) {
120 if (e.delayMs > kComposeDelayMaxMs) {
121 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
122 }
123 if (e.scale <= 0.0f || e.scale > 1.0f) {
124 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
125 }
126 if (e.primitive < CompositePrimitive::NOOP ||
127 e.primitive > CompositePrimitive::QUICK_FALL) {
128 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
129 }
130 }
131
132 std::thread([=] {
133 LOG(INFO) << "Starting compose on another thread";
134
135 for (auto& e : composite) {
136 if (e.delayMs) {
137 usleep(e.delayMs * 1000);
138 }
139 LOG(INFO) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale "
140 << e.scale;
141 }
142
143 if (callback != nullptr) {
144 LOG(INFO) << "Notifying perform complete";
145 callback->onComplete();
146 }
147 }).detach();
148
149 return ndk::ScopedAStatus::ok();
150}
151
Steven Morelandd44007e2019-10-24 18:12:46 -0700152} // namespace vibrator
153} // namespace hardware
154} // namespace android
155} // namespace aidl