blob: 4dd6b139c4995aedb596af3ece77bca8b7215165 [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
Tomasz Wasilczyk701a5bd2017-08-10 12:32:45 -0700118bool isAm(const Band band) {
119 return band == Band::AM || band == Band::AM_HD;
120}
121
122bool isFm(const Band band) {
123 return band == Band::FM || band == Band::FM_HD;
124}
125
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700126bool hasId(const ProgramSelector& sel, const IdentifierType type) {
127 auto itype = static_cast<uint32_t>(type);
128 if (sel.primaryId.type == itype) return true;
129 // not optimal, but we don't care in default impl
130 for (auto&& id : sel.secondaryIds) {
131 if (id.type == itype) return true;
132 }
133 return false;
134}
135
136uint64_t getId(const ProgramSelector& sel, const IdentifierType type) {
137 auto itype = static_cast<uint32_t>(type);
138 if (sel.primaryId.type == itype) return sel.primaryId.value;
139 // not optimal, but we don't care in default impl
140 for (auto&& id : sel.secondaryIds) {
141 if (id.type == itype) return id.value;
142 }
143 ALOGW("Identifier %s not found", toString(type).c_str());
144 return 0;
145}
146
147uint64_t getId(const ProgramSelector& sel, const IdentifierType type, uint64_t defval) {
148 if (!hasId(sel, type)) return defval;
149 return getId(sel, type);
150}
151
152ProgramSelector make_selector(Band band, uint32_t channel, uint32_t subChannel) {
153 ProgramSelector sel = {};
154
155 ALOGW_IF((subChannel > 0) && (band == Band::AM || band == Band::FM),
156 "got subChannel for non-HD AM/FM");
157
158 // we can't use ProgramType::AM_HD or FM_HD, because we don't know HD station ID
159 ProgramType type;
Tomasz Wasilczyk701a5bd2017-08-10 12:32:45 -0700160 if (isAm(band)) {
161 type = ProgramType::AM;
162 } else if (isFm(band)) {
163 type = ProgramType::FM;
164 } else {
165 LOG_ALWAYS_FATAL("Unsupported band: %s", toString(band).c_str());
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700166 }
167
168 sel.programType = static_cast<uint32_t>(type);
169 sel.primaryId.type = static_cast<uint32_t>(IdentifierType::AMFM_FREQUENCY);
170 sel.primaryId.value = channel;
171 if (subChannel > 0) {
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700172 /* stating sub channel for AM/FM channel does not give any guarantees,
173 * but we can't do much more without HD station ID
174 *
175 * The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
176 */
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700177 sel.secondaryIds = hidl_vec<ProgramIdentifier>{
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700178 {static_cast<uint32_t>(IdentifierType::HD_SUBCHANNEL), subChannel - 1},
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700179 };
180 }
181
182 return sel;
183}
184
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700185bool getLegacyChannel(const ProgramSelector& sel, uint32_t* channelOut, uint32_t* subChannelOut) {
186 if (channelOut) *channelOut = 0;
187 if (subChannelOut) *subChannelOut = 0;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700188 if (isAmFm(getType(sel))) {
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700189 if (channelOut) *channelOut = getId(sel, IdentifierType::AMFM_FREQUENCY);
190 if (subChannelOut && hasId(sel, IdentifierType::HD_SUBCHANNEL)) {
191 // The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
192 *subChannelOut = getId(sel, IdentifierType::HD_SUBCHANNEL) + 1;
193 }
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700194 return true;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700195 }
Tomasz Wasilczyk2834b952017-07-12 14:17:09 -0700196 return false;
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700197}
198
199bool isDigital(const ProgramSelector& sel) {
200 switch (getType(sel)) {
201 case ProgramType::AM:
202 case ProgramType::FM:
203 return false;
204 default:
205 // VENDOR might not be digital, but it doesn't matter for default impl.
206 return true;
207 }
208}
209
210} // namespace utils
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700211} // namespace V1_1
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -0700212
213namespace V1_0 {
214
215bool operator==(const BandConfig& l, const BandConfig& r) {
216 if (l.type != r.type) return false;
217 if (l.antennaConnected != r.antennaConnected) return false;
218 if (l.lowerLimit != r.lowerLimit) return false;
219 if (l.upperLimit != r.upperLimit) return false;
220 if (l.spacings != r.spacings) return false;
Tomasz Wasilczyk701a5bd2017-08-10 12:32:45 -0700221 if (V1_1::utils::isAm(l.type)) {
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -0700222 return l.ext.am == r.ext.am;
Tomasz Wasilczyk701a5bd2017-08-10 12:32:45 -0700223 } else if (V1_1::utils::isFm(l.type)) {
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -0700224 return l.ext.fm == r.ext.fm;
225 } else {
226 ALOGW("Unsupported band config type: %s", toString(l.type).c_str());
227 return false;
228 }
229}
230
231} // namespace V1_0
Tomasz Wasilczyka02b6ef2017-07-05 11:23:30 -0700232} // namespace broadcastradio
233} // namespace hardware
234} // namespace android