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