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 #define LOG_TAG "HdmiCec_hal_test"
18 #include <android-base/logging.h>
19
20 #include <android/hardware/tv/cec/1.0/IHdmiCec.h>
21 #include <android/hardware/tv/cec/1.0/types.h>
22 #include <utils/Log.h>
23 #include <sstream>
24 #include <vector>
25
26 #include <gtest/gtest.h>
27 #include <hidl/GtestPrinter.h>
28 #include <hidl/ServiceManagement.h>
29
30 using ::android::sp;
31 using ::android::hardware::hidl_death_recipient;
32 using ::android::hardware::hidl_vec;
33 using ::android::hardware::Return;
34 using ::android::hardware::tv::cec::V1_0::CecDeviceType;
35 using ::android::hardware::tv::cec::V1_0::CecLogicalAddress;
36 using ::android::hardware::tv::cec::V1_0::CecMessage;
37 using ::android::hardware::tv::cec::V1_0::HdmiPortInfo;
38 using ::android::hardware::tv::cec::V1_0::HdmiPortType;
39 using ::android::hardware::tv::cec::V1_0::IHdmiCec;
40 using ::android::hardware::tv::cec::V1_0::OptionKey;
41 using ::android::hardware::tv::cec::V1_0::Result;
42 using ::android::hardware::tv::cec::V1_0::SendMessageResult;
43
44 #define CEC_VERSION 0x05
45 #define INCORRECT_VENDOR_ID 0x00
46 #define TV_PHYSICAL_ADDRESS 0x0000
47
48 // The main test class for TV CEC HAL.
49 class HdmiCecTest : public ::testing::TestWithParam<std::string> {
50 public:
SetUp()51 void SetUp() override {
52 hdmiCec = IHdmiCec::getService(GetParam());
53 ASSERT_NE(hdmiCec, nullptr);
54 ALOGI("%s: getService() for hdmiCec is %s", __func__,
55 hdmiCec->isRemote() ? "remote" : "local");
56
57 hdmiCec_death_recipient = new HdmiCecDeathRecipient();
58 ASSERT_NE(hdmiCec_death_recipient, nullptr);
59 ASSERT_TRUE(hdmiCec->linkToDeath(hdmiCec_death_recipient, 0).isOk());
60 }
61
getDeviceTypes()62 std::vector<int> getDeviceTypes() {
63 std::vector<int> deviceTypes;
64 FILE* p = popen("getprop ro.hdmi.device_type", "re");
65 if (p) {
66 char* line = NULL;
67 size_t len = 0;
68 if (getline(&line, &len, p) > 0) {
69 std::istringstream stream(line);
70 std::string number{};
71 while (std::getline(stream, number, ',')) {
72 deviceTypes.push_back(stoi(number));
73 }
74 }
75 pclose(p);
76 }
77 return deviceTypes;
78 }
79
hasDeviceType(CecDeviceType type)80 bool hasDeviceType(CecDeviceType type) {
81 std::vector<int> deviceTypes = getDeviceTypes();
82 for (auto deviceType = deviceTypes.begin(); deviceType != deviceTypes.end(); ++deviceType) {
83 if (*deviceType == (int)type) {
84 return true;
85 }
86 }
87 return false;
88 }
89
90 class HdmiCecDeathRecipient : public hidl_death_recipient {
91 public:
serviceDied(uint64_t,const android::wp<::android::hidl::base::V1_0::IBase> &)92 void serviceDied(uint64_t /*cookie*/,
93 const android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) override {
94 FAIL();
95 }
96 };
97
98 sp<IHdmiCec> hdmiCec;
99 sp<HdmiCecDeathRecipient> hdmiCec_death_recipient;
100 };
101
102 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HdmiCecTest);
103 INSTANTIATE_TEST_SUITE_P(
104 PerInstance, HdmiCecTest,
105 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IHdmiCec::descriptor)),
106 android::hardware::PrintInstanceNameToString);
107
TEST_P(HdmiCecTest,ClearAddLogicalAddress)108 TEST_P(HdmiCecTest, ClearAddLogicalAddress) {
109 hdmiCec->clearLogicalAddress();
110 Return<Result> ret = hdmiCec->addLogicalAddress(CecLogicalAddress::PLAYBACK_3);
111 EXPECT_EQ(ret, Result::SUCCESS);
112 }
113
TEST_P(HdmiCecTest,PhysicalAddress)114 TEST_P(HdmiCecTest, PhysicalAddress) {
115 Result result;
116 uint16_t addr;
117 Return<void> ret = hdmiCec->getPhysicalAddress([&result, &addr](Result res, uint16_t paddr) {
118 result = res;
119 addr = paddr;
120 });
121 EXPECT_TRUE(ret.isOk());
122 EXPECT_EQ(result, Result::SUCCESS);
123 if (!hasDeviceType(CecDeviceType::TV)) {
124 EXPECT_NE(addr, TV_PHYSICAL_ADDRESS);
125 }
126 }
127
TEST_P(HdmiCecTest,SendMessage)128 TEST_P(HdmiCecTest, SendMessage) {
129 CecMessage message;
130 if (hasDeviceType(CecDeviceType::TV))
131 {
132 hdmiCec->clearLogicalAddress();
133 Return<Result> result = hdmiCec->addLogicalAddress(CecLogicalAddress::TV);
134 EXPECT_EQ(result, Result::SUCCESS);
135 message.initiator = CecLogicalAddress::TV;
136 }
137 else
138 message.initiator = CecLogicalAddress::PLAYBACK_1;
139 message.destination = CecLogicalAddress::BROADCAST;
140 message.body.resize(1);
141 message.body[0] = 131;
142 SendMessageResult ret = hdmiCec->sendMessage(message);
143 EXPECT_EQ(ret, SendMessageResult::SUCCESS);
144 }
145
TEST_P(HdmiCecTest,CecVersion)146 TEST_P(HdmiCecTest, CecVersion) {
147 Return<int32_t> ret = hdmiCec->getCecVersion();
148 EXPECT_GE(ret, CEC_VERSION);
149 }
150
TEST_P(HdmiCecTest,VendorId)151 TEST_P(HdmiCecTest, VendorId) {
152 Return<uint32_t> ret = hdmiCec->getVendorId();
153 EXPECT_NE(ret, INCORRECT_VENDOR_ID);
154 }
155
TEST_P(HdmiCecTest,GetPortInfo)156 TEST_P(HdmiCecTest, GetPortInfo) {
157 hidl_vec<HdmiPortInfo> ports;
158 Return<void> ret =
159 hdmiCec->getPortInfo([&ports](hidl_vec<HdmiPortInfo> list) { ports = list; });
160 EXPECT_TRUE(ret.isOk());
161 bool cecSupportedOnDevice = false;
162 for (size_t i = 0; i < ports.size(); ++i) {
163 EXPECT_TRUE((ports[i].type == HdmiPortType::OUTPUT) ||
164 (ports[i].type == HdmiPortType::INPUT));
165 if (ports[i].portId == 0) {
166 ALOGW("%s: Port id should start from 1", __func__);
167 }
168 cecSupportedOnDevice = cecSupportedOnDevice | ports[i].cecSupported;
169 }
170 EXPECT_NE(cecSupportedOnDevice, false) << "At least one port should support CEC";
171 }
172
TEST_P(HdmiCecTest,SetOption)173 TEST_P(HdmiCecTest, SetOption) {
174 Return<void> ret;
175 ret = hdmiCec->setOption(OptionKey::WAKEUP, false);
176 EXPECT_TRUE(ret.isOk());
177 ret = hdmiCec->setOption(OptionKey::ENABLE_CEC, false);
178 EXPECT_TRUE(ret.isOk());
179 ret = hdmiCec->setOption(OptionKey::SYSTEM_CEC_CONTROL, true);
180 EXPECT_TRUE(ret.isOk());
181 // Restore option keys to their default values
182 ret = hdmiCec->setOption(OptionKey::WAKEUP, true);
183 EXPECT_TRUE(ret.isOk());
184 ret = hdmiCec->setOption(OptionKey::ENABLE_CEC, true);
185 EXPECT_TRUE(ret.isOk());
186 ret = hdmiCec->setOption(OptionKey::SYSTEM_CEC_CONTROL, false);
187 EXPECT_TRUE(ret.isOk());
188 }
189
TEST_P(HdmiCecTest,SetLanguage)190 TEST_P(HdmiCecTest, SetLanguage) {
191 Return<void> ret = hdmiCec->setLanguage("eng");
192 EXPECT_TRUE(ret.isOk());
193 }
194
TEST_P(HdmiCecTest,EnableAudioReturnChannel)195 TEST_P(HdmiCecTest, EnableAudioReturnChannel) {
196 hidl_vec<HdmiPortInfo> ports;
197 Return<void> ret =
198 hdmiCec->getPortInfo([&ports](hidl_vec<HdmiPortInfo> list) { ports = list; });
199 EXPECT_TRUE(ret.isOk());
200 for (size_t i = 0; i < ports.size(); ++i) {
201 if (ports[i].arcSupported) {
202 ret = hdmiCec->enableAudioReturnChannel(ports[i].portId, true);
203 EXPECT_TRUE(ret.isOk());
204 }
205 }
206 }
207
TEST_P(HdmiCecTest,IsConnected)208 TEST_P(HdmiCecTest, IsConnected) {
209 hidl_vec<HdmiPortInfo> ports;
210 Return<void> ret =
211 hdmiCec->getPortInfo([&ports](hidl_vec<HdmiPortInfo> list) { ports = list; });
212 EXPECT_TRUE(ret.isOk());
213 for (size_t i = 0; i < ports.size(); ++i) {
214 Return<bool> ret = hdmiCec->isConnected(ports[i].portId);
215 EXPECT_TRUE(ret.isOk());
216 }
217 }
218