1 /*
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 "VirtualRadio.h"
18 #include <broadcastradio-utils-aidl/Utils.h>
19 #include <unordered_set>
20 
21 namespace aidl::android::hardware::broadcastradio {
22 
23 using ::aidl::android::hardware::broadcastradio::utils::makeSelectorAmfm;
24 using ::aidl::android::hardware::broadcastradio::utils::makeSelectorDab;
25 using ::aidl::android::hardware::broadcastradio::utils::makeSelectorHd;
26 using ::std::string;
27 using ::std::unordered_set;
28 using ::std::vector;
29 
VirtualRadio(const string & name,const vector<VirtualProgram> & initialList)30 VirtualRadio::VirtualRadio(const string& name, const vector<VirtualProgram>& initialList)
31     : mName(name), mPrograms(initialList) {
32     sort(mPrograms.begin(), mPrograms.end());
33 }
34 
getName() const35 string VirtualRadio::getName() const {
36     return mName;
37 }
38 
getProgramList() const39 const vector<VirtualProgram>& VirtualRadio::getProgramList() const {
40     return mPrograms;
41 }
42 
getProgram(const ProgramSelector & selector,VirtualProgram * programOut) const43 bool VirtualRadio::getProgram(const ProgramSelector& selector, VirtualProgram* programOut) const {
44     for (auto it = mPrograms.begin(); it != mPrograms.end(); it++) {
45         if (!utils::tunesTo(selector, it->selector)) {
46             continue;
47         }
48         auto firstMatchIt = it;
49         if (utils::hasAmFmFrequency(it->selector)) {
50             uint32_t channelFreq = utils::getAmFmFrequency(it->selector);
51             it++;
52             while (it != mPrograms.end() && utils::hasAmFmFrequency(it->selector) &&
53                    utils::getAmFmFrequency(it->selector) == channelFreq) {
54                 if (it->selector == selector) {
55                     *programOut = *it;
56                     return true;
57                 }
58                 it++;
59             }
60         }
61         *programOut = *firstMatchIt;
62         return true;
63     }
64     return false;
65 }
66 
getSupportedIdentifierTypes() const67 vector<IdentifierType> VirtualRadio::getSupportedIdentifierTypes() const {
68     unordered_set<IdentifierType> supportedIdentifierTypeSet;
69     for (const auto& program : mPrograms) {
70         IdentifierType type = program.selector.primaryId.type;
71         if (supportedIdentifierTypeSet.count(type)) {
72             continue;
73         }
74         supportedIdentifierTypeSet.insert(type);
75     }
76     vector<IdentifierType> supportedIdentifierTypes(supportedIdentifierTypeSet.begin(),
77                                                     supportedIdentifierTypeSet.end());
78     return supportedIdentifierTypes;
79 }
80 
81 // get singleton of AMFM Virtual Radio
getAmFmRadio()82 const VirtualRadio& VirtualRadio::getAmFmRadio() {
83     // clang-format off
84     static VirtualRadio amFmRadioMock(
85         "AM/FM radio mock",
86         {
87             {makeSelectorAmfm(/* frequency= */ 94900u), "Wild 94.9", "Drake ft. Rihanna",
88                 "Too Good"},
89             {makeSelectorAmfm(/* frequency= */ 96500u), "KOIT", "Celine Dion", "All By Myself"},
90             {makeSelectorAmfm(/* frequency= */ 101300u), "101-3 KISS-FM", "Justin Timberlake",
91                 "Rock Your Body"},
92             {makeSelectorAmfm(/* frequency= */ 103700u), "iHeart80s @ 103.7", "Michael Jackson",
93                 "Billie Jean"},
94             {makeSelectorAmfm(/* frequency= */ 106100u), "106 KMEL", "Drake", "Marvins Room"},
95             {makeSelectorAmfm(/* frequency= */ 560u), "Talk Radio 560 KSFO", "Artist560", "Title560"},
96             {makeSelectorAmfm(/* frequency= */ 680u), "KNBR 680", "Artist680", "Title680"},
97             {makeSelectorAmfm(/* frequency= */ 97300u), "Alice@97.3", "Drops of Jupiter", "Train"},
98             {makeSelectorAmfm(/* frequency= */ 99700u), "99.7 Now!", "The Chainsmokers", "Closer"},
99             {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 0u, /* frequency= */ 97700u),
100                 "K-LOVE", "ArtistHd0", "TitleHd0"},
101             {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 1u, /* frequency= */ 97700u),
102                 "Air1", "ArtistHd1", "TitleHd1"},
103             {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 2u, /* frequency= */ 97700u),
104                 "K-LOVE Classics", "ArtistHd2", "TitleHd2"},
105             {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 0u, /* frequency= */ 98500u),
106                 "98.5-1 South Bay's Classic Rock", "ArtistHd0", "TitleHd0"},
107             {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 1u, /* frequency= */ 98500u),
108                 "Highway 1 - Different", "ArtistHd1", "TitleHd1"},
109             {makeSelectorHd(/* stationId= */ 0xB0000001u, /* subChannel= */ 0u, /* frequency= */ 1170u),
110                 "KLOK", "ArtistHd1", "TitleHd1"},
111         });
112     // clang-format on
113     return amFmRadioMock;
114 }
115 
116 // get singleton of DAB Virtual Radio
getDabRadio()117 const VirtualRadio& VirtualRadio::getDabRadio() {
118     // clang-format off
119     static VirtualRadio dabRadioMock(
120         "DAB radio mock",
121         {
122             {makeSelectorDab(/* sidExt= */ 0x0E10000C221u, /* ensemble= */ 0xCE15u,
123                 /* freq= */ 225648u), "BBC Radio 1", "Khalid", "Talk"},
124             {makeSelectorDab(/* sidExt= */ 0x0E10000C222u, /* ensemble= */ 0xCE15u,
125                     /* freq= */ 225648u), "BBC Radio 2", "Khalid", "Talk"},
126             {makeSelectorDab(/* sidExt= */ 0xE10000C224u, /* ensemble= */ 0xCE15u,
127                     /* freq= */ 225648u), "BBC Radio 4", "ArtistBBC1", "TitleCountry1"},
128             {makeSelectorDab(/* sidExt= */ 0x1E10000C224u, /* ensemble= */ 0xCE15u,
129                     /* freq= */ 225648u), "BBC Radio 4 LW", "ArtistBBC2", "TitleCountry2"},
130             {makeSelectorDab(/* sidExt= */ 0x0E10000C21Au, /* ensemble= */ 0xC181u,
131                 /* freq= */ 222064u), "Classic FM", "Jean Sibelius", "Andante Festivo"},
132             {makeSelectorDab(/* sidExt= */ 0x0E10000C1C0u, /* ensemble= */ 0xC181u,
133                 /* freq= */ 223936u), "Absolute Radio", "Coldplay", "Clocks"},
134             {makeSelectorDab(/* sidExt= */ 0x0E10000C1C0u, /* ensemble= */ 0xC181u,
135                 /* freq= */ 222064u), "Absolute Radio", "Coldplay", "Clocks"},
136             {makeSelectorDab(/* sidExt= */ 0x0E10000CCE7u, /* ensemble= */ 0xC19Du,
137                     /* freq= */ 218640u), "Absolute Radio Country", "ArtistCountry1", "TitleCountry1"},
138             {makeSelectorDab(/* sidExt= */ 0x0E10000CCE7u, /* ensemble= */ 0xC1A0u,
139                     /* freq= */ 218640u), "Absolute Radio Country", "ArtistCountry2", "TitleCountry2"},
140         });
141     // clang-format on
142     return dabRadioMock;
143 }
144 
145 }  // namespace aidl::android::hardware::broadcastradio
146