-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vibrator.cpp
268 lines (223 loc) · 6.89 KB
/
Vibrator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (C) 2017 The Android Open Source Project
* Copyright (C) 2021 Caleb Connolly <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "VibratorService"
#include <log/log.h>
#include <hardware/hardware.h>
#include <hardware/vibrator.h>
#include <cutils/properties.h>
#include <linux/input.h>
#include "Vibrator.h"
#include <cinttypes>
#include <cmath>
#include <iostream>
#include <fstream>
#include <cstring>
namespace android {
namespace hardware {
namespace vibrator {
namespace V1_1 {
namespace implementation {
using Status = ::android::hardware::vibrator::V1_0::Status;
using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
using Effect = ::android::hardware::vibrator::V1_1::Effect_1_1;
/*
* These have to be kept in line with HAL version
*/
static constexpr Effect MAX_EFFECT = Effect::TICK;
static constexpr EffectStrength MAX_EFFECT_STRENGTH = EffectStrength::STRONG;
/**
* Helper to index effects in the mEffects map.
* from their strength and effect type.
*/
#define EFFECT_INDEX(effect, strength) \
((uint16_t)effect * (uint16_t)MAX_EFFECT_STRENGTH + (uint16_t)strength)
static constexpr int DEFAULT_EFFECT_ID = EFFECT_INDEX(Effect::CLICK, EffectStrength::MEDIUM);
Vibrator::Vibrator(std::string devpath) :
mInputDevPath(devpath)
{
// We just keep the input device open the whole time we're running.
// Closing / reopening it seems to break things.
if (mInputDevPath.length() < 2) {
mIsStub = true;
return;
}
mfd = openInputDev();
if (mfd < 0) {
ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str());
return;
}
// Build a table of effects for each strength of each effect.
int baseStrength = 0;
for (uint16_t i = 0; i <= (uint16_t)MAX_EFFECT; i++)
{
for (uint16_t j = 0; j <= (uint16_t)MAX_EFFECT_STRENGTH; j++)
{
switch((Effect)i) {
default:
case Effect::CLICK:
baseStrength = 0x4000;
break;
case Effect::TICK:
baseStrength = 0x1000;
break;
case Effect::DOUBLE_CLICK:
baseStrength = 0x3000;
break;
}
ALOGV("%s() uploading effect %d, strength %d, magnitude = 0x%x", __func__, i, j, baseStrength + j * 0x1000);
// 0x8000 is about the max for qcom_spmi_haptics.
mEffects[EFFECT_INDEX(i, j)] = FF_EFFECT((uint16_t)(baseStrength + j * 0x800));
uploadEffectToKernel(&mEffects[EFFECT_INDEX(i, j)]);
}
}
}
int Vibrator::openInputDev() {
if (mIsStub)
return 0;
return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC);
}
void Vibrator::uploadEffectToKernel(struct ff_effect* effect) {
int ret;
if (mIsStub)
return;
ret = ioctl(mfd, EVIOCSFF, effect);
if (ret < 0) {
ALOGE("%s() Couldn't upload rumble effect to device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
}
}
void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect
int ret;
if (mIsStub)
return;
ret = ioctl(mfd, EVIOCRMFF, effect);
if (ret < 0) {
ALOGE("%s() Failed to remove rumble effect from device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
return;
}
effect->id = -1;
}
// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
Return<Status> Vibrator::on(uint32_t timeoutMs) {
struct ff_effect* effect;
if (mIsStub)
return Status::OK;
// If the active effect is set, use it instead of the default
if (mActiveEffectId < 0) {
effect = &mEffects[DEFAULT_EFFECT_ID];
} else {
effect = &mEffects[mActiveEffectId];
}
ALOGV("%s() mActiveEffectId = %d, timeoutMs = %d, effect.id = %d, magnitude = %d", __func__, mActiveEffectId, timeoutMs, effect->id, effect->u.rumble.strong_magnitude);
struct input_event play;
int ret;
play.type = EV_FF;
play.code = effect->id;
play.value = 1;
ret = write(mfd, (const void*) &play, sizeof(play));
if (ret != sizeof(play)) {
ALOGE("%s() failed to fully write play event to input device: %d", __func__, errno);
return Status::UNKNOWN_ERROR;
}
usleep(timeoutMs * 1000);
off();
return Status::OK;
}
Return<Status> Vibrator::off() {
ALOGV("%s", __func__);
struct input_event stop;
struct ff_effect* effect;
if (mIsStub)
return Status::OK;
// If the active effect is set, use it instead of the default
if (mActiveEffectId < 0) {
ALOGV("%s() no active effect, stopping default", __func__);
effect = &mEffects[DEFAULT_EFFECT_ID];
} else {
effect = &mEffects[mActiveEffectId];
}
stop.type = EV_FF;
stop.code = effect->id;
stop.value = 0;
if (write(mfd, (const void*) &stop, sizeof(stop)) != sizeof(stop)) {
ALOGE("%s() failed to fully write stop event to input device", __func__);
return Status::UNKNOWN_ERROR;
}
return Status::OK;
}
Return<bool> Vibrator::supportsAmplitudeControl() {
ALOGV("%s", __func__);
return false;
}
Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
ALOGV("%s", __func__);
if (!amplitude) {
return Status::BAD_VALUE;
}
return Status::OK;
}
Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength,
perform_cb _hidl_cb) {
return performWrapper(effect, strength, _hidl_cb);
}
Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
perform_cb _hidl_cb) {
return performWrapper(effect, strength, _hidl_cb);
}
template <typename T>
Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) {
ALOGV("%s, ", __func__);
auto validRange = hidl_enum_range<T>();
if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
_hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
return Void();
}
return performEffect(static_cast<Effect>(effect), strength, _hidl_cb);
}
Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength,
perform_cb _hidl_cb) {
Status status = Status::OK;
uint32_t timeMs = 9;
bool doubleClick = effect == Effect::DOUBLE_CLICK;
ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength);
if (effect > MAX_EFFECT || mIsStub){
_hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
return Void();
}
mActiveEffectId = EFFECT_INDEX(effect, strength);
// Play the effect
on(timeMs);
/*
* Android calls off() for us, so
* only call it if we're doing a double click
*/
if (doubleClick) {
timeMs += 59;
usleep(50 * 1000);
on(timeMs);
}
mActiveEffectId = -1;
_hidl_cb(status, timeMs);
return Void();
}
Vibrator::~Vibrator() {
close(mfd);
}
} // namespace implementation
} // namespace V1_1
} // namespace vibrator
} // namespace hardware
} // namespace android