Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [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 "BcRadioDef.utils" |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | |
| 19 | #include <broadcastradio-utils-2x/Utils.h> |
| 20 | |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 22 | #include <log/log.h> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace broadcastradio { |
| 27 | namespace utils { |
| 28 | |
| 29 | using V2_0::IdentifierType; |
| 30 | using V2_0::Metadata; |
| 31 | using V2_0::MetadataKey; |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 32 | using V2_0::ProgramFilter; |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 33 | using V2_0::ProgramIdentifier; |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 34 | using V2_0::ProgramInfo; |
| 35 | using V2_0::ProgramListChunk; |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 36 | using V2_0::ProgramSelector; |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 37 | using V2_0::Properties; |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 38 | |
| 39 | using std::string; |
Tomasz Wasilczyk | 31e8632 | 2017-12-05 09:36:11 -0800 | [diff] [blame] | 40 | using std::vector; |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 41 | |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 42 | IdentifierType getType(uint32_t typeAsInt) { |
| 43 | return static_cast<IdentifierType>(typeAsInt); |
| 44 | } |
| 45 | |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 46 | IdentifierType getType(const ProgramIdentifier& id) { |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 47 | return getType(id.type); |
| 48 | } |
| 49 | |
| 50 | IdentifierIterator::IdentifierIterator(const V2_0::ProgramSelector& sel) |
| 51 | : IdentifierIterator(sel, 0) {} |
| 52 | |
| 53 | IdentifierIterator::IdentifierIterator(const V2_0::ProgramSelector& sel, size_t pos) |
| 54 | : mSel(sel), mPos(pos) {} |
| 55 | |
| 56 | IdentifierIterator IdentifierIterator::operator++(int) { |
| 57 | auto i = *this; |
| 58 | mPos++; |
| 59 | return i; |
| 60 | } |
| 61 | |
| 62 | IdentifierIterator& IdentifierIterator::operator++() { |
| 63 | ++mPos; |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | IdentifierIterator::ref_type IdentifierIterator::operator*() const { |
| 68 | if (mPos == 0) return sel().primaryId; |
| 69 | |
| 70 | // mPos is 1-based for secondary identifiers |
| 71 | DCHECK(mPos <= sel().secondaryIds.size()); |
| 72 | return sel().secondaryIds[mPos - 1]; |
| 73 | } |
| 74 | |
| 75 | bool IdentifierIterator::operator==(const IdentifierIterator& rhs) const { |
| 76 | // Check, if both iterators points at the same selector. |
| 77 | if (reinterpret_cast<uintptr_t>(&sel()) != reinterpret_cast<uintptr_t>(&rhs.sel())) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | return mPos == rhs.mPos; |
| 82 | } |
| 83 | |
| 84 | IdentifierIterator begin(const V2_0::ProgramSelector& sel) { |
| 85 | return IdentifierIterator(sel); |
| 86 | } |
| 87 | |
| 88 | IdentifierIterator end(const V2_0::ProgramSelector& sel) { |
| 89 | return IdentifierIterator(sel) + 1 /* primary id */ + sel.secondaryIds.size(); |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static bool bothHaveId(const ProgramSelector& a, const ProgramSelector& b, |
| 93 | const IdentifierType type) { |
| 94 | return hasId(a, type) && hasId(b, type); |
| 95 | } |
| 96 | |
| 97 | static bool haveEqualIds(const ProgramSelector& a, const ProgramSelector& b, |
| 98 | const IdentifierType type) { |
| 99 | if (!bothHaveId(a, b, type)) return false; |
| 100 | /* We should check all Ids of a given type (ie. other AF), |
| 101 | * but it doesn't matter for default implementation. |
| 102 | */ |
| 103 | return getId(a, type) == getId(b, type); |
| 104 | } |
| 105 | |
| 106 | static int getHdSubchannel(const ProgramSelector& sel) { |
| 107 | auto hdsidext = getId(sel, IdentifierType::HD_STATION_ID_EXT, 0); |
| 108 | hdsidext >>= 32; // Station ID number |
| 109 | return hdsidext & 0xF; // HD Radio subchannel |
| 110 | } |
| 111 | |
| 112 | bool tunesTo(const ProgramSelector& a, const ProgramSelector& b) { |
| 113 | auto type = getType(b.primaryId); |
| 114 | |
| 115 | switch (type) { |
| 116 | case IdentifierType::HD_STATION_ID_EXT: |
| 117 | case IdentifierType::RDS_PI: |
| 118 | case IdentifierType::AMFM_FREQUENCY: |
| 119 | if (haveEqualIds(a, b, IdentifierType::HD_STATION_ID_EXT)) return true; |
| 120 | if (haveEqualIds(a, b, IdentifierType::RDS_PI)) return true; |
| 121 | return getHdSubchannel(b) == 0 && haveEqualIds(a, b, IdentifierType::AMFM_FREQUENCY); |
| 122 | case IdentifierType::DAB_SID_EXT: |
| 123 | return haveEqualIds(a, b, IdentifierType::DAB_SID_EXT); |
| 124 | case IdentifierType::DRMO_SERVICE_ID: |
| 125 | return haveEqualIds(a, b, IdentifierType::DRMO_SERVICE_ID); |
| 126 | case IdentifierType::SXM_SERVICE_ID: |
| 127 | return haveEqualIds(a, b, IdentifierType::SXM_SERVICE_ID); |
| 128 | default: // includes all vendor types |
| 129 | ALOGW("Unsupported program type: %s", toString(type).c_str()); |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static bool maybeGetId(const ProgramSelector& sel, const IdentifierType type, uint64_t* val) { |
| 135 | auto itype = static_cast<uint32_t>(type); |
| 136 | |
| 137 | if (sel.primaryId.type == itype) { |
| 138 | if (val) *val = sel.primaryId.value; |
| 139 | return true; |
| 140 | } |
| 141 | |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 142 | // TODO(twasilczyk): use IdentifierIterator |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 143 | // not optimal, but we don't care in default impl |
| 144 | for (auto&& id : sel.secondaryIds) { |
| 145 | if (id.type == itype) { |
| 146 | if (val) *val = id.value; |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | bool hasId(const ProgramSelector& sel, const IdentifierType type) { |
| 155 | return maybeGetId(sel, type, nullptr); |
| 156 | } |
| 157 | |
| 158 | uint64_t getId(const ProgramSelector& sel, const IdentifierType type) { |
| 159 | uint64_t val; |
| 160 | |
| 161 | if (maybeGetId(sel, type, &val)) { |
| 162 | return val; |
| 163 | } |
| 164 | |
| 165 | ALOGW("Identifier %s not found", toString(type).c_str()); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | uint64_t getId(const ProgramSelector& sel, const IdentifierType type, uint64_t defval) { |
| 170 | if (!hasId(sel, type)) return defval; |
| 171 | return getId(sel, type); |
| 172 | } |
| 173 | |
Tomasz Wasilczyk | 31e8632 | 2017-12-05 09:36:11 -0800 | [diff] [blame] | 174 | vector<uint64_t> getAllIds(const ProgramSelector& sel, const IdentifierType type) { |
| 175 | vector<uint64_t> ret; |
| 176 | auto itype = static_cast<uint32_t>(type); |
| 177 | |
| 178 | if (sel.primaryId.type == itype) ret.push_back(sel.primaryId.value); |
| 179 | |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 180 | // TODO(twasilczyk): use IdentifierIterator |
Tomasz Wasilczyk | 31e8632 | 2017-12-05 09:36:11 -0800 | [diff] [blame] | 181 | for (auto&& id : sel.secondaryIds) { |
| 182 | if (id.type == itype) ret.push_back(id.value); |
| 183 | } |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 188 | bool isSupported(const Properties& prop, const ProgramSelector& sel) { |
| 189 | // TODO(twasilczyk): use IdentifierIterator |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 190 | // Not optimal, but it doesn't matter for default impl nor VTS tests. |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 191 | for (auto&& idType : prop.supportedIdentifierTypes) { |
| 192 | if (hasId(sel, getType(idType))) return true; |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | static bool isValid(const ProgramIdentifier& id) { |
| 198 | auto val = id.value; |
| 199 | bool valid = true; |
| 200 | |
| 201 | auto expect = [&valid](bool condition, std::string message) { |
| 202 | if (!condition) { |
| 203 | valid = false; |
| 204 | ALOGE("Identifier not valid, expected %s", message.c_str()); |
| 205 | } |
| 206 | }; |
| 207 | |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 208 | switch (getType(id)) { |
Tomasz Wasilczyk | 4ce6382 | 2017-12-21 14:25:54 -0800 | [diff] [blame] | 209 | case IdentifierType::INVALID: |
| 210 | expect(false, "IdentifierType::INVALID"); |
| 211 | break; |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 212 | case IdentifierType::AMFM_FREQUENCY: |
| 213 | case IdentifierType::DAB_FREQUENCY: |
| 214 | case IdentifierType::DRMO_FREQUENCY: |
| 215 | expect(val > 100u, "f > 100kHz"); |
| 216 | expect(val < 10000000u, "f < 10GHz"); |
| 217 | break; |
| 218 | case IdentifierType::RDS_PI: |
| 219 | expect(val != 0u, "RDS PI != 0"); |
| 220 | expect(val <= 0xFFFFu, "16bit id"); |
| 221 | break; |
| 222 | case IdentifierType::HD_STATION_ID_EXT: { |
| 223 | auto stationId = val & 0xFFFFFFFF; // 32bit |
| 224 | val >>= 32; |
| 225 | auto subchannel = val & 0xF; // 4bit |
| 226 | val >>= 4; |
| 227 | auto freq = val & 0x3FFFF; // 18bit |
| 228 | expect(stationId != 0u, "HD station id != 0"); |
| 229 | expect(subchannel < 8u, "HD subch < 8"); |
| 230 | expect(freq > 100u, "f > 100kHz"); |
| 231 | expect(freq < 10000000u, "f < 10GHz"); |
| 232 | break; |
| 233 | } |
| 234 | case IdentifierType::DAB_SID_EXT: { |
| 235 | auto sid = val & 0xFFFF; // 16bit |
| 236 | val >>= 16; |
| 237 | auto ecc = val & 0xFF; // 8bit |
| 238 | expect(sid != 0u, "DAB SId != 0"); |
| 239 | expect(ecc >= 0xA0u && ecc <= 0xF6u, "Invalid ECC, see ETSI TS 101 756 V2.1.1"); |
| 240 | break; |
| 241 | } |
| 242 | case IdentifierType::DAB_ENSEMBLE: |
| 243 | expect(val != 0u, "DAB ensemble != 0"); |
| 244 | expect(val <= 0xFFFFu, "16bit id"); |
| 245 | break; |
| 246 | case IdentifierType::DAB_SCID: |
| 247 | expect(val > 0xFu, "12bit SCId (not 4bit SCIdS)"); |
| 248 | expect(val <= 0xFFFu, "12bit id"); |
| 249 | break; |
| 250 | case IdentifierType::DRMO_SERVICE_ID: |
| 251 | expect(val != 0u, "DRM SId != 0"); |
| 252 | expect(val <= 0xFFFFFFu, "24bit id"); |
| 253 | break; |
| 254 | case IdentifierType::SXM_SERVICE_ID: |
| 255 | expect(val != 0u, "SXM SId != 0"); |
| 256 | expect(val <= 0xFFFFFFFFu, "32bit id"); |
| 257 | break; |
| 258 | case IdentifierType::SXM_CHANNEL: |
| 259 | expect(val < 1000u, "SXM channel < 1000"); |
| 260 | break; |
| 261 | case IdentifierType::VENDOR_START: |
| 262 | case IdentifierType::VENDOR_END: |
| 263 | // skip |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | return valid; |
| 268 | } |
| 269 | |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 270 | bool isValid(const ProgramSelector& sel) { |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 271 | if (!isValid(sel.primaryId)) return false; |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 272 | // TODO(twasilczyk): use IdentifierIterator |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 273 | for (auto&& id : sel.secondaryIds) { |
| 274 | if (!isValid(id)) return false; |
| 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | ProgramIdentifier make_identifier(IdentifierType type, uint64_t value) { |
| 280 | return {static_cast<uint32_t>(type), value}; |
| 281 | } |
| 282 | |
| 283 | ProgramSelector make_selector_amfm(uint32_t frequency) { |
| 284 | ProgramSelector sel = {}; |
| 285 | sel.primaryId = make_identifier(IdentifierType::AMFM_FREQUENCY, frequency); |
| 286 | return sel; |
| 287 | } |
| 288 | |
| 289 | Metadata make_metadata(MetadataKey key, int64_t value) { |
| 290 | Metadata meta = {}; |
| 291 | meta.key = static_cast<uint32_t>(key); |
| 292 | meta.intValue = value; |
| 293 | return meta; |
| 294 | } |
| 295 | |
| 296 | Metadata make_metadata(MetadataKey key, string value) { |
| 297 | Metadata meta = {}; |
| 298 | meta.key = static_cast<uint32_t>(key); |
| 299 | meta.stringValue = value; |
| 300 | return meta; |
| 301 | } |
| 302 | |
Tomasz Wasilczyk | bceb885 | 2017-12-18 13:59:29 -0800 | [diff] [blame] | 303 | bool satisfies(const ProgramFilter& filter, const ProgramSelector& sel) { |
| 304 | if (filter.identifierTypes.size() > 0) { |
| 305 | auto typeEquals = [](const V2_0::ProgramIdentifier& id, uint32_t type) { |
| 306 | return id.type == type; |
| 307 | }; |
| 308 | auto it = std::find_first_of(begin(sel), end(sel), filter.identifierTypes.begin(), |
| 309 | filter.identifierTypes.end(), typeEquals); |
| 310 | if (it == end(sel)) return false; |
| 311 | } |
| 312 | |
| 313 | if (filter.identifiers.size() > 0) { |
| 314 | auto it = std::find_first_of(begin(sel), end(sel), filter.identifiers.begin(), |
| 315 | filter.identifiers.end()); |
| 316 | if (it == end(sel)) return false; |
| 317 | } |
| 318 | |
| 319 | if (!filter.includeCategories) { |
| 320 | if (getType(sel.primaryId) == IdentifierType::DAB_ENSEMBLE) return false; |
| 321 | } |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | size_t ProgramInfoHasher::operator()(const ProgramInfo& info) const { |
| 327 | auto& id = info.selector.primaryId; |
| 328 | |
| 329 | /* This is not the best hash implementation, but good enough for default HAL |
| 330 | * implementation and tests. */ |
| 331 | auto h = std::hash<uint32_t>{}(id.type); |
| 332 | h += 0x9e3779b9; |
| 333 | h ^= std::hash<uint64_t>{}(id.value); |
| 334 | |
| 335 | return h; |
| 336 | } |
| 337 | |
| 338 | bool ProgramInfoKeyEqual::operator()(const ProgramInfo& info1, const ProgramInfo& info2) const { |
| 339 | auto& id1 = info1.selector.primaryId; |
| 340 | auto& id2 = info2.selector.primaryId; |
| 341 | return id1.type == id2.type && id1.value == id2.value; |
| 342 | } |
| 343 | |
| 344 | void updateProgramList(ProgramInfoSet& list, const ProgramListChunk& chunk) { |
| 345 | if (chunk.purge) list.clear(); |
| 346 | |
| 347 | list.insert(chunk.modified.begin(), chunk.modified.end()); |
| 348 | |
| 349 | for (auto&& id : chunk.removed) { |
| 350 | ProgramInfo info = {}; |
| 351 | info.selector.primaryId = id; |
| 352 | list.erase(info); |
| 353 | } |
| 354 | } |
| 355 | |
Tomasz Wasilczyk | 06100b3 | 2017-12-04 09:53:32 -0800 | [diff] [blame] | 356 | } // namespace utils |
| 357 | } // namespace broadcastradio |
| 358 | } // namespace hardware |
| 359 | } // namespace android |