OSDN Git Service

release-request-bd6aa7dd-7b02-4794-942c-14599bf61208-for-git_oc-mr1-release-4193791...
[android-x86/hardware-interfaces.git] / broadcastradio / 1.1 / default / Tuner.cpp
1 /*
2  * Copyright (C) 2017 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 "BroadcastRadioDefault.tuner"
18 #define LOG_NDEBUG 0
19
20 #include "BroadcastRadio.h"
21 #include "Tuner.h"
22
23 #include <Utils.h>
24 #include <log/log.h>
25
26 namespace android {
27 namespace hardware {
28 namespace broadcastradio {
29 namespace V1_1 {
30 namespace implementation {
31
32 using namespace std::chrono_literals;
33
34 using V1_0::Band;
35 using V1_0::BandConfig;
36 using V1_0::Direction;
37
38 using std::chrono::milliseconds;
39 using std::lock_guard;
40 using std::move;
41 using std::mutex;
42 using std::sort;
43 using std::vector;
44
45 const struct {
46     milliseconds config = 50ms;
47     milliseconds scan = 200ms;
48     milliseconds step = 100ms;
49     milliseconds tune = 150ms;
50 } gDefaultDelay;
51
52 Tuner::Tuner(const sp<V1_0::ITunerCallback>& callback)
53     : mCallback(callback),
54       mCallback1_1(ITunerCallback::castFrom(callback).withDefault(nullptr)),
55       mVirtualFm(make_fm_radio()) {}
56
57 void Tuner::forceClose() {
58     lock_guard<mutex> lk(mMut);
59     mIsClosed = true;
60     mThread.cancelAll();
61 }
62
63 Return<Result> Tuner::setConfiguration(const BandConfig& config) {
64     ALOGV("%s", __func__);
65
66     if (config.lowerLimit >= config.upperLimit) return Result::INVALID_ARGUMENTS;
67
68     auto task = [this, config]() {
69         ALOGI("Setting AM/FM config");
70         lock_guard<mutex> lk(mMut);
71
72         mAmfmConfig = move(config);
73         mAmfmConfig.antennaConnected = true;
74         mCurrentProgram = utils::make_selector(mAmfmConfig.type, mAmfmConfig.lowerLimit);
75
76         mIsAmfmConfigSet = true;
77         mCallback->configChange(Result::OK, mAmfmConfig);
78     };
79     mThread.schedule(task, gDefaultDelay.config);
80
81     return Result::OK;
82 }
83
84 Return<void> Tuner::getConfiguration(getConfiguration_cb _hidl_cb) {
85     ALOGV("%s", __func__);
86
87     lock_guard<mutex> lk(mMut);
88     if (mIsAmfmConfigSet) {
89         _hidl_cb(Result::OK, mAmfmConfig);
90     } else {
91         _hidl_cb(Result::NOT_INITIALIZED, {});
92     }
93     return Void();
94 }
95
96 // makes ProgramInfo that points to no program
97 static ProgramInfo makeDummyProgramInfo(const ProgramSelector& selector) {
98     ProgramInfo info11 = {};
99     auto& info10 = info11.base;
100
101     utils::getLegacyChannel(selector, &info10.channel, &info10.subChannel);
102     info11.selector = selector;
103     info11.flags |= ProgramInfoFlags::MUTED;
104
105     return info11;
106 }
107
108 bool Tuner::isFmLocked() {
109     if (!utils::isAmFm(utils::getType(mCurrentProgram))) return false;
110     return mAmfmConfig.type == Band::FM_HD || mAmfmConfig.type == Band::FM;
111 }
112
113 void Tuner::tuneInternalLocked(const ProgramSelector& sel) {
114     VirtualRadio* virtualRadio = nullptr;
115     if (isFmLocked()) {
116         virtualRadio = &mVirtualFm;
117     }
118
119     VirtualProgram virtualProgram;
120     if (virtualRadio != nullptr && virtualRadio->getProgram(sel, virtualProgram)) {
121         mCurrentProgram = virtualProgram.selector;
122         mCurrentProgramInfo = static_cast<ProgramInfo>(virtualProgram);
123     } else {
124         mCurrentProgram = sel;
125         mCurrentProgramInfo = makeDummyProgramInfo(sel);
126     }
127     mIsTuneCompleted = true;
128
129     if (mCallback1_1 == nullptr) {
130         mCallback->tuneComplete(Result::OK, mCurrentProgramInfo.base);
131     } else {
132         mCallback1_1->tuneComplete_1_1(Result::OK, mCurrentProgramInfo.selector);
133     }
134 }
135
136 Return<Result> Tuner::scan(Direction direction, bool skipSubChannel __unused) {
137     ALOGV("%s", __func__);
138     lock_guard<mutex> lk(mMut);
139     vector<VirtualProgram> list;
140
141     if (isFmLocked()) {
142         list = mVirtualFm.getProgramList();
143     }
144
145     if (list.empty()) {
146         mIsTuneCompleted = false;
147         auto task = [this, direction]() {
148             ALOGI("Performing failed scan %s", toString(direction).c_str());
149
150             if (mCallback1_1 == nullptr) {
151                 mCallback->tuneComplete(Result::TIMEOUT, {});
152             } else {
153                 mCallback1_1->tuneComplete_1_1(Result::TIMEOUT, {});
154             }
155         };
156         mThread.schedule(task, gDefaultDelay.scan);
157
158         return Result::OK;
159     }
160
161     // Not optimal (O(sort) instead of O(n)), but not a big deal here;
162     // also, it's likely that list is already sorted (so O(n) anyway).
163     sort(list.begin(), list.end());
164     auto current = mCurrentProgram;
165     auto found = lower_bound(list.begin(), list.end(), VirtualProgram({current}));
166     if (direction == Direction::UP) {
167         if (found < list.end() - 1) {
168             if (utils::tunesTo(current, found->selector)) found++;
169         } else {
170             found = list.begin();
171         }
172     } else {
173         if (found > list.begin() && found != list.end()) {
174             found--;
175         } else {
176             found = list.end() - 1;
177         }
178     }
179     auto tuneTo = found->selector;
180
181     mIsTuneCompleted = false;
182     auto task = [this, tuneTo, direction]() {
183         ALOGI("Performing scan %s", toString(direction).c_str());
184
185         lock_guard<mutex> lk(mMut);
186         tuneInternalLocked(tuneTo);
187     };
188     mThread.schedule(task, gDefaultDelay.scan);
189
190     return Result::OK;
191 }
192
193 Return<Result> Tuner::step(Direction direction, bool skipSubChannel) {
194     ALOGV("%s", __func__);
195     ALOGW_IF(!skipSubChannel, "can't step to next frequency without ignoring subChannel");
196
197     lock_guard<mutex> lk(mMut);
198
199     if (!utils::isAmFm(utils::getType(mCurrentProgram))) {
200         ALOGE("Can't step in anything else than AM/FM");
201         return Result::NOT_INITIALIZED;
202     }
203
204     ALOGW_IF(!mIsAmfmConfigSet, "AM/FM config not set");
205     if (!mIsAmfmConfigSet) return Result::INVALID_STATE;
206     mIsTuneCompleted = false;
207
208     auto task = [this, direction]() {
209         ALOGI("Performing step %s", toString(direction).c_str());
210
211         lock_guard<mutex> lk(mMut);
212
213         auto current = utils::getId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY, 0);
214
215         if (direction == Direction::UP) {
216             current += mAmfmConfig.spacings[0];
217         } else {
218             current -= mAmfmConfig.spacings[0];
219         }
220
221         if (current > mAmfmConfig.upperLimit) current = mAmfmConfig.lowerLimit;
222         if (current < mAmfmConfig.lowerLimit) current = mAmfmConfig.upperLimit;
223
224         tuneInternalLocked(utils::make_selector(mAmfmConfig.type, current));
225     };
226     mThread.schedule(task, gDefaultDelay.step);
227
228     return Result::OK;
229 }
230
231 Return<Result> Tuner::tune(uint32_t channel, uint32_t subChannel) {
232     ALOGV("%s(%d, %d)", __func__, channel, subChannel);
233     Band band;
234     {
235         lock_guard<mutex> lk(mMut);
236         band = mAmfmConfig.type;
237     }
238     return tune_1_1(utils::make_selector(band, channel, subChannel));
239 }
240
241 Return<Result> Tuner::tune_1_1(const ProgramSelector& sel) {
242     ALOGV("%s(%s)", __func__, toString(sel).c_str());
243
244     lock_guard<mutex> lk(mMut);
245
246     if (utils::isAmFm(utils::getType(mCurrentProgram))) {
247         ALOGW_IF(!mIsAmfmConfigSet, "AM/FM config not set");
248         if (!mIsAmfmConfigSet) return Result::INVALID_STATE;
249
250         auto freq = utils::getId(sel, IdentifierType::AMFM_FREQUENCY);
251         if (freq < mAmfmConfig.lowerLimit || freq > mAmfmConfig.upperLimit) {
252             return Result::INVALID_ARGUMENTS;
253         }
254     }
255
256     mIsTuneCompleted = false;
257     auto task = [this, sel]() {
258         lock_guard<mutex> lk(mMut);
259         tuneInternalLocked(sel);
260     };
261     mThread.schedule(task, gDefaultDelay.tune);
262
263     return Result::OK;
264 }
265
266 Return<Result> Tuner::cancel() {
267     ALOGV("%s", __func__);
268     mThread.cancelAll();
269     return Result::OK;
270 }
271
272 Return<Result> Tuner::cancelAnnouncement() {
273     ALOGV("%s", __func__);
274     return Result::OK;
275 }
276
277 Return<void> Tuner::getProgramInformation(getProgramInformation_cb _hidl_cb) {
278     ALOGV("%s", __func__);
279     return getProgramInformation_1_1([&](Result result, const ProgramInfo& info) {
280         _hidl_cb(result, info.base);
281     });
282 }
283
284 Return<void> Tuner::getProgramInformation_1_1(getProgramInformation_1_1_cb _hidl_cb) {
285     ALOGV("%s", __func__);
286
287     lock_guard<mutex> lk(mMut);
288     if (mIsTuneCompleted) {
289         _hidl_cb(Result::OK, mCurrentProgramInfo);
290     } else {
291         _hidl_cb(Result::NOT_INITIALIZED, makeDummyProgramInfo(mCurrentProgram));
292     }
293     return Void();
294 }
295
296 Return<ProgramListResult> Tuner::startBackgroundScan() {
297     ALOGV("%s", __func__);
298     return ProgramListResult::UNAVAILABLE;
299 }
300
301 Return<void> Tuner::getProgramList(const hidl_string& filter __unused, getProgramList_cb _hidl_cb) {
302     ALOGV("%s", __func__);
303     lock_guard<mutex> lk(mMut);
304
305     auto& virtualRadio = mVirtualFm;
306     if (!isFmLocked()) {
307         ALOGI("bands other than FM are not supported yet");
308         _hidl_cb(ProgramListResult::OK, {});
309         return Void();
310     }
311
312     auto list = virtualRadio.getProgramList();
313     ALOGD("returning a list of %zu programs", list.size());
314     _hidl_cb(ProgramListResult::OK, vector<ProgramInfo>(list.begin(), list.end()));
315     return Void();
316 }
317
318 Return<void> Tuner::isAnalogForced(isAnalogForced_cb _hidl_cb) {
319     ALOGV("%s", __func__);
320     // TODO(b/36864090): implement
321     _hidl_cb(Result::INVALID_STATE, false);
322     return Void();
323 }
324
325 Return<Result> Tuner::setAnalogForced(bool isForced __unused) {
326     ALOGV("%s", __func__);
327     // TODO(b/36864090): implement
328     return Result::INVALID_STATE;
329 }
330
331 }  // namespace implementation
332 }  // namespace V1_1
333 }  // namespace broadcastradio
334 }  // namespace hardware
335 }  // namespace android