blob: 7b6dda52eeb9eb33e2d2ab81b6f4f64ebb957b78 [file] [log] [blame]
Devin Moore0de7ad62021-11-05 17:30:04 +00001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <aidl/android/hardware/ir/BnConsumerIr.h>
sangweilin94f68782022-10-31 18:21:27 +080018#include <aidl/android/hardware/ir/ConsumerIrFreqRange.h>
Devin Moore0de7ad62021-11-05 17:30:04 +000019#include <android-base/logging.h>
20#include <android/binder_interface_utils.h>
21#include <android/binder_manager.h>
22#include <android/binder_process.h>
sangweilin94f68782022-10-31 18:21:27 +080023#include <hardware/consumerir.h>
Devin Moore0de7ad62021-11-05 17:30:04 +000024#include <numeric>
25
sangweilin94f68782022-10-31 18:21:27 +080026#include <log/log.h>
27
28using ::aidl::android::hardware::ir::ConsumerIrFreqRange;
29
Devin Moore0de7ad62021-11-05 17:30:04 +000030namespace aidl::android::hardware::ir {
31
Devin Moore0de7ad62021-11-05 17:30:04 +000032class ConsumerIr : public BnConsumerIr {
sangweilin94f68782022-10-31 18:21:27 +080033 public:
34 ConsumerIr();
35 private:
Devin Moore0de7ad62021-11-05 17:30:04 +000036 ::ndk::ScopedAStatus getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) override;
Devin Moore48113812022-01-10 17:42:53 +000037 ::ndk::ScopedAStatus transmit(int32_t in_carrierFreqHz,
Devin Moore0de7ad62021-11-05 17:30:04 +000038 const std::vector<int32_t>& in_pattern) override;
sangweilin94f68782022-10-31 18:21:27 +080039 consumerir_device_t *mDevice;
Devin Moore0de7ad62021-11-05 17:30:04 +000040};
41
sangweilin94f68782022-10-31 18:21:27 +080042ConsumerIr::ConsumerIr() {
43 const hw_module_t *hw_module = NULL;
44
45 int ret = hw_get_module(CONSUMERIR_HARDWARE_MODULE_ID, &hw_module);
46 if (ret != 0) {
47 ALOGE("hw_get_module %s failed: %d", CONSUMERIR_HARDWARE_MODULE_ID, ret);
48 return;
49 }
50 ret = hw_module->methods->open(hw_module, CONSUMERIR_TRANSMITTER, (hw_device_t **) &mDevice);
51 if (ret < 0) {
52 ALOGE("Can't open consumer IR transmitter, error: %d", ret);
53 }
Devin Moore0de7ad62021-11-05 17:30:04 +000054}
55
sangweilin94f68782022-10-31 18:21:27 +080056::ndk::ScopedAStatus ConsumerIr::getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) {
57 int32_t len = mDevice->get_num_carrier_freqs(mDevice);
58 if (len < 0) {
59 (*_aidl_return).clear();
60 return ::ndk::ScopedAStatus::ok();
Devin Moore0de7ad62021-11-05 17:30:04 +000061 }
sangweilin94f68782022-10-31 18:21:27 +080062
63 consumerir_freq_range_t *rangeAr = new consumerir_freq_range_t[len];
64 bool success = (mDevice->get_carrier_freqs(mDevice, len, rangeAr) >= 0);
65 if (!success) {
66 (*_aidl_return).clear();
67 return ::ndk::ScopedAStatus::ok();
68 }
69
70 (*_aidl_return).resize(len);
71 for (int32_t i = 0; i < len; i++) {
72 (*_aidl_return)[i].minHz = static_cast<uint32_t>(rangeAr[i].min);
73 (*_aidl_return)[i].maxHz = static_cast<uint32_t>(rangeAr[i].max);
74 }
75 return ::ndk::ScopedAStatus::ok();
Devin Moore0de7ad62021-11-05 17:30:04 +000076}
77
Devin Moore48113812022-01-10 17:42:53 +000078::ndk::ScopedAStatus ConsumerIr::transmit(int32_t in_carrierFreqHz,
Devin Moore0de7ad62021-11-05 17:30:04 +000079 const std::vector<int32_t>& in_pattern) {
sangweilin94f68782022-10-31 18:21:27 +080080 if (in_carrierFreqHz > 0) {
81 mDevice->transmit(mDevice, in_carrierFreqHz, in_pattern.data(), in_pattern.size());
Devin Moore0de7ad62021-11-05 17:30:04 +000082 return ::ndk::ScopedAStatus::ok();
83 } else {
Devin Moore0de7ad62021-11-05 17:30:04 +000084 return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
85 }
Devin Moore0de7ad62021-11-05 17:30:04 +000086}
87
88} // namespace aidl::android::hardware::ir
89
90using aidl::android::hardware::ir::ConsumerIr;
91
92int main() {
93 auto binder = ::ndk::SharedRefBase::make<ConsumerIr>();
94 const std::string name = std::string() + ConsumerIr::descriptor + "/default";
95 CHECK_EQ(STATUS_OK, AServiceManager_addService(binder->asBinder().get(), name.c_str()))
96 << "Failed to register " << name;
97
98 ABinderProcess_setThreadPoolMaxThreadCount(0);
99 ABinderProcess_joinThreadPool();
100
101 return EXIT_FAILURE; // should not reached
102}