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