Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 HIDL_CALLBACK_UTIL_H_ |
| 18 | #define HIDL_CALLBACK_UTIL_H_ |
| 19 | |
| 20 | #include <set> |
| 21 | |
| 22 | #include <hidl/HidlSupport.h> |
| 23 | |
| 24 | namespace { |
| 25 | // Type of callback invoked by the death handler. |
| 26 | using on_death_cb_function = std::function<void(uint64_t)>; |
| 27 | |
| 28 | // Private class used to keep track of death of individual |
| 29 | // callbacks stored in HidlCallbackHandler. |
| 30 | template <typename CallbackType> |
| 31 | class HidlDeathHandler : public android::hardware::hidl_death_recipient { |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 32 | public: |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 33 | HidlDeathHandler(const on_death_cb_function& user_cb_function) |
| 34 | : cb_function_(user_cb_function) {} |
| 35 | ~HidlDeathHandler() = default; |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 36 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 37 | // Death notification for callbacks. |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 38 | void serviceDied(uint64_t cookie, |
| 39 | const android::wp<android::hidl::base::V1_0::IBase>& /* who */) override { |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 40 | cb_function_(cookie); |
| 41 | } |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 42 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 43 | private: |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 44 | on_death_cb_function cb_function_; |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 45 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 46 | DISALLOW_COPY_AND_ASSIGN(HidlDeathHandler); |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 47 | }; |
| 48 | } // namespace |
| 49 | |
| 50 | namespace android { |
| 51 | namespace hardware { |
| 52 | namespace wifi { |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 53 | namespace V1_6 { |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 54 | namespace implementation { |
| 55 | namespace hidl_callback_util { |
| 56 | template <typename CallbackType> |
| 57 | // Provides a class to manage callbacks for the various HIDL interfaces and |
| 58 | // handle the death of the process hosting each callback. |
| 59 | class HidlCallbackHandler { |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 60 | public: |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 61 | HidlCallbackHandler() |
| 62 | : death_handler_(new HidlDeathHandler<CallbackType>( |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 63 | std::bind(&HidlCallbackHandler::onObjectDeath, this, std::placeholders::_1))) {} |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 64 | ~HidlCallbackHandler() = default; |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 65 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 66 | bool addCallback(const sp<CallbackType>& cb) { |
| 67 | // TODO(b/33818800): Can't compare proxies yet. So, use the cookie |
| 68 | // (callback proxy's raw pointer) to track the death of individual |
| 69 | // clients. |
| 70 | uint64_t cookie = reinterpret_cast<uint64_t>(cb.get()); |
| 71 | if (cb_set_.find(cb) != cb_set_.end()) { |
| 72 | LOG(WARNING) << "Duplicate death notification registration"; |
| 73 | return true; |
| 74 | } |
| 75 | if (!cb->linkToDeath(death_handler_, cookie)) { |
| 76 | LOG(ERROR) << "Failed to register death notification"; |
| 77 | return false; |
| 78 | } |
| 79 | cb_set_.insert(cb); |
| 80 | return true; |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 81 | } |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 82 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 83 | const std::set<android::sp<CallbackType>>& getCallbacks() { return cb_set_; } |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 84 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 85 | // Death notification for callbacks. |
| 86 | void onObjectDeath(uint64_t cookie) { |
| 87 | CallbackType* cb = reinterpret_cast<CallbackType*>(cookie); |
| 88 | const auto& iter = cb_set_.find(cb); |
| 89 | if (iter == cb_set_.end()) { |
| 90 | LOG(ERROR) << "Unknown callback death notification received"; |
| 91 | return; |
| 92 | } |
| 93 | cb_set_.erase(iter); |
| 94 | LOG(DEBUG) << "Dead callback removed from list"; |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 95 | } |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 96 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 97 | void invalidate() { |
| 98 | for (const sp<CallbackType>& cb : cb_set_) { |
| 99 | if (!cb->unlinkToDeath(death_handler_)) { |
| 100 | LOG(ERROR) << "Failed to deregister death notification"; |
| 101 | } |
| 102 | } |
| 103 | cb_set_.clear(); |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 104 | } |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 105 | |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 106 | private: |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 107 | std::set<sp<CallbackType>> cb_set_; |
| 108 | sp<HidlDeathHandler<CallbackType>> death_handler_; |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 109 | |
Roshan Pius | abcf78f | 2017-10-06 16:30:38 -0700 | [diff] [blame] | 110 | DISALLOW_COPY_AND_ASSIGN(HidlCallbackHandler); |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace hidl_callback_util |
| 114 | } // namespace implementation |
Ahmed ElArabawy | 687ce13 | 2022-01-11 16:42:48 -0800 | [diff] [blame^] | 115 | } // namespace V1_6 |
Roshan Pius | d37341f | 2017-01-31 13:13:28 -0800 | [diff] [blame] | 116 | } // namespace wifi |
| 117 | } // namespace hardware |
| 118 | } // namespace android |
| 119 | #endif // HIDL_CALLBACK_UTIL_H_ |