blob: 9b39d36eea25cb3082c34d2ea73006094e780b00 [file] [log] [blame]
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -08001/*
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
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070017#define LOG_TAG "BroadcastRadioDefault.tuner"
18#define LOG_NDEBUG 0
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -080019
20#include "BroadcastRadio.h"
21#include "Tuner.h"
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070022
23#include <log/log.h>
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -080024
25namespace android {
26namespace hardware {
27namespace broadcastradio {
28namespace V1_1 {
29namespace implementation {
30
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070031using namespace std::chrono_literals;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -080032
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070033using V1_0::Band;
34using V1_0::BandConfig;
35using V1_0::Direction;
36
37using std::chrono::milliseconds;
38using std::lock_guard;
39using std::move;
40using std::mutex;
41using std::sort;
42using std::vector;
43
44const struct {
45 milliseconds config = 50ms;
46 milliseconds scan = 200ms;
47 milliseconds step = 100ms;
48 milliseconds tune = 150ms;
49} gDefaultDelay;
50
51Tuner::Tuner(const sp<V1_0::ITunerCallback>& callback)
52 : mCallback(callback),
53 mCallback1_1(ITunerCallback::castFrom(callback).withDefault(nullptr)),
54 mVirtualFm(make_fm_radio()) {}
55
56void Tuner::forceClose() {
57 lock_guard<mutex> lk(mMut);
58 mIsClosed = true;
59 mThread.cancelAll();
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -080060}
61
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070062Return<Result> Tuner::setConfiguration(const BandConfig& config) {
63 ALOGV("%s", __func__);
64
65 if (config.lowerLimit >= config.upperLimit) return Result::INVALID_ARGUMENTS;
66
67 auto task = [this, config]() {
68 ALOGI("Setting AM/FM config");
69 lock_guard<mutex> lk(mMut);
70
71 mAmfmConfig = move(config);
72 mAmfmConfig.antennaConnected = true;
73 mIsAmfmConfigSet = true;
74 mCallback->configChange(Result::OK, mAmfmConfig);
75 };
76 mThread.schedule(task, gDefaultDelay.config);
77
78 return Result::OK;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -080079}
80
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070081Return<void> Tuner::getConfiguration(getConfiguration_cb _hidl_cb) {
82 ALOGV("%s", __func__);
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -080083
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070084 lock_guard<mutex> lk(mMut);
85 if (mIsAmfmConfigSet) {
86 _hidl_cb(Result::OK, mAmfmConfig);
87 } else {
88 _hidl_cb(Result::NOT_INITIALIZED, {});
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -080089 }
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -080090 return Void();
91}
92
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070093// makes ProgramInfo that points to no channel
94static ProgramInfo makeDummyProgramInfo(uint32_t channel) {
95 ProgramInfo info11 = {};
96 auto& info10 = info11.base;
97
98 info10.channel = channel;
99 info11.flags |= ProgramInfoFlags::MUTED;
100
101 return info11;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800102}
103
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700104void Tuner::tuneInternalLocked() {
105 VirtualRadio* virtualRadio = nullptr;
106 if (mAmfmConfig.type == Band::FM_HD || mAmfmConfig.type == Band::FM) {
107 virtualRadio = &mVirtualFm;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800108 }
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700109
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700110 VirtualProgram virtualProgram;
111 if (virtualRadio != nullptr && virtualRadio->getProgram(mCurrentProgram, virtualProgram)) {
Tomasz Wasilczyk100f2ed2017-06-29 16:04:05 -0700112 mCurrentProgramInfo = static_cast<ProgramInfo>(virtualProgram);
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700113 } else {
Tomasz Wasilczyk100f2ed2017-06-29 16:04:05 -0700114 mCurrentProgramInfo = makeDummyProgramInfo(mCurrentProgram);
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700115 }
116 mIsTuneCompleted = true;
117
Tomasz Wasilczyk100f2ed2017-06-29 16:04:05 -0700118 mCallback->tuneComplete(Result::OK, mCurrentProgramInfo.base);
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700119 if (mCallback1_1 != nullptr) {
Tomasz Wasilczyk100f2ed2017-06-29 16:04:05 -0700120 mCallback1_1->tuneComplete_1_1(Result::OK, mCurrentProgramInfo);
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700121 }
122}
123
124Return<Result> Tuner::scan(Direction direction, bool skipSubChannel __unused) {
125 ALOGV("%s", __func__);
126 lock_guard<mutex> lk(mMut);
127 vector<VirtualProgram> list;
128
129 if (mAmfmConfig.type == Band::FM_HD || mAmfmConfig.type == Band::FM) {
130 list = mVirtualFm.getProgramList();
131 }
132
133 if (list.size() == 0) {
134 mIsTuneCompleted = false;
135 auto task = [this, direction]() {
136 ALOGI("Performing failed scan %s", toString(direction).c_str());
137
138 mCallback->tuneComplete(Result::TIMEOUT, {});
139 if (mCallback1_1 != nullptr) {
140 mCallback1_1->tuneComplete_1_1(Result::TIMEOUT, {});
141 }
142 };
143 mThread.schedule(task, gDefaultDelay.scan);
144
145 return Result::OK;
146 }
147
148 // Not optimal (O(sort) instead of O(n)), but not a big deal here;
149 // also, it's likely that list is already sorted (so O(n) anyway).
150 sort(list.begin(), list.end());
151 auto current = mCurrentProgram;
152 auto found = lower_bound(list.begin(), list.end(), VirtualProgram({current}));
153 if (direction == Direction::UP) {
154 if (found < list.end() - 1) {
155 if (found->channel == current) found++;
156 } else {
157 found = list.begin();
158 }
159 } else {
160 if (found > list.begin() && found != list.end()) {
161 found--;
162 } else {
163 found = list.end() - 1;
164 }
165 }
166 auto tuneTo = found->channel;
167
168 mIsTuneCompleted = false;
169 auto task = [this, tuneTo, direction]() {
170 ALOGI("Performing scan %s", toString(direction).c_str());
171
172 lock_guard<mutex> lk(mMut);
173 mCurrentProgram = tuneTo;
174 tuneInternalLocked();
175 };
176 mThread.schedule(task, gDefaultDelay.scan);
177
178 return Result::OK;
179}
180
181Return<Result> Tuner::step(Direction direction, bool skipSubChannel __unused) {
182 ALOGV("%s", __func__);
183
184 lock_guard<mutex> lk(mMut);
185 ALOGW_IF(!mIsAmfmConfigSet, "AM/FM config not set");
186 if (!mIsAmfmConfigSet) return Result::INVALID_STATE;
187 mIsTuneCompleted = false;
188
189 auto task = [this, direction]() {
190 ALOGI("Performing step %s", toString(direction).c_str());
191
192 lock_guard<mutex> lk(mMut);
193
194 if (direction == Direction::UP) {
195 mCurrentProgram += mAmfmConfig.spacings[0];
196 } else {
197 mCurrentProgram -= mAmfmConfig.spacings[0];
198 }
199
200 if (mCurrentProgram > mAmfmConfig.upperLimit) mCurrentProgram = mAmfmConfig.lowerLimit;
201 if (mCurrentProgram < mAmfmConfig.lowerLimit) mCurrentProgram = mAmfmConfig.upperLimit;
202
203 tuneInternalLocked();
204 };
205 mThread.schedule(task, gDefaultDelay.step);
206
207 return Result::OK;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800208}
209
210Return<Result> Tuner::tune(uint32_t channel, uint32_t subChannel) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700211 ALOGV("%s(%d, %d)", __func__, channel, subChannel);
212
213 lock_guard<mutex> lk(mMut);
214 ALOGW_IF(!mIsAmfmConfigSet, "AM/FM config not set");
215 if (!mIsAmfmConfigSet) return Result::INVALID_STATE;
216 if (channel < mAmfmConfig.lowerLimit || channel > mAmfmConfig.upperLimit) {
217 return Result::INVALID_ARGUMENTS;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800218 }
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700219 mIsTuneCompleted = false;
220
221 auto task = [this, channel]() {
222 lock_guard<mutex> lk(mMut);
223 mCurrentProgram = channel;
224 tuneInternalLocked();
225 };
226 mThread.schedule(task, gDefaultDelay.tune);
227
228 return Result::OK;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800229}
230
231Return<Result> Tuner::cancel() {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700232 ALOGV("%s", __func__);
233 mThread.cancelAll();
234 return Result::OK;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800235}
236
237Return<void> Tuner::getProgramInformation(getProgramInformation_cb _hidl_cb) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700238 ALOGV("%s", __func__);
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800239 return getProgramInformation_1_1([&](Result result, const ProgramInfo& info) {
240 _hidl_cb(result, info.base);
241 });
242}
243
244Return<void> Tuner::getProgramInformation_1_1(getProgramInformation_1_1_cb _hidl_cb) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700245 ALOGV("%s", __func__);
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800246
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700247 lock_guard<mutex> lk(mMut);
248 if (mIsTuneCompleted) {
249 _hidl_cb(Result::OK, mCurrentProgramInfo);
250 } else {
251 _hidl_cb(Result::NOT_INITIALIZED, makeDummyProgramInfo(mCurrentProgram));
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800252 }
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800253 return Void();
254}
255
Tomasz Wasilczyk803301a2017-03-13 14:30:15 -0700256Return<ProgramListResult> Tuner::startBackgroundScan() {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700257 ALOGV("%s", __func__);
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700258 return ProgramListResult::UNAVAILABLE;
Tomasz Wasilczyk803301a2017-03-13 14:30:15 -0700259}
260
Tomasz Wasilczyk2dd1d8d2017-03-01 14:21:07 -0800261Return<void> Tuner::getProgramList(const hidl_string& filter __unused, getProgramList_cb _hidl_cb) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700262 ALOGV("%s", __func__);
263
264 auto& virtualRadio = mVirtualFm;
265 if (mAmfmConfig.type != Band::FM_HD && mAmfmConfig.type != Band::FM) {
266 _hidl_cb(ProgramListResult::OK, {});
267 return Void();
268 }
269
Tomasz Wasilczyk100f2ed2017-06-29 16:04:05 -0700270 auto list = virtualRadio.getProgramList();
271 _hidl_cb(ProgramListResult::OK, vector<ProgramInfo>(list.begin(), list.end()));
Tomasz Wasilczyk2dd1d8d2017-03-01 14:21:07 -0800272 return Void();
273}
274
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700275Return<void> Tuner::isAnalogForced(isAnalogForced_cb _hidl_cb) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700276 ALOGV("%s", __func__);
277 // TODO(b/36864090): implement
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700278 _hidl_cb(Result::INVALID_STATE, false);
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700279 return Void();
280}
281
282Return<Result> Tuner::setAnalogForced(bool isForced __unused) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700283 ALOGV("%s", __func__);
284 // TODO(b/36864090): implement
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700285 return Result::INVALID_STATE;
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700286}
287
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700288} // namespace implementation
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800289} // namespace V1_1
290} // namespace broadcastradio
291} // namespace hardware
292} // namespace android