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