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.crosshatch"
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
31 static constexpr char ACTIVATE_PATH[] = "/sys/class/leds/vibrator/activate";
32 static constexpr char DURATION_PATH[] = "/sys/class/leds/vibrator/duration";
33 static constexpr char STATE_PATH[] = "/sys/class/leds/vibrator/state";
34 static constexpr char EFFECT_INDEX_PATH[] = "/sys/class/leds/vibrator/device/cp_trigger_index";
35 static constexpr char EFFECT_QUEUE_PATH[] = "/sys/class/leds/vibrator/device/cp_trigger_queue";
36 static constexpr char DIGI_SCALE_PATH[] = "/sys/class/leds/vibrator/device/dig_scale";
37
38 // File path to the calibration file
39 static constexpr char CALIBRATION_FILEPATH[] = "/persist/haptics/cs40l20.cal";
40
41 // Kernel ABIs for updating the calibration data
42 static constexpr char F0_CONFIG[] = "f0_measured";
43 static constexpr char REDC_CONFIG[] = "redc_measured";
44 static constexpr char F0_FILEPATH[] = "/sys/class/leds/vibrator/device/f0_stored";
45 static constexpr char REDC_FILEPATH[] = "/sys/class/leds/vibrator/device/redc_stored";
46
trim(const std::string & str,const std::string & whitespace=" \\t")47 static std::string trim(const std::string &str, const std::string &whitespace = " \t") {
48 const auto str_begin = str.find_first_not_of(whitespace);
49 if (str_begin == std::string::npos) {
50 return "";
51 }
52
53 const auto str_end = str.find_last_not_of(whitespace);
54 const auto str_range = str_end - str_begin + 1;
55
56 return str.substr(str_begin, str_range);
57 }
58
loadCalibrationData()59 static bool loadCalibrationData() {
60 std::map<std::string, std::string> config_data;
61
62 std::ofstream f0{F0_FILEPATH};
63 if (!f0) {
64 ALOGE("Failed to open %s (%d): %s", F0_FILEPATH, errno, strerror(errno));
65 }
66
67 std::ofstream redc{REDC_FILEPATH};
68 if (!redc) {
69 ALOGE("Failed to open %s (%d): %s", REDC_FILEPATH, errno, strerror(errno));
70 }
71
72 std::ifstream cal_data{CALIBRATION_FILEPATH};
73 if (!cal_data) {
74 ALOGE("Failed to open %s (%d): %s", CALIBRATION_FILEPATH, errno, strerror(errno));
75 return false;
76 }
77
78 for (std::string line; std::getline(cal_data, line);) {
79 if (line.empty() || line[0] == '#') {
80 continue;
81 }
82 std::istringstream is_line(line);
83 std::string key;
84 if (std::getline(is_line, key, ':')) {
85 std::string value;
86
87 if (std::getline(is_line, value)) {
88 config_data[trim(key)] = trim(value);
89 }
90 }
91 }
92
93 if (config_data.find(F0_CONFIG) != config_data.end()) {
94 f0 << config_data[F0_CONFIG] << std::endl;
95 }
96
97 if (config_data.find(REDC_CONFIG) != config_data.end()) {
98 redc << config_data[REDC_CONFIG] << std::endl;
99 }
100
101 return true;
102 }
103
104 // passing out ownership, can't make sp yet
makeVibratorService()105 IVibrator *makeVibratorService() {
106 // ostreams below are required
107 std::ofstream activate{ACTIVATE_PATH};
108 if (!activate) {
109 ALOGE("Failed to open %s (%d): %s", ACTIVATE_PATH, errno, strerror(errno));
110 }
111
112 std::ofstream duration{DURATION_PATH};
113 if (!duration) {
114 ALOGE("Failed to open %s (%d): %s", DURATION_PATH, errno, strerror(errno));
115 }
116
117 std::ofstream state{STATE_PATH};
118 if (!state) {
119 ALOGE("Failed to open %s (%d): %s", STATE_PATH, errno, strerror(errno));
120 }
121
122 std::ofstream effect{EFFECT_INDEX_PATH};
123 if (!state) {
124 ALOGE("Failed to open %s (%d): %s", EFFECT_INDEX_PATH, errno, strerror(errno));
125 }
126
127 std::ofstream queue{EFFECT_QUEUE_PATH};
128 if (!state) {
129 ALOGE("Failed to open %s (%d): %s", EFFECT_QUEUE_PATH, errno, strerror(errno));
130 }
131
132 std::ofstream scale{DIGI_SCALE_PATH};
133 if (!scale) {
134 ALOGE("Failed to open %s (%d): %s", DIGI_SCALE_PATH, errno, strerror(errno));
135 }
136
137 state << 1 << std::endl;
138 if (!state) {
139 ALOGE("Failed to set state (%d): %s", errno, strerror(errno));
140 }
141
142 if (!loadCalibrationData()) {
143 ALOGW("Failed to load calibration data");
144 }
145
146 return new Vibrator(std::move(activate), std::move(duration), std::move(effect),
147 std::move(queue), std::move(scale));
148 }
149
HIDL_FETCH_IVibrator(const char *)150 extern "C" IVibrator *HIDL_FETCH_IVibrator(const char * /*instnace*/) {
151 return makeVibratorService();
152 }
153