blob: 96eef0751451c62a681b3bfa7ca8083c51bc8c55 [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.
Caleb Connolly9cf6ac12021-10-29 20:36:23 +010062 if (mInputDevPath.length() < 2) {
63 mIsStub = true;
64 return;
65 }
Caleb Connolly542816b2021-08-12 15:31:07 +010066 mfd = openInputDev();
67 if (mfd < 0) {
68 ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str());
69 return;
70 }
Caleb Connollyc445fab2021-08-11 14:42:57 +010071
Caleb Connolly542816b2021-08-12 15:31:07 +010072 // Build a table of effects for each strength of each effect.
73 int baseStrength = 0;
74 for (uint16_t i = 0; i <= (uint16_t)MAX_EFFECT; i++)
75 {
76 for (uint16_t j = 0; j <= (uint16_t)MAX_EFFECT_STRENGTH; j++)
77 {
78 switch((Effect)i) {
79 default:
80 case Effect::CLICK:
81 baseStrength = 0x4000;
82 break;
83 case Effect::TICK:
84 baseStrength = 0x1000;
85 break;
86 case Effect::DOUBLE_CLICK:
87 baseStrength = 0x3000;
88 break;
89 }
90 ALOGV("%s() uploading effect %d, strength %d, magnitude = 0x%x", __func__, i, j, baseStrength + j * 0x1000);
91 // 0x8000 is about the max for qcom_spmi_haptics.
92 mEffects[EFFECT_INDEX(i, j)] = FF_EFFECT((uint16_t)(baseStrength + j * 0x800));
93
94 uploadEffectToKernel(&mEffects[EFFECT_INDEX(i, j)]);
95 }
Caleb Connollyc445fab2021-08-11 14:42:57 +010096 }
97}
98
99int Vibrator::openInputDev() {
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100100 if (mIsStub)
101 return 0;
Caleb Connolly542816b2021-08-12 15:31:07 +0100102 return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100103}
104
Caleb Connolly542816b2021-08-12 15:31:07 +0100105void Vibrator::uploadEffectToKernel(struct ff_effect* effect) {
106 int ret;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100107 if (mIsStub)
108 return;
Caleb Connolly542816b2021-08-12 15:31:07 +0100109
110 ret = ioctl(mfd, EVIOCSFF, effect);
111 if (ret < 0) {
112 ALOGE("%s() Couldn't upload rumble effect to device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100113 }
Caleb Connolly542816b2021-08-12 15:31:07 +0100114}
Caleb Connollyc445fab2021-08-11 14:42:57 +0100115
Caleb Connolly542816b2021-08-12 15:31:07 +0100116void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect
117 int ret;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100118 if (mIsStub)
119 return;
Caleb Connolly542816b2021-08-12 15:31:07 +0100120
121 ret = ioctl(mfd, EVIOCRMFF, effect);
122 if (ret < 0) {
123 ALOGE("%s() Failed to remove rumble effect from device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
124 return;
125 }
126 effect->id = -1;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100127}
128
129// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
130Return<Status> Vibrator::on(uint32_t timeoutMs) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100131 struct ff_effect* effect;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100132 if (mIsStub)
133 return Status::OK;
Caleb Connolly542816b2021-08-12 15:31:07 +0100134 // If the active effect is set, use it instead of the default
135 if (mActiveEffectId < 0) {
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100136 effect = &mEffects[DEFAULT_EFFECT_ID];
Caleb Connolly542816b2021-08-12 15:31:07 +0100137 } else {
138 effect = &mEffects[mActiveEffectId];
139 }
140
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100141 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 +0100142 struct input_event play;
143 int ret;
144
145 play.type = EV_FF;
146 play.code = effect->id;
147 play.value = 1;
148
149 ret = write(mfd, (const void*) &play, sizeof(play));
150 if (ret != sizeof(play)) {
151 ALOGE("%s() failed to fully write play event to input device: %d", __func__, errno);
152 return Status::UNKNOWN_ERROR;
153 }
154
155 usleep(timeoutMs * 1000);
156
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100157 off();
158
Caleb Connolly542816b2021-08-12 15:31:07 +0100159 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100160}
161
162Return<Status> Vibrator::off() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100163 ALOGV("%s", __func__);
164 struct input_event stop;
165 struct ff_effect* effect;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100166 if (mIsStub)
167 return Status::OK;
Caleb Connolly542816b2021-08-12 15:31:07 +0100168 // If the active effect is set, use it instead of the default
169 if (mActiveEffectId < 0) {
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100170 ALOGV("%s() no active effect, stopping default", __func__);
171 effect = &mEffects[DEFAULT_EFFECT_ID];
Caleb Connolly542816b2021-08-12 15:31:07 +0100172 } else {
173 effect = &mEffects[mActiveEffectId];
Caleb Connollyc445fab2021-08-11 14:42:57 +0100174 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100175
Caleb Connolly542816b2021-08-12 15:31:07 +0100176 stop.type = EV_FF;
177 stop.code = effect->id;
178 stop.value = 0;
179 if (write(mfd, (const void*) &stop, sizeof(stop)) != sizeof(stop)) {
180 ALOGE("%s() failed to fully write stop event to input device", __func__);
181 return Status::UNKNOWN_ERROR;
182 }
183
184 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100185}
186
187Return<bool> Vibrator::supportsAmplitudeControl() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100188 ALOGV("%s", __func__);
189 return false;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100190}
191
192Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100193 ALOGV("%s", __func__);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100194
Caleb Connolly542816b2021-08-12 15:31:07 +0100195 if (!amplitude) {
196 return Status::BAD_VALUE;
197 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100198
Caleb Connolly542816b2021-08-12 15:31:07 +0100199 return Status::OK;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100200}
201
202Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100203 perform_cb _hidl_cb) {
204 return performWrapper(effect, strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100205}
206
207Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100208 perform_cb _hidl_cb) {
209 return performWrapper(effect, strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100210}
211
212template <typename T>
213Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100214 ALOGV("%s, ", __func__);
215 auto validRange = hidl_enum_range<T>();
216 if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
217 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
218 return Void();
219 }
220 return performEffect(static_cast<Effect>(effect), strength, _hidl_cb);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100221}
222
223Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength,
Caleb Connolly542816b2021-08-12 15:31:07 +0100224 perform_cb _hidl_cb) {
225 Status status = Status::OK;
226 uint32_t timeMs = 9;
227 bool doubleClick = effect == Effect::DOUBLE_CLICK;
Caleb Connolly9cf6ac12021-10-29 20:36:23 +0100228 if (mIsStub)
229 return Void();
Caleb Connollyc445fab2021-08-11 14:42:57 +0100230
Caleb Connolly542816b2021-08-12 15:31:07 +0100231 ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100232
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100233 if (effect > MAX_EFFECT){
Caleb Connolly542816b2021-08-12 15:31:07 +0100234 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
235 return Void();
236 }
Caleb Connollyc445fab2021-08-11 14:42:57 +0100237
Caleb Connolly542816b2021-08-12 15:31:07 +0100238 mActiveEffectId = EFFECT_INDEX(effect, strength);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100239
Caleb Connolly542816b2021-08-12 15:31:07 +0100240 // Play the effect
241 on(timeMs);
242 /*
243 * Android calls off() for us, so
244 * only call it if we're doing a double click
245 */
246 if (doubleClick) {
Caleb Connolly542816b2021-08-12 15:31:07 +0100247 timeMs += 59;
248 usleep(50 * 1000);
249 on(timeMs);
Caleb Connolly542816b2021-08-12 15:31:07 +0100250 }
Caleb Connollyad8a8b52021-09-14 16:28:36 +0100251 mActiveEffectId = -1;
Caleb Connollyc445fab2021-08-11 14:42:57 +0100252
Caleb Connolly542816b2021-08-12 15:31:07 +0100253 _hidl_cb(status, timeMs);
254
255 return Void();
Caleb Connollyc445fab2021-08-11 14:42:57 +0100256}
257
258Vibrator::~Vibrator() {
Caleb Connolly542816b2021-08-12 15:31:07 +0100259 close(mfd);
Caleb Connollyc445fab2021-08-11 14:42:57 +0100260}
261
262
263} // namespace implementation
264} // namespace V1_1
265} // namespace vibrator
266} // namespace hardware
267} // namespace android