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