blob: 09cd2345bfbd3086047ef1ece315b04ad79638a4 [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
27ndk::ScopedAStatus Vibrator::getCapabilities(int32_t* _aidl_return) {
28 LOG(INFO) << "Vibrator reporting capabilities";
29 *_aidl_return = IVibrator::CAP_ON_CALLBACK | IVibrator::CAP_PERFORM_CALLBACK |
Steven Morelandc0b92d52019-11-05 13:31:41 -080030 IVibrator::CAP_AMPLITUDE_CONTROL | IVibrator::CAP_EXTERNAL_CONTROL |
31 IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL;
Steven Morelandd44007e2019-10-24 18:12:46 -070032 return ndk::ScopedAStatus::ok();
33}
34
35ndk::ScopedAStatus Vibrator::off() {
36 LOG(INFO) << "Vibrator off";
37 return ndk::ScopedAStatus::ok();
38}
39
40ndk::ScopedAStatus Vibrator::on(int32_t timeoutMs,
41 const std::shared_ptr<IVibratorCallback>& callback) {
42 LOG(INFO) << "Vibrator on for timeoutMs: " << timeoutMs;
43 if (callback != nullptr) {
44 std::thread([=] {
45 LOG(INFO) << "Starting on on another thread";
46 usleep(timeoutMs * 1000);
47 LOG(INFO) << "Notifying on complete";
48 callback->onComplete();
49 }).detach();
50 }
51 return ndk::ScopedAStatus::ok();
52}
53
54ndk::ScopedAStatus Vibrator::perform(Effect effect, EffectStrength strength,
55 const std::shared_ptr<IVibratorCallback>& callback,
56 int32_t* _aidl_return) {
57 LOG(INFO) << "Vibrator perform";
58
59 if (effect != Effect::CLICK && effect != Effect::TICK) {
60 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
61 }
62 if (strength != EffectStrength::LIGHT && strength != EffectStrength::MEDIUM &&
63 strength != EffectStrength::STRONG) {
64 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_UNSUPPORTED_OPERATION));
65 }
66
67 constexpr size_t kEffectMillis = 100;
68
69 if (callback != nullptr) {
70 std::thread([=] {
71 LOG(INFO) << "Starting perform on another thread";
72 usleep(kEffectMillis * 1000);
73 LOG(INFO) << "Notifying perform complete";
74 callback->onComplete();
75 }).detach();
76 }
77
78 *_aidl_return = kEffectMillis;
79 return ndk::ScopedAStatus::ok();
80}
81
Steven Moreland2932b222019-11-05 14:30:17 -080082ndk::ScopedAStatus Vibrator::getSupportedEffects(std::vector<Effect>* _aidl_return) {
83 *_aidl_return = {Effect::CLICK, Effect::TICK};
84 return ndk::ScopedAStatus::ok();
85}
86
Steven Morelandd44007e2019-10-24 18:12:46 -070087ndk::ScopedAStatus Vibrator::setAmplitude(int32_t amplitude) {
88 LOG(INFO) << "Vibrator set amplitude: " << amplitude;
89 if (amplitude <= 0 || amplitude > 255) {
90 return ndk::ScopedAStatus(AStatus_fromExceptionCode(EX_ILLEGAL_ARGUMENT));
91 }
92 return ndk::ScopedAStatus::ok();
93}
94
95ndk::ScopedAStatus Vibrator::setExternalControl(bool enabled) {
96 LOG(INFO) << "Vibrator set external control: " << enabled;
97 return ndk::ScopedAStatus::ok();
98}
99
100} // namespace vibrator
101} // namespace hardware
102} // namespace android
103} // namespace aidl