blob: 7c4a8169c841e3734859518d724bcff8aa496347 [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>
18#include <android-base/logging.h>
19#include <android/binder_interface_utils.h>
20#include <android/binder_manager.h>
21#include <android/binder_process.h>
22#include <numeric>
23
24namespace aidl::android::hardware::ir {
25
26const std::vector<ConsumerIrFreqRange> kSupportedFreqs = {
27 {2000, 4000},
28 {10000, 30000},
29};
30
31class ConsumerIr : public BnConsumerIr {
32 ::ndk::ScopedAStatus getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) override;
Devin Moore48113812022-01-10 17:42:53 +000033 ::ndk::ScopedAStatus transmit(int32_t in_carrierFreqHz,
Devin Moore0de7ad62021-11-05 17:30:04 +000034 const std::vector<int32_t>& in_pattern) override;
35};
36
37::ndk::ScopedAStatus ConsumerIr::getCarrierFreqs(std::vector<ConsumerIrFreqRange>* _aidl_return) {
38 *_aidl_return = kSupportedFreqs;
39 return ::ndk::ScopedAStatus::ok();
40}
41
42bool isSupportedFreq(int32_t freq) {
43 for (const auto& range : kSupportedFreqs) {
44 if (freq >= range.minHz && freq <= range.maxHz) return true;
45 }
46 return false;
47}
48
Devin Moore48113812022-01-10 17:42:53 +000049::ndk::ScopedAStatus ConsumerIr::transmit(int32_t in_carrierFreqHz,
Devin Moore0de7ad62021-11-05 17:30:04 +000050 const std::vector<int32_t>& in_pattern) {
Devin Moore48113812022-01-10 17:42:53 +000051 if (isSupportedFreq(in_carrierFreqHz)) {
Devin Moore0de7ad62021-11-05 17:30:04 +000052 // trasmit the pattern, each integer is number of microseconds in an
53 // alternating on/off state.
54 usleep(std::accumulate(in_pattern.begin(), in_pattern.end(), 0));
55 return ::ndk::ScopedAStatus::ok();
56 } else {
57 // unsupported operation
58 return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
59 }
60 return ::ndk::ScopedAStatus::ok();
61}
62
63} // namespace aidl::android::hardware::ir
64
65using aidl::android::hardware::ir::ConsumerIr;
66
67int main() {
68 auto binder = ::ndk::SharedRefBase::make<ConsumerIr>();
69 const std::string name = std::string() + ConsumerIr::descriptor + "/default";
70 CHECK_EQ(STATUS_OK, AServiceManager_addService(binder->asBinder().get(), name.c_str()))
71 << "Failed to register " << name;
72
73 ABinderProcess_setThreadPoolMaxThreadCount(0);
74 ABinderProcess_joinThreadPool();
75
76 return EXIT_FAILURE; // should not reached
77}