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 | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 48 | // These have to be kept in line with HAL version |
| 49 | static constexpr Effect MAX_EFFECT = Effect::TICK; |
| 50 | static 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 54 | |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 55 | static constexpr int DEFAULT_EFFECT_ID = EFFECT_INDEX(Effect::CLICK, EffectStrength::MEDIUM); |
| 56 | |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 57 | Vibrator::Vibrator(std::string devpath) : |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 58 | mInputDevPath(devpath) |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 59 | { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 60 | // We just keep the input device open the whole time we're running. |
| 61 | // Closing / reopening it seems to break things. |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame^] | 62 | if (mInputDevPath.length() < 2) { |
| 63 | mIsStub = true; |
| 64 | return; |
| 65 | } |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 66 | mfd = openInputDev(); |
| 67 | if (mfd < 0) { |
| 68 | ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str()); |
| 69 | return; |
| 70 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 71 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 72 | // 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | int Vibrator::openInputDev() { |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame^] | 100 | if (mIsStub) |
| 101 | return 0; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 102 | return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 105 | void Vibrator::uploadEffectToKernel(struct ff_effect* effect) { |
| 106 | int ret; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame^] | 107 | if (mIsStub) |
| 108 | return; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 109 | |
| 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 113 | } |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 114 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 115 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 116 | void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect |
| 117 | int ret; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame^] | 118 | if (mIsStub) |
| 119 | return; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 120 | |
| 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Methods from ::android::hardware::vibrator::V1_1::IVibrator follow. |
| 130 | Return<Status> Vibrator::on(uint32_t timeoutMs) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 131 | struct ff_effect* effect; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame^] | 132 | if (mIsStub) |
| 133 | return Status::OK; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 134 | // If the active effect is set, use it instead of the default |
| 135 | if (mActiveEffectId < 0) { |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 136 | effect = &mEffects[DEFAULT_EFFECT_ID]; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 137 | } else { |
| 138 | effect = &mEffects[mActiveEffectId]; |
| 139 | } |
| 140 | |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 141 | 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] | 142 | 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 Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 157 | off(); |
| 158 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 159 | return Status::OK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Return<Status> Vibrator::off() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 163 | ALOGV("%s", __func__); |
| 164 | struct input_event stop; |
| 165 | struct ff_effect* effect; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame^] | 166 | if (mIsStub) |
| 167 | return Status::OK; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 168 | // If the active effect is set, use it instead of the default |
| 169 | if (mActiveEffectId < 0) { |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 170 | ALOGV("%s() no active effect, stopping default", __func__); |
| 171 | effect = &mEffects[DEFAULT_EFFECT_ID]; |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 172 | } else { |
| 173 | effect = &mEffects[mActiveEffectId]; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 174 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 175 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 176 | 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | Return<bool> Vibrator::supportsAmplitudeControl() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 188 | ALOGV("%s", __func__); |
| 189 | return false; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | Return<Status> Vibrator::setAmplitude(uint8_t amplitude) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 193 | ALOGV("%s", __func__); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 194 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 195 | if (!amplitude) { |
| 196 | return Status::BAD_VALUE; |
| 197 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 198 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 199 | return Status::OK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength, |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 203 | perform_cb _hidl_cb) { |
| 204 | return performWrapper(effect, strength, _hidl_cb); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | 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] | 208 | perform_cb _hidl_cb) { |
| 209 | return performWrapper(effect, strength, _hidl_cb); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | template <typename T> |
| 213 | Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 214 | 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 Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength, |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 224 | perform_cb _hidl_cb) { |
| 225 | Status status = Status::OK; |
| 226 | uint32_t timeMs = 9; |
| 227 | bool doubleClick = effect == Effect::DOUBLE_CLICK; |
Caleb Connolly | 9cf6ac1 | 2021-10-29 20:36:23 +0100 | [diff] [blame^] | 228 | if (mIsStub) |
| 229 | return Void(); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 230 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 231 | ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 232 | |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 233 | if (effect > MAX_EFFECT){ |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 234 | _hidl_cb(Status::UNSUPPORTED_OPERATION, 0); |
| 235 | return Void(); |
| 236 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 237 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 238 | mActiveEffectId = EFFECT_INDEX(effect, strength); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 239 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 240 | // 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 Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 247 | timeMs += 59; |
| 248 | usleep(50 * 1000); |
| 249 | on(timeMs); |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 250 | } |
Caleb Connolly | ad8a8b5 | 2021-09-14 16:28:36 +0100 | [diff] [blame] | 251 | mActiveEffectId = -1; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 252 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 253 | _hidl_cb(status, timeMs); |
| 254 | |
| 255 | return Void(); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | Vibrator::~Vibrator() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 259 | close(mfd); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | } // namespace implementation |
| 264 | } // namespace V1_1 |
| 265 | } // namespace vibrator |
| 266 | } // namespace hardware |
| 267 | } // namespace android |