blob: 64d8b896131fb17a428657099d010b76d8f39492 [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
110 auto& info11 = mCurrentProgramInfo;
111 auto& info10 = info11.base;
112
113 VirtualProgram virtualProgram;
114 if (virtualRadio != nullptr && virtualRadio->getProgram(mCurrentProgram, virtualProgram)) {
115 // TODO(b/36864090): convert virtualProgram to ProgramInfo instead
116 info10.channel = mCurrentProgram;
117 info10.tuned = true;
118 info10.stereo = true;
119 info10.signalStrength = 100;
120 } else {
121 info11 = makeDummyProgramInfo(mCurrentProgram);
122 }
123 mIsTuneCompleted = true;
124
125 mCallback->tuneComplete(Result::OK, info10);
126 if (mCallback1_1 != nullptr) {
127 mCallback1_1->tuneComplete_1_1(Result::OK, info11);
128 }
129}
130
131Return<Result> Tuner::scan(Direction direction, bool skipSubChannel __unused) {
132 ALOGV("%s", __func__);
133 lock_guard<mutex> lk(mMut);
134 vector<VirtualProgram> list;
135
136 if (mAmfmConfig.type == Band::FM_HD || mAmfmConfig.type == Band::FM) {
137 list = mVirtualFm.getProgramList();
138 }
139
140 if (list.size() == 0) {
141 mIsTuneCompleted = false;
142 auto task = [this, direction]() {
143 ALOGI("Performing failed scan %s", toString(direction).c_str());
144
145 mCallback->tuneComplete(Result::TIMEOUT, {});
146 if (mCallback1_1 != nullptr) {
147 mCallback1_1->tuneComplete_1_1(Result::TIMEOUT, {});
148 }
149 };
150 mThread.schedule(task, gDefaultDelay.scan);
151
152 return Result::OK;
153 }
154
155 // Not optimal (O(sort) instead of O(n)), but not a big deal here;
156 // also, it's likely that list is already sorted (so O(n) anyway).
157 sort(list.begin(), list.end());
158 auto current = mCurrentProgram;
159 auto found = lower_bound(list.begin(), list.end(), VirtualProgram({current}));
160 if (direction == Direction::UP) {
161 if (found < list.end() - 1) {
162 if (found->channel == current) found++;
163 } else {
164 found = list.begin();
165 }
166 } else {
167 if (found > list.begin() && found != list.end()) {
168 found--;
169 } else {
170 found = list.end() - 1;
171 }
172 }
173 auto tuneTo = found->channel;
174
175 mIsTuneCompleted = false;
176 auto task = [this, tuneTo, direction]() {
177 ALOGI("Performing scan %s", toString(direction).c_str());
178
179 lock_guard<mutex> lk(mMut);
180 mCurrentProgram = tuneTo;
181 tuneInternalLocked();
182 };
183 mThread.schedule(task, gDefaultDelay.scan);
184
185 return Result::OK;
186}
187
188Return<Result> Tuner::step(Direction direction, bool skipSubChannel __unused) {
189 ALOGV("%s", __func__);
190
191 lock_guard<mutex> lk(mMut);
192 ALOGW_IF(!mIsAmfmConfigSet, "AM/FM config not set");
193 if (!mIsAmfmConfigSet) return Result::INVALID_STATE;
194 mIsTuneCompleted = false;
195
196 auto task = [this, direction]() {
197 ALOGI("Performing step %s", toString(direction).c_str());
198
199 lock_guard<mutex> lk(mMut);
200
201 if (direction == Direction::UP) {
202 mCurrentProgram += mAmfmConfig.spacings[0];
203 } else {
204 mCurrentProgram -= mAmfmConfig.spacings[0];
205 }
206
207 if (mCurrentProgram > mAmfmConfig.upperLimit) mCurrentProgram = mAmfmConfig.lowerLimit;
208 if (mCurrentProgram < mAmfmConfig.lowerLimit) mCurrentProgram = mAmfmConfig.upperLimit;
209
210 tuneInternalLocked();
211 };
212 mThread.schedule(task, gDefaultDelay.step);
213
214 return Result::OK;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800215}
216
217Return<Result> Tuner::tune(uint32_t channel, uint32_t subChannel) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700218 ALOGV("%s(%d, %d)", __func__, channel, subChannel);
219
220 lock_guard<mutex> lk(mMut);
221 ALOGW_IF(!mIsAmfmConfigSet, "AM/FM config not set");
222 if (!mIsAmfmConfigSet) return Result::INVALID_STATE;
223 if (channel < mAmfmConfig.lowerLimit || channel > mAmfmConfig.upperLimit) {
224 return Result::INVALID_ARGUMENTS;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800225 }
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700226 mIsTuneCompleted = false;
227
228 auto task = [this, channel]() {
229 lock_guard<mutex> lk(mMut);
230 mCurrentProgram = channel;
231 tuneInternalLocked();
232 };
233 mThread.schedule(task, gDefaultDelay.tune);
234
235 return Result::OK;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800236}
237
238Return<Result> Tuner::cancel() {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700239 ALOGV("%s", __func__);
240 mThread.cancelAll();
241 return Result::OK;
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800242}
243
244Return<void> Tuner::getProgramInformation(getProgramInformation_cb _hidl_cb) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700245 ALOGV("%s", __func__);
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800246 return getProgramInformation_1_1([&](Result result, const ProgramInfo& info) {
247 _hidl_cb(result, info.base);
248 });
249}
250
251Return<void> Tuner::getProgramInformation_1_1(getProgramInformation_1_1_cb _hidl_cb) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700252 ALOGV("%s", __func__);
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800253
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700254 lock_guard<mutex> lk(mMut);
255 if (mIsTuneCompleted) {
256 _hidl_cb(Result::OK, mCurrentProgramInfo);
257 } else {
258 _hidl_cb(Result::NOT_INITIALIZED, makeDummyProgramInfo(mCurrentProgram));
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800259 }
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800260 return Void();
261}
262
Tomasz Wasilczyk803301a2017-03-13 14:30:15 -0700263Return<ProgramListResult> Tuner::startBackgroundScan() {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700264 ALOGV("%s", __func__);
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700265 return ProgramListResult::UNAVAILABLE;
Tomasz Wasilczyk803301a2017-03-13 14:30:15 -0700266}
267
Tomasz Wasilczyk2dd1d8d2017-03-01 14:21:07 -0800268Return<void> Tuner::getProgramList(const hidl_string& filter __unused, getProgramList_cb _hidl_cb) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700269 ALOGV("%s", __func__);
270
271 auto& virtualRadio = mVirtualFm;
272 if (mAmfmConfig.type != Band::FM_HD && mAmfmConfig.type != Band::FM) {
273 _hidl_cb(ProgramListResult::OK, {});
274 return Void();
275 }
276
277 hidl_vec<ProgramInfo> list;
278 auto vList = virtualRadio.getProgramList();
279 list.resize(vList.size());
280 for (size_t i = 0; i < vList.size(); i++) {
281 auto& src = vList[i];
282 auto& dst11 = list[i];
283 auto& dst10 = dst11.base;
284
285 // TODO(b/36864090): convert virtualProgram to ProgramInfo instead
286 dst10.channel = src.channel;
287 dst10.tuned = true;
288 }
289
290 _hidl_cb(ProgramListResult::OK, list);
Tomasz Wasilczyk2dd1d8d2017-03-01 14:21:07 -0800291 return Void();
292}
293
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700294Return<void> Tuner::isAnalogForced(isAnalogForced_cb _hidl_cb) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700295 ALOGV("%s", __func__);
296 // TODO(b/36864090): implement
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700297 _hidl_cb(Result::INVALID_STATE, false);
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700298 return Void();
299}
300
301Return<Result> Tuner::setAnalogForced(bool isForced __unused) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700302 ALOGV("%s", __func__);
303 // TODO(b/36864090): implement
Tomasz Wasilczyk6ca90ed2017-05-15 12:57:20 -0700304 return Result::INVALID_STATE;
Tomasz Wasilczykc7002822017-03-27 14:29:16 -0700305}
306
Tomasz Wasilczyk48377552017-06-22 10:45:33 -0700307} // namespace implementation
Tomasz Wasilczyk213170b2017-02-07 17:38:21 -0800308} // namespace V1_1
309} // namespace broadcastradio
310} // namespace hardware
311} // namespace android