vibrator: switch to external HAL
The vibrator HAL has been moved to
https://github.com/aospm/external_vibrator-ff to allow other devices to
use it.
diff --git a/device-common.mk b/device-common.mk
index d27500d..473d570 100644
--- a/device-common.mk
+++ b/device-common.mk
@@ -183,7 +183,7 @@
# Haptics
PRODUCT_PACKAGES += \
- android.hardware.vibrator@1.1-service.sdm845
+ android.hardware.vibrator@1.1-service.ff
# Copy standard platform config files
PRODUCT_COPY_FILES += \
diff --git a/vibrator/Android.bp b/vibrator/Android.bp
deleted file mode 100644
index 653457c..0000000
--- a/vibrator/Android.bp
+++ /dev/null
@@ -1,31 +0,0 @@
-//
-// Copyright (C) 2021 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.
-cc_binary {
- name: "android.hardware.vibrator@1.1-service.sdm845",
- vendor: true,
- relative_install_path: "hw",
- init_rc: ["android.hardware.vibrator@1.1-service.sdm845.rc"],
- srcs: ["service.cpp", "Vibrator.cpp"],
- cflags: ["-Wall", "-Werror"],
- shared_libs: [
- "libhidlbase",
- "libcutils",
- "liblog",
- "libutils",
- "libhardware",
- "android.hardware.vibrator@1.0",
- "android.hardware.vibrator@1.1",
- ],
-}
diff --git a/vibrator/Vibrator.cpp b/vibrator/Vibrator.cpp
deleted file mode 100644
index 50aef4f..0000000
--- a/vibrator/Vibrator.cpp
+++ /dev/null
@@ -1,271 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-
-/* Adapted from Crosshatch downstream:
- * https://github.com/GrapheneOS/device_google_crosshatch/blob/c7f62a539b6cb4685348fbd747605cbfabf1b94d/vibrator/Vibrator.cpp
- */
-
-#define LOG_TAG "VibratorService"
-
-#include <log/log.h>
-
-#include <hardware/hardware.h>
-#include <hardware/vibrator.h>
-#include <cutils/properties.h>
-#include <linux/input.h>
-
-#include "Vibrator.h"
-
-#include <cinttypes>
-#include <cmath>
-#include <iostream>
-#include <fstream>
-#include <cstring>
-
-namespace android {
-namespace hardware {
-namespace vibrator {
-namespace V1_1 {
-namespace implementation {
-
-using Status = ::android::hardware::vibrator::V1_0::Status;
-using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
-using Effect = ::android::hardware::vibrator::V1_1::Effect_1_1;
-
-/*
- * These have to be kept in line with HAL version
- */
-static constexpr Effect MAX_EFFECT = Effect::TICK;
-static constexpr EffectStrength MAX_EFFECT_STRENGTH = EffectStrength::STRONG;
-
-/**
- * Helper to index effects in the mEffects map.
- * from their strength and effect type.
- */
-#define EFFECT_INDEX(effect, strength) \
- ((uint16_t)effect * (uint16_t)MAX_EFFECT_STRENGTH + (uint16_t)strength)
-
-static constexpr int DEFAULT_EFFECT_ID = EFFECT_INDEX(Effect::CLICK, EffectStrength::MEDIUM);
-
-Vibrator::Vibrator(std::string devpath) :
- mInputDevPath(devpath)
-{
- // We just keep the input device open the whole time we're running.
- // Closing / reopening it seems to break things.
- if (mInputDevPath.length() < 2) {
- mIsStub = true;
- return;
- }
- mfd = openInputDev();
- if (mfd < 0) {
- ALOGE("%s() can't open FF input device %s", __func__, mInputDevPath.c_str());
- return;
- }
-
- // Build a table of effects for each strength of each effect.
- int baseStrength = 0;
- for (uint16_t i = 0; i <= (uint16_t)MAX_EFFECT; i++)
- {
- for (uint16_t j = 0; j <= (uint16_t)MAX_EFFECT_STRENGTH; j++)
- {
- switch((Effect)i) {
- default:
- case Effect::CLICK:
- baseStrength = 0x4000;
- break;
- case Effect::TICK:
- baseStrength = 0x1000;
- break;
- case Effect::DOUBLE_CLICK:
- baseStrength = 0x3000;
- break;
- }
- ALOGV("%s() uploading effect %d, strength %d, magnitude = 0x%x", __func__, i, j, baseStrength + j * 0x1000);
- // 0x8000 is about the max for qcom_spmi_haptics.
- mEffects[EFFECT_INDEX(i, j)] = FF_EFFECT((uint16_t)(baseStrength + j * 0x800));
-
- uploadEffectToKernel(&mEffects[EFFECT_INDEX(i, j)]);
- }
- }
-}
-
-int Vibrator::openInputDev() {
- if (mIsStub)
- return 0;
- return open(mInputDevPath.c_str(), O_RDWR|O_CLOEXEC);
-}
-
-void Vibrator::uploadEffectToKernel(struct ff_effect* effect) {
- int ret;
- if (mIsStub)
- return;
-
- ret = ioctl(mfd, EVIOCSFF, effect);
- if (ret < 0) {
- ALOGE("%s() Couldn't upload rumble effect to device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
- }
-}
-
-void Vibrator::deleteEffectFromKernel(struct ff_effect* effect) { // Unload rumble effect
- int ret;
- if (mIsStub)
- return;
-
- ret = ioctl(mfd, EVIOCRMFF, effect);
- if (ret < 0) {
- ALOGE("%s() Failed to remove rumble effect from device %s, ret = %d", __func__, mInputDevPath.c_str(), ret);
- return;
- }
- effect->id = -1;
-}
-
-// Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
-Return<Status> Vibrator::on(uint32_t timeoutMs) {
- struct ff_effect* effect;
- if (mIsStub)
- return Status::OK;
- // If the active effect is set, use it instead of the default
- if (mActiveEffectId < 0) {
- effect = &mEffects[DEFAULT_EFFECT_ID];
- } else {
- effect = &mEffects[mActiveEffectId];
- }
-
- ALOGV("%s() mActiveEffectId = %d, timeoutMs = %d, effect.id = %d, magnitude = %d", __func__, mActiveEffectId, timeoutMs, effect->id, effect->u.rumble.strong_magnitude);
- struct input_event play;
- int ret;
-
- play.type = EV_FF;
- play.code = effect->id;
- play.value = 1;
-
- ret = write(mfd, (const void*) &play, sizeof(play));
- if (ret != sizeof(play)) {
- ALOGE("%s() failed to fully write play event to input device: %d", __func__, errno);
- return Status::UNKNOWN_ERROR;
- }
-
- usleep(timeoutMs * 1000);
-
- off();
-
- return Status::OK;
-}
-
-Return<Status> Vibrator::off() {
- ALOGV("%s", __func__);
- struct input_event stop;
- struct ff_effect* effect;
- if (mIsStub)
- return Status::OK;
- // If the active effect is set, use it instead of the default
- if (mActiveEffectId < 0) {
- ALOGV("%s() no active effect, stopping default", __func__);
- effect = &mEffects[DEFAULT_EFFECT_ID];
- } else {
- effect = &mEffects[mActiveEffectId];
- }
-
- stop.type = EV_FF;
- stop.code = effect->id;
- stop.value = 0;
- if (write(mfd, (const void*) &stop, sizeof(stop)) != sizeof(stop)) {
- ALOGE("%s() failed to fully write stop event to input device", __func__);
- return Status::UNKNOWN_ERROR;
- }
-
- return Status::OK;
-}
-
-Return<bool> Vibrator::supportsAmplitudeControl() {
- ALOGV("%s", __func__);
- return false;
-}
-
-Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
- ALOGV("%s", __func__);
-
- if (!amplitude) {
- return Status::BAD_VALUE;
- }
-
- return Status::OK;
-}
-
-Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength,
- perform_cb _hidl_cb) {
- return performWrapper(effect, strength, _hidl_cb);
-}
-
-Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
- perform_cb _hidl_cb) {
- return performWrapper(effect, strength, _hidl_cb);
-}
-
-template <typename T>
-Return<void> Vibrator::performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb) {
- ALOGV("%s, ", __func__);
- auto validRange = hidl_enum_range<T>();
- if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
- _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
- return Void();
- }
- return performEffect(static_cast<Effect>(effect), strength, _hidl_cb);
-}
-
-Return<void> Vibrator::performEffect(Effect effect, EffectStrength strength,
- perform_cb _hidl_cb) {
- Status status = Status::OK;
- uint32_t timeMs = 9;
- bool doubleClick = effect == Effect::DOUBLE_CLICK;
-
- ALOGV("%s() effect = %d, strength = %d", __func__, effect, (int)strength);
-
- if (effect > MAX_EFFECT || mIsStub){
- _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
- return Void();
- }
-
- mActiveEffectId = EFFECT_INDEX(effect, strength);
-
- // Play the effect
- on(timeMs);
- /*
- * Android calls off() for us, so
- * only call it if we're doing a double click
- */
- if (doubleClick) {
- timeMs += 59;
- usleep(50 * 1000);
- on(timeMs);
- }
- mActiveEffectId = -1;
-
- _hidl_cb(status, timeMs);
-
- return Void();
-}
-
-Vibrator::~Vibrator() {
- close(mfd);
-}
-
-
-} // namespace implementation
-} // namespace V1_1
-} // namespace vibrator
-} // namespace hardware
-} // namespace android
\ No newline at end of file
diff --git a/vibrator/Vibrator.h b/vibrator/Vibrator.h
deleted file mode 100644
index 2dc8cb0..0000000
--- a/vibrator/Vibrator.h
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-#ifndef ANDROID_HARDWARE_VIBRATOR_V1_1_VIBRATOR_H
-#define ANDROID_HARDWARE_VIBRATOR_V1_1_VIBRATOR_H
-
-#include <android/hardware/vibrator/1.1/IVibrator.h>
-#include <hidl/Status.h>
-
-#include <linux/input.h>
-
-#include <fstream>
-#include <map>
-
-// Borrowed from linuxconsole/utils/bitmaskros.h
-/* Number of bits for 1 unsigned char */
-#define nBitsPerUchar (sizeof(unsigned char) * 8)
-/* Index=Offset of given bit in 1 unsigned char */
-#define bitOffsetInUchar(bit) ((bit)%nBitsPerUchar)
-/* Index=Offset of the unsigned char associated to the bit
- * at the given index=offset
- */
-#define ucharIndexForBit(bit) ((bit)/nBitsPerUchar)
-/* Test the bit with given index=offset in an unsigned char array */
-#define testBit(bit, array) ((array[ucharIndexForBit(bit)] >> bitOffsetInUchar(bit)) & 1)
-
-/**
- * Builder for ff_effect struct.
- * We don't bother using weak_magnitude here as most hardware
- * doesn't have any way to differentiate strong / weak haptics
- */
-#define FF_EFFECT(rumbleStrenth) \
- { \
- .type = FF_RUMBLE, \
- .id = -1, \
- .direction = 0, \
- .trigger = { \
- .button = 0, \
- .interval = 0, \
- }, \
- .replay = { \
- .length = 0, \
- .delay = 0, \
- }, \
- .u = { \
- .rumble = { \
- .strong_magnitude = rumbleStrenth, \
- .weak_magnitude = 0, \
- }, \
- }, \
- }
-
-namespace android {
-namespace hardware {
-namespace vibrator {
-namespace V1_1 {
-namespace implementation {
-
-class Vibrator : public IVibrator {
-public:
- Vibrator(std::string mInputDevPath);
-
- // Methods from ::android::hardware::vibrator::V1_0::IVibrator follow.
- using Status = ::android::hardware::vibrator::V1_0::Status;
- Return<Status> on(uint32_t timeoutMs) override;
- Return<Status> off() override;
- Return<bool> supportsAmplitudeControl() override;
- Return<Status> setAmplitude(uint8_t amplitude) override;
-
- using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength;
- Return<void> perform(V1_0::Effect effect, EffectStrength strength, perform_cb _hidl_cb)
- override;
- Return<void> perform_1_1(Effect_1_1 effect, EffectStrength strength, perform_cb _hidl_cb)
- override;
-
-private:
- ~Vibrator();
- void uploadEffectToKernel(struct ff_effect*);
- void deleteEffectFromKernel(struct ff_effect*);
- int openInputDev();
- template <typename T>
- Return<void> performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb);
- Return<void> performEffect(Effect_1_1 effect, EffectStrength strength, perform_cb _hidl_cb);
- std::string mInputDevPath;
- int mfd;
- int mActiveEffectId;
- // If the haptics device path is invalid
- // the just stub all behaviour. Otherwise
- // the device will fail to boot because the haptics
- // service keeps crashing.
- bool mIsStub;
- // Look up table of effects by type and strength
- std::map<int, struct ff_effect> mEffects;
-
-};
-
-} // namespace implementation
-} // namespace V1_1
-} // namespace vibrator
-} // namespace hardware
-} // namespace android
-
-class FileDescGuard {
- public:
- FileDescGuard(int fd) : m_fd(fd) {}
-
- ~FileDescGuard() {
- if (close(m_fd) != 0)
- {
- ALOGE("CA:: Failed to close fd %d, errno = %d", m_fd, errno);
- }
- }
- private:
- int m_fd = -1;
-};
-
-#endif // ANDROID_HARDWARE_VIBRATOR_V1_1_VIBRATOR_H
diff --git a/vibrator/android.hardware.vibrator@1.1-service.sdm845.rc b/vibrator/android.hardware.vibrator@1.1-service.sdm845.rc
deleted file mode 100644
index a47743a..0000000
--- a/vibrator/android.hardware.vibrator@1.1-service.sdm845.rc
+++ /dev/null
@@ -1,4 +0,0 @@
-service vendor.vibrator-1-1 /vendor/bin/hw/android.hardware.vibrator@1.1-service.sdm845
- class hal
- user system
- group system
diff --git a/vibrator/service.cpp b/vibrator/service.cpp
deleted file mode 100644
index f81da17..0000000
--- a/vibrator/service.cpp
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * Copyright (C) 2017 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 "android.hardware.vibrator@1.2-service.sdm845"
-
-#include <log/log.h>
-
-#include <android/hardware/vibrator/1.1/IVibrator.h>
-#include <hidl/HidlSupport.h>
-#include <hidl/HidlTransportSupport.h>
-#include <utils/Errors.h>
-#include <utils/StrongPointer.h>
-
-#include <linux/input.h>
-
-#include <iostream>
-#include <stdio.h>
-#include <dirent.h>
-#include <fcntl.h>
-
-#include "Vibrator.h"
-
-#define DEV_INPUT_DIR "/dev/input"
-
-using android::hardware::configureRpcThreadpool;
-using android::hardware::joinRpcThreadpool;
-using android::hardware::vibrator::V1_1::IVibrator;
-using android::hardware::vibrator::V1_1::implementation::Vibrator;
-using namespace android;
-
-// From : https://github.com/ubports/hfd-service/blob/xenial/src/vibrator-ff.cpp#L46
-static bool inputDevSupportsFF(std::string devname) {
- int ret;
- unsigned char features[1 + FF_MAX/8/sizeof(unsigned char)];
- int tempFd = open(devname.c_str(), O_RDWR);
- int request = EVIOCGBIT(EV_FF, sizeof(features)*sizeof(unsigned char));
- bool supported = false;
-
- ALOGV("%s: Testing device: %s", __func__, devname.c_str());
-
- if ((ret = ioctl(tempFd, request, &features)) < 0) {
- ALOGE("%s: ioctl() failed with errno = %d", __func__, ret);
- supported = false;
- }
- if (testBit(FF_RUMBLE, features)) {
- supported = true;
- }
-
- close(tempFd);
- return supported;
-}
-
-// Returns path event path to to the first input device
-// which supports forcefeedback.
-// Adapted from getevent.c (I know this is bad)
-static std::string findFirstFFDevice() {
- char devname[PATH_MAX];
- char *filename;
- struct dirent *de;
- DIR *dir = opendir(DEV_INPUT_DIR);
- if (dir == NULL)
- return NULL;
- strcpy(devname, DEV_INPUT_DIR);
- filename = devname + strlen(devname);
- *filename++ = '/';
- while((de = readdir(dir))) {
- if (std::string(de->d_name).find("event") == std::string::npos)
- continue;
- strcpy(filename, de->d_name);
- // At this point devname is the full path to the dirent
- std::string devpath(devname);
- if (inputDevSupportsFF(devpath)) {
- closedir(dir);
- ALOGV("%s(): Found haptics device: %s", __func__, devpath.c_str());
- return devpath;
- }
- }
- closedir(dir);
- return "";
-}
-
-
-status_t registerVibratorService() {
- std::string hapticsDev = findFirstFFDevice();
- if (hapticsDev.length() < 2)
- ALOGE("Couldn't find a haptics device, stubbing HAL to prevent"
- " boot failing");
- sp<IVibrator> vibrator = new Vibrator(hapticsDev);
-
- return vibrator->registerAsService();
-}
-
-int main() {
- configureRpcThreadpool(1, true);
- status_t status = registerVibratorService();
-
- if (status != OK) {
- return status;
- }
-
- joinRpcThreadpool();
-}