1/*
2 * Copyright (C) 2009 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#define LOG_TAG "VibratorService"
18
19#include <android/hardware/vibrator/1.0/IVibrator.h>
20#include <android/hardware/vibrator/1.0/types.h>
21#include <android/hardware/vibrator/1.0/IVibrator.h>
22#include <android/hardware/vibrator/1.1/types.h>
23#include <android/hardware/vibrator/1.2/IVibrator.h>
24#include <android/hardware/vibrator/1.2/types.h>
25
26#include "jni.h"
27#include <nativehelper/JNIHelp.h>
28#include "android_runtime/AndroidRuntime.h"
29
30#include <utils/misc.h>
31#include <utils/Log.h>
32#include <hardware/vibrator.h>
33
34#include <inttypes.h>
35#include <stdio.h>
36
37using android::hardware::Return;
38using android::hardware::vibrator::V1_0::EffectStrength;
39using android::hardware::vibrator::V1_0::Status;
40using android::hardware::vibrator::V1_1::Effect_1_1;
41
42namespace V1_0 = android::hardware::vibrator::V1_0;
43namespace V1_1 = android::hardware::vibrator::V1_1;
44namespace V1_2 = android::hardware::vibrator::V1_2;
45
46namespace android {
47
48static constexpr int NUM_TRIES = 2;
49
50// Creates a Return<R> with STATUS::EX_NULL_POINTER.
51template<class R>
52inline Return<R> NullptrStatus() {
53    using ::android::hardware::Status;
54    return Return<R>{Status::fromExceptionCode(Status::EX_NULL_POINTER)};
55}
56
57// Helper used to transparently deal with the vibrator HAL becoming unavailable.
58template<class R, class I, class... Args0, class... Args1>
59Return<R> halCall(Return<R> (I::* fn)(Args0...), Args1&&... args1) {
60    // Assume that if getService returns a nullptr, HAL is not available on the
61    // device.
62    static sp<I> sHal = I::getService();
63    static bool sAvailable = sHal != nullptr;
64
65    if (!sAvailable) {
66        return NullptrStatus<R>();
67    }
68
69    // Return<R> doesn't have a default constructor, so make a Return<R> with
70    // STATUS::EX_NONE.
71    using ::android::hardware::Status;
72    Return<R> ret{Status::fromExceptionCode(Status::EX_NONE)};
73
74    // Note that ret is guaranteed to be changed after this loop.
75    for (int i = 0; i < NUM_TRIES; ++i) {
76        ret = (sHal == nullptr) ? NullptrStatus<R>()
77                : (*sHal.*fn)(std::forward<Args1>(args1)...);
78
79        if (ret.isOk()) {
80            break;
81        }
82
83        ALOGE("Failed to issue command to vibrator HAL. Retrying.");
84        // Restoring connection to the HAL.
85        sHal = I::tryGetService();
86    }
87    return ret;
88}
89
90template<class R>
91bool isValidEffect(jlong effect) {
92    if (effect < 0) {
93        return false;
94    }
95    R val = static_cast<R>(effect);
96    auto iter = hardware::hidl_enum_iterator<R>();
97    return val >= *iter.begin() && val < *std::prev(iter.end());
98}
99
100static void vibratorInit(JNIEnv /* env */, jobject /* clazz */)
101{
102    halCall(&V1_0::IVibrator::ping).isOk();
103}
104
105static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */)
106{
107    return halCall(&V1_0::IVibrator::ping).isOk() ? JNI_TRUE : JNI_FALSE;
108}
109
110static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms)
111{
112    Status retStatus = halCall(&V1_0::IVibrator::on, timeout_ms).withDefault(Status::UNKNOWN_ERROR);
113    if (retStatus != Status::OK) {
114        ALOGE("vibratorOn command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus));
115    }
116}
117
118static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */)
119{
120    Status retStatus = halCall(&V1_0::IVibrator::off).withDefault(Status::UNKNOWN_ERROR);
121    if (retStatus != Status::OK) {
122        ALOGE("vibratorOff command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus));
123    }
124}
125
126static jlong vibratorSupportsAmplitudeControl(JNIEnv*, jobject) {
127    return halCall(&V1_0::IVibrator::supportsAmplitudeControl).withDefault(false);
128}
129
130static void vibratorSetAmplitude(JNIEnv*, jobject, jint amplitude) {
131    Status status = halCall(&V1_0::IVibrator::setAmplitude, static_cast<uint32_t>(amplitude))
132        .withDefault(Status::UNKNOWN_ERROR);
133    if (status != Status::OK) {
134      ALOGE("Failed to set vibrator amplitude (%" PRIu32 ").",
135            static_cast<uint32_t>(status));
136    }
137}
138
139static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jint strength) {
140    Status status;
141    uint32_t lengthMs;
142    auto callback = [&status, &lengthMs](Status retStatus, uint32_t retLengthMs) {
143        status = retStatus;
144        lengthMs = retLengthMs;
145    };
146    EffectStrength effectStrength(static_cast<EffectStrength>(strength));
147
148    Return<void> ret;
149    if (isValidEffect<V1_0::Effect>(effect)) {
150        ret = halCall(&V1_0::IVibrator::perform, static_cast<V1_0::Effect>(effect),
151                effectStrength, callback);
152    } else if (isValidEffect<Effect_1_1>(effect)) {
153        ret = halCall(&V1_1::IVibrator::perform_1_1, static_cast<Effect_1_1>(effect),
154                           effectStrength, callback);
155    } else if (isValidEffect<V1_2::Effect>(effect)) {
156        ret = halCall(&V1_2::IVibrator::perform_1_2, static_cast<V1_2::Effect>(effect),
157                           effectStrength, callback);
158    } else {
159        ALOGW("Unable to perform haptic effect, invalid effect ID (%" PRId32 ")",
160                static_cast<int32_t>(effect));
161        return -1;
162    }
163
164    if (!ret.isOk()) {
165        ALOGW("Failed to perform effect (%" PRId32 ")", static_cast<int32_t>(effect));
166        return -1;
167    }
168
169    if (status == Status::OK) {
170        return lengthMs;
171    } else if (status != Status::UNSUPPORTED_OPERATION) {
172        // Don't warn on UNSUPPORTED_OPERATION, that's a normal event and just means the motor
173        // doesn't have a pre-defined waveform to perform for it, so we should just give the
174        // opportunity to fall back to the framework waveforms.
175        ALOGE("Failed to perform haptic effect: effect=%" PRId64 ", strength=%" PRId32
176                ", error=%" PRIu32 ").", static_cast<int64_t>(effect),
177                static_cast<int32_t>(strength), static_cast<uint32_t>(status));
178    }
179
180    return -1;
181}
182
183static const JNINativeMethod method_table[] = {
184    { "vibratorExists", "()Z", (void*)vibratorExists },
185    { "vibratorInit", "()V", (void*)vibratorInit },
186    { "vibratorOn", "(J)V", (void*)vibratorOn },
187    { "vibratorOff", "()V", (void*)vibratorOff },
188    { "vibratorSupportsAmplitudeControl", "()Z", (void*)vibratorSupportsAmplitudeControl},
189    { "vibratorSetAmplitude", "(I)V", (void*)vibratorSetAmplitude},
190    { "vibratorPerformEffect", "(JJ)J", (void*)vibratorPerformEffect}
191};
192
193int register_android_server_VibratorService(JNIEnv *env)
194{
195    return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
196            method_table, NELEM(method_table));
197}
198
199};
200