1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "BroadcastRadioDefault.utils"
//#define LOG_NDEBUG 0
#include <broadcastradio-utils-1x/Utils.h>
#include <log/log.h>
namespace android {
namespace hardware {
namespace broadcastradio {
namespace utils {
using V1_0::Band;
using V1_1::ProgramIdentifier;
using V1_1::ProgramSelector;
using V1_1::ProgramType;
using V1_2::IdentifierType;
static bool isCompatibleProgramType(const uint32_t ia, const uint32_t ib) {
auto a = static_cast<ProgramType>(ia);
auto b = static_cast<ProgramType>(ib);
if (a == b) return true;
if (a == ProgramType::AM && b == ProgramType::AM_HD) return true;
if (a == ProgramType::AM_HD && b == ProgramType::AM) return true;
if (a == ProgramType::FM && b == ProgramType::FM_HD) return true;
if (a == ProgramType::FM_HD && b == ProgramType::FM) return true;
return false;
}
static bool bothHaveId(const ProgramSelector& a, const ProgramSelector& b,
const IdentifierType type) {
return hasId(a, type) && hasId(b, type);
}
static bool anyHaveId(const ProgramSelector& a, const ProgramSelector& b,
const IdentifierType type) {
return hasId(a, type) || hasId(b, type);
}
static bool haveEqualIds(const ProgramSelector& a, const ProgramSelector& b,
const IdentifierType type) {
if (!bothHaveId(a, b, type)) return false;
/* We should check all Ids of a given type (ie. other AF),
* but it doesn't matter for default implementation.
*/
return getId(a, type) == getId(b, type);
}
bool tunesTo(const ProgramSelector& a, const ProgramSelector& b) {
if (!isCompatibleProgramType(a.programType, b.programType)) return false;
auto type = getType(a);
switch (type) {
case ProgramType::AM:
case ProgramType::AM_HD:
case ProgramType::FM:
case ProgramType::FM_HD:
if (haveEqualIds(a, b, IdentifierType::HD_STATION_ID_EXT)) return true;
// if HD Radio subchannel is specified, it must match
if (anyHaveId(a, b, IdentifierType::HD_SUBCHANNEL)) {
// missing subchannel (analog) is an equivalent of first subchannel (MPS)
auto aCh = getId(a, IdentifierType::HD_SUBCHANNEL, 0);
auto bCh = getId(b, IdentifierType::HD_SUBCHANNEL, 0);
if (aCh != bCh) return false;
}
if (haveEqualIds(a, b, IdentifierType::RDS_PI)) return true;
return haveEqualIds(a, b, IdentifierType::AMFM_FREQUENCY);
case ProgramType::DAB:
return haveEqualIds(a, b, IdentifierType::DAB_SID_EXT);
case ProgramType::DRMO:
return haveEqualIds(a, b, IdentifierType::DRMO_SERVICE_ID);
case ProgramType::SXM:
if (anyHaveId(a, b, IdentifierType::SXM_SERVICE_ID)) {
return haveEqualIds(a, b, IdentifierType::SXM_SERVICE_ID);
}
return haveEqualIds(a, b, IdentifierType::SXM_CHANNEL);
default: // includes all vendor types
ALOGW("Unsupported program type: %s", toString(type).c_str());
return false;
}
}
ProgramType getType(const ProgramSelector& sel) {
return static_cast<ProgramType>(sel.programType);
}
bool isAmFm(const ProgramType type) {
switch (type) {
case ProgramType::AM:
case ProgramType::FM:
case ProgramType::AM_HD:
case ProgramType::FM_HD:
return true;
default:
return false;
}
}
bool isAm(const Band band) {
return band == Band::AM || band == Band::AM_HD;
}
bool isFm(const Band band) {
return band == Band::FM || band == Band::FM_HD;
}
static bool maybeGetId(const ProgramSelector& sel, const IdentifierType type, uint64_t* val) {
auto itype = static_cast<uint32_t>(type);
auto itypeAlt = itype;
if (type == IdentifierType::DAB_SIDECC) {
itypeAlt = static_cast<uint32_t>(IdentifierType::DAB_SID_EXT);
}
if (type == IdentifierType::DAB_SID_EXT) {
itypeAlt = static_cast<uint32_t>(IdentifierType::DAB_SIDECC);
}
if (sel.primaryId.type == itype || sel.primaryId.type == itypeAlt) {
if (val) *val = sel.primaryId.value;
return true;
}
// not optimal, but we don't care in default impl
bool gotAlt = false;
for (auto&& id : sel.secondaryIds) {
if (id.type == itype) {
if (val) *val = id.value;
return true;
}
// alternative identifier is a backup, we prefer original value
if (id.type == itypeAlt) {
if (val) *val = id.value;
gotAlt = true;
continue;
}
}
return gotAlt;
}
bool hasId(const ProgramSelector& sel, const IdentifierType type) {
return maybeGetId(sel, type, nullptr);
}
uint64_t getId(const ProgramSelector& sel, const IdentifierType type) {
uint64_t val;
if (maybeGetId(sel, type, &val)) {
return val;
}
ALOGW("Identifier %s not found", toString(type).c_str());
return 0;
}
uint64_t getId(const ProgramSelector& sel, const IdentifierType type, uint64_t defval) {
if (!hasId(sel, type)) return defval;
return getId(sel, type);
}
ProgramSelector make_selector(Band band, uint32_t channel, uint32_t subChannel) {
ProgramSelector sel = {};
ALOGW_IF((subChannel > 0) && (band == Band::AM || band == Band::FM),
"got subChannel for non-HD AM/FM");
// we can't use ProgramType::AM_HD or FM_HD, because we don't know HD station ID
ProgramType type;
if (isAm(band)) {
type = ProgramType::AM;
} else if (isFm(band)) {
type = ProgramType::FM;
} else {
LOG_ALWAYS_FATAL("Unsupported band: %s", toString(band).c_str());
}
sel.programType = static_cast<uint32_t>(type);
sel.primaryId.type = static_cast<uint32_t>(IdentifierType::AMFM_FREQUENCY);
sel.primaryId.value = channel;
if (subChannel > 0) {
/* stating sub channel for AM/FM channel does not give any guarantees,
* but we can't do much more without HD station ID
*
* The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
*/
sel.secondaryIds = hidl_vec<ProgramIdentifier>{
{static_cast<uint32_t>(IdentifierType::HD_SUBCHANNEL), subChannel - 1},
};
}
return sel;
}
bool getLegacyChannel(const ProgramSelector& sel, uint32_t* channelOut, uint32_t* subChannelOut) {
if (channelOut) *channelOut = 0;
if (subChannelOut) *subChannelOut = 0;
if (isAmFm(getType(sel))) {
if (channelOut) *channelOut = getId(sel, IdentifierType::AMFM_FREQUENCY);
if (subChannelOut && hasId(sel, IdentifierType::HD_SUBCHANNEL)) {
// The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based.
*subChannelOut = getId(sel, IdentifierType::HD_SUBCHANNEL) + 1;
}
return true;
}
return false;
}
bool isDigital(const ProgramSelector& sel) {
switch (getType(sel)) {
case ProgramType::AM:
case ProgramType::FM:
return false;
default:
// VENDOR might not be digital, but it doesn't matter for default impl.
return true;
}
}
} // namespace utils
namespace V1_0 {
bool operator==(const BandConfig& l, const BandConfig& r) {
using namespace utils;
if (l.type != r.type) return false;
if (l.antennaConnected != r.antennaConnected) return false;
if (l.lowerLimit != r.lowerLimit) return false;
if (l.upperLimit != r.upperLimit) return false;
if (l.spacings != r.spacings) return false;
if (isAm(l.type)) {
return l.ext.am == r.ext.am;
} else if (isFm(l.type)) {
return l.ext.fm == r.ext.fm;
} else {
ALOGW("Unsupported band config type: %s", toString(l.type).c_str());
return false;
}
}
} // namespace V1_0
} // namespace broadcastradio
} // namespace hardware
} // namespace android
|