Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #ifndef AIDL_CALLBACK_UTIL_H_ |
| 18 | #define AIDL_CALLBACK_UTIL_H_ |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 22 | #include <mutex> |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 23 | #include <set> |
| 24 | #include <unordered_map> |
| 25 | |
| 26 | namespace { |
| 27 | std::unordered_map<void* /* callback */, void* /* handler */> callback_handler_map_; |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 28 | std::mutex callback_handler_lock_; |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | namespace aidl { |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | namespace wifi { |
| 35 | namespace aidl_callback_util { |
| 36 | |
| 37 | // Provides a class to manage callbacks for the various AIDL interfaces and |
| 38 | // handle the death of the process hosting each callback. |
| 39 | template <typename CallbackType> |
| 40 | class AidlCallbackHandler { |
| 41 | public: |
| 42 | AidlCallbackHandler() { |
| 43 | death_handler_ = AIBinder_DeathRecipient_new(AidlCallbackHandler::onCallbackDeath); |
| 44 | } |
| 45 | ~AidlCallbackHandler() { invalidate(); } |
| 46 | |
| 47 | bool addCallback(const std::shared_ptr<CallbackType>& cb) { |
Gabriel Biren | 1110418 | 2024-11-04 18:26:11 +0000 | [diff] [blame] | 48 | if (cb == nullptr) { |
| 49 | LOG(ERROR) << "Unable to register a null callback"; |
| 50 | return false; |
| 51 | } |
| 52 | |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 53 | std::unique_lock<std::mutex> lk(callback_handler_lock_); |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 54 | void* cbPtr = reinterpret_cast<void*>(cb->asBinder().get()); |
| 55 | const auto& cbPosition = findCbInSet(cbPtr); |
| 56 | if (cbPosition != cb_set_.end()) { |
| 57 | LOG(WARNING) << "Duplicate death notification registration"; |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | if (AIBinder_linkToDeath(cb->asBinder().get(), death_handler_, cbPtr /* cookie */) != |
| 62 | STATUS_OK) { |
| 63 | LOG(ERROR) << "Failed to register death notification"; |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | callback_handler_map_[cbPtr] = reinterpret_cast<void*>(this); |
| 68 | cb_set_.insert(cb); |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 69 | // unique_lock unlocked here |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 70 | return true; |
| 71 | } |
| 72 | |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 73 | const std::set<std::shared_ptr<CallbackType>>& getCallbacks() { |
| 74 | std::unique_lock<std::mutex> lk(callback_handler_lock_); |
| 75 | // unique_lock unlocked here |
| 76 | return cb_set_; |
| 77 | } |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 78 | |
| 79 | void invalidate() { |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 80 | std::unique_lock<std::mutex> lk(callback_handler_lock_); |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 81 | for (auto cb : cb_set_) { |
| 82 | void* cookie = reinterpret_cast<void*>(cb->asBinder().get()); |
| 83 | if (AIBinder_unlinkToDeath(cb->asBinder().get(), death_handler_, cookie) != STATUS_OK) { |
| 84 | LOG(ERROR) << "Failed to deregister death notification"; |
| 85 | } |
| 86 | if (!removeCbFromHandlerMap(cookie)) { |
| 87 | LOG(ERROR) << "Failed to remove callback from handler map"; |
| 88 | } |
| 89 | } |
| 90 | cb_set_.clear(); |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 91 | // unique_lock unlocked here |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Entry point for the death handling logic. AIBinder_DeathRecipient |
| 95 | // can only call a static function, so use the cookie to find the |
| 96 | // proper handler and route the request there. |
| 97 | static void onCallbackDeath(void* cookie) { |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 98 | std::unique_lock<std::mutex> lk(callback_handler_lock_); |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 99 | auto cbQuery = callback_handler_map_.find(cookie); |
| 100 | if (cbQuery == callback_handler_map_.end()) { |
| 101 | LOG(ERROR) << "Invalid death cookie received"; |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | AidlCallbackHandler* cbHandler = reinterpret_cast<AidlCallbackHandler*>(cbQuery->second); |
| 106 | if (cbHandler == nullptr) { |
| 107 | LOG(ERROR) << "Handler mapping contained an invalid handler"; |
| 108 | return; |
| 109 | } |
| 110 | cbHandler->handleCallbackDeath(cbQuery->first); |
Gabriel Biren | fce371b | 2023-04-18 22:21:46 +0000 | [diff] [blame] | 111 | // unique_lock unlocked here |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private: |
| 115 | std::set<std::shared_ptr<CallbackType>> cb_set_; |
| 116 | AIBinder_DeathRecipient* death_handler_; |
| 117 | |
| 118 | typename std::set<std::shared_ptr<CallbackType>>::iterator findCbInSet(void* cbPtr) { |
| 119 | const auto& cbPosition = std::find_if( |
| 120 | cb_set_.begin(), cb_set_.end(), [cbPtr](const std::shared_ptr<CallbackType>& p) { |
| 121 | return cbPtr == reinterpret_cast<void*>(p->asBinder().get()); |
| 122 | }); |
| 123 | return cbPosition; |
| 124 | } |
| 125 | |
| 126 | bool removeCbFromHandlerMap(void* cbPtr) { |
| 127 | auto cbQuery = callback_handler_map_.find(cbPtr); |
| 128 | if (cbQuery != callback_handler_map_.end()) { |
| 129 | callback_handler_map_.erase(cbQuery); |
| 130 | return true; |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | void handleCallbackDeath(void* cbPtr) { |
| 136 | const auto& cbPosition = findCbInSet(cbPtr); |
| 137 | if (cbPosition == cb_set_.end()) { |
| 138 | LOG(ERROR) << "Unknown callback death notification received"; |
| 139 | return; |
| 140 | } |
| 141 | cb_set_.erase(cbPosition); |
| 142 | |
| 143 | if (!removeCbFromHandlerMap(cbPtr)) { |
| 144 | LOG(ERROR) << "Callback was not in callback handler map"; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | DISALLOW_COPY_AND_ASSIGN(AidlCallbackHandler); |
| 149 | }; |
| 150 | |
| 151 | } // namespace aidl_callback_util |
| 152 | } // namespace wifi |
| 153 | } // namespace hardware |
| 154 | } // namespace android |
| 155 | } // namespace aidl |
| 156 | |
| 157 | #endif // AIDL_CALLBACK_UTIL_H_ |