Weilin Xu | b61ee9e | 2023-12-15 16:05:31 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 34 | namespace aidl::android::hardware::broadcastradio { |
| 35 | |
| 36 | namespace { |
| 37 | using ::ndk::ScopedAStatus; |
| 38 | |
| 39 | } // namespace |
| 40 | |
| 41 | class 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(); |
Weilin Xu | ed44150 | 2023-12-15 16:05:31 -0800 | [diff] [blame] | 52 | bool waitProgramReady(); |
Weilin Xu | b61ee9e | 2023-12-15 16:05:31 -0800 | [diff] [blame] | 53 | bool isTunerFailed(); |
| 54 | void reset(); |
| 55 | |
| 56 | ProgramInfo getCurrentProgramInfo(); |
Weilin Xu | ed44150 | 2023-12-15 16:05:31 -0800 | [diff] [blame] | 57 | utils::ProgramInfoSet getProgramList(); |
Weilin Xu | b61ee9e | 2023-12-15 16:05:31 -0800 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | class CallbackFlag final { |
| 61 | public: |
| 62 | CallbackFlag(int timeoutMs) { mTimeoutMs = timeoutMs; } |
| 63 | /** |
| 64 | * Notify that the callback is called. |
| 65 | */ |
| 66 | void notify() { |
| 67 | std::unique_lock<std::mutex> lock(mMutex); |
| 68 | mCalled = true; |
| 69 | lock.unlock(); |
| 70 | mCv.notify_all(); |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * Wait for the timeout passed into the constructor. |
| 75 | */ |
| 76 | bool wait() { |
| 77 | std::unique_lock<std::mutex> lock(mMutex); |
| 78 | return mCv.wait_for(lock, std::chrono::milliseconds(mTimeoutMs), |
| 79 | [this] { return mCalled; }); |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Reset the callback to not called. |
| 84 | */ |
| 85 | void reset() { |
| 86 | std::unique_lock<std::mutex> lock(mMutex); |
| 87 | mCalled = false; |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | std::mutex mMutex; |
| 92 | bool mCalled GUARDED_BY(mMutex) = false; |
| 93 | std::condition_variable mCv; |
| 94 | int mTimeoutMs; |
| 95 | }; |
| 96 | |
| 97 | std::mutex mLock; |
| 98 | bool mAntennaConnectionState GUARDED_BY(mLock); |
| 99 | bool tunerFailed GUARDED_BY(mLock) = false; |
| 100 | ProgramInfo mCurrentProgramInfo GUARDED_BY(mLock); |
| 101 | utils::ProgramInfoSet mProgramList GUARDED_BY(mLock); |
| 102 | CallbackFlag mOnCurrentProgramInfoChangedFlag = CallbackFlag(IBroadcastRadio::TUNER_TIMEOUT_MS); |
Weilin Xu | ed44150 | 2023-12-15 16:05:31 -0800 | [diff] [blame] | 103 | CallbackFlag mOnProgramListReadyFlag = CallbackFlag(IBroadcastRadio::LIST_COMPLETE_TIMEOUT_MS); |
Weilin Xu | b61ee9e | 2023-12-15 16:05:31 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace aidl::android::hardware::broadcastradio |