• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.connection"
18 #include <android-base/logging.h>
19 #include <fcntl.h>
20 #include <utils/Log.h>
21 
22 #include "HdmiConnectionMock.h"
23 
24 using ndk::ScopedAStatus;
25 
26 namespace android {
27 namespace hardware {
28 namespace tv {
29 namespace hdmi {
30 namespace connection {
31 namespace implementation {
32 
serviceDied(void * cookie)33 void HdmiConnectionMock::serviceDied(void* cookie) {
34     ALOGE("HdmiConnectionMock died");
35     auto hdmi = static_cast<HdmiConnectionMock*>(cookie);
36     hdmi->mHdmiThreadRun = false;
37 }
38 
getPortInfo(std::vector<HdmiPortInfo> * _aidl_return)39 ScopedAStatus HdmiConnectionMock::getPortInfo(std::vector<HdmiPortInfo>* _aidl_return) {
40     *_aidl_return = mPortInfos;
41     return ScopedAStatus::ok();
42 }
43 
isConnected(int32_t portId,bool * _aidl_return)44 ScopedAStatus HdmiConnectionMock::isConnected(int32_t portId, bool* _aidl_return) {
45     // Maintain port connection status and update on hotplug event
46     if (portId <= mTotalPorts && portId >= 1) {
47         *_aidl_return = mPortConnectionStatus.at(portId - 1);
48     } else {
49         *_aidl_return = false;
50     }
51 
52     return ScopedAStatus::ok();
53 }
54 
setCallback(const std::shared_ptr<IHdmiConnectionCallback> & callback)55 ScopedAStatus HdmiConnectionMock::setCallback(
56         const std::shared_ptr<IHdmiConnectionCallback>& callback) {
57     if (mCallback != nullptr) {
58         mCallback = nullptr;
59     }
60 
61     if (callback != nullptr) {
62         mCallback = callback;
63         AIBinder_linkToDeath(this->asBinder().get(), mDeathRecipient.get(), 0 /* cookie */);
64 
65         mInputFile = open(HDMI_MSG_IN_FIFO, O_RDWR | O_CLOEXEC);
66         pthread_create(&mThreadId, NULL, __threadLoop, this);
67         pthread_setname_np(mThreadId, "hdmi_loop");
68     }
69     return ScopedAStatus::ok();
70 }
71 
setHpdSignal(HpdSignal signal,int32_t portId)72 ScopedAStatus HdmiConnectionMock::setHpdSignal(HpdSignal signal, int32_t portId) {
73     if (portId > mTotalPorts || portId < 1) {
74         return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
75     }
76     if (!mHdmiThreadRun) {
77         return ScopedAStatus::fromServiceSpecificError(
78                 static_cast<int32_t>(Result::FAILURE_INVALID_STATE));
79     }
80     mHpdSignal.at(portId - 1) = signal;
81     return ScopedAStatus::ok();
82 }
83 
getHpdSignal(int32_t portId,HpdSignal * _aidl_return)84 ScopedAStatus HdmiConnectionMock::getHpdSignal(int32_t portId, HpdSignal* _aidl_return) {
85     if (portId > mTotalPorts || portId < 1) {
86         return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
87     }
88     *_aidl_return = mHpdSignal.at(portId - 1);
89     return ScopedAStatus::ok();
90 }
91 
__threadLoop(void * user)92 void* HdmiConnectionMock::__threadLoop(void* user) {
93     HdmiConnectionMock* const self = static_cast<HdmiConnectionMock*>(user);
94     self->threadLoop();
95     return 0;
96 }
97 
readMessageFromFifo(unsigned char * buf,int msgCount)98 int HdmiConnectionMock::readMessageFromFifo(unsigned char* buf, int msgCount) {
99     if (msgCount <= 0 || !buf) {
100         return 0;
101     }
102 
103     int ret = -1;
104     // Maybe blocked at driver
105     ret = read(mInputFile, buf, msgCount);
106     if (ret < 0) {
107         ALOGE("[halimp_aidl] read :%s failed, ret:%d\n", HDMI_MSG_IN_FIFO, ret);
108         return -1;
109     }
110 
111     return ret;
112 }
113 
printEventBuf(const char * msg_buf,int len)114 void HdmiConnectionMock::printEventBuf(const char* msg_buf, int len) {
115     int i, size = 0;
116     const int bufSize = MESSAGE_BODY_MAX_LENGTH * 3;
117     // Use 2 characters for each byte in the message plus 1 space
118     char buf[bufSize] = {0};
119 
120     // Messages longer than max length will be truncated.
121     for (i = 0; i < len && size < bufSize; i++) {
122         size += sprintf(buf + size, " %02x", msg_buf[i]);
123     }
124     ALOGD("[halimp_aidl] %s, msg:%.*s", __FUNCTION__, size, buf);
125 }
126 
handleHotplugMessage(unsigned char * msgBuf)127 void HdmiConnectionMock::handleHotplugMessage(unsigned char* msgBuf) {
128     bool connected = ((msgBuf[3]) & 0xf) > 0;
129     int32_t portId = static_cast<uint32_t>(msgBuf[0] & 0xf);
130 
131     if (portId > static_cast<int32_t>(mPortInfos.size()) || portId < 1) {
132         ALOGD("[halimp_aidl] ignore hot plug message, id %x does not exist", portId);
133         return;
134     }
135 
136     ALOGD("[halimp_aidl] hot plug port id %x, is connected %x", (msgBuf[0] & 0xf),
137           (msgBuf[3] & 0xf));
138     mPortConnectionStatus.at(portId - 1) = connected;
139     if (mPortInfos.at(portId - 1).type == HdmiPortType::OUTPUT) {
140         mPhysicalAddress = (connected ? 0xffff : ((msgBuf[1] << 8) | (msgBuf[2])));
141         mPortInfos.at(portId - 1).physicalAddress = mPhysicalAddress;
142         ALOGD("[halimp_aidl] hot plug physical address %x", mPhysicalAddress);
143     }
144 
145     if (mCallback != nullptr) {
146         mCallback->onHotplugEvent(connected, portId);
147     }
148 }
149 
threadLoop()150 void HdmiConnectionMock::threadLoop() {
151     ALOGD("[halimp_aidl] threadLoop start.");
152     unsigned char msgBuf[MESSAGE_BODY_MAX_LENGTH];
153     int r = -1;
154 
155     // Open the input pipe
156     while (mInputFile < 0) {
157         usleep(1000 * 1000);
158         mInputFile = open(HDMI_MSG_IN_FIFO, O_RDONLY | O_CLOEXEC);
159     }
160     ALOGD("[halimp_aidl] file open ok, fd = %d.", mInputFile);
161 
162     while (mHdmiThreadRun) {
163         memset(msgBuf, 0, sizeof(msgBuf));
164         // Try to get a message from dev.
165         // echo -n -e '\x04\x83' >> /dev/cec
166         r = readMessageFromFifo(msgBuf, MESSAGE_BODY_MAX_LENGTH);
167         if (r <= 1) {
168             // Ignore received ping messages
169             continue;
170         }
171 
172         printEventBuf((const char*)msgBuf, r);
173 
174         if (((msgBuf[0] >> 4) & 0xf) == 0xf) {
175             handleHotplugMessage(msgBuf);
176         }
177     }
178 
179     ALOGD("[halimp_aidl] thread end.");
180 }
181 
HdmiConnectionMock()182 HdmiConnectionMock::HdmiConnectionMock() {
183     ALOGE("[halimp_aidl] Opening a virtual HDMI HAL for testing and virtual machine.");
184     mCallback = nullptr;
185     mPortInfos.resize(mTotalPorts);
186     mPortConnectionStatus.resize(mTotalPorts);
187     mHpdSignal.resize(mTotalPorts);
188     mPortInfos[0] = {.type = HdmiPortType::OUTPUT,
189                      .portId = static_cast<uint32_t>(1),
190                      .cecSupported = true,
191                      .arcSupported = false,
192                      .eArcSupported = false,
193                      .physicalAddress = mPhysicalAddress};
194     mPortConnectionStatus[0] = false;
195     mHpdSignal[0] = HpdSignal::HDMI_HPD_PHYSICAL;
196     mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(serviceDied));
197 }
198 
199 }  // namespace implementation
200 }  // namespace connection
201 }  // namespace hdmi
202 }  // namespace tv
203 }  // namespace hardware
204 }  // namespace android
205