blob: 50a407c7b78c1f997d78ce8fd2b89728c3505ec5 [file] [log] [blame]
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -07001/*
2 * Copyright (C) 2017 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#define LOG_TAG "BroadcastRadioDefault.utils"
17//#define LOG_NDEBUG 0
18
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -070019#include <broadcastradio-utils/Utils.h>
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -070020
21#include <log/log.h>
22
23namespace android {
24namespace hardware {
25namespace broadcastradio {
26namespace V1_1 {
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -070027namespace utils {
28
29using V1_0::Band;
30
31static bool isCompatibleProgramType(const uint32_t ia, const uint32_t ib) {
32 auto a = static_cast<ProgramType>(ia);
33 auto b = static_cast<ProgramType>(ib);
34
35 if (a == b) return true;
36 if (a == ProgramType::AM && b == ProgramType::AM_HD) return true;
37 if (a == ProgramType::AM_HD && b == ProgramType::AM) return true;
38 if (a == ProgramType::FM && b == ProgramType::FM_HD) return true;
39 if (a == ProgramType::FM_HD && b == ProgramType::FM) return true;
40 return false;
41}
42
43static bool bothHaveId(const ProgramSelector& a, const ProgramSelector& b,
44 const IdentifierType type) {
45 return hasId(a, type) && hasId(b, type);
46}
47
48static bool anyHaveId(const ProgramSelector& a, const ProgramSelector& b,
49 const IdentifierType type) {
50 return hasId(a, type) || hasId(b, type);
51}
52
53static bool haveEqualIds(const ProgramSelector& a, const ProgramSelector& b,
54 const IdentifierType type) {
55 if (!bothHaveId(a, b, type)) return false;
Tomasz Wasilczyk753c1d12017-07-25 14:55:49 -070056 /* We should check all Ids of a given type (ie. other AF),
57 * but it doesn't matter for default implementation.
58 */
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -070059 auto aId = getId(a, type);
60 auto bId = getId(b, type);
61 return aId == bId;
62}
63
64bool tunesTo(const ProgramSelector& a, const ProgramSelector& b) {
65 if (!isCompatibleProgramType(a.programType, b.programType)) return false;
66
67 auto type = getType(a);
68
69 switch (type) {
70 case ProgramType::AM:
71 case ProgramType::AM_HD:
72 case ProgramType::FM:
73 case ProgramType::FM_HD:
74 if (haveEqualIds(a, b, IdentifierType::HD_STATION_ID_EXT)) return true;
75
76 // if HD Radio subchannel is specified, it must match
77 if (anyHaveId(a, b, IdentifierType::HD_SUBCHANNEL)) {
78 // missing subchannel (analog) is an equivalent of first subchannel (MPS)
79 auto aCh = getId(a, IdentifierType::HD_SUBCHANNEL, 0);
80 auto bCh = getId(b, IdentifierType::HD_SUBCHANNEL, 0);
81 if (aCh != bCh) return false;
82 }
83
84 if (haveEqualIds(a, b, IdentifierType::RDS_PI)) return true;
85
86 return haveEqualIds(a, b, IdentifierType::AMFM_FREQUENCY);
87 case ProgramType::DAB:
88 return haveEqualIds(a, b, IdentifierType::DAB_SIDECC);
89 case ProgramType::DRMO:
90 return haveEqualIds(a, b, IdentifierType::DRMO_SERVICE_ID);
91 case ProgramType::SXM:
92 if (anyHaveId(a, b, IdentifierType::SXM_SERVICE_ID)) {
93 return haveEqualIds(a, b, IdentifierType::SXM_SERVICE_ID);
94 }
95 return haveEqualIds(a, b, IdentifierType::SXM_CHANNEL);
Tomasz Wasilczykd167caf2017-07-17 16:15:25 -070096 case ProgramType::VENDOR1:
97 case ProgramType::VENDOR2:
98 case ProgramType::VENDOR3:
99 case ProgramType::VENDOR4:
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700100 default:
101 ALOGW("Unsupported program type: %s", toString(type).c_str());
102 return false;
103 }
104}
105
106ProgramType getType(const ProgramSelector& sel) {
107 return static_cast<ProgramType>(sel.programType);
108}
109
110bool isAmFm(const ProgramType type) {
111 switch (type) {
112 case ProgramType::AM:
113 case ProgramType::FM:
114 case ProgramType::AM_HD:
115 case ProgramType::FM_HD:
116 return true;
117 default:
118 return false;
119 }
120}
121
122bool hasId(const ProgramSelector& sel, const IdentifierType type) {
123 auto itype = static_cast<uint32_t>(type);
124 if (sel.primaryId.type == itype) return true;
125 // not optimal, but we don't care in default impl
126 for (auto&& id : sel.secondaryIds) {
127 if (id.type == itype) return true;
128 }
129 return false;
130}
131
132uint64_t getId(const ProgramSelector& sel, const IdentifierType type) {
133 auto itype = static_cast<uint32_t>(type);
134 if (sel.primaryId.type == itype) return sel.primaryId.value;
135 // not optimal, but we don't care in default impl
136 for (auto&& id : sel.secondaryIds) {
137 if (id.type == itype) return id.value;
138 }
139 ALOGW("Identifier %s not found", toString(type).c_str());
140 return 0;
141}
142
143uint64_t getId(const ProgramSelector& sel, const IdentifierType type, uint64_t defval) {
144 if (!hasId(sel, type)) return defval;
145 return getId(sel, type);
146}
147
148ProgramSelector make_selector(Band band, uint32_t channel, uint32_t subChannel) {
149 ProgramSelector sel = {};
150
151 ALOGW_IF((subChannel > 0) && (band == Band::AM || band == Band::FM),
152 "got subChannel for non-HD AM/FM");
153
154 // we can't use ProgramType::AM_HD or FM_HD, because we don't know HD station ID
155 ProgramType type;
156 switch (band) {
157 case Band::AM:
158 case Band::AM_HD:
159 type = ProgramType::AM;
160 break;
161 case Band::FM:
162 case Band::FM_HD:
163 type = ProgramType::FM;
164 break;
165 default:
166 LOG_ALWAYS_FATAL("Unsupported band: %s", toString(band).c_str());
167 }
168
169 sel.programType = static_cast<uint32_t>(type);
170 sel.primaryId.type = static_cast<uint32_t>(IdentifierType::AMFM_FREQUENCY);
171 sel.primaryId.value = channel;
172 if (subChannel > 0) {
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700173 /* stating sub channel for AM/FM channel does not give any guarantees,
174 * but we can't do much more without HD station ID
175 *
176 * The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
177 */
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700178 sel.secondaryIds = hidl_vec<ProgramIdentifier>{
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700179 {static_cast<uint32_t>(IdentifierType::HD_SUBCHANNEL), subChannel - 1},
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700180 };
181 }
182
183 return sel;
184}
185
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700186bool getLegacyChannel(const ProgramSelector& sel, uint32_t* channelOut, uint32_t* subChannelOut) {
187 if (channelOut) *channelOut = 0;
188 if (subChannelOut) *subChannelOut = 0;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700189 if (isAmFm(getType(sel))) {
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700190 if (channelOut) *channelOut = getId(sel, IdentifierType::AMFM_FREQUENCY);
191 if (subChannelOut && hasId(sel, IdentifierType::HD_SUBCHANNEL)) {
192 // The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
193 *subChannelOut = getId(sel, IdentifierType::HD_SUBCHANNEL) + 1;
194 }
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700195 return true;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700196 }
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700197 return false;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700198}
199
200bool isDigital(const ProgramSelector& sel) {
201 switch (getType(sel)) {
202 case ProgramType::AM:
203 case ProgramType::FM:
204 return false;
205 default:
206 // VENDOR might not be digital, but it doesn't matter for default impl.
207 return true;
208 }
209}
210
211} // namespace utils
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700212} // namespace V1_1
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -0700213
214namespace V1_0 {
215
216bool operator==(const BandConfig& l, const BandConfig& r) {
217 if (l.type != r.type) return false;
218 if (l.antennaConnected != r.antennaConnected) return false;
219 if (l.lowerLimit != r.lowerLimit) return false;
220 if (l.upperLimit != r.upperLimit) return false;
221 if (l.spacings != r.spacings) return false;
222 if (l.type == Band::AM || l.type == Band::AM_HD) {
223 return l.ext.am == r.ext.am;
224 } else if (l.type == Band::FM || l.type == Band::FM_HD) {
225 return l.ext.fm == r.ext.fm;
226 } else {
227 ALOGW("Unsupported band config type: %s", toString(l.type).c_str());
228 return false;
229 }
230}
231
232} // namespace V1_0
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700233} // namespace broadcastradio
234} // namespace hardware
235} // namespace android