summaryrefslogtreecommitdiff
path: root/services/vibratorservice/VibratorController.cpp
blob: 21924e9f1394aa2b8fe186da432e86fcb21e667e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "VibratorController"

#ifndef qDoWithRetries
#define qDoWithRetries(op) doWithRetries(op, __FUNCTION__)
#endif

#include <aidl/android/hardware/vibrator/IVibrator.h>
#include <android/binder_manager.h>
#include <binder/IServiceManager.h>

#include <utils/Log.h>

#include <vibratorservice/VibratorController.h>

using ::aidl::android::hardware::vibrator::Effect;
using ::aidl::android::hardware::vibrator::EffectStrength;
using ::aidl::android::hardware::vibrator::IVibrator;

using Status = ::ndk::ScopedAStatus;

using namespace std::placeholders;

namespace android {

namespace vibrator {

// -------------------------------------------------------------------------------------------------

inline bool isStatusUnsupported(const Status& status) {
    // STATUS_UNKNOWN_TRANSACTION means the HAL is an older version, so operation is unsupported.
    return status.getStatus() == STATUS_UNKNOWN_TRANSACTION ||
            status.getExceptionCode() == EX_UNSUPPORTED_OPERATION;
}

inline bool isStatusTransactionFailed(const Status& status) {
    // STATUS_UNKNOWN_TRANSACTION means the HAL is an older version, so operation is unsupported.
    return status.getStatus() != STATUS_UNKNOWN_TRANSACTION &&
            status.getExceptionCode() == EX_TRANSACTION_FAILED;
}

// -------------------------------------------------------------------------------------------------

bool VibratorProvider::isDeclared() {
    std::lock_guard<std::mutex> lock(mMutex);
    if (mIsDeclared.has_value()) {
        return *mIsDeclared;
    }

    bool isDeclared = AServiceManager_isDeclared(mServiceName.c_str());
    if (!isDeclared) {
        ALOGV("Vibrator HAL service not declared.");
    }

    mIsDeclared.emplace(isDeclared);
    return isDeclared;
}

std::shared_ptr<IVibrator> VibratorProvider::waitForVibrator() {
    if (!isDeclared()) {
        return nullptr;
    }

    auto vibrator = IVibrator::fromBinder(
            ndk::SpAIBinder(AServiceManager_waitForService(mServiceName.c_str())));
    if (vibrator) {
        ALOGV("Successfully connected to Vibrator HAL service.");
    } else {
        ALOGE("Error connecting to declared Vibrator HAL service.");
    }

    return vibrator;
}

std::shared_ptr<IVibrator> VibratorProvider::checkForVibrator() {
    if (!isDeclared()) {
        return nullptr;
    }

    auto vibrator = IVibrator::fromBinder(
            ndk::SpAIBinder(AServiceManager_checkService(mServiceName.c_str())));
    if (vibrator) {
        ALOGV("Successfully reconnected to Vibrator HAL service.");
    } else {
        ALOGE("Error reconnecting to declared Vibrator HAL service.");
    }

    return vibrator;
}

// -------------------------------------------------------------------------------------------------

bool VibratorController::init() {
    if (!mVibratorProvider->isDeclared()) {
        return false;
    }
    std::lock_guard<std::mutex> lock(mMutex);
    if (mVibrator == nullptr) {
        mVibrator = mVibratorProvider->waitForVibrator();
    }
    return mVibratorProvider->isDeclared();
}

Status VibratorController::off() {
    return qDoWithRetries(std::bind(&IVibrator::off, _1));
}

Status VibratorController::setAmplitude(float amplitude) {
    return qDoWithRetries(std::bind(&IVibrator::setAmplitude, _1, amplitude));
}

Status VibratorController::setExternalControl(bool enabled) {
    return qDoWithRetries(std::bind(&IVibrator::setExternalControl, _1, enabled));
}

Status VibratorController::alwaysOnEnable(int32_t id, const Effect& effect,
                                          const EffectStrength& strength) {
    return qDoWithRetries(std::bind(&IVibrator::alwaysOnEnable, _1, id, effect, strength));
}

Status VibratorController::alwaysOnDisable(int32_t id) {
    return qDoWithRetries(std::bind(&IVibrator::alwaysOnDisable, _1, id));
}

// -------------------------------------------------------------------------------------------------

std::shared_ptr<IVibrator> VibratorController::reconnectToVibrator() {
    std::lock_guard<std::mutex> lock(mMutex);
    mVibrator = mVibratorProvider->checkForVibrator();
    return mVibrator;
}

Status VibratorController::doWithRetries(const VibratorController::VibratorOp& op,
                                         const char* logLabel) {
    if (!init()) {
        ALOGV("Skipped %s because Vibrator HAL is not declared", logLabel);
        return Status::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE, "IVibrator not declared");
    }
    std::shared_ptr<IVibrator> vibrator;
    {
        std::lock_guard<std::mutex> lock(mMutex);
        vibrator = mVibrator;
    }

    if (!vibrator) {
        ALOGE("Skipped %s because Vibrator HAL is declared but failed to load", logLabel);
        return Status::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
                                                    "IVibrator declared but failed to load");
    }

    auto status = doOnce(vibrator.get(), op, logLabel);
    for (int i = 1; i < MAX_ATTEMPTS && isStatusTransactionFailed(status); i++) {
        vibrator = reconnectToVibrator();
        if (!vibrator) {
            // Failed to reconnect to vibrator HAL after a transaction failed, skip retries.
            break;
        }
        status = doOnce(vibrator.get(), op, logLabel);
    }

    return status;
}

Status VibratorController::doOnce(IVibrator* vibrator, const VibratorController::VibratorOp& op,
                                  const char* logLabel) {
    auto status = op(vibrator);
    if (!status.isOk()) {
        if (isStatusUnsupported(status)) {
            ALOGV("Vibrator HAL %s is unsupported: %s", logLabel, status.getMessage());
        } else {
            ALOGE("Vibrator HAL %s failed: %s", logLabel, status.getMessage());
        }
    }
    return status;
}

// -------------------------------------------------------------------------------------------------

}; // namespace vibrator

}; // namespace android