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 "android.hardware.tv.tuner@1.0-Tuner"
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[0] = new Frontend(FrontendType::DVBT, 0, this);
41 mFrontends[1] = new Frontend(FrontendType::ATSC, 1, this);
42 mFrontends[2] = new Frontend(FrontendType::DVBC, 2, this);
43 mFrontends[3] = new Frontend(FrontendType::DVBS, 3, this);
44 mFrontends[4] = new Frontend(FrontendType::DVBT, 4, this);
45 mFrontends[5] = new Frontend(FrontendType::ISDBT, 5, this);
46 mFrontends[6] = new Frontend(FrontendType::ANALOG, 6, this);
47 mFrontends[7] = new Frontend(FrontendType::ATSC, 7, this);
48
49 FrontendInfo::FrontendCapabilities caps;
50 caps = FrontendInfo::FrontendCapabilities();
51 caps.dvbtCaps(FrontendDvbtCapabilities());
52 mFrontendCaps[0] = caps;
53
54 caps = FrontendInfo::FrontendCapabilities();
55 caps.atscCaps(FrontendAtscCapabilities());
56 mFrontendCaps[1] = caps;
57
58 caps = FrontendInfo::FrontendCapabilities();
59 caps.dvbcCaps(FrontendDvbcCapabilities());
60 mFrontendCaps[2] = caps;
61
62 caps = FrontendInfo::FrontendCapabilities();
63 caps.dvbsCaps(FrontendDvbsCapabilities());
64 mFrontendCaps[3] = caps;
65
66 caps = FrontendInfo::FrontendCapabilities();
67 caps.dvbtCaps(FrontendDvbtCapabilities());
68 mFrontendCaps[4] = caps;
69
70 caps = FrontendInfo::FrontendCapabilities();
71 FrontendIsdbtCapabilities isdbtCaps{
72 .modeCap = FrontendIsdbtMode::MODE_1 | FrontendIsdbtMode::MODE_2,
73 .bandwidthCap = (unsigned int)FrontendIsdbtBandwidth::BANDWIDTH_6MHZ,
74 .modulationCap = (unsigned int)FrontendIsdbtModulation::MOD_16QAM,
75 // ISDBT shares coderate and guard interval with DVBT
76 .coderateCap = FrontendDvbtCoderate::CODERATE_4_5 | FrontendDvbtCoderate::CODERATE_6_7,
77 .guardIntervalCap = (unsigned int)FrontendDvbtGuardInterval::INTERVAL_1_128,
78 };
79 caps.isdbtCaps(isdbtCaps);
80 mFrontendCaps[5] = caps;
81
82 caps = FrontendInfo::FrontendCapabilities();
83 caps.analogCaps(FrontendAnalogCapabilities());
84 mFrontendCaps[6] = caps;
85
86 caps = FrontendInfo::FrontendCapabilities();
87 caps.atscCaps(FrontendAtscCapabilities());
88 mFrontendCaps[7] = caps;
89
90 mLnbs.resize(2);
91 mLnbs[0] = new Lnb(0);
92 mLnbs[1] = new Lnb(1);
93 }
94
~Tuner()95 Tuner::~Tuner() {}
96
getFrontendIds(getFrontendIds_cb _hidl_cb)97 Return<void> Tuner::getFrontendIds(getFrontendIds_cb _hidl_cb) {
98 ALOGV("%s", __FUNCTION__);
99
100 vector<FrontendId> frontendIds;
101 frontendIds.resize(mFrontendSize);
102 for (int i = 0; i < mFrontendSize; i++) {
103 frontendIds[i] = mFrontends[i]->getFrontendId();
104 }
105
106 _hidl_cb(Result::SUCCESS, frontendIds);
107 return Void();
108 }
109
openFrontendById(uint32_t frontendId,openFrontendById_cb _hidl_cb)110 Return<void> Tuner::openFrontendById(uint32_t frontendId, openFrontendById_cb _hidl_cb) {
111 ALOGV("%s", __FUNCTION__);
112
113 if (frontendId >= mFrontendSize || frontendId < 0) {
114 ALOGW("[ WARN ] Frontend with id %d isn't available", frontendId);
115 _hidl_cb(Result::UNAVAILABLE, nullptr);
116 return Void();
117 }
118
119 _hidl_cb(Result::SUCCESS, mFrontends[frontendId]);
120 return Void();
121 }
122
openDemux(openDemux_cb _hidl_cb)123 Return<void> Tuner::openDemux(openDemux_cb _hidl_cb) {
124 ALOGV("%s", __FUNCTION__);
125
126 DemuxId demuxId = mLastUsedId + 1;
127 mLastUsedId += 1;
128 sp<Demux> demux = new Demux(demuxId, this);
129 mDemuxes[demuxId] = demux;
130
131 _hidl_cb(Result::SUCCESS, demuxId, demux);
132 return Void();
133 }
134
getDemuxCaps(getDemuxCaps_cb _hidl_cb)135 Return<void> Tuner::getDemuxCaps(getDemuxCaps_cb _hidl_cb) {
136 ALOGV("%s", __FUNCTION__);
137
138 DemuxCapabilities caps;
139
140 // IP filter can be an MMTP filter's data source.
141 caps.linkCaps = {0x00, 0x00, 0x02, 0x00, 0x00};
142 // Support time filter testing
143 caps.bTimeFilter = true;
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 = 139000000,
179 .maxFrequency = 1139000000,
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
removeDemux(uint32_t demuxId)239 void Tuner::removeDemux(uint32_t demuxId) {
240 map<uint32_t, uint32_t>::iterator it;
241 for (it = mFrontendToDemux.begin(); it != mFrontendToDemux.end(); it++) {
242 if (it->second == demuxId) {
243 it = mFrontendToDemux.erase(it);
244 break;
245 }
246 }
247 mDemuxes.erase(demuxId);
248 }
249
removeFrontend(uint32_t frontendId)250 void Tuner::removeFrontend(uint32_t frontendId) {
251 mFrontendToDemux.erase(frontendId);
252 }
253
frontendStopTune(uint32_t frontendId)254 void Tuner::frontendStopTune(uint32_t frontendId) {
255 map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
256 uint32_t demuxId;
257 if (it != mFrontendToDemux.end()) {
258 demuxId = it->second;
259 mDemuxes[demuxId]->stopFrontendInput();
260 }
261 }
262
frontendStartTune(uint32_t frontendId)263 void Tuner::frontendStartTune(uint32_t frontendId) {
264 map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
265 uint32_t demuxId;
266 if (it != mFrontendToDemux.end()) {
267 demuxId = it->second;
268 mDemuxes[demuxId]->startFrontendInputLoop();
269 }
270 }
271
272 } // namespace implementation
273 } // namespace V1_0
274 } // namespace tuner
275 } // namespace tv
276 } // namespace hardware
277 } // namespace android
278