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 #ifndef ANDROID_HARDWARE_BROADCASTRADIO_V2_0_TUNER_H
17 #define ANDROID_HARDWARE_BROADCASTRADIO_V2_0_TUNER_H
18 
19 #include "VirtualRadio.h"
20 
21 #include <android/hardware/broadcastradio/2.0/ITunerCallback.h>
22 #include <android/hardware/broadcastradio/2.0/ITunerSession.h>
23 #include <broadcastradio-utils/WorkerThread.h>
24 
25 #include <optional>
26 
27 namespace android {
28 namespace hardware {
29 namespace broadcastradio {
30 namespace V2_0 {
31 namespace implementation {
32 
33 struct BroadcastRadio;
34 
35 struct TunerSession : public ITunerSession {
36     TunerSession(BroadcastRadio& module, const sp<ITunerCallback>& callback);
37 
38     // V2_0::ITunerSession methods
39     virtual Return<Result> tune(const ProgramSelector& program) override;
40     virtual Return<Result> scan(bool directionUp, bool skipSubChannel) override;
41     virtual Return<Result> step(bool directionUp) override;
42     virtual Return<void> cancel() override;
43     virtual Return<Result> startProgramListUpdates(const ProgramFilter& filter);
44     virtual Return<void> stopProgramListUpdates();
45     virtual Return<void> isConfigFlagSet(ConfigFlag flag, isConfigFlagSet_cb _hidl_cb);
46     virtual Return<Result> setConfigFlag(ConfigFlag flag, bool value);
47     virtual Return<void> setParameters(const hidl_vec<VendorKeyValue>& parameters,
48                                        setParameters_cb _hidl_cb) override;
49     virtual Return<void> getParameters(const hidl_vec<hidl_string>& keys,
50                                        getParameters_cb _hidl_cb) override;
51     virtual Return<void> close() override;
52 
53     std::optional<AmFmBandRange> getAmFmRangeLocked() const;
54 
55    private:
56     std::mutex mMut;
57     WorkerThread mThread;
58     bool mIsClosed = false;
59 
60     const sp<ITunerCallback> mCallback;
61 
62     std::reference_wrapper<BroadcastRadio> mModule;
63     bool mIsTuneCompleted = false;
64     ProgramSelector mCurrentProgram = {};
65 
66     void cancelLocked();
67     void tuneInternalLocked(const ProgramSelector& sel);
68     const VirtualRadio& virtualRadio() const;
69     const BroadcastRadio& module() const;
70 };
71 
72 }  // namespace implementation
73 }  // namespace V2_0
74 }  // namespace broadcastradio
75 }  // namespace hardware
76 }  // namespace android
77 
78 #endif  // ANDROID_HARDWARE_BROADCASTRADIO_V2_0_TUNER_H
79