Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 94f68788 authored by sangweilin's avatar sangweilin Committed by Steven Moreland
Browse files

[BugFix] open ir hal lib to support aidl ir service




Android T change hidl to aidl for many services, also the ir hal service. We make ir aidl service running with real ir hardware through this patch.

Change-Id: I5bd2fa28b34bec8183ad110639d85e4fc59d8f3f
Signed-off-by: default avatarsangweilin <sangweilin@xiaomi.com>
parent 73cb8af6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ cc_binary {
        "liblog",
        "libutils",
        "android.hardware.ir-V1-ndk",
        "libhardware"
    ],

    srcs: ["main.cpp"],
+2 −2
Original line number Diff line number Diff line
service vendor.ir-default /vendor/bin/hw/android.hardware.ir-service.example
    class hal
    user nobody
    group nobody
    user system
    group system
+43 −18
Original line number Diff line number Diff line
@@ -15,49 +15,74 @@
 */

#include <aidl/android/hardware/ir/BnConsumerIr.h>
#include <aidl/android/hardware/ir/ConsumerIrFreqRange.h>
#include <android-base/logging.h>
#include <android/binder_interface_utils.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include <hardware/consumerir.h>
#include <numeric>

namespace aidl::android::hardware::ir {
#include <log/log.h>

const std::vector<ConsumerIrFreqRange> kSupportedFreqs = {
        {2000, 4000},
        {10000, 30000},
};
using ::aidl::android::hardware::ir::ConsumerIrFreqRange;

namespace aidl::android::hardware::ir {

class ConsumerIr : public BnConsumerIr {
  public:
    ConsumerIr();
  private:
    ::ndk::ScopedAStatus getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) override;
    ::ndk::ScopedAStatus transmit(int32_t in_carrierFreqHz,
                                  const std::vector<int32_t>& in_pattern) override;
    consumerir_device_t *mDevice;
};

ConsumerIr::ConsumerIr() {
    const hw_module_t *hw_module = NULL;

    int ret = hw_get_module(CONSUMERIR_HARDWARE_MODULE_ID, &hw_module);
    if (ret != 0) {
        ALOGE("hw_get_module %s failed: %d", CONSUMERIR_HARDWARE_MODULE_ID, ret);
        return;
    }
    ret = hw_module->methods->open(hw_module, CONSUMERIR_TRANSMITTER, (hw_device_t **) &mDevice);
    if (ret < 0) {
        ALOGE("Can't open consumer IR transmitter, error: %d", ret);
    }
}

::ndk::ScopedAStatus ConsumerIr::getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) {
    *_aidl_return = kSupportedFreqs;
    int32_t len = mDevice->get_num_carrier_freqs(mDevice);
    if (len < 0) {
        (*_aidl_return).clear();
        return ::ndk::ScopedAStatus::ok();
    }

bool isSupportedFreq(int32_t freq) {
    for (const auto& range : kSupportedFreqs) {
        if (freq >= range.minHz && freq <= range.maxHz) return true;
    consumerir_freq_range_t *rangeAr = new consumerir_freq_range_t[len];
    bool success = (mDevice->get_carrier_freqs(mDevice, len, rangeAr) >= 0);
    if (!success) {
        (*_aidl_return).clear();
        return ::ndk::ScopedAStatus::ok();
    }
    return false;

    (*_aidl_return).resize(len);
    for (int32_t i = 0; i < len; i++) {
        (*_aidl_return)[i].minHz = static_cast<uint32_t>(rangeAr[i].min);
        (*_aidl_return)[i].maxHz = static_cast<uint32_t>(rangeAr[i].max);
    }
    return ::ndk::ScopedAStatus::ok();
}

::ndk::ScopedAStatus ConsumerIr::transmit(int32_t in_carrierFreqHz,
                                          const std::vector<int32_t>& in_pattern) {
    if (isSupportedFreq(in_carrierFreqHz)) {
        // trasmit the pattern, each integer is number of microseconds in an
        // alternating on/off state.
        usleep(std::accumulate(in_pattern.begin(), in_pattern.end(), 0));
    if (in_carrierFreqHz > 0) {
        mDevice->transmit(mDevice, in_carrierFreqHz, in_pattern.data(), in_pattern.size());
        return ::ndk::ScopedAStatus::ok();
    } else {
        // unsupported operation
        return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
    }
    return ::ndk::ScopedAStatus::ok();
}

}  // namespace aidl::android::hardware::ir