blob: 0209a0e6ef00fc2a66bd1e6c05df09b02fa1cd9b [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 };
241 mThread->schedule(task, kTuneDelayTimeMs);
242
243 return ScopedAStatus::ok();
244}
245
246ScopedAStatus BroadcastRadio::seek(bool directionUp, bool skipSubChannel) {
247 LOG(DEBUG) << __func__ << ": seek " << (directionUp ? "up" : "down") << " with skipSubChannel? "
248 << (skipSubChannel ? "yes" : "no") << "...";
249
250 lock_guard<mutex> lk(mMutex);
251 if (mCallback == nullptr) {
252 LOG(ERROR) << __func__ << ": callback is not registered.";
253 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
254 resultToInt(Result::INVALID_STATE), "callback is not registered");
255 }
256
257 cancelLocked();
258
259 const auto& list = mVirtualRadio.getProgramList();
260 std::shared_ptr<ITunerCallback> callback = mCallback;
261 if (list.empty()) {
262 mIsTuneCompleted = false;
263 auto task = [callback]() {
264 LOG(DEBUG) << "seek: program list is empty, seek couldn't stop";
265
266 callback->onTuneFailed(Result::TIMEOUT, {});
267 };
268 mThread->schedule(task, kSeekDelayTimeMs);
269
270 return ScopedAStatus::ok();
271 }
272
273 // The list is not sorted here since it has already stored in VirtualRadio.
274 // If the list is not sorted in advance, it should be sorted here.
275 const auto& current = mCurrentProgram;
276 auto found = std::lower_bound(list.begin(), list.end(), VirtualProgram({current}));
277 if (directionUp) {
278 if (found < list.end() - 1) {
279 if (tunesTo(current, found->selector)) found++;
280 } else {
281 found = list.begin();
282 }
283 } else {
284 if (found > list.begin() && found != list.end()) {
285 found--;
286 } else {
287 found = list.end() - 1;
288 }
289 }
290 const ProgramSelector tuneTo = found->selector;
291
292 mIsTuneCompleted = false;
293 auto task = [this, tuneTo, callback]() {
294 ProgramInfo programInfo = {};
295 {
296 lock_guard<mutex> lk(mMutex);
297 programInfo = tuneInternalLocked(tuneTo);
298 }
299 callback->onCurrentProgramInfoChanged(programInfo);
300 };
301 mThread->schedule(task, kSeekDelayTimeMs);
302
303 return ScopedAStatus::ok();
304}
305
306ScopedAStatus BroadcastRadio::step(bool directionUp) {
307 LOG(DEBUG) << __func__ << ": step " << (directionUp ? "up" : "down") << "...";
308
309 lock_guard<mutex> lk(mMutex);
310 if (mCallback == nullptr) {
311 LOG(ERROR) << __func__ << ": callback is not registered.";
312 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
313 resultToInt(Result::INVALID_STATE), "callback is not registered");
314 }
315
316 cancelLocked();
317
318 if (!utils::hasId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ)) {
319 LOG(WARNING) << __func__ << ": can't step in anything else than AM/FM";
320 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
321 resultToInt(Result::NOT_SUPPORTED), "cannot step in anything else than AM/FM");
322 }
323
324 int64_t stepTo = utils::getId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ);
325 std::optional<AmFmBandRange> range = getAmFmRangeLocked();
326 if (!range) {
327 LOG(ERROR) << __func__ << ": can't find current band or tune operation is in process";
328 ScopedAStatus::fromServiceSpecificErrorWithMessage(
329 resultToInt(Result::INTERNAL_ERROR),
330 "can't find current band or tune operation is in process");
331 }
332
333 if (directionUp) {
334 stepTo += range->spacing;
335 } else {
336 stepTo -= range->spacing;
337 }
338 if (stepTo > range->upperBound) {
339 stepTo = range->lowerBound;
340 }
341 if (stepTo < range->lowerBound) {
342 stepTo = range->upperBound;
343 }
344
345 mIsTuneCompleted = false;
346 std::shared_ptr<ITunerCallback> callback = mCallback;
347 auto task = [this, stepTo, callback]() {
348 ProgramInfo programInfo;
349 {
350 lock_guard<mutex> lk(mMutex);
351 programInfo = tuneInternalLocked(utils::makeSelectorAmfm(stepTo));
352 }
353 callback->onCurrentProgramInfoChanged(programInfo);
354 };
355 mThread->schedule(task, kStepDelayTimeMs);
356
357 return ScopedAStatus::ok();
358}
359
360void BroadcastRadio::cancelLocked() {
361 LOG(DEBUG) << __func__ << ": cancelling current operations...";
362
363 mThread->cancelAll();
364 if (mCurrentProgram.primaryId.type != IdentifierType::INVALID) {
365 mIsTuneCompleted = true;
366 }
367}
368
369ScopedAStatus BroadcastRadio::cancel() {
370 LOG(DEBUG) << __func__ << ": cancel pending tune, seek and step...";
371
372 lock_guard<mutex> lk(mMutex);
373 cancelLocked();
374
375 return ScopedAStatus::ok();
376}
377
378ScopedAStatus BroadcastRadio::startProgramListUpdates(const ProgramFilter& filter) {
379 LOG(DEBUG) << __func__ << ": requested program list updates, filter = " << filter.toString()
380 << "...";
381
382 auto filterCb = [&filter](const VirtualProgram& program) {
383 return utils::satisfies(filter, program.selector);
384 };
385
386 lock_guard<mutex> lk(mMutex);
387
388 const auto& list = mVirtualRadio.getProgramList();
389 vector<VirtualProgram> filteredList;
390 std::copy_if(list.begin(), list.end(), std::back_inserter(filteredList), filterCb);
391
392 auto task = [this, filteredList]() {
393 std::shared_ptr<ITunerCallback> callback;
394 {
395 lock_guard<mutex> lk(mMutex);
396 if (mCallback == nullptr) {
397 LOG(WARNING) << "Callback is null when updating program List";
398 return;
399 }
400 callback = mCallback;
401 }
402
403 ProgramListChunk chunk = {};
404 chunk.purge = true;
405 chunk.complete = true;
406 chunk.modified = vector<ProgramInfo>(filteredList.begin(), filteredList.end());
407
408 callback->onProgramListUpdated(chunk);
409 };
410 mThread->schedule(task, kListDelayTimeS);
411
412 return ScopedAStatus::ok();
413}
414
415ScopedAStatus BroadcastRadio::stopProgramListUpdates() {
416 LOG(DEBUG) << __func__ << ": requested program list updates to stop...";
417 // TODO(b/243681584) Implement stop program list updates method
418 return ScopedAStatus::ok();
419}
420
421ScopedAStatus BroadcastRadio::isConfigFlagSet(ConfigFlag flag, [[maybe_unused]] bool* returnIsSet) {
422 LOG(DEBUG) << __func__ << ": flag = " << toString(flag);
423
424 LOG(INFO) << __func__ << ": getting ConfigFlag is not supported";
425 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
426 resultToInt(Result::NOT_SUPPORTED), "getting ConfigFlag is not supported");
427}
428
429ScopedAStatus BroadcastRadio::setConfigFlag(ConfigFlag flag, bool value) {
430 LOG(DEBUG) << __func__ << ": flag = " << toString(flag) << ", value = " << value;
431
432 LOG(INFO) << __func__ << ": setting ConfigFlag is not supported";
433 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
434 resultToInt(Result::NOT_SUPPORTED), "setting ConfigFlag is not supported");
435}
436
437ScopedAStatus BroadcastRadio::setParameters(
438 [[maybe_unused]] const vector<VendorKeyValue>& parameters,
439 vector<VendorKeyValue>* returnParameters) {
440 // TODO(b/243682330) Support vendor parameter functionality
441 *returnParameters = {};
442 return ScopedAStatus::ok();
443}
444
445ScopedAStatus BroadcastRadio::getParameters([[maybe_unused]] const vector<string>& keys,
446 vector<VendorKeyValue>* returnParameters) {
447 // TODO(b/243682330) Support vendor parameter functionality
448 *returnParameters = {};
449 return ScopedAStatus::ok();
450}
451
452std::optional<AmFmBandRange> BroadcastRadio::getAmFmRangeLocked() const {
453 if (!mIsTuneCompleted) {
454 LOG(WARNING) << __func__ << ": tune operation is in process";
455 return {};
456 }
457 if (!utils::hasId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ)) {
458 LOG(WARNING) << __func__ << ": current program does not has AMFM_FREQUENCY_KHZ identifier";
459 return {};
460 }
461
462 int64_t freq = utils::getId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ);
463 for (const auto& range : mAmFmConfig.ranges) {
464 if (range.lowerBound <= freq && range.upperBound >= freq) {
465 return range;
466 }
467 }
468
469 return {};
470}
471
472ScopedAStatus BroadcastRadio::registerAnnouncementListener(
473 [[maybe_unused]] const std::shared_ptr<IAnnouncementListener>& listener,
474 const vector<AnnouncementType>& enabled, std::shared_ptr<ICloseHandle>* returnCloseHandle) {
475 LOG(DEBUG) << __func__ << ": registering announcement listener for "
476 << utils::vectorToString(enabled);
477
478 // TODO(b/243683842) Support announcement listener
479 *returnCloseHandle = nullptr;
480 LOG(INFO) << __func__ << ": registering announcementListener is not supported";
481 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
482 resultToInt(Result::NOT_SUPPORTED),
483 "registering announcementListener is not supported");
484}
485
Weilin Xu97203b02022-06-23 00:19:03 +0000486binder_status_t BroadcastRadio::dump(int fd, const char** args, uint32_t numArgs) {
487 if (numArgs == 0) {
488 return dumpsys(fd);
489 }
490
491 string option = string(args[0]);
492 if (EqualsIgnoreCase(option, "--help")) {
493 return cmdHelp(fd);
494 } else if (EqualsIgnoreCase(option, "--tune")) {
495 return cmdTune(fd, args, numArgs);
496 } else if (EqualsIgnoreCase(option, "--seek")) {
497 return cmdSeek(fd, args, numArgs);
498 } else if (EqualsIgnoreCase(option, "--step")) {
499 return cmdStep(fd, args, numArgs);
500 } else if (EqualsIgnoreCase(option, "--cancel")) {
501 return cmdCancel(fd, numArgs);
502 } else if (EqualsIgnoreCase(option, "--startProgramListUpdates")) {
503 return cmdStartProgramListUpdates(fd, args, numArgs);
504 } else if (EqualsIgnoreCase(option, "--stopProgramListUpdates")) {
505 return cmdStopProgramListUpdates(fd, numArgs);
506 }
507 dprintf(fd, "Invalid option: %s\n", option.c_str());
508 return STATUS_BAD_VALUE;
509}
510
511binder_status_t BroadcastRadio::dumpsys(int fd) {
512 if (!checkDumpCallerHasWritePermissions(fd)) {
513 return STATUS_PERMISSION_DENIED;
514 }
515 lock_guard<mutex> lk(mMutex);
516 dprintf(fd, "AmFmRegionConfig: %s\n", mAmFmConfig.toString().c_str());
517 dprintf(fd, "Properties: %s \n", mProperties.toString().c_str());
518 if (mIsTuneCompleted) {
519 dprintf(fd, "Tune completed\n");
520 } else {
521 dprintf(fd, "Tune not completed\n");
522 }
523 if (mCallback == nullptr) {
524 dprintf(fd, "No ITunerCallback registered\n");
525 } else {
526 dprintf(fd, "ITunerCallback registered\n");
527 }
528 dprintf(fd, "CurrentProgram: %s \n", mCurrentProgram.toString().c_str());
529 return STATUS_OK;
530}
531
532binder_status_t BroadcastRadio::cmdHelp(int fd) const {
533 dprintf(fd, "Usage: \n\n");
534 dprintf(fd, "[no args]: dumps focus listener / gain callback registered status\n");
535 dprintf(fd, "--help: shows this help\n");
536 dprintf(fd,
537 "--tune amfm <FREQUENCY>: tunes amfm radio to frequency (in Hz) specified: "
538 "frequency (int) \n"
539 "--tune dab <SID> <ENSEMBLE>: tunes dab radio to sid and ensemble specified: "
540 "sidExt (int), ensemble (int) \n");
541 dprintf(fd,
542 "--seek [up|down] <SKIP_SUB_CHANNEL>: seek with direction (up or down) and "
543 "option whether skipping sub channel: "
544 "skipSubChannel (string, should be either \"true\" or \"false\")\n");
545 dprintf(fd, "--step [up|down]: step in direction (up or down) specified\n");
546 dprintf(fd, "--cancel: cancel current pending tune, step, and seek\n");
547 dprintf(fd,
548 "--startProgramListUpdates <IDENTIFIER_TYPES> <IDENTIFIERS> <INCLUDE_CATEGORIES> "
549 "<EXCLUDE_MODIFICATIONS>: start update program list with the filter specified: "
550 "identifier types (string, in format <TYPE>,<TYPE>,...,<TYPE> or \"null\" (if empty), "
551 "where TYPE is int), "
552 "program identifiers (string, in format "
553 "<TYPE>:<VALUE>,<TYPE>:<VALUE>,...,<TYPE>:<VALUE> or \"null\" (if empty), "
554 "where TYPE is int and VALUE is long), "
555 "includeCategories (string, should be either \"true\" or \"false\"), "
556 "excludeModifications (string, should be either \"true\" or \"false\")\n");
557 dprintf(fd, "--stopProgramListUpdates: stop current pending program list updates\n");
558 dprintf(fd,
559 "Note on <TYPE> for --startProgramList command: it is int for identifier type. "
560 "Please see broadcastradio/aidl/android/hardware/broadcastradio/IdentifierType.aidl "
561 "for its definition.\n");
562 dprintf(fd,
563 "Note on <VALUE> for --startProgramList command: it is long type for identifier value. "
564 "Please see broadcastradio/aidl/android/hardware/broadcastradio/IdentifierType.aidl "
565 "for its value.\n");
566
567 return STATUS_OK;
568}
569
570binder_status_t BroadcastRadio::cmdTune(int fd, const char** args, uint32_t numArgs) {
571 if (!checkDumpCallerHasWritePermissions(fd)) {
572 return STATUS_PERMISSION_DENIED;
573 }
574 if (numArgs != 3 && numArgs != 4) {
575 dprintf(fd,
576 "Invalid number of arguments: please provide --tune amfm <FREQUENCY> "
577 "or --tune dab <SID> <ENSEMBLE>\n");
578 return STATUS_BAD_VALUE;
579 }
580 bool isDab = false;
581 if (EqualsIgnoreCase(string(args[1]), "dab")) {
582 isDab = true;
583 } else if (!EqualsIgnoreCase(string(args[1]), "amfm")) {
584 dprintf(fd, "Unknown radio type provided with tune: %s\n", args[1]);
585 return STATUS_BAD_VALUE;
586 }
587 ProgramSelector sel = {};
588 if (isDab) {
589 if (numArgs != 4) {
590 dprintf(fd,
591 "Invalid number of arguments: please provide --tune dab <SID> <ENSEMBLE>\n");
592 return STATUS_BAD_VALUE;
593 }
594 int sid;
595 if (!utils::parseArgInt(string(args[2]), &sid)) {
596 dprintf(fd, "Non-integer sid provided with tune: %s\n", string(args[2]).c_str());
597 return STATUS_BAD_VALUE;
598 }
599 int ensemble;
600 if (!utils::parseArgInt(string(args[3]), &ensemble)) {
601 dprintf(fd, "Non-integer ensemble provided with tune: %s\n", string(args[3]).c_str());
602 return STATUS_BAD_VALUE;
603 }
604 sel = utils::makeSelectorDab(sid, ensemble);
605 } else {
606 if (numArgs != 3) {
607 dprintf(fd, "Invalid number of arguments: please provide --tune amfm <FREQUENCY>\n");
608 return STATUS_BAD_VALUE;
609 }
610 int freq;
611 if (!utils::parseArgInt(string(args[2]), &freq)) {
612 dprintf(fd, "Non-integer frequency provided with tune: %s\n", string(args[2]).c_str());
613 return STATUS_BAD_VALUE;
614 }
615 sel = utils::makeSelectorAmfm(freq);
616 }
617
618 auto tuneResult = tune(sel);
619 if (!tuneResult.isOk()) {
620 dprintf(fd, "Unable to tune %s radio to %s\n", args[1], sel.toString().c_str());
621 return STATUS_BAD_VALUE;
622 }
623 dprintf(fd, "Tune %s radio to %s \n", args[1], sel.toString().c_str());
624 return STATUS_OK;
625}
626
627binder_status_t BroadcastRadio::cmdSeek(int fd, const char** args, uint32_t numArgs) {
628 if (!checkDumpCallerHasWritePermissions(fd)) {
629 return STATUS_PERMISSION_DENIED;
630 }
631 if (numArgs != 3) {
632 dprintf(fd,
633 "Invalid number of arguments: please provide --seek <DIRECTION> "
634 "<SKIP_SUB_CHANNEL>\n");
635 return STATUS_BAD_VALUE;
636 }
637 string seekDirectionIn = string(args[1]);
638 bool seekDirectionUp;
639 if (!utils::parseArgDirection(seekDirectionIn, &seekDirectionUp)) {
640 dprintf(fd, "Invalid direction (\"up\" or \"down\") provided with seek: %s\n",
641 seekDirectionIn.c_str());
642 return STATUS_BAD_VALUE;
643 }
644 string skipSubChannelIn = string(args[2]);
645 bool skipSubChannel;
646 if (!utils::parseArgBool(skipSubChannelIn, &skipSubChannel)) {
647 dprintf(fd, "Invalid skipSubChannel (\"true\" or \"false\") provided with seek: %s\n",
648 skipSubChannelIn.c_str());
649 return STATUS_BAD_VALUE;
650 }
651
652 auto seekResult = seek(seekDirectionUp, skipSubChannel);
653 if (!seekResult.isOk()) {
654 dprintf(fd, "Unable to seek in %s direction\n", seekDirectionIn.c_str());
655 return STATUS_BAD_VALUE;
656 }
657 dprintf(fd, "Seek in %s direction\n", seekDirectionIn.c_str());
658 return STATUS_OK;
659}
660
661binder_status_t BroadcastRadio::cmdStep(int fd, const char** args, uint32_t numArgs) {
662 if (!checkDumpCallerHasWritePermissions(fd)) {
663 return STATUS_PERMISSION_DENIED;
664 }
665 if (numArgs != 2) {
666 dprintf(fd, "Invalid number of arguments: please provide --step <DIRECTION>\n");
667 return STATUS_BAD_VALUE;
668 }
669 string stepDirectionIn = string(args[1]);
670 bool stepDirectionUp;
671 if (!utils::parseArgDirection(stepDirectionIn, &stepDirectionUp)) {
672 dprintf(fd, "Invalid direction (\"up\" or \"down\") provided with step: %s\n",
673 stepDirectionIn.c_str());
674 return STATUS_BAD_VALUE;
675 }
676
677 auto stepResult = step(stepDirectionUp);
678 if (!stepResult.isOk()) {
679 dprintf(fd, "Unable to step in %s direction\n", stepDirectionIn.c_str());
680 return STATUS_BAD_VALUE;
681 }
682 dprintf(fd, "Step in %s direction\n", stepDirectionIn.c_str());
683 return STATUS_OK;
684}
685
686binder_status_t BroadcastRadio::cmdCancel(int fd, uint32_t numArgs) {
687 if (!checkDumpCallerHasWritePermissions(fd)) {
688 return STATUS_PERMISSION_DENIED;
689 }
690 if (numArgs != 1) {
691 dprintf(fd,
692 "Invalid number of arguments: please provide --cancel "
693 "only and no more arguments\n");
694 return STATUS_BAD_VALUE;
695 }
696
697 auto cancelResult = cancel();
698 if (!cancelResult.isOk()) {
699 dprintf(fd, "Unable to cancel pending tune, seek, and step\n");
700 return STATUS_BAD_VALUE;
701 }
702 dprintf(fd, "Canceled pending tune, seek, and step\n");
703 return STATUS_OK;
704}
705
706binder_status_t BroadcastRadio::cmdStartProgramListUpdates(int fd, const char** args,
707 uint32_t numArgs) {
708 if (!checkDumpCallerHasWritePermissions(fd)) {
709 return STATUS_PERMISSION_DENIED;
710 }
711 if (numArgs != 5) {
712 dprintf(fd,
713 "Invalid number of arguments: please provide --startProgramListUpdates "
714 "<IDENTIFIER_TYPES> <IDENTIFIERS> <INCLUDE_CATEGORIES> "
715 "<EXCLUDE_MODIFICATIONS>\n");
716 return STATUS_BAD_VALUE;
717 }
718 string filterTypesStr = string(args[1]);
719 std::vector<IdentifierType> filterTypeList;
720 if (!EqualsIgnoreCase(filterTypesStr, "null") &&
721 !utils::parseArgIdentifierTypeArray(filterTypesStr, &filterTypeList)) {
722 dprintf(fd,
723 "Invalid identifier types provided with startProgramListUpdates: %s, "
724 "should be: <TYPE>,<TYPE>,...,<TYPE>\n",
725 filterTypesStr.c_str());
726 return STATUS_BAD_VALUE;
727 }
728 string filtersStr = string(args[2]);
729 std::vector<ProgramIdentifier> filterList;
730 if (!EqualsIgnoreCase(filtersStr, "null") &&
731 !utils::parseProgramIdentifierList(filtersStr, &filterList)) {
732 dprintf(fd,
733 "Invalid program identifiers provided with startProgramListUpdates: %s, "
734 "should be: <TYPE>:<VALUE>,<TYPE>:<VALUE>,...,<TYPE>:<VALUE>\n",
735 filtersStr.c_str());
736 return STATUS_BAD_VALUE;
737 }
738 string includeCategoriesStr = string(args[3]);
739 bool includeCategories;
740 if (!utils::parseArgBool(includeCategoriesStr, &includeCategories)) {
741 dprintf(fd,
742 "Invalid includeCategories (\"true\" or \"false\") "
743 "provided with startProgramListUpdates : %s\n",
744 includeCategoriesStr.c_str());
745 return STATUS_BAD_VALUE;
746 }
747 string excludeModificationsStr = string(args[4]);
748 bool excludeModifications;
749 if (!utils::parseArgBool(excludeModificationsStr, &excludeModifications)) {
750 dprintf(fd,
751 "Invalid excludeModifications(\"true\" or \"false\") "
752 "provided with startProgramListUpdates : %s\n",
753 excludeModificationsStr.c_str());
754 return STATUS_BAD_VALUE;
755 }
756 ProgramFilter filter = {filterTypeList, filterList, includeCategories, excludeModifications};
757
758 auto updateResult = startProgramListUpdates(filter);
759 if (!updateResult.isOk()) {
760 dprintf(fd, "Unable to start program list update for filter %s \n",
761 filter.toString().c_str());
762 return STATUS_BAD_VALUE;
763 }
764 dprintf(fd, "Start program list update for filter %s\n", filter.toString().c_str());
765 return STATUS_OK;
766}
767
768binder_status_t BroadcastRadio::cmdStopProgramListUpdates(int fd, uint32_t numArgs) {
769 if (!checkDumpCallerHasWritePermissions(fd)) {
770 return STATUS_PERMISSION_DENIED;
771 }
772 if (numArgs != 1) {
773 dprintf(fd,
774 "Invalid number of arguments: please provide --stopProgramListUpdates "
775 "only and no more arguments\n");
776 return STATUS_BAD_VALUE;
777 }
778
779 auto stopResult = stopProgramListUpdates();
780 if (!stopResult.isOk()) {
781 dprintf(fd, "Unable to stop pending program list update\n");
782 return STATUS_BAD_VALUE;
783 }
784 dprintf(fd, "Stop pending program list update\n");
785 return STATUS_OK;
786}
787
Weilin Xub2a6ca62022-05-08 23:47:04 +0000788} // namespace aidl::android::hardware::broadcastradio