blob: 954982e4bcdb521012196dd3307657f1abfd4ba4 [file] [log] [blame]
Venkatarama Avadhani820b5482022-05-18 15:19:04 +05301/*
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
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053017#define LOG_TAG "android.hardware.tv.hdmi.connection"
Sham Rathodd0a67fa2023-12-08 16:25:31 +053018#include "HdmiConnectionMock.h"
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053019#include <android-base/logging.h>
20#include <fcntl.h>
21#include <utils/Log.h>
22
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053023using ndk::ScopedAStatus;
24
25namespace android {
26namespace hardware {
27namespace tv {
28namespace hdmi {
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053029namespace connection {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053030namespace implementation {
31
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053032void HdmiConnectionMock::serviceDied(void* cookie) {
33 ALOGE("HdmiConnectionMock died");
34 auto hdmi = static_cast<HdmiConnectionMock*>(cookie);
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053035 hdmi->mHdmiThreadRun = false;
Sham Rathodd0a67fa2023-12-08 16:25:31 +053036 pthread_join(hdmi->mThreadId, NULL);
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053037}
38
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053039ScopedAStatus HdmiConnectionMock::getPortInfo(std::vector<HdmiPortInfo>* _aidl_return) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053040 *_aidl_return = mPortInfos;
41 return ScopedAStatus::ok();
42}
43
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053044ScopedAStatus HdmiConnectionMock::isConnected(int32_t portId, bool* _aidl_return) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053045 // Maintain port connection status and update on hotplug event
46 if (portId <= mTotalPorts && portId >= 1) {
Nathalie Le Clair28318a32023-01-24 10:25:45 +010047 *_aidl_return = mPortConnectionStatus.at(portId - 1);
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053048 } else {
49 *_aidl_return = false;
50 }
51
52 return ScopedAStatus::ok();
53}
54
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053055ScopedAStatus HdmiConnectionMock::setCallback(
56 const std::shared_ptr<IHdmiConnectionCallback>& callback) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053057 if (mCallback != nullptr) {
Sham Rathodd0a67fa2023-12-08 16:25:31 +053058 stopThread();
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053059 mCallback = nullptr;
60 }
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053061 if (callback != nullptr) {
62 mCallback = callback;
Sham Rathodd0a67fa2023-12-08 16:25:31 +053063 mDeathRecipient =
64 ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(serviceDied));
65
66 AIBinder_linkToDeath(callback->asBinder().get(), mDeathRecipient.get(), this /* cookie */);
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053067
68 mInputFile = open(HDMI_MSG_IN_FIFO, O_RDWR | O_CLOEXEC);
69 pthread_create(&mThreadId, NULL, __threadLoop, this);
70 pthread_setname_np(mThreadId, "hdmi_loop");
71 }
72 return ScopedAStatus::ok();
73}
74
Nathalie Le Clair4eaa1782023-01-24 17:26:42 +010075ScopedAStatus HdmiConnectionMock::setHpdSignal(HpdSignal signal, int32_t portId) {
Nathalie Le Clair501c3c12023-02-10 10:06:23 +010076 if (portId > mTotalPorts || portId < 1) {
77 return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
78 }
79 if (!mHdmiThreadRun) {
Venkatarama Avadhani04ee1a42022-11-09 16:21:55 +053080 return ScopedAStatus::fromServiceSpecificError(
81 static_cast<int32_t>(Result::FAILURE_INVALID_STATE));
82 }
Nathalie Le Clair501c3c12023-02-10 10:06:23 +010083 mHpdSignal.at(portId - 1) = signal;
84 return ScopedAStatus::ok();
Venkatarama Avadhani04ee1a42022-11-09 16:21:55 +053085}
86
Nathalie Le Clair4eaa1782023-01-24 17:26:42 +010087ScopedAStatus HdmiConnectionMock::getHpdSignal(int32_t portId, HpdSignal* _aidl_return) {
Nathalie Le Clair501c3c12023-02-10 10:06:23 +010088 if (portId > mTotalPorts || portId < 1) {
89 return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
90 }
Nathalie Le Clair4eaa1782023-01-24 17:26:42 +010091 *_aidl_return = mHpdSignal.at(portId - 1);
Venkatarama Avadhani04ee1a42022-11-09 16:21:55 +053092 return ScopedAStatus::ok();
93}
94
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053095void* HdmiConnectionMock::__threadLoop(void* user) {
96 HdmiConnectionMock* const self = static_cast<HdmiConnectionMock*>(user);
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053097 self->threadLoop();
98 return 0;
99}
100
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530101int HdmiConnectionMock::readMessageFromFifo(unsigned char* buf, int msgCount) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530102 if (msgCount <= 0 || !buf) {
103 return 0;
104 }
105
106 int ret = -1;
107 // Maybe blocked at driver
108 ret = read(mInputFile, buf, msgCount);
109 if (ret < 0) {
110 ALOGE("[halimp_aidl] read :%s failed, ret:%d\n", HDMI_MSG_IN_FIFO, ret);
111 return -1;
112 }
113
114 return ret;
115}
116
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530117void HdmiConnectionMock::printEventBuf(const char* msg_buf, int len) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530118 int i, size = 0;
119 const int bufSize = MESSAGE_BODY_MAX_LENGTH * 3;
120 // Use 2 characters for each byte in the message plus 1 space
121 char buf[bufSize] = {0};
122
123 // Messages longer than max length will be truncated.
124 for (i = 0; i < len && size < bufSize; i++) {
125 size += sprintf(buf + size, " %02x", msg_buf[i]);
126 }
127 ALOGD("[halimp_aidl] %s, msg:%.*s", __FUNCTION__, size, buf);
128}
129
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530130void HdmiConnectionMock::handleHotplugMessage(unsigned char* msgBuf) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530131 bool connected = ((msgBuf[3]) & 0xf) > 0;
132 int32_t portId = static_cast<uint32_t>(msgBuf[0] & 0xf);
133
Nathalie Le Clair501c3c12023-02-10 10:06:23 +0100134 if (portId > static_cast<int32_t>(mPortInfos.size()) || portId < 1) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530135 ALOGD("[halimp_aidl] ignore hot plug message, id %x does not exist", portId);
136 return;
137 }
138
139 ALOGD("[halimp_aidl] hot plug port id %x, is connected %x", (msgBuf[0] & 0xf),
140 (msgBuf[3] & 0xf));
Nathalie Le Clair28318a32023-01-24 10:25:45 +0100141 mPortConnectionStatus.at(portId - 1) = connected;
142 if (mPortInfos.at(portId - 1).type == HdmiPortType::OUTPUT) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530143 mPhysicalAddress = (connected ? 0xffff : ((msgBuf[1] << 8) | (msgBuf[2])));
Nathalie Le Clair28318a32023-01-24 10:25:45 +0100144 mPortInfos.at(portId - 1).physicalAddress = mPhysicalAddress;
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530145 ALOGD("[halimp_aidl] hot plug physical address %x", mPhysicalAddress);
146 }
147
148 if (mCallback != nullptr) {
149 mCallback->onHotplugEvent(connected, portId);
150 }
151}
152
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530153void HdmiConnectionMock::threadLoop() {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530154 ALOGD("[halimp_aidl] threadLoop start.");
155 unsigned char msgBuf[MESSAGE_BODY_MAX_LENGTH];
156 int r = -1;
157
158 // Open the input pipe
Sham Rathodd0a67fa2023-12-08 16:25:31 +0530159 while (mHdmiThreadRun && mInputFile < 0) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530160 usleep(1000 * 1000);
161 mInputFile = open(HDMI_MSG_IN_FIFO, O_RDONLY | O_CLOEXEC);
162 }
163 ALOGD("[halimp_aidl] file open ok, fd = %d.", mInputFile);
164
165 while (mHdmiThreadRun) {
166 memset(msgBuf, 0, sizeof(msgBuf));
167 // Try to get a message from dev.
168 // echo -n -e '\x04\x83' >> /dev/cec
169 r = readMessageFromFifo(msgBuf, MESSAGE_BODY_MAX_LENGTH);
170 if (r <= 1) {
171 // Ignore received ping messages
172 continue;
173 }
174
175 printEventBuf((const char*)msgBuf, r);
176
177 if (((msgBuf[0] >> 4) & 0xf) == 0xf) {
178 handleHotplugMessage(msgBuf);
179 }
180 }
181
182 ALOGD("[halimp_aidl] thread end.");
183}
184
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530185HdmiConnectionMock::HdmiConnectionMock() {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530186 ALOGE("[halimp_aidl] Opening a virtual HDMI HAL for testing and virtual machine.");
187 mCallback = nullptr;
188 mPortInfos.resize(mTotalPorts);
189 mPortConnectionStatus.resize(mTotalPorts);
Nathalie Le Clair4eaa1782023-01-24 17:26:42 +0100190 mHpdSignal.resize(mTotalPorts);
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530191 mPortInfos[0] = {.type = HdmiPortType::OUTPUT,
192 .portId = static_cast<uint32_t>(1),
193 .cecSupported = true,
194 .arcSupported = false,
Venkatarama Avadhani3d35efc2022-11-02 10:16:28 +0530195 .eArcSupported = false,
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530196 .physicalAddress = mPhysicalAddress};
197 mPortConnectionStatus[0] = false;
Nathalie Le Clair4eaa1782023-01-24 17:26:42 +0100198 mHpdSignal[0] = HpdSignal::HDMI_HPD_PHYSICAL;
Sham Rathodd0a67fa2023-12-08 16:25:31 +0530199 mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(nullptr);
200}
201
202void HdmiConnectionMock::stopThread() {
203 if (mCallback != nullptr) {
204 ALOGE("[halimp_aidl] HdmiConnectionMock shutting down.");
205 mCallback = nullptr;
206 mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(nullptr);
207 mHdmiThreadRun = false;
208 pthread_join(mThreadId, NULL);
209 }
210}
211
212HdmiConnectionMock::~HdmiConnectionMock() {
213 stopThread();
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530214}
215
216} // namespace implementation
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530217} // namespace connection
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530218} // namespace hdmi
219} // namespace tv
220} // namespace hardware
221} // namespace android