blob: bac11fd0281fe83c700e2c09b71d9289ba57dba4 [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 Wasilczykbceb8852017-12-18 13:59:29 -080030V2_0::IdentifierType getType(uint32_t typeAsInt);
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -080031V2_0::IdentifierType getType(const V2_0::ProgramIdentifier& id);
32
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -080033class IdentifierIterator
34 : public std::iterator<std::random_access_iterator_tag, V2_0::ProgramIdentifier, ssize_t,
35 const V2_0::ProgramIdentifier*, const V2_0::ProgramIdentifier&> {
36 using traits = std::iterator_traits<IdentifierIterator>;
37 using ptr_type = typename traits::pointer;
38 using ref_type = typename traits::reference;
39 using diff_type = typename traits::difference_type;
40
41 public:
42 explicit IdentifierIterator(const V2_0::ProgramSelector& sel);
43
44 IdentifierIterator operator++(int);
45 IdentifierIterator& operator++();
46 ref_type operator*() const;
47 inline ptr_type operator->() const { return &operator*(); }
48 IdentifierIterator operator+(diff_type v) const { return IdentifierIterator(mSel, mPos + v); }
49 bool operator==(const IdentifierIterator& rhs) const;
50 inline bool operator!=(const IdentifierIterator& rhs) const { return !operator==(rhs); };
51
52 private:
53 explicit IdentifierIterator(const V2_0::ProgramSelector& sel, size_t pos);
54
55 std::reference_wrapper<const V2_0::ProgramSelector> mSel;
56
57 const V2_0::ProgramSelector& sel() const { return mSel.get(); }
58
59 /** 0 is the primary identifier, 1-n are secondary identifiers. */
60 size_t mPos = 0;
61};
62
63IdentifierIterator begin(const V2_0::ProgramSelector& sel);
64IdentifierIterator end(const V2_0::ProgramSelector& sel);
65
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -080066/**
67 * Checks, if {@code pointer} tunes to {@channel}.
68 *
69 * For example, having a channel {AMFM_FREQUENCY = 103.3}:
70 * - selector {AMFM_FREQUENCY = 103.3, HD_SUBCHANNEL = 0} can tune to this channel;
71 * - selector {AMFM_FREQUENCY = 103.3, HD_SUBCHANNEL = 1} can't.
72 *
73 * @param pointer selector we're trying to match against channel.
74 * @param channel existing channel.
75 */
76bool tunesTo(const V2_0::ProgramSelector& pointer, const V2_0::ProgramSelector& channel);
77
78bool hasId(const V2_0::ProgramSelector& sel, const V2_0::IdentifierType type);
79
80/**
81 * Returns ID (either primary or secondary) for a given program selector.
82 *
83 * If the selector does not contain given type, returns 0 and emits a warning.
84 */
85uint64_t getId(const V2_0::ProgramSelector& sel, const V2_0::IdentifierType type);
86
87/**
88 * Returns ID (either primary or secondary) for a given program selector.
89 *
90 * If the selector does not contain given type, returns default value.
91 */
92uint64_t getId(const V2_0::ProgramSelector& sel, const V2_0::IdentifierType type, uint64_t defval);
93
94/**
Tomasz Wasilczyk31e86322017-12-05 09:36:11 -080095 * Returns all IDs of a given type.
96 */
97std::vector<uint64_t> getAllIds(const V2_0::ProgramSelector& sel, const V2_0::IdentifierType type);
98
99/**
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -0800100 * Checks, if a given selector is supported by the radio module.
101 *
102 * @param prop Module description.
103 * @param sel The selector to check.
104 * @return True, if the selector is supported, false otherwise.
105 */
106bool isSupported(const V2_0::Properties& prop, const V2_0::ProgramSelector& sel);
107
108bool isValid(const V2_0::ProgramSelector& sel);
109
110V2_0::ProgramIdentifier make_identifier(V2_0::IdentifierType type, uint64_t value);
111V2_0::ProgramSelector make_selector_amfm(uint32_t frequency);
112V2_0::Metadata make_metadata(V2_0::MetadataKey key, int64_t value);
113V2_0::Metadata make_metadata(V2_0::MetadataKey key, std::string value);
114
Tomasz Wasilczykbceb8852017-12-18 13:59:29 -0800115bool satisfies(const V2_0::ProgramFilter& filter, const V2_0::ProgramSelector& sel);
116
117struct ProgramInfoHasher {
118 size_t operator()(const V2_0::ProgramInfo& info) const;
119};
120
121struct ProgramInfoKeyEqual {
122 bool operator()(const V2_0::ProgramInfo& info1, const V2_0::ProgramInfo& info2) const;
123};
124
125typedef std::unordered_set<V2_0::ProgramInfo, ProgramInfoHasher, ProgramInfoKeyEqual>
126 ProgramInfoSet;
127
128void updateProgramList(ProgramInfoSet& list, const V2_0::ProgramListChunk& chunk);
129
Tomasz Wasilczyk06100b32017-12-04 09:53:32 -0800130} // namespace utils
131} // namespace broadcastradio
132} // namespace hardware
133} // namespace android
134
135#endif // ANDROID_HARDWARE_BROADCASTRADIO_COMMON_UTILS_2X_H