1 /*
2  * Copyright (C) 2022 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 #define LOG_TAG "android.hardware.tv.hdmi.earc"
18 #include <android-base/logging.h>
19 #include <fcntl.h>
20 #include <utils/Log.h>
21 
22 #include "EArcMock.h"
23 
24 using ndk::ScopedAStatus;
25 
26 namespace android {
27 namespace hardware {
28 namespace tv {
29 namespace hdmi {
30 namespace earc {
31 namespace implementation {
32 
serviceDied(void * cookie)33 void EArcMock::serviceDied(void* cookie) {
34     ALOGE("EArcMock died");
35     auto eArc = static_cast<EArcMock*>(cookie);
36     eArc->mEArcEnabled = false;
37 }
38 
setEArcEnabled(bool in_enabled)39 ScopedAStatus EArcMock::setEArcEnabled(bool in_enabled) {
40     mEArcEnabled = in_enabled;
41     if (mEArcEnabled != in_enabled) {
42         return ScopedAStatus::fromServiceSpecificError(
43                 static_cast<int32_t>(Result::FAILURE_UNKNOWN));
44     } else {
45         return ScopedAStatus::ok();
46     }
47 }
48 
isEArcEnabled(bool * _aidl_return)49 ScopedAStatus EArcMock::isEArcEnabled(bool* _aidl_return) {
50     *_aidl_return = mEArcEnabled;
51     return ScopedAStatus::ok();
52 }
53 
getState(int32_t portId,IEArcStatus * _aidl_return)54 ScopedAStatus EArcMock::getState(int32_t portId, IEArcStatus* _aidl_return) {
55     // Maintain port connection status and update on hotplug event
56     if (portId <= mTotalPorts && portId >= 1) {
57         *_aidl_return = mPortStatus.at(portId - 1);
58     } else {
59         return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
60     }
61 
62     return ScopedAStatus::ok();
63 }
64 
getLastReportedAudioCapabilities(int32_t portId,std::vector<uint8_t> * _aidl_return)65 ScopedAStatus EArcMock::getLastReportedAudioCapabilities(int32_t portId,
66                                                          std::vector<uint8_t>* _aidl_return) {
67     if (portId <= mTotalPorts && portId >= 1) {
68         *_aidl_return = mCapabilities.at(portId - 1);
69     } else {
70         return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
71     }
72 
73     return ScopedAStatus::ok();
74 }
75 
setCallback(const std::shared_ptr<IEArcCallback> & callback)76 ScopedAStatus EArcMock::setCallback(const std::shared_ptr<IEArcCallback>& callback) {
77     if (mCallback != nullptr) {
78         mCallback = nullptr;
79     }
80 
81     if (callback != nullptr) {
82         mCallback = callback;
83         AIBinder_linkToDeath(this->asBinder().get(), mDeathRecipient.get(), 0 /* cookie */);
84     }
85     return ScopedAStatus::ok();
86 }
87 
reportCapabilities(const std::vector<uint8_t> & capabilities,int32_t portId)88 ScopedAStatus EArcMock::reportCapabilities(const std::vector<uint8_t>& capabilities,
89                                            int32_t portId) {
90     if (mCallback != nullptr) {
91         mCallback->onCapabilitiesReported(capabilities, portId);
92         return ScopedAStatus::ok();
93     } else {
94         return ScopedAStatus::fromExceptionCode(EX_NULL_POINTER);
95     }
96 }
97 
changeState(const IEArcStatus status,int32_t portId)98 ScopedAStatus EArcMock::changeState(const IEArcStatus status, int32_t portId) {
99     if (mCallback != nullptr) {
100         mCallback->onStateChange(status, portId);
101         return ScopedAStatus::ok();
102     } else {
103         return ScopedAStatus::fromExceptionCode(EX_NULL_POINTER);
104     }
105 }
106 
EArcMock()107 EArcMock::EArcMock() {
108     ALOGE("[halimp_aidl] Opening a virtual eARC HAL for testing and virtual machine.");
109     mCallback = nullptr;
110     mCapabilities.resize(mTotalPorts);
111     mPortStatus.resize(mTotalPorts);
112     mPortStatus[0] = IEArcStatus::IDLE;
113     mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(serviceDied));
114 }
115 
116 }  // namespace implementation
117 }  // namespace earc
118 }  // namespace hdmi
119 }  // namespace tv
120 }  // namespace hardware
121 }  // namespace android
122