1 /*
2  * Copyright (C) 2019 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 "[email protected]"
18 
19 #include "Tuner.h"
20 #include <android/hardware/tv/tuner/1.0/IFrontendCallback.h>
21 #include <utils/Log.h>
22 #include "Demux.h"
23 #include "Descrambler.h"
24 #include "Frontend.h"
25 #include "Lnb.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace tv {
30 namespace tuner {
31 namespace V1_0 {
32 namespace implementation {
33 
34 using ::android::hardware::tv::tuner::V1_0::DemuxId;
35 
Tuner()36 Tuner::Tuner() {
37     // Static Frontends array to maintain local frontends information
38     // Array index matches their FrontendId in the default impl
39     mFrontendSize = 8;
40     mFrontends.resize(mFrontendSize);
41     mFrontends[0] = new Frontend(FrontendType::DVBT, 0, this);
42     mFrontends[1] = new Frontend(FrontendType::ATSC, 1, this);
43     mFrontends[2] = new Frontend(FrontendType::DVBC, 2, this);
44     mFrontends[3] = new Frontend(FrontendType::DVBS, 3, this);
45     mFrontends[4] = new Frontend(FrontendType::DVBT, 4, this);
46     mFrontends[5] = new Frontend(FrontendType::ISDBT, 5, this);
47     mFrontends[6] = new Frontend(FrontendType::ANALOG, 6, this);
48     mFrontends[7] = new Frontend(FrontendType::ATSC, 7, this);
49 
50     FrontendInfo::FrontendCapabilities caps;
51     mFrontendCaps.resize(mFrontendSize);
52     caps = FrontendInfo::FrontendCapabilities();
53     caps.dvbtCaps(FrontendDvbtCapabilities());
54     mFrontendCaps[0] = caps;
55 
56     caps = FrontendInfo::FrontendCapabilities();
57     caps.atscCaps(FrontendAtscCapabilities());
58     mFrontendCaps[1] = caps;
59 
60     caps = FrontendInfo::FrontendCapabilities();
61     caps.dvbcCaps(FrontendDvbcCapabilities());
62     mFrontendCaps[2] = caps;
63 
64     caps = FrontendInfo::FrontendCapabilities();
65     caps.dvbsCaps(FrontendDvbsCapabilities());
66     mFrontendCaps[3] = caps;
67 
68     caps = FrontendInfo::FrontendCapabilities();
69     caps.dvbtCaps(FrontendDvbtCapabilities());
70     mFrontendCaps[4] = caps;
71 
72     caps = FrontendInfo::FrontendCapabilities();
73     FrontendIsdbtCapabilities isdbtCaps{
74             .modeCap = FrontendIsdbtMode::MODE_1 | FrontendIsdbtMode::MODE_2,
75             .bandwidthCap = (unsigned int)FrontendIsdbtBandwidth::BANDWIDTH_6MHZ,
76             .modulationCap = (unsigned int)FrontendIsdbtModulation::MOD_16QAM,
77             // ISDBT shares coderate and guard interval with DVBT
78             .coderateCap = FrontendDvbtCoderate::CODERATE_4_5 | FrontendDvbtCoderate::CODERATE_6_7,
79             .guardIntervalCap = (unsigned int)FrontendDvbtGuardInterval::INTERVAL_1_128,
80     };
81     caps.isdbtCaps(isdbtCaps);
82     mFrontendCaps[5] = caps;
83 
84     caps = FrontendInfo::FrontendCapabilities();
85     caps.analogCaps(FrontendAnalogCapabilities());
86     mFrontendCaps[6] = caps;
87 
88     caps = FrontendInfo::FrontendCapabilities();
89     caps.atscCaps(FrontendAtscCapabilities());
90     mFrontendCaps[7] = caps;
91 
92     mLnbs.resize(2);
93     mLnbs[0] = new Lnb(0);
94     mLnbs[1] = new Lnb(1);
95 }
96 
~Tuner()97 Tuner::~Tuner() {}
98 
getFrontendIds(getFrontendIds_cb _hidl_cb)99 Return<void> Tuner::getFrontendIds(getFrontendIds_cb _hidl_cb) {
100     ALOGV("%s", __FUNCTION__);
101 
102     vector<FrontendId> frontendIds;
103     frontendIds.resize(mFrontendSize);
104     for (int i = 0; i < mFrontendSize; i++) {
105         frontendIds[i] = mFrontends[i]->getFrontendId();
106     }
107 
108     _hidl_cb(Result::SUCCESS, frontendIds);
109     return Void();
110 }
111 
openFrontendById(uint32_t frontendId,openFrontendById_cb _hidl_cb)112 Return<void> Tuner::openFrontendById(uint32_t frontendId, openFrontendById_cb _hidl_cb) {
113     ALOGV("%s", __FUNCTION__);
114 
115     if (frontendId >= mFrontendSize || frontendId < 0) {
116         ALOGW("[   WARN   ] Frontend with id %d isn't available", frontendId);
117         _hidl_cb(Result::UNAVAILABLE, nullptr);
118         return Void();
119     }
120 
121     _hidl_cb(Result::SUCCESS, mFrontends[frontendId]);
122     return Void();
123 }
124 
openDemux(openDemux_cb _hidl_cb)125 Return<void> Tuner::openDemux(openDemux_cb _hidl_cb) {
126     ALOGV("%s", __FUNCTION__);
127 
128     DemuxId demuxId = mLastUsedId + 1;
129     mLastUsedId += 1;
130     sp<Demux> demux = new Demux(demuxId, this);
131     mDemuxes[demuxId] = demux;
132 
133     _hidl_cb(Result::SUCCESS, demuxId, demux);
134     return Void();
135 }
136 
getDemuxCaps(getDemuxCaps_cb _hidl_cb)137 Return<void> Tuner::getDemuxCaps(getDemuxCaps_cb _hidl_cb) {
138     ALOGV("%s", __FUNCTION__);
139 
140     DemuxCapabilities caps;
141 
142     // IP filter can be an MMTP filter's data source.
143     caps.linkCaps = {0x00, 0x00, 0x02, 0x00, 0x00};
144     _hidl_cb(Result::SUCCESS, caps);
145     return Void();
146 }
147 
openDescrambler(openDescrambler_cb _hidl_cb)148 Return<void> Tuner::openDescrambler(openDescrambler_cb _hidl_cb) {
149     ALOGV("%s", __FUNCTION__);
150 
151     sp<IDescrambler> descrambler = new Descrambler();
152 
153     _hidl_cb(Result::SUCCESS, descrambler);
154     return Void();
155 }
156 
getFrontendInfo(FrontendId frontendId,getFrontendInfo_cb _hidl_cb)157 Return<void> Tuner::getFrontendInfo(FrontendId frontendId, getFrontendInfo_cb _hidl_cb) {
158     ALOGV("%s", __FUNCTION__);
159 
160     FrontendInfo info;
161     if (frontendId >= mFrontendSize) {
162         _hidl_cb(Result::INVALID_ARGUMENT, info);
163         return Void();
164     }
165 
166     vector<FrontendStatusType> statusCaps = {
167             FrontendStatusType::DEMOD_LOCK,
168             FrontendStatusType::SNR,
169             FrontendStatusType::FEC,
170             FrontendStatusType::MODULATION,
171             FrontendStatusType::PLP_ID,
172             FrontendStatusType::LAYER_ERROR,
173             FrontendStatusType::ATSC3_PLP_INFO,
174     };
175     // assign randomly selected values for testing.
176     info = {
177             .type = mFrontends[frontendId]->getFrontendType(),
178             .minFrequency = 139,
179             .maxFrequency = 1139,
180             .minSymbolRate = 45,
181             .maxSymbolRate = 1145,
182             .acquireRange = 30,
183             .exclusiveGroupId = 57,
184             .statusCaps = statusCaps,
185             .frontendCaps = mFrontendCaps[frontendId],
186     };
187 
188     _hidl_cb(Result::SUCCESS, info);
189     return Void();
190 }
191 
getLnbIds(getLnbIds_cb _hidl_cb)192 Return<void> Tuner::getLnbIds(getLnbIds_cb _hidl_cb) {
193     ALOGV("%s", __FUNCTION__);
194 
195     vector<LnbId> lnbIds;
196     lnbIds.resize(mLnbs.size());
197     for (int i = 0; i < lnbIds.size(); i++) {
198         lnbIds[i] = mLnbs[i]->getId();
199     }
200 
201     _hidl_cb(Result::SUCCESS, lnbIds);
202     return Void();
203 }
204 
openLnbById(LnbId lnbId,openLnbById_cb _hidl_cb)205 Return<void> Tuner::openLnbById(LnbId lnbId, openLnbById_cb _hidl_cb) {
206     ALOGV("%s", __FUNCTION__);
207 
208     if (lnbId >= mLnbs.size()) {
209         _hidl_cb(Result::INVALID_ARGUMENT, nullptr);
210         return Void();
211     }
212 
213     _hidl_cb(Result::SUCCESS, mLnbs[lnbId]);
214     return Void();
215 }
216 
getFrontendById(uint32_t frontendId)217 sp<Frontend> Tuner::getFrontendById(uint32_t frontendId) {
218     ALOGV("%s", __FUNCTION__);
219 
220     return mFrontends[frontendId];
221 }
222 
openLnbByName(const hidl_string &,openLnbByName_cb _hidl_cb)223 Return<void> Tuner::openLnbByName(const hidl_string& /*lnbName*/, openLnbByName_cb _hidl_cb) {
224     ALOGV("%s", __FUNCTION__);
225 
226     sp<ILnb> lnb = new Lnb();
227 
228     _hidl_cb(Result::SUCCESS, 1234, lnb);
229     return Void();
230 }
231 
setFrontendAsDemuxSource(uint32_t frontendId,uint32_t demuxId)232 void Tuner::setFrontendAsDemuxSource(uint32_t frontendId, uint32_t demuxId) {
233     mFrontendToDemux[frontendId] = demuxId;
234     if (mFrontends[frontendId] != nullptr && mFrontends[frontendId]->isLocked()) {
235         mDemuxes[demuxId]->startFrontendInputLoop();
236     }
237 }
238 
frontendStopTune(uint32_t frontendId)239 void Tuner::frontendStopTune(uint32_t frontendId) {
240     map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
241     uint32_t demuxId;
242     if (it != mFrontendToDemux.end()) {
243         demuxId = it->second;
244         mDemuxes[demuxId]->stopFrontendInput();
245     }
246 }
247 
frontendStartTune(uint32_t frontendId)248 void Tuner::frontendStartTune(uint32_t frontendId) {
249     map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
250     uint32_t demuxId;
251     if (it != mFrontendToDemux.end()) {
252         demuxId = it->second;
253         mDemuxes[demuxId]->startFrontendInputLoop();
254     }
255 }
256 
257 }  // namespace implementation
258 }  // namespace V1_0
259 }  // namespace tuner
260 }  // namespace tv
261 }  // namespace hardware
262 }  // namespace android
263