blob: 3af560f62c906dc9004f4b5d90460b41cd27beb8 [file] [log] [blame]
Weilin Xub2a6ca62022-05-08 23:47:04 +00001/*
2 * Copyright (C) 2022 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#include "BroadcastRadio.h"
18#include <broadcastradio-utils-aidl/Utils.h>
Weilin Xu31c541c2023-09-07 17:00:57 -070019#include <broadcastradio-utils-aidl/UtilsV2.h>
Weilin Xub2a6ca62022-05-08 23:47:04 +000020#include "resources.h"
21
22#include <aidl/android/hardware/broadcastradio/IdentifierType.h>
23#include <aidl/android/hardware/broadcastradio/Result.h>
24
25#include <android-base/logging.h>
Weilin Xu97203b02022-06-23 00:19:03 +000026#include <android-base/strings.h>
27
28#include <private/android_filesystem_config.h>
Weilin Xub2a6ca62022-05-08 23:47:04 +000029
30namespace aidl::android::hardware::broadcastradio {
31
32using ::aidl::android::hardware::broadcastradio::utils::resultToInt;
33using ::aidl::android::hardware::broadcastradio::utils::tunesTo;
Weilin Xu97203b02022-06-23 00:19:03 +000034using ::android::base::EqualsIgnoreCase;
Weilin Xub2a6ca62022-05-08 23:47:04 +000035using ::ndk::ScopedAStatus;
36using ::std::literals::chrono_literals::operator""ms;
37using ::std::literals::chrono_literals::operator""s;
38using ::std::lock_guard;
39using ::std::mutex;
40using ::std::string;
41using ::std::vector;
42
43namespace {
44
45inline constexpr std::chrono::milliseconds kSeekDelayTimeMs = 200ms;
46inline constexpr std::chrono::milliseconds kStepDelayTimeMs = 100ms;
47inline constexpr std::chrono::milliseconds kTuneDelayTimeMs = 150ms;
48inline constexpr std::chrono::seconds kListDelayTimeS = 1s;
49
50// clang-format off
Weilin Xu48338962023-11-03 14:31:15 -070051const AmFmBandRange kFmFullBandRange = {65000, 108000, 10, 0};
52const AmFmBandRange kAmFullBandRange = {150, 30000, 1, 0};
Weilin Xub2a6ca62022-05-08 23:47:04 +000053const AmFmRegionConfig kDefaultAmFmConfig = {
54 {
55 {87500, 108000, 100, 100}, // FM
56 {153, 282, 3, 9}, // AM LW
57 {531, 1620, 9, 9}, // AM MW
58 {1600, 30000, 1, 5}, // AM SW
59 },
60 AmFmRegionConfig::DEEMPHASIS_D50,
61 AmFmRegionConfig::RDS};
62// clang-format on
63
64Properties initProperties(const VirtualRadio& virtualRadio) {
65 Properties prop = {};
66
67 prop.maker = "Android";
68 prop.product = virtualRadio.getName();
69 prop.supportedIdentifierTypes = vector<IdentifierType>({
70 IdentifierType::AMFM_FREQUENCY_KHZ,
71 IdentifierType::RDS_PI,
72 IdentifierType::HD_STATION_ID_EXT,
73 IdentifierType::DAB_SID_EXT,
74 });
75 prop.vendorInfo = vector<VendorKeyValue>({
76 {"com.android.sample", "sample"},
77 });
78
79 return prop;
80}
81
Weilin Xu48338962023-11-03 14:31:15 -070082bool isDigitalProgramAllowed(const ProgramSelector& sel, bool forceAnalogFm, bool forceAnalogAm) {
83 if (sel.primaryId.type != IdentifierType::HD_STATION_ID_EXT) {
84 return true;
85 }
86 int32_t freq = static_cast<int32_t>(utils::getAmFmFrequency(sel));
87 bool isFm = freq >= kFmFullBandRange.lowerBound && freq <= kFmFullBandRange.upperBound;
88 return isFm ? !forceAnalogFm : !forceAnalogAm;
89}
90
91/**
92 * Checks whether a program selector is in the current band.
93 *
94 * <p>For an AM/FM program, this method checks whether it is in the current AM/FM band. For a
95 * program selector is also an HD program, it is also checked whether HD radio is enabled in the
96 * current AM/FM band. For a non-AM/FM program, the method will returns {@code true} directly.
97 * @param sel Program selector to be checked
98 * @param currentAmFmBandRange the current AM/FM band
99 * @param forceAnalogFm whether FM band is forced to be analog
100 * @param forceAnalogAm whether AM band is forced to be analog
101 * @return whether the program selector is in the current band if it is an AM/FM (including HD)
102 * selector, {@code true} otherwise
103 */
104bool isProgramInBand(const ProgramSelector& sel,
105 const std::optional<AmFmBandRange>& currentAmFmBandRange, bool forceAnalogFm,
106 bool forceAnalogAm) {
107 if (!utils::hasAmFmFrequency(sel)) {
108 return true;
109 }
110 if (!currentAmFmBandRange.has_value()) {
111 return false;
112 }
113 int32_t freq = static_cast<int32_t>(utils::getAmFmFrequency(sel));
114 if (freq < currentAmFmBandRange->lowerBound || freq > currentAmFmBandRange->upperBound) {
115 return false;
116 }
117 return isDigitalProgramAllowed(sel, forceAnalogFm, forceAnalogAm);
118}
119
Weilin Xub2a6ca62022-05-08 23:47:04 +0000120// Makes ProgramInfo that does not point to any particular program
121ProgramInfo makeSampleProgramInfo(const ProgramSelector& selector) {
122 ProgramInfo info = {};
123 info.selector = selector;
Weilin Xu39dd0f82023-09-07 17:00:57 -0700124 switch (info.selector.primaryId.type) {
125 case IdentifierType::AMFM_FREQUENCY_KHZ:
126 info.logicallyTunedTo = utils::makeIdentifier(
127 IdentifierType::AMFM_FREQUENCY_KHZ,
128 utils::getId(selector, IdentifierType::AMFM_FREQUENCY_KHZ));
129 info.physicallyTunedTo = info.logicallyTunedTo;
130 break;
131 case IdentifierType::HD_STATION_ID_EXT:
132 info.logicallyTunedTo = utils::makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ,
133 utils::getAmFmFrequency(info.selector));
134 info.physicallyTunedTo = info.logicallyTunedTo;
135 break;
136 case IdentifierType::DAB_SID_EXT:
137 info.logicallyTunedTo = info.selector.primaryId;
138 info.physicallyTunedTo = utils::makeIdentifier(
139 IdentifierType::DAB_FREQUENCY_KHZ,
140 utils::getId(selector, IdentifierType::DAB_FREQUENCY_KHZ));
141 break;
142 default:
143 info.logicallyTunedTo = info.selector.primaryId;
144 info.physicallyTunedTo = info.logicallyTunedTo;
145 break;
146 }
Weilin Xub2a6ca62022-05-08 23:47:04 +0000147 return info;
148}
149
Weilin Xu97203b02022-06-23 00:19:03 +0000150static bool checkDumpCallerHasWritePermissions(int fd) {
151 uid_t uid = AIBinder_getCallingUid();
152 if (uid == AID_ROOT || uid == AID_SHELL || uid == AID_SYSTEM) {
153 return true;
154 }
155 dprintf(fd, "BroadcastRadio HAL dump must be root, shell or system\n");
156 return false;
157}
158
Weilin Xub2a6ca62022-05-08 23:47:04 +0000159} // namespace
160
161BroadcastRadio::BroadcastRadio(const VirtualRadio& virtualRadio)
162 : mVirtualRadio(virtualRadio),
163 mAmFmConfig(kDefaultAmFmConfig),
164 mProperties(initProperties(virtualRadio)) {
165 const auto& ranges = kDefaultAmFmConfig.ranges;
166 if (ranges.size() > 0) {
167 ProgramSelector sel = utils::makeSelectorAmfm(ranges[0].lowerBound);
168 VirtualProgram virtualProgram = {};
169 if (mVirtualRadio.getProgram(sel, &virtualProgram)) {
170 mCurrentProgram = virtualProgram.selector;
171 } else {
172 mCurrentProgram = sel;
173 }
Weilin Xu48338962023-11-03 14:31:15 -0700174 adjustAmFmRangeLocked();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000175 }
176}
177
178BroadcastRadio::~BroadcastRadio() {
Weilin Xu764fe0d2023-07-26 18:07:05 +0000179 mTuningThread.reset();
180 mProgramListThread.reset();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000181}
182
183ScopedAStatus BroadcastRadio::getAmFmRegionConfig(bool full, AmFmRegionConfig* returnConfigs) {
184 if (full) {
185 *returnConfigs = {};
186 returnConfigs->ranges = vector<AmFmBandRange>({
Weilin Xu48338962023-11-03 14:31:15 -0700187 kFmFullBandRange,
188 kAmFullBandRange,
Weilin Xub2a6ca62022-05-08 23:47:04 +0000189 });
190 returnConfigs->fmDeemphasis =
191 AmFmRegionConfig::DEEMPHASIS_D50 | AmFmRegionConfig::DEEMPHASIS_D75;
192 returnConfigs->fmRds = AmFmRegionConfig::RDS | AmFmRegionConfig::RBDS;
193 return ScopedAStatus::ok();
194 }
195 lock_guard<mutex> lk(mMutex);
196 *returnConfigs = mAmFmConfig;
197 return ScopedAStatus::ok();
198}
199
200ScopedAStatus BroadcastRadio::getDabRegionConfig(vector<DabTableEntry>* returnConfigs) {
201 *returnConfigs = {
202 {"5A", 174928}, {"7D", 194064}, {"8A", 195936}, {"8B", 197648}, {"9A", 202928},
203 {"9B", 204640}, {"9C", 206352}, {"10B", 211648}, {"10C", 213360}, {"10D", 215072},
204 {"11A", 216928}, {"11B", 218640}, {"11C", 220352}, {"11D", 222064}, {"12A", 223936},
205 {"12B", 225648}, {"12C", 227360}, {"12D", 229072},
206 };
207 return ScopedAStatus::ok();
208}
209
210ScopedAStatus BroadcastRadio::getImage(int32_t id, vector<uint8_t>* returnImage) {
211 LOG(DEBUG) << __func__ << ": fetching image " << std::hex << id;
212
213 if (id == resources::kDemoPngId) {
214 *returnImage = vector<uint8_t>(resources::kDemoPng, std::end(resources::kDemoPng));
215 return ScopedAStatus::ok();
216 }
217
218 LOG(WARNING) << __func__ << ": image of id " << std::hex << id << " doesn't exist";
219 *returnImage = {};
220 return ScopedAStatus::ok();
221}
222
223ScopedAStatus BroadcastRadio::getProperties(Properties* returnProperties) {
224 lock_guard<mutex> lk(mMutex);
225 *returnProperties = mProperties;
226 return ScopedAStatus::ok();
227}
228
229ProgramInfo BroadcastRadio::tuneInternalLocked(const ProgramSelector& sel) {
230 LOG(DEBUG) << __func__ << ": tune (internal) to " << sel.toString();
231
232 VirtualProgram virtualProgram = {};
233 ProgramInfo programInfo;
Weilin Xu48338962023-11-03 14:31:15 -0700234 bool isProgramAllowed =
235 isDigitalProgramAllowed(sel, isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_FM),
236 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_AM));
237 if (isProgramAllowed && mVirtualRadio.getProgram(sel, &virtualProgram)) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000238 mCurrentProgram = virtualProgram.selector;
239 programInfo = virtualProgram;
240 } else {
Weilin Xu48338962023-11-03 14:31:15 -0700241 if (!isProgramAllowed) {
242 mCurrentProgram = utils::makeSelectorAmfm(utils::getAmFmFrequency(sel));
243 } else {
244 mCurrentProgram = sel;
245 }
Weilin Xub2a6ca62022-05-08 23:47:04 +0000246 programInfo = makeSampleProgramInfo(sel);
247 }
Weilin Xu79919962023-11-06 19:11:02 -0800248 programInfo.infoFlags |= ProgramInfo::FLAG_SIGNAL_ACQUISITION;
249 if (programInfo.selector.primaryId.type != IdentifierType::HD_STATION_ID_EXT) {
250 mIsTuneCompleted = true;
251 }
Weilin Xu48338962023-11-03 14:31:15 -0700252 if (adjustAmFmRangeLocked()) {
253 startProgramListUpdatesLocked({});
254 }
Weilin Xub2a6ca62022-05-08 23:47:04 +0000255
256 return programInfo;
257}
258
259ScopedAStatus BroadcastRadio::setTunerCallback(const std::shared_ptr<ITunerCallback>& callback) {
260 LOG(DEBUG) << __func__ << ": setTunerCallback";
261
262 if (callback == nullptr) {
263 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
264 resultToInt(Result::INVALID_ARGUMENTS), "cannot set tuner callback to null");
265 }
266
267 lock_guard<mutex> lk(mMutex);
268 mCallback = callback;
269
270 return ScopedAStatus::ok();
271}
272
273ScopedAStatus BroadcastRadio::unsetTunerCallback() {
274 LOG(DEBUG) << __func__ << ": unsetTunerCallback";
275
276 lock_guard<mutex> lk(mMutex);
277 mCallback = nullptr;
278
279 return ScopedAStatus::ok();
280}
281
Weilin Xu79919962023-11-06 19:11:02 -0800282void BroadcastRadio::handleProgramInfoUpdateRadioCallback(
283 ProgramInfo programInfo, const std::shared_ptr<ITunerCallback>& callback) {
284 callback->onCurrentProgramInfoChanged(programInfo);
285 if (programInfo.selector.primaryId.type != IdentifierType::HD_STATION_ID_EXT) {
286 return;
287 }
288 ProgramSelector sel = programInfo.selector;
289 auto cancelTask = [sel, callback]() { callback->onTuneFailed(Result::CANCELED, sel); };
290 programInfo.infoFlags |= ProgramInfo::FLAG_HD_SIS_ACQUISITION;
291 auto sisAcquiredTask = [this, callback, programInfo, cancelTask]() {
292 callback->onCurrentProgramInfoChanged(programInfo);
293 auto audioAcquiredTask = [this, callback, programInfo]() {
294 ProgramInfo hdProgramInfoWithAudio = programInfo;
295 hdProgramInfoWithAudio.infoFlags |= ProgramInfo::FLAG_HD_AUDIO_ACQUISITION;
296 callback->onCurrentProgramInfoChanged(hdProgramInfoWithAudio);
297 lock_guard<mutex> lk(mMutex);
298 mIsTuneCompleted = true;
299 };
300 lock_guard<mutex> lk(mMutex);
301 mTuningThread->schedule(audioAcquiredTask, cancelTask, kTuneDelayTimeMs);
302 };
303
304 lock_guard<mutex> lk(mMutex);
305 mTuningThread->schedule(sisAcquiredTask, cancelTask, kTuneDelayTimeMs);
306}
307
Weilin Xub2a6ca62022-05-08 23:47:04 +0000308ScopedAStatus BroadcastRadio::tune(const ProgramSelector& program) {
309 LOG(DEBUG) << __func__ << ": tune to " << program.toString() << "...";
310
311 lock_guard<mutex> lk(mMutex);
312 if (mCallback == nullptr) {
313 LOG(ERROR) << __func__ << ": callback is not registered.";
314 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
315 resultToInt(Result::INVALID_STATE), "callback is not registered");
316 }
317
318 if (!utils::isSupported(mProperties, program)) {
319 LOG(WARNING) << __func__ << ": selector not supported: " << program.toString();
320 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
321 resultToInt(Result::NOT_SUPPORTED), "selector is not supported");
322 }
323
Weilin Xu31c541c2023-09-07 17:00:57 -0700324 if (!utils::isValidV2(program)) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000325 LOG(ERROR) << __func__ << ": selector is not valid: " << program.toString();
326 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
327 resultToInt(Result::INVALID_ARGUMENTS), "selector is not valid");
328 }
329
330 cancelLocked();
331
332 mIsTuneCompleted = false;
333 std::shared_ptr<ITunerCallback> callback = mCallback;
334 auto task = [this, program, callback]() {
335 ProgramInfo programInfo = {};
336 {
337 lock_guard<mutex> lk(mMutex);
338 programInfo = tuneInternalLocked(program);
339 }
Weilin Xu79919962023-11-06 19:11:02 -0800340 handleProgramInfoUpdateRadioCallback(programInfo, callback);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000341 };
Weilin Xud7483322022-11-29 01:12:36 +0000342 auto cancelTask = [program, callback]() { callback->onTuneFailed(Result::CANCELED, program); };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000343 mTuningThread->schedule(task, cancelTask, kTuneDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000344
345 return ScopedAStatus::ok();
346}
347
Weilin Xu39dd0f82023-09-07 17:00:57 -0700348bool BroadcastRadio::findNextLocked(const ProgramSelector& current, bool directionUp,
349 bool skipSubChannel, VirtualProgram* nextProgram) const {
350 if (mProgramList.empty()) {
351 return false;
352 }
353 // The list is not sorted here since it has already stored in VirtualRadio.
354 bool hasAmFmFrequency = utils::hasAmFmFrequency(current);
355 uint32_t currentFreq = hasAmFmFrequency ? utils::getAmFmFrequency(current) : 0;
356 auto found =
357 std::lower_bound(mProgramList.begin(), mProgramList.end(), VirtualProgram({current}));
358 if (directionUp) {
359 if (found < mProgramList.end() - 1) {
360 // When seeking up, tuner will jump to the first selector which is main program service
Weilin Xu48338962023-11-03 14:31:15 -0700361 // greater than and of the same band as the current program selector in the program
362 // list (if not exist, jump to the first selector in the same band) for skipping
363 // sub-channels case or AM/FM without HD radio enabled case. Otherwise, the tuner will
364 // jump to the first selector which is greater than and of the same band as the current
365 // program selector.
Weilin Xu39dd0f82023-09-07 17:00:57 -0700366 if (utils::tunesTo(current, found->selector)) found++;
367 if (skipSubChannel && hasAmFmFrequency) {
368 auto firstFound = found;
369 while (utils::getAmFmFrequency(found->selector) == currentFreq) {
370 if (found < mProgramList.end() - 1) {
371 found++;
372 } else {
373 found = mProgramList.begin();
374 }
375 if (found == firstFound) {
376 // Only one main channel exists in the program list, the tuner cannot skip
377 // sub-channel to the next program selector.
378 return false;
379 }
380 }
381 }
382 } else {
Weilin Xu48338962023-11-03 14:31:15 -0700383 // If the selector of current program is no less than all selectors of the same band or
384 // not found in the program list, seeking up should wrap the tuner to the first program
385 // selector of the same band in the program list.
Weilin Xu39dd0f82023-09-07 17:00:57 -0700386 found = mProgramList.begin();
387 }
388 } else {
389 if (found > mProgramList.begin() && found != mProgramList.end()) {
390 // When seeking down, tuner will jump to the first selector which is main program
Weilin Xu48338962023-11-03 14:31:15 -0700391 // service less than and of the same band as the current program selector in the
392 // program list (if not exist, jump to the last main program service selector of the
393 // same band) for skipping sub-channels case or AM/FM without HD radio enabled case.
394 // Otherwise, the tuner will jump to the first selector less than and of the same band
395 // as the current program selector.
Weilin Xu39dd0f82023-09-07 17:00:57 -0700396 found--;
397 if (hasAmFmFrequency && utils::hasAmFmFrequency(found->selector)) {
398 uint32_t nextFreq = utils::getAmFmFrequency(found->selector);
399 if (nextFreq != currentFreq) {
400 jumpToFirstSubChannelLocked(found);
401 } else if (skipSubChannel) {
402 jumpToFirstSubChannelLocked(found);
403 auto firstFound = found;
404 if (found > mProgramList.begin()) {
405 found--;
406 } else {
407 found = mProgramList.end() - 1;
408 }
409 jumpToFirstSubChannelLocked(found);
410 if (found == firstFound) {
411 // Only one main channel exists in the program list, the tuner cannot skip
412 // sub-channel to the next program selector.
413 return false;
414 }
415 }
416 }
417 } else {
Weilin Xu48338962023-11-03 14:31:15 -0700418 // If the selector of current program is no greater than all selectors of the same band
419 // or not found in the program list, seeking down should wrap the tuner to the last
420 // selector of the same band in the program list. If the last program selector in the
421 // program list is sub-channel and skipping sub-channels is needed, the tuner will jump
422 // to the last main program service of the same band in the program list.
Weilin Xu39dd0f82023-09-07 17:00:57 -0700423 found = mProgramList.end() - 1;
424 jumpToFirstSubChannelLocked(found);
425 }
426 }
427 *nextProgram = *found;
428 return true;
429}
430
431void BroadcastRadio::jumpToFirstSubChannelLocked(vector<VirtualProgram>::const_iterator& it) const {
432 if (!utils::hasAmFmFrequency(it->selector) || it == mProgramList.begin()) {
433 return;
434 }
435 uint32_t currentFrequency = utils::getAmFmFrequency(it->selector);
436 it--;
437 while (it != mProgramList.begin() && utils::hasAmFmFrequency(it->selector) &&
438 utils::getAmFmFrequency(it->selector) == currentFrequency) {
439 it--;
440 }
441 it++;
442}
443
Weilin Xub2a6ca62022-05-08 23:47:04 +0000444ScopedAStatus BroadcastRadio::seek(bool directionUp, bool skipSubChannel) {
445 LOG(DEBUG) << __func__ << ": seek " << (directionUp ? "up" : "down") << " with skipSubChannel? "
446 << (skipSubChannel ? "yes" : "no") << "...";
447
448 lock_guard<mutex> lk(mMutex);
449 if (mCallback == nullptr) {
450 LOG(ERROR) << __func__ << ": callback is not registered.";
451 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
452 resultToInt(Result::INVALID_STATE), "callback is not registered");
453 }
454
455 cancelLocked();
456
Weilin Xu48338962023-11-03 14:31:15 -0700457 auto filterCb = [this](const VirtualProgram& program) {
458 return isProgramInBand(program.selector, mCurrentAmFmBandRange,
459 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_FM),
460 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_AM));
461 };
462 const auto& list = mVirtualRadio.getProgramList();
463 mProgramList.clear();
464 std::copy_if(list.begin(), list.end(), std::back_inserter(mProgramList), filterCb);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000465 std::shared_ptr<ITunerCallback> callback = mCallback;
Weilin Xud7483322022-11-29 01:12:36 +0000466 auto cancelTask = [callback]() { callback->onTuneFailed(Result::CANCELED, {}); };
Weilin Xu39dd0f82023-09-07 17:00:57 -0700467
468 VirtualProgram nextProgram = {};
469 bool foundNext = findNextLocked(mCurrentProgram, directionUp, skipSubChannel, &nextProgram);
470 mIsTuneCompleted = false;
471 if (!foundNext) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000472 auto task = [callback]() {
473 LOG(DEBUG) << "seek: program list is empty, seek couldn't stop";
474
475 callback->onTuneFailed(Result::TIMEOUT, {});
476 };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000477 mTuningThread->schedule(task, cancelTask, kSeekDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000478
479 return ScopedAStatus::ok();
480 }
481
Weilin Xu39dd0f82023-09-07 17:00:57 -0700482 auto task = [this, nextProgram, callback]() {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000483 ProgramInfo programInfo = {};
484 {
485 lock_guard<mutex> lk(mMutex);
Weilin Xu39dd0f82023-09-07 17:00:57 -0700486 programInfo = tuneInternalLocked(nextProgram.selector);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000487 }
Weilin Xu79919962023-11-06 19:11:02 -0800488 handleProgramInfoUpdateRadioCallback(programInfo, callback);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000489 };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000490 mTuningThread->schedule(task, cancelTask, kSeekDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000491
492 return ScopedAStatus::ok();
493}
494
495ScopedAStatus BroadcastRadio::step(bool directionUp) {
496 LOG(DEBUG) << __func__ << ": step " << (directionUp ? "up" : "down") << "...";
497
498 lock_guard<mutex> lk(mMutex);
499 if (mCallback == nullptr) {
500 LOG(ERROR) << __func__ << ": callback is not registered.";
501 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
502 resultToInt(Result::INVALID_STATE), "callback is not registered");
503 }
504
505 cancelLocked();
506
Weilin Xu39dd0f82023-09-07 17:00:57 -0700507 int64_t stepTo;
508 if (utils::hasId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ)) {
509 stepTo = utils::getId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ);
510 } else if (mCurrentProgram.primaryId.type == IdentifierType::HD_STATION_ID_EXT) {
511 stepTo = utils::getHdFrequency(mCurrentProgram);
512 } else {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000513 LOG(WARNING) << __func__ << ": can't step in anything else than AM/FM";
514 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
515 resultToInt(Result::NOT_SUPPORTED), "cannot step in anything else than AM/FM");
516 }
517
Weilin Xu48338962023-11-03 14:31:15 -0700518 if (!mCurrentAmFmBandRange.has_value()) {
519 LOG(ERROR) << __func__ << ": can't find current band";
Weilin Xuee546a62023-11-14 12:57:50 -0800520 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
Weilin Xu48338962023-11-03 14:31:15 -0700521 resultToInt(Result::INTERNAL_ERROR), "can't find current band");
Weilin Xub2a6ca62022-05-08 23:47:04 +0000522 }
523
524 if (directionUp) {
Weilin Xu48338962023-11-03 14:31:15 -0700525 stepTo += mCurrentAmFmBandRange->spacing;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000526 } else {
Weilin Xu48338962023-11-03 14:31:15 -0700527 stepTo -= mCurrentAmFmBandRange->spacing;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000528 }
Weilin Xu48338962023-11-03 14:31:15 -0700529 if (stepTo > mCurrentAmFmBandRange->upperBound) {
530 stepTo = mCurrentAmFmBandRange->lowerBound;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000531 }
Weilin Xu48338962023-11-03 14:31:15 -0700532 if (stepTo < mCurrentAmFmBandRange->lowerBound) {
533 stepTo = mCurrentAmFmBandRange->upperBound;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000534 }
535
536 mIsTuneCompleted = false;
537 std::shared_ptr<ITunerCallback> callback = mCallback;
538 auto task = [this, stepTo, callback]() {
539 ProgramInfo programInfo;
540 {
541 lock_guard<mutex> lk(mMutex);
542 programInfo = tuneInternalLocked(utils::makeSelectorAmfm(stepTo));
543 }
Weilin Xu79919962023-11-06 19:11:02 -0800544 handleProgramInfoUpdateRadioCallback(programInfo, callback);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000545 };
Weilin Xud7483322022-11-29 01:12:36 +0000546 auto cancelTask = [callback]() { callback->onTuneFailed(Result::CANCELED, {}); };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000547 mTuningThread->schedule(task, cancelTask, kStepDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000548
549 return ScopedAStatus::ok();
550}
551
552void BroadcastRadio::cancelLocked() {
Weilin Xu764fe0d2023-07-26 18:07:05 +0000553 LOG(DEBUG) << __func__ << ": cancelling current tuning operations...";
Weilin Xub2a6ca62022-05-08 23:47:04 +0000554
Weilin Xu764fe0d2023-07-26 18:07:05 +0000555 mTuningThread->cancelAll();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000556 if (mCurrentProgram.primaryId.type != IdentifierType::INVALID) {
557 mIsTuneCompleted = true;
558 }
559}
560
561ScopedAStatus BroadcastRadio::cancel() {
562 LOG(DEBUG) << __func__ << ": cancel pending tune, seek and step...";
563
564 lock_guard<mutex> lk(mMutex);
565 cancelLocked();
566
567 return ScopedAStatus::ok();
568}
569
Weilin Xu48338962023-11-03 14:31:15 -0700570void BroadcastRadio::startProgramListUpdatesLocked(const ProgramFilter& filter) {
571 auto filterCb = [&filter, this](const VirtualProgram& program) {
572 return utils::satisfies(filter, program.selector) &&
573 isProgramInBand(program.selector, mCurrentAmFmBandRange,
574 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_FM),
575 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_AM));
Weilin Xub2a6ca62022-05-08 23:47:04 +0000576 };
577
Weilin Xu764fe0d2023-07-26 18:07:05 +0000578 cancelProgramListUpdateLocked();
579
Weilin Xub2a6ca62022-05-08 23:47:04 +0000580 const auto& list = mVirtualRadio.getProgramList();
581 vector<VirtualProgram> filteredList;
582 std::copy_if(list.begin(), list.end(), std::back_inserter(filteredList), filterCb);
583
584 auto task = [this, filteredList]() {
585 std::shared_ptr<ITunerCallback> callback;
586 {
587 lock_guard<mutex> lk(mMutex);
588 if (mCallback == nullptr) {
589 LOG(WARNING) << "Callback is null when updating program List";
590 return;
591 }
592 callback = mCallback;
593 }
594
595 ProgramListChunk chunk = {};
596 chunk.purge = true;
597 chunk.complete = true;
598 chunk.modified = vector<ProgramInfo>(filteredList.begin(), filteredList.end());
599
600 callback->onProgramListUpdated(chunk);
601 };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000602 mProgramListThread->schedule(task, kListDelayTimeS);
Weilin Xu48338962023-11-03 14:31:15 -0700603}
604
605ScopedAStatus BroadcastRadio::startProgramListUpdates(const ProgramFilter& filter) {
606 LOG(DEBUG) << __func__ << ": requested program list updates, filter = " << filter.toString()
607 << "...";
608
609 lock_guard<mutex> lk(mMutex);
610
611 startProgramListUpdatesLocked(filter);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000612
613 return ScopedAStatus::ok();
614}
615
Weilin Xu764fe0d2023-07-26 18:07:05 +0000616void BroadcastRadio::cancelProgramListUpdateLocked() {
617 LOG(DEBUG) << __func__ << ": cancelling current program list update operations...";
618 mProgramListThread->cancelAll();
619}
620
Weilin Xub2a6ca62022-05-08 23:47:04 +0000621ScopedAStatus BroadcastRadio::stopProgramListUpdates() {
622 LOG(DEBUG) << __func__ << ": requested program list updates to stop...";
Weilin Xu764fe0d2023-07-26 18:07:05 +0000623 lock_guard<mutex> lk(mMutex);
624 cancelProgramListUpdateLocked();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000625 return ScopedAStatus::ok();
626}
627
Weilin Xu48338962023-11-03 14:31:15 -0700628bool BroadcastRadio::isConfigFlagSetLocked(ConfigFlag flag) const {
629 int flagBit = static_cast<int>(flag);
630 return ((mConfigFlagValues >> flagBit) & 1) == 1;
631}
632
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000633ScopedAStatus BroadcastRadio::isConfigFlagSet(ConfigFlag flag, bool* returnIsSet) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000634 LOG(DEBUG) << __func__ << ": flag = " << toString(flag);
635
Weilin Xu48338962023-11-03 14:31:15 -0700636 if (flag == ConfigFlag::FORCE_ANALOG) {
637 flag = ConfigFlag::FORCE_ANALOG_FM;
638 }
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000639 lock_guard<mutex> lk(mMutex);
Weilin Xu48338962023-11-03 14:31:15 -0700640 *returnIsSet = isConfigFlagSetLocked(flag);
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000641 return ScopedAStatus::ok();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000642}
643
644ScopedAStatus BroadcastRadio::setConfigFlag(ConfigFlag flag, bool value) {
645 LOG(DEBUG) << __func__ << ": flag = " << toString(flag) << ", value = " << value;
646
Weilin Xu48338962023-11-03 14:31:15 -0700647 if (flag == ConfigFlag::FORCE_ANALOG) {
648 flag = ConfigFlag::FORCE_ANALOG_FM;
649 }
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000650 int flagBitMask = 1 << (static_cast<int>(flag));
651 lock_guard<mutex> lk(mMutex);
652 if (value) {
653 mConfigFlagValues |= flagBitMask;
654 } else {
655 mConfigFlagValues &= ~flagBitMask;
656 }
Weilin Xu48338962023-11-03 14:31:15 -0700657 if (flag == ConfigFlag::FORCE_ANALOG_AM || flag == ConfigFlag::FORCE_ANALOG_FM) {
658 startProgramListUpdatesLocked({});
659 }
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000660 return ScopedAStatus::ok();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000661}
662
663ScopedAStatus BroadcastRadio::setParameters(
664 [[maybe_unused]] const vector<VendorKeyValue>& parameters,
665 vector<VendorKeyValue>* returnParameters) {
666 // TODO(b/243682330) Support vendor parameter functionality
667 *returnParameters = {};
668 return ScopedAStatus::ok();
669}
670
671ScopedAStatus BroadcastRadio::getParameters([[maybe_unused]] const vector<string>& keys,
672 vector<VendorKeyValue>* returnParameters) {
673 // TODO(b/243682330) Support vendor parameter functionality
674 *returnParameters = {};
675 return ScopedAStatus::ok();
676}
677
Weilin Xu48338962023-11-03 14:31:15 -0700678bool BroadcastRadio::adjustAmFmRangeLocked() {
679 bool hasBandBefore = mCurrentAmFmBandRange.has_value();
Weilin Xu39dd0f82023-09-07 17:00:57 -0700680 if (!utils::hasAmFmFrequency(mCurrentProgram)) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000681 LOG(WARNING) << __func__ << ": current program does not has AMFM_FREQUENCY_KHZ identifier";
Weilin Xu48338962023-11-03 14:31:15 -0700682 mCurrentAmFmBandRange.reset();
683 return hasBandBefore;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000684 }
685
Weilin Xu48338962023-11-03 14:31:15 -0700686 int32_t freq = static_cast<int32_t>(utils::getAmFmFrequency(mCurrentProgram));
Weilin Xub2a6ca62022-05-08 23:47:04 +0000687 for (const auto& range : mAmFmConfig.ranges) {
688 if (range.lowerBound <= freq && range.upperBound >= freq) {
Weilin Xu48338962023-11-03 14:31:15 -0700689 bool isBandChanged = hasBandBefore ? *mCurrentAmFmBandRange != range : true;
690 mCurrentAmFmBandRange = range;
691 return isBandChanged;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000692 }
693 }
694
Weilin Xu48338962023-11-03 14:31:15 -0700695 mCurrentAmFmBandRange.reset();
696 return !hasBandBefore;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000697}
698
699ScopedAStatus BroadcastRadio::registerAnnouncementListener(
700 [[maybe_unused]] const std::shared_ptr<IAnnouncementListener>& listener,
701 const vector<AnnouncementType>& enabled, std::shared_ptr<ICloseHandle>* returnCloseHandle) {
702 LOG(DEBUG) << __func__ << ": registering announcement listener for "
703 << utils::vectorToString(enabled);
704
705 // TODO(b/243683842) Support announcement listener
706 *returnCloseHandle = nullptr;
707 LOG(INFO) << __func__ << ": registering announcementListener is not supported";
708 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
709 resultToInt(Result::NOT_SUPPORTED),
710 "registering announcementListener is not supported");
711}
712
Weilin Xu97203b02022-06-23 00:19:03 +0000713binder_status_t BroadcastRadio::dump(int fd, const char** args, uint32_t numArgs) {
714 if (numArgs == 0) {
715 return dumpsys(fd);
716 }
717
718 string option = string(args[0]);
719 if (EqualsIgnoreCase(option, "--help")) {
720 return cmdHelp(fd);
721 } else if (EqualsIgnoreCase(option, "--tune")) {
722 return cmdTune(fd, args, numArgs);
723 } else if (EqualsIgnoreCase(option, "--seek")) {
724 return cmdSeek(fd, args, numArgs);
725 } else if (EqualsIgnoreCase(option, "--step")) {
726 return cmdStep(fd, args, numArgs);
727 } else if (EqualsIgnoreCase(option, "--cancel")) {
728 return cmdCancel(fd, numArgs);
729 } else if (EqualsIgnoreCase(option, "--startProgramListUpdates")) {
730 return cmdStartProgramListUpdates(fd, args, numArgs);
731 } else if (EqualsIgnoreCase(option, "--stopProgramListUpdates")) {
732 return cmdStopProgramListUpdates(fd, numArgs);
733 }
734 dprintf(fd, "Invalid option: %s\n", option.c_str());
735 return STATUS_BAD_VALUE;
736}
737
738binder_status_t BroadcastRadio::dumpsys(int fd) {
739 if (!checkDumpCallerHasWritePermissions(fd)) {
740 return STATUS_PERMISSION_DENIED;
741 }
742 lock_guard<mutex> lk(mMutex);
743 dprintf(fd, "AmFmRegionConfig: %s\n", mAmFmConfig.toString().c_str());
744 dprintf(fd, "Properties: %s \n", mProperties.toString().c_str());
745 if (mIsTuneCompleted) {
746 dprintf(fd, "Tune completed\n");
747 } else {
748 dprintf(fd, "Tune not completed\n");
749 }
750 if (mCallback == nullptr) {
751 dprintf(fd, "No ITunerCallback registered\n");
752 } else {
753 dprintf(fd, "ITunerCallback registered\n");
754 }
755 dprintf(fd, "CurrentProgram: %s \n", mCurrentProgram.toString().c_str());
756 return STATUS_OK;
757}
758
759binder_status_t BroadcastRadio::cmdHelp(int fd) const {
760 dprintf(fd, "Usage: \n\n");
761 dprintf(fd, "[no args]: dumps focus listener / gain callback registered status\n");
762 dprintf(fd, "--help: shows this help\n");
763 dprintf(fd,
764 "--tune amfm <FREQUENCY>: tunes amfm radio to frequency (in Hz) specified: "
765 "frequency (int) \n"
766 "--tune dab <SID> <ENSEMBLE>: tunes dab radio to sid and ensemble specified: "
767 "sidExt (int), ensemble (int) \n");
768 dprintf(fd,
769 "--seek [up|down] <SKIP_SUB_CHANNEL>: seek with direction (up or down) and "
770 "option whether skipping sub channel: "
771 "skipSubChannel (string, should be either \"true\" or \"false\")\n");
772 dprintf(fd, "--step [up|down]: step in direction (up or down) specified\n");
773 dprintf(fd, "--cancel: cancel current pending tune, step, and seek\n");
774 dprintf(fd,
775 "--startProgramListUpdates <IDENTIFIER_TYPES> <IDENTIFIERS> <INCLUDE_CATEGORIES> "
776 "<EXCLUDE_MODIFICATIONS>: start update program list with the filter specified: "
777 "identifier types (string, in format <TYPE>,<TYPE>,...,<TYPE> or \"null\" (if empty), "
778 "where TYPE is int), "
779 "program identifiers (string, in format "
780 "<TYPE>:<VALUE>,<TYPE>:<VALUE>,...,<TYPE>:<VALUE> or \"null\" (if empty), "
781 "where TYPE is int and VALUE is long), "
782 "includeCategories (string, should be either \"true\" or \"false\"), "
783 "excludeModifications (string, should be either \"true\" or \"false\")\n");
784 dprintf(fd, "--stopProgramListUpdates: stop current pending program list updates\n");
785 dprintf(fd,
786 "Note on <TYPE> for --startProgramList command: it is int for identifier type. "
787 "Please see broadcastradio/aidl/android/hardware/broadcastradio/IdentifierType.aidl "
788 "for its definition.\n");
789 dprintf(fd,
790 "Note on <VALUE> for --startProgramList command: it is long type for identifier value. "
791 "Please see broadcastradio/aidl/android/hardware/broadcastradio/IdentifierType.aidl "
792 "for its value.\n");
793
794 return STATUS_OK;
795}
796
797binder_status_t BroadcastRadio::cmdTune(int fd, const char** args, uint32_t numArgs) {
798 if (!checkDumpCallerHasWritePermissions(fd)) {
799 return STATUS_PERMISSION_DENIED;
800 }
801 if (numArgs != 3 && numArgs != 4) {
802 dprintf(fd,
803 "Invalid number of arguments: please provide --tune amfm <FREQUENCY> "
804 "or --tune dab <SID> <ENSEMBLE>\n");
805 return STATUS_BAD_VALUE;
806 }
807 bool isDab = false;
808 if (EqualsIgnoreCase(string(args[1]), "dab")) {
809 isDab = true;
810 } else if (!EqualsIgnoreCase(string(args[1]), "amfm")) {
811 dprintf(fd, "Unknown radio type provided with tune: %s\n", args[1]);
812 return STATUS_BAD_VALUE;
813 }
814 ProgramSelector sel = {};
815 if (isDab) {
Weilin Xu64cb9632023-03-15 23:46:43 +0000816 if (numArgs != 5 && numArgs != 3) {
Weilin Xu97203b02022-06-23 00:19:03 +0000817 dprintf(fd,
Weilin Xu0d4207d2022-12-09 00:37:44 +0000818 "Invalid number of arguments: please provide "
Weilin Xu64cb9632023-03-15 23:46:43 +0000819 "--tune dab <SID> <ENSEMBLE> <FREQUENCY> or "
820 "--tune dab <SID>\n");
Weilin Xu97203b02022-06-23 00:19:03 +0000821 return STATUS_BAD_VALUE;
822 }
823 int sid;
824 if (!utils::parseArgInt(string(args[2]), &sid)) {
Weilin Xu0d4207d2022-12-09 00:37:44 +0000825 dprintf(fd, "Non-integer sid provided with tune: %s\n", args[2]);
Weilin Xu97203b02022-06-23 00:19:03 +0000826 return STATUS_BAD_VALUE;
827 }
Weilin Xu64cb9632023-03-15 23:46:43 +0000828 if (numArgs == 3) {
829 sel = utils::makeSelectorDab(sid);
830 } else {
831 int ensemble;
832 if (!utils::parseArgInt(string(args[3]), &ensemble)) {
833 dprintf(fd, "Non-integer ensemble provided with tune: %s\n", args[3]);
834 return STATUS_BAD_VALUE;
835 }
836 int freq;
837 if (!utils::parseArgInt(string(args[4]), &freq)) {
838 dprintf(fd, "Non-integer frequency provided with tune: %s\n", args[4]);
839 return STATUS_BAD_VALUE;
840 }
841 sel = utils::makeSelectorDab(sid, ensemble, freq);
Weilin Xu97203b02022-06-23 00:19:03 +0000842 }
Weilin Xu97203b02022-06-23 00:19:03 +0000843 } else {
844 if (numArgs != 3) {
845 dprintf(fd, "Invalid number of arguments: please provide --tune amfm <FREQUENCY>\n");
846 return STATUS_BAD_VALUE;
847 }
848 int freq;
849 if (!utils::parseArgInt(string(args[2]), &freq)) {
Weilin Xu0d4207d2022-12-09 00:37:44 +0000850 dprintf(fd, "Non-integer frequency provided with tune: %s\n", args[2]);
Weilin Xu97203b02022-06-23 00:19:03 +0000851 return STATUS_BAD_VALUE;
852 }
853 sel = utils::makeSelectorAmfm(freq);
854 }
855
856 auto tuneResult = tune(sel);
857 if (!tuneResult.isOk()) {
858 dprintf(fd, "Unable to tune %s radio to %s\n", args[1], sel.toString().c_str());
859 return STATUS_BAD_VALUE;
860 }
861 dprintf(fd, "Tune %s radio to %s \n", args[1], sel.toString().c_str());
862 return STATUS_OK;
863}
864
865binder_status_t BroadcastRadio::cmdSeek(int fd, const char** args, uint32_t numArgs) {
866 if (!checkDumpCallerHasWritePermissions(fd)) {
867 return STATUS_PERMISSION_DENIED;
868 }
869 if (numArgs != 3) {
870 dprintf(fd,
871 "Invalid number of arguments: please provide --seek <DIRECTION> "
872 "<SKIP_SUB_CHANNEL>\n");
873 return STATUS_BAD_VALUE;
874 }
875 string seekDirectionIn = string(args[1]);
876 bool seekDirectionUp;
877 if (!utils::parseArgDirection(seekDirectionIn, &seekDirectionUp)) {
878 dprintf(fd, "Invalid direction (\"up\" or \"down\") provided with seek: %s\n",
879 seekDirectionIn.c_str());
880 return STATUS_BAD_VALUE;
881 }
882 string skipSubChannelIn = string(args[2]);
883 bool skipSubChannel;
884 if (!utils::parseArgBool(skipSubChannelIn, &skipSubChannel)) {
885 dprintf(fd, "Invalid skipSubChannel (\"true\" or \"false\") provided with seek: %s\n",
886 skipSubChannelIn.c_str());
887 return STATUS_BAD_VALUE;
888 }
889
890 auto seekResult = seek(seekDirectionUp, skipSubChannel);
891 if (!seekResult.isOk()) {
892 dprintf(fd, "Unable to seek in %s direction\n", seekDirectionIn.c_str());
893 return STATUS_BAD_VALUE;
894 }
895 dprintf(fd, "Seek in %s direction\n", seekDirectionIn.c_str());
896 return STATUS_OK;
897}
898
899binder_status_t BroadcastRadio::cmdStep(int fd, const char** args, uint32_t numArgs) {
900 if (!checkDumpCallerHasWritePermissions(fd)) {
901 return STATUS_PERMISSION_DENIED;
902 }
903 if (numArgs != 2) {
904 dprintf(fd, "Invalid number of arguments: please provide --step <DIRECTION>\n");
905 return STATUS_BAD_VALUE;
906 }
907 string stepDirectionIn = string(args[1]);
908 bool stepDirectionUp;
909 if (!utils::parseArgDirection(stepDirectionIn, &stepDirectionUp)) {
910 dprintf(fd, "Invalid direction (\"up\" or \"down\") provided with step: %s\n",
911 stepDirectionIn.c_str());
912 return STATUS_BAD_VALUE;
913 }
914
915 auto stepResult = step(stepDirectionUp);
916 if (!stepResult.isOk()) {
917 dprintf(fd, "Unable to step in %s direction\n", stepDirectionIn.c_str());
918 return STATUS_BAD_VALUE;
919 }
920 dprintf(fd, "Step in %s direction\n", stepDirectionIn.c_str());
921 return STATUS_OK;
922}
923
924binder_status_t BroadcastRadio::cmdCancel(int fd, uint32_t numArgs) {
925 if (!checkDumpCallerHasWritePermissions(fd)) {
926 return STATUS_PERMISSION_DENIED;
927 }
928 if (numArgs != 1) {
929 dprintf(fd,
930 "Invalid number of arguments: please provide --cancel "
931 "only and no more arguments\n");
932 return STATUS_BAD_VALUE;
933 }
934
935 auto cancelResult = cancel();
936 if (!cancelResult.isOk()) {
937 dprintf(fd, "Unable to cancel pending tune, seek, and step\n");
938 return STATUS_BAD_VALUE;
939 }
940 dprintf(fd, "Canceled pending tune, seek, and step\n");
941 return STATUS_OK;
942}
943
944binder_status_t BroadcastRadio::cmdStartProgramListUpdates(int fd, const char** args,
945 uint32_t numArgs) {
946 if (!checkDumpCallerHasWritePermissions(fd)) {
947 return STATUS_PERMISSION_DENIED;
948 }
949 if (numArgs != 5) {
950 dprintf(fd,
951 "Invalid number of arguments: please provide --startProgramListUpdates "
952 "<IDENTIFIER_TYPES> <IDENTIFIERS> <INCLUDE_CATEGORIES> "
953 "<EXCLUDE_MODIFICATIONS>\n");
954 return STATUS_BAD_VALUE;
955 }
956 string filterTypesStr = string(args[1]);
957 std::vector<IdentifierType> filterTypeList;
958 if (!EqualsIgnoreCase(filterTypesStr, "null") &&
959 !utils::parseArgIdentifierTypeArray(filterTypesStr, &filterTypeList)) {
960 dprintf(fd,
961 "Invalid identifier types provided with startProgramListUpdates: %s, "
962 "should be: <TYPE>,<TYPE>,...,<TYPE>\n",
963 filterTypesStr.c_str());
964 return STATUS_BAD_VALUE;
965 }
966 string filtersStr = string(args[2]);
967 std::vector<ProgramIdentifier> filterList;
968 if (!EqualsIgnoreCase(filtersStr, "null") &&
969 !utils::parseProgramIdentifierList(filtersStr, &filterList)) {
970 dprintf(fd,
971 "Invalid program identifiers provided with startProgramListUpdates: %s, "
972 "should be: <TYPE>:<VALUE>,<TYPE>:<VALUE>,...,<TYPE>:<VALUE>\n",
973 filtersStr.c_str());
974 return STATUS_BAD_VALUE;
975 }
976 string includeCategoriesStr = string(args[3]);
977 bool includeCategories;
978 if (!utils::parseArgBool(includeCategoriesStr, &includeCategories)) {
979 dprintf(fd,
980 "Invalid includeCategories (\"true\" or \"false\") "
981 "provided with startProgramListUpdates : %s\n",
982 includeCategoriesStr.c_str());
983 return STATUS_BAD_VALUE;
984 }
985 string excludeModificationsStr = string(args[4]);
986 bool excludeModifications;
987 if (!utils::parseArgBool(excludeModificationsStr, &excludeModifications)) {
988 dprintf(fd,
989 "Invalid excludeModifications(\"true\" or \"false\") "
990 "provided with startProgramListUpdates : %s\n",
991 excludeModificationsStr.c_str());
992 return STATUS_BAD_VALUE;
993 }
994 ProgramFilter filter = {filterTypeList, filterList, includeCategories, excludeModifications};
995
996 auto updateResult = startProgramListUpdates(filter);
997 if (!updateResult.isOk()) {
998 dprintf(fd, "Unable to start program list update for filter %s \n",
999 filter.toString().c_str());
1000 return STATUS_BAD_VALUE;
1001 }
1002 dprintf(fd, "Start program list update for filter %s\n", filter.toString().c_str());
1003 return STATUS_OK;
1004}
1005
1006binder_status_t BroadcastRadio::cmdStopProgramListUpdates(int fd, uint32_t numArgs) {
1007 if (!checkDumpCallerHasWritePermissions(fd)) {
1008 return STATUS_PERMISSION_DENIED;
1009 }
1010 if (numArgs != 1) {
1011 dprintf(fd,
1012 "Invalid number of arguments: please provide --stopProgramListUpdates "
1013 "only and no more arguments\n");
1014 return STATUS_BAD_VALUE;
1015 }
1016
1017 auto stopResult = stopProgramListUpdates();
1018 if (!stopResult.isOk()) {
1019 dprintf(fd, "Unable to stop pending program list update\n");
1020 return STATUS_BAD_VALUE;
1021 }
1022 dprintf(fd, "Stop pending program list update\n");
1023 return STATUS_OK;
1024}
1025
Weilin Xub2a6ca62022-05-08 23:47:04 +00001026} // namespace aidl::android::hardware::broadcastradio