2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "VibratorService"
19 #include <android/hardware/vibrator/1.0/IVibrator.h>
20 #include <android/hardware/vibrator/1.0/types.h>
21 #include <android/hardware/vibrator/1.1/IVibrator.h>
25 #include "android_runtime/AndroidRuntime.h"
27 #include <utils/misc.h>
28 #include <utils/Log.h>
29 #include <hardware/vibrator.h>
34 using android::hardware::Return;
35 using android::hardware::vibrator::V1_0::Effect;
36 using android::hardware::vibrator::V1_0::EffectStrength;
37 using android::hardware::vibrator::V1_0::IVibrator;
38 using android::hardware::vibrator::V1_0::Status;
39 using android::hardware::vibrator::V1_1::Effect_1_1;
40 using IVibrator_1_1 = android::hardware::vibrator::V1_1::IVibrator;
45 static constexpr int NUM_TRIES = 2;
47 // Creates a Return<R> with STATUS::EX_NULL_POINTER.
49 inline Return<R> NullptrStatus() {
50 using ::android::hardware::Status;
51 return Return<R>{Status::fromExceptionCode(Status::EX_NULL_POINTER)};
54 // Helper used to transparently deal with the vibrator HAL becoming unavailable.
55 template<class R, class I, class... Args0, class... Args1>
56 Return<R> halCall(Return<R> (I::* fn)(Args0...), Args1&&... args1) {
57 // Assume that if getService returns a nullptr, HAL is not available on the
59 static sp<I> sHal = I::getService();
60 static bool sAvailable = sHal != nullptr;
63 return NullptrStatus<R>();
66 // Return<R> doesn't have a default constructor, so make a Return<R> with
68 using ::android::hardware::Status;
69 Return<R> ret{Status::fromExceptionCode(Status::EX_NONE)};
71 // Note that ret is guaranteed to be changed after this loop.
72 for (int i = 0; i < NUM_TRIES; ++i) {
73 ret = (sHal == nullptr) ? NullptrStatus<R>()
74 : (*sHal.*fn)(std::forward<Args1>(args1)...);
80 ALOGE("Failed to issue command to vibrator HAL. Retrying.");
81 // Restoring connection to the HAL.
82 sHal = I::tryGetService();
87 static void vibratorInit(JNIEnv /* env */, jobject /* clazz */)
89 halCall(&IVibrator::ping).isOk();
92 static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */)
94 return halCall(&IVibrator::ping).isOk() ? JNI_TRUE : JNI_FALSE;
97 static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms)
99 Status retStatus = halCall(&IVibrator::on, timeout_ms).withDefault(Status::UNKNOWN_ERROR);
100 if (retStatus != Status::OK) {
101 ALOGE("vibratorOn command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus));
105 static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */)
107 Status retStatus = halCall(&IVibrator::off).withDefault(Status::UNKNOWN_ERROR);
108 if (retStatus != Status::OK) {
109 ALOGE("vibratorOff command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus));
113 static jlong vibratorSupportsAmplitudeControl(JNIEnv*, jobject) {
114 return halCall(&IVibrator::supportsAmplitudeControl).withDefault(false);
117 static void vibratorSetAmplitude(JNIEnv*, jobject, jint amplitude) {
118 Status status = halCall(&IVibrator::setAmplitude, static_cast<uint32_t>(amplitude))
119 .withDefault(Status::UNKNOWN_ERROR);
120 if (status != Status::OK) {
121 ALOGE("Failed to set vibrator amplitude (%" PRIu32 ").",
122 static_cast<uint32_t>(status));
126 static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jint strength) {
129 auto callback = [&status, &lengthMs](Status retStatus, uint32_t retLengthMs) {
131 lengthMs = retLengthMs;
133 EffectStrength effectStrength(static_cast<EffectStrength>(strength));
135 if (effect < 0 || effect > static_cast<uint32_t>(Effect_1_1::TICK)) {
136 ALOGW("Unable to perform haptic effect, invalid effect ID (%" PRId32 ")",
137 static_cast<int32_t>(effect));
138 } else if (effect == static_cast<uint32_t>(Effect_1_1::TICK)) {
139 auto ret = halCall(&IVibrator_1_1::perform_1_1, static_cast<Effect_1_1>(effect),
140 effectStrength, callback);
142 ALOGW("Failed to perform effect (%" PRId32 "), insufficient HAL version",
143 static_cast<int32_t>(effect));
146 auto ret = halCall(&IVibrator::perform, static_cast<Effect>(effect), effectStrength,
149 ALOGW("Failed to perform effect (%" PRId32 ")", static_cast<int32_t>(effect));
153 if (status == Status::OK) {
155 } else if (status != Status::UNSUPPORTED_OPERATION) {
156 // Don't warn on UNSUPPORTED_OPERATION, that's a normal even and just means the motor
157 // doesn't have a pre-defined waveform to perform for it, so we should just fall back
158 // to the framework waveforms.
159 ALOGE("Failed to perform haptic effect: effect=%" PRId64 ", strength=%" PRId32
160 ", error=%" PRIu32 ").", static_cast<int64_t>(effect),
161 static_cast<int32_t>(strength), static_cast<uint32_t>(status));
166 static const JNINativeMethod method_table[] = {
167 { "vibratorExists", "()Z", (void*)vibratorExists },
168 { "vibratorInit", "()V", (void*)vibratorInit },
169 { "vibratorOn", "(J)V", (void*)vibratorOn },
170 { "vibratorOff", "()V", (void*)vibratorOff },
171 { "vibratorSupportsAmplitudeControl", "()Z", (void*)vibratorSupportsAmplitudeControl},
172 { "vibratorSetAmplitude", "(I)V", (void*)vibratorSetAmplitude},
173 { "vibratorPerformEffect", "(JJ)J", (void*)vibratorPerformEffect}
176 int register_android_server_VibratorService(JNIEnv *env)
178 return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
179 method_table, NELEM(method_table));