blob: 2ae03e3f160d871d8c1774a25ee40be7cde74275 [file] [log] [blame]
Weilin Xub61ee9e2023-12-15 16:05:31 -08001/*
2 * Copyright (C) 2024 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/BnTunerCallback.h>
20#include <aidl/android/hardware/broadcastradio/ConfigFlag.h>
21#include <aidl/android/hardware/broadcastradio/IBroadcastRadio.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/Result.h>
26#include <aidl/android/hardware/broadcastradio/VendorKeyValue.h>
27
28#include <android-base/thread_annotations.h>
29
30#include <broadcastradio-utils-aidl/Utils.h>
31
32#include <condition_variable>
33
34namespace aidl::android::hardware::broadcastradio {
35
36namespace {
37using ::ndk::ScopedAStatus;
38
39} // namespace
40
41class MockBroadcastRadioCallback final : public BnTunerCallback {
42 public:
43 explicit MockBroadcastRadioCallback();
44 ScopedAStatus onTuneFailed(Result result, const ProgramSelector& selector) override;
45 ScopedAStatus onCurrentProgramInfoChanged(const ProgramInfo& info) override;
46 ScopedAStatus onProgramListUpdated(const ProgramListChunk& chunk) override;
47 ScopedAStatus onParametersUpdated(const std::vector<VendorKeyValue>& parameters) override;
48 ScopedAStatus onAntennaStateChange(bool connected) override;
49 ScopedAStatus onConfigFlagUpdated(ConfigFlag in_flag, bool in_value) override;
50
51 bool waitOnCurrentProgramInfoChangedCallback();
52 bool isTunerFailed();
53 void reset();
54
55 ProgramInfo getCurrentProgramInfo();
56
57 private:
58 class CallbackFlag final {
59 public:
60 CallbackFlag(int timeoutMs) { mTimeoutMs = timeoutMs; }
61 /**
62 * Notify that the callback is called.
63 */
64 void notify() {
65 std::unique_lock<std::mutex> lock(mMutex);
66 mCalled = true;
67 lock.unlock();
68 mCv.notify_all();
69 };
70
71 /**
72 * Wait for the timeout passed into the constructor.
73 */
74 bool wait() {
75 std::unique_lock<std::mutex> lock(mMutex);
76 return mCv.wait_for(lock, std::chrono::milliseconds(mTimeoutMs),
77 [this] { return mCalled; });
78 };
79
80 /**
81 * Reset the callback to not called.
82 */
83 void reset() {
84 std::unique_lock<std::mutex> lock(mMutex);
85 mCalled = false;
86 }
87
88 private:
89 std::mutex mMutex;
90 bool mCalled GUARDED_BY(mMutex) = false;
91 std::condition_variable mCv;
92 int mTimeoutMs;
93 };
94
95 std::mutex mLock;
96 bool mAntennaConnectionState GUARDED_BY(mLock);
97 bool tunerFailed GUARDED_BY(mLock) = false;
98 ProgramInfo mCurrentProgramInfo GUARDED_BY(mLock);
99 utils::ProgramInfoSet mProgramList GUARDED_BY(mLock);
100 CallbackFlag mOnCurrentProgramInfoChangedFlag = CallbackFlag(IBroadcastRadio::TUNER_TIMEOUT_MS);
101};
102
103} // namespace aidl::android::hardware::broadcastradio