hardware_xiaomi/aidl/ir/ConsumerIr.cpp
Sebastiano Barezzi 424428e9b5 ir: Stop setting duty cycle
Change-Id: Ibafc8877123abf26d8007ffdb2a7b3c9cfead2c8
2024-08-31 12:12:17 +00:00

81 lines
1.9 KiB
C++

/*
* 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 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);
}
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