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 #define LOG_TAG "android.hardware.tv.tuner@1.1-Tuner"
18 
19 #include "Tuner.h"
20 #include <utils/Log.h>
21 #include "Demux.h"
22 #include "Descrambler.h"
23 #include "Frontend.h"
24 #include "Lnb.h"
25 
26 namespace android {
27 namespace hardware {
28 namespace tv {
29 namespace tuner {
30 namespace V1_0 {
31 namespace implementation {
32 
Tuner()33 Tuner::Tuner() {
34     // Static Frontends array to maintain local frontends information
35     // Array index matches their FrontendId in the default impl
36     mFrontendSize = 10;
37     mFrontends[0] = new Frontend(FrontendType::ISDBS, 0, this);
38     mFrontends[1] = new Frontend(FrontendType::ATSC3, 1, this);
39     mFrontends[2] = new Frontend(FrontendType::DVBC, 2, this);
40     mFrontends[3] = new Frontend(FrontendType::DVBS, 3, this);
41     mFrontends[4] = new Frontend(FrontendType::DVBT, 4, this);
42     mFrontends[5] = new Frontend(FrontendType::ISDBT, 5, this);
43     mFrontends[6] = new Frontend(FrontendType::ANALOG, 6, this);
44     mFrontends[7] = new Frontend(FrontendType::ATSC, 7, this);
45     mFrontends[8] = new Frontend(FrontendType::ISDBS3, 8, this);
46     mFrontends[9] =
47             new Frontend(static_cast<V1_0::FrontendType>(V1_1::FrontendType::DTMB), 9, this);
48 
49     FrontendInfo::FrontendCapabilities caps;
50     vector<FrontendStatusType> statusCaps;
51 
52     caps = FrontendInfo::FrontendCapabilities();
53     caps.isdbsCaps(FrontendIsdbsCapabilities());
54     mFrontendCaps[0] = caps;
55     statusCaps = {
56             FrontendStatusType::DEMOD_LOCK,
57             FrontendStatusType::SNR,
58             FrontendStatusType::FEC,
59             FrontendStatusType::MODULATION,
60             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
61             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::ROLL_OFF),
62     };
63     mFrontendStatusCaps[0] = statusCaps;
64 
65     caps = FrontendInfo::FrontendCapabilities();
66     caps.atsc3Caps(FrontendAtsc3Capabilities());
67     mFrontendCaps[1] = caps;
68     statusCaps = {
69             FrontendStatusType::BER,
70             FrontendStatusType::PER,
71             FrontendStatusType::ATSC3_PLP_INFO,
72             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
73             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::BERS),
74             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::INTERLEAVINGS),
75             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::BANDWIDTH),
76     };
77     mFrontendStatusCaps[1] = statusCaps;
78 
79     caps = FrontendInfo::FrontendCapabilities();
80     caps.dvbcCaps(FrontendDvbcCapabilities());
81     mFrontendCaps[2] = caps;
82     statusCaps = {
83             FrontendStatusType::PRE_BER,
84             FrontendStatusType::SIGNAL_QUALITY,
85             FrontendStatusType::MODULATION,
86             FrontendStatusType::SPECTRAL,
87             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
88             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::CODERATES),
89             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::INTERLEAVINGS),
90             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::BANDWIDTH),
91     };
92     mFrontendStatusCaps[2] = statusCaps;
93 
94     caps = FrontendInfo::FrontendCapabilities();
95     caps.dvbsCaps(FrontendDvbsCapabilities());
96     mFrontendCaps[3] = caps;
97     statusCaps = {
98             FrontendStatusType::SIGNAL_STRENGTH,
99             FrontendStatusType::SYMBOL_RATE,
100             FrontendStatusType::MODULATION,
101             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
102             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::ROLL_OFF),
103             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::IS_MISO),
104     };
105     mFrontendStatusCaps[3] = statusCaps;
106 
107     caps = FrontendInfo::FrontendCapabilities();
108     caps.dvbtCaps(FrontendDvbtCapabilities());
109     mFrontendCaps[4] = caps;
110     statusCaps = {
111             FrontendStatusType::EWBS,
112             FrontendStatusType::PLP_ID,
113             FrontendStatusType::HIERARCHY,
114             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
115             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::BANDWIDTH),
116             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::GUARD_INTERVAL),
117             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::TRANSMISSION_MODE),
118             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::T2_SYSTEM_ID),
119     };
120     mFrontendStatusCaps[4] = statusCaps;
121 
122     caps = FrontendInfo::FrontendCapabilities();
123     FrontendIsdbtCapabilities isdbtCaps{
124             .modeCap = FrontendIsdbtMode::MODE_1 | FrontendIsdbtMode::MODE_2,
125             .bandwidthCap = (unsigned int)FrontendIsdbtBandwidth::BANDWIDTH_6MHZ,
126             .modulationCap = (unsigned int)FrontendIsdbtModulation::MOD_16QAM,
127             // ISDBT shares coderate and guard interval with DVBT
128             .coderateCap = FrontendDvbtCoderate::CODERATE_4_5 | FrontendDvbtCoderate::CODERATE_6_7,
129             .guardIntervalCap = (unsigned int)FrontendDvbtGuardInterval::INTERVAL_1_128,
130     };
131     caps.isdbtCaps(isdbtCaps);
132     mFrontendCaps[5] = caps;
133     statusCaps = {
134             FrontendStatusType::AGC,
135             FrontendStatusType::LNA,
136             FrontendStatusType::MODULATION,
137             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
138             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::BANDWIDTH),
139             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::GUARD_INTERVAL),
140             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::TRANSMISSION_MODE),
141             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::ISDBT_SEGMENTS),
142     };
143     mFrontendStatusCaps[5] = statusCaps;
144 
145     caps = FrontendInfo::FrontendCapabilities();
146     caps.analogCaps(FrontendAnalogCapabilities());
147     mFrontendCaps[6] = caps;
148     statusCaps = {
149             FrontendStatusType::LAYER_ERROR,
150             FrontendStatusType::MER,
151             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::UEC),
152             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::TS_DATA_RATES),
153     };
154     mFrontendStatusCaps[6] = statusCaps;
155 
156     caps = FrontendInfo::FrontendCapabilities();
157     caps.atscCaps(FrontendAtscCapabilities());
158     mFrontendCaps[7] = caps;
159     statusCaps = {
160             FrontendStatusType::FREQ_OFFSET,
161             FrontendStatusType::RF_LOCK,
162             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
163             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::IS_LINEAR),
164     };
165     mFrontendStatusCaps[7] = statusCaps;
166 
167     caps = FrontendInfo::FrontendCapabilities();
168     caps.isdbs3Caps(FrontendIsdbs3Capabilities());
169     mFrontendCaps[8] = caps;
170     statusCaps = {
171             FrontendStatusType::DEMOD_LOCK,
172             FrontendStatusType::MODULATION,
173             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
174             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::ROLL_OFF),
175             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::IS_SHORT_FRAMES),
176     };
177     mFrontendStatusCaps[8] = statusCaps;
178 
179     statusCaps = {
180             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::MODULATIONS),
181             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::INTERLEAVINGS),
182             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::BANDWIDTH),
183             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::GUARD_INTERVAL),
184             static_cast<FrontendStatusType>(V1_1::FrontendStatusTypeExt1_1::TRANSMISSION_MODE),
185     };
186     mFrontendStatusCaps[9] = statusCaps;
187 
188     mLnbs.resize(2);
189     mLnbs[0] = new Lnb(0);
190     mLnbs[1] = new Lnb(1);
191 }
192 
~Tuner()193 Tuner::~Tuner() {}
194 
getFrontendIds(getFrontendIds_cb _hidl_cb)195 Return<void> Tuner::getFrontendIds(getFrontendIds_cb _hidl_cb) {
196     ALOGV("%s", __FUNCTION__);
197 
198     vector<FrontendId> frontendIds;
199     frontendIds.resize(mFrontendSize);
200     for (int i = 0; i < mFrontendSize; i++) {
201         frontendIds[i] = mFrontends[i]->getFrontendId();
202     }
203 
204     _hidl_cb(Result::SUCCESS, frontendIds);
205     return Void();
206 }
207 
openFrontendById(uint32_t frontendId,openFrontendById_cb _hidl_cb)208 Return<void> Tuner::openFrontendById(uint32_t frontendId, openFrontendById_cb _hidl_cb) {
209     ALOGV("%s", __FUNCTION__);
210 
211     if (frontendId >= mFrontendSize || frontendId < 0) {
212         ALOGW("[   WARN   ] Frontend with id %d isn't available", frontendId);
213         _hidl_cb(Result::UNAVAILABLE, nullptr);
214         return Void();
215     }
216 
217     _hidl_cb(Result::SUCCESS, mFrontends[frontendId]);
218     return Void();
219 }
220 
openDemux(openDemux_cb _hidl_cb)221 Return<void> Tuner::openDemux(openDemux_cb _hidl_cb) {
222     ALOGV("%s", __FUNCTION__);
223 
224     uint32_t demuxId = mLastUsedId + 1;
225     mLastUsedId += 1;
226     sp<Demux> demux = new Demux(demuxId, this);
227     mDemuxes[demuxId] = demux;
228 
229     _hidl_cb(Result::SUCCESS, demuxId, demux);
230     return Void();
231 }
232 
getDemuxCaps(getDemuxCaps_cb _hidl_cb)233 Return<void> Tuner::getDemuxCaps(getDemuxCaps_cb _hidl_cb) {
234     ALOGV("%s", __FUNCTION__);
235 
236     DemuxCapabilities caps;
237 
238     // IP filter can be an MMTP filter's data source.
239     caps.linkCaps = {0x00, 0x00, 0x02, 0x00, 0x00};
240     // Support time filter testing
241     caps.bTimeFilter = true;
242     _hidl_cb(Result::SUCCESS, caps);
243     return Void();
244 }
245 
openDescrambler(openDescrambler_cb _hidl_cb)246 Return<void> Tuner::openDescrambler(openDescrambler_cb _hidl_cb) {
247     ALOGV("%s", __FUNCTION__);
248 
249     sp<V1_0::IDescrambler> descrambler = new Descrambler();
250 
251     _hidl_cb(Result::SUCCESS, descrambler);
252     return Void();
253 }
254 
getFrontendInfo(FrontendId frontendId,getFrontendInfo_cb _hidl_cb)255 Return<void> Tuner::getFrontendInfo(FrontendId frontendId, getFrontendInfo_cb _hidl_cb) {
256     ALOGV("%s", __FUNCTION__);
257 
258     FrontendInfo info;
259     if (frontendId >= mFrontendSize) {
260         _hidl_cb(Result::INVALID_ARGUMENT, info);
261         return Void();
262     }
263 
264     // assign randomly selected values for testing.
265     info = {
266             .type = mFrontends[frontendId]->getFrontendType(),
267             .minFrequency = 139,
268             .maxFrequency = 1139,
269             .minSymbolRate = 45,
270             .maxSymbolRate = 1145,
271             .acquireRange = 30,
272             .exclusiveGroupId = 57,
273             .statusCaps = mFrontendStatusCaps[frontendId],
274             .frontendCaps = mFrontendCaps[frontendId],
275     };
276 
277     _hidl_cb(Result::SUCCESS, info);
278     return Void();
279 }
280 
getLnbIds(getLnbIds_cb _hidl_cb)281 Return<void> Tuner::getLnbIds(getLnbIds_cb _hidl_cb) {
282     ALOGV("%s", __FUNCTION__);
283 
284     vector<V1_0::LnbId> lnbIds;
285     lnbIds.resize(mLnbs.size());
286     for (int i = 0; i < lnbIds.size(); i++) {
287         lnbIds[i] = mLnbs[i]->getId();
288     }
289 
290     _hidl_cb(Result::SUCCESS, lnbIds);
291     return Void();
292 }
293 
openLnbById(V1_0::LnbId lnbId,openLnbById_cb _hidl_cb)294 Return<void> Tuner::openLnbById(V1_0::LnbId lnbId, openLnbById_cb _hidl_cb) {
295     ALOGV("%s", __FUNCTION__);
296 
297     if (lnbId >= mLnbs.size()) {
298         _hidl_cb(Result::INVALID_ARGUMENT, nullptr);
299         return Void();
300     }
301 
302     _hidl_cb(Result::SUCCESS, mLnbs[lnbId]);
303     return Void();
304 }
305 
getFrontendById(uint32_t frontendId)306 sp<Frontend> Tuner::getFrontendById(uint32_t frontendId) {
307     ALOGV("%s", __FUNCTION__);
308 
309     return mFrontends[frontendId];
310 }
311 
openLnbByName(const hidl_string &,openLnbByName_cb _hidl_cb)312 Return<void> Tuner::openLnbByName(const hidl_string& /*lnbName*/, openLnbByName_cb _hidl_cb) {
313     ALOGV("%s", __FUNCTION__);
314 
315     sp<V1_0::ILnb> lnb = new Lnb();
316 
317     _hidl_cb(Result::SUCCESS, 1234, lnb);
318     return Void();
319 }
320 
getFrontendDtmbCapabilities(uint32_t frontendId,getFrontendDtmbCapabilities_cb _hidl_cb)321 Return<void> Tuner::getFrontendDtmbCapabilities(uint32_t frontendId,
322                                                 getFrontendDtmbCapabilities_cb _hidl_cb) {
323     ALOGV("%s", __FUNCTION__);
324 
325     if (mFrontends[frontendId] != nullptr &&
326         (mFrontends[frontendId]->getFrontendType() ==
327          static_cast<V1_0::FrontendType>(V1_1::FrontendType::DTMB))) {
328         _hidl_cb(Result::SUCCESS, mDtmbCaps);
329     } else {
330         _hidl_cb(Result::UNAVAILABLE, mDtmbCaps);
331     }
332     return Void();
333 }
334 
setFrontendAsDemuxSource(uint32_t frontendId,uint32_t demuxId)335 void Tuner::setFrontendAsDemuxSource(uint32_t frontendId, uint32_t demuxId) {
336     mFrontendToDemux[frontendId] = demuxId;
337     if (mFrontends[frontendId] != nullptr && mFrontends[frontendId]->isLocked()) {
338         mDemuxes[demuxId]->startFrontendInputLoop();
339     }
340 }
341 
removeDemux(uint32_t demuxId)342 void Tuner::removeDemux(uint32_t demuxId) {
343     map<uint32_t, uint32_t>::iterator it;
344     for (it = mFrontendToDemux.begin(); it != mFrontendToDemux.end(); it++) {
345         if (it->second == demuxId) {
346             it = mFrontendToDemux.erase(it);
347             break;
348         }
349     }
350     mDemuxes.erase(demuxId);
351 }
352 
removeFrontend(uint32_t frontendId)353 void Tuner::removeFrontend(uint32_t frontendId) {
354     mFrontendToDemux.erase(frontendId);
355 }
356 
frontendStopTune(uint32_t frontendId)357 void Tuner::frontendStopTune(uint32_t frontendId) {
358     map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
359     uint32_t demuxId;
360     if (it != mFrontendToDemux.end()) {
361         demuxId = it->second;
362         mDemuxes[demuxId]->stopFrontendInput();
363     }
364 }
365 
frontendStartTune(uint32_t frontendId)366 void Tuner::frontendStartTune(uint32_t frontendId) {
367     map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
368     uint32_t demuxId;
369     if (it != mFrontendToDemux.end()) {
370         demuxId = it->second;
371         mDemuxes[demuxId]->startFrontendInputLoop();
372     }
373 }
374 
375 }  // namespace implementation
376 }  // namespace V1_0
377 }  // namespace tuner
378 }  // namespace tv
379 }  // namespace hardware
380 }  // namespace android
381