blob: b5294371081d61dd37120cd07d3fe55f9d8d96e2 [file] [log] [blame]
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +09001/*
2 * Copyright (C) 2019 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#define LOG_TAG "VibratorService"
18
19#include <log/log.h>
20
21#include "Vibrator.h"
22
23namespace android {
24namespace hardware {
25namespace vibrator {
26namespace V1_3 {
27namespace implementation {
28
29static constexpr uint32_t MS_PER_S = 1000;
30static constexpr uint32_t NS_PER_MS = 1000000;
31
32Vibrator::Vibrator() {
33 sigevent se{};
34 se.sigev_notify = SIGEV_THREAD;
35 se.sigev_value.sival_ptr = this;
36 se.sigev_notify_function = timerCallback;
37 se.sigev_notify_attributes = nullptr;
38
39 if (timer_create(CLOCK_REALTIME, &se, &mTimer) < 0) {
40 ALOGE("Can not create timer!%s", strerror(errno));
41 }
42}
43
44// Methods from ::android::hardware::vibrator::V1_0::IVibrator follow.
45
46Return<Status> Vibrator::on(uint32_t timeoutMs) {
47 return activate(timeoutMs);
48}
49
50Return<Status> Vibrator::off() {
51 return activate(0);
52}
53
54Return<bool> Vibrator::supportsAmplitudeControl() {
55 return true;
56}
57
58Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
Harpreet "Eli" Sangha15d52022019-04-10 16:38:12 +090059 if (!amplitude) {
60 return Status::BAD_VALUE;
61 }
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +090062 ALOGI("Amplitude: %u -> %u\n", mAmplitude, amplitude);
63 mAmplitude = amplitude;
64 return Status::OK;
65}
66
67Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
Harpreet "Eli" Sangha15d52022019-04-10 16:38:12 +090068 return perform<decltype(effect)>(effect, strength, _hidl_cb);
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +090069}
70
71// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
72
73Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
74 perform_cb _hidl_cb) {
Harpreet "Eli" Sangha15d52022019-04-10 16:38:12 +090075 return perform<decltype(effect)>(effect, strength, _hidl_cb);
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +090076}
77
78// Methods from ::android::hardware::vibrator::V1_2::IVibrator follow.
79
Michael Wrighta4c94fd2019-03-21 20:48:34 +000080Return<void> Vibrator::perform_1_2(V1_2::Effect effect, EffectStrength strength,
81 perform_cb _hidl_cb) {
Harpreet "Eli" Sangha15d52022019-04-10 16:38:12 +090082 return perform<decltype(effect)>(effect, strength, _hidl_cb);
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +090083}
84
85// Methods from ::android::hardware::vibrator::V1_3::IVibrator follow.
86
87Return<bool> Vibrator::supportsExternalControl() {
88 return true;
89}
90
91Return<Status> Vibrator::setExternalControl(bool enabled) {
92 if (mEnabled) {
93 ALOGW("Setting external control while the vibrator is enabled is unsupported!");
94 return Status::UNSUPPORTED_OPERATION;
95 } else {
96 ALOGI("ExternalControl: %s -> %s\n", mExternalControl ? "true" : "false",
97 enabled ? "true" : "false");
98 mExternalControl = enabled;
99 return Status::OK;
100 }
101}
102
Michael Wrighta4c94fd2019-03-21 20:48:34 +0000103Return<void> Vibrator::perform_1_3(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
Harpreet "Eli" Sangha15d52022019-04-10 16:38:12 +0900104 return perform<decltype(effect)>(effect, strength, _hidl_cb);
105}
106
107// Private methods follow.
108
109Return<void> Vibrator::perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
Michael Wrighta4c94fd2019-03-21 20:48:34 +0000110 uint8_t amplitude;
111 uint32_t ms;
Harpreet "Eli" Sangha79755bc2019-03-25 16:42:28 +0900112 Status status = Status::OK;
Michael Wrighta4c94fd2019-03-21 20:48:34 +0000113
Harpreet "Eli" Sanghaa2443212019-03-25 14:08:59 +0900114 ALOGI("Perform: Effect %s\n", effectToName(effect).c_str());
Michael Wrighta4c94fd2019-03-21 20:48:34 +0000115
Harpreet "Eli" Sangha79755bc2019-03-25 16:42:28 +0900116 amplitude = strengthToAmplitude(strength, &status);
117 if (status != Status::OK) {
118 _hidl_cb(status, 0);
119 return Void();
120 }
Michael Wrighta4c94fd2019-03-21 20:48:34 +0000121 setAmplitude(amplitude);
122
Harpreet "Eli" Sangha79755bc2019-03-25 16:42:28 +0900123 ms = effectToMs(effect, &status);
124 if (status != Status::OK) {
125 _hidl_cb(status, 0);
126 return Void();
127 }
Michael Wrighta4c94fd2019-03-21 20:48:34 +0000128 status = activate(ms);
129
130 _hidl_cb(status, ms);
131
132 return Void();
133}
134
Harpreet "Eli" Sangha15d52022019-04-10 16:38:12 +0900135template <typename T>
136Return<void> Vibrator::perform(T effect, EffectStrength strength, perform_cb _hidl_cb) {
137 auto validRange = hidl_enum_range<T>();
138 if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
139 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
140 return Void();
141 }
142 return perform(static_cast<Effect>(effect), strength, _hidl_cb);
143}
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +0900144
145Status Vibrator::enable(bool enabled) {
146 if (mExternalControl) {
147 ALOGW("Enabling/disabling while the vibrator is externally controlled is unsupported!");
148 return Status::UNSUPPORTED_OPERATION;
149 } else {
150 ALOGI("Enabled: %s -> %s\n", mEnabled ? "true" : "false", enabled ? "true" : "false");
151 mEnabled = enabled;
152 return Status::OK;
153 }
154}
155
156Status Vibrator::activate(uint32_t ms) {
157 std::lock_guard<std::mutex> lock{mMutex};
158 Status status = Status::OK;
159
160 if (ms > 0) {
161 status = enable(true);
162 if (status != Status::OK) {
163 return status;
164 }
165 }
166
167 itimerspec ts{};
168 ts.it_value.tv_sec = ms / MS_PER_S;
169 ts.it_value.tv_nsec = ms % MS_PER_S * NS_PER_MS;
170
171 if (timer_settime(mTimer, 0, &ts, nullptr) < 0) {
172 ALOGE("Can not set timer!");
173 status = Status::UNKNOWN_ERROR;
174 }
175
176 if ((status != Status::OK) || !ms) {
177 Status _status;
178
179 _status = enable(false);
180
181 if (status == Status::OK) {
182 status = _status;
183 }
184 }
185
186 return status;
187}
188
189void Vibrator::timeout() {
190 std::lock_guard<std::mutex> lock{mMutex};
191 itimerspec ts{};
192
193 if (timer_gettime(mTimer, &ts) < 0) {
194 ALOGE("Can not read timer!");
195 }
196
197 if (ts.it_value.tv_sec == 0 && ts.it_value.tv_nsec == 0) {
198 enable(false);
199 }
200}
201
202void Vibrator::timerCallback(union sigval sigval) {
203 static_cast<Vibrator*>(sigval.sival_ptr)->timeout();
204}
205
Harpreet "Eli" Sanghaa2443212019-03-25 14:08:59 +0900206const std::string Vibrator::effectToName(Effect effect) {
207 return toString(effect);
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +0900208}
209
Harpreet "Eli" Sangha79755bc2019-03-25 16:42:28 +0900210uint32_t Vibrator::effectToMs(Effect effect, Status* status) {
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +0900211 switch (effect) {
212 case Effect::CLICK:
213 return 10;
214 case Effect::DOUBLE_CLICK:
215 return 15;
216 case Effect::TICK:
Michael Wrighta4c94fd2019-03-21 20:48:34 +0000217 case Effect::TEXTURE_TICK:
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +0900218 return 5;
219 case Effect::THUD:
220 return 5;
221 case Effect::POP:
222 return 5;
223 case Effect::HEAVY_CLICK:
224 return 10;
225 case Effect::RINGTONE_1:
226 return 30000;
227 case Effect::RINGTONE_2:
228 return 30000;
229 case Effect::RINGTONE_3:
230 return 30000;
231 case Effect::RINGTONE_4:
232 return 30000;
233 case Effect::RINGTONE_5:
234 return 30000;
235 case Effect::RINGTONE_6:
236 return 30000;
237 case Effect::RINGTONE_7:
238 return 30000;
239 case Effect::RINGTONE_8:
240 return 30000;
241 case Effect::RINGTONE_9:
242 return 30000;
243 case Effect::RINGTONE_10:
244 return 30000;
245 case Effect::RINGTONE_11:
246 return 30000;
247 case Effect::RINGTONE_12:
248 return 30000;
249 case Effect::RINGTONE_13:
250 return 30000;
251 case Effect::RINGTONE_14:
252 return 30000;
253 case Effect::RINGTONE_15:
254 return 30000;
255 }
Harpreet "Eli" Sangha79755bc2019-03-25 16:42:28 +0900256 *status = Status::UNSUPPORTED_OPERATION;
257 return 0;
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +0900258}
259
Harpreet "Eli" Sangha79755bc2019-03-25 16:42:28 +0900260uint8_t Vibrator::strengthToAmplitude(EffectStrength strength, Status* status) {
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +0900261 switch (strength) {
262 case EffectStrength::LIGHT:
263 return 128;
264 case EffectStrength::MEDIUM:
265 return 192;
266 case EffectStrength::STRONG:
267 return 255;
268 }
Harpreet "Eli" Sangha79755bc2019-03-25 16:42:28 +0900269 *status = Status::UNSUPPORTED_OPERATION;
270 return 0;
Harpreet "Eli" Sanghae8631242019-01-31 15:43:36 +0900271}
272
273} // namespace implementation
274} // namespace V1_3
275} // namespace vibrator
276} // namespace hardware
277} // namespace android