blob: 25a031e23c3ca7b67961657dd6868b809ea71dcf [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
Caleb Connollyc445fab2021-08-11 14:42:57 +010038namespace android {
39namespace hardware {
40namespace vibrator {
41namespace V1_1 {
42namespace implementation {
43
44using Status = ::android::hardware::vibrator::V1_0::Status;
45using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
46using Effect = ::android::hardware::vibrator::V1_1::Effect_1_1;
47
Caleb Connolly542816b2021-08-12 15:31:07 +010048// These have to be kept in line with HAL version
49static constexpr Effect MAX_EFFECT = Effect::TICK;
50static constexpr EffectStrength MAX_EFFECT_STRENGTH = EffectStrength::STRONG;
51
52#define EFFECT_INDEX(effect, strength) \
53 ((uint16_t)effect * (uint16_t)MAX_EFFECT_STRENGTH + (uint16_t)strength)
Caleb Connollyc445fab2021-08-11 14:42:57 +010054
Caleb Connollyad8a8b52021-09-14 16:28:36 +010055static constexpr int DEFAULT_EFFECT_ID = EFFECT_INDEX(Effect::CLICK, EffectStrength::MEDIUM);
56
Caleb Connollyc445fab2021-08-11 14:42:57 +010057Vibrator::Vibrator(std::string devpath) :
Caleb Connolly542816b2021-08-12 15:31:07 +010058 mInputDevPath(devpath)
Caleb Connollyc445fab2021-08-11 14:42:57 +010059{
Caleb Connolly542816b2021-08-12 15:31:07 +010060 // We just keep the input device open the whole time we're running.
61 // Closing / reopening it seems to break things.
62 mfd = openInputDev();
63 if (mfd < 0) {
64 ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str());
65 return;
66 }
Caleb Connollyc445fab2021-08-11 14:42:57 +010067
Caleb Connolly542816b2021-08-12 15:31:07 +010068 // Build a table of effects for each strength of each effect.
69 int baseStrength = 0;
70 for (uint16_t i = 0; i <= (uint16_t)MAX_EFFECT; i++)
71 {
72 for (uint16_t j = 0; j <= (uint16_t)MAX_EFFECT_STRENGTH; j++)
73 {
74 switch((Effect)i) {
75 default:
76 case Effect::CLICK:
77 baseStrength = 0x4000;
78 break;
79 case Effect::TICK:
80 baseStrength = 0x1000;
81 break;
82 case Effect::DOUBLE_CLICK:
83 baseStrength = 0x3000;
84 break;
85 }
86 ALOGV("%s() uploading effect %d, strength %d, magnitude = 0x%x", __func__, i, j, baseStrength + j * 0x1000);
87 // 0x8000 is about the max for qcom_spmi_haptics.
88 mEffects[EFFECT_INDEX(i, j)] = FF_EFFECT((uint16_t)(baseStrength + j * 0x800));
89
90 uploadEffectToKernel(&mEffects[EFFECT_INDEX(i, j)]);
91 }
Caleb Connollyc445fab2021-08-11 14:42:57 +010092 }
93}
94
95int Vibrator::openInputDev() {
Caleb Connolly542816b2021-08-12 15:31:07 +010096 return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC);
Caleb Connollyc445fab2021-08-11 14:42:57 +010097}
98
Caleb Connolly542816b2021-08-12 15:31:07 +010099void Vibrator::uploadEffectToKernel(struct ff_effect* effect) {
100 int ret;
101
102 ret = ioctl(mfd, EVIOCSFF, effect);
103 if (ret < 0) {
104 ALOGE("%s() Couldn't upload rumble effect to device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100105 }
Caleb Connolly542816b2021-08-12 15:31:07 +0100106}
Caleb Connollyc445fab2021-08-11 14:42:57 +0100107
Caleb Connolly542816b2021-08-12 15:31:07 +0100108void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect
109 int ret;
110
111 ret = ioctl(mfd, EVIOCRMFF, effect);
112 if (ret < 0) {
113 ALOGE("%s() Failed to remove rumble effect from device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
114 return;
115 }
116 effect->id = -1;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100117}
118
119// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
120Return<Status> Vibrator::on(uint32_t timeoutMs) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100121 struct ff_effect* effect;
122 // If the active effect is set, use it instead of the default
123 if (mActiveEffectId < 0) {
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100124 effect = &mEffects[DEFAULT_EFFECT_ID];
Caleb Connolly542816b2021-08-12 15:31:07 +0100125 } else {
126 effect = &mEffects[mActiveEffectId];
127 }
128
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100129 ALOGV("%s() mActiveEffectId = %d, timeoutMs = %d, effect.id = %d, magnitude = %d", __func__, mActiveEffectId, timeoutMs, effect->id, effect->u.rumble.strong_magnitude);
Caleb Connolly542816b2021-08-12 15:31:07 +0100130 struct input_event play;
131 int ret;
132
133 play.type = EV_FF;
134 play.code = effect->id;
135 play.value = 1;
136
137 ret = write(mfd, (const void*) &play, sizeof(play));
138 if (ret != sizeof(play)) {
139 ALOGE("%s() failed to fully write play event to input device: %d", __func__, errno);
140 return Status::UNKNOWN_ERROR;
141 }
142
143 usleep(timeoutMs * 1000);
144
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100145 off();
146
Caleb Connolly542816b2021-08-12 15:31:07 +0100147 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100148}
149
150Return<Status> Vibrator::off() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100151 ALOGV("%s", __func__);
152 struct input_event stop;
153 struct ff_effect* effect;
154 // If the active effect is set, use it instead of the default
155 if (mActiveEffectId < 0) {
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100156 ALOGV("%s() no active effect, stopping default", __func__);
157 effect = &mEffects[DEFAULT_EFFECT_ID];
Caleb Connolly542816b2021-08-12 15:31:07 +0100158 } else {
159 effect = &mEffects[mActiveEffectId];
Caleb Connollyc445fab2021-08-11 14:42:57 +0100160 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100161
Caleb Connolly542816b2021-08-12 15:31:07 +0100162 stop.type = EV_FF;
163 stop.code = effect->id;
164 stop.value = 0;
165 if (write(mfd, (const void*) &stop, sizeof(stop)) != sizeof(stop)) {
166 ALOGE("%s() failed to fully write stop event to input device", __func__);
167 return Status::UNKNOWN_ERROR;
168 }
169
170 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100171}
172
173Return<bool> Vibrator::supportsAmplitudeControl() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100174 ALOGV("%s", __func__);
175 return false;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100176}
177
178Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100179 ALOGV("%s", __func__);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100180
Caleb Connolly542816b2021-08-12 15:31:07 +0100181 if (!amplitude) {
182 return Status::BAD_VALUE;
183 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100184
Caleb Connolly542816b2021-08-12 15:31:07 +0100185 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100186}
187
188Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100189 perform_cb _hidl_cb) {
190 return performWrapper(effect, strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100191}
192
193Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100194 perform_cb _hidl_cb) {
195 return performWrapper(effect, strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100196}
197
198template <typename T>
199Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100200 ALOGV("%s, ", __func__);
201 auto validRange = hidl_enum_range<T>();
202 if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
203 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
204 return Void();
205 }
206 return performEffect(static_cast<Effect>(effect), strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100207}
208
209Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100210 perform_cb _hidl_cb) {
211 Status status = Status::OK;
212 uint32_t timeMs = 9;
213 bool doubleClick = effect == Effect::DOUBLE_CLICK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100214
Caleb Connolly542816b2021-08-12 15:31:07 +0100215 ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100216
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100217 if (effect > MAX_EFFECT){
Caleb Connolly542816b2021-08-12 15:31:07 +0100218 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
219 return Void();
220 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100221
Caleb Connolly542816b2021-08-12 15:31:07 +0100222 mActiveEffectId = EFFECT_INDEX(effect, strength);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100223
Caleb Connolly542816b2021-08-12 15:31:07 +0100224 // Play the effect
225 on(timeMs);
226 /*
227 * Android calls off() for us, so
228 * only call it if we're doing a double click
229 */
230 if (doubleClick) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100231 timeMs += 59;
232 usleep(50 * 1000);
233 on(timeMs);
Caleb Connolly542816b2021-08-12 15:31:07 +0100234 }
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100235 mActiveEffectId = -1;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100236
Caleb Connolly542816b2021-08-12 15:31:07 +0100237 _hidl_cb(status, timeMs);
238
239 return Void();
Caleb Connollyc445fab2021-08-11 14:42:57 +0100240}
241
242Vibrator::~Vibrator() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100243 close(mfd);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100244}
245
246
247} // namespace implementation
248} // namespace V1_1
249} // namespace vibrator
250} // namespace hardware
251} // namespace android