1 /*
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>
18 #include <aidl/android/hardware/ir/ConsumerIrFreqRange.h>
19 #include <android-base/logging.h>
20 #include <android/binder_interface_utils.h>
21 #include <android/binder_manager.h>
22 #include <android/binder_process.h>
23 #include <hardware/consumerir.h>
24 #include <numeric>
25 
26 #include <log/log.h>
27 
28 using ::aidl::android::hardware::ir::ConsumerIrFreqRange;
29 
30 namespace aidl::android::hardware::ir {
31 
32 class ConsumerIr : public BnConsumerIr {
33   public:
34     ConsumerIr();
35   private:
36     ::ndk::ScopedAStatus getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) override;
37     ::ndk::ScopedAStatus transmit(int32_t in_carrierFreqHz,
38                                   const std::vector<int32_t>& in_pattern) override;
39     consumerir_device_t *mDevice = nullptr;
40 };
41 
ConsumerIr()42 ConsumerIr::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         // note - may want to make this a fatal error - otherwise the service will crash when it's used
53         ALOGE("Can't open consumer IR transmitter, error: %d", ret);
54         // in case it's modified
55         mDevice = nullptr;
56     }
57 }
58 
getCarrierFreqs(std::vector<ConsumerIrFreqRange> * _aidl_return)59 ::ndk::ScopedAStatus ConsumerIr::getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) {
60     int32_t len = mDevice->get_num_carrier_freqs(mDevice);
61     if (len < 0) {
62         (*_aidl_return).clear();
63         return ::ndk::ScopedAStatus::ok();
64     }
65 
66     consumerir_freq_range_t *rangeAr = new consumerir_freq_range_t[len];
67     bool success = (mDevice->get_carrier_freqs(mDevice, len, rangeAr) >= 0);
68     if (!success) {
69         (*_aidl_return).clear();
70         return ::ndk::ScopedAStatus::ok();
71     }
72 
73     (*_aidl_return).resize(len);
74     for (int32_t i = 0; i < len; i++) {
75         (*_aidl_return)[i].minHz = static_cast<uint32_t>(rangeAr[i].min);
76         (*_aidl_return)[i].maxHz = static_cast<uint32_t>(rangeAr[i].max);
77     }
78     return ::ndk::ScopedAStatus::ok();
79 }
80 
transmit(int32_t in_carrierFreqHz,const std::vector<int32_t> & in_pattern)81 ::ndk::ScopedAStatus ConsumerIr::transmit(int32_t in_carrierFreqHz,
82                                           const std::vector<int32_t>& in_pattern) {
83     if (in_carrierFreqHz > 0) {
84         mDevice->transmit(mDevice, in_carrierFreqHz, in_pattern.data(), in_pattern.size());
85         return ::ndk::ScopedAStatus::ok();
86     } else {
87         return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
88     }
89 }
90 
91 }  // namespace aidl::android::hardware::ir
92 
93 using aidl::android::hardware::ir::ConsumerIr;
94 
main()95 int main() {
96     auto binder = ::ndk::SharedRefBase::make<ConsumerIr>();
97     const std::string name = std::string() + ConsumerIr::descriptor + "/default";
98     CHECK_EQ(STATUS_OK, AServiceManager_addService(binder->asBinder().get(), name.c_str()))
99             << "Failed to register " << name;
100 
101     ABinderProcess_setThreadPoolMaxThreadCount(0);
102     ABinderProcess_joinThreadPool();
103 
104     return EXIT_FAILURE;  // should not reached
105 }
106