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 "HdmiCec_hal_test"
18
19 #include <aidl/Gtest.h>
20 #include <aidl/Vintf.h>
21 #include <aidl/android/hardware/tv/hdmi/cec/BnHdmiCec.h>
22 #include <aidl/android/hardware/tv/hdmi/cec/BnHdmiCecCallback.h>
23 #include <aidl/android/hardware/tv/hdmi/cec/CecDeviceType.h>
24 #include <aidl/android/hardware/tv/hdmi/connection/BnHdmiConnection.h>
25 #include <android-base/logging.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28 #include <gtest/gtest.h>
29 #include <log/log.h>
30 #include <sstream>
31 #include <vector>
32
33 using ::aidl::android::hardware::tv::hdmi::cec::BnHdmiCecCallback;
34 using ::aidl::android::hardware::tv::hdmi::cec::CecDeviceType;
35 using ::aidl::android::hardware::tv::hdmi::cec::CecLogicalAddress;
36 using ::aidl::android::hardware::tv::hdmi::cec::CecMessage;
37 using ::aidl::android::hardware::tv::hdmi::cec::IHdmiCec;
38 using ::aidl::android::hardware::tv::hdmi::cec::IHdmiCecCallback;
39 using ::aidl::android::hardware::tv::hdmi::cec::Result;
40 using ::aidl::android::hardware::tv::hdmi::cec::SendMessageResult;
41 using ::aidl::android::hardware::tv::hdmi::connection::HdmiPortInfo;
42 using ::ndk::SpAIBinder;
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> {
serviceDied(void *)50 static void serviceDied(void* /* cookie */) { ALOGE("VtsHalTvCecAidlTargetTest died"); }
51
52 public:
SetUp()53 void SetUp() override {
54 hdmiCec = IHdmiCec::fromBinder(
55 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
56 ASSERT_NE(hdmiCec, nullptr);
57 ALOGI("%s: getService() for hdmiCec is %s", __func__,
58 hdmiCec->isRemote() ? "remote" : "local");
59
60 hdmiCecCallback = ::ndk::SharedRefBase::make<CecCallback>();
61 ASSERT_NE(hdmiCecCallback, nullptr);
62 hdmiCecDeathRecipient =
63 ndk::ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient_new(&serviceDied));
64 ASSERT_EQ(AIBinder_linkToDeath(hdmiCec->asBinder().get(), hdmiCecDeathRecipient.get(), 0),
65 STATUS_OK);
66 }
67
getDeviceTypes()68 std::vector<int> getDeviceTypes() {
69 std::vector<int> deviceTypes;
70 FILE* p = popen("getprop ro.hdmi.device_type", "re");
71 if (p) {
72 char* line = NULL;
73 size_t len = 0;
74 if (getline(&line, &len, p) > 0) {
75 std::istringstream stream(line);
76 std::string number{};
77 while (std::getline(stream, number, ',')) {
78 deviceTypes.push_back(stoi(number));
79 }
80 }
81 pclose(p);
82 }
83 return deviceTypes;
84 }
85
hasDeviceType(CecDeviceType type)86 bool hasDeviceType(CecDeviceType type) {
87 std::vector<int> deviceTypes = getDeviceTypes();
88 return std::find(deviceTypes.begin(), deviceTypes.end(), (int)type) != deviceTypes.end();
89 }
90
91 class CecCallback : public BnHdmiCecCallback {
92 public:
onCecMessage(const CecMessage & message __unused)93 ::ndk::ScopedAStatus onCecMessage(const CecMessage& message __unused) {
94 return ::ndk::ScopedAStatus::ok();
95 };
96 };
97
98 std::shared_ptr<IHdmiCec> hdmiCec;
99 std::shared_ptr<IHdmiCecCallback> hdmiCecCallback;
100 ::ndk::ScopedAIBinder_DeathRecipient hdmiCecDeathRecipient;
101 };
102
103 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(HdmiCecTest);
104 INSTANTIATE_TEST_SUITE_P(PerInstance, HdmiCecTest,
105 testing::ValuesIn(android::getAidlHalInstanceNames(IHdmiCec::descriptor)),
106 android::PrintInstanceNameToString);
107
TEST_P(HdmiCecTest,ClearAddLogicalAddress)108 TEST_P(HdmiCecTest, ClearAddLogicalAddress) {
109 Result addLaResult;
110 ASSERT_TRUE(hdmiCec->clearLogicalAddress().isOk());
111 ASSERT_TRUE(hdmiCec->addLogicalAddress(CecLogicalAddress::PLAYBACK_3, &addLaResult).isOk());
112 EXPECT_EQ(addLaResult, Result::SUCCESS);
113 }
114
TEST_P(HdmiCecTest,PhysicalAddress)115 TEST_P(HdmiCecTest, PhysicalAddress) {
116 int32_t addr;
117 ASSERT_TRUE(hdmiCec->getPhysicalAddress(&addr).isOk());
118 if (!hasDeviceType(CecDeviceType::TV)) {
119 EXPECT_NE(addr, TV_PHYSICAL_ADDRESS);
120 }
121 }
122
TEST_P(HdmiCecTest,SendMessage)123 TEST_P(HdmiCecTest, SendMessage) {
124 CecMessage message;
125 message.initiator = CecLogicalAddress::PLAYBACK_1;
126 message.destination = CecLogicalAddress::BROADCAST;
127 message.body.resize(1);
128 message.body[0] = 131;
129 SendMessageResult result;
130 ASSERT_TRUE(hdmiCec->sendMessage(message, &result).isOk());
131 EXPECT_EQ(result, SendMessageResult::SUCCESS);
132 }
133
TEST_P(HdmiCecTest,CecVersion)134 TEST_P(HdmiCecTest, CecVersion) {
135 int32_t version;
136 ASSERT_TRUE(hdmiCec->getCecVersion(&version).isOk());
137 EXPECT_GE(version, CEC_VERSION);
138 }
139
TEST_P(HdmiCecTest,SetCallback)140 TEST_P(HdmiCecTest, SetCallback) {
141 ASSERT_TRUE(hdmiCec->setCallback(::ndk::SharedRefBase::make<CecCallback>()).isOk());
142 }
143
TEST_P(HdmiCecTest,VendorId)144 TEST_P(HdmiCecTest, VendorId) {
145 int32_t vendorId;
146 ASSERT_TRUE(hdmiCec->getVendorId(&vendorId).isOk());
147 EXPECT_NE(vendorId, INCORRECT_VENDOR_ID);
148 }
149
TEST_P(HdmiCecTest,EnableWakeupByOtp)150 TEST_P(HdmiCecTest, EnableWakeupByOtp) {
151 ASSERT_TRUE(hdmiCec->enableWakeupByOtp(false).isOk());
152 // Restore option to its default value
153 ASSERT_TRUE(hdmiCec->enableWakeupByOtp(true).isOk());
154 }
155
TEST_P(HdmiCecTest,EnableCec)156 TEST_P(HdmiCecTest, EnableCec) {
157 ASSERT_TRUE(hdmiCec->enableCec(false).isOk());
158 // Restore option to its default value
159 ASSERT_TRUE(hdmiCec->enableCec(true).isOk());
160 }
161
TEST_P(HdmiCecTest,EnableSystemCecControl)162 TEST_P(HdmiCecTest, EnableSystemCecControl) {
163 ASSERT_TRUE(hdmiCec->enableSystemCecControl(true).isOk());
164 // Restore option to its default value
165 ASSERT_TRUE(hdmiCec->enableSystemCecControl(false).isOk());
166 }
167
TEST_P(HdmiCecTest,SetLanguage)168 TEST_P(HdmiCecTest, SetLanguage) {
169 ASSERT_TRUE(hdmiCec->setLanguage("eng").isOk());
170 }
171