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