Tomasz Wasilczyk | 8579f1d | 2021-12-16 12:19:09 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 52 | RadioResponse& CallbackManager::response() const { |
| 53 | return *mRadioResponse; |
| 54 | } |
| 55 | |
| 56 | void CallbackManager::setResponseFunctionsDelayed() { |
| 57 | std::unique_lock<std::mutex> lock(mDelayedSetterGuard); |
| 58 | mDelayedSetterDeadline = std::chrono::steady_clock::now() + kDelayedSetterDelay; |
| 59 | mDelayedSetterCv.notify_all(); |
| 60 | } |
| 61 | |
| 62 | void CallbackManager::delayedSetterThread() { |
| 63 | while (!mDestroy) { |
| 64 | std::unique_lock<std::mutex> lock(mDelayedSetterGuard); |
| 65 | auto deadline = mDelayedSetterDeadline; |
| 66 | |
| 67 | // not waiting to set response functions |
| 68 | if (!deadline) { |
| 69 | mDelayedSetterCv.wait(lock); |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | // waiting to set response functions, but not yet |
| 74 | if (*deadline > std::chrono::steady_clock::now()) { |
| 75 | mDelayedSetterCv.wait_until(lock, *deadline); |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | mHidlHal->setResponseFunctions(mRadioResponse, mRadioIndication).assertOk(); |
| 80 | mDelayedSetterDeadline = std::nullopt; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | } // namespace android::hardware::radio::compat |