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.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 
~Tuner()51 Tuner::~Tuner() {}
52 
getFrontendIds(getFrontendIds_cb _hidl_cb)53 Return<void> Tuner::getFrontendIds(getFrontendIds_cb _hidl_cb) {
54     ALOGV("%s", __FUNCTION__);
55 
56     vector<FrontendId> frontendIds;
57     frontendIds.resize(mFrontendSize);
58     for (int i = 0; i < mFrontendSize; i++) {
59         frontendIds[i] = mFrontends[i]->getFrontendId();
60     }
61 
62     _hidl_cb(Result::SUCCESS, frontendIds);
63     return Void();
64 }
65 
openFrontendById(uint32_t frontendId,openFrontendById_cb _hidl_cb)66 Return<void> Tuner::openFrontendById(uint32_t frontendId, openFrontendById_cb _hidl_cb) {
67     ALOGV("%s", __FUNCTION__);
68 
69     if (frontendId >= mFrontendSize || frontendId < 0) {
70         ALOGW("[   WARN   ] Frontend with id %d isn't available", frontendId);
71         _hidl_cb(Result::UNAVAILABLE, nullptr);
72         return Void();
73     }
74 
75     _hidl_cb(Result::SUCCESS, mFrontends[frontendId]);
76     return Void();
77 }
78 
openDemux(openDemux_cb _hidl_cb)79 Return<void> Tuner::openDemux(openDemux_cb _hidl_cb) {
80     ALOGV("%s", __FUNCTION__);
81 
82     DemuxId demuxId = mLastUsedId + 1;
83     mLastUsedId += 1;
84     sp<Demux> demux = new Demux(demuxId, this);
85     mDemuxes[demuxId] = demux;
86 
87     _hidl_cb(Result::SUCCESS, demuxId, demux);
88     return Void();
89 }
90 
getDemuxCaps(getDemuxCaps_cb _hidl_cb)91 Return<void> Tuner::getDemuxCaps(getDemuxCaps_cb _hidl_cb) {
92     ALOGV("%s", __FUNCTION__);
93 
94     DemuxCapabilities caps;
95 
96     _hidl_cb(Result::SUCCESS, caps);
97     return Void();
98 }
99 
openDescrambler(openDescrambler_cb _hidl_cb)100 Return<void> Tuner::openDescrambler(openDescrambler_cb _hidl_cb) {
101     ALOGV("%s", __FUNCTION__);
102 
103     sp<IDescrambler> descrambler = new Descrambler();
104 
105     _hidl_cb(Result::SUCCESS, descrambler);
106     return Void();
107 }
108 
getFrontendInfo(FrontendId,getFrontendInfo_cb _hidl_cb)109 Return<void> Tuner::getFrontendInfo(FrontendId /* frontendId */, getFrontendInfo_cb _hidl_cb) {
110     ALOGV("%s", __FUNCTION__);
111 
112     FrontendInfo info;
113 
114     _hidl_cb(Result::SUCCESS, info);
115     return Void();
116 }
117 
getLnbIds(getLnbIds_cb _hidl_cb)118 Return<void> Tuner::getLnbIds(getLnbIds_cb _hidl_cb) {
119     ALOGV("%s", __FUNCTION__);
120 
121     vector<LnbId> lnbIds;
122 
123     _hidl_cb(Result::SUCCESS, lnbIds);
124     return Void();
125 }
126 
openLnbById(LnbId,openLnbById_cb _hidl_cb)127 Return<void> Tuner::openLnbById(LnbId /* lnbId */, openLnbById_cb _hidl_cb) {
128     ALOGV("%s", __FUNCTION__);
129 
130     sp<ILnb> lnb = new Lnb();
131 
132     _hidl_cb(Result::SUCCESS, lnb);
133     return Void();
134 }
135 
getFrontendById(uint32_t frontendId)136 sp<Frontend> Tuner::getFrontendById(uint32_t frontendId) {
137     ALOGV("%s", __FUNCTION__);
138 
139     return mFrontends[frontendId];
140 }
141 
openLnbByName(const hidl_string &,openLnbByName_cb _hidl_cb)142 Return<void> Tuner::openLnbByName(const hidl_string& /*lnbName*/, openLnbByName_cb _hidl_cb) {
143     ALOGV("%s", __FUNCTION__);
144 
145     sp<ILnb> lnb = new Lnb();
146 
147     _hidl_cb(Result::SUCCESS, 1234, lnb);
148     return Void();
149 }
150 
setFrontendAsDemuxSource(uint32_t frontendId,uint32_t demuxId)151 void Tuner::setFrontendAsDemuxSource(uint32_t frontendId, uint32_t demuxId) {
152     mFrontendToDemux[frontendId] = demuxId;
153 }
154 
frontendStopTune(uint32_t frontendId)155 void Tuner::frontendStopTune(uint32_t frontendId) {
156     map<uint32_t, uint32_t>::iterator it = mFrontendToDemux.find(frontendId);
157     uint32_t demuxId;
158     if (it != mFrontendToDemux.end()) {
159         demuxId = it->second;
160         mDemuxes[demuxId]->stopFrontendInput();
161     }
162 }
163 
164 }  // namespace implementation
165 }  // namespace V1_0
166 }  // namespace tuner
167 }  // namespace tv
168 }  // namespace hardware
169 }  // namespace android
170