Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 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 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 17 | #define LOG_TAG "BroadcastRadioDefault.tuner" |
| 18 | #define LOG_NDEBUG 0 |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 19 | |
| 20 | #include "BroadcastRadio.h" |
| 21 | #include "Tuner.h" |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 22 | |
| 23 | #include <log/log.h> |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace broadcastradio { |
| 28 | namespace V1_1 { |
| 29 | namespace implementation { |
| 30 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 31 | using namespace std::chrono_literals; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 32 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 33 | using V1_0::Band; |
| 34 | using V1_0::BandConfig; |
| 35 | using V1_0::Direction; |
| 36 | |
| 37 | using std::chrono::milliseconds; |
| 38 | using std::lock_guard; |
| 39 | using std::move; |
| 40 | using std::mutex; |
| 41 | using std::sort; |
| 42 | using std::vector; |
| 43 | |
| 44 | const struct { |
| 45 | milliseconds config = 50ms; |
| 46 | milliseconds scan = 200ms; |
| 47 | milliseconds step = 100ms; |
| 48 | milliseconds tune = 150ms; |
| 49 | } gDefaultDelay; |
| 50 | |
| 51 | Tuner::Tuner(const sp<V1_0::ITunerCallback>& callback) |
| 52 | : mCallback(callback), |
| 53 | mCallback1_1(ITunerCallback::castFrom(callback).withDefault(nullptr)), |
| 54 | mVirtualFm(make_fm_radio()) {} |
| 55 | |
| 56 | void Tuner::forceClose() { |
| 57 | lock_guard<mutex> lk(mMut); |
| 58 | mIsClosed = true; |
| 59 | mThread.cancelAll(); |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 62 | Return<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 Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 81 | Return<void> Tuner::getConfiguration(getConfiguration_cb _hidl_cb) { |
| 82 | ALOGV("%s", __func__); |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 83 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 84 | lock_guard<mutex> lk(mMut); |
| 85 | if (mIsAmfmConfigSet) { |
| 86 | _hidl_cb(Result::OK, mAmfmConfig); |
| 87 | } else { |
| 88 | _hidl_cb(Result::NOT_INITIALIZED, {}); |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 89 | } |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 90 | return Void(); |
| 91 | } |
| 92 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 93 | // makes ProgramInfo that points to no channel |
| 94 | static 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 Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 104 | void Tuner::tuneInternalLocked() { |
| 105 | VirtualRadio* virtualRadio = nullptr; |
| 106 | if (mAmfmConfig.type == Band::FM_HD || mAmfmConfig.type == Band::FM) { |
| 107 | virtualRadio = &mVirtualFm; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 108 | } |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 109 | |
| 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 | |
| 131 | Return<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 | |
| 188 | Return<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 Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | Return<Result> Tuner::tune(uint32_t channel, uint32_t subChannel) { |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 218 | 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 Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 225 | } |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 226 | 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 Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | Return<Result> Tuner::cancel() { |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 239 | ALOGV("%s", __func__); |
| 240 | mThread.cancelAll(); |
| 241 | return Result::OK; |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | Return<void> Tuner::getProgramInformation(getProgramInformation_cb _hidl_cb) { |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 245 | ALOGV("%s", __func__); |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 246 | return getProgramInformation_1_1([&](Result result, const ProgramInfo& info) { |
| 247 | _hidl_cb(result, info.base); |
| 248 | }); |
| 249 | } |
| 250 | |
| 251 | Return<void> Tuner::getProgramInformation_1_1(getProgramInformation_1_1_cb _hidl_cb) { |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 252 | ALOGV("%s", __func__); |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 253 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 254 | 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 Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 259 | } |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 260 | return Void(); |
| 261 | } |
| 262 | |
Tomasz Wasilczyk | 803301a | 2017-03-13 14:30:15 -0700 | [diff] [blame] | 263 | Return<ProgramListResult> Tuner::startBackgroundScan() { |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 264 | ALOGV("%s", __func__); |
Tomasz Wasilczyk | 6ca90ed | 2017-05-15 12:57:20 -0700 | [diff] [blame] | 265 | return ProgramListResult::UNAVAILABLE; |
Tomasz Wasilczyk | 803301a | 2017-03-13 14:30:15 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Tomasz Wasilczyk | 2dd1d8d | 2017-03-01 14:21:07 -0800 | [diff] [blame] | 268 | Return<void> Tuner::getProgramList(const hidl_string& filter __unused, getProgramList_cb _hidl_cb) { |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 269 | 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 Wasilczyk | 2dd1d8d | 2017-03-01 14:21:07 -0800 | [diff] [blame] | 291 | return Void(); |
| 292 | } |
| 293 | |
Tomasz Wasilczyk | c700282 | 2017-03-27 14:29:16 -0700 | [diff] [blame] | 294 | Return<void> Tuner::isAnalogForced(isAnalogForced_cb _hidl_cb) { |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 295 | ALOGV("%s", __func__); |
| 296 | // TODO(b/36864090): implement |
Tomasz Wasilczyk | 6ca90ed | 2017-05-15 12:57:20 -0700 | [diff] [blame] | 297 | _hidl_cb(Result::INVALID_STATE, false); |
Tomasz Wasilczyk | c700282 | 2017-03-27 14:29:16 -0700 | [diff] [blame] | 298 | return Void(); |
| 299 | } |
| 300 | |
| 301 | Return<Result> Tuner::setAnalogForced(bool isForced __unused) { |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 302 | ALOGV("%s", __func__); |
| 303 | // TODO(b/36864090): implement |
Tomasz Wasilczyk | 6ca90ed | 2017-05-15 12:57:20 -0700 | [diff] [blame] | 304 | return Result::INVALID_STATE; |
Tomasz Wasilczyk | c700282 | 2017-03-27 14:29:16 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame^] | 307 | } // namespace implementation |
Tomasz Wasilczyk | 213170b | 2017-02-07 17:38:21 -0800 | [diff] [blame] | 308 | } // namespace V1_1 |
| 309 | } // namespace broadcastradio |
| 310 | } // namespace hardware |
| 311 | } // namespace android |