blob: f82e767a7794d87dba3cfaf0d1346479749ffd24 [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 }
248 mIsTuneCompleted = true;
Weilin Xu48338962023-11-03 14:31:15 -0700249 if (adjustAmFmRangeLocked()) {
250 startProgramListUpdatesLocked({});
251 }
Weilin Xub2a6ca62022-05-08 23:47:04 +0000252
253 return programInfo;
254}
255
256ScopedAStatus BroadcastRadio::setTunerCallback(const std::shared_ptr<ITunerCallback>& callback) {
257 LOG(DEBUG) << __func__ << ": setTunerCallback";
258
259 if (callback == nullptr) {
260 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
261 resultToInt(Result::INVALID_ARGUMENTS), "cannot set tuner callback to null");
262 }
263
264 lock_guard<mutex> lk(mMutex);
265 mCallback = callback;
266
267 return ScopedAStatus::ok();
268}
269
270ScopedAStatus BroadcastRadio::unsetTunerCallback() {
271 LOG(DEBUG) << __func__ << ": unsetTunerCallback";
272
273 lock_guard<mutex> lk(mMutex);
274 mCallback = nullptr;
275
276 return ScopedAStatus::ok();
277}
278
279ScopedAStatus BroadcastRadio::tune(const ProgramSelector& program) {
280 LOG(DEBUG) << __func__ << ": tune to " << program.toString() << "...";
281
282 lock_guard<mutex> lk(mMutex);
283 if (mCallback == nullptr) {
284 LOG(ERROR) << __func__ << ": callback is not registered.";
285 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
286 resultToInt(Result::INVALID_STATE), "callback is not registered");
287 }
288
289 if (!utils::isSupported(mProperties, program)) {
290 LOG(WARNING) << __func__ << ": selector not supported: " << program.toString();
291 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
292 resultToInt(Result::NOT_SUPPORTED), "selector is not supported");
293 }
294
Weilin Xu31c541c2023-09-07 17:00:57 -0700295 if (!utils::isValidV2(program)) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000296 LOG(ERROR) << __func__ << ": selector is not valid: " << program.toString();
297 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
298 resultToInt(Result::INVALID_ARGUMENTS), "selector is not valid");
299 }
300
301 cancelLocked();
302
303 mIsTuneCompleted = false;
304 std::shared_ptr<ITunerCallback> callback = mCallback;
305 auto task = [this, program, callback]() {
306 ProgramInfo programInfo = {};
307 {
308 lock_guard<mutex> lk(mMutex);
309 programInfo = tuneInternalLocked(program);
310 }
311 callback->onCurrentProgramInfoChanged(programInfo);
312 };
Weilin Xud7483322022-11-29 01:12:36 +0000313 auto cancelTask = [program, callback]() { callback->onTuneFailed(Result::CANCELED, program); };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000314 mTuningThread->schedule(task, cancelTask, kTuneDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000315
316 return ScopedAStatus::ok();
317}
318
Weilin Xu39dd0f82023-09-07 17:00:57 -0700319bool BroadcastRadio::findNextLocked(const ProgramSelector& current, bool directionUp,
320 bool skipSubChannel, VirtualProgram* nextProgram) const {
321 if (mProgramList.empty()) {
322 return false;
323 }
324 // The list is not sorted here since it has already stored in VirtualRadio.
325 bool hasAmFmFrequency = utils::hasAmFmFrequency(current);
326 uint32_t currentFreq = hasAmFmFrequency ? utils::getAmFmFrequency(current) : 0;
327 auto found =
328 std::lower_bound(mProgramList.begin(), mProgramList.end(), VirtualProgram({current}));
329 if (directionUp) {
330 if (found < mProgramList.end() - 1) {
331 // When seeking up, tuner will jump to the first selector which is main program service
Weilin Xu48338962023-11-03 14:31:15 -0700332 // greater than and of the same band as the current program selector in the program
333 // list (if not exist, jump to the first selector in the same band) for skipping
334 // sub-channels case or AM/FM without HD radio enabled case. Otherwise, the tuner will
335 // jump to the first selector which is greater than and of the same band as the current
336 // program selector.
Weilin Xu39dd0f82023-09-07 17:00:57 -0700337 if (utils::tunesTo(current, found->selector)) found++;
338 if (skipSubChannel && hasAmFmFrequency) {
339 auto firstFound = found;
340 while (utils::getAmFmFrequency(found->selector) == currentFreq) {
341 if (found < mProgramList.end() - 1) {
342 found++;
343 } else {
344 found = mProgramList.begin();
345 }
346 if (found == firstFound) {
347 // Only one main channel exists in the program list, the tuner cannot skip
348 // sub-channel to the next program selector.
349 return false;
350 }
351 }
352 }
353 } else {
Weilin Xu48338962023-11-03 14:31:15 -0700354 // If the selector of current program is no less than all selectors of the same band or
355 // not found in the program list, seeking up should wrap the tuner to the first program
356 // selector of the same band in the program list.
Weilin Xu39dd0f82023-09-07 17:00:57 -0700357 found = mProgramList.begin();
358 }
359 } else {
360 if (found > mProgramList.begin() && found != mProgramList.end()) {
361 // When seeking down, tuner will jump to the first selector which is main program
Weilin Xu48338962023-11-03 14:31:15 -0700362 // service less than and of the same band as the current program selector in the
363 // program list (if not exist, jump to the last main program service selector of the
364 // same band) for skipping sub-channels case or AM/FM without HD radio enabled case.
365 // Otherwise, the tuner will jump to the first selector less than and of the same band
366 // as the current program selector.
Weilin Xu39dd0f82023-09-07 17:00:57 -0700367 found--;
368 if (hasAmFmFrequency && utils::hasAmFmFrequency(found->selector)) {
369 uint32_t nextFreq = utils::getAmFmFrequency(found->selector);
370 if (nextFreq != currentFreq) {
371 jumpToFirstSubChannelLocked(found);
372 } else if (skipSubChannel) {
373 jumpToFirstSubChannelLocked(found);
374 auto firstFound = found;
375 if (found > mProgramList.begin()) {
376 found--;
377 } else {
378 found = mProgramList.end() - 1;
379 }
380 jumpToFirstSubChannelLocked(found);
381 if (found == firstFound) {
382 // Only one main channel exists in the program list, the tuner cannot skip
383 // sub-channel to the next program selector.
384 return false;
385 }
386 }
387 }
388 } else {
Weilin Xu48338962023-11-03 14:31:15 -0700389 // If the selector of current program is no greater than all selectors of the same band
390 // or not found in the program list, seeking down should wrap the tuner to the last
391 // selector of the same band in the program list. If the last program selector in the
392 // program list is sub-channel and skipping sub-channels is needed, the tuner will jump
393 // to the last main program service of the same band in the program list.
Weilin Xu39dd0f82023-09-07 17:00:57 -0700394 found = mProgramList.end() - 1;
395 jumpToFirstSubChannelLocked(found);
396 }
397 }
398 *nextProgram = *found;
399 return true;
400}
401
402void BroadcastRadio::jumpToFirstSubChannelLocked(vector<VirtualProgram>::const_iterator& it) const {
403 if (!utils::hasAmFmFrequency(it->selector) || it == mProgramList.begin()) {
404 return;
405 }
406 uint32_t currentFrequency = utils::getAmFmFrequency(it->selector);
407 it--;
408 while (it != mProgramList.begin() && utils::hasAmFmFrequency(it->selector) &&
409 utils::getAmFmFrequency(it->selector) == currentFrequency) {
410 it--;
411 }
412 it++;
413}
414
Weilin Xub2a6ca62022-05-08 23:47:04 +0000415ScopedAStatus BroadcastRadio::seek(bool directionUp, bool skipSubChannel) {
416 LOG(DEBUG) << __func__ << ": seek " << (directionUp ? "up" : "down") << " with skipSubChannel? "
417 << (skipSubChannel ? "yes" : "no") << "...";
418
419 lock_guard<mutex> lk(mMutex);
420 if (mCallback == nullptr) {
421 LOG(ERROR) << __func__ << ": callback is not registered.";
422 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
423 resultToInt(Result::INVALID_STATE), "callback is not registered");
424 }
425
426 cancelLocked();
427
Weilin Xu48338962023-11-03 14:31:15 -0700428 auto filterCb = [this](const VirtualProgram& program) {
429 return isProgramInBand(program.selector, mCurrentAmFmBandRange,
430 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_FM),
431 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_AM));
432 };
433 const auto& list = mVirtualRadio.getProgramList();
434 mProgramList.clear();
435 std::copy_if(list.begin(), list.end(), std::back_inserter(mProgramList), filterCb);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000436 std::shared_ptr<ITunerCallback> callback = mCallback;
Weilin Xud7483322022-11-29 01:12:36 +0000437 auto cancelTask = [callback]() { callback->onTuneFailed(Result::CANCELED, {}); };
Weilin Xu39dd0f82023-09-07 17:00:57 -0700438
439 VirtualProgram nextProgram = {};
440 bool foundNext = findNextLocked(mCurrentProgram, directionUp, skipSubChannel, &nextProgram);
441 mIsTuneCompleted = false;
442 if (!foundNext) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000443 auto task = [callback]() {
444 LOG(DEBUG) << "seek: program list is empty, seek couldn't stop";
445
446 callback->onTuneFailed(Result::TIMEOUT, {});
447 };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000448 mTuningThread->schedule(task, cancelTask, kSeekDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000449
450 return ScopedAStatus::ok();
451 }
452
Weilin Xu39dd0f82023-09-07 17:00:57 -0700453 auto task = [this, nextProgram, callback]() {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000454 ProgramInfo programInfo = {};
455 {
456 lock_guard<mutex> lk(mMutex);
Weilin Xu39dd0f82023-09-07 17:00:57 -0700457 programInfo = tuneInternalLocked(nextProgram.selector);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000458 }
459 callback->onCurrentProgramInfoChanged(programInfo);
460 };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000461 mTuningThread->schedule(task, cancelTask, kSeekDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000462
463 return ScopedAStatus::ok();
464}
465
466ScopedAStatus BroadcastRadio::step(bool directionUp) {
467 LOG(DEBUG) << __func__ << ": step " << (directionUp ? "up" : "down") << "...";
468
469 lock_guard<mutex> lk(mMutex);
470 if (mCallback == nullptr) {
471 LOG(ERROR) << __func__ << ": callback is not registered.";
472 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
473 resultToInt(Result::INVALID_STATE), "callback is not registered");
474 }
475
476 cancelLocked();
477
Weilin Xu39dd0f82023-09-07 17:00:57 -0700478 int64_t stepTo;
479 if (utils::hasId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ)) {
480 stepTo = utils::getId(mCurrentProgram, IdentifierType::AMFM_FREQUENCY_KHZ);
481 } else if (mCurrentProgram.primaryId.type == IdentifierType::HD_STATION_ID_EXT) {
482 stepTo = utils::getHdFrequency(mCurrentProgram);
483 } else {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000484 LOG(WARNING) << __func__ << ": can't step in anything else than AM/FM";
485 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
486 resultToInt(Result::NOT_SUPPORTED), "cannot step in anything else than AM/FM");
487 }
488
Weilin Xu48338962023-11-03 14:31:15 -0700489 if (!mCurrentAmFmBandRange.has_value()) {
490 LOG(ERROR) << __func__ << ": can't find current band";
Weilin Xuee546a62023-11-14 12:57:50 -0800491 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
Weilin Xu48338962023-11-03 14:31:15 -0700492 resultToInt(Result::INTERNAL_ERROR), "can't find current band");
Weilin Xub2a6ca62022-05-08 23:47:04 +0000493 }
494
495 if (directionUp) {
Weilin Xu48338962023-11-03 14:31:15 -0700496 stepTo += mCurrentAmFmBandRange->spacing;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000497 } else {
Weilin Xu48338962023-11-03 14:31:15 -0700498 stepTo -= mCurrentAmFmBandRange->spacing;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000499 }
Weilin Xu48338962023-11-03 14:31:15 -0700500 if (stepTo > mCurrentAmFmBandRange->upperBound) {
501 stepTo = mCurrentAmFmBandRange->lowerBound;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000502 }
Weilin Xu48338962023-11-03 14:31:15 -0700503 if (stepTo < mCurrentAmFmBandRange->lowerBound) {
504 stepTo = mCurrentAmFmBandRange->upperBound;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000505 }
506
507 mIsTuneCompleted = false;
508 std::shared_ptr<ITunerCallback> callback = mCallback;
509 auto task = [this, stepTo, callback]() {
510 ProgramInfo programInfo;
511 {
512 lock_guard<mutex> lk(mMutex);
513 programInfo = tuneInternalLocked(utils::makeSelectorAmfm(stepTo));
514 }
515 callback->onCurrentProgramInfoChanged(programInfo);
516 };
Weilin Xud7483322022-11-29 01:12:36 +0000517 auto cancelTask = [callback]() { callback->onTuneFailed(Result::CANCELED, {}); };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000518 mTuningThread->schedule(task, cancelTask, kStepDelayTimeMs);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000519
520 return ScopedAStatus::ok();
521}
522
523void BroadcastRadio::cancelLocked() {
Weilin Xu764fe0d2023-07-26 18:07:05 +0000524 LOG(DEBUG) << __func__ << ": cancelling current tuning operations...";
Weilin Xub2a6ca62022-05-08 23:47:04 +0000525
Weilin Xu764fe0d2023-07-26 18:07:05 +0000526 mTuningThread->cancelAll();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000527 if (mCurrentProgram.primaryId.type != IdentifierType::INVALID) {
528 mIsTuneCompleted = true;
529 }
530}
531
532ScopedAStatus BroadcastRadio::cancel() {
533 LOG(DEBUG) << __func__ << ": cancel pending tune, seek and step...";
534
535 lock_guard<mutex> lk(mMutex);
536 cancelLocked();
537
538 return ScopedAStatus::ok();
539}
540
Weilin Xu48338962023-11-03 14:31:15 -0700541void BroadcastRadio::startProgramListUpdatesLocked(const ProgramFilter& filter) {
542 auto filterCb = [&filter, this](const VirtualProgram& program) {
543 return utils::satisfies(filter, program.selector) &&
544 isProgramInBand(program.selector, mCurrentAmFmBandRange,
545 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_FM),
546 isConfigFlagSetLocked(ConfigFlag::FORCE_ANALOG_AM));
Weilin Xub2a6ca62022-05-08 23:47:04 +0000547 };
548
Weilin Xu764fe0d2023-07-26 18:07:05 +0000549 cancelProgramListUpdateLocked();
550
Weilin Xub2a6ca62022-05-08 23:47:04 +0000551 const auto& list = mVirtualRadio.getProgramList();
552 vector<VirtualProgram> filteredList;
553 std::copy_if(list.begin(), list.end(), std::back_inserter(filteredList), filterCb);
554
555 auto task = [this, filteredList]() {
556 std::shared_ptr<ITunerCallback> callback;
557 {
558 lock_guard<mutex> lk(mMutex);
559 if (mCallback == nullptr) {
560 LOG(WARNING) << "Callback is null when updating program List";
561 return;
562 }
563 callback = mCallback;
564 }
565
566 ProgramListChunk chunk = {};
567 chunk.purge = true;
568 chunk.complete = true;
569 chunk.modified = vector<ProgramInfo>(filteredList.begin(), filteredList.end());
570
571 callback->onProgramListUpdated(chunk);
572 };
Weilin Xu764fe0d2023-07-26 18:07:05 +0000573 mProgramListThread->schedule(task, kListDelayTimeS);
Weilin Xu48338962023-11-03 14:31:15 -0700574}
575
576ScopedAStatus BroadcastRadio::startProgramListUpdates(const ProgramFilter& filter) {
577 LOG(DEBUG) << __func__ << ": requested program list updates, filter = " << filter.toString()
578 << "...";
579
580 lock_guard<mutex> lk(mMutex);
581
582 startProgramListUpdatesLocked(filter);
Weilin Xub2a6ca62022-05-08 23:47:04 +0000583
584 return ScopedAStatus::ok();
585}
586
Weilin Xu764fe0d2023-07-26 18:07:05 +0000587void BroadcastRadio::cancelProgramListUpdateLocked() {
588 LOG(DEBUG) << __func__ << ": cancelling current program list update operations...";
589 mProgramListThread->cancelAll();
590}
591
Weilin Xub2a6ca62022-05-08 23:47:04 +0000592ScopedAStatus BroadcastRadio::stopProgramListUpdates() {
593 LOG(DEBUG) << __func__ << ": requested program list updates to stop...";
Weilin Xu764fe0d2023-07-26 18:07:05 +0000594 lock_guard<mutex> lk(mMutex);
595 cancelProgramListUpdateLocked();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000596 return ScopedAStatus::ok();
597}
598
Weilin Xu48338962023-11-03 14:31:15 -0700599bool BroadcastRadio::isConfigFlagSetLocked(ConfigFlag flag) const {
600 int flagBit = static_cast<int>(flag);
601 return ((mConfigFlagValues >> flagBit) & 1) == 1;
602}
603
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000604ScopedAStatus BroadcastRadio::isConfigFlagSet(ConfigFlag flag, bool* returnIsSet) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000605 LOG(DEBUG) << __func__ << ": flag = " << toString(flag);
606
Weilin Xu48338962023-11-03 14:31:15 -0700607 if (flag == ConfigFlag::FORCE_ANALOG) {
608 flag = ConfigFlag::FORCE_ANALOG_FM;
609 }
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000610 lock_guard<mutex> lk(mMutex);
Weilin Xu48338962023-11-03 14:31:15 -0700611 *returnIsSet = isConfigFlagSetLocked(flag);
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000612 return ScopedAStatus::ok();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000613}
614
615ScopedAStatus BroadcastRadio::setConfigFlag(ConfigFlag flag, bool value) {
616 LOG(DEBUG) << __func__ << ": flag = " << toString(flag) << ", value = " << value;
617
Weilin Xu48338962023-11-03 14:31:15 -0700618 if (flag == ConfigFlag::FORCE_ANALOG) {
619 flag = ConfigFlag::FORCE_ANALOG_FM;
620 }
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000621 int flagBitMask = 1 << (static_cast<int>(flag));
622 lock_guard<mutex> lk(mMutex);
623 if (value) {
624 mConfigFlagValues |= flagBitMask;
625 } else {
626 mConfigFlagValues &= ~flagBitMask;
627 }
Weilin Xu48338962023-11-03 14:31:15 -0700628 if (flag == ConfigFlag::FORCE_ANALOG_AM || flag == ConfigFlag::FORCE_ANALOG_FM) {
629 startProgramListUpdatesLocked({});
630 }
Weilin Xu3bd4d9b2023-07-19 00:38:57 +0000631 return ScopedAStatus::ok();
Weilin Xub2a6ca62022-05-08 23:47:04 +0000632}
633
634ScopedAStatus BroadcastRadio::setParameters(
635 [[maybe_unused]] const vector<VendorKeyValue>& parameters,
636 vector<VendorKeyValue>* returnParameters) {
637 // TODO(b/243682330) Support vendor parameter functionality
638 *returnParameters = {};
639 return ScopedAStatus::ok();
640}
641
642ScopedAStatus BroadcastRadio::getParameters([[maybe_unused]] const vector<string>& keys,
643 vector<VendorKeyValue>* returnParameters) {
644 // TODO(b/243682330) Support vendor parameter functionality
645 *returnParameters = {};
646 return ScopedAStatus::ok();
647}
648
Weilin Xu48338962023-11-03 14:31:15 -0700649bool BroadcastRadio::adjustAmFmRangeLocked() {
650 bool hasBandBefore = mCurrentAmFmBandRange.has_value();
Weilin Xu39dd0f82023-09-07 17:00:57 -0700651 if (!utils::hasAmFmFrequency(mCurrentProgram)) {
Weilin Xub2a6ca62022-05-08 23:47:04 +0000652 LOG(WARNING) << __func__ << ": current program does not has AMFM_FREQUENCY_KHZ identifier";
Weilin Xu48338962023-11-03 14:31:15 -0700653 mCurrentAmFmBandRange.reset();
654 return hasBandBefore;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000655 }
656
Weilin Xu48338962023-11-03 14:31:15 -0700657 int32_t freq = static_cast<int32_t>(utils::getAmFmFrequency(mCurrentProgram));
Weilin Xub2a6ca62022-05-08 23:47:04 +0000658 for (const auto& range : mAmFmConfig.ranges) {
659 if (range.lowerBound <= freq && range.upperBound >= freq) {
Weilin Xu48338962023-11-03 14:31:15 -0700660 bool isBandChanged = hasBandBefore ? *mCurrentAmFmBandRange != range : true;
661 mCurrentAmFmBandRange = range;
662 return isBandChanged;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000663 }
664 }
665
Weilin Xu48338962023-11-03 14:31:15 -0700666 mCurrentAmFmBandRange.reset();
667 return !hasBandBefore;
Weilin Xub2a6ca62022-05-08 23:47:04 +0000668}
669
670ScopedAStatus BroadcastRadio::registerAnnouncementListener(
671 [[maybe_unused]] const std::shared_ptr<IAnnouncementListener>& listener,
672 const vector<AnnouncementType>& enabled, std::shared_ptr<ICloseHandle>* returnCloseHandle) {
673 LOG(DEBUG) << __func__ << ": registering announcement listener for "
674 << utils::vectorToString(enabled);
675
676 // TODO(b/243683842) Support announcement listener
677 *returnCloseHandle = nullptr;
678 LOG(INFO) << __func__ << ": registering announcementListener is not supported";
679 return ScopedAStatus::fromServiceSpecificErrorWithMessage(
680 resultToInt(Result::NOT_SUPPORTED),
681 "registering announcementListener is not supported");
682}
683
Weilin Xu97203b02022-06-23 00:19:03 +0000684binder_status_t BroadcastRadio::dump(int fd, const char** args, uint32_t numArgs) {
685 if (numArgs == 0) {
686 return dumpsys(fd);
687 }
688
689 string option = string(args[0]);
690 if (EqualsIgnoreCase(option, "--help")) {
691 return cmdHelp(fd);
692 } else if (EqualsIgnoreCase(option, "--tune")) {
693 return cmdTune(fd, args, numArgs);
694 } else if (EqualsIgnoreCase(option, "--seek")) {
695 return cmdSeek(fd, args, numArgs);
696 } else if (EqualsIgnoreCase(option, "--step")) {
697 return cmdStep(fd, args, numArgs);
698 } else if (EqualsIgnoreCase(option, "--cancel")) {
699 return cmdCancel(fd, numArgs);
700 } else if (EqualsIgnoreCase(option, "--startProgramListUpdates")) {
701 return cmdStartProgramListUpdates(fd, args, numArgs);
702 } else if (EqualsIgnoreCase(option, "--stopProgramListUpdates")) {
703 return cmdStopProgramListUpdates(fd, numArgs);
704 }
705 dprintf(fd, "Invalid option: %s\n", option.c_str());
706 return STATUS_BAD_VALUE;
707}
708
709binder_status_t BroadcastRadio::dumpsys(int fd) {
710 if (!checkDumpCallerHasWritePermissions(fd)) {
711 return STATUS_PERMISSION_DENIED;
712 }
713 lock_guard<mutex> lk(mMutex);
714 dprintf(fd, "AmFmRegionConfig: %s\n", mAmFmConfig.toString().c_str());
715 dprintf(fd, "Properties: %s \n", mProperties.toString().c_str());
716 if (mIsTuneCompleted) {
717 dprintf(fd, "Tune completed\n");
718 } else {
719 dprintf(fd, "Tune not completed\n");
720 }
721 if (mCallback == nullptr) {
722 dprintf(fd, "No ITunerCallback registered\n");
723 } else {
724 dprintf(fd, "ITunerCallback registered\n");
725 }
726 dprintf(fd, "CurrentProgram: %s \n", mCurrentProgram.toString().c_str());
727 return STATUS_OK;
728}
729
730binder_status_t BroadcastRadio::cmdHelp(int fd) const {
731 dprintf(fd, "Usage: \n\n");
732 dprintf(fd, "[no args]: dumps focus listener / gain callback registered status\n");
733 dprintf(fd, "--help: shows this help\n");
734 dprintf(fd,
735 "--tune amfm <FREQUENCY>: tunes amfm radio to frequency (in Hz) specified: "
736 "frequency (int) \n"
737 "--tune dab <SID> <ENSEMBLE>: tunes dab radio to sid and ensemble specified: "
738 "sidExt (int), ensemble (int) \n");
739 dprintf(fd,
740 "--seek [up|down] <SKIP_SUB_CHANNEL>: seek with direction (up or down) and "
741 "option whether skipping sub channel: "
742 "skipSubChannel (string, should be either \"true\" or \"false\")\n");
743 dprintf(fd, "--step [up|down]: step in direction (up or down) specified\n");
744 dprintf(fd, "--cancel: cancel current pending tune, step, and seek\n");
745 dprintf(fd,
746 "--startProgramListUpdates <IDENTIFIER_TYPES> <IDENTIFIERS> <INCLUDE_CATEGORIES> "
747 "<EXCLUDE_MODIFICATIONS>: start update program list with the filter specified: "
748 "identifier types (string, in format <TYPE>,<TYPE>,...,<TYPE> or \"null\" (if empty), "
749 "where TYPE is int), "
750 "program identifiers (string, in format "
751 "<TYPE>:<VALUE>,<TYPE>:<VALUE>,...,<TYPE>:<VALUE> or \"null\" (if empty), "
752 "where TYPE is int and VALUE is long), "
753 "includeCategories (string, should be either \"true\" or \"false\"), "
754 "excludeModifications (string, should be either \"true\" or \"false\")\n");
755 dprintf(fd, "--stopProgramListUpdates: stop current pending program list updates\n");
756 dprintf(fd,
757 "Note on <TYPE> for --startProgramList command: it is int for identifier type. "
758 "Please see broadcastradio/aidl/android/hardware/broadcastradio/IdentifierType.aidl "
759 "for its definition.\n");
760 dprintf(fd,
761 "Note on <VALUE> for --startProgramList command: it is long type for identifier value. "
762 "Please see broadcastradio/aidl/android/hardware/broadcastradio/IdentifierType.aidl "
763 "for its value.\n");
764
765 return STATUS_OK;
766}
767
768binder_status_t BroadcastRadio::cmdTune(int fd, const char** args, uint32_t numArgs) {
769 if (!checkDumpCallerHasWritePermissions(fd)) {
770 return STATUS_PERMISSION_DENIED;
771 }
772 if (numArgs != 3 && numArgs != 4) {
773 dprintf(fd,
774 "Invalid number of arguments: please provide --tune amfm <FREQUENCY> "
775 "or --tune dab <SID> <ENSEMBLE>\n");
776 return STATUS_BAD_VALUE;
777 }
778 bool isDab = false;
779 if (EqualsIgnoreCase(string(args[1]), "dab")) {
780 isDab = true;
781 } else if (!EqualsIgnoreCase(string(args[1]), "amfm")) {
782 dprintf(fd, "Unknown radio type provided with tune: %s\n", args[1]);
783 return STATUS_BAD_VALUE;
784 }
785 ProgramSelector sel = {};
786 if (isDab) {
Weilin Xu64cb9632023-03-15 23:46:43 +0000787 if (numArgs != 5 && numArgs != 3) {
Weilin Xu97203b02022-06-23 00:19:03 +0000788 dprintf(fd,
Weilin Xu0d4207d2022-12-09 00:37:44 +0000789 "Invalid number of arguments: please provide "
Weilin Xu64cb9632023-03-15 23:46:43 +0000790 "--tune dab <SID> <ENSEMBLE> <FREQUENCY> or "
791 "--tune dab <SID>\n");
Weilin Xu97203b02022-06-23 00:19:03 +0000792 return STATUS_BAD_VALUE;
793 }
794 int sid;
795 if (!utils::parseArgInt(string(args[2]), &sid)) {
Weilin Xu0d4207d2022-12-09 00:37:44 +0000796 dprintf(fd, "Non-integer sid provided with tune: %s\n", args[2]);
Weilin Xu97203b02022-06-23 00:19:03 +0000797 return STATUS_BAD_VALUE;
798 }
Weilin Xu64cb9632023-03-15 23:46:43 +0000799 if (numArgs == 3) {
800 sel = utils::makeSelectorDab(sid);
801 } else {
802 int ensemble;
803 if (!utils::parseArgInt(string(args[3]), &ensemble)) {
804 dprintf(fd, "Non-integer ensemble provided with tune: %s\n", args[3]);
805 return STATUS_BAD_VALUE;
806 }
807 int freq;
808 if (!utils::parseArgInt(string(args[4]), &freq)) {
809 dprintf(fd, "Non-integer frequency provided with tune: %s\n", args[4]);
810 return STATUS_BAD_VALUE;
811 }
812 sel = utils::makeSelectorDab(sid, ensemble, freq);
Weilin Xu97203b02022-06-23 00:19:03 +0000813 }
Weilin Xu97203b02022-06-23 00:19:03 +0000814 } else {
815 if (numArgs != 3) {
816 dprintf(fd, "Invalid number of arguments: please provide --tune amfm <FREQUENCY>\n");
817 return STATUS_BAD_VALUE;
818 }
819 int freq;
820 if (!utils::parseArgInt(string(args[2]), &freq)) {
Weilin Xu0d4207d2022-12-09 00:37:44 +0000821 dprintf(fd, "Non-integer frequency provided with tune: %s\n", args[2]);
Weilin Xu97203b02022-06-23 00:19:03 +0000822 return STATUS_BAD_VALUE;
823 }
824 sel = utils::makeSelectorAmfm(freq);
825 }
826
827 auto tuneResult = tune(sel);
828 if (!tuneResult.isOk()) {
829 dprintf(fd, "Unable to tune %s radio to %s\n", args[1], sel.toString().c_str());
830 return STATUS_BAD_VALUE;
831 }
832 dprintf(fd, "Tune %s radio to %s \n", args[1], sel.toString().c_str());
833 return STATUS_OK;
834}
835
836binder_status_t BroadcastRadio::cmdSeek(int fd, const char** args, uint32_t numArgs) {
837 if (!checkDumpCallerHasWritePermissions(fd)) {
838 return STATUS_PERMISSION_DENIED;
839 }
840 if (numArgs != 3) {
841 dprintf(fd,
842 "Invalid number of arguments: please provide --seek <DIRECTION> "
843 "<SKIP_SUB_CHANNEL>\n");
844 return STATUS_BAD_VALUE;
845 }
846 string seekDirectionIn = string(args[1]);
847 bool seekDirectionUp;
848 if (!utils::parseArgDirection(seekDirectionIn, &seekDirectionUp)) {
849 dprintf(fd, "Invalid direction (\"up\" or \"down\") provided with seek: %s\n",
850 seekDirectionIn.c_str());
851 return STATUS_BAD_VALUE;
852 }
853 string skipSubChannelIn = string(args[2]);
854 bool skipSubChannel;
855 if (!utils::parseArgBool(skipSubChannelIn, &skipSubChannel)) {
856 dprintf(fd, "Invalid skipSubChannel (\"true\" or \"false\") provided with seek: %s\n",
857 skipSubChannelIn.c_str());
858 return STATUS_BAD_VALUE;
859 }
860
861 auto seekResult = seek(seekDirectionUp, skipSubChannel);
862 if (!seekResult.isOk()) {
863 dprintf(fd, "Unable to seek in %s direction\n", seekDirectionIn.c_str());
864 return STATUS_BAD_VALUE;
865 }
866 dprintf(fd, "Seek in %s direction\n", seekDirectionIn.c_str());
867 return STATUS_OK;
868}
869
870binder_status_t BroadcastRadio::cmdStep(int fd, const char** args, uint32_t numArgs) {
871 if (!checkDumpCallerHasWritePermissions(fd)) {
872 return STATUS_PERMISSION_DENIED;
873 }
874 if (numArgs != 2) {
875 dprintf(fd, "Invalid number of arguments: please provide --step <DIRECTION>\n");
876 return STATUS_BAD_VALUE;
877 }
878 string stepDirectionIn = string(args[1]);
879 bool stepDirectionUp;
880 if (!utils::parseArgDirection(stepDirectionIn, &stepDirectionUp)) {
881 dprintf(fd, "Invalid direction (\"up\" or \"down\") provided with step: %s\n",
882 stepDirectionIn.c_str());
883 return STATUS_BAD_VALUE;
884 }
885
886 auto stepResult = step(stepDirectionUp);
887 if (!stepResult.isOk()) {
888 dprintf(fd, "Unable to step in %s direction\n", stepDirectionIn.c_str());
889 return STATUS_BAD_VALUE;
890 }
891 dprintf(fd, "Step in %s direction\n", stepDirectionIn.c_str());
892 return STATUS_OK;
893}
894
895binder_status_t BroadcastRadio::cmdCancel(int fd, uint32_t numArgs) {
896 if (!checkDumpCallerHasWritePermissions(fd)) {
897 return STATUS_PERMISSION_DENIED;
898 }
899 if (numArgs != 1) {
900 dprintf(fd,
901 "Invalid number of arguments: please provide --cancel "
902 "only and no more arguments\n");
903 return STATUS_BAD_VALUE;
904 }
905
906 auto cancelResult = cancel();
907 if (!cancelResult.isOk()) {
908 dprintf(fd, "Unable to cancel pending tune, seek, and step\n");
909 return STATUS_BAD_VALUE;
910 }
911 dprintf(fd, "Canceled pending tune, seek, and step\n");
912 return STATUS_OK;
913}
914
915binder_status_t BroadcastRadio::cmdStartProgramListUpdates(int fd, const char** args,
916 uint32_t numArgs) {
917 if (!checkDumpCallerHasWritePermissions(fd)) {
918 return STATUS_PERMISSION_DENIED;
919 }
920 if (numArgs != 5) {
921 dprintf(fd,
922 "Invalid number of arguments: please provide --startProgramListUpdates "
923 "<IDENTIFIER_TYPES> <IDENTIFIERS> <INCLUDE_CATEGORIES> "
924 "<EXCLUDE_MODIFICATIONS>\n");
925 return STATUS_BAD_VALUE;
926 }
927 string filterTypesStr = string(args[1]);
928 std::vector<IdentifierType> filterTypeList;
929 if (!EqualsIgnoreCase(filterTypesStr, "null") &&
930 !utils::parseArgIdentifierTypeArray(filterTypesStr, &filterTypeList)) {
931 dprintf(fd,
932 "Invalid identifier types provided with startProgramListUpdates: %s, "
933 "should be: <TYPE>,<TYPE>,...,<TYPE>\n",
934 filterTypesStr.c_str());
935 return STATUS_BAD_VALUE;
936 }
937 string filtersStr = string(args[2]);
938 std::vector<ProgramIdentifier> filterList;
939 if (!EqualsIgnoreCase(filtersStr, "null") &&
940 !utils::parseProgramIdentifierList(filtersStr, &filterList)) {
941 dprintf(fd,
942 "Invalid program identifiers provided with startProgramListUpdates: %s, "
943 "should be: <TYPE>:<VALUE>,<TYPE>:<VALUE>,...,<TYPE>:<VALUE>\n",
944 filtersStr.c_str());
945 return STATUS_BAD_VALUE;
946 }
947 string includeCategoriesStr = string(args[3]);
948 bool includeCategories;
949 if (!utils::parseArgBool(includeCategoriesStr, &includeCategories)) {
950 dprintf(fd,
951 "Invalid includeCategories (\"true\" or \"false\") "
952 "provided with startProgramListUpdates : %s\n",
953 includeCategoriesStr.c_str());
954 return STATUS_BAD_VALUE;
955 }
956 string excludeModificationsStr = string(args[4]);
957 bool excludeModifications;
958 if (!utils::parseArgBool(excludeModificationsStr, &excludeModifications)) {
959 dprintf(fd,
960 "Invalid excludeModifications(\"true\" or \"false\") "
961 "provided with startProgramListUpdates : %s\n",
962 excludeModificationsStr.c_str());
963 return STATUS_BAD_VALUE;
964 }
965 ProgramFilter filter = {filterTypeList, filterList, includeCategories, excludeModifications};
966
967 auto updateResult = startProgramListUpdates(filter);
968 if (!updateResult.isOk()) {
969 dprintf(fd, "Unable to start program list update for filter %s \n",
970 filter.toString().c_str());
971 return STATUS_BAD_VALUE;
972 }
973 dprintf(fd, "Start program list update for filter %s\n", filter.toString().c_str());
974 return STATUS_OK;
975}
976
977binder_status_t BroadcastRadio::cmdStopProgramListUpdates(int fd, uint32_t numArgs) {
978 if (!checkDumpCallerHasWritePermissions(fd)) {
979 return STATUS_PERMISSION_DENIED;
980 }
981 if (numArgs != 1) {
982 dprintf(fd,
983 "Invalid number of arguments: please provide --stopProgramListUpdates "
984 "only and no more arguments\n");
985 return STATUS_BAD_VALUE;
986 }
987
988 auto stopResult = stopProgramListUpdates();
989 if (!stopResult.isOk()) {
990 dprintf(fd, "Unable to stop pending program list update\n");
991 return STATUS_BAD_VALUE;
992 }
993 dprintf(fd, "Stop pending program list update\n");
994 return STATUS_OK;
995}
996
Weilin Xub2a6ca62022-05-08 23:47:04 +0000997} // namespace aidl::android::hardware::broadcastradio