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
17 #include <libradiocompat/CallbackManager.h>
18
19 #include <android-base/logging.h>
20
21 using namespace std::literals::chrono_literals;
22
23 namespace android::hardware::radio::compat {
24
25 /**
26 * How much setter thread will wait with setting response functions after the last
27 * setResponseFunctions call from the framework. Subsequent calls from the framework reset the
28 * clock, so this number should be larger than the longest time between setResponseFunctions calls
29 * from the framework.
30 *
31 * Real world measurements with Cuttlefish give <10ms delay between Modem and Data and <2ms delays
32 * between all others.
33 */
34 static constexpr auto kDelayedSetterDelay = 100ms;
35
CallbackManager(std::shared_ptr<DriverContext> context,sp<V1_5::IRadio> hidlHal)36 CallbackManager::CallbackManager(std::shared_ptr<DriverContext> context, sp<V1_5::IRadio> hidlHal)
37 : mHidlHal(hidlHal),
38 mRadioResponse(sp<compat::RadioResponse>::make(context)),
39 mRadioIndication(sp<compat::RadioIndication>::make(context)),
40 mDelayedSetterThread(&CallbackManager::delayedSetterThread, this) {}
41
~CallbackManager()42 CallbackManager::~CallbackManager() {
43 {
44 std::unique_lock<std::mutex> lock(mDelayedSetterGuard);
45 mDelayedSetterDeadline = std::nullopt;
46 mDestroy = true;
47 mDelayedSetterCv.notify_all();
48 }
49 mDelayedSetterThread.join();
50 }
51
response() const52 RadioResponse& CallbackManager::response() const {
53 return *mRadioResponse;
54 }
55
indication() const56 RadioIndication& CallbackManager::indication() const {
57 return *mRadioIndication;
58 }
59
setResponseFunctionsDelayed()60 void CallbackManager::setResponseFunctionsDelayed() {
61 std::unique_lock<std::mutex> lock(mDelayedSetterGuard);
62 mDelayedSetterDeadline = std::chrono::steady_clock::now() + kDelayedSetterDelay;
63 mDelayedSetterCv.notify_all();
64 }
65
delayedSetterThread()66 void CallbackManager::delayedSetterThread() {
67 while (!mDestroy) {
68 std::unique_lock<std::mutex> lock(mDelayedSetterGuard);
69 auto deadline = mDelayedSetterDeadline;
70
71 // not waiting to set response functions
72 if (!deadline) {
73 mDelayedSetterCv.wait(lock);
74 continue;
75 }
76
77 // waiting to set response functions, but not yet
78 if (*deadline > std::chrono::steady_clock::now()) {
79 mDelayedSetterCv.wait_until(lock, *deadline);
80 continue;
81 }
82
83 mHidlHal->setResponseFunctions(mRadioResponse, mRadioIndication).assertOk();
84 mDelayedSetterDeadline = std::nullopt;
85 }
86 }
87
88 } // namespace android::hardware::radio::compat
89