blob: 4d097c16077a4b9ddc839dd3a4ce0b54e56b6250 [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>
19#include "resources.h"
20
21#include <aidl/android/hardware/broadcastradio/IdentifierType.h>
22#include <aidl/android/hardware/broadcastradio/Result.h>
23
24#include <android-base/logging.h>
Weilin Xu97203b02022-06-23 00:19:03 +000025#include <android-base/strings.h>
26
27#include <private/android_filesystem_config.h>
Weilin Xub2a6ca62022-05-08 23:47:04 +000028
29namespace aidl::android::hardware::broadcastradio {
30
31using ::aidl::android::hardware::broadcastradio::utils::resultToInt;
32using ::aidl::android::hardware::broadcastradio::utils::tunesTo;
Weilin Xu97203b02022-06-23 00:19:03 +000033using ::android::base::EqualsIgnoreCase;
Weilin Xub2a6ca62022-05-08 23:47:04 +000034using ::ndk::ScopedAStatus;
35using ::std::literals::chrono_literals::operator""ms;
36using ::std::literals::chrono_literals::operator""s;
37using ::std::lock_guard;
38using ::std::mutex;
39using ::std::string;
40using ::std::vector;
41
42namespace {
43
44inline constexpr std::chrono::milliseconds kSeekDelayTimeMs = 200ms;
45inline constexpr std::chrono::milliseconds kStepDelayTimeMs = 100ms;
46inline constexpr std::chrono::milliseconds kTuneDelayTimeMs = 150ms;
47inline constexpr std::chrono::seconds kListDelayTimeS = 1s;
48
49// clang-format off
50const AmFmRegionConfig kDefaultAmFmConfig = {
51 {
52 {87500, 108000, 100, 100}, // FM
53 {153, 282, 3, 9}, // AM LW
54 {531, 1620, 9, 9}, // AM MW
55 {1600, 30000, 1, 5}, // AM SW
56 },
57 AmFmRegionConfig::DEEMPHASIS_D50,
58 AmFmRegionConfig::RDS};
59// clang-format on
60
61Properties initProperties(const VirtualRadio& virtualRadio) {
62 Properties prop = {};
63
64 prop.maker = "Android";
65 prop.product = virtualRadio.getName();
66 prop.supportedIdentifierTypes = vector<IdentifierType>({
67 IdentifierType::AMFM_FREQUENCY_KHZ,
68 IdentifierType::RDS_PI,
69 IdentifierType::HD_STATION_ID_EXT,
70 IdentifierType::DAB_SID_EXT,
71 });
72 prop.vendorInfo = vector<VendorKeyValue>({
73 {"com.android.sample", "sample"},
74 });
75
76 return prop;
77}
78
79// Makes ProgramInfo that does not point to any particular program
80ProgramInfo makeSampleProgramInfo(const ProgramSelector& selector) {
81 ProgramInfo info = {};
82 info.selector = selector;
83 info.logicallyTunedTo =
84 utils::makeIdentifier(IdentifierType::AMFM_FREQUENCY_KHZ,
85 utils::getId(selector, IdentifierType::AMFM_FREQUENCY_KHZ));
86 info.physicallyTunedTo = info.logicallyTunedTo;
87 return info;
88}
89
Weilin Xu97203b02022-06-23 00:19:03 +000090static bool checkDumpCallerHasWritePermissions(int fd) {
91 uid_t uid = AIBinder_getCallingUid();
92 if (uid == AID_ROOT || uid == AID_SHELL || uid == AID_SYSTEM) {
93 return true;
94 }
95 dprintf(fd, "BroadcastRadio HAL dump must be root, shell or system\n");
96 return false;
97}
98
Weilin Xub2a6ca62022-05-08 23:47:04 +000099} // namespace
100
101BroadcastRadio::BroadcastRadio(const VirtualRadio& virtualRadio)
102 : mVirtualRadio(virtualRadio),
103 mAmFmConfig(kDefaultAmFmConfig),
104 mProperties(initProperties(virtualRadio)) {
105 const auto& ranges = kDefaultAmFmConfig.ranges;
106 if (ranges.size() > 0) {
107 ProgramSelector sel = utils::makeSelectorAmfm(ranges[0].lowerBound);
108 VirtualProgram virtualProgram = {};
109 if (mVirtualRadio.getProgram(sel, &virtualProgram)) {
110 mCurrentProgram = virtualProgram.selector;
111 } else {
112 mCurrentProgram = sel;
113 }
114 }
115}
116
117BroadcastRadio::~BroadcastRadio() {
118 mThread.reset();
119}
120
121ScopedAStatus BroadcastRadio::getAmFmRegionConfig(bool full, AmFmRegionConfig* returnConfigs) {
122 if (full) {
123 *returnConfigs = {};
124 returnConfigs->ranges = vector<AmFmBandRange>({
125 {65000, 108000, 10, 0}, // FM
126 {150, 30000, 1, 0}, // AM
127 });
128 returnConfigs->fmDeemphasis =
129 AmFmRegionConfig::DEEMPHASIS_D50 | AmFmRegionConfig::DEEMPHASIS_D75;
130 returnConfigs->fmRds = AmFmRegionConfig::RDS | AmFmRegionConfig::RBDS;
131 return ScopedAStatus::ok();
132 }
133 lock_guard<mutex> lk(mMutex);
134 *returnConfigs = mAmFmConfig;
135 return ScopedAStatus::ok();
136}
137
138ScopedAStatus BroadcastRadio::getDabRegionConfig(vector<DabTableEntry>* returnConfigs) {
139 *returnConfigs = {
140 {"5A", 174928}, {"7D", 194064}, {"8A", 195936}, {"8B", 197648}, {"9A", 202928},
141 {"9B", 204640}, {"9C", 206352}, {"10B", 211648}, {"10C", 213360}, {"10D", 215072},
142 {"11A", 216928}, {"11B", 218640}, {"11C", 220352}, {"11D", 222064}, {"12A", 223936},
143 {"12B", 225648}, {"12C", 227360}, {"12D", 229072},
144 };
145 return ScopedAStatus::ok();
146}
147
148ScopedAStatus BroadcastRadio::getImage(int32_t id, vector<uint8_t>* returnImage) {
149 LOG(DEBUG) << __func__ << ": fetching image " << std::hex << id;
150
151 if (id == resources::kDemoPngId) {
152 *returnImage = vector<uint8_t>(resources::kDemoPng, std::end(resources::kDemoPng));
153 return ScopedAStatus::ok();
154 }
155
156 LOG(WARNING) << __func__ << ": image of id " << std::hex << id << " doesn't exist";
157 *returnImage = {};
158 return ScopedAStatus::ok();
159}
160
161ScopedAStatus BroadcastRadio::getProperties(Properties* returnProperties) {
162 lock_guard<mutex> lk(mMutex);
163 *returnProperties = mProperties;
164 return ScopedAStatus::ok();
165}
166
167ProgramInfo BroadcastRadio::tuneInternalLocked(const ProgramSelector& sel) {
168 LOG(DEBUG) << __func__ << ": tune (internal) to " << sel.toString();
169
170 VirtualProgram virtualProgram = {};
171 ProgramInfo programInfo;
172 if (mVirtualRadio.getProgram(sel, &virtualProgram)) {
173 mCurrentProgram = virtualProgram.selector;
174 programInfo = virtualProgram;
175 } else {
176 mCurrentProgram = sel;
177 programInfo = makeSampleProgramInfo(sel);
178 }
179 mIsTuneCompleted = true;
180
181 return programInfo;
182}
183
184ScopedAStatus BroadcastRadio::setTunerCallback(const std::shared_ptr<ITunerCallback>& callback) {
185 LOG(DEBUG) << __func__ << ": setTunerCallback";
186
187 if (callback == nullptr) {
188 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
189 resultToInt(Result::INVALID_ARGUMENTS), "cannot set tuner callback to null");
190 }
191
192 lock_guard<mutex> lk(mMutex);
193 mCallback = callback;
194
195 return ScopedAStatus::ok();
196}
197
198ScopedAStatus BroadcastRadio::unsetTunerCallback() {
199 LOG(DEBUG) << __func__ << ": unsetTunerCallback";
200
201 lock_guard<mutex> lk(mMutex);
202 mCallback = nullptr;
203
204 return ScopedAStatus::ok();
205}
206
207ScopedAStatus BroadcastRadio::tune(const ProgramSelector& program) {
208 LOG(DEBUG) << __func__ << ": tune to " << program.toString() << "...";
209
210 lock_guard<mutex> lk(mMutex);
211 if (mCallback == nullptr) {
212 LOG(ERROR) << __func__ << ": callback is not registered.";
213 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
214 resultToInt(Result::INVALID_STATE), "callback is not registered");
215 }
216
217 if (!utils::isSupported(mProperties, program)) {
218 LOG(WARNING) << __func__ << ": selector not supported: " << program.toString();
219 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
220 resultToInt(Result::NOT_SUPPORTED), "selector is not supported");
221 }
222
223 if (!utils::isValid(program)) {
224 LOG(ERROR) << __func__ << ": selector is not valid: " << program.toString();
225 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
226 resultToInt(Result::INVALID_ARGUMENTS), "selector is not valid");
227 }
228
229 cancelLocked();
230
231 mIsTuneCompleted = false;
232 std::shared_ptr<ITunerCallback> callback = mCallback;
233 auto task = [this, program, callback]() {
234 ProgramInfo programInfo = {};
235 {
236 lock_guard<mutex> lk(mMutex);
237 programInfo = tuneInternalLocked(program);
238 }
239 callback->onCurrentProgramInfoChanged(programInfo);
240 };
Weilin Xud7483322022-11-29 01:12:36 +0000241 auto cancelTask = [program, callback]() { callback->onTuneFailed(Result::CANCELED, program); };
242 mThread->schedule(task, cancelTask, kTuneDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000243
244 return ScopedAStatus::ok();
245}
246
247ScopedAStatus BroadcastRadio::seek(bool directionUp, bool skipSubChannel) {
248 LOG(DEBUG) << __func__ << ": seek " << (directionUp ? "up" : "down") << " with skipSubChannel? "
249 << (skipSubChannel ? "yes" : "no") << "...";
250
251 lock_guard<mutex> lk(mMutex);
252 if (mCallback == nullptr) {
253 LOG(ERROR) << __func__ << ": callback is not registered.";
254 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
255 resultToInt(Result::INVALID_STATE), "callback is not registered");
256 }
257
258 cancelLocked();
259
260 const auto& list = mVirtualRadio.getProgramList();
261 std::shared_ptr<ITunerCallback> callback = mCallback;
Weilin Xud7483322022-11-29 01:12:36 +0000262 auto cancelTask = [callback]() { callback->onTuneFailed(Result::CANCELED, {}); };
Weilin Xub2a6ca62022-05-08 23:47:04 +0000263 if (list.empty()) {
264 mIsTuneCompleted = false;
265 auto task = [callback]() {
266 LOG(DEBUG) << "seek: program list is empty, seek couldn't stop";
267
268 callback->onTuneFailed(Result::TIMEOUT, {});
269 };
Weilin Xud7483322022-11-29 01:12:36 +0000270 mThread->schedule(task, cancelTask, kSeekDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000271
272 return ScopedAStatus::ok();
273 }
274
275 // The list is not sorted here since it has already stored in VirtualRadio.
276 // If the list is not sorted in advance, it should be sorted here.
277 const auto& current = mCurrentProgram;
278 auto found = std::lower_bound(list.begin(), list.end(), VirtualProgram({current}));
279 if (directionUp) {
280 if (found < list.end() - 1) {
281 if (tunesTo(current, found->selector)) found++;
282 } else {
283 found = list.begin();
284 }
285 } else {
286 if (found > list.begin() && found != list.end()) {
287 found--;
288 } else {
289 found = list.end() - 1;
290 }
291 }
292 const ProgramSelector tuneTo = found->selector;
293
294 mIsTuneCompleted = false;
295 auto task = [this, tuneTo, callback]() {
296 ProgramInfo programInfo = {};
297 {
298 lock_guard<mutex> lk(mMutex);
299 programInfo = tuneInternalLocked(tuneTo);
300 }
301 callback->onCurrentProgramInfoChanged(programInfo);
302 };
Weilin Xud7483322022-11-29 01:12:36 +0000303 mThread->schedule(task, cancelTask, kSeekDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000304
305 return ScopedAStatus::ok();
306}
307
308ScopedAStatus BroadcastRadio::step(bool directionUp) {
309 LOG(DEBUG) << __func__ << ": step " << (directionUp ? "up" : "down") << "...";
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 cancelLocked();
319
320 if (!utils::hasId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ)) {
321 LOG(WARNING) << __func__ << ": can't step in anything else than AM/FM";
322 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
323 resultToInt(Result::NOT_SUPPORTED), "cannot step in anything else than AM/FM");
324 }
325
326 int64_t stepTo = utils::getId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ);
327 std::optional<AmFmBandRange> range = getAmFmRangeLocked();
328 if (!range) {
329 LOG(ERROR) << __func__ << ": can't find current band or tune operation is in process";
330 ScopedAStatus::fromServiceSpecificErrorWithMessage(
331 resultToInt(Result::INTERNAL_ERROR),
332 "can't find current band or tune operation is in process");
333 }
334
335 if (directionUp) {
336 stepTo += range->spacing;
337 } else {
338 stepTo -= range->spacing;
339 }
340 if (stepTo > range->upperBound) {
341 stepTo = range->lowerBound;
342 }
343 if (stepTo < range->lowerBound) {
344 stepTo = range->upperBound;
345 }
346
347 mIsTuneCompleted = false;
348 std::shared_ptr<ITunerCallback> callback = mCallback;
349 auto task = [this, stepTo, callback]() {
350 ProgramInfo programInfo;
351 {
352 lock_guard<mutex> lk(mMutex);
353 programInfo = tuneInternalLocked(utils::makeSelectorAmfm(stepTo));
354 }
355 callback->onCurrentProgramInfoChanged(programInfo);
356 };
Weilin Xud7483322022-11-29 01:12:36 +0000357 auto cancelTask = [callback]() { callback->onTuneFailed(Result::CANCELED, {}); };
358 mThread->schedule(task, cancelTask, kStepDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000359
360 return ScopedAStatus::ok();
361}
362
363void BroadcastRadio::cancelLocked() {
364 LOG(DEBUG) << __func__ << ": cancelling current operations...";
365
366 mThread->cancelAll();
367 if (mCurrentProgram.primaryId.type != IdentifierType::INVALID) {
368 mIsTuneCompleted = true;
369 }
370}
371
372ScopedAStatus BroadcastRadio::cancel() {
373 LOG(DEBUG) << __func__ << ": cancel pending tune, seek and step...";
374
375 lock_guard<mutex> lk(mMutex);
376 cancelLocked();
377
378 return ScopedAStatus::ok();
379}
380
381ScopedAStatus BroadcastRadio::startProgramListUpdates(const ProgramFilter& filter) {
382 LOG(DEBUG) << __func__ << ": requested program list updates, filter = " << filter.toString()
383 << "...";
384
385 auto filterCb = [&filter](const VirtualProgram& program) {
386 return utils::satisfies(filter, program.selector);
387 };
388
389 lock_guard<mutex> lk(mMutex);
390
391 const auto& list = mVirtualRadio.getProgramList();
392 vector<VirtualProgram> filteredList;
393 std::copy_if(list.begin(), list.end(), std::back_inserter(filteredList), filterCb);
394
395 auto task = [this, filteredList]() {
396 std::shared_ptr<ITunerCallback> callback;
397 {
398 lock_guard<mutex> lk(mMutex);
399 if (mCallback == nullptr) {
400 LOG(WARNING) << "Callback is null when updating program List";
401 return;
402 }
403 callback = mCallback;
404 }
405
406 ProgramListChunk chunk = {};
407 chunk.purge = true;
408 chunk.complete = true;
409 chunk.modified = vector<ProgramInfo>(filteredList.begin(), filteredList.end());
410
411 callback->onProgramListUpdated(chunk);
412 };
413 mThread->schedule(task, kListDelayTimeS);
414
415 return ScopedAStatus::ok();
416}
417
418ScopedAStatus BroadcastRadio::stopProgramListUpdates() {
419 LOG(DEBUG) << __func__ << ": requested program list updates to stop...";
420 // TODO(b/243681584) Implement stop program list updates method
421 return ScopedAStatus::ok();
422}
423
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000424ScopedAStatus BroadcastRadio::isConfigFlagSet(ConfigFlag flag, bool* returnIsSet) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000425 LOG(DEBUG) << __func__ << ": flag = " << toString(flag);
426
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000427 int flagBit = static_cast<int>(flag);
428 lock_guard<mutex> lk(mMutex);
429 *returnIsSet = ((mConfigFlagValues >> flagBit) & 1) == 1;
430 return ScopedAStatus::ok();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000431}
432
433ScopedAStatus BroadcastRadio::setConfigFlag(ConfigFlag flag, bool value) {
434 LOG(DEBUG) << __func__ << ": flag = " << toString(flag) << ", value = " << value;
435
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000436 int flagBitMask = 1 << (static_cast<int>(flag));
437 lock_guard<mutex> lk(mMutex);
438 if (value) {
439 mConfigFlagValues |= flagBitMask;
440 } else {
441 mConfigFlagValues &= ~flagBitMask;
442 }
443 return ScopedAStatus::ok();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000444}
445
446ScopedAStatus BroadcastRadio::setParameters(
447 [[maybe_unused]] const vector<VendorKeyValue>& parameters,
448 vector<VendorKeyValue>* returnParameters) {
449 // TODO(b/243682330) Support vendor parameter functionality
450 *returnParameters = {};
451 return ScopedAStatus::ok();
452}
453
454ScopedAStatus BroadcastRadio::getParameters([[maybe_unused]] const vector<string>& keys,
455 vector<VendorKeyValue>* returnParameters) {
456 // TODO(b/243682330) Support vendor parameter functionality
457 *returnParameters = {};
458 return ScopedAStatus::ok();
459}
460
461std::optional<AmFmBandRange> BroadcastRadio::getAmFmRangeLocked() const {
462 if (!mIsTuneCompleted) {
463 LOG(WARNING) << __func__ << ": tune operation is in process";
464 return {};
465 }
466 if (!utils::hasId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ)) {
467 LOG(WARNING) << __func__ << ": current program does not has AMFM_FREQUENCY_KHZ identifier";
468 return {};
469 }
470
471 int64_t freq = utils::getId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ);
472 for (const auto& range : mAmFmConfig.ranges) {
473 if (range.lowerBound <= freq && range.upperBound >= freq) {
474 return range;
475 }
476 }
477
478 return {};
479}
480
481ScopedAStatus BroadcastRadio::registerAnnouncementListener(
482 [[maybe_unused]] const std::shared_ptr<IAnnouncementListener>& listener,
483 const vector<AnnouncementType>& enabled, std::shared_ptr<ICloseHandle>* returnCloseHandle) {
484 LOG(DEBUG) << __func__ << ": registering announcement listener for "
485 << utils::vectorToString(enabled);
486
487 // TODO(b/243683842) Support announcement listener
488 *returnCloseHandle = nullptr;
489 LOG(INFO) << __func__ << ": registering announcementListener is not supported";
490 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
491 resultToInt(Result::NOT_SUPPORTED),
492 "registering announcementListener is not supported");
493}
494
Weilin Xu97203b02022-06-23 00:19:03 +0000495binder_status_t BroadcastRadio::dump(int fd, const char** args, uint32_t numArgs) {
496 if (numArgs == 0) {
497 return dumpsys(fd);
498 }
499
500 string option = string(args[0]);
501 if (EqualsIgnoreCase(option, "--help")) {
502 return cmdHelp(fd);
503 } else if (EqualsIgnoreCase(option, "--tune")) {
504 return cmdTune(fd, args, numArgs);
505 } else if (EqualsIgnoreCase(option, "--seek")) {
506 return cmdSeek(fd, args, numArgs);
507 } else if (EqualsIgnoreCase(option, "--step")) {
508 return cmdStep(fd, args, numArgs);
509 } else if (EqualsIgnoreCase(option, "--cancel")) {
510 return cmdCancel(fd, numArgs);
511 } else if (EqualsIgnoreCase(option, "--startProgramListUpdates")) {
512 return cmdStartProgramListUpdates(fd, args, numArgs);
513 } else if (EqualsIgnoreCase(option, "--stopProgramListUpdates")) {
514 return cmdStopProgramListUpdates(fd, numArgs);
515 }
516 dprintf(fd, "Invalid option: %s\n", option.c_str());
517 return STATUS_BAD_VALUE;
518}
519
520binder_status_t BroadcastRadio::dumpsys(int fd) {
521 if (!checkDumpCallerHasWritePermissions(fd)) {
522 return STATUS_PERMISSION_DENIED;
523 }
524 lock_guard<mutex> lk(mMutex);
525 dprintf(fd, "AmFmRegionConfig: %s\n", mAmFmConfig.toString().c_str());
526 dprintf(fd, "Properties: %s \n", mProperties.toString().c_str());
527 if (mIsTuneCompleted) {
528 dprintf(fd, "Tune completed\n");
529 } else {
530 dprintf(fd, "Tune not completed\n");
531 }
532 if (mCallback == nullptr) {
533 dprintf(fd, "No ITunerCallback registered\n");
534 } else {
535 dprintf(fd, "ITunerCallback registered\n");
536 }
537 dprintf(fd, "CurrentProgram: %s \n", mCurrentProgram.toString().c_str());
538 return STATUS_OK;
539}
540
541binder_status_t BroadcastRadio::cmdHelp(int fd) const {
542 dprintf(fd, "Usage: \n\n");
543 dprintf(fd, "[no args]: dumps focus listener / gain callback registered status\n");
544 dprintf(fd, "--help: shows this help\n");
545 dprintf(fd,
546 "--tune amfm <FREQUENCY>: tunes amfm radio to frequency (in Hz) specified: "
547 "frequency (int) \n"
548 "--tune dab <SID> <ENSEMBLE>: tunes dab radio to sid and ensemble specified: "
549 "sidExt (int), ensemble (int) \n");
550 dprintf(fd,
551 "--seek [up|down] <SKIP_SUB_CHANNEL>: seek with direction (up or down) and "
552 "option whether skipping sub channel: "
553 "skipSubChannel (string, should be either \"true\" or \"false\")\n");
554 dprintf(fd, "--step [up|down]: step in direction (up or down) specified\n");
555 dprintf(fd, "--cancel: cancel current pending tune, step, and seek\n");
556 dprintf(fd,
557 "--startProgramListUpdates <IDENTIFIER_TYPES> <IDENTIFIERS> <INCLUDE_CATEGORIES> "
558 "<EXCLUDE_MODIFICATIONS>: start update program list with the filter specified: "
559 "identifier types (string, in format <TYPE>,<TYPE>,...,<TYPE> or \"null\" (if empty), "
560 "where TYPE is int), "
561 "program identifiers (string, in format "
562 "<TYPE>:<VALUE>,<TYPE>:<VALUE>,...,<TYPE>:<VALUE> or \"null\" (if empty), "
563 "where TYPE is int and VALUE is long), "
564 "includeCategories (string, should be either \"true\" or \"false\"), "
565 "excludeModifications (string, should be either \"true\" or \"false\")\n");
566 dprintf(fd, "--stopProgramListUpdates: stop current pending program list updates\n");
567 dprintf(fd,
568 "Note on <TYPE> for --startProgramList command: it is int for identifier type. "
569 "Please see broadcastradio/aidl/android/hardware/broadcastradio/IdentifierType.aidl "
570 "for its definition.\n");
571 dprintf(fd,
572 "Note on <VALUE> for --startProgramList command: it is long type for identifier value. "
573 "Please see broadcastradio/aidl/android/hardware/broadcastradio/IdentifierType.aidl "
574 "for its value.\n");
575
576 return STATUS_OK;
577}
578
579binder_status_t BroadcastRadio::cmdTune(int fd, const char** args, uint32_t numArgs) {
580 if (!checkDumpCallerHasWritePermissions(fd)) {
581 return STATUS_PERMISSION_DENIED;
582 }
583 if (numArgs != 3 && numArgs != 4) {
584 dprintf(fd,
585 "Invalid number of arguments: please provide --tune amfm <FREQUENCY> "
586 "or --tune dab <SID> <ENSEMBLE>\n");
587 return STATUS_BAD_VALUE;
588 }
589 bool isDab = false;
590 if (EqualsIgnoreCase(string(args[1]), "dab")) {
591 isDab = true;
592 } else if (!EqualsIgnoreCase(string(args[1]), "amfm")) {
593 dprintf(fd, "Unknown radio type provided with tune: %s\n", args[1]);
594 return STATUS_BAD_VALUE;
595 }
596 ProgramSelector sel = {};
597 if (isDab) {
Weilin Xu64cb9632023-03-15 23:46:43 +0000598 if (numArgs != 5 && numArgs != 3) {
Weilin Xu97203b02022-06-23 00:19:03 +0000599 dprintf(fd,
Weilin Xu0d4207d2022-12-09 00:37:44 +0000600 "Invalid number of arguments: please provide "
Weilin Xu64cb9632023-03-15 23:46:43 +0000601 "--tune dab <SID> <ENSEMBLE> <FREQUENCY> or "
602 "--tune dab <SID>\n");
Weilin Xu97203b02022-06-23 00:19:03 +0000603 return STATUS_BAD_VALUE;
604 }
605 int sid;
606 if (!utils::parseArgInt(string(args[2]), &sid)) {
Weilin Xu0d4207d2022-12-09 00:37:44 +0000607 dprintf(fd, "Non-integer sid provided with tune: %s\n", args[2]);
Weilin Xu97203b02022-06-23 00:19:03 +0000608 return STATUS_BAD_VALUE;
609 }
Weilin Xu64cb9632023-03-15 23:46:43 +0000610 if (numArgs == 3) {
611 sel = utils::makeSelectorDab(sid);
612 } else {
613 int ensemble;
614 if (!utils::parseArgInt(string(args[3]), &ensemble)) {
615 dprintf(fd, "Non-integer ensemble provided with tune: %s\n", args[3]);
616 return STATUS_BAD_VALUE;
617 }
618 int freq;
619 if (!utils::parseArgInt(string(args[4]), &freq)) {
620 dprintf(fd, "Non-integer frequency provided with tune: %s\n", args[4]);
621 return STATUS_BAD_VALUE;
622 }
623 sel = utils::makeSelectorDab(sid, ensemble, freq);
Weilin Xu97203b02022-06-23 00:19:03 +0000624 }
Weilin Xu97203b02022-06-23 00:19:03 +0000625 } else {
626 if (numArgs != 3) {
627 dprintf(fd, "Invalid number of arguments: please provide --tune amfm <FREQUENCY>\n");
628 return STATUS_BAD_VALUE;
629 }
630 int freq;
631 if (!utils::parseArgInt(string(args[2]), &freq)) {
Weilin Xu0d4207d2022-12-09 00:37:44 +0000632 dprintf(fd, "Non-integer frequency provided with tune: %s\n", args[2]);
Weilin Xu97203b02022-06-23 00:19:03 +0000633 return STATUS_BAD_VALUE;
634 }
635 sel = utils::makeSelectorAmfm(freq);
636 }
637
638 auto tuneResult = tune(sel);
639 if (!tuneResult.isOk()) {
640 dprintf(fd, "Unable to tune %s radio to %s\n", args[1], sel.toString().c_str());
641 return STATUS_BAD_VALUE;
642 }
643 dprintf(fd, "Tune %s radio to %s \n", args[1], sel.toString().c_str());
644 return STATUS_OK;
645}
646
647binder_status_t BroadcastRadio::cmdSeek(int fd, const char** args, uint32_t numArgs) {
648 if (!checkDumpCallerHasWritePermissions(fd)) {
649 return STATUS_PERMISSION_DENIED;
650 }
651 if (numArgs != 3) {
652 dprintf(fd,
653 "Invalid number of arguments: please provide --seek <DIRECTION> "
654 "<SKIP_SUB_CHANNEL>\n");
655 return STATUS_BAD_VALUE;
656 }
657 string seekDirectionIn = string(args[1]);
658 bool seekDirectionUp;
659 if (!utils::parseArgDirection(seekDirectionIn, &seekDirectionUp)) {
660 dprintf(fd, "Invalid direction (\"up\" or \"down\") provided with seek: %s\n",
661 seekDirectionIn.c_str());
662 return STATUS_BAD_VALUE;
663 }
664 string skipSubChannelIn = string(args[2]);
665 bool skipSubChannel;
666 if (!utils::parseArgBool(skipSubChannelIn, &skipSubChannel)) {
667 dprintf(fd, "Invalid skipSubChannel (\"true\" or \"false\") provided with seek: %s\n",
668 skipSubChannelIn.c_str());
669 return STATUS_BAD_VALUE;
670 }
671
672 auto seekResult = seek(seekDirectionUp, skipSubChannel);
673 if (!seekResult.isOk()) {
674 dprintf(fd, "Unable to seek in %s direction\n", seekDirectionIn.c_str());
675 return STATUS_BAD_VALUE;
676 }
677 dprintf(fd, "Seek in %s direction\n", seekDirectionIn.c_str());
678 return STATUS_OK;
679}
680
681binder_status_t BroadcastRadio::cmdStep(int fd, const char** args, uint32_t numArgs) {
682 if (!checkDumpCallerHasWritePermissions(fd)) {
683 return STATUS_PERMISSION_DENIED;
684 }
685 if (numArgs != 2) {
686 dprintf(fd, "Invalid number of arguments: please provide --step <DIRECTION>\n");
687 return STATUS_BAD_VALUE;
688 }
689 string stepDirectionIn = string(args[1]);
690 bool stepDirectionUp;
691 if (!utils::parseArgDirection(stepDirectionIn, &stepDirectionUp)) {
692 dprintf(fd, "Invalid direction (\"up\" or \"down\") provided with step: %s\n",
693 stepDirectionIn.c_str());
694 return STATUS_BAD_VALUE;
695 }
696
697 auto stepResult = step(stepDirectionUp);
698 if (!stepResult.isOk()) {
699 dprintf(fd, "Unable to step in %s direction\n", stepDirectionIn.c_str());
700 return STATUS_BAD_VALUE;
701 }
702 dprintf(fd, "Step in %s direction\n", stepDirectionIn.c_str());
703 return STATUS_OK;
704}
705
706binder_status_t BroadcastRadio::cmdCancel(int fd, uint32_t numArgs) {
707 if (!checkDumpCallerHasWritePermissions(fd)) {
708 return STATUS_PERMISSION_DENIED;
709 }
710 if (numArgs != 1) {
711 dprintf(fd,
712 "Invalid number of arguments: please provide --cancel "
713 "only and no more arguments\n");
714 return STATUS_BAD_VALUE;
715 }
716
717 auto cancelResult = cancel();
718 if (!cancelResult.isOk()) {
719 dprintf(fd, "Unable to cancel pending tune, seek, and step\n");
720 return STATUS_BAD_VALUE;
721 }
722 dprintf(fd, "Canceled pending tune, seek, and step\n");
723 return STATUS_OK;
724}
725
726binder_status_t BroadcastRadio::cmdStartProgramListUpdates(int fd, const char** args,
727 uint32_t numArgs) {
728 if (!checkDumpCallerHasWritePermissions(fd)) {
729 return STATUS_PERMISSION_DENIED;
730 }
731 if (numArgs != 5) {
732 dprintf(fd,
733 "Invalid number of arguments: please provide --startProgramListUpdates "
734 "<IDENTIFIER_TYPES> <IDENTIFIERS> <INCLUDE_CATEGORIES> "
735 "<EXCLUDE_MODIFICATIONS>\n");
736 return STATUS_BAD_VALUE;
737 }
738 string filterTypesStr = string(args[1]);
739 std::vector<IdentifierType> filterTypeList;
740 if (!EqualsIgnoreCase(filterTypesStr, "null") &&
741 !utils::parseArgIdentifierTypeArray(filterTypesStr, &filterTypeList)) {
742 dprintf(fd,
743 "Invalid identifier types provided with startProgramListUpdates: %s, "
744 "should be: <TYPE>,<TYPE>,...,<TYPE>\n",
745 filterTypesStr.c_str());
746 return STATUS_BAD_VALUE;
747 }
748 string filtersStr = string(args[2]);
749 std::vector<ProgramIdentifier> filterList;
750 if (!EqualsIgnoreCase(filtersStr, "null") &&
751 !utils::parseProgramIdentifierList(filtersStr, &filterList)) {
752 dprintf(fd,
753 "Invalid program identifiers provided with startProgramListUpdates: %s, "
754 "should be: <TYPE>:<VALUE>,<TYPE>:<VALUE>,...,<TYPE>:<VALUE>\n",
755 filtersStr.c_str());
756 return STATUS_BAD_VALUE;
757 }
758 string includeCategoriesStr = string(args[3]);
759 bool includeCategories;
760 if (!utils::parseArgBool(includeCategoriesStr, &includeCategories)) {
761 dprintf(fd,
762 "Invalid includeCategories (\"true\" or \"false\") "
763 "provided with startProgramListUpdates : %s\n",
764 includeCategoriesStr.c_str());
765 return STATUS_BAD_VALUE;
766 }
767 string excludeModificationsStr = string(args[4]);
768 bool excludeModifications;
769 if (!utils::parseArgBool(excludeModificationsStr, &excludeModifications)) {
770 dprintf(fd,
771 "Invalid excludeModifications(\"true\" or \"false\") "
772 "provided with startProgramListUpdates : %s\n",
773 excludeModificationsStr.c_str());
774 return STATUS_BAD_VALUE;
775 }
776 ProgramFilter filter = {filterTypeList, filterList, includeCategories, excludeModifications};
777
778 auto updateResult = startProgramListUpdates(filter);
779 if (!updateResult.isOk()) {
780 dprintf(fd, "Unable to start program list update for filter %s \n",
781 filter.toString().c_str());
782 return STATUS_BAD_VALUE;
783 }
784 dprintf(fd, "Start program list update for filter %s\n", filter.toString().c_str());
785 return STATUS_OK;
786}
787
788binder_status_t BroadcastRadio::cmdStopProgramListUpdates(int fd, uint32_t numArgs) {
789 if (!checkDumpCallerHasWritePermissions(fd)) {
790 return STATUS_PERMISSION_DENIED;
791 }
792 if (numArgs != 1) {
793 dprintf(fd,
794 "Invalid number of arguments: please provide --stopProgramListUpdates "
795 "only and no more arguments\n");
796 return STATUS_BAD_VALUE;
797 }
798
799 auto stopResult = stopProgramListUpdates();
800 if (!stopResult.isOk()) {
801 dprintf(fd, "Unable to stop pending program list update\n");
802 return STATUS_BAD_VALUE;
803 }
804 dprintf(fd, "Stop pending program list update\n");
805 return STATUS_OK;
806}
807
Weilin Xub2a6ca62022-05-08 23:47:04 +0000808} // namespace aidl::android::hardware::broadcastradio