blob: 22a697066ad2795eeb3b87eaeddd35db42e3b875 [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 {
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -070026namespace utils {
27
28using V1_0::Band;
Tomasz Wasilczykf679e8b2017-09-14 09:43:35 -070029using V1_1::ProgramIdentifier;
30using V1_1::ProgramSelector;
31using V1_1::ProgramType;
Tomasz Wasilczyk002151c2017-11-22 09:43:06 -080032using V1_2::IdentifierType;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -070033
34static bool isCompatibleProgramType(const uint32_t ia, const uint32_t ib) {
35 auto a = static_cast<ProgramType>(ia);
36 auto b = static_cast<ProgramType>(ib);
37
38 if (a == b) return true;
39 if (a == ProgramType::AM && b == ProgramType::AM_HD) return true;
40 if (a == ProgramType::AM_HD && b == ProgramType::AM) return true;
41 if (a == ProgramType::FM && b == ProgramType::FM_HD) return true;
42 if (a == ProgramType::FM_HD && b == ProgramType::FM) return true;
43 return false;
44}
45
46static bool bothHaveId(const ProgramSelector& a, const ProgramSelector& b,
47 const IdentifierType type) {
48 return hasId(a, type) && hasId(b, type);
49}
50
51static bool anyHaveId(const ProgramSelector& a, const ProgramSelector& b,
52 const IdentifierType type) {
53 return hasId(a, type) || hasId(b, type);
54}
55
56static bool haveEqualIds(const ProgramSelector& a, const ProgramSelector& b,
57 const IdentifierType type) {
58 if (!bothHaveId(a, b, type)) return false;
Tomasz Wasilczyk753c1d12017-07-25 14:55:49 -070059 /* We should check all Ids of a given type (ie. other AF),
60 * but it doesn't matter for default implementation.
61 */
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -070062 auto aId = getId(a, type);
63 auto bId = getId(b, type);
64 return aId == bId;
65}
66
67bool tunesTo(const ProgramSelector& a, const ProgramSelector& b) {
68 if (!isCompatibleProgramType(a.programType, b.programType)) return false;
69
70 auto type = getType(a);
71
72 switch (type) {
73 case ProgramType::AM:
74 case ProgramType::AM_HD:
75 case ProgramType::FM:
76 case ProgramType::FM_HD:
77 if (haveEqualIds(a, b, IdentifierType::HD_STATION_ID_EXT)) return true;
78
79 // if HD Radio subchannel is specified, it must match
80 if (anyHaveId(a, b, IdentifierType::HD_SUBCHANNEL)) {
81 // missing subchannel (analog) is an equivalent of first subchannel (MPS)
82 auto aCh = getId(a, IdentifierType::HD_SUBCHANNEL, 0);
83 auto bCh = getId(b, IdentifierType::HD_SUBCHANNEL, 0);
84 if (aCh != bCh) return false;
85 }
86
87 if (haveEqualIds(a, b, IdentifierType::RDS_PI)) return true;
88
89 return haveEqualIds(a, b, IdentifierType::AMFM_FREQUENCY);
90 case ProgramType::DAB:
Tomasz Wasilczyk002151c2017-11-22 09:43:06 -080091 return haveEqualIds(a, b, IdentifierType::DAB_SID_EXT);
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -070092 case ProgramType::DRMO:
93 return haveEqualIds(a, b, IdentifierType::DRMO_SERVICE_ID);
94 case ProgramType::SXM:
95 if (anyHaveId(a, b, IdentifierType::SXM_SERVICE_ID)) {
96 return haveEqualIds(a, b, IdentifierType::SXM_SERVICE_ID);
97 }
98 return haveEqualIds(a, b, IdentifierType::SXM_CHANNEL);
Tomasz Wasilczykda97a6d2017-08-04 12:57:29 -070099 default: // includes all vendor types
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700100 ALOGW("Unsupported program type: %s", toString(type).c_str());
101 return false;
102 }
103}
104
105ProgramType getType(const ProgramSelector& sel) {
106 return static_cast<ProgramType>(sel.programType);
107}
108
109bool isAmFm(const ProgramType type) {
110 switch (type) {
111 case ProgramType::AM:
112 case ProgramType::FM:
113 case ProgramType::AM_HD:
114 case ProgramType::FM_HD:
115 return true;
116 default:
117 return false;
118 }
119}
120
Tomasz Wasilczyk701a5bd2017-08-10 12:32:45 -0700121bool isAm(const Band band) {
122 return band == Band::AM || band == Band::AM_HD;
123}
124
125bool isFm(const Band band) {
126 return band == Band::FM || band == Band::FM_HD;
127}
128
Tomasz Wasilczyk002151c2017-11-22 09:43:06 -0800129static bool maybeGetId(const ProgramSelector& sel, const IdentifierType type, uint64_t* val) {
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700130 auto itype = static_cast<uint32_t>(type);
Tomasz Wasilczyk002151c2017-11-22 09:43:06 -0800131 auto itypeAlt = itype;
132 if (type == IdentifierType::DAB_SIDECC) {
133 itypeAlt = static_cast<uint32_t>(IdentifierType::DAB_SID_EXT);
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700134 }
Tomasz Wasilczyk002151c2017-11-22 09:43:06 -0800135 if (type == IdentifierType::DAB_SID_EXT) {
136 itypeAlt = static_cast<uint32_t>(IdentifierType::DAB_SIDECC);
137 }
138
139 if (sel.primaryId.type == itype || sel.primaryId.type == itypeAlt) {
140 if (val) *val = sel.primaryId.value;
141 return true;
142 }
143
144 // not optimal, but we don't care in default impl
145 bool gotAlt = false;
146 for (auto&& id : sel.secondaryIds) {
147 if (id.type == itype) {
148 if (val) *val = id.value;
149 return true;
150 }
151 // alternative identifier is a backup, we prefer original value
152 if (id.type == itypeAlt) {
153 if (val) *val = id.value;
154 gotAlt = true;
155 continue;
156 }
157 }
158
159 return gotAlt;
160}
161
162bool hasId(const ProgramSelector& sel, const IdentifierType type) {
163 return maybeGetId(sel, type, nullptr);
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700164}
165
166uint64_t getId(const ProgramSelector& sel, const IdentifierType type) {
Tomasz Wasilczyk002151c2017-11-22 09:43:06 -0800167 uint64_t val;
168
169 if (maybeGetId(sel, type, &val)) {
170 return val;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700171 }
Tomasz Wasilczyk002151c2017-11-22 09:43:06 -0800172
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700173 ALOGW("Identifier %s not found", toString(type).c_str());
174 return 0;
175}
176
177uint64_t getId(const ProgramSelector& sel, const IdentifierType type, uint64_t defval) {
178 if (!hasId(sel, type)) return defval;
179 return getId(sel, type);
180}
181
182ProgramSelector make_selector(Band band, uint32_t channel, uint32_t subChannel) {
183 ProgramSelector sel = {};
184
185 ALOGW_IF((subChannel > 0) && (band == Band::AM || band == Band::FM),
186 "got subChannel for non-HD AM/FM");
187
188 // we can't use ProgramType::AM_HD or FM_HD, because we don't know HD station ID
189 ProgramType type;
Tomasz Wasilczyk701a5bd2017-08-10 12:32:45 -0700190 if (isAm(band)) {
191 type = ProgramType::AM;
192 } else if (isFm(band)) {
193 type = ProgramType::FM;
194 } else {
195 LOG_ALWAYS_FATAL("Unsupported band: %s", toString(band).c_str());
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700196 }
197
198 sel.programType = static_cast<uint32_t>(type);
199 sel.primaryId.type = static_cast<uint32_t>(IdentifierType::AMFM_FREQUENCY);
200 sel.primaryId.value = channel;
201 if (subChannel > 0) {
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700202 /* stating sub channel for AM/FM channel does not give any guarantees,
203 * but we can't do much more without HD station ID
204 *
205 * The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
206 */
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700207 sel.secondaryIds = hidl_vec<ProgramIdentifier>{
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700208 {static_cast<uint32_t>(IdentifierType::HD_SUBCHANNEL), subChannel - 1},
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700209 };
210 }
211
212 return sel;
213}
214
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700215bool getLegacyChannel(const ProgramSelector& sel, uint32_t* channelOut, uint32_t* subChannelOut) {
216 if (channelOut) *channelOut = 0;
217 if (subChannelOut) *subChannelOut = 0;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700218 if (isAmFm(getType(sel))) {
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700219 if (channelOut) *channelOut = getId(sel, IdentifierType::AMFM_FREQUENCY);
220 if (subChannelOut && hasId(sel, IdentifierType::HD_SUBCHANNEL)) {
221 // The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
222 *subChannelOut = getId(sel, IdentifierType::HD_SUBCHANNEL) + 1;
223 }
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700224 return true;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700225 }
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700226 return false;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700227}
228
229bool isDigital(const ProgramSelector& sel) {
230 switch (getType(sel)) {
231 case ProgramType::AM:
232 case ProgramType::FM:
233 return false;
234 default:
235 // VENDOR might not be digital, but it doesn't matter for default impl.
236 return true;
237 }
238}
239
240} // namespace utils
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -0700241
242namespace V1_0 {
243
244bool operator==(const BandConfig& l, const BandConfig& r) {
245 if (l.type != r.type) return false;
246 if (l.antennaConnected != r.antennaConnected) return false;
247 if (l.lowerLimit != r.lowerLimit) return false;
248 if (l.upperLimit != r.upperLimit) return false;
249 if (l.spacings != r.spacings) return false;
Tomasz Wasilczykf679e8b2017-09-14 09:43:35 -0700250 if (utils::isAm(l.type)) {
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -0700251 return l.ext.am == r.ext.am;
Tomasz Wasilczykf679e8b2017-09-14 09:43:35 -0700252 } else if (utils::isFm(l.type)) {
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -0700253 return l.ext.fm == r.ext.fm;
254 } else {
255 ALOGW("Unsupported band config type: %s", toString(l.type).c_str());
256 return false;
257 }
258}
259
260} // namespace V1_0
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700261} // namespace broadcastradio
262} // namespace hardware
263} // namespace android