blob: 8ccd98e2f102943ac973c3d6ac38c14966dc5d33 [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 Wasilczykda97a6d2017-08-04 12:57:29 -070096 default: // includes all vendor types
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -070097 ALOGW("Unsupported program type: %s", toString(type).c_str());
98 return false;
99 }
100}
101
102ProgramType getType(const ProgramSelector& sel) {
103 return static_cast<ProgramType>(sel.programType);
104}
105
106bool isAmFm(const ProgramType type) {
107 switch (type) {
108 case ProgramType::AM:
109 case ProgramType::FM:
110 case ProgramType::AM_HD:
111 case ProgramType::FM_HD:
112 return true;
113 default:
114 return false;
115 }
116}
117
118bool hasId(const ProgramSelector& sel, const IdentifierType type) {
119 auto itype = static_cast<uint32_t>(type);
120 if (sel.primaryId.type == itype) return true;
121 // not optimal, but we don't care in default impl
122 for (auto&& id : sel.secondaryIds) {
123 if (id.type == itype) return true;
124 }
125 return false;
126}
127
128uint64_t getId(const ProgramSelector& sel, const IdentifierType type) {
129 auto itype = static_cast<uint32_t>(type);
130 if (sel.primaryId.type == itype) return sel.primaryId.value;
131 // not optimal, but we don't care in default impl
132 for (auto&& id : sel.secondaryIds) {
133 if (id.type == itype) return id.value;
134 }
135 ALOGW("Identifier %s not found", toString(type).c_str());
136 return 0;
137}
138
139uint64_t getId(const ProgramSelector& sel, const IdentifierType type, uint64_t defval) {
140 if (!hasId(sel, type)) return defval;
141 return getId(sel, type);
142}
143
144ProgramSelector make_selector(Band band, uint32_t channel, uint32_t subChannel) {
145 ProgramSelector sel = {};
146
147 ALOGW_IF((subChannel > 0) && (band == Band::AM || band == Band::FM),
148 "got subChannel for non-HD AM/FM");
149
150 // we can't use ProgramType::AM_HD or FM_HD, because we don't know HD station ID
151 ProgramType type;
152 switch (band) {
153 case Band::AM:
154 case Band::AM_HD:
155 type = ProgramType::AM;
156 break;
157 case Band::FM:
158 case Band::FM_HD:
159 type = ProgramType::FM;
160 break;
161 default:
162 LOG_ALWAYS_FATAL("Unsupported band: %s", toString(band).c_str());
163 }
164
165 sel.programType = static_cast<uint32_t>(type);
166 sel.primaryId.type = static_cast<uint32_t>(IdentifierType::AMFM_FREQUENCY);
167 sel.primaryId.value = channel;
168 if (subChannel > 0) {
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700169 /* stating sub channel for AM/FM channel does not give any guarantees,
170 * but we can't do much more without HD station ID
171 *
172 * The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
173 */
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700174 sel.secondaryIds = hidl_vec<ProgramIdentifier>{
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700175 {static_cast<uint32_t>(IdentifierType::HD_SUBCHANNEL), subChannel - 1},
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700176 };
177 }
178
179 return sel;
180}
181
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700182bool getLegacyChannel(const ProgramSelector& sel, uint32_t* channelOut, uint32_t* subChannelOut) {
183 if (channelOut) *channelOut = 0;
184 if (subChannelOut) *subChannelOut = 0;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700185 if (isAmFm(getType(sel))) {
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700186 if (channelOut) *channelOut = getId(sel, IdentifierType::AMFM_FREQUENCY);
187 if (subChannelOut && hasId(sel, IdentifierType::HD_SUBCHANNEL)) {
188 // The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
189 *subChannelOut = getId(sel, IdentifierType::HD_SUBCHANNEL) + 1;
190 }
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700191 return true;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700192 }
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700193 return false;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700194}
195
196bool isDigital(const ProgramSelector& sel) {
197 switch (getType(sel)) {
198 case ProgramType::AM:
199 case ProgramType::FM:
200 return false;
201 default:
202 // VENDOR might not be digital, but it doesn't matter for default impl.
203 return true;
204 }
205}
206
207} // namespace utils
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700208} // namespace V1_1
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -0700209
210namespace V1_0 {
211
212bool operator==(const BandConfig& l, const BandConfig& r) {
213 if (l.type != r.type) return false;
214 if (l.antennaConnected != r.antennaConnected) return false;
215 if (l.lowerLimit != r.lowerLimit) return false;
216 if (l.upperLimit != r.upperLimit) return false;
217 if (l.spacings != r.spacings) return false;
218 if (l.type == Band::AM || l.type == Band::AM_HD) {
219 return l.ext.am == r.ext.am;
220 } else if (l.type == Band::FM || l.type == Band::FM_HD) {
221 return l.ext.fm == r.ext.fm;
222 } else {
223 ALOGW("Unsupported band config type: %s", toString(l.type).c_str());
224 return false;
225 }
226}
227
228} // namespace V1_0
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700229} // namespace broadcastradio
230} // namespace hardware
231} // namespace android