Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 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> |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 18 | #include <aidl/android/hardware/ir/ConsumerIrFreqRange.h> |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 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> |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 23 | #include <hardware/consumerir.h> |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 24 | #include <numeric> |
| 25 | |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 26 | #include <log/log.h> |
| 27 | |
| 28 | using ::aidl::android::hardware::ir::ConsumerIrFreqRange; |
| 29 | |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 30 | namespace aidl::android::hardware::ir { |
| 31 | |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 32 | class ConsumerIr : public BnConsumerIr { |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 33 | public: |
| 34 | ConsumerIr(); |
| 35 | private: |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 36 | ::ndk::ScopedAStatus getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) override; |
Devin Moore | 4811381 | 2022-01-10 17:42:53 +0000 | [diff] [blame] | 37 | ::ndk::ScopedAStatus transmit(int32_t in_carrierFreqHz, |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 38 | const std::vector<int32_t>& in_pattern) override; |
Steven Moreland | 19d780b | 2022-10-31 20:43:41 +0000 | [diff] [blame] | 39 | consumerir_device_t *mDevice = nullptr; |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 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) { |
Steven Moreland | 19d780b | 2022-10-31 20:43:41 +0000 | [diff] [blame] | 52 | // note - may want to make this a fatal error - otherwise the service will crash when it's used |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 53 | ALOGE("Can't open consumer IR transmitter, error: %d", ret); |
Steven Moreland | 19d780b | 2022-10-31 20:43:41 +0000 | [diff] [blame] | 54 | // in case it's modified |
| 55 | mDevice = nullptr; |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 56 | } |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 57 | } |
| 58 | |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 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(); |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 64 | } |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 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(); |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Devin Moore | 4811381 | 2022-01-10 17:42:53 +0000 | [diff] [blame] | 81 | ::ndk::ScopedAStatus ConsumerIr::transmit(int32_t in_carrierFreqHz, |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 82 | const std::vector<int32_t>& in_pattern) { |
sangweilin | 94f6878 | 2022-10-31 18:21:27 +0800 | [diff] [blame] | 83 | if (in_carrierFreqHz > 0) { |
| 84 | mDevice->transmit(mDevice, in_carrierFreqHz, in_pattern.data(), in_pattern.size()); |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 85 | return ::ndk::ScopedAStatus::ok(); |
| 86 | } else { |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 87 | return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 88 | } |
Devin Moore | 0de7ad6 | 2021-11-05 17:30:04 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace aidl::android::hardware::ir |
| 92 | |
| 93 | using aidl::android::hardware::ir::ConsumerIr; |
| 94 | |
| 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 | } |