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 LOG(INFO) << "CallbackManager: defer setResponseFunctions";
62 std::unique_lock<std::mutex> lock(mDelayedSetterGuard);
63 mDelayedSetterDeadline = std::chrono::steady_clock::now() + kDelayedSetterDelay;
64 mDelayedSetterCv.notify_all();
65 }
66
delayedSetterThread()67 void CallbackManager::delayedSetterThread() {
68 // TODO(b/405830123): remove or degrade logs from this file once the race condition is solved
69 LOG(INFO) << "CallbackManager: setting up delay thread";
70 while (!mDestroy) {
71 std::unique_lock<std::mutex> lock(mDelayedSetterGuard);
72 auto deadline = mDelayedSetterDeadline;
73
74 // not waiting to set response functions
75 if (!deadline) {
76 mDelayedSetterCv.wait(lock);
77 continue;
78 }
79
80 // waiting to set response functions, but not yet
81 if (*deadline > std::chrono::steady_clock::now()) {
82 LOG(INFO) << "CallbackManager: wait until " << deadline->time_since_epoch().count();
83 mDelayedSetterCv.wait_until(lock, *deadline);
84 continue;
85 }
86
87 LOG(INFO) << "CallbackManager: calling deferred setResponseFunctions";
88 mHidlHal->setResponseFunctions(mRadioResponse, mRadioIndication).assertOk();
89 mDelayedSetterDeadline = std::nullopt;
90 }
91 LOG(INFO) << "CallbackManager: delay thread destroyed";
92 }
93
94 } // namespace android::hardware::radio::compat
95