Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 19 | #include <broadcastradio-utils-1x/Utils.h> |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 20 | |
| 21 | #include <log/log.h> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace broadcastradio { |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 26 | namespace utils { |
| 27 | |
| 28 | using V1_0::Band; |
Tomasz Wasilczyk | 8c34c81 | 2018-02-08 13:42:31 -0800 | [diff] [blame] | 29 | using V1_1::IdentifierType; |
Tomasz Wasilczyk | f679e8b | 2017-09-14 09:43:35 -0700 | [diff] [blame] | 30 | using V1_1::ProgramIdentifier; |
| 31 | using V1_1::ProgramSelector; |
| 32 | using V1_1::ProgramType; |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 33 | |
| 34 | static 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 | |
| 46 | static bool bothHaveId(const ProgramSelector& a, const ProgramSelector& b, |
| 47 | const IdentifierType type) { |
| 48 | return hasId(a, type) && hasId(b, type); |
| 49 | } |
| 50 | |
| 51 | static bool anyHaveId(const ProgramSelector& a, const ProgramSelector& b, |
| 52 | const IdentifierType type) { |
| 53 | return hasId(a, type) || hasId(b, type); |
| 54 | } |
| 55 | |
| 56 | static bool haveEqualIds(const ProgramSelector& a, const ProgramSelector& b, |
| 57 | const IdentifierType type) { |
| 58 | if (!bothHaveId(a, b, type)) return false; |
Tomasz Wasilczyk | 753c1d1 | 2017-07-25 14:55:49 -0700 | [diff] [blame] | 59 | /* We should check all Ids of a given type (ie. other AF), |
| 60 | * but it doesn't matter for default implementation. |
| 61 | */ |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 62 | return getId(a, type) == getId(b, type); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool tunesTo(const ProgramSelector& a, const ProgramSelector& b) { |
| 66 | if (!isCompatibleProgramType(a.programType, b.programType)) return false; |
| 67 | |
| 68 | auto type = getType(a); |
| 69 | |
| 70 | switch (type) { |
| 71 | case ProgramType::AM: |
| 72 | case ProgramType::AM_HD: |
| 73 | case ProgramType::FM: |
| 74 | case ProgramType::FM_HD: |
| 75 | if (haveEqualIds(a, b, IdentifierType::HD_STATION_ID_EXT)) return true; |
| 76 | |
| 77 | // if HD Radio subchannel is specified, it must match |
| 78 | if (anyHaveId(a, b, IdentifierType::HD_SUBCHANNEL)) { |
| 79 | // missing subchannel (analog) is an equivalent of first subchannel (MPS) |
| 80 | auto aCh = getId(a, IdentifierType::HD_SUBCHANNEL, 0); |
| 81 | auto bCh = getId(b, IdentifierType::HD_SUBCHANNEL, 0); |
| 82 | if (aCh != bCh) return false; |
| 83 | } |
| 84 | |
| 85 | if (haveEqualIds(a, b, IdentifierType::RDS_PI)) return true; |
| 86 | |
| 87 | return haveEqualIds(a, b, IdentifierType::AMFM_FREQUENCY); |
| 88 | case ProgramType::DAB: |
Tomasz Wasilczyk | 8c34c81 | 2018-02-08 13:42:31 -0800 | [diff] [blame] | 89 | return haveEqualIds(a, b, IdentifierType::DAB_SIDECC); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 90 | case ProgramType::DRMO: |
| 91 | return haveEqualIds(a, b, IdentifierType::DRMO_SERVICE_ID); |
| 92 | case ProgramType::SXM: |
| 93 | if (anyHaveId(a, b, IdentifierType::SXM_SERVICE_ID)) { |
| 94 | return haveEqualIds(a, b, IdentifierType::SXM_SERVICE_ID); |
| 95 | } |
| 96 | return haveEqualIds(a, b, IdentifierType::SXM_CHANNEL); |
Tomasz Wasilczyk | da97a6d | 2017-08-04 12:57:29 -0700 | [diff] [blame] | 97 | default: // includes all vendor types |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 98 | ALOGW("Unsupported program type: %s", toString(type).c_str()); |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | ProgramType getType(const ProgramSelector& sel) { |
| 104 | return static_cast<ProgramType>(sel.programType); |
| 105 | } |
| 106 | |
| 107 | bool isAmFm(const ProgramType type) { |
| 108 | switch (type) { |
| 109 | case ProgramType::AM: |
| 110 | case ProgramType::FM: |
| 111 | case ProgramType::AM_HD: |
| 112 | case ProgramType::FM_HD: |
| 113 | return true; |
| 114 | default: |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
Tomasz Wasilczyk | 701a5bd | 2017-08-10 12:32:45 -0700 | [diff] [blame] | 119 | bool isAm(const Band band) { |
| 120 | return band == Band::AM || band == Band::AM_HD; |
| 121 | } |
| 122 | |
| 123 | bool isFm(const Band band) { |
| 124 | return band == Band::FM || band == Band::FM_HD; |
| 125 | } |
| 126 | |
Tomasz Wasilczyk | 002151c | 2017-11-22 09:43:06 -0800 | [diff] [blame] | 127 | static bool maybeGetId(const ProgramSelector& sel, const IdentifierType type, uint64_t* val) { |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 128 | auto itype = static_cast<uint32_t>(type); |
Tomasz Wasilczyk | 002151c | 2017-11-22 09:43:06 -0800 | [diff] [blame] | 129 | |
Tomasz Wasilczyk | 8c34c81 | 2018-02-08 13:42:31 -0800 | [diff] [blame] | 130 | if (sel.primaryId.type == itype) { |
Tomasz Wasilczyk | 002151c | 2017-11-22 09:43:06 -0800 | [diff] [blame] | 131 | if (val) *val = sel.primaryId.value; |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | // not optimal, but we don't care in default impl |
Tomasz Wasilczyk | 002151c | 2017-11-22 09:43:06 -0800 | [diff] [blame] | 136 | for (auto&& id : sel.secondaryIds) { |
| 137 | if (id.type == itype) { |
| 138 | if (val) *val = id.value; |
| 139 | return true; |
| 140 | } |
Tomasz Wasilczyk | 002151c | 2017-11-22 09:43:06 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Tomasz Wasilczyk | 8c34c81 | 2018-02-08 13:42:31 -0800 | [diff] [blame] | 143 | return false; |
Tomasz Wasilczyk | 002151c | 2017-11-22 09:43:06 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | bool hasId(const ProgramSelector& sel, const IdentifierType type) { |
| 147 | return maybeGetId(sel, type, nullptr); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | uint64_t getId(const ProgramSelector& sel, const IdentifierType type) { |
Tomasz Wasilczyk | 002151c | 2017-11-22 09:43:06 -0800 | [diff] [blame] | 151 | uint64_t val; |
| 152 | |
| 153 | if (maybeGetId(sel, type, &val)) { |
| 154 | return val; |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 155 | } |
Tomasz Wasilczyk | 002151c | 2017-11-22 09:43:06 -0800 | [diff] [blame] | 156 | |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 157 | ALOGW("Identifier %s not found", toString(type).c_str()); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | uint64_t getId(const ProgramSelector& sel, const IdentifierType type, uint64_t defval) { |
| 162 | if (!hasId(sel, type)) return defval; |
| 163 | return getId(sel, type); |
| 164 | } |
| 165 | |
| 166 | ProgramSelector make_selector(Band band, uint32_t channel, uint32_t subChannel) { |
| 167 | ProgramSelector sel = {}; |
| 168 | |
| 169 | ALOGW_IF((subChannel > 0) && (band == Band::AM || band == Band::FM), |
| 170 | "got subChannel for non-HD AM/FM"); |
| 171 | |
| 172 | // we can't use ProgramType::AM_HD or FM_HD, because we don't know HD station ID |
| 173 | ProgramType type; |
Tomasz Wasilczyk | 701a5bd | 2017-08-10 12:32:45 -0700 | [diff] [blame] | 174 | if (isAm(band)) { |
| 175 | type = ProgramType::AM; |
| 176 | } else if (isFm(band)) { |
| 177 | type = ProgramType::FM; |
| 178 | } else { |
| 179 | LOG_ALWAYS_FATAL("Unsupported band: %s", toString(band).c_str()); |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | sel.programType = static_cast<uint32_t>(type); |
| 183 | sel.primaryId.type = static_cast<uint32_t>(IdentifierType::AMFM_FREQUENCY); |
| 184 | sel.primaryId.value = channel; |
| 185 | if (subChannel > 0) { |
Tomasz Wasilczyk | 2834b95 | 2017-07-12 14:17:09 -0700 | [diff] [blame] | 186 | /* stating sub channel for AM/FM channel does not give any guarantees, |
| 187 | * but we can't do much more without HD station ID |
| 188 | * |
| 189 | * The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based. |
| 190 | */ |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 191 | sel.secondaryIds = hidl_vec<ProgramIdentifier>{ |
Tomasz Wasilczyk | 2834b95 | 2017-07-12 14:17:09 -0700 | [diff] [blame] | 192 | {static_cast<uint32_t>(IdentifierType::HD_SUBCHANNEL), subChannel - 1}, |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 193 | }; |
| 194 | } |
| 195 | |
| 196 | return sel; |
| 197 | } |
| 198 | |
Tomasz Wasilczyk | 2834b95 | 2017-07-12 14:17:09 -0700 | [diff] [blame] | 199 | bool getLegacyChannel(const ProgramSelector& sel, uint32_t* channelOut, uint32_t* subChannelOut) { |
| 200 | if (channelOut) *channelOut = 0; |
| 201 | if (subChannelOut) *subChannelOut = 0; |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 202 | if (isAmFm(getType(sel))) { |
Tomasz Wasilczyk | 2834b95 | 2017-07-12 14:17:09 -0700 | [diff] [blame] | 203 | if (channelOut) *channelOut = getId(sel, IdentifierType::AMFM_FREQUENCY); |
| 204 | if (subChannelOut && hasId(sel, IdentifierType::HD_SUBCHANNEL)) { |
| 205 | // The legacy APIs had 1-based subChannels, while ProgramSelector is 0-based. |
| 206 | *subChannelOut = getId(sel, IdentifierType::HD_SUBCHANNEL) + 1; |
| 207 | } |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 208 | return true; |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 209 | } |
Tomasz Wasilczyk | 2834b95 | 2017-07-12 14:17:09 -0700 | [diff] [blame] | 210 | return false; |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bool isDigital(const ProgramSelector& sel) { |
| 214 | switch (getType(sel)) { |
| 215 | case ProgramType::AM: |
| 216 | case ProgramType::FM: |
| 217 | return false; |
| 218 | default: |
| 219 | // VENDOR might not be digital, but it doesn't matter for default impl. |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | } // namespace utils |
Tomasz Wasilczyk | c1763a6 | 2017-07-25 10:01:17 -0700 | [diff] [blame] | 225 | |
| 226 | namespace V1_0 { |
| 227 | |
| 228 | bool operator==(const BandConfig& l, const BandConfig& r) { |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 229 | using namespace utils; |
| 230 | |
Tomasz Wasilczyk | c1763a6 | 2017-07-25 10:01:17 -0700 | [diff] [blame] | 231 | if (l.type != r.type) return false; |
| 232 | if (l.antennaConnected != r.antennaConnected) return false; |
| 233 | if (l.lowerLimit != r.lowerLimit) return false; |
| 234 | if (l.upperLimit != r.upperLimit) return false; |
| 235 | if (l.spacings != r.spacings) return false; |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 236 | if (isAm(l.type)) { |
Tomasz Wasilczyk | c1763a6 | 2017-07-25 10:01:17 -0700 | [diff] [blame] | 237 | return l.ext.am == r.ext.am; |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 238 | } else if (isFm(l.type)) { |
Tomasz Wasilczyk | c1763a6 | 2017-07-25 10:01:17 -0700 | [diff] [blame] | 239 | return l.ext.fm == r.ext.fm; |
| 240 | } else { |
| 241 | ALOGW("Unsupported band config type: %s", toString(l.type).c_str()); |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | } // namespace V1_0 |
Tomasz Wasilczyk | a02b6ef | 2017-07-05 11:23:30 -0700 | [diff] [blame] | 247 | } // namespace broadcastradio |
| 248 | } // namespace hardware |
| 249 | } // namespace android |