2017-02-08 02:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
#define LOG_TAG "BroadcastRadioDefault.tuner"
|
|
|
|
#define LOG_NDEBUG 0
|
2017-02-08 02:38:21 +01:00
|
|
|
|
|
|
|
#include "BroadcastRadio.h"
|
|
|
|
#include "Tuner.h"
|
2017-06-22 19:45:33 +02:00
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
#include <Utils.h>
|
2017-06-22 19:45:33 +02:00
|
|
|
#include <log/log.h>
|
2017-02-08 02:38:21 +01:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace hardware {
|
|
|
|
namespace broadcastradio {
|
|
|
|
namespace V1_1 {
|
|
|
|
namespace implementation {
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
using namespace std::chrono_literals;
|
2017-02-08 02:38:21 +01:00
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
using V1_0::Band;
|
|
|
|
using V1_0::BandConfig;
|
|
|
|
using V1_0::Direction;
|
|
|
|
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
using std::lock_guard;
|
|
|
|
using std::move;
|
|
|
|
using std::mutex;
|
|
|
|
using std::sort;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
milliseconds config = 50ms;
|
|
|
|
milliseconds scan = 200ms;
|
|
|
|
milliseconds step = 100ms;
|
|
|
|
milliseconds tune = 150ms;
|
|
|
|
} gDefaultDelay;
|
|
|
|
|
|
|
|
Tuner::Tuner(const sp<V1_0::ITunerCallback>& callback)
|
|
|
|
: mCallback(callback),
|
|
|
|
mCallback1_1(ITunerCallback::castFrom(callback).withDefault(nullptr)),
|
|
|
|
mVirtualFm(make_fm_radio()) {}
|
2017-02-08 02:38:21 +01:00
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
void Tuner::forceClose() {
|
|
|
|
lock_guard<mutex> lk(mMut);
|
|
|
|
mIsClosed = true;
|
|
|
|
mThread.cancelAll();
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
Return<Result> Tuner::setConfiguration(const BandConfig& config) {
|
|
|
|
ALOGV("%s", __func__);
|
2017-02-08 02:38:21 +01:00
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
if (config.lowerLimit >= config.upperLimit) return Result::INVALID_ARGUMENTS;
|
|
|
|
|
|
|
|
auto task = [this, config]() {
|
|
|
|
ALOGI("Setting AM/FM config");
|
|
|
|
lock_guard<mutex> lk(mMut);
|
|
|
|
|
|
|
|
mAmfmConfig = move(config);
|
|
|
|
mAmfmConfig.antennaConnected = true;
|
2017-07-05 20:23:30 +02:00
|
|
|
mCurrentProgram = utils::make_selector(mAmfmConfig.type, mAmfmConfig.lowerLimit);
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
mIsAmfmConfigSet = true;
|
|
|
|
mCallback->configChange(Result::OK, mAmfmConfig);
|
|
|
|
};
|
|
|
|
mThread.schedule(task, gDefaultDelay.config);
|
|
|
|
|
|
|
|
return Result::OK;
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
Return<void> Tuner::getConfiguration(getConfiguration_cb _hidl_cb) {
|
|
|
|
ALOGV("%s", __func__);
|
|
|
|
|
|
|
|
lock_guard<mutex> lk(mMut);
|
|
|
|
if (mIsAmfmConfigSet) {
|
|
|
|
_hidl_cb(Result::OK, mAmfmConfig);
|
|
|
|
} else {
|
|
|
|
_hidl_cb(Result::NOT_INITIALIZED, {});
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
2017-06-22 19:45:33 +02:00
|
|
|
return Void();
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
// makes ProgramInfo that points to no program
|
|
|
|
static ProgramInfo makeDummyProgramInfo(const ProgramSelector& selector) {
|
2017-06-22 19:45:33 +02:00
|
|
|
ProgramInfo info11 = {};
|
|
|
|
auto& info10 = info11.base;
|
|
|
|
|
2017-07-12 23:17:09 +02:00
|
|
|
utils::getLegacyChannel(selector, &info10.channel, &info10.subChannel);
|
2017-07-05 20:23:30 +02:00
|
|
|
info11.selector = selector;
|
2017-06-22 19:45:33 +02:00
|
|
|
info11.flags |= ProgramInfoFlags::MUTED;
|
|
|
|
|
|
|
|
return info11;
|
|
|
|
}
|
2017-02-08 02:38:21 +01:00
|
|
|
|
2017-07-07 22:28:00 +02:00
|
|
|
bool Tuner::isFmLocked() {
|
|
|
|
if (!utils::isAmFm(utils::getType(mCurrentProgram))) return false;
|
|
|
|
return mAmfmConfig.type == Band::FM_HD || mAmfmConfig.type == Band::FM;
|
|
|
|
}
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
void Tuner::tuneInternalLocked(const ProgramSelector& sel) {
|
2017-06-22 19:45:33 +02:00
|
|
|
VirtualRadio* virtualRadio = nullptr;
|
2017-07-07 22:28:00 +02:00
|
|
|
if (isFmLocked()) {
|
2017-06-22 19:45:33 +02:00
|
|
|
virtualRadio = &mVirtualFm;
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
2017-06-22 19:45:33 +02:00
|
|
|
|
|
|
|
VirtualProgram virtualProgram;
|
2017-07-05 20:23:30 +02:00
|
|
|
if (virtualRadio != nullptr && virtualRadio->getProgram(sel, virtualProgram)) {
|
|
|
|
mCurrentProgram = virtualProgram.selector;
|
2017-06-30 01:04:05 +02:00
|
|
|
mCurrentProgramInfo = static_cast<ProgramInfo>(virtualProgram);
|
2017-06-22 19:45:33 +02:00
|
|
|
} else {
|
2017-07-05 20:23:30 +02:00
|
|
|
mCurrentProgram = sel;
|
|
|
|
mCurrentProgramInfo = makeDummyProgramInfo(sel);
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
2017-06-22 19:45:33 +02:00
|
|
|
mIsTuneCompleted = true;
|
2017-02-08 02:38:21 +01:00
|
|
|
|
2017-06-30 01:04:05 +02:00
|
|
|
mCallback->tuneComplete(Result::OK, mCurrentProgramInfo.base);
|
2017-06-22 19:45:33 +02:00
|
|
|
if (mCallback1_1 != nullptr) {
|
2017-06-30 01:04:05 +02:00
|
|
|
mCallback1_1->tuneComplete_1_1(Result::OK, mCurrentProgramInfo);
|
2017-06-22 19:45:33 +02:00
|
|
|
}
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
Return<Result> Tuner::scan(Direction direction, bool skipSubChannel __unused) {
|
|
|
|
ALOGV("%s", __func__);
|
|
|
|
lock_guard<mutex> lk(mMut);
|
|
|
|
vector<VirtualProgram> list;
|
|
|
|
|
2017-07-07 22:28:00 +02:00
|
|
|
if (isFmLocked()) {
|
2017-06-22 19:45:33 +02:00
|
|
|
list = mVirtualFm.getProgramList();
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
|
2017-07-07 22:28:00 +02:00
|
|
|
if (list.empty()) {
|
2017-06-22 19:45:33 +02:00
|
|
|
mIsTuneCompleted = false;
|
|
|
|
auto task = [this, direction]() {
|
|
|
|
ALOGI("Performing failed scan %s", toString(direction).c_str());
|
|
|
|
|
|
|
|
mCallback->tuneComplete(Result::TIMEOUT, {});
|
|
|
|
if (mCallback1_1 != nullptr) {
|
|
|
|
mCallback1_1->tuneComplete_1_1(Result::TIMEOUT, {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
mThread.schedule(task, gDefaultDelay.scan);
|
|
|
|
|
|
|
|
return Result::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not optimal (O(sort) instead of O(n)), but not a big deal here;
|
|
|
|
// also, it's likely that list is already sorted (so O(n) anyway).
|
|
|
|
sort(list.begin(), list.end());
|
|
|
|
auto current = mCurrentProgram;
|
|
|
|
auto found = lower_bound(list.begin(), list.end(), VirtualProgram({current}));
|
|
|
|
if (direction == Direction::UP) {
|
|
|
|
if (found < list.end() - 1) {
|
2017-07-05 20:23:30 +02:00
|
|
|
if (utils::tunesTo(current, found->selector)) found++;
|
2017-06-22 19:45:33 +02:00
|
|
|
} else {
|
|
|
|
found = list.begin();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (found > list.begin() && found != list.end()) {
|
|
|
|
found--;
|
|
|
|
} else {
|
|
|
|
found = list.end() - 1;
|
|
|
|
}
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
2017-07-05 20:23:30 +02:00
|
|
|
auto tuneTo = found->selector;
|
2017-06-22 19:45:33 +02:00
|
|
|
|
|
|
|
mIsTuneCompleted = false;
|
|
|
|
auto task = [this, tuneTo, direction]() {
|
|
|
|
ALOGI("Performing scan %s", toString(direction).c_str());
|
|
|
|
|
|
|
|
lock_guard<mutex> lk(mMut);
|
2017-07-05 20:23:30 +02:00
|
|
|
tuneInternalLocked(tuneTo);
|
2017-06-22 19:45:33 +02:00
|
|
|
};
|
|
|
|
mThread.schedule(task, gDefaultDelay.scan);
|
|
|
|
|
|
|
|
return Result::OK;
|
|
|
|
}
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
Return<Result> Tuner::step(Direction direction, bool skipSubChannel) {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s", __func__);
|
2017-07-05 20:23:30 +02:00
|
|
|
ALOGW_IF(!skipSubChannel, "can't step to next frequency without ignoring subChannel");
|
2017-06-22 19:45:33 +02:00
|
|
|
|
|
|
|
lock_guard<mutex> lk(mMut);
|
2017-07-05 20:23:30 +02:00
|
|
|
|
|
|
|
if (!utils::isAmFm(utils::getType(mCurrentProgram))) {
|
|
|
|
ALOGE("Can't step in anything else than AM/FM");
|
|
|
|
return Result::NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGW_IF(!mIsAmfmConfigSet, "AM/FM config not set");
|
|
|
|
if (!mIsAmfmConfigSet) return Result::INVALID_STATE;
|
|
|
|
mIsTuneCompleted = false;
|
|
|
|
|
|
|
|
auto task = [this, direction]() {
|
|
|
|
ALOGI("Performing step %s", toString(direction).c_str());
|
|
|
|
|
|
|
|
lock_guard<mutex> lk(mMut);
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
auto current = utils::getId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY, 0);
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
if (direction == Direction::UP) {
|
2017-07-05 20:23:30 +02:00
|
|
|
current += mAmfmConfig.spacings[0];
|
2017-06-22 19:45:33 +02:00
|
|
|
} else {
|
2017-07-05 20:23:30 +02:00
|
|
|
current -= mAmfmConfig.spacings[0];
|
2017-06-22 19:45:33 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
if (current > mAmfmConfig.upperLimit) current = mAmfmConfig.lowerLimit;
|
|
|
|
if (current < mAmfmConfig.lowerLimit) current = mAmfmConfig.upperLimit;
|
2017-06-22 19:45:33 +02:00
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
tuneInternalLocked(utils::make_selector(mAmfmConfig.type, current));
|
2017-06-22 19:45:33 +02:00
|
|
|
};
|
|
|
|
mThread.schedule(task, gDefaultDelay.step);
|
|
|
|
|
|
|
|
return Result::OK;
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
Return<Result> Tuner::tune(uint32_t channel, uint32_t subChannel) {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s(%d, %d)", __func__, channel, subChannel);
|
2017-07-05 20:23:30 +02:00
|
|
|
Band band;
|
|
|
|
{
|
|
|
|
lock_guard<mutex> lk(mMut);
|
|
|
|
band = mAmfmConfig.type;
|
|
|
|
}
|
|
|
|
return tune_1_1(utils::make_selector(band, channel, subChannel));
|
|
|
|
}
|
|
|
|
|
|
|
|
Return<Result> Tuner::tune_1_1(const ProgramSelector& sel) {
|
|
|
|
ALOGV("%s(%s)", __func__, toString(sel).c_str());
|
2017-06-22 19:45:33 +02:00
|
|
|
|
|
|
|
lock_guard<mutex> lk(mMut);
|
2017-07-05 20:23:30 +02:00
|
|
|
|
|
|
|
if (utils::isAmFm(utils::getType(mCurrentProgram))) {
|
|
|
|
ALOGW_IF(!mIsAmfmConfigSet, "AM/FM config not set");
|
|
|
|
if (!mIsAmfmConfigSet) return Result::INVALID_STATE;
|
|
|
|
|
|
|
|
auto freq = utils::getId(sel, IdentifierType::AMFM_FREQUENCY);
|
|
|
|
if (freq < mAmfmConfig.lowerLimit || freq > mAmfmConfig.upperLimit) {
|
|
|
|
return Result::INVALID_ARGUMENTS;
|
|
|
|
}
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
2017-06-22 19:45:33 +02:00
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
mIsTuneCompleted = false;
|
|
|
|
auto task = [this, sel]() {
|
2017-06-22 19:45:33 +02:00
|
|
|
lock_guard<mutex> lk(mMut);
|
2017-07-05 20:23:30 +02:00
|
|
|
tuneInternalLocked(sel);
|
2017-06-22 19:45:33 +02:00
|
|
|
};
|
|
|
|
mThread.schedule(task, gDefaultDelay.tune);
|
|
|
|
|
|
|
|
return Result::OK;
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
Return<Result> Tuner::cancel() {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s", __func__);
|
|
|
|
mThread.cancelAll();
|
|
|
|
return Result::OK;
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
Return<void> Tuner::getProgramInformation(getProgramInformation_cb _hidl_cb) {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s", __func__);
|
2017-02-08 02:38:21 +01:00
|
|
|
return getProgramInformation_1_1([&](Result result, const ProgramInfo& info) {
|
|
|
|
_hidl_cb(result, info.base);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-05 20:23:30 +02:00
|
|
|
Return<void> Tuner::getProgramInformation_1_1(getProgramInformation_1_1_cb _hidl_cb) {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s", __func__);
|
2017-02-08 02:38:21 +01:00
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
lock_guard<mutex> lk(mMut);
|
|
|
|
if (mIsTuneCompleted) {
|
|
|
|
_hidl_cb(Result::OK, mCurrentProgramInfo);
|
|
|
|
} else {
|
|
|
|
_hidl_cb(Result::NOT_INITIALIZED, makeDummyProgramInfo(mCurrentProgram));
|
2017-02-08 02:38:21 +01:00
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2017-03-13 22:30:15 +01:00
|
|
|
Return<ProgramListResult> Tuner::startBackgroundScan() {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s", __func__);
|
2017-05-15 21:57:20 +02:00
|
|
|
return ProgramListResult::UNAVAILABLE;
|
2017-03-13 22:30:15 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 23:21:07 +01:00
|
|
|
Return<void> Tuner::getProgramList(const hidl_string& filter __unused, getProgramList_cb _hidl_cb) {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s", __func__);
|
2017-07-07 22:28:00 +02:00
|
|
|
lock_guard<mutex> lk(mMut);
|
2017-06-22 19:45:33 +02:00
|
|
|
|
|
|
|
auto& virtualRadio = mVirtualFm;
|
2017-07-07 22:28:00 +02:00
|
|
|
if (!isFmLocked()) {
|
|
|
|
ALOGI("bands other than FM are not supported yet");
|
2017-06-22 19:45:33 +02:00
|
|
|
_hidl_cb(ProgramListResult::OK, {});
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2017-06-30 01:04:05 +02:00
|
|
|
auto list = virtualRadio.getProgramList();
|
2017-07-07 22:28:00 +02:00
|
|
|
ALOGD("returning a list of %zu programs", list.size());
|
2017-06-30 01:04:05 +02:00
|
|
|
_hidl_cb(ProgramListResult::OK, vector<ProgramInfo>(list.begin(), list.end()));
|
2017-03-01 23:21:07 +01:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2017-03-27 23:29:16 +02:00
|
|
|
Return<void> Tuner::isAnalogForced(isAnalogForced_cb _hidl_cb) {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s", __func__);
|
|
|
|
// TODO(b/36864090): implement
|
2017-05-15 21:57:20 +02:00
|
|
|
_hidl_cb(Result::INVALID_STATE, false);
|
2017-03-27 23:29:16 +02:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
Return<Result> Tuner::setAnalogForced(bool isForced __unused) {
|
2017-06-22 19:45:33 +02:00
|
|
|
ALOGV("%s", __func__);
|
|
|
|
// TODO(b/36864090): implement
|
2017-05-15 21:57:20 +02:00
|
|
|
return Result::INVALID_STATE;
|
2017-03-27 23:29:16 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:45:33 +02:00
|
|
|
} // namespace implementation
|
2017-02-08 02:38:21 +01:00
|
|
|
} // namespace V1_1
|
|
|
|
} // namespace broadcastradio
|
|
|
|
} // namespace hardware
|
|
|
|
} // namespace android
|