blob: befdeab08afe72719991eec1544e6e7782481a84 [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";
Steven Morelandf57ad9b2019-11-27 16:04:52 -080051 if (!callback->onComplete().isOk()) {
52 LOG(ERROR) << "Failed to call onComplete";
53 }
Steven Morelandd44007e2019-10-24 18:12:46 -070054 }).detach();
55 }
56 return ndk::ScopedAStatus::ok();
57}
58
59ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength,
60 const std::shared_ptr<IVibratorCallback>& callback,
61 int32_t* _aidl_return) {
62 LOG(INFO) << "Vibrator perform";
63
64 if (effect != Effect::CLICK && effect != Effect::TICK) {
65 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
66 }
67 if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM &&
68 strength != EffectStrength::STRONG) {
69 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
70 }
71
72 constexpr size_t kEffectMillis = 100;
73
74 if (callback != nullptr) {
75 std::thread([=] {
76 LOG(INFO) << "Starting perform on another thread";
77 usleep(kEffectMillis * 1000);
78 LOG(INFO) << "Notifying perform complete";
79 callback->onComplete();
80 }).detach();
81 }
82
83 *_aidl_return = kEffectMillis;
84 return ndk::ScopedAStatus::ok();
85}
86
Steven Moreland2932b222019-11-05 14:30:17 -080087ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
88 *_aidl_return = {Effect::CLICK, Effect::TICK};
89 return ndk::ScopedAStatus::ok();
90}
91
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090092ndk::ScopedAStatus Vibrator::setAmplitude(float amplitude) {
Steven Morelandd44007e2019-10-24 18:12:46 -070093 LOG(INFO) << "Vibrator set amplitude: " << amplitude;
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +090094 if (amplitude <= 0.0f || amplitude > 1.0f) {
Steven Morelandd44007e2019-10-24 18:12:46 -070095 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
96 }
97 return ndk::ScopedAStatus::ok();
98}
99
100ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) {
101 LOG(INFO) << "Vibrator set external control: " << enabled;
102 return ndk::ScopedAStatus::ok();
103}
104
Harpreet \"Eli\" Sanghaf4de5b02019-10-23 09:25:52 +0900105ndk::ScopedAStatus Vibrator::getCompositionDelayMax(int32_t* maxDelayMs) {
106 *maxDelayMs = kComposeDelayMaxMs;
107 return ndk::ScopedAStatus::ok();
108}
109
110ndk::ScopedAStatus Vibrator::getCompositionSizeMax(int32_t* maxSize) {
111 *maxSize = kComposeSizeMax;
112 return ndk::ScopedAStatus::ok();
113}
114
115ndk::ScopedAStatus Vibrator::compose(const std::vector<CompositeEffect>& composite,
116 const std::shared_ptr<IVibratorCallback>& callback) {
117 if (composite.size() > kComposeSizeMax) {
118 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
119 }
120
121 for (auto& e : composite) {
122 if (e.delayMs > kComposeDelayMaxMs) {
123 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
124 }
125 if (e.scale <= 0.0f || e.scale > 1.0f) {
126 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
127 }
128 if (e.primitive < CompositePrimitive::NOOP ||
129 e.primitive > CompositePrimitive::QUICK_FALL) {
130 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
131 }
132 }
133
134 std::thread([=] {
135 LOG(INFO) << "Starting compose on another thread";
136
137 for (auto& e : composite) {
138 if (e.delayMs) {
139 usleep(e.delayMs * 1000);
140 }
141 LOG(INFO) << "triggering primitive " << static_cast<int>(e.primitive) << " @ scale "
142 << e.scale;
143 }
144
145 if (callback != nullptr) {
146 LOG(INFO) << "Notifying perform complete";
147 callback->onComplete();
148 }
149 }).detach();
150
151 return ndk::ScopedAStatus::ok();
152}
153
Steven Morelandd44007e2019-10-24 18:12:46 -0700154} // namespace vibrator
155} // namespace hardware
156} // namespace android
157} // namespace aidl