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 "LnbClient"
18 
19 #include <android-base/logging.h>
20 #include <utils/Log.h>
21 
22 #include "LnbClient.h"
23 
24 using ::android::hardware::tv::tuner::V1_0::Result;
25 
26 namespace android {
27 
28 /////////////// LnbClient ///////////////////////
29 
LnbClient(shared_ptr<ITunerLnb> tunerLnb)30 LnbClient::LnbClient(shared_ptr<ITunerLnb> tunerLnb) {
31     mTunerLnb = tunerLnb;
32     mId = -1;
33 }
34 
~LnbClient()35 LnbClient::~LnbClient() {
36     mTunerLnb = NULL;
37     mLnb = NULL;
38     mId = -1;
39 }
40 
41 // TODO: remove after migration to Tuner Service is done.
setHidlLnb(sp<ILnb> lnb)42 void LnbClient::setHidlLnb(sp<ILnb> lnb) {
43     mLnb = lnb;
44 }
45 
setCallback(sp<LnbClientCallback> cb)46 Result LnbClient::setCallback(sp<LnbClientCallback> cb) {
47     if (mTunerLnb != NULL) {
48         shared_ptr<TunerLnbCallback> aidlCallback =
49                 ::ndk::SharedRefBase::make<TunerLnbCallback>(cb);
50         Status s = mTunerLnb->setCallback(aidlCallback);
51         return ClientHelper::getServiceSpecificErrorCode(s);
52     }
53 
54     if (mLnb != NULL) {
55         sp<HidlLnbCallback> hidlCallback = new HidlLnbCallback(cb);
56         return mLnb->setCallback(hidlCallback);
57     }
58 
59     return Result::INVALID_STATE;
60 }
61 
setVoltage(LnbVoltage voltage)62 Result LnbClient::setVoltage(LnbVoltage voltage) {
63     if (mTunerLnb != NULL) {
64         Status s = mTunerLnb->setVoltage((int)voltage);
65         return ClientHelper::getServiceSpecificErrorCode(s);
66     }
67 
68     if (mLnb != NULL) {
69         return mLnb->setVoltage(voltage);
70     }
71 
72     return Result::INVALID_STATE;
73 }
74 
setTone(LnbTone tone)75 Result LnbClient::setTone(LnbTone tone) {
76     if (mTunerLnb != NULL) {
77         Status s = mTunerLnb->setTone((int)tone);
78         return ClientHelper::getServiceSpecificErrorCode(s);
79     }
80 
81     if (mLnb != NULL) {
82         return mLnb->setTone(tone);
83     }
84 
85     return Result::INVALID_STATE;
86 }
87 
setSatellitePosition(LnbPosition position)88 Result LnbClient::setSatellitePosition(LnbPosition position) {
89     if (mTunerLnb != NULL) {
90         Status s = mTunerLnb->setSatellitePosition((int)position);
91         return ClientHelper::getServiceSpecificErrorCode(s);
92     }
93 
94     if (mLnb != NULL) {
95         return mLnb->setSatellitePosition(position);
96     }
97 
98     return Result::INVALID_STATE;
99 }
100 
sendDiseqcMessage(vector<uint8_t> diseqcMessage)101 Result LnbClient::sendDiseqcMessage(vector<uint8_t> diseqcMessage) {
102     if (mTunerLnb != NULL) {
103         Status s = mTunerLnb->sendDiseqcMessage(diseqcMessage);
104         return ClientHelper::getServiceSpecificErrorCode(s);
105     }
106 
107     if (mLnb != NULL) {
108         return mLnb->sendDiseqcMessage(diseqcMessage);
109     }
110 
111     return Result::INVALID_STATE;
112 }
113 
close()114 Result LnbClient::close() {
115     if (mTunerLnb != NULL) {
116         Status s = mTunerLnb->close();
117         mTunerLnb = NULL;
118         return ClientHelper::getServiceSpecificErrorCode(s);
119     }
120 
121     if (mLnb != NULL) {
122         Result res = mLnb->close();
123         mLnb = NULL;
124         return res;
125     }
126 
127     return Result::INVALID_STATE;
128 }
129 
130 /////////////// ILnbCallback ///////////////////////
131 
HidlLnbCallback(sp<LnbClientCallback> lnbClientCallback)132 HidlLnbCallback::HidlLnbCallback(sp<LnbClientCallback> lnbClientCallback)
133         : mLnbClientCallback(lnbClientCallback) {}
134 
onEvent(const LnbEventType lnbEventType)135 Return<void> HidlLnbCallback::onEvent(const LnbEventType lnbEventType) {
136     if (mLnbClientCallback != NULL) {
137         mLnbClientCallback->onEvent(lnbEventType);
138     }
139     return Void();
140 }
141 
onDiseqcMessage(const hidl_vec<uint8_t> & diseqcMessage)142 Return<void> HidlLnbCallback::onDiseqcMessage(const hidl_vec<uint8_t>& diseqcMessage) {
143     if (mLnbClientCallback != NULL) {
144         mLnbClientCallback->onDiseqcMessage(diseqcMessage);
145     }
146     return Void();
147 }
148 
149 /////////////// TunerLnbCallback ///////////////////////
150 
TunerLnbCallback(sp<LnbClientCallback> lnbClientCallback)151 TunerLnbCallback::TunerLnbCallback(sp<LnbClientCallback> lnbClientCallback)
152         : mLnbClientCallback(lnbClientCallback) {}
153 
onEvent(int lnbEventType)154 Status TunerLnbCallback::onEvent(int lnbEventType) {
155     if (mLnbClientCallback != NULL) {
156         mLnbClientCallback->onEvent(static_cast<LnbEventType>(lnbEventType));
157         return Status::ok();
158     }
159     return Status::fromServiceSpecificError(static_cast<int32_t>(Result::INVALID_STATE));
160 }
161 
onDiseqcMessage(const vector<uint8_t> & diseqcMessage)162 Status TunerLnbCallback::onDiseqcMessage(const vector<uint8_t>& diseqcMessage) {
163     if (mLnbClientCallback != NULL) {
164         hidl_vec<uint8_t> msg(begin(diseqcMessage), end(diseqcMessage));
165         mLnbClientCallback->onDiseqcMessage(msg);
166         return Status::ok();
167     }
168     return Status::fromServiceSpecificError(static_cast<int32_t>(Result::INVALID_STATE));
169 }
170 }  // namespace android
171