xref: /aosp_15_r20/hardware/interfaces/tv/hdmi/cec/aidl/default/HdmiCecMock.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 #include <aidl/android/hardware/tv/hdmi/cec/BnHdmiCec.h>
18 #include <algorithm>
19 #include <vector>
20 
21 using namespace std;
22 
23 namespace android {
24 namespace hardware {
25 namespace tv {
26 namespace hdmi {
27 namespace cec {
28 namespace implementation {
29 
30 using ::aidl::android::hardware::tv::hdmi::cec::BnHdmiCec;
31 using ::aidl::android::hardware::tv::hdmi::cec::CecLogicalAddress;
32 using ::aidl::android::hardware::tv::hdmi::cec::CecMessage;
33 using ::aidl::android::hardware::tv::hdmi::cec::IHdmiCec;
34 using ::aidl::android::hardware::tv::hdmi::cec::IHdmiCecCallback;
35 using ::aidl::android::hardware::tv::hdmi::cec::Result;
36 using ::aidl::android::hardware::tv::hdmi::cec::SendMessageResult;
37 
38 #define CEC_MSG_IN_FIFO "/dev/cec_aidl_in_pipe"
39 #define CEC_MSG_OUT_FIFO "/dev/cec_aidl_out_pipe"
40 
41 struct HdmiCecMock : public BnHdmiCec {
42     HdmiCecMock();
43     ~HdmiCecMock();
44     ::ndk::ScopedAStatus addLogicalAddress(CecLogicalAddress addr, Result* _aidl_return) override;
45     ::ndk::ScopedAStatus clearLogicalAddress() override;
46     ::ndk::ScopedAStatus enableAudioReturnChannel(int32_t portId, bool enable) override;
47     ::ndk::ScopedAStatus getCecVersion(int32_t* _aidl_return) override;
48     ::ndk::ScopedAStatus getPhysicalAddress(int32_t* _aidl_return) override;
49     ::ndk::ScopedAStatus getVendorId(int32_t* _aidl_return) override;
50     ::ndk::ScopedAStatus sendMessage(const CecMessage& message,
51                                      SendMessageResult* _aidl_return) override;
52     ::ndk::ScopedAStatus setCallback(const std::shared_ptr<IHdmiCecCallback>& callback) override;
53     ::ndk::ScopedAStatus setLanguage(const std::string& language) override;
54     ::ndk::ScopedAStatus enableWakeupByOtp(bool value) override;
55     ::ndk::ScopedAStatus enableCec(bool value) override;
56     ::ndk::ScopedAStatus enableSystemCecControl(bool value) override;
57     void printCecMsgBuf(const char* msg_buf, int len);
58     void closeCallback();
59 
60   private:
61     static void* __threadLoop(void* data);
62     void threadLoop();
63     int readMessageFromFifo(unsigned char* buf, int msgCount);
64     int sendMessageToFifo(const CecMessage& message);
65     void handleCecMessage(unsigned char* msgBuf, int length);
66     static void serviceDied(void* cookie);
67 
68     std::shared_ptr<IHdmiCecCallback> mCallback;
69 
70     // Variables for the virtual cec hal impl
71     uint16_t mPhysicalAddress = 0xFFFF;
72     vector<CecLogicalAddress> mLogicalAddresses;
73     int32_t mCecVersion = 0x06;
74     uint32_t mCecVendorId = 0x01;
75 
76     // CEC Option value
77     bool mOptionWakeUp = 0;
78     bool mOptionEnableCec = 0;
79     bool mOptionSystemCecControl = 0;
80     int mOptionLanguage;
81 
82     // Testing variables
83     // Input file descriptor
84     int mInputFile;
85     // Output file descriptor
86     int mOutputFile;
87     bool mCecThreadRun = true;
88     pthread_t mThreadId = 0;
89 
90     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
91 };
92 }  // namespace implementation
93 }  // namespace cec
94 }  // namespace hdmi
95 }  // namespace tv
96 }  // namespace hardware
97 }  // namespace android
98