Rewrite IR HAL in AIDL

Change-Id: Ie27056c065766ab802ee337ced921876726de5cb
This commit is contained in:
Sebastiano Barezzi 2024-08-18 22:48:40 +02:00 committed by Bruno Martins
parent 1f819b04da
commit a271b57c99
6 changed files with 188 additions and 0 deletions

21
aidl/ir/Android.bp Normal file
View file

@ -0,0 +1,21 @@
//
// SPDX-FileCopyrightText: 2024 The LineageOS Project
// SPDX-License-Identifier: Apache-2.0
//
cc_binary {
name: "android.hardware.ir-service.xiaomi",
relative_install_path: "hw",
vendor: true,
init_rc: ["android.hardware.ir-service.xiaomi.rc"],
vintf_fragments: ["android.hardware.ir-service.xiaomi.xml"],
srcs: [
"ConsumerIr.cpp",
"service.cpp",
],
shared_libs: [
"libbase",
"libbinder_ndk",
"android.hardware.ir-V1-ndk",
],
}

91
aidl/ir/ConsumerIr.cpp Normal file
View file

@ -0,0 +1,91 @@
/*
* SPDX-FileCopyrightText: 2017-2024 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_TAG "ConsumerIr"
#include "ConsumerIr.h"
#include <android-base/logging.h>
#include <fcntl.h>
#include <linux/lirc.h>
#include <string>
using std::vector;
namespace aidl {
namespace android {
namespace hardware {
namespace ir {
static const std::string kIrDevice = "/dev/lirc0";
static const int kDutyCycle = 33;
static vector<ConsumerIrFreqRange> kRangeVec{
{.minHz = 30000, .maxHz = 60000},
};
::ndk::ScopedAStatus ConsumerIr::getCarrierFreqs(vector<ConsumerIrFreqRange>* _aidl_return) {
*_aidl_return = kRangeVec;
return ::ndk::ScopedAStatus::ok();
}
::ndk::ScopedAStatus ConsumerIr::transmit(int32_t carrierFreqHz, const vector<int32_t>& pattern) {
size_t entries = pattern.size();
if (entries == 0) {
return ::ndk::ScopedAStatus::ok();
}
int fd = open(kIrDevice.c_str(), O_RDWR);
if (fd < 0) {
LOG(ERROR) << "Failed to open " << kIrDevice << ", error " << fd;
return ::ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
}
int rc = ioctl(fd, LIRC_SET_SEND_CARRIER, &carrierFreqHz);
if (rc < 0) {
LOG(ERROR) << "Failed to set carrier " << carrierFreqHz << ", error: " << errno;
close(fd);
return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
}
rc = ioctl(fd, LIRC_SET_SEND_DUTY_CYCLE, &kDutyCycle);
if (rc < 0) {
LOG(ERROR) << "Failed to set duty cycle " << kDutyCycle << ", error: " << errno;
close(fd);
return ::ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
}
if ((entries & 1) != 0) {
rc = write(fd, pattern.data(), entries * sizeof(int32_t));
} else {
rc = write(fd, pattern.data(), (entries - 1) * sizeof(int32_t));
usleep(pattern[entries - 1]);
}
if (rc < 0) {
LOG(ERROR) << "Failed to write pattern, " << entries << " entries, error: " << errno;
close(fd);
return ::ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
}
close(fd);
return ::ndk::ScopedAStatus::ok();
}
} // namespace ir
} // namespace hardware
} // namespace android
} // namespace aidl

27
aidl/ir/ConsumerIr.h Normal file
View file

@ -0,0 +1,27 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <aidl/android/hardware/ir/BnConsumerIr.h>
namespace aidl {
namespace android {
namespace hardware {
namespace ir {
class ConsumerIr : public BnConsumerIr {
public:
::ndk::ScopedAStatus getCarrierFreqs(
::std::vector<::aidl::android::hardware::ir::ConsumerIrFreqRange>* _aidl_return)
override;
::ndk::ScopedAStatus transmit(int32_t carrierFreqHz,
const ::std::vector<int32_t>& pattern) override;
};
} // namespace ir
} // namespace hardware
} // namespace android
} // namespace aidl

View file

@ -0,0 +1,14 @@
#
# SPDX-FileCopyrightText: 2024 The LineageOS Project
# SPDX-License-Identifier: Apache-2.0
#
on early-boot
# IR device
chown system system /dev/lirc0
service vendor.ir-default /vendor/bin/hw/android.hardware.ir-service.xiaomi
class hal
user system
group system
shutdown critical

View file

@ -0,0 +1,11 @@
<!--
SPDX-FileCopyrightText: 2024 The LineageOS Project
SPDX-License-Identifier: Apache-2.0
-->
<manifest version="1.0" type="device">
<hal format="aidl">
<name>android.hardware.ir</name>
<version>1</version>
<fqname>IConsumerIr/default</fqname>
</hal>
</manifest>

24
aidl/ir/service.cpp Normal file
View file

@ -0,0 +1,24 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
#include "ConsumerIr.h"
#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
using aidl::android::hardware::ir::ConsumerIr;
int main() {
ABinderProcess_setThreadPoolMaxThreadCount(0);
std::shared_ptr<ConsumerIr> hal = ::ndk::SharedRefBase::make<ConsumerIr>();
const std::string instance = std::string(ConsumerIr::descriptor) + "/default";
binder_status_t status = AServiceManager_addService(hal->asBinder().get(), instance.c_str());
CHECK_EQ(status, STATUS_OK);
ABinderProcess_joinThreadPool();
return EXIT_FAILURE; // should not reach
}