blob: f77cfd4c9028b9aaac9f92658d16aaceb7044268 [file] [log] [blame]
Caleb Connollyc445fab2021-08-11 14:42:57 +01001/*
2 * Copyright (C) 2017 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/* Adapted from Crosshatch downstream:
18 * https://github.com/GrapheneOS/device_google_crosshatch/blob/c7f62a539b6cb4685348fbd747605cbfabf1b94d/vibrator/Vibrator.cpp
19 */
20
21#define LOG_TAG "VibratorService"
22
23#include <log/log.h>
24
25#include <hardware/hardware.h>
26#include <hardware/vibrator.h>
27#include <cutils/properties.h>
28#include <linux/input.h>
29
30#include "Vibrator.h"
31
32#include <cinttypes>
33#include <cmath>
34#include <iostream>
35#include <fstream>
36#include <cstring>
37
38
39namespace android {
40namespace hardware {
41namespace vibrator {
42namespace V1_1 {
43namespace implementation {
44
45using Status = ::android::hardware::vibrator::V1_0::Status;
46using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
47using Effect = ::android::hardware::vibrator::V1_1::Effect_1_1;
48
Caleb Connolly542816b2021-08-12 15:31:07 +010049// These have to be kept in line with HAL version
50static constexpr Effect MAX_EFFECT = Effect::TICK;
51static constexpr EffectStrength MAX_EFFECT_STRENGTH = EffectStrength::STRONG;
52
53#define EFFECT_INDEX(effect, strength) \
54 ((uint16_t)effect * (uint16_t)MAX_EFFECT_STRENGTH + (uint16_t)strength)
Caleb Connollyc445fab2021-08-11 14:42:57 +010055
56Vibrator::Vibrator(std::string devpath) :
Caleb Connolly542816b2021-08-12 15:31:07 +010057 mInputDevPath(devpath)
Caleb Connollyc445fab2021-08-11 14:42:57 +010058{
Caleb Connolly542816b2021-08-12 15:31:07 +010059 // We just keep the input device open the whole time we're running.
60 // Closing / reopening it seems to break things.
61 mfd = openInputDev();
62 if (mfd < 0) {
63 ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str());
64 return;
65 }
Caleb Connollyc445fab2021-08-11 14:42:57 +010066
Caleb Connolly542816b2021-08-12 15:31:07 +010067 // Build a table of effects for each strength of each effect.
68 int baseStrength = 0;
69 for (uint16_t i = 0; i <= (uint16_t)MAX_EFFECT; i++)
70 {
71 for (uint16_t j = 0; j <= (uint16_t)MAX_EFFECT_STRENGTH; j++)
72 {
73 switch((Effect)i) {
74 default:
75 case Effect::CLICK:
76 baseStrength = 0x4000;
77 break;
78 case Effect::TICK:
79 baseStrength = 0x1000;
80 break;
81 case Effect::DOUBLE_CLICK:
82 baseStrength = 0x3000;
83 break;
84 }
85 ALOGV("%s() uploading effect %d, strength %d, magnitude = 0x%x", __func__, i, j, baseStrength + j * 0x1000);
86 // 0x8000 is about the max for qcom_spmi_haptics.
87 mEffects[EFFECT_INDEX(i, j)] = FF_EFFECT((uint16_t)(baseStrength + j * 0x800));
88
89 uploadEffectToKernel(&mEffects[EFFECT_INDEX(i, j)]);
90 }
Caleb Connollyc445fab2021-08-11 14:42:57 +010091 }
92}
93
94int Vibrator::openInputDev() {
Caleb Connolly542816b2021-08-12 15:31:07 +010095 return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC);
Caleb Connollyc445fab2021-08-11 14:42:57 +010096}
97
Caleb Connolly542816b2021-08-12 15:31:07 +010098void Vibrator::uploadEffectToKernel(struct ff_effect* effect) {
99 int ret;
100
101 ret = ioctl(mfd, EVIOCSFF, effect);
102 if (ret < 0) {
103 ALOGE("%s() Couldn't upload rumble effect to device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100104 }
Caleb Connolly542816b2021-08-12 15:31:07 +0100105}
Caleb Connollyc445fab2021-08-11 14:42:57 +0100106
Caleb Connolly542816b2021-08-12 15:31:07 +0100107void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect
108 int ret;
109
110 ret = ioctl(mfd, EVIOCRMFF, effect);
111 if (ret < 0) {
112 ALOGE("%s() Failed to remove rumble effect from device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
113 return;
114 }
115 effect->id = -1;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100116}
117
118// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
119Return<Status> Vibrator::on(uint32_t timeoutMs) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100120 struct ff_effect* effect;
121 // If the active effect is set, use it instead of the default
122 if (mActiveEffectId < 0) {
123 effect = &mEffects[EFFECT_INDEX(Effect::CLICK , EffectStrength::MEDIUM)];
124 } else {
125 effect = &mEffects[mActiveEffectId];
126 }
127
128 ALOGV("%s, timeoutMs = %d, effect.id = %d, magnitude = %d", __func__, timeoutMs, effect->id, effect->u.rumble.strong_magnitude);
129 struct input_event play;
130 int ret;
131
132 play.type = EV_FF;
133 play.code = effect->id;
134 play.value = 1;
135
136 ret = write(mfd, (const void*) &play, sizeof(play));
137 if (ret != sizeof(play)) {
138 ALOGE("%s() failed to fully write play event to input device: %d", __func__, errno);
139 return Status::UNKNOWN_ERROR;
140 }
141
142 usleep(timeoutMs * 1000);
143
144 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100145}
146
147Return<Status> Vibrator::off() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100148 ALOGV("%s", __func__);
149 struct input_event stop;
150 struct ff_effect* effect;
151 // If the active effect is set, use it instead of the default
152 if (mActiveEffectId < 0) {
153 effect = &mEffects[(uint16_t)Effect::CLICK * (uint16_t)EffectStrength::STRONG];
154 } else {
155 effect = &mEffects[mActiveEffectId];
Caleb Connollyc445fab2021-08-11 14:42:57 +0100156 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100157
Caleb Connolly542816b2021-08-12 15:31:07 +0100158 stop.type = EV_FF;
159 stop.code = effect->id;
160 stop.value = 0;
161 if (write(mfd, (const void*) &stop, sizeof(stop)) != sizeof(stop)) {
162 ALOGE("%s() failed to fully write stop event to input device", __func__);
163 return Status::UNKNOWN_ERROR;
164 }
165
166 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100167}
168
169Return<bool> Vibrator::supportsAmplitudeControl() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100170 ALOGV("%s", __func__);
171 return false;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100172}
173
174Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100175 ALOGV("%s", __func__);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100176
Caleb Connolly542816b2021-08-12 15:31:07 +0100177 if (!amplitude) {
178 return Status::BAD_VALUE;
179 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100180
Caleb Connolly542816b2021-08-12 15:31:07 +0100181 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100182}
183
184Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100185 perform_cb _hidl_cb) {
186 return performWrapper(effect, strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100187}
188
189Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100190 perform_cb _hidl_cb) {
191 return performWrapper(effect, strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100192}
193
194template <typename T>
195Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100196 ALOGV("%s, ", __func__);
197 auto validRange = hidl_enum_range<T>();
198 if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
199 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
200 return Void();
201 }
202 return performEffect(static_cast<Effect>(effect), strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100203}
204
205Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100206 perform_cb _hidl_cb) {
207 Status status = Status::OK;
208 uint32_t timeMs = 9;
209 bool doubleClick = effect == Effect::DOUBLE_CLICK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100210
Caleb Connolly542816b2021-08-12 15:31:07 +0100211 ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100212
Caleb Connolly542816b2021-08-12 15:31:07 +0100213 if (effect > Effect::TICK){
214 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
215 return Void();
216 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100217
Caleb Connolly542816b2021-08-12 15:31:07 +0100218 mActiveEffectId = EFFECT_INDEX(effect, strength);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100219
Caleb Connolly542816b2021-08-12 15:31:07 +0100220 // Play the effect
221 on(timeMs);
222 /*
223 * Android calls off() for us, so
224 * only call it if we're doing a double click
225 */
226 if (doubleClick) {
227 off();
228 timeMs += 59;
229 usleep(50 * 1000);
230 on(timeMs);
231 mActiveEffectId = -1; // Reset the active effectId
232 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100233
Caleb Connolly542816b2021-08-12 15:31:07 +0100234 _hidl_cb(status, timeMs);
235
236 return Void();
Caleb Connollyc445fab2021-08-11 14:42:57 +0100237}
238
239Vibrator::~Vibrator() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100240 close(mfd);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100241}
242
243
244} // namespace implementation
245} // namespace V1_1
246} // namespace vibrator
247} // namespace hardware
248} // namespace android