xref: /aosp_15_r20/hardware/interfaces/broadcastradio/aidl/default/BroadcastRadio.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 "VirtualRadio.h"
20 
21 #include <aidl/android/hardware/broadcastradio/AmFmBandRange.h>
22 #include <aidl/android/hardware/broadcastradio/AmFmRegionConfig.h>
23 #include <aidl/android/hardware/broadcastradio/AnnouncementType.h>
24 #include <aidl/android/hardware/broadcastradio/BnBroadcastRadio.h>
25 #include <aidl/android/hardware/broadcastradio/DabTableEntry.h>
26 #include <aidl/android/hardware/broadcastradio/IAnnouncementListener.h>
27 #include <aidl/android/hardware/broadcastradio/ICloseHandle.h>
28 #include <aidl/android/hardware/broadcastradio/ITunerCallback.h>
29 #include <aidl/android/hardware/broadcastradio/Properties.h>
30 #include <broadcastradio-utils/WorkerThread.h>
31 
32 #include <android-base/thread_annotations.h>
33 
34 #include <optional>
35 
36 namespace aidl::android::hardware::broadcastradio {
37 
38 class BroadcastRadio final : public BnBroadcastRadio {
39   public:
40     explicit BroadcastRadio(const VirtualRadio& virtualRadio);
41     ~BroadcastRadio();
42     ndk::ScopedAStatus getAmFmRegionConfig(bool full, AmFmRegionConfig* returnConfigs)
43             EXCLUDES(mMutex) override;
44     ndk::ScopedAStatus getDabRegionConfig(std::vector<DabTableEntry>* returnConfigs) override;
45     ndk::ScopedAStatus getImage(int32_t id, std::vector<uint8_t>* returnImage) override;
46     ndk::ScopedAStatus getProperties(Properties* returnProperties) EXCLUDES(mMutex) override;
47 
48     ndk::ScopedAStatus setTunerCallback(const std::shared_ptr<ITunerCallback>& callback)
49             EXCLUDES(mMutex) override;
50     ndk::ScopedAStatus unsetTunerCallback() EXCLUDES(mMutex) override;
51     ndk::ScopedAStatus tune(const ProgramSelector& program) EXCLUDES(mMutex) override;
52     ndk::ScopedAStatus seek(bool directionUp, bool skipSubChannel) EXCLUDES(mMutex) override;
53     ndk::ScopedAStatus step(bool directionUp) EXCLUDES(mMutex) override;
54     ndk::ScopedAStatus cancel() EXCLUDES(mMutex) override;
55     ndk::ScopedAStatus startProgramListUpdates(const ProgramFilter& filter)
56             EXCLUDES(mMutex) override;
57     ndk::ScopedAStatus stopProgramListUpdates() EXCLUDES(mMutex) override;
58     ndk::ScopedAStatus isConfigFlagSet(ConfigFlag flag, bool* returnIsSet)
59             EXCLUDES(mMutex) override;
60     ndk::ScopedAStatus setConfigFlag(ConfigFlag flag, bool in_value) EXCLUDES(mMutex) override;
61     ndk::ScopedAStatus setParameters(const std::vector<VendorKeyValue>& parameters,
62                                      std::vector<VendorKeyValue>* returnParameters) override;
63     ndk::ScopedAStatus getParameters(const std::vector<std::string>& keys,
64                                      std::vector<VendorKeyValue>* returnParameters) override;
65     ndk::ScopedAStatus registerAnnouncementListener(
66             const std::shared_ptr<IAnnouncementListener>& listener,
67             const std::vector<AnnouncementType>& enabled,
68             std::shared_ptr<ICloseHandle>* returnCloseHandle) override;
69     binder_status_t dump(int fd, const char** args, uint32_t numArgs) EXCLUDES(mMutex) override;
70 
71   private:
72     const VirtualRadio& mVirtualRadio;
73     std::mutex mMutex;
74     AmFmRegionConfig mAmFmConfig GUARDED_BY(mMutex);
75     std::unique_ptr<::android::WorkerThread> mTuningThread GUARDED_BY(mMutex) =
76             std::unique_ptr<::android::WorkerThread>(new ::android::WorkerThread());
77     std::unique_ptr<::android::WorkerThread> mProgramListThread GUARDED_BY(mMutex) =
78             std::unique_ptr<::android::WorkerThread>(new ::android::WorkerThread());
79     bool mIsTuneCompleted GUARDED_BY(mMutex) = true;
80     Properties mProperties GUARDED_BY(mMutex);
81     ProgramSelector mCurrentProgramSelector GUARDED_BY(mMutex) = {};
82     ProgramInfo mCurrentProgramInfo GUARDED_BY(mMutex) = {};
83     std::vector<VirtualProgram> mProgramList GUARDED_BY(mMutex) = {};
84     std::optional<AmFmBandRange> mCurrentAmFmBandRange GUARDED_BY(mMutex);
85     std::shared_ptr<ITunerCallback> mCallback GUARDED_BY(mMutex);
86 
87     // Bitmap for all ConfigFlag values
88     int mConfigFlagValues GUARDED_BY(mMutex) = 0;
89 
90     bool adjustAmFmRangeLocked() REQUIRES(mMutex);
91     void cancelLocked() REQUIRES(mMutex);
92     ProgramInfo tuneInternalLocked(const ProgramSelector& sel) REQUIRES(mMutex);
93     void startProgramListUpdatesLocked(const ProgramFilter& filter) REQUIRES(mMutex);
94     void cancelProgramListUpdateLocked() REQUIRES(mMutex);
95     void handleProgramInfoUpdateRadioCallback(ProgramInfo programInfo,
96                                               const std::shared_ptr<ITunerCallback>& callback)
97             EXCLUDES(mMutex);
98     bool findNextLocked(const ProgramSelector& current, bool directionUp, bool skipSubChannel,
99                         VirtualProgram* nextProgram) const REQUIRES(mMutex);
100     void jumpToFirstSubChannelLocked(std::vector<VirtualProgram>::const_iterator& it) const
101             REQUIRES(mMutex);
102     bool isConfigFlagSetLocked(ConfigFlag flag) const REQUIRES(mMutex);
103     void updateCurrentProgramInfoWithAlert(std::optional<Alert>& alert);
104 
105     binder_status_t cmdHelp(int fd) const;
106     binder_status_t cmdTune(int fd, const char** args, uint32_t numArgs);
107     binder_status_t cmdSeek(int fd, const char** args, uint32_t numArgs);
108     binder_status_t cmdStep(int fd, const char** args, uint32_t numArgs);
109     binder_status_t cmdCancel(int fd, uint32_t numArgs);
110     binder_status_t cmdStartProgramListUpdates(int fd, const char** args, uint32_t numArgs);
111     binder_status_t cmdStopProgramListUpdates(int fd, uint32_t numArgs);
112     binder_status_t cmdSimulateAlert(int fd, const char** args, uint32_t numArgs);
113 
114     binder_status_t dumpsys(int fd) EXCLUDES(mMutex);
115 };
116 
117 }  // namespace aidl::android::hardware::broadcastradio
118