blob: 110ca9db9fa951c105da19155806049995e1114a [file] [log] [blame]
Andreas Schneiderccd5c4c2020-05-25 17:10:35 +02001/*
2 * Copyright (C) 2020 The LineageOS 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 "vibrator@1.3-samsung"
18
19#include "Vibrator.h"
20
21#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
23#include <hardware/hardware.h>
24#include <hardware/vibrator.h>
25
26#include <cinttypes>
27#include <cmath>
28#include <fstream>
29#include <iostream>
30
31namespace android {
32namespace hardware {
33namespace vibrator {
34namespace V1_3 {
35namespace implementation {
36
37/*
38 * Write value to path and close file.
39 */
40template <typename T>
41static Return<Status> writeNode(const std::string& path, const T& value) {
42 std::ofstream node(path);
43 if (!node) {
44 LOG(ERROR) << "Failed to open: " << path;
45 return Status::UNKNOWN_ERROR;
46 }
47
48 LOG(DEBUG) << "writeNode node: " << path << " value: " << value;
49
50 node << value << std::endl;
51 if (!node) {
52 LOG(ERROR) << "Failed to write: " << value;
53 return Status::UNKNOWN_ERROR;
54 }
55
56 return Status::OK;
57}
58
59static bool nodeExists(const std::string& path) {
60 std::ofstream f(path.c_str());
61 return f.good();
62}
63
64Vibrator::Vibrator() {
65 bool ok;
66
67 ok = nodeExists(VIBRATOR_TIMEOUT_PATH);
68 if (ok) {
69 mIsTimedOutVibriator = true;
70 }
71
72 ok = nodeExists(VIBRATOR_INTENSITY_PATH);
73 if (ok) {
74 mhasTimedOutIntensity = true;
75 }
76}
77
78// Methods from ::android::hardware::vibrator::V1_0::IVibrator follow.
79
80Return<Status> Vibrator::on(uint32_t timeoutMs) {
81 return activate(timeoutMs);
82}
83
84Return<Status> Vibrator::off() {
85 return activate(0);
86}
87
88Return<bool> Vibrator::supportsAmplitudeControl() {
89 return true;
90}
91
92Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
93 uint32_t intensity;
94
95 if (amplitude == 0) {
96 return Status::BAD_VALUE;
97 }
98
99 LOG(DEBUG) << "setting amplitude: " << (uint32_t)amplitude;
100
101 intensity = std::lround((amplitude - 1) * 10000.0 / 254.0);
102 if (intensity > INTENSITY_MAX) {
103 intensity = INTENSITY_MAX;
104 }
105 LOG(DEBUG) << "setting intensity: " << intensity;
106
107 if (mhasTimedOutIntensity) {
108 return writeNode(VIBRATOR_INTENSITY_PATH, intensity);
109 }
110
111 return Status::OK;
112}
113
114Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
115 return perform<decltype(effect)>(effect, strength, _hidl_cb);
116}
117
118// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
119
120Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
121 perform_cb _hidl_cb) {
122 return perform<decltype(effect)>(effect, strength, _hidl_cb);
123}
124
125// Methods from ::android::hardware::vibrator::V1_2::IVibrator follow.
126
127Return<void> Vibrator::perform_1_2(V1_2::Effect effect, EffectStrength strength,
128 perform_cb _hidl_cb) {
129 return perform<decltype(effect)>(effect, strength, _hidl_cb);
130}
131
132// Methods from ::android::hardware::vibrator::V1_3::IVibrator follow.
133
134Return<bool> Vibrator::supportsExternalControl() {
135 return true;
136}
137
138Return<Status> Vibrator::setExternalControl(bool enabled) {
139 if (mEnabled) {
140 LOG(WARNING) << "Setting external control while the vibrator is enabled is "
141 "unsupported!";
142 return Status::UNSUPPORTED_OPERATION;
143 }
144
145 LOG(INFO) << "ExternalControl: " << mExternalControl << " -> " << enabled;
146 mExternalControl = enabled;
147 return Status::OK;
148}
149
150Return<void> Vibrator::perform_1_3(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
151 return perform<decltype(effect)>(effect, strength, _hidl_cb);
152}
153
154// Private methods follow.
155
156Return<void> Vibrator::perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
157 Status status = Status::OK;
158 uint8_t amplitude;
159 uint32_t ms;
160
161 LOG(DEBUG) << "perform effect: " << toString(effect)
162 << ", strength: " << toString(strength);
163
164 amplitude = strengthToAmplitude(strength, &status);
165 if (status != Status::OK) {
166 _hidl_cb(status, 0);
167 return Void();
168 }
169 setAmplitude(amplitude);
170
171 ms = effectToMs(effect, &status);
172 if (status != Status::OK) {
173 _hidl_cb(status, 0);
174 return Void();
175 }
176 status = activate(ms);
177
178 _hidl_cb(status, ms);
179
180 return Void();
181}
182
183template <typename T>
184Return<void> Vibrator::perform(T effect, EffectStrength strength, perform_cb _hidl_cb) {
185 auto validRange = hidl_enum_range<T>();
186 if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
187 _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
188 return Void();
189 }
190 return perform(static_cast<Effect>(effect), strength, _hidl_cb);
191}
192
193Status Vibrator::activate(uint32_t timeoutMs) {
194 std::lock_guard<std::mutex> lock{mMutex};
195 if (!mIsTimedOutVibriator) {
196 return Status::UNSUPPORTED_OPERATION;
197 }
198
199 return writeNode(VIBRATOR_TIMEOUT_PATH, timeoutMs);
200}
201
202uint8_t Vibrator::strengthToAmplitude(EffectStrength strength, Status* status) {
203 *status = Status::OK;
204
205 switch (strength) {
206 case EffectStrength::LIGHT:
207 return 78;
208 case EffectStrength::MEDIUM:
209 return 128;
210 case EffectStrength::STRONG:
211 return 204;
212 }
213
214 *status = Status::UNSUPPORTED_OPERATION;
215 return 0;
216}
217
218uint32_t Vibrator::effectToMs(Effect effect, Status* status) {
219 *status = Status::OK;
220
221 switch (effect) {
222 case Effect::CLICK:
223 return 20;
224 case Effect::DOUBLE_CLICK:
225 return 25;
226 case Effect::HEAVY_CLICK:
227 return 30;
228 case Effect::TICK:
229 case Effect::TEXTURE_TICK:
230 case Effect::THUD:
231 case Effect::POP:
232 return 15;
233 case Effect::RINGTONE_1:
234 case Effect::RINGTONE_2:
235 case Effect::RINGTONE_3:
236 case Effect::RINGTONE_4:
237 case Effect::RINGTONE_5:
238 case Effect::RINGTONE_6:
239 case Effect::RINGTONE_7:
240 case Effect::RINGTONE_8:
241 case Effect::RINGTONE_9:
242 case Effect::RINGTONE_10:
243 case Effect::RINGTONE_11:
244 case Effect::RINGTONE_12:
245 case Effect::RINGTONE_13:
246 case Effect::RINGTONE_14:
247 case Effect::RINGTONE_15:
248 return 300;
249 }
250
251 *status = Status::UNSUPPORTED_OPERATION;
252 return 0;
253}
254
255} // namespace implementation
256} // namespace V1_3
257} // namespace vibrator
258} // namespace hardware
259} // namespace android