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 <android/hardware/tv/cec/1.1/IHdmiCec.h>
18 #include <hidl/Status.h>
19 #include <algorithm>
20 #include <vector>
21 
22 using namespace std;
23 
24 namespace android {
25 namespace hardware {
26 namespace tv {
27 namespace cec {
28 namespace V1_1 {
29 namespace implementation {
30 
31 using ::android::sp;
32 using ::android::hardware::hidl_string;
33 using ::android::hardware::hidl_vec;
34 using ::android::hardware::Return;
35 using ::android::hardware::Void;
36 using ::android::hardware::tv::cec::V1_0::CecLogicalAddress;
37 using ::android::hardware::tv::cec::V1_0::CecMessage;
38 using ::android::hardware::tv::cec::V1_0::HdmiPortInfo;
39 using ::android::hardware::tv::cec::V1_0::HdmiPortType;
40 using ::android::hardware::tv::cec::V1_0::HotplugEvent;
41 using ::android::hardware::tv::cec::V1_0::IHdmiCecCallback;
42 using ::android::hardware::tv::cec::V1_0::MaxLength;
43 using ::android::hardware::tv::cec::V1_0::OptionKey;
44 using ::android::hardware::tv::cec::V1_0::Result;
45 using ::android::hardware::tv::cec::V1_0::SendMessageResult;
46 using ::android::hardware::tv::cec::V1_1::IHdmiCec;
47 
48 #define CEC_MSG_IN_FIFO "/dev/cec_in_pipe"
49 #define CEC_MSG_OUT_FIFO "/dev/cec_out_pipe"
50 
51 struct HdmiCecMock : public IHdmiCec, public hidl_death_recipient {
52     HdmiCecMock();
53     // Methods from ::android::hardware::tv::cec::V1_0::IHdmiCec follow.
54     Return<Result> addLogicalAddress(CecLogicalAddress addr) override;
55     Return<void> clearLogicalAddress() override;
56     Return<void> getPhysicalAddress(getPhysicalAddress_cb _hidl_cb) override;
57     Return<SendMessageResult> sendMessage(const CecMessage& message) override;
58     Return<void> setCallback(
59             const sp<::android::hardware::tv::cec::V1_0::IHdmiCecCallback>& callback) override;
60     Return<int32_t> getCecVersion() override;
61     Return<uint32_t> getVendorId() override;
62     Return<void> getPortInfo(getPortInfo_cb _hidl_cb) override;
63     Return<void> setOption(OptionKey key, bool value) override;
64     Return<void> setLanguage(const hidl_string& language) override;
65     Return<void> enableAudioReturnChannel(int32_t portId, bool enable) override;
66     Return<bool> isConnected(int32_t portId) override;
67 
68     // Methods from ::android::hardware::tv::cec::V1_1::IHdmiCec follow.
69     Return<Result> addLogicalAddress_1_1(
70             ::android::hardware::tv::cec::V1_1::CecLogicalAddress addr) override;
71     Return<SendMessageResult> sendMessage_1_1(
72             const ::android::hardware::tv::cec::V1_1::CecMessage& message) override;
73     Return<void> setCallback_1_1(
74             const sp<::android::hardware::tv::cec::V1_1::IHdmiCecCallback>& callback) override;
75 
serviceDiedHdmiCecMock76     virtual void serviceDied(uint64_t /*cookie*/,
77                              const wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
78         setCallback(nullptr);
79     }
80 
81     void cec_set_option(int flag, int value);
82     void printCecMsgBuf(const char* msg_buf, int len);
83 
84   private:
85     static void* __threadLoop(void* data);
86     void threadLoop();
87     int readMessageFromFifo(unsigned char* buf, int msgCount);
88     int sendMessageToFifo(const ::android::hardware::tv::cec::V1_1::CecMessage& message);
89     void handleHotplugMessage(unsigned char* msgBuf);
90     void handleCecMessage(unsigned char* msgBuf, int length);
91 
92   private:
93     sp<::android::hardware::tv::cec::V1_1::IHdmiCecCallback> mCallback;
94 
95     // Variables for the virtual cec hal impl
96     uint16_t mPhysicalAddress = 0xFFFF;
97     vector<::android::hardware::tv::cec::V1_1::CecLogicalAddress> mLogicalAddresses;
98     int32_t mCecVersion = 0x06;
99     uint32_t mCecVendorId = 0x01;
100 
101     // Port configuration
102     int mTotalPorts = 1;
103     hidl_vec<HdmiPortInfo> mPortInfo;
104     hidl_vec<bool> mPortConnectionStatus;
105 
106     // CEC Option value
107     int mOptionWakeUp = 0;
108     int mOptionEnableCec = 0;
109     int mOptionSystemCecControl = 0;
110     int mOptionLanguage = 0;
111 
112     // Testing variables
113     // Input file descriptor
114     int mInputFile;
115     // Output file descriptor
116     int mOutputFile;
117     bool mCecThreadRun = true;
118     pthread_t mThreadId = 0;
119 };
120 }  // namespace implementation
121 }  // namespace V1_1
122 }  // namespace cec
123 }  // namespace tv
124 }  // namespace hardware
125 }  // namespace android