blob: aab0ae5393d5825369962d3af1f94d98012c087e [file] [log] [blame]
Roshan Piusd37341f2017-01-31 13:13:28 -08001/*
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>
Isaac Chiou9cf44592022-01-17 13:37:35 +080023#include <hidl/HidlTransportSupport.h>
Roshan Piusd37341f2017-01-31 13:13:28 -080024
25namespace {
26// Type of callback invoked by the death handler.
27using on_death_cb_function = std::function<void(uint64_t)>;
28
29// Private class used to keep track of death of individual
30// callbacks stored in HidlCallbackHandler.
31template <typename CallbackType>
32class HidlDeathHandler : public android::hardware::hidl_death_recipient {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080033 public:
Roshan Piusabcf78f2017-10-06 16:30:38 -070034 HidlDeathHandler(const on_death_cb_function& user_cb_function)
35 : cb_function_(user_cb_function) {}
36 ~HidlDeathHandler() = default;
Roshan Piusd37341f2017-01-31 13:13:28 -080037
Roshan Piusabcf78f2017-10-06 16:30:38 -070038 // Death notification for callbacks.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080039 void serviceDied(uint64_t cookie,
40 const android::wp<android::hidl::base::V1_0::IBase>& /* who */) override {
Roshan Piusabcf78f2017-10-06 16:30:38 -070041 cb_function_(cookie);
42 }
Roshan Piusd37341f2017-01-31 13:13:28 -080043
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080044 private:
Roshan Piusabcf78f2017-10-06 16:30:38 -070045 on_death_cb_function cb_function_;
Roshan Piusd37341f2017-01-31 13:13:28 -080046
Roshan Piusabcf78f2017-10-06 16:30:38 -070047 DISALLOW_COPY_AND_ASSIGN(HidlDeathHandler);
Roshan Piusd37341f2017-01-31 13:13:28 -080048};
49} // namespace
50
51namespace android {
52namespace hardware {
53namespace wifi {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080054namespace V1_6 {
Roshan Piusd37341f2017-01-31 13:13:28 -080055namespace implementation {
56namespace hidl_callback_util {
57template <typename CallbackType>
58// Provides a class to manage callbacks for the various HIDL interfaces and
59// handle the death of the process hosting each callback.
60class HidlCallbackHandler {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080061 public:
Roshan Piusabcf78f2017-10-06 16:30:38 -070062 HidlCallbackHandler()
63 : death_handler_(new HidlDeathHandler<CallbackType>(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080064 std::bind(&HidlCallbackHandler::onObjectDeath, this, std::placeholders::_1))) {}
Roshan Piusabcf78f2017-10-06 16:30:38 -070065 ~HidlCallbackHandler() = default;
Roshan Piusd37341f2017-01-31 13:13:28 -080066
Roshan Piusabcf78f2017-10-06 16:30:38 -070067 bool addCallback(const sp<CallbackType>& cb) {
68 // TODO(b/33818800): Can't compare proxies yet. So, use the cookie
69 // (callback proxy's raw pointer) to track the death of individual
70 // clients.
71 uint64_t cookie = reinterpret_cast<uint64_t>(cb.get());
Isaac Chiou9cf44592022-01-17 13:37:35 +080072 for (const auto& s : cb_set_) {
73 if (interfacesEqual(cb, s)) {
74 LOG(ERROR) << "Duplicate death notification registration";
75 return true;
76 }
Roshan Piusabcf78f2017-10-06 16:30:38 -070077 }
78 if (!cb->linkToDeath(death_handler_, cookie)) {
79 LOG(ERROR) << "Failed to register death notification";
80 return false;
81 }
82 cb_set_.insert(cb);
83 return true;
Roshan Piusd37341f2017-01-31 13:13:28 -080084 }
Roshan Piusabcf78f2017-10-06 16:30:38 -070085
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080086 const std::set<android::sp<CallbackType>>& getCallbacks() { return cb_set_; }
Roshan Piusd37341f2017-01-31 13:13:28 -080087
Roshan Piusabcf78f2017-10-06 16:30:38 -070088 // Death notification for callbacks.
89 void onObjectDeath(uint64_t cookie) {
90 CallbackType* cb = reinterpret_cast<CallbackType*>(cookie);
91 const auto& iter = cb_set_.find(cb);
92 if (iter == cb_set_.end()) {
93 LOG(ERROR) << "Unknown callback death notification received";
94 return;
95 }
96 cb_set_.erase(iter);
97 LOG(DEBUG) << "Dead callback removed from list";
Roshan Piusd37341f2017-01-31 13:13:28 -080098 }
Roshan Piusd37341f2017-01-31 13:13:28 -080099
Roshan Piusabcf78f2017-10-06 16:30:38 -0700100 void invalidate() {
101 for (const sp<CallbackType>& cb : cb_set_) {
102 if (!cb->unlinkToDeath(death_handler_)) {
103 LOG(ERROR) << "Failed to deregister death notification";
104 }
105 }
106 cb_set_.clear();
Roshan Piusd37341f2017-01-31 13:13:28 -0800107 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800108
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800109 private:
Roshan Piusabcf78f2017-10-06 16:30:38 -0700110 std::set<sp<CallbackType>> cb_set_;
111 sp<HidlDeathHandler<CallbackType>> death_handler_;
Roshan Piusd37341f2017-01-31 13:13:28 -0800112
Roshan Piusabcf78f2017-10-06 16:30:38 -0700113 DISALLOW_COPY_AND_ASSIGN(HidlCallbackHandler);
Roshan Piusd37341f2017-01-31 13:13:28 -0800114};
115
116} // namespace hidl_callback_util
117} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800118} // namespace V1_6
Roshan Piusd37341f2017-01-31 13:13:28 -0800119} // namespace wifi
120} // namespace hardware
121} // namespace android
122#endif // HIDL_CALLBACK_UTIL_H_