1 /*
2  * Copyright 2021 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 "TunerClient"
18 
19 #include <android/binder_manager.h>
20 #include <android-base/logging.h>
21 #include <utils/Log.h>
22 
23 #include "TunerClient.h"
24 
25 using ::aidl::android::hardware::tv::tuner::FrontendType;
26 
27 namespace android {
28 
29 int32_t TunerClient::mTunerVersion;
30 
31 /////////////// TunerClient ///////////////////////
32 
TunerClient()33 TunerClient::TunerClient() {
34     ::ndk::SpAIBinder binder(AServiceManager_waitForService("media.tuner"));
35     mTunerService = ITunerService::fromBinder(binder);
36     if (mTunerService == nullptr) {
37         ALOGE("Failed to get tuner service");
38     } else {
39         mTunerService->getTunerHalVersion(&mTunerVersion);
40     }
41 }
42 
~TunerClient()43 TunerClient::~TunerClient() {
44 }
45 
getFrontendIds()46 vector<int32_t> TunerClient::getFrontendIds() {
47     vector<int32_t> ids;
48 
49     if (mTunerService != nullptr) {
50         Status s = mTunerService->getFrontendIds(&ids);
51         if (!s.isOk()) {
52             ids.clear();
53         }
54     }
55 
56     return ids;
57 }
58 
openFrontend(int32_t frontendHandle)59 sp<FrontendClient> TunerClient::openFrontend(int32_t frontendHandle) {
60     if (mTunerService != nullptr) {
61         shared_ptr<ITunerFrontend> tunerFrontend;
62         Status s = mTunerService->openFrontend(frontendHandle, &tunerFrontend);
63         if (!s.isOk() || tunerFrontend == nullptr) {
64             return nullptr;
65         }
66         int32_t id;
67         s = tunerFrontend->getFrontendId(&id);
68         if (!s.isOk()) {
69             tunerFrontend->close();
70             return nullptr;
71         }
72         FrontendInfo frontendInfo;
73         s = mTunerService->getFrontendInfo(id, &frontendInfo);
74         if (!s.isOk()) {
75             tunerFrontend->close();
76             return nullptr;
77         }
78         return new FrontendClient(tunerFrontend, frontendInfo.type);
79     }
80 
81     return nullptr;
82 }
83 
getFrontendInfo(int32_t id)84 shared_ptr<FrontendInfo> TunerClient::getFrontendInfo(int32_t id) {
85     if (mTunerService != nullptr) {
86         FrontendInfo aidlFrontendInfo;
87         Status s = mTunerService->getFrontendInfo(id, &aidlFrontendInfo);
88         if (!s.isOk()) {
89             return nullptr;
90         }
91         return make_shared<FrontendInfo>(aidlFrontendInfo);
92     }
93 
94     return nullptr;
95 }
96 
openDemux(int32_t demuxHandle)97 sp<DemuxClient> TunerClient::openDemux(int32_t demuxHandle) {
98     if (mTunerService != nullptr) {
99         shared_ptr<ITunerDemux> tunerDemux;
100         Status s = mTunerService->openDemux(demuxHandle, &tunerDemux);
101         if (!s.isOk()) {
102             return nullptr;
103         }
104         return new DemuxClient(tunerDemux);
105     }
106 
107     return nullptr;
108 }
109 
getDemuxInfo(int32_t demuxHandle)110 shared_ptr<DemuxInfo> TunerClient::getDemuxInfo(int32_t demuxHandle) {
111     if (mTunerService != nullptr) {
112         DemuxInfo aidlDemuxInfo;
113         Status s = mTunerService->getDemuxInfo(demuxHandle, &aidlDemuxInfo);
114         if (!s.isOk()) {
115             return nullptr;
116         }
117         return make_shared<DemuxInfo>(aidlDemuxInfo);
118     }
119     return nullptr;
120 }
121 
getDemuxInfoList(vector<DemuxInfo> * demuxInfoList)122 void TunerClient::getDemuxInfoList(vector<DemuxInfo>* demuxInfoList) {
123     if (mTunerService != nullptr) {
124         Status s = mTunerService->getDemuxInfoList(demuxInfoList);
125         if (!s.isOk()) {
126             demuxInfoList->clear();
127         }
128     }
129 }
130 
getDemuxCaps()131 shared_ptr<DemuxCapabilities> TunerClient::getDemuxCaps() {
132     if (mTunerService != nullptr) {
133         DemuxCapabilities aidlCaps;
134         Status s = mTunerService->getDemuxCaps(&aidlCaps);
135         if (!s.isOk()) {
136             return nullptr;
137         }
138         return make_shared<DemuxCapabilities>(aidlCaps);
139     }
140 
141     return nullptr;
142 }
143 
openDescrambler(int32_t descramblerHandle)144 sp<DescramblerClient> TunerClient::openDescrambler(int32_t descramblerHandle) {
145     if (mTunerService != nullptr) {
146         shared_ptr<ITunerDescrambler> tunerDescrambler;
147         Status s = mTunerService->openDescrambler(descramblerHandle, &tunerDescrambler);
148         if (!s.isOk()) {
149             return nullptr;
150         }
151         return new DescramblerClient(tunerDescrambler);
152     }
153 
154     return nullptr;
155 }
156 
openLnb(int32_t lnbHandle)157 sp<LnbClient> TunerClient::openLnb(int32_t lnbHandle) {
158     if (mTunerService != nullptr) {
159         shared_ptr<ITunerLnb> tunerLnb;
160         Status s = mTunerService->openLnb(lnbHandle, &tunerLnb);
161         if (!s.isOk()) {
162             return nullptr;
163         }
164         return new LnbClient(tunerLnb);
165     }
166 
167     return nullptr;
168 }
169 
openLnbByName(string lnbName)170 sp<LnbClient> TunerClient::openLnbByName(string lnbName) {
171     if (mTunerService != nullptr) {
172         shared_ptr<ITunerLnb> tunerLnb;
173         Status s = mTunerService->openLnbByName(lnbName, &tunerLnb);
174         if (!s.isOk()) {
175             return nullptr;
176         }
177         return new LnbClient(tunerLnb);
178     }
179 
180     return nullptr;
181 }
182 
openSharedFilter(const string & filterToken,sp<FilterClientCallback> cb)183 sp<FilterClient> TunerClient::openSharedFilter(const string& filterToken,
184                                                sp<FilterClientCallback> cb) {
185     if (cb == nullptr) {
186         return nullptr;
187     }
188 
189     if (mTunerService != nullptr) {
190         shared_ptr<ITunerFilter> tunerFilter;
191         shared_ptr<TunerFilterCallback> callback =
192                 ::ndk::SharedRefBase::make<TunerFilterCallback>(cb);
193         Status s = mTunerService->openSharedFilter(filterToken, callback, &tunerFilter);
194         if (!s.isOk()) {
195             return nullptr;
196         }
197         DemuxFilterType type;
198         tunerFilter->getFilterType(&type);
199         return new FilterClient(type, tunerFilter);
200     }
201 
202     return nullptr;
203 }
204 
setLna(bool bEnable)205 Result TunerClient::setLna(bool bEnable) {
206     if (mTunerService != nullptr) {
207         Status s = mTunerService->setLna(bEnable);
208         return ClientHelper::getServiceSpecificErrorCode(s);
209     }
210 
211     return Result::INVALID_STATE;
212 }
213 
setMaxNumberOfFrontends(FrontendType frontendType,int32_t maxNumber)214 Result TunerClient::setMaxNumberOfFrontends(FrontendType frontendType, int32_t maxNumber) {
215     if (mTunerService != nullptr) {
216         Status s = mTunerService->setMaxNumberOfFrontends(frontendType, maxNumber);
217         return ClientHelper::getServiceSpecificErrorCode(s);
218     }
219 
220     return Result::INVALID_STATE;
221 }
222 
getMaxNumberOfFrontends(FrontendType frontendType)223 int TunerClient::getMaxNumberOfFrontends(FrontendType frontendType) {
224     if (mTunerService != nullptr) {
225         int32_t maxNumber;
226         mTunerService->getMaxNumberOfFrontends(frontendType, &maxNumber);
227         return maxNumber;
228     }
229 
230     return -1;
231 }
232 
isLnaSupported()233 bool TunerClient::isLnaSupported() {
234     if (mTunerService != nullptr) {
235         bool lnaSupported;
236         Status s = mTunerService->isLnaSupported(&lnaSupported);
237         if (!s.isOk()) {
238             return false;
239         }
240         return lnaSupported;
241     }
242 
243     return false;
244 }
245 
246 }  // namespace android
247