blob: 3d1b4136745a18992a5c0975326c2f342dc77852 [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
38
39namespace android {
40namespace hardware {
41namespace vibrator {
42namespace V1_1 {
43namespace implementation {
44
45using Status = ::android::hardware::vibrator::V1_0::Status;
46using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
47using Effect = ::android::hardware::vibrator::V1_1::Effect_1_1;
48
49static constexpr int8_t MAX_TRIGGER_LATENCY_MS = 6;
50
51Vibrator::Vibrator(std::string devpath) :
52 mInputDevPath(devpath)
53{
54 std::memset(&mEffect, 0, sizeof(mEffect));
55 mEffect.type = FF_RUMBLE;
56 mEffect.id = -1;
57
58 mfd = openInputDev();
59 if (mfd < 0) {
60 ALOGE("CA:: %s() can't open FF input device %s", __func__, mInputDevPath.c_str());
61 return;
62 }
63}
64
65int Vibrator::openInputDev() {
66 return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC);
67}
68
69Return<Status> Vibrator::on(uint32_t timeoutMs, uint32_t playCount) {
70 ALOGI("CA:: %s, timeoutMs = %d, playCount = %d, effect.id = %d, strong magnitude = %d, weak magnitude = %d", __func__, timeoutMs, playCount, mEffect.id, mEffect.u.rumble.strong_magnitude, mEffect.u.rumble.weak_magnitude);
71 struct input_event play;
72 int ret;
73
74 // Upload Rumble effect
75 ret = ioctl(mfd, EVIOCSFF, &mEffect);
76 if (ret < 0) {
77 ALOGE("CA:: %s() Couldn't upload rumble effect to device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
78 return Status::UNKNOWN_ERROR;
79 }
80
81 play.type = EV_FF;
82 play.code = mEffect.id;
83 play.value = playCount;
84 ret = write(mfd, (const void*) &play, sizeof(play));
85 if (ret != sizeof(play)) {
86 ALOGE("CA:: %s() failed to fully write play event to input device: %d", __func__, errno);
87 return Status::UNKNOWN_ERROR;
88 }
89
90 usleep(timeoutMs * 1000);
91
92 return Status::OK;
93}
94
95// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
96Return<Status> Vibrator::on(uint32_t timeoutMs) {
97 return(on(timeoutMs, 1));
98}
99
100Return<Status> Vibrator::off() {
101 ALOGI("CA:: %s", __func__);
102 struct input_event stop;
103 int ret;
104
105 stop.type = EV_FF;
106 stop.code = mEffect.id;
107 stop.value = 0;
108 if (write(mfd, (const void*) &stop, sizeof(stop)) != sizeof(stop)) {
109 ALOGE("CA:: %s() failed to fully write stop event to input device", __func__);
110 return Status::UNKNOWN_ERROR;
111 }
112
113 // Unload rumble effect
114 ret = ioctl(mfd, EVIOCRMFF, &mEffect);
115 if (ret < 0) {
116 ALOGE("CA:: %s() Failed to remove rumble effect from device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
117 return Status::UNKNOWN_ERROR;
118 }
119 mEffect.id = -1;
120
121 return Status::OK;
122}
123
124Return<bool> Vibrator::supportsAmplitudeControl() {
125 ALOGI("CA:: %s", __func__);
126 return false;
127}
128
129Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
130 ALOGI("CA:: %s", __func__);
131
132 if (!amplitude) {
133 return Status::BAD_VALUE;
134 }
135
136 return Status::OK;
137}
138
139Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength,
140 perform_cb _hidl_cb) {
141 return performWrapper(effect, strength, _hidl_cb);
142}
143
144Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
145 perform_cb _hidl_cb) {
146 return performWrapper(effect, strength, _hidl_cb);
147}
148
149template <typename T>
150Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) {
151 ALOGI("CA:: %s, ", __func__);
152 auto validRange = hidl_enum_range<T>();
153 if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
154 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
155 return Void();
156 }
157 return performEffect(static_cast<Effect>(effect), strength, _hidl_cb);
158}
159
160Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength,
161 perform_cb _hidl_cb) {
162 Status status = Status::OK;
163 uint32_t timeMs;
164 int playCount = 1;
165 mEffect.u.rumble.weak_magnitude = mEffect.u.rumble.strong_magnitude = 0;
166
167 ALOGI("CA:: %s() effect = %d", __func__, effect);
168
169 switch (effect) {
170 case Effect::TICK:
171 mEffect.u.rumble.weak_magnitude = 0x5000 + (int)strength * 0x1000;
172 timeMs = 9;
173 break;
174 case Effect::CLICK:
175 mEffect.u.rumble.strong_magnitude = 0x500 + (int)strength * 0x1000;
176 timeMs = 9;
177 break;
178 case Effect::DOUBLE_CLICK:
179 mEffect.u.rumble.strong_magnitude = 0x4000 + (int)strength * 0x1000;
180 playCount = 2;
181 timeMs = 130;
182 break;
183 default:
184 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
185 return Void();
186 }
187
188 timeMs += MAX_TRIGGER_LATENCY_MS; // Add expected cold-start latency
189
190 on(timeMs, playCount);
191 // Android calls off() for us.
192 _hidl_cb(status, timeMs);
193
194 return Void();
195}
196
197Vibrator::~Vibrator() {
198 close(mfd);
199}
200
201
202} // namespace implementation
203} // namespace V1_1
204} // namespace vibrator
205} // namespace hardware
206} // namespace android