Home | History | Annotate | Download | only in jni
      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.1/IVibrator.h>
     22 
     23 #include "jni.h"
     24 #include <nativehelper/JNIHelp.h>
     25 #include "android_runtime/AndroidRuntime.h"
     26 
     27 #include <utils/misc.h>
     28 #include <utils/Log.h>
     29 #include <hardware/vibrator.h>
     30 
     31 #include <inttypes.h>
     32 #include <stdio.h>
     33 
     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;
     41 
     42 namespace android
     43 {
     44 
     45 static constexpr int NUM_TRIES = 2;
     46 
     47 // Creates a Return<R> with STATUS::EX_NULL_POINTER.
     48 template<class R>
     49 inline Return<R> NullptrStatus() {
     50     using ::android::hardware::Status;
     51     return Return<R>{Status::fromExceptionCode(Status::EX_NULL_POINTER)};
     52 }
     53 
     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
     58     // device.
     59     static sp<I> sHal = I::getService();
     60     static bool sAvailable = sHal != nullptr;
     61 
     62     if (!sAvailable) {
     63         return NullptrStatus<R>();
     64     }
     65 
     66     // Return<R> doesn't have a default constructor, so make a Return<R> with
     67     // STATUS::EX_NONE.
     68     using ::android::hardware::Status;
     69     Return<R> ret{Status::fromExceptionCode(Status::EX_NONE)};
     70 
     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)...);
     75 
     76         if (ret.isOk()) {
     77             break;
     78         }
     79 
     80         ALOGE("Failed to issue command to vibrator HAL. Retrying.");
     81         // Restoring connection to the HAL.
     82         sHal = I::tryGetService();
     83     }
     84     return ret;
     85 }
     86 
     87 static void vibratorInit(JNIEnv /* env */, jobject /* clazz */)
     88 {
     89     halCall(&IVibrator::ping).isOk();
     90 }
     91 
     92 static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */)
     93 {
     94     return halCall(&IVibrator::ping).isOk() ? JNI_TRUE : JNI_FALSE;
     95 }
     96 
     97 static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms)
     98 {
     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));
    102     }
    103 }
    104 
    105 static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */)
    106 {
    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));
    110     }
    111 }
    112 
    113 static jlong vibratorSupportsAmplitudeControl(JNIEnv*, jobject) {
    114     return halCall(&IVibrator::supportsAmplitudeControl).withDefault(false);
    115 }
    116 
    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));
    123     }
    124 }
    125 
    126 static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jint strength) {
    127     Status status;
    128     uint32_t lengthMs;
    129     auto callback = [&status, &lengthMs](Status retStatus, uint32_t retLengthMs) {
    130         status = retStatus;
    131         lengthMs = retLengthMs;
    132     };
    133     EffectStrength effectStrength(static_cast<EffectStrength>(strength));
    134 
    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);
    141         if (!ret.isOk()) {
    142             ALOGW("Failed to perform effect (%" PRId32 "), insufficient HAL version",
    143                     static_cast<int32_t>(effect));
    144         }
    145     } else {
    146         auto ret = halCall(&IVibrator::perform, static_cast<Effect>(effect), effectStrength,
    147                            callback);
    148         if (!ret.isOk()) {
    149             ALOGW("Failed to perform effect (%" PRId32 ")", static_cast<int32_t>(effect));
    150         }
    151     }
    152 
    153     if (status == Status::OK) {
    154         return lengthMs;
    155     } else if (status != Status::UNSUPPORTED_OPERATION) {
    156         // Don't warn on UNSUPPORTED_OPERATION, that's a normal event and just means the motor
    157         // doesn't have a pre-defined waveform to perform for it, so we should just give the
    158         // opportunity to fall back 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));
    162     }
    163     return -1;
    164 }
    165 
    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}
    174 };
    175 
    176 int register_android_server_VibratorService(JNIEnv *env)
    177 {
    178     return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
    179             method_table, NELEM(method_table));
    180 }
    181 
    182 };
    183