blob: 76ce890717d78dca3acb48cadbfb624b174c46c8 [file] [log] [blame]
Mike Lockwood3a322132009-11-24 00:30:52 -05001/*
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
Prashant Malania9cdaac2016-09-16 13:51:18 -070019#include <android/hardware/vibrator/1.0/IVibrator.h>
20#include <android/hardware/vibrator/1.0/types.h>
21
Mike Lockwood3a322132009-11-24 00:30:52 -050022#include "jni.h"
23#include "JNIHelp.h"
24#include "android_runtime/AndroidRuntime.h"
25
26#include <utils/misc.h>
27#include <utils/Log.h>
Vincent Beckere6904fb2012-08-10 14:17:33 +020028#include <hardware/vibrator.h>
Mike Lockwood3a322132009-11-24 00:30:52 -050029
Michael Wright71216972017-01-31 18:33:54 +000030#include <inttypes.h>
Mike Lockwood3a322132009-11-24 00:30:52 -050031#include <stdio.h>
32
Michael Wright71216972017-01-31 18:33:54 +000033using android::hardware::Return;
34using android::hardware::vibrator::V1_0::Effect;
35using android::hardware::vibrator::V1_0::EffectStrength;
Prashant Malania9cdaac2016-09-16 13:51:18 -070036using android::hardware::vibrator::V1_0::IVibrator;
37using android::hardware::vibrator::V1_0::Status;
38
Mike Lockwood3a322132009-11-24 00:30:52 -050039namespace android
40{
41
Prashant Malania9cdaac2016-09-16 13:51:18 -070042static sp<IVibrator> mHal;
Vincent Beckere6904fb2012-08-10 14:17:33 +020043
44static void vibratorInit(JNIEnv /* env */, jobject /* clazz */)
45{
Prashant Malania9cdaac2016-09-16 13:51:18 -070046 /* TODO(b/31632518) */
47 if (mHal != nullptr) {
Vincent Beckere6904fb2012-08-10 14:17:33 +020048 return;
49 }
Chris Phoenixc310a902017-01-19 15:54:28 -080050 mHal = IVibrator::getService();
Vincent Beckere6904fb2012-08-10 14:17:33 +020051}
52
Andreas Gampe26872f42014-09-24 21:39:34 -070053static jboolean vibratorExists(JNIEnv* /* env */, jobject /* clazz */)
Dianne Hackbornea9020e2010-11-04 11:39:12 -070054{
Prashant Malania9cdaac2016-09-16 13:51:18 -070055 if (mHal != nullptr) {
Vincent Beckere6904fb2012-08-10 14:17:33 +020056 return JNI_TRUE;
57 } else {
58 return JNI_FALSE;
59 }
Dianne Hackbornea9020e2010-11-04 11:39:12 -070060}
61
Andreas Gampe26872f42014-09-24 21:39:34 -070062static void vibratorOn(JNIEnv* /* env */, jobject /* clazz */, jlong timeout_ms)
Mike Lockwood3a322132009-11-24 00:30:52 -050063{
Prashant Malania9cdaac2016-09-16 13:51:18 -070064 if (mHal != nullptr) {
65 Status retStatus = mHal->on(timeout_ms);
Michael Wright71216972017-01-31 18:33:54 +000066 if (retStatus != Status::OK) {
67 ALOGE("vibratorOn command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus));
Vincent Beckere6904fb2012-08-10 14:17:33 +020068 }
69 } else {
70 ALOGW("Tried to vibrate but there is no vibrator device.");
71 }
Mike Lockwood3a322132009-11-24 00:30:52 -050072}
73
Andreas Gampe26872f42014-09-24 21:39:34 -070074static void vibratorOff(JNIEnv* /* env */, jobject /* clazz */)
Mike Lockwood3a322132009-11-24 00:30:52 -050075{
Prashant Malania9cdaac2016-09-16 13:51:18 -070076 if (mHal != nullptr) {
77 Status retStatus = mHal->off();
Michael Wright71216972017-01-31 18:33:54 +000078 if (retStatus != Status::OK) {
79 ALOGE("vibratorOff command failed (%" PRIu32 ").", static_cast<uint32_t>(retStatus));
Vincent Beckere6904fb2012-08-10 14:17:33 +020080 }
81 } else {
82 ALOGW("Tried to stop vibrating but there is no vibrator device.");
83 }
Mike Lockwood3a322132009-11-24 00:30:52 -050084}
85
Michael Wright71216972017-01-31 18:33:54 +000086static jlong vibratorSupportsAmplitudeControl(JNIEnv*, jobject) {
87 if (mHal != nullptr) {
88 return mHal->supportsAmplitudeControl();
89 } else {
90 ALOGW("Unable to get max vibration amplitude, there is no vibrator device.");
91 }
92 return false;
93}
94
95static void vibratorSetAmplitude(JNIEnv*, jobject, jint amplitude) {
96 if (mHal != nullptr) {
97 Status status = mHal->setAmplitude(static_cast<uint32_t>(amplitude));
98 if (status != Status::OK) {
99 ALOGE("Failed to set vibrator amplitude (%" PRIu32 ").",
100 static_cast<uint32_t>(status));
101 }
102 } else {
103 ALOGW("Unable to set vibration amplitude, there is no vibrator device.");
104 }
105}
106
107static jlong vibratorPerformEffect(JNIEnv*, jobject, jlong effect, jint strength) {
108 if (mHal != nullptr) {
109 Status status;
110 uint32_t lengthMs;
111 mHal->perform(static_cast<Effect>(effect), static_cast<EffectStrength>(strength),
112 [&status, &lengthMs](Status retStatus, uint32_t retLengthMs) {
113 status = retStatus;
114 lengthMs = retLengthMs;
115 });
116 if (status == Status::OK) {
117 return lengthMs;
118 } else if (status != Status::UNSUPPORTED_OPERATION) {
119 // Don't warn on UNSUPPORTED_OPERATION, that's a normal even and just means the motor
120 // doesn't have a pre-defined waveform to perform for it, so we should just fall back
121 // to the framework waveforms.
122 ALOGE("Failed to perform haptic effect: effect=%" PRId64 ", strength=%" PRId32
123 ", error=%" PRIu32 ").", static_cast<int64_t>(effect),
124 static_cast<int32_t>(strength), static_cast<uint32_t>(status));
125 }
126 } else {
127 ALOGW("Unable to perform haptic effect, there is no vibrator device.");
128 }
129 return -1;
130}
131
Daniel Micay76f6a862015-09-19 17:31:01 -0400132static const JNINativeMethod method_table[] = {
Dianne Hackbornea9020e2010-11-04 11:39:12 -0700133 { "vibratorExists", "()Z", (void*)vibratorExists },
Vincent Beckere6904fb2012-08-10 14:17:33 +0200134 { "vibratorInit", "()V", (void*)vibratorInit },
Mike Lockwood3a322132009-11-24 00:30:52 -0500135 { "vibratorOn", "(J)V", (void*)vibratorOn },
Michael Wright71216972017-01-31 18:33:54 +0000136 { "vibratorOff", "()V", (void*)vibratorOff },
137 { "vibratorSupportsAmplitudeControl", "()Z", (void*)vibratorSupportsAmplitudeControl},
138 { "vibratorSetAmplitude", "(I)V", (void*)vibratorSetAmplitude},
139 { "vibratorPerformEffect", "(JJ)J", (void*)vibratorPerformEffect}
Mike Lockwood3a322132009-11-24 00:30:52 -0500140};
141
142int register_android_server_VibratorService(JNIEnv *env)
143{
144 return jniRegisterNativeMethods(env, "com/android/server/VibratorService",
145 method_table, NELEM(method_table));
146}
147
148};