1 /*
2  * Copyright 2020 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 #include <android-base/logging.h>
18 #include <android/hardware/tv/tuner/1.0/types.h>
19 #include <android/hardware/tv/tuner/1.1/IFrontend.h>
20 #include <android/hardware/tv/tuner/1.1/IFrontendCallback.h>
21 #include <android/hardware/tv/tuner/1.1/ITuner.h>
22 #include <binder/MemoryDealer.h>
23 #include <gtest/gtest.h>
24 #include <hidl/GtestPrinter.h>
25 #include <hidl/HidlSupport.h>
26 #include <hidl/HidlTransportSupport.h>
27 #include <hidl/ServiceManagement.h>
28 #include <hidl/Status.h>
29 #include <hidlmemory/FrameworkUtils.h>
30 #include <utils/Condition.h>
31 #include <utils/Mutex.h>
32 #include <map>
33 
34 #include "DvrTests.h"
35 #include "VtsHalTvTunerV1_1TestConfigurations.h"
36 
37 #define WAIT_TIMEOUT 3000000000
38 #define INVALID_ID -1
39 
40 using android::Condition;
41 using android::IMemory;
42 using android::IMemoryHeap;
43 using android::MemoryDealer;
44 using android::Mutex;
45 using android::sp;
46 using android::hardware::fromHeap;
47 using android::hardware::hidl_vec;
48 using android::hardware::Return;
49 using android::hardware::Void;
50 using android::hardware::tv::tuner::V1_0::FrontendEventType;
51 using android::hardware::tv::tuner::V1_0::FrontendId;
52 using android::hardware::tv::tuner::V1_0::FrontendInfo;
53 using android::hardware::tv::tuner::V1_0::FrontendScanMessage;
54 using android::hardware::tv::tuner::V1_0::FrontendScanMessageType;
55 using android::hardware::tv::tuner::V1_0::FrontendScanType;
56 using android::hardware::tv::tuner::V1_0::IFrontend;
57 using android::hardware::tv::tuner::V1_0::Result;
58 using android::hardware::tv::tuner::V1_1::FrontendDtmbCapabilities;
59 using android::hardware::tv::tuner::V1_1::FrontendModulation;
60 using android::hardware::tv::tuner::V1_1::FrontendScanMessageExt1_1;
61 using android::hardware::tv::tuner::V1_1::FrontendScanMessageTypeExt1_1;
62 using android::hardware::tv::tuner::V1_1::IFrontendCallback;
63 using android::hardware::tv::tuner::V1_1::ITuner;
64 
65 using ::testing::AssertionResult;
66 
67 using namespace std;
68 
69 #define INVALID_ID -1
70 #define WAIT_TIMEOUT 3000000000
71 
72 class FrontendCallback : public IFrontendCallback {
73   public:
74     virtual Return<void> onEvent(FrontendEventType frontendEventType) override;
75     virtual Return<void> onScanMessage(FrontendScanMessageType type,
76                                        const FrontendScanMessage& message) override;
77     virtual Return<void> onScanMessageExt1_1(FrontendScanMessageTypeExt1_1 type,
78                                              const FrontendScanMessageExt1_1& message) override;
79 
80     void tuneTestOnLock(sp<IFrontend>& frontend, FrontendSettings settings,
81                         FrontendSettingsExt1_1 settingsExt1_1);
82     void scanTest(sp<IFrontend>& frontend, FrontendConfig1_1 config, FrontendScanType type);
83 
84     // Helper methods
85     uint32_t getTargetFrequency(FrontendSettings settings);
86     void resetBlindScanStartingFrequency(FrontendConfig1_1& config, uint32_t resetingFreq);
87 
88   private:
89     void readFrontendScanMessageExt1_1Modulation(FrontendModulation modulation);
90 
91     bool mEventReceived = false;
92     bool mScanMessageReceived = false;
93     bool mLockMsgReceived = false;
94     bool mScanMsgProcessed = true;
95     FrontendScanMessageType mScanMessageType;
96     FrontendScanMessage mScanMessage;
97     hidl_vec<uint8_t> mEventMessage;
98     android::Mutex mMsgLock;
99     android::Condition mMsgCondition;
100     android::Condition mLockMsgCondition;
101 };
102 
103 class FrontendTests {
104   public:
105     sp<ITuner> mService;
106 
setService(sp<ITuner> tuner)107     void setService(sp<ITuner> tuner) {
108         mService = tuner;
109         getDvrTests()->setService(tuner);
110         getDefaultSoftwareFrontendPlaybackConfig(mDvrConfig);
111     }
112 
113     AssertionResult getFrontendIds();
114     AssertionResult getFrontendInfo(uint32_t frontendId);
115     AssertionResult openFrontendById(uint32_t frontendId);
116     AssertionResult setFrontendCallback();
117     AssertionResult scanFrontend(FrontendConfig1_1 config, FrontendScanType type);
118     AssertionResult stopScanFrontend();
119     AssertionResult tuneFrontend(FrontendConfig1_1 config, bool testWithDemux);
120     void verifyFrontendStatusExt1_1(vector<FrontendStatusTypeExt1_1> statusTypes,
121                                     vector<FrontendStatusExt1_1> expectStatuses);
122     AssertionResult stopTuneFrontend(bool testWithDemux);
123     AssertionResult closeFrontend();
124     AssertionResult getFrontendDtmbCaps(uint32_t);
125 
126     AssertionResult linkCiCam(uint32_t ciCamId);
127     AssertionResult unlinkCiCam(uint32_t ciCamId);
128 
129     void getFrontendIdByType(FrontendType feType, uint32_t& feId);
130     void tuneTest(FrontendConfig1_1 frontendConf);
131     void scanTest(FrontendConfig1_1 frontend, FrontendScanType type);
132     void getFrontendDtmbCapsTest();
133 
setDvrTests(DvrTests * dvrTests)134     void setDvrTests(DvrTests* dvrTests) { mExternalDvrTests = dvrTests; }
setDemux(sp<IDemux> demux)135     void setDemux(sp<IDemux> demux) { getDvrTests()->setDemux(demux); }
setSoftwareFrontendDvrConfig(DvrConfig conf)136     void setSoftwareFrontendDvrConfig(DvrConfig conf) { mDvrConfig = conf; }
137 
138   protected:
failure()139     static AssertionResult failure() { return ::testing::AssertionFailure(); }
success()140     static AssertionResult success() { return ::testing::AssertionSuccess(); }
141 
getDefaultSoftwareFrontendPlaybackConfig(DvrConfig & dvrConfig)142     void getDefaultSoftwareFrontendPlaybackConfig(DvrConfig& dvrConfig) {
143         PlaybackSettings playbackSettings{
144                 .statusMask = 0xf,
145                 .lowThreshold = 0x1000,
146                 .highThreshold = 0x07fff,
147                 .dataFormat = DataFormat::ES,
148                 .packetSize = 188,
149         };
150         dvrConfig.type = DvrType::PLAYBACK;
151         dvrConfig.playbackInputFile = "/data/local/tmp/test.es";
152         dvrConfig.bufferSize = FMQ_SIZE_4M;
153         dvrConfig.settings.playback(playbackSettings);
154     }
155 
getDvrTests()156     DvrTests* getDvrTests() {
157         return (mExternalDvrTests != nullptr ? mExternalDvrTests : &mDvrTests);
158     }
159 
160     sp<IFrontend> mFrontend;
161     FrontendInfo mFrontendInfo;
162     sp<FrontendCallback> mFrontendCallback;
163     hidl_vec<FrontendId> mFeIds;
164 
165     DvrTests* mExternalDvrTests = nullptr;
166     DvrTests mDvrTests;
167     bool mIsSoftwareFe = false;
168     DvrConfig mDvrConfig;
169 };
170