Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 1 | /* |
| 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace hardware { |
| 40 | namespace vibrator { |
| 41 | namespace V1_1 { |
| 42 | namespace implementation { |
| 43 | |
| 44 | using Status = ::android::hardware::vibrator::V1_0::Status; |
| 45 | using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength; |
| 46 | using Effect = ::android::hardware::vibrator::V1_1::Effect_1_1; |
| 47 | |
Caleb Connolly | 0ffa5d5 | 2021-11-20 05:17:36 +0000 | [diff] [blame^] | 48 | /* |
| 49 | * These have to be kept in line with HAL version |
| 50 | */ |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 51 | static constexpr Effect MAX_EFFECT = Effect::TICK; |
| 52 | static constexpr EffectStrength MAX_EFFECT_STRENGTH = EffectStrength::STRONG; |
| 53 | |
Caleb Connolly | 0ffa5d5 | 2021-11-20 05:17:36 +0000 | [diff] [blame^] | 54 | /** |
| 55 | * Helper to index effects in the mEffects map. |
| 56 | * from their strength and effect type. |
| 57 | */ |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 58 | #define EFFECT_INDEX(effect, strength) \ |
| 59 | ((uint16_t)effect * (uint16_t)MAX_EFFECT_STRENGTH + (uint16_t)strength) |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 60 | |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 61 | static constexpr int DEFAULT_EFFECT_ID = EFFECT_INDEX(Effect::CLICK, EffectStrength::MEDIUM); |
| 62 | |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 63 | Vibrator::Vibrator(std::string devpath) : |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 64 | mInputDevPath(devpath) |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 65 | { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 66 | // We just keep the input device open the whole time we're running. |
| 67 | // Closing / reopening it seems to break things. |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame] | 68 | if (mInputDevPath.length() < 2) { |
| 69 | mIsStub = true; |
| 70 | return; |
| 71 | } |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 72 | mfd = openInputDev(); |
| 73 | if (mfd < 0) { |
| 74 | ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str()); |
| 75 | return; |
| 76 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 77 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 78 | // 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | int Vibrator::openInputDev() { |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame] | 106 | if (mIsStub) |
| 107 | return 0; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 108 | return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 111 | void Vibrator::uploadEffectToKernel(struct ff_effect* effect) { |
| 112 | int ret; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame] | 113 | if (mIsStub) |
| 114 | return; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 115 | |
| 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 119 | } |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 120 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 121 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 122 | void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect |
| 123 | int ret; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame] | 124 | if (mIsStub) |
| 125 | return; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 126 | |
| 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Methods from ::android::hardware::vibrator::V1_1::IVibrator follow. |
| 136 | Return<Status> Vibrator::on(uint32_t timeoutMs) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 137 | struct ff_effect* effect; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame] | 138 | if (mIsStub) |
| 139 | return Status::OK; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 140 | // If the active effect is set, use it instead of the default |
| 141 | if (mActiveEffectId < 0) { |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 142 | effect = &mEffects[DEFAULT_EFFECT_ID]; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 143 | } else { |
| 144 | effect = &mEffects[mActiveEffectId]; |
| 145 | } |
| 146 | |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 147 | ALOGV("%s() mActiveEffectId = %d, timeoutMs = %d, effect.id = %d, magnitude = %d", __func__, mActiveEffectId, timeoutMs, effect->id, effect->u.rumble.strong_magnitude); |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 148 | 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 Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 163 | off(); |
| 164 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 165 | return Status::OK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | Return<Status> Vibrator::off() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 169 | ALOGV("%s", __func__); |
| 170 | struct input_event stop; |
| 171 | struct ff_effect* effect; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame] | 172 | if (mIsStub) |
| 173 | return Status::OK; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 174 | // If the active effect is set, use it instead of the default |
| 175 | if (mActiveEffectId < 0) { |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 176 | ALOGV("%s() no active effect, stopping default", __func__); |
| 177 | effect = &mEffects[DEFAULT_EFFECT_ID]; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 178 | } else { |
| 179 | effect = &mEffects[mActiveEffectId]; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 180 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 181 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 182 | 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | Return<bool> Vibrator::supportsAmplitudeControl() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 194 | ALOGV("%s", __func__); |
| 195 | return false; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | Return<Status> Vibrator::setAmplitude(uint8_t amplitude) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 199 | ALOGV("%s", __func__); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 200 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 201 | if (!amplitude) { |
| 202 | return Status::BAD_VALUE; |
| 203 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 204 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 205 | return Status::OK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength, |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 209 | perform_cb _hidl_cb) { |
| 210 | return performWrapper(effect, strength, _hidl_cb); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength, |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 214 | perform_cb _hidl_cb) { |
| 215 | return performWrapper(effect, strength, _hidl_cb); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | template <typename T> |
| 219 | Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 220 | 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength, |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 230 | perform_cb _hidl_cb) { |
| 231 | Status status = Status::OK; |
| 232 | uint32_t timeMs = 9; |
| 233 | bool doubleClick = effect == Effect::DOUBLE_CLICK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 234 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 235 | ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 236 | |
Caleb Connolly | 0ffa5d5 | 2021-11-20 05:17:36 +0000 | [diff] [blame^] | 237 | if (effect > MAX_EFFECT || mIsStub){ |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 238 | _hidl_cb(Status::UNSUPPORTED_OPERATION, 0); |
| 239 | return Void(); |
| 240 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 241 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 242 | mActiveEffectId = EFFECT_INDEX(effect, strength); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 243 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 244 | // 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 Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 251 | timeMs += 59; |
| 252 | usleep(50 * 1000); |
| 253 | on(timeMs); |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 254 | } |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 255 | mActiveEffectId = -1; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 256 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 257 | _hidl_cb(status, timeMs); |
| 258 | |
| 259 | return Void(); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | Vibrator::~Vibrator() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 263 | close(mfd); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
| 267 | } // namespace implementation |
| 268 | } // namespace V1_1 |
| 269 | } // namespace vibrator |
| 270 | } // namespace hardware |
| 271 | } // namespace android |