1 /*
2  * Copyright (C) 2021 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 #pragma once
17 
18 #include "DriverContext.h"
19 #include "RadioIndication.h"
20 #include "RadioResponse.h"
21 
22 #include <android-base/logging.h>
23 #include <android/hardware/radio/1.6/IRadio.h>
24 
25 #include <thread>
26 
27 namespace android::hardware::radio::compat {
28 
29 class CallbackManager {
30     sp<V1_5::IRadio> mHidlHal;
31     sp<RadioResponse> mRadioResponse;
32     sp<RadioIndication> mRadioIndication;
33 
34     std::thread mDelayedSetterThread;
35     std::mutex mDelayedSetterGuard;
36     std::optional<std::chrono::time_point<std::chrono::steady_clock>> mDelayedSetterDeadline
37             GUARDED_BY(mDelayedSetterGuard);
38     std::condition_variable mDelayedSetterCv GUARDED_BY(mDelayedSetterGuard);
39     bool mDestroy GUARDED_BY(mDelayedSetterGuard) = false;
40 
41     void setResponseFunctionsDelayed();
42     void delayedSetterThread();
43 
44   public:
45     CallbackManager(std::shared_ptr<DriverContext> context, sp<V1_5::IRadio> hidlHal);
46     ~CallbackManager();
47 
48     RadioResponse& response() const;
49     RadioIndication& indication() const;
50 
51     template <typename ResponseType, typename IndicationType>
setResponseFunctions(const std::shared_ptr<ResponseType> & response,const std::shared_ptr<IndicationType> & indication)52     void setResponseFunctions(const std::shared_ptr<ResponseType>& response,
53                               const std::shared_ptr<IndicationType>& indication) {
54         CHECK(response);
55         CHECK(indication);
56 
57         mRadioResponse->setResponseFunction(response);
58         mRadioIndication->setResponseFunction(indication);
59         setResponseFunctionsDelayed();
60     }
61 };
62 
63 }  // namespace android::hardware::radio::compat
64