blob: 5e51941634004717bad7a8ed2f00df4e0ed94507 [file] [log] [blame]
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -08001/*
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#ifndef ANDROID_HARDWARE_BROADCASTRADIO_COMMON_UTILS_2X_H
17#define ANDROID_HARDWARE_BROADCASTRADIO_COMMON_UTILS_2X_H
18
19#include <android/hardware/broadcastradio/2.0/types.h>
20#include <chrono>
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -080021#include <optional>
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -080022#include <queue>
23#include <thread>
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080024#include <unordered_set>
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -080025
26namespace android {
27namespace hardware {
28namespace broadcastradio {
29namespace utils {
30
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -080031enum class FrequencyBand {
32 UNKNOWN,
33 FM,
34 AM_LW,
35 AM_MW,
36 AM_SW,
37};
38
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080039V2_0::IdentifierType getType(uint32_t typeAsInt);
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -080040V2_0::IdentifierType getType(const V2_0::ProgramIdentifier& id);
41
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080042class IdentifierIterator
43 : public std::iterator<std::random_access_iterator_tag, V2_0::ProgramIdentifier, ssize_t,
44 const V2_0::ProgramIdentifier*, const V2_0::ProgramIdentifier&> {
45 using traits = std::iterator_traits<IdentifierIterator>;
46 using ptr_type = typename traits::pointer;
47 using ref_type = typename traits::reference;
48 using diff_type = typename traits::difference_type;
49
50 public:
51 explicit IdentifierIterator(const V2_0::ProgramSelector& sel);
52
53 IdentifierIterator operator++(int);
54 IdentifierIterator& operator++();
55 ref_type operator*() const;
56 inline ptr_type operator->() const { return &operator*(); }
57 IdentifierIterator operator+(diff_type v) const { return IdentifierIterator(mSel, mPos + v); }
58 bool operator==(const IdentifierIterator& rhs) const;
59 inline bool operator!=(const IdentifierIterator& rhs) const { return !operator==(rhs); };
60
61 private:
62 explicit IdentifierIterator(const V2_0::ProgramSelector& sel, size_t pos);
63
64 std::reference_wrapper<const V2_0::ProgramSelector> mSel;
65
66 const V2_0::ProgramSelector& sel() const { return mSel.get(); }
67
68 /** 0 is the primary identifier, 1-n are secondary identifiers. */
69 size_t mPos = 0;
70};
71
72IdentifierIterator begin(const V2_0::ProgramSelector& sel);
73IdentifierIterator end(const V2_0::ProgramSelector& sel);
74
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -080075/**
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -080076 * Guesses band from the frequency value.
77 *
78 * The band bounds are not exact to cover multiple regions.
79 * The function is biased towards success, i.e. it never returns
80 * FrequencyBand::UNKNOWN for correct frequency, but a result for
81 * incorrect one is undefined (it doesn't have to return UNKNOWN).
82 */
83FrequencyBand getBand(uint64_t frequency);
84
85/**
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -080086 * Checks, if {@code pointer} tunes to {@channel}.
87 *
88 * For example, having a channel {AMFM_FREQUENCY = 103.3}:
89 * - selector {AMFM_FREQUENCY = 103.3, HD_SUBCHANNEL = 0} can tune to this channel;
90 * - selector {AMFM_FREQUENCY = 103.3, HD_SUBCHANNEL = 1} can't.
91 *
92 * @param pointer selector we're trying to match against channel.
93 * @param channel existing channel.
94 */
95bool tunesTo(const V2_0::ProgramSelector& pointer, const V2_0::ProgramSelector& channel);
96
97bool hasId(const V2_0::ProgramSelector& sel, const V2_0::IdentifierType type);
98
99/**
100 * Returns ID (either primary or secondary) for a given program selector.
101 *
102 * If the selector does not contain given type, returns 0 and emits a warning.
103 */
104uint64_t getId(const V2_0::ProgramSelector& sel, const V2_0::IdentifierType type);
105
106/**
107 * Returns ID (either primary or secondary) for a given program selector.
108 *
109 * If the selector does not contain given type, returns default value.
110 */
111uint64_t getId(const V2_0::ProgramSelector& sel, const V2_0::IdentifierType type, uint64_t defval);
112
113/**
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -0800114 * Returns all IDs of a given type.
115 */
116std::vector<uint64_t> getAllIds(const V2_0::ProgramSelector& sel, const V2_0::IdentifierType type);
117
118/**
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -0800119 * Checks, if a given selector is supported by the radio module.
120 *
121 * @param prop Module description.
122 * @param sel The selector to check.
123 * @return True, if the selector is supported, false otherwise.
124 */
125bool isSupported(const V2_0::Properties& prop, const V2_0::ProgramSelector& sel);
126
Tomasz Wasilczyk8b70ee42017-12-21 11:51:29 -0800127bool isValid(const V2_0::ProgramIdentifier& id);
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -0800128bool isValid(const V2_0::ProgramSelector& sel);
129
130V2_0::ProgramIdentifier make_identifier(V2_0::IdentifierType type, uint64_t value);
131V2_0::ProgramSelector make_selector_amfm(uint32_t frequency);
132V2_0::Metadata make_metadata(V2_0::MetadataKey key, int64_t value);
133V2_0::Metadata make_metadata(V2_0::MetadataKey key, std::string value);
134
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800135bool satisfies(const V2_0::ProgramFilter& filter, const V2_0::ProgramSelector& sel);
136
137struct ProgramInfoHasher {
138 size_t operator()(const V2_0::ProgramInfo& info) const;
139};
140
141struct ProgramInfoKeyEqual {
142 bool operator()(const V2_0::ProgramInfo& info1, const V2_0::ProgramInfo& info2) const;
143};
144
145typedef std::unordered_set<V2_0::ProgramInfo, ProgramInfoHasher, ProgramInfoKeyEqual>
146 ProgramInfoSet;
147
148void updateProgramList(ProgramInfoSet& list, const V2_0::ProgramListChunk& chunk);
149
Tomasz Wasilczykc71624f2017-12-22 10:54:34 -0800150std::optional<std::string> getMetadataString(const V2_0::ProgramInfo& info,
151 const V2_0::MetadataKey key);
152
153V2_0::ProgramIdentifier make_hdradio_station_name(const std::string& name);
154
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -0800155} // namespace utils
156} // namespace broadcastradio
157} // namespace hardware
158} // namespace android
159
160#endif // ANDROID_HARDWARE_BROADCASTRADIO_COMMON_UTILS_2X_H