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