blob: 2419db9c0b7f8569002053a6b9058ccf8eeb2734 [file] [log] [blame]
Gabriel Birenf3262f92022-07-15 23:25:39 +00001/*
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 Birenfce371b2023-04-18 22:21:46 +000022#include <mutex>
Gabriel Birenf3262f92022-07-15 23:25:39 +000023#include <set>
24#include <unordered_map>
25
26namespace {
27std::unordered_map<void* /* callback */, void* /* handler */> callback_handler_map_;
Gabriel Birenfce371b2023-04-18 22:21:46 +000028std::mutex callback_handler_lock_;
Gabriel Birenf3262f92022-07-15 23:25:39 +000029}
30
31namespace aidl {
32namespace android {
33namespace hardware {
34namespace wifi {
35namespace 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.
39template <typename CallbackType>
40class 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 Biren11104182024-11-04 18:26:11 +000048 if (cb == nullptr) {
49 LOG(ERROR) << "Unable to register a null callback";
50 return false;
51 }
52
Gabriel Birenfce371b2023-04-18 22:21:46 +000053 std::unique_lock<std::mutex> lk(callback_handler_lock_);
Gabriel Birenf3262f92022-07-15 23:25:39 +000054 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 Birenfce371b2023-04-18 22:21:46 +000069 // unique_lock unlocked here
Gabriel Birenf3262f92022-07-15 23:25:39 +000070 return true;
71 }
72
Gabriel Birenfce371b2023-04-18 22:21:46 +000073 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 Birenf3262f92022-07-15 23:25:39 +000078
79 void invalidate() {
Gabriel Birenfce371b2023-04-18 22:21:46 +000080 std::unique_lock<std::mutex> lk(callback_handler_lock_);
Gabriel Birenf3262f92022-07-15 23:25:39 +000081 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 Birenfce371b2023-04-18 22:21:46 +000091 // unique_lock unlocked here
Gabriel Birenf3262f92022-07-15 23:25:39 +000092 }
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 Birenfce371b2023-04-18 22:21:46 +000098 std::unique_lock<std::mutex> lk(callback_handler_lock_);
Gabriel Birenf3262f92022-07-15 23:25:39 +000099 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 Birenfce371b2023-04-18 22:21:46 +0000111 // unique_lock unlocked here
Gabriel Birenf3262f92022-07-15 23:25:39 +0000112 }
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_