1 /*
2 * Copyright (C) 2022 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
17 #pragma once
18
19 #include <aidl/android/hardware/broadcastradio/IdentifierType.h>
20 #include <aidl/android/hardware/broadcastradio/ProgramFilter.h>
21 #include <aidl/android/hardware/broadcastradio/ProgramIdentifier.h>
22 #include <aidl/android/hardware/broadcastradio/ProgramInfo.h>
23 #include <aidl/android/hardware/broadcastradio/ProgramListChunk.h>
24 #include <aidl/android/hardware/broadcastradio/ProgramSelector.h>
25 #include <aidl/android/hardware/broadcastradio/Properties.h>
26 #include <aidl/android/hardware/broadcastradio/Result.h>
27
28 #include <numeric>
29 #include <optional>
30 #include <thread>
31 #include <unordered_set>
32
33 namespace aidl::android::hardware::broadcastradio {
34
35 namespace utils {
36
37 enum class FrequencyBand {
38 UNKNOWN,
39 FM,
40 AM_LW,
41 AM_MW,
42 AM_SW,
43 };
44
45 class IdentifierIterator final
46 : public std::iterator<std::random_access_iterator_tag, ProgramIdentifier, ssize_t,
47 const ProgramIdentifier*, const ProgramIdentifier&> {
48 using ptrType = typename std::iterator_traits<IdentifierIterator>::pointer;
49 using refType = typename std::iterator_traits<IdentifierIterator>::reference;
50 using diffType = typename std::iterator_traits<IdentifierIterator>::difference_type;
51
52 public:
53 explicit IdentifierIterator(const ProgramSelector& sel);
54
55 const IdentifierIterator operator++(int);
56 IdentifierIterator& operator++();
57 refType operator*() const;
58 inline ptrType operator->() const { return &operator*(); }
59 IdentifierIterator operator+(diffType v) const { return IdentifierIterator(mSel, mPos + v); }
60 bool operator==(const IdentifierIterator& rhs) const;
61 inline bool operator!=(const IdentifierIterator& rhs) const { return !operator==(rhs); };
62
63 private:
64 explicit IdentifierIterator(const ProgramSelector& sel, size_t pos);
65
66 std::reference_wrapper<const ProgramSelector> mSel;
67
getSelector()68 const ProgramSelector& getSelector() const { return mSel.get(); }
69
70 /** 0 is the primary identifier, 1-n are secondary identifiers. */
71 size_t mPos = 0;
72 };
73
74 /**
75 * Convert Result to int
76 */
77 int32_t resultToInt(Result result);
78
79 /**
80 * Guesses band from the frequency value.
81 *
82 * The band bounds are not exact to cover multiple regions.
83 * The function is biased towards success, i.e. it never returns
84 * FrequencyBand::UNKNOWN for correct frequency, but a result for
85 * incorrect one is undefined (it doesn't have to return UNKNOWN).
86 */
87 FrequencyBand getBand(int64_t frequency);
88
89 /**
90 * Checks, if {@code pointer} tunes to {@channel}.
91 *
92 * For example, having a channel {AMFM_FREQUENCY_KHZ = 103.3}:
93 * - selector {AMFM_FREQUENCY_KHZ = 103.3, HD_SUBCHANNEL = 0} can tune to this channel;
94 * - selector {AMFM_FREQUENCY_KHZ = 103.3, HD_SUBCHANNEL = 1} can't.
95 *
96 * @param pointer selector we're trying to match against channel.
97 * @param channel existing channel.
98 */
99 bool tunesTo(const ProgramSelector& pointer, const ProgramSelector& channel);
100
101 /**
102 * Checks whether a given program selector has the given ID (either primary or secondary).
103 */
104 bool hasId(const ProgramSelector& sel, const 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 kValueForNotFoundIdentifier
110 * and emits a warning.
111 */
112 int64_t getId(const ProgramSelector& sel, const IdentifierType& type);
113
114 /**
115 * Returns ID (either primary or secondary) for a given program selector.
116 *
117 * If the selector does not contain given type, returns default value.
118 */
119 int64_t getId(const ProgramSelector& sel, const IdentifierType& type, int64_t defaultValue);
120
121 /**
122 * Returns all IDs of a given type.
123 */
124 std::vector<int64_t> getAllIds(const ProgramSelector& sel, const IdentifierType& type);
125
126 /**
127 * Checks, if a given selector is supported by the radio module.
128 *
129 * @param prop Module description.
130 * @param sel The selector to check.
131 * @return True, if the selector is supported, false otherwise.
132 */
133 bool isSupported(const Properties& prop, const ProgramSelector& sel);
134
135 bool isValid(const ProgramIdentifier& id);
136 bool isValid(const ProgramSelector& sel);
137
138 ProgramIdentifier makeIdentifier(IdentifierType type, int64_t value);
139 ProgramSelector makeSelectorAmfm(uint32_t frequency);
140 ProgramSelector makeSelectorDab(uint64_t sidExt);
141 ProgramSelector makeSelectorDab(uint64_t sidExt, uint32_t ensemble, uint64_t freq);
142 ProgramSelector makeSelectorHd(uint64_t stationId, uint64_t subChannel, uint64_t frequency);
143
144 bool satisfies(const ProgramFilter& filter, const ProgramSelector& sel);
145
146 bool isValidMetadata(const Metadata& metadata);
147
148 struct ProgramSelectorComparator {
149 bool operator()(const ProgramSelector& lhs, const ProgramSelector& rhs) const;
150 };
151
152 struct ProgramInfoComparator {
153 bool operator()(const ProgramInfo& lhs, const ProgramInfo& rhs) const;
154 };
155
156 struct ProgramInfoHasher {
157 size_t operator()(const ProgramInfo& info) const;
158 };
159
160 struct ProgramInfoKeyEqual {
161 bool operator()(const ProgramInfo& info1, const ProgramInfo& info2) const;
162 };
163
164 typedef std::unordered_set<ProgramInfo, ProgramInfoHasher, ProgramInfoKeyEqual> ProgramInfoSet;
165
166 void updateProgramList(const ProgramListChunk& chunk, ProgramInfoSet* list);
167
168 std::optional<std::string> getMetadataString(const ProgramInfo& info, const Metadata::Tag& tag);
169
170 ProgramIdentifier makeHdRadioStationName(const std::string& name);
171
172 uint32_t getHdFrequency(const ProgramSelector& sel);
173
174 int getHdSubchannel(const ProgramSelector& sel);
175
176 uint32_t getDabSId(const ProgramSelector& sel);
177
178 int getDabEccCode(const ProgramSelector& sel);
179
180 int getDabSCIdS(const ProgramSelector& sel);
181
182 bool hasAmFmFrequency(const ProgramSelector& sel);
183
184 uint32_t getAmFmFrequency(const ProgramSelector& sel);
185
186 template <typename aidl_type>
vectorToString(const std::vector<aidl_type> & in_values)187 inline std::string vectorToString(const std::vector<aidl_type>& in_values) {
188 return std::accumulate(std::begin(in_values), std::end(in_values), std::string{},
189 [](const std::string& ls, const aidl_type& rs) {
190 return ls + (ls.empty() ? "" : ",") + toString(rs);
191 });
192 }
193
194 IdentifierType getType(int typeAsInt);
195
196 bool parseArgInt(const std::string& s, int* out);
197
198 bool parseArgLong(const std::string& s, long* out);
199
200 bool parseArgBool(const std::string& s, bool* out);
201
202 bool parseArgDirection(const std::string& s, bool* out);
203
204 bool parseArgIdentifierTypeArray(const std::string& s, std::vector<IdentifierType>* out);
205
206 bool parseProgramIdentifierList(const std::string& s, std::vector<ProgramIdentifier>* out);
207
208 } // namespace utils
209
210 utils::IdentifierIterator begin(const ProgramSelector& sel);
211 utils::IdentifierIterator end(const ProgramSelector& sel);
212
213 } // namespace aidl::android::hardware::broadcastradio
214