1 /*
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 #define LOG_TAG "android.hardware.vibrator@1.2-service.wahoo"
17
18 #include <android/hardware/vibrator/1.2/IVibrator.h>
19 #include <hidl/HidlSupport.h>
20 #include <hidl/HidlTransportSupport.h>
21 #include <utils/Errors.h>
22 #include <utils/StrongPointer.h>
23
24 #include "Vibrator.h"
25
26 using android::hardware::configureRpcThreadpool;
27 using android::hardware::joinRpcThreadpool;
28 using android::hardware::vibrator::V1_2::IVibrator;
29 using android::hardware::vibrator::V1_2::implementation::Vibrator;
30 using namespace android;
31
32 // Refer to Documentation/ABI/testing/sysfs-class-led-driver-drv2624
33 // kernel documentation on the detail usages for ABIs below
34 static constexpr char ACTIVATE_PATH[] = "/sys/class/leds/vibrator/activate";
35 static constexpr char DURATION_PATH[] = "/sys/class/leds/vibrator/duration";
36 static constexpr char STATE_PATH[] = "/sys/class/leds/vibrator/state";
37 static constexpr char RTP_INPUT_PATH[] = "/sys/class/leds/vibrator/device/rtp_input";
38 static constexpr char MODE_PATH[] = "/sys/class/leds/vibrator/device/mode";
39 static constexpr char SEQUENCER_PATH[] = "/sys/class/leds/vibrator/device/set_sequencer";
40 static constexpr char SCALE_PATH[] = "/sys/class/leds/vibrator/device/scale";
41 static constexpr char CTRL_LOOP_PATH[] = "/sys/class/leds/vibrator/device/ctrl_loop";
42 static constexpr char LP_TRIGGER_PATH[] = "/sys/class/leds/vibrator/device/lp_trigger_effect";
43
44 // File path to the calibration file
45 static constexpr char CALIBRATION_FILEPATH[] = "/persist/haptics/drv2624.cal";
46
47 // Kernel ABIs for updating the calibration data
48 static constexpr char AUTOCAL_CONFIG[] = "autocal";
49 static constexpr char LRA_PERIOD_CONFIG[] = "lra_period";
50 static constexpr char AUTOCAL_FILEPATH[] = "/sys/class/leds/vibrator/device/autocal";
51 static constexpr char OL_LRA_PERIOD_FILEPATH[] = "/sys/class/leds/vibrator/device/ol_lra_period";
52
trim(const std::string & str,const std::string & whitespace=" \\t")53 static std::string trim(const std::string& str,
54 const std::string& whitespace = " \t") {
55 const auto str_begin = str.find_first_not_of(whitespace);
56 if (str_begin == std::string::npos) {
57 return "";
58 }
59
60 const auto str_end = str.find_last_not_of(whitespace);
61 const auto str_range = str_end - str_begin + 1;
62
63 return str.substr(str_begin, str_range);
64 }
65
loadCalibrationData()66 static bool loadCalibrationData() {
67 std::map<std::string, std::string> config_data;
68
69 std::ofstream autocal{AUTOCAL_FILEPATH};
70 if (!autocal) {
71 int error = errno;
72 ALOGE("Failed to open %s (%d): %s", AUTOCAL_FILEPATH, error,
73 strerror(error));
74 return false;
75 }
76
77 std::ofstream ol_lra_period{OL_LRA_PERIOD_FILEPATH};
78 if (!ol_lra_period) {
79 int error = errno;
80 ALOGE("Failed to open %s (%d): %s", OL_LRA_PERIOD_FILEPATH, error,
81 strerror(error));
82 return false;
83 }
84
85 std::ifstream cal_data{CALIBRATION_FILEPATH};
86 if (!cal_data) {
87 int error = errno;
88 ALOGE("Failed to open %s (%d): %s", CALIBRATION_FILEPATH, error,
89 strerror(error));
90 return false;
91 }
92
93 std::string line;
94
95 while (std::getline(cal_data, line)) {
96 if (line.empty() || line[0] == '#') {
97 continue;
98 }
99 std::istringstream is_line(line);
100 std::string key;
101 if (std::getline(is_line, key, ':')) {
102 std::string value;
103
104 if (std::getline(is_line, value)) {
105 config_data[trim(key)] = trim(value);
106 }
107 }
108 }
109
110 if(config_data.find(AUTOCAL_CONFIG) != config_data.end()) {
111 autocal << config_data[AUTOCAL_CONFIG] << std::endl;
112 }
113
114 if(config_data.find(LRA_PERIOD_CONFIG) != config_data.end()) {
115 ol_lra_period << config_data[LRA_PERIOD_CONFIG] << std::endl;
116 }
117
118 return true;
119 }
120
registerVibratorService()121 status_t registerVibratorService() {
122 // ostreams below are required
123 std::ofstream activate{ACTIVATE_PATH};
124 if (!activate) {
125 int error = errno;
126 ALOGE("Failed to open %s (%d): %s", ACTIVATE_PATH, error, strerror(error));
127 return -error;
128 }
129
130 std::ofstream duration{DURATION_PATH};
131 if (!duration) {
132 int error = errno;
133 ALOGE("Failed to open %s (%d): %s", DURATION_PATH, error, strerror(error));
134 return -error;
135 }
136
137 std::ofstream state{STATE_PATH};
138 if (!state) {
139 int error = errno;
140 ALOGE("Failed to open %s (%d): %s", STATE_PATH, error, strerror(error));
141 return -error;
142 }
143
144 state << 1 << std::endl;
145 if (!state) {
146 int error = errno;
147 ALOGE("Failed to set state (%d): %s", errno, strerror(errno));
148 return -error;
149 }
150
151 // ostreams below are optional
152 std::ofstream rtpinput{RTP_INPUT_PATH};
153 if (!rtpinput) {
154 int error = errno;
155 ALOGW("Failed to open %s (%d): %s", RTP_INPUT_PATH, error, strerror(error));
156 }
157
158 std::ofstream mode{MODE_PATH};
159 if (!mode) {
160 int error = errno;
161 ALOGW("Failed to open %s (%d): %s", MODE_PATH, error, strerror(error));
162 }
163
164 std::ofstream sequencer{SEQUENCER_PATH};
165 if (!sequencer) {
166 int error = errno;
167 ALOGW("Failed to open %s (%d): %s", SEQUENCER_PATH, error, strerror(error));
168 }
169
170 std::ofstream scale{SCALE_PATH};
171 if (!scale) {
172 int error = errno;
173 ALOGW("Failed to open %s (%d): %s", SCALE_PATH, error, strerror(error));
174 }
175
176 std::ofstream ctrlloop{CTRL_LOOP_PATH};
177 if (!ctrlloop) {
178 int error = errno;
179 ALOGW("Failed to open %s (%d): %s", CTRL_LOOP_PATH, error, strerror(error));
180 }
181
182 std::ofstream lptrigger{LP_TRIGGER_PATH};
183 if (!lptrigger) {
184 int error = errno;
185 ALOGW("Failed to open %s (%d): %s", LP_TRIGGER_PATH, error, strerror(error));
186 }
187
188 if (!loadCalibrationData()) {
189 ALOGW("Failed load calibration data");
190 }
191
192 sp<IVibrator> vibrator = new Vibrator(std::move(activate), std::move(duration),
193 std::move(state), std::move(rtpinput), std::move(mode),
194 std::move(sequencer), std::move(scale), std::move(ctrlloop), std::move(lptrigger));
195
196 return vibrator->registerAsService();
197 }
198
main()199 int main() {
200 configureRpcThreadpool(1, true);
201 status_t status = registerVibratorService();
202
203 if (status != OK) {
204 return status;
205 }
206
207 joinRpcThreadpool();
208 }
209