mirror of
https://github.com/team-infusion-developers/android_hardware_samsung.git
synced 2024-11-01 07:47:55 +00:00
samsung: hidl: add vibrator HAL for SEC Haptic Engine
Change-Id: I758b40deb4e0b8bfda1aa9a24c27207d3f3d7dbe
This commit is contained in:
parent
83cc460bb7
commit
a4922f4f1c
5 changed files with 272 additions and 0 deletions
30
hidl/vibrator/haptic/Android.bp
Normal file
30
hidl/vibrator/haptic/Android.bp
Normal file
|
@ -0,0 +1,30 @@
|
|||
// Copyright (C) 2019 The LineageOS 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.0-service.samsung-haptic",
|
||||
defaults: ["hidl_defaults"],
|
||||
vendor: true,
|
||||
relative_install_path: "hw",
|
||||
init_rc: ["android.hardware.vibrator@1.0-service.samsung-haptic.rc"],
|
||||
srcs: ["service.cpp", "Vibrator.cpp"],
|
||||
shared_libs: [
|
||||
"libbase",
|
||||
"libhidlbase",
|
||||
"liblog",
|
||||
"libutils",
|
||||
"libhardware",
|
||||
"android.hardware.vibrator@1.0",
|
||||
],
|
||||
}
|
121
hidl/vibrator/haptic/Vibrator.cpp
Normal file
121
hidl/vibrator/haptic/Vibrator.cpp
Normal file
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* Copyright (C) 2019 The LineageOS 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.0-service.samsung-haptic"
|
||||
|
||||
#include <log/log.h>
|
||||
|
||||
#include <android-base/stringprintf.h>
|
||||
|
||||
#include <hardware/hardware.h>
|
||||
#include <hardware/vibrator.h>
|
||||
|
||||
#include "Vibrator.h"
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace android {
|
||||
namespace hardware {
|
||||
namespace vibrator {
|
||||
namespace V1_0 {
|
||||
namespace implementation {
|
||||
|
||||
/*
|
||||
* Write value to path and close file.
|
||||
*/
|
||||
template <typename T>
|
||||
static void set(const std::string& path, const T& value) {
|
||||
std::ofstream file(path);
|
||||
file << value << std::endl;
|
||||
}
|
||||
|
||||
Vibrator::Vibrator() {
|
||||
mIntensity = 10000;
|
||||
}
|
||||
|
||||
// SEC Haptic Engine
|
||||
Return<Status> Vibrator::doHaptic(int timeout, int intensity, int freq, int overdrive) {
|
||||
std::string haptic =
|
||||
android::base::StringPrintf("4 %d %d %d %d", timeout, intensity, freq, overdrive);
|
||||
set("/sys/class/timed_output/vibrator/haptic_engine", haptic);
|
||||
set("/sys/class/timed_output/vibrator/enable", 1);
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
// Methods from ::android::hardware::vibrator::V1_0::IVibrator follow.
|
||||
Return<Status> Vibrator::on(uint32_t timeout_ms) {
|
||||
return doHaptic(timeout_ms, mIntensity, 0, 0);
|
||||
}
|
||||
|
||||
Return<Status> Vibrator::off() {
|
||||
set("/sys/class/timed_output/vibrator/enable", 0);
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Return<bool> Vibrator::supportsAmplitudeControl() {
|
||||
return true;
|
||||
}
|
||||
|
||||
Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
|
||||
if (amplitude == 0) {
|
||||
return Status::BAD_VALUE;
|
||||
}
|
||||
mIntensity = amplitude * 10000 / 255;
|
||||
return Status::OK;
|
||||
}
|
||||
|
||||
Return<void> Vibrator::perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
|
||||
Status status = Status::OK;
|
||||
uint32_t timeMS;
|
||||
uint32_t intensity;
|
||||
|
||||
switch (strength) {
|
||||
case EffectStrength::LIGHT:
|
||||
intensity = 1000;
|
||||
break;
|
||||
case EffectStrength::STRONG:
|
||||
intensity = 5000;
|
||||
break;
|
||||
default:
|
||||
intensity = 3000;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (effect) {
|
||||
case Effect::CLICK:
|
||||
case Effect::DOUBLE_CLICK:
|
||||
status = doHaptic(7, intensity, 2000, 1);
|
||||
timeMS = 7;
|
||||
break;
|
||||
default:
|
||||
status = Status::UNSUPPORTED_OPERATION;
|
||||
timeMS = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
_hidl_cb(status, timeMS);
|
||||
return Void();
|
||||
}
|
||||
|
||||
} // namespace implementation
|
||||
} // namespace V1_0
|
||||
} // namespace vibrator
|
||||
} // namespace hardware
|
||||
} // namespace android
|
53
hidl/vibrator/haptic/Vibrator.h
Normal file
53
hidl/vibrator/haptic/Vibrator.h
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (C) 2019 The LineageOS 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_0_VIBRATOR_H
|
||||
#define ANDROID_HARDWARE_VIBRATOR_V1_0_VIBRATOR_H
|
||||
|
||||
#include <android/hardware/vibrator/1.0/IVibrator.h>
|
||||
#include <hidl/Status.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace android {
|
||||
namespace hardware {
|
||||
namespace vibrator {
|
||||
namespace V1_0 {
|
||||
namespace implementation {
|
||||
|
||||
class Vibrator : public IVibrator {
|
||||
public:
|
||||
Vibrator();
|
||||
|
||||
// Methods from ::android::hardware::vibrator::V1_0::IVibrator follow.
|
||||
Return<Status> on(uint32_t timeoutMs) override;
|
||||
Return<Status> off() override;
|
||||
Return<bool> supportsAmplitudeControl() override;
|
||||
Return<Status> setAmplitude(uint8_t) override;
|
||||
Return<void> perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) override;
|
||||
|
||||
private:
|
||||
Return<Status> doHaptic(int timeout, int intensity, int freq, int overdrive);
|
||||
uint32_t mIntensity;
|
||||
};
|
||||
|
||||
} // namespace implementation
|
||||
} // namespace V1_0
|
||||
} // namespace vibrator
|
||||
} // namespace hardware
|
||||
} // namespace android
|
||||
|
||||
#endif // ANDROID_HARDWARE_VIBRATOR_V1_0_VIBRATOR_H
|
|
@ -0,0 +1,4 @@
|
|||
service vendor.vibrator-1-0 /vendor/bin/hw/android.hardware.vibrator@1.0-service.samsung-haptic
|
||||
class hal
|
||||
user system
|
||||
group system
|
64
hidl/vibrator/haptic/service.cpp
Normal file
64
hidl/vibrator/haptic/service.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (C) 2019 The LineageOS 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.0-service.samsung-haptic"
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android/hardware/vibrator/1.0/IVibrator.h>
|
||||
#include <hidl/HidlSupport.h>
|
||||
#include <hidl/HidlTransportSupport.h>
|
||||
#include <utils/Errors.h>
|
||||
#include <utils/StrongPointer.h>
|
||||
|
||||
#include "Vibrator.h"
|
||||
|
||||
using android::hardware::configureRpcThreadpool;
|
||||
using android::hardware::joinRpcThreadpool;
|
||||
using android::hardware::vibrator::V1_0::IVibrator;
|
||||
using android::hardware::vibrator::V1_0::implementation::Vibrator;
|
||||
|
||||
using android::OK;
|
||||
using android::sp;
|
||||
using android::status_t;
|
||||
|
||||
int main() {
|
||||
status_t status;
|
||||
sp<IVibrator> vibrator;
|
||||
|
||||
LOG(INFO) << "Vibrator HAL service is starting.";
|
||||
|
||||
vibrator = new Vibrator();
|
||||
if (vibrator == nullptr) {
|
||||
LOG(ERROR) << "Can not create an instance of Vibrator HAL IVibrator, exiting.";
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
configureRpcThreadpool(1, true);
|
||||
|
||||
status = vibrator->registerAsService();
|
||||
if (status != OK) {
|
||||
LOG(ERROR) << "Could not register service for Vibrator HAL";
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
LOG(INFO) << "Vibrator HAL service is Ready.";
|
||||
joinRpcThreadpool();
|
||||
|
||||
shutdown:
|
||||
// In normal operation, we don't expect the thread pool to shutdown
|
||||
LOG(ERROR) << "Vibrator HAL failed to join thread pool.";
|
||||
return 1;
|
||||
}
|
Loading…
Reference in a new issue