blob: 50aef4f679e739a380f3b2e4b5016b41e6ff18f2 [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 Connolly0ffa5d52021-11-20 05:17:36 +000048/*
49 * These have to be kept in line with HAL version
50 */
Caleb Connolly542816b2021-08-12 15:31:07 +010051static constexpr Effect MAX_EFFECT = Effect::TICK;
52static constexpr EffectStrength MAX_EFFECT_STRENGTH = EffectStrength::STRONG;
53
Caleb Connolly0ffa5d52021-11-20 05:17:36 +000054/**
55 * Helper to index effects in the mEffects map.
56 * from their strength and effect type.
57 */
Caleb Connolly542816b2021-08-12 15:31:07 +010058#define EFFECT_INDEX(effect, strength) \
59 ((uint16_t)effect * (uint16_t)MAX_EFFECT_STRENGTH + (uint16_t)strength)
Caleb Connollyc445fab2021-08-11 14:42:57 +010060
Caleb Connollyad8a8b52021-09-14 16:28:36 +010061static constexpr int DEFAULT_EFFECT_ID = EFFECT_INDEX(Effect::CLICK, EffectStrength::MEDIUM);
62
Caleb Connollyc445fab2021-08-11 14:42:57 +010063Vibrator::Vibrator(std::string devpath) :
Caleb Connolly542816b2021-08-12 15:31:07 +010064 mInputDevPath(devpath)
Caleb Connollyc445fab2021-08-11 14:42:57 +010065{
Caleb Connolly542816b2021-08-12 15:31:07 +010066 // We just keep the input device open the whole time we're running.
67 // Closing / reopening it seems to break things.
Caleb Connolly9cf6ac12021-10-29 20:36:23 +010068 if (mInputDevPath.length() < 2) {
69 mIsStub = true;
70 return;
71 }
Caleb Connolly542816b2021-08-12 15:31:07 +010072 mfd = openInputDev();
73 if (mfd < 0) {
74 ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str());
75 return;
76 }
Caleb Connollyc445fab2021-08-11 14:42:57 +010077
Caleb Connolly542816b2021-08-12 15:31:07 +010078 // Build a table of effects for each strength of each effect.
79 int baseStrength = 0;
80 for (uint16_t i = 0; i <= (uint16_t)MAX_EFFECT; i++)
81 {
82 for (uint16_t j = 0; j <= (uint16_t)MAX_EFFECT_STRENGTH; j++)
83 {
84 switch((Effect)i) {
85 default:
86 case Effect::CLICK:
87 baseStrength = 0x4000;
88 break;
89 case Effect::TICK:
90 baseStrength = 0x1000;
91 break;
92 case Effect::DOUBLE_CLICK:
93 baseStrength = 0x3000;
94 break;
95 }
96 ALOGV("%s() uploading effect %d, strength %d, magnitude = 0x%x", __func__, i, j, baseStrength + j * 0x1000);
97 // 0x8000 is about the max for qcom_spmi_haptics.
98 mEffects[EFFECT_INDEX(i, j)] = FF_EFFECT((uint16_t)(baseStrength + j * 0x800));
99
100 uploadEffectToKernel(&mEffects[EFFECT_INDEX(i, j)]);
101 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100102 }
103}
104
105int Vibrator::openInputDev() {
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100106 if (mIsStub)
107 return 0;
Caleb Connolly542816b2021-08-12 15:31:07 +0100108 return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100109}
110
Caleb Connolly542816b2021-08-12 15:31:07 +0100111void Vibrator::uploadEffectToKernel(struct ff_effect* effect) {
112 int ret;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100113 if (mIsStub)
114 return;
Caleb Connolly542816b2021-08-12 15:31:07 +0100115
116 ret = ioctl(mfd, EVIOCSFF, effect);
117 if (ret < 0) {
118 ALOGE("%s() Couldn't upload rumble effect to device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100119 }
Caleb Connolly542816b2021-08-12 15:31:07 +0100120}
Caleb Connollyc445fab2021-08-11 14:42:57 +0100121
Caleb Connolly542816b2021-08-12 15:31:07 +0100122void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect
123 int ret;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100124 if (mIsStub)
125 return;
Caleb Connolly542816b2021-08-12 15:31:07 +0100126
127 ret = ioctl(mfd, EVIOCRMFF, effect);
128 if (ret < 0) {
129 ALOGE("%s() Failed to remove rumble effect from device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
130 return;
131 }
132 effect->id = -1;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100133}
134
135// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
136Return<Status> Vibrator::on(uint32_t timeoutMs) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100137 struct ff_effect* effect;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100138 if (mIsStub)
139 return Status::OK;
Caleb Connolly542816b2021-08-12 15:31:07 +0100140 // If the active effect is set, use it instead of the default
141 if (mActiveEffectId < 0) {
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100142 effect = &mEffects[DEFAULT_EFFECT_ID];
Caleb Connolly542816b2021-08-12 15:31:07 +0100143 } else {
144 effect = &mEffects[mActiveEffectId];
145 }
146
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100147 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 +0100148 struct input_event play;
149 int ret;
150
151 play.type = EV_FF;
152 play.code = effect->id;
153 play.value = 1;
154
155 ret = write(mfd, (const void*) &play, sizeof(play));
156 if (ret != sizeof(play)) {
157 ALOGE("%s() failed to fully write play event to input device: %d", __func__, errno);
158 return Status::UNKNOWN_ERROR;
159 }
160
161 usleep(timeoutMs * 1000);
162
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100163 off();
164
Caleb Connolly542816b2021-08-12 15:31:07 +0100165 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100166}
167
168Return<Status> Vibrator::off() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100169 ALOGV("%s", __func__);
170 struct input_event stop;
171 struct ff_effect* effect;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100172 if (mIsStub)
173 return Status::OK;
Caleb Connolly542816b2021-08-12 15:31:07 +0100174 // If the active effect is set, use it instead of the default
175 if (mActiveEffectId < 0) {
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100176 ALOGV("%s() no active effect, stopping default", __func__);
177 effect = &mEffects[DEFAULT_EFFECT_ID];
Caleb Connolly542816b2021-08-12 15:31:07 +0100178 } else {
179 effect = &mEffects[mActiveEffectId];
Caleb Connollyc445fab2021-08-11 14:42:57 +0100180 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100181
Caleb Connolly542816b2021-08-12 15:31:07 +0100182 stop.type = EV_FF;
183 stop.code = effect->id;
184 stop.value = 0;
185 if (write(mfd, (const void*) &stop, sizeof(stop)) != sizeof(stop)) {
186 ALOGE("%s() failed to fully write stop event to input device", __func__);
187 return Status::UNKNOWN_ERROR;
188 }
189
190 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100191}
192
193Return<bool> Vibrator::supportsAmplitudeControl() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100194 ALOGV("%s", __func__);
195 return false;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100196}
197
198Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100199 ALOGV("%s", __func__);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100200
Caleb Connolly542816b2021-08-12 15:31:07 +0100201 if (!amplitude) {
202 return Status::BAD_VALUE;
203 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100204
Caleb Connolly542816b2021-08-12 15:31:07 +0100205 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100206}
207
208Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100209 perform_cb _hidl_cb) {
210 return performWrapper(effect, strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100211}
212
213Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100214 perform_cb _hidl_cb) {
215 return performWrapper(effect, strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100216}
217
218template <typename T>
219Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100220 ALOGV("%s, ", __func__);
221 auto validRange = hidl_enum_range<T>();
222 if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
223 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
224 return Void();
225 }
226 return performEffect(static_cast<Effect>(effect), strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100227}
228
229Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100230 perform_cb _hidl_cb) {
231 Status status = Status::OK;
232 uint32_t timeMs = 9;
233 bool doubleClick = effect == Effect::DOUBLE_CLICK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100234
Caleb Connolly542816b2021-08-12 15:31:07 +0100235 ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100236
Caleb Connolly0ffa5d52021-11-20 05:17:36 +0000237 if (effect > MAX_EFFECT || mIsStub){
Caleb Connolly542816b2021-08-12 15:31:07 +0100238 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
239 return Void();
240 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100241
Caleb Connolly542816b2021-08-12 15:31:07 +0100242 mActiveEffectId = EFFECT_INDEX(effect, strength);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100243
Caleb Connolly542816b2021-08-12 15:31:07 +0100244 // Play the effect
245 on(timeMs);
246 /*
247 * Android calls off() for us, so
248 * only call it if we're doing a double click
249 */
250 if (doubleClick) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100251 timeMs += 59;
252 usleep(50 * 1000);
253 on(timeMs);
Caleb Connolly542816b2021-08-12 15:31:07 +0100254 }
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100255 mActiveEffectId = -1;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100256
Caleb Connolly542816b2021-08-12 15:31:07 +0100257 _hidl_cb(status, timeMs);
258
259 return Void();
Caleb Connollyc445fab2021-08-11 14:42:57 +0100260}
261
262Vibrator::~Vibrator() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100263 close(mfd);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100264}
265
266
267} // namespace implementation
268} // namespace V1_1
269} // namespace vibrator
270} // namespace hardware
271} // namespace android