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 | |
| 38 | |
| 39 | namespace android { |
| 40 | namespace hardware { |
| 41 | namespace vibrator { |
| 42 | namespace V1_1 { |
| 43 | namespace implementation { |
| 44 | |
| 45 | using Status = ::android::hardware::vibrator::V1_0::Status; |
| 46 | using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength; |
| 47 | using Effect = ::android::hardware::vibrator::V1_1::Effect_1_1; |
| 48 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 49 | // These have to be kept in line with HAL version |
| 50 | static constexpr Effect MAX_EFFECT = Effect::TICK; |
| 51 | static constexpr EffectStrength MAX_EFFECT_STRENGTH = EffectStrength::STRONG; |
| 52 | |
| 53 | #define EFFECT_INDEX(effect, strength) \ |
| 54 | ((uint16_t)effect * (uint16_t)MAX_EFFECT_STRENGTH + (uint16_t)strength) |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 55 | |
| 56 | Vibrator::Vibrator(std::string devpath) : |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 57 | mInputDevPath(devpath) |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 58 | { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 59 | // We just keep the input device open the whole time we're running. |
| 60 | // Closing / reopening it seems to break things. |
| 61 | mfd = openInputDev(); |
| 62 | if (mfd < 0) { |
| 63 | ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str()); |
| 64 | return; |
| 65 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 66 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 67 | // Build a table of effects for each strength of each effect. |
| 68 | int baseStrength = 0; |
| 69 | for (uint16_t i = 0; i <= (uint16_t)MAX_EFFECT; i++) |
| 70 | { |
| 71 | for (uint16_t j = 0; j <= (uint16_t)MAX_EFFECT_STRENGTH; j++) |
| 72 | { |
| 73 | switch((Effect)i) { |
| 74 | default: |
| 75 | case Effect::CLICK: |
| 76 | baseStrength = 0x4000; |
| 77 | break; |
| 78 | case Effect::TICK: |
| 79 | baseStrength = 0x1000; |
| 80 | break; |
| 81 | case Effect::DOUBLE_CLICK: |
| 82 | baseStrength = 0x3000; |
| 83 | break; |
| 84 | } |
| 85 | ALOGV("%s() uploading effect %d, strength %d, magnitude = 0x%x", __func__, i, j, baseStrength + j * 0x1000); |
| 86 | // 0x8000 is about the max for qcom_spmi_haptics. |
| 87 | mEffects[EFFECT_INDEX(i, j)] = FF_EFFECT((uint16_t)(baseStrength + j * 0x800)); |
| 88 | |
| 89 | uploadEffectToKernel(&mEffects[EFFECT_INDEX(i, j)]); |
| 90 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | int Vibrator::openInputDev() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 95 | return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 98 | void Vibrator::uploadEffectToKernel(struct ff_effect* effect) { |
| 99 | int ret; |
| 100 | |
| 101 | ret = ioctl(mfd, EVIOCSFF, effect); |
| 102 | if (ret < 0) { |
| 103 | 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] | 104 | } |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 105 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 106 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 107 | void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect |
| 108 | int ret; |
| 109 | |
| 110 | ret = ioctl(mfd, EVIOCRMFF, effect); |
| 111 | if (ret < 0) { |
| 112 | ALOGE("%s() Failed to remove rumble effect from device %s, ret = %d", __func__, mInputDevPath.c_str(), ret); |
| 113 | return; |
| 114 | } |
| 115 | effect->id = -1; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Methods from ::android::hardware::vibrator::V1_1::IVibrator follow. |
| 119 | Return<Status> Vibrator::on(uint32_t timeoutMs) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 120 | struct ff_effect* effect; |
| 121 | // If the active effect is set, use it instead of the default |
| 122 | if (mActiveEffectId < 0) { |
| 123 | effect = &mEffects[EFFECT_INDEX(Effect::CLICK , EffectStrength::MEDIUM)]; |
| 124 | } else { |
| 125 | effect = &mEffects[mActiveEffectId]; |
| 126 | } |
| 127 | |
| 128 | ALOGV("%s, timeoutMs = %d, effect.id = %d, magnitude = %d", __func__, timeoutMs, effect->id, effect->u.rumble.strong_magnitude); |
| 129 | struct input_event play; |
| 130 | int ret; |
| 131 | |
| 132 | play.type = EV_FF; |
| 133 | play.code = effect->id; |
| 134 | play.value = 1; |
| 135 | |
| 136 | ret = write(mfd, (const void*) &play, sizeof(play)); |
| 137 | if (ret != sizeof(play)) { |
| 138 | ALOGE("%s() failed to fully write play event to input device: %d", __func__, errno); |
| 139 | return Status::UNKNOWN_ERROR; |
| 140 | } |
| 141 | |
| 142 | usleep(timeoutMs * 1000); |
| 143 | |
| 144 | return Status::OK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | Return<Status> Vibrator::off() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 148 | ALOGV("%s", __func__); |
| 149 | struct input_event stop; |
| 150 | struct ff_effect* effect; |
| 151 | // If the active effect is set, use it instead of the default |
| 152 | if (mActiveEffectId < 0) { |
| 153 | effect = &mEffects[(uint16_t)Effect::CLICK * (uint16_t)EffectStrength::STRONG]; |
| 154 | } else { |
| 155 | effect = &mEffects[mActiveEffectId]; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 156 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 157 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 158 | stop.type = EV_FF; |
| 159 | stop.code = effect->id; |
| 160 | stop.value = 0; |
| 161 | if (write(mfd, (const void*) &stop, sizeof(stop)) != sizeof(stop)) { |
| 162 | ALOGE("%s() failed to fully write stop event to input device", __func__); |
| 163 | return Status::UNKNOWN_ERROR; |
| 164 | } |
| 165 | |
| 166 | return Status::OK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | Return<bool> Vibrator::supportsAmplitudeControl() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 170 | ALOGV("%s", __func__); |
| 171 | return false; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | Return<Status> Vibrator::setAmplitude(uint8_t amplitude) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 175 | ALOGV("%s", __func__); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 176 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 177 | if (!amplitude) { |
| 178 | return Status::BAD_VALUE; |
| 179 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 180 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 181 | return Status::OK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength, |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 185 | perform_cb _hidl_cb) { |
| 186 | return performWrapper(effect, strength, _hidl_cb); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | 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] | 190 | perform_cb _hidl_cb) { |
| 191 | return performWrapper(effect, strength, _hidl_cb); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | template <typename T> |
| 195 | Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 196 | ALOGV("%s, ", __func__); |
| 197 | auto validRange = hidl_enum_range<T>(); |
| 198 | if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) { |
| 199 | _hidl_cb(Status::UNSUPPORTED_OPERATION, 0); |
| 200 | return Void(); |
| 201 | } |
| 202 | return performEffect(static_cast<Effect>(effect), strength, _hidl_cb); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength, |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 206 | perform_cb _hidl_cb) { |
| 207 | Status status = Status::OK; |
| 208 | uint32_t timeMs = 9; |
| 209 | bool doubleClick = effect == Effect::DOUBLE_CLICK; |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 210 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 211 | ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 212 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 213 | if (effect > Effect::TICK){ |
| 214 | _hidl_cb(Status::UNSUPPORTED_OPERATION, 0); |
| 215 | return Void(); |
| 216 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 217 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 218 | mActiveEffectId = EFFECT_INDEX(effect, strength); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 219 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 220 | // Play the effect |
| 221 | on(timeMs); |
| 222 | /* |
| 223 | * Android calls off() for us, so |
| 224 | * only call it if we're doing a double click |
| 225 | */ |
| 226 | if (doubleClick) { |
| 227 | off(); |
| 228 | timeMs += 59; |
| 229 | usleep(50 * 1000); |
| 230 | on(timeMs); |
| 231 | mActiveEffectId = -1; // Reset the active effectId |
| 232 | } |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 233 | |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 234 | _hidl_cb(status, timeMs); |
| 235 | |
| 236 | return Void(); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | Vibrator::~Vibrator() { |
Caleb Connolly | 542816b | 2021-08-12 15:31:07 +0100 | [diff] [blame] | 240 | close(mfd); |
Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | |
| 244 | } // namespace implementation |
| 245 | } // namespace V1_1 |
| 246 | } // namespace vibrator |
| 247 | } // namespace hardware |
| 248 | } // namespace android |