blob: db9f4c156c625fad86e8b3eb908d14a0d52ad3c1 [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"
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053018#include <android-base/logging.h>
19#include <fcntl.h>
20#include <utils/Log.h>
21
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053022#include "HdmiConnectionMock.h"
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053023
24using ndk::ScopedAStatus;
25
26namespace android {
27namespace hardware {
28namespace tv {
29namespace hdmi {
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053030namespace connection {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053031namespace implementation {
32
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053033void HdmiConnectionMock::serviceDied(void* cookie) {
34 ALOGE("HdmiConnectionMock died");
35 auto hdmi = static_cast<HdmiConnectionMock*>(cookie);
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053036 hdmi->mHdmiThreadRun = false;
37}
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) {
47 *_aidl_return = mPortConnectionStatus[portId];
48 } 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) {
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
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053072ScopedAStatus HdmiConnectionMock::setHpdSignal(HpdSignal signal) {
Venkatarama Avadhani04ee1a42022-11-09 16:21:55 +053073 if (mHdmiThreadRun) {
74 mHpdSignal = signal;
75 return ScopedAStatus::ok();
76 } else {
77 return ScopedAStatus::fromServiceSpecificError(
78 static_cast<int32_t>(Result::FAILURE_INVALID_STATE));
79 }
80}
81
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053082ScopedAStatus HdmiConnectionMock::getHpdSignal(HpdSignal* _aidl_return) {
Venkatarama Avadhani04ee1a42022-11-09 16:21:55 +053083 *_aidl_return = mHpdSignal;
84 return ScopedAStatus::ok();
85}
86
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053087void* HdmiConnectionMock::__threadLoop(void* user) {
88 HdmiConnectionMock* const self = static_cast<HdmiConnectionMock*>(user);
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053089 self->threadLoop();
90 return 0;
91}
92
Venkatarama Avadhani601d2992022-12-12 22:29:30 +053093int HdmiConnectionMock::readMessageFromFifo(unsigned char* buf, int msgCount) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +053094 if (msgCount <= 0 || !buf) {
95 return 0;
96 }
97
98 int ret = -1;
99 // Maybe blocked at driver
100 ret = read(mInputFile, buf, msgCount);
101 if (ret < 0) {
102 ALOGE("[halimp_aidl] read :%s failed, ret:%d\n", HDMI_MSG_IN_FIFO, ret);
103 return -1;
104 }
105
106 return ret;
107}
108
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530109void HdmiConnectionMock::printEventBuf(const char* msg_buf, int len) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530110 int i, size = 0;
111 const int bufSize = MESSAGE_BODY_MAX_LENGTH * 3;
112 // Use 2 characters for each byte in the message plus 1 space
113 char buf[bufSize] = {0};
114
115 // Messages longer than max length will be truncated.
116 for (i = 0; i < len && size < bufSize; i++) {
117 size += sprintf(buf + size, " %02x", msg_buf[i]);
118 }
119 ALOGD("[halimp_aidl] %s, msg:%.*s", __FUNCTION__, size, buf);
120}
121
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530122void HdmiConnectionMock::handleHotplugMessage(unsigned char* msgBuf) {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530123 bool connected = ((msgBuf[3]) & 0xf) > 0;
124 int32_t portId = static_cast<uint32_t>(msgBuf[0] & 0xf);
125
126 if (portId > static_cast<int32_t>(mPortInfos.size())) {
127 ALOGD("[halimp_aidl] ignore hot plug message, id %x does not exist", portId);
128 return;
129 }
130
131 ALOGD("[halimp_aidl] hot plug port id %x, is connected %x", (msgBuf[0] & 0xf),
132 (msgBuf[3] & 0xf));
133 mPortConnectionStatus[portId] = connected;
134 if (mPortInfos[portId].type == HdmiPortType::OUTPUT) {
135 mPhysicalAddress = (connected ? 0xffff : ((msgBuf[1] << 8) | (msgBuf[2])));
136 mPortInfos[portId].physicalAddress = mPhysicalAddress;
137 ALOGD("[halimp_aidl] hot plug physical address %x", mPhysicalAddress);
138 }
139
140 if (mCallback != nullptr) {
141 mCallback->onHotplugEvent(connected, portId);
142 }
143}
144
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530145void HdmiConnectionMock::threadLoop() {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530146 ALOGD("[halimp_aidl] threadLoop start.");
147 unsigned char msgBuf[MESSAGE_BODY_MAX_LENGTH];
148 int r = -1;
149
150 // Open the input pipe
151 while (mInputFile < 0) {
152 usleep(1000 * 1000);
153 mInputFile = open(HDMI_MSG_IN_FIFO, O_RDONLY | O_CLOEXEC);
154 }
155 ALOGD("[halimp_aidl] file open ok, fd = %d.", mInputFile);
156
157 while (mHdmiThreadRun) {
158 memset(msgBuf, 0, sizeof(msgBuf));
159 // Try to get a message from dev.
160 // echo -n -e '\x04\x83' >> /dev/cec
161 r = readMessageFromFifo(msgBuf, MESSAGE_BODY_MAX_LENGTH);
162 if (r <= 1) {
163 // Ignore received ping messages
164 continue;
165 }
166
167 printEventBuf((const char*)msgBuf, r);
168
169 if (((msgBuf[0] >> 4) & 0xf) == 0xf) {
170 handleHotplugMessage(msgBuf);
171 }
172 }
173
174 ALOGD("[halimp_aidl] thread end.");
175}
176
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530177HdmiConnectionMock::HdmiConnectionMock() {
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530178 ALOGE("[halimp_aidl] Opening a virtual HDMI HAL for testing and virtual machine.");
179 mCallback = nullptr;
180 mPortInfos.resize(mTotalPorts);
181 mPortConnectionStatus.resize(mTotalPorts);
182 mPortInfos[0] = {.type = HdmiPortType::OUTPUT,
183 .portId = static_cast<uint32_t>(1),
184 .cecSupported = true,
185 .arcSupported = false,
Venkatarama Avadhani3d35efc2022-11-02 10:16:28 +0530186 .eArcSupported = false,
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530187 .physicalAddress = mPhysicalAddress};
188 mPortConnectionStatus[0] = false;
189 mDeathRecipient = ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(serviceDied));
190}
191
192} // namespace implementation
Venkatarama Avadhani601d2992022-12-12 22:29:30 +0530193} // namespace connection
Venkatarama Avadhani820b5482022-05-18 15:19:04 +0530194} // namespace hdmi
195} // namespace tv
196} // namespace hardware
197} // namespace android