1 /*
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 
23 namespace android {
24 namespace hardware {
25 namespace vibrator {
26 namespace V1_3 {
27 namespace implementation {
28 
29 static constexpr uint32_t MS_PER_S = 1000;
30 static constexpr uint32_t NS_PER_MS = 1000000;
31 
Vibrator()32 Vibrator::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 
on(uint32_t timeoutMs)46 Return<Status> Vibrator::on(uint32_t timeoutMs) {
47     return activate(timeoutMs);
48 }
49 
off()50 Return<Status> Vibrator::off() {
51     return activate(0);
52 }
53 
supportsAmplitudeControl()54 Return<bool> Vibrator::supportsAmplitudeControl() {
55     return true;
56 }
57 
setAmplitude(uint8_t amplitude)58 Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
59     if (!amplitude) {
60         return Status::BAD_VALUE;
61     }
62     ALOGI("Amplitude: %u -> %u\n", mAmplitude, amplitude);
63     mAmplitude = amplitude;
64     return Status::OK;
65 }
66 
perform(V1_0::Effect effect,EffectStrength strength,perform_cb _hidl_cb)67 Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
68     return perform<decltype(effect)>(effect, strength, _hidl_cb);
69 }
70 
71 // Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
72 
perform_1_1(V1_1::Effect_1_1 effect,EffectStrength strength,perform_cb _hidl_cb)73 Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
74                                    perform_cb _hidl_cb) {
75     return perform<decltype(effect)>(effect, strength, _hidl_cb);
76 }
77 
78 // Methods from ::android::hardware::vibrator::V1_2::IVibrator follow.
79 
perform_1_2(V1_2::Effect effect,EffectStrength strength,perform_cb _hidl_cb)80 Return<void> Vibrator::perform_1_2(V1_2::Effect effect, EffectStrength strength,
81                                    perform_cb _hidl_cb) {
82     return perform<decltype(effect)>(effect, strength, _hidl_cb);
83 }
84 
85 // Methods from ::android::hardware::vibrator::V1_3::IVibrator follow.
86 
supportsExternalControl()87 Return<bool> Vibrator::supportsExternalControl() {
88     return true;
89 }
90 
setExternalControl(bool enabled)91 Return<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 
perform_1_3(Effect effect,EffectStrength strength,perform_cb _hidl_cb)103 Return<void> Vibrator::perform_1_3(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
104     return perform<decltype(effect)>(effect, strength, _hidl_cb);
105 }
106 
107 // Private methods follow.
108 
perform(Effect effect,EffectStrength strength,perform_cb _hidl_cb)109 Return<void> Vibrator::perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
110     uint8_t amplitude;
111     uint32_t ms;
112     Status status = Status::OK;
113 
114     ALOGI("Perform: Effect %s\n", effectToName(effect).c_str());
115 
116     amplitude = strengthToAmplitude(strength, &status);
117     if (status != Status::OK) {
118         _hidl_cb(status, 0);
119         return Void();
120     }
121     setAmplitude(amplitude);
122 
123     ms = effectToMs(effect, &status);
124     if (status != Status::OK) {
125         _hidl_cb(status, 0);
126         return Void();
127     }
128     status = activate(ms);
129 
130     _hidl_cb(status, ms);
131 
132     return Void();
133 }
134 
135 template <typename T>
perform(T effect,EffectStrength strength,perform_cb _hidl_cb)136 Return<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 }
144 
enable(bool enabled)145 Status 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 
activate(uint32_t ms)156 Status 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 
timeout()189 void 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 
timerCallback(union sigval sigval)202 void Vibrator::timerCallback(union sigval sigval) {
203     static_cast<Vibrator*>(sigval.sival_ptr)->timeout();
204 }
205 
effectToName(Effect effect)206 const std::string Vibrator::effectToName(Effect effect) {
207     return toString(effect);
208 }
209 
effectToMs(Effect effect,Status * status)210 uint32_t Vibrator::effectToMs(Effect effect, Status* status) {
211     switch (effect) {
212         case Effect::CLICK:
213             return 10;
214         case Effect::DOUBLE_CLICK:
215             return 15;
216         case Effect::TICK:
217         case Effect::TEXTURE_TICK:
218             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     }
256     *status = Status::UNSUPPORTED_OPERATION;
257     return 0;
258 }
259 
strengthToAmplitude(EffectStrength strength,Status * status)260 uint8_t Vibrator::strengthToAmplitude(EffectStrength strength, Status* status) {
261     switch (strength) {
262         case EffectStrength::LIGHT:
263             return 128;
264         case EffectStrength::MEDIUM:
265             return 192;
266         case EffectStrength::STRONG:
267             return 255;
268     }
269     *status = Status::UNSUPPORTED_OPERATION;
270     return 0;
271 }
272 
273 }  // namespace implementation
274 }  // namespace V1_3
275 }  // namespace vibrator
276 }  // namespace hardware
277 }  // namespace android
278