blob: 36f82e1ee3d473f991f710075a08c67b33206667 [file] [log] [blame]
Mathias Agopian16475702009-05-19 19:08:10 -07001/*
2 * Copyright (C) 2005 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
Yifan Hong1e118d22017-01-12 14:42:28 -080017#ifndef ANDROID_HARDWARE_BPHWBINDER_H
18#define ANDROID_HARDWARE_BPHWBINDER_H
Mathias Agopian16475702009-05-19 19:08:10 -070019
Martijn Coenen4080edc2016-05-04 14:17:02 +020020#include <hwbinder/IBinder.h>
Mathias Agopian16475702009-05-19 19:08:10 -070021#include <utils/KeyedVector.h>
22#include <utils/threads.h>
23
Steven Morelandb5f6e002021-02-26 01:51:20 +000024// WARNING: this code is part of libhwbinder, a fork of libbinder. Generally,
25// this means that it is only relevant to HIDL. Any AIDL- or libbinder-specific
26// code should not try to use these things.
27
Mathias Agopian16475702009-05-19 19:08:10 -070028// ---------------------------------------------------------------------------
29namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020030namespace hardware {
Mathias Agopian16475702009-05-19 19:08:10 -070031
Yifan Hong1e118d22017-01-12 14:42:28 -080032class BpHwBinder : public IBinder
Mathias Agopian16475702009-05-19 19:08:10 -070033{
34public:
Yifan Hong1e118d22017-01-12 14:42:28 -080035 BpHwBinder(int32_t handle);
Mathias Agopian16475702009-05-19 19:08:10 -070036
37 inline int32_t handle() const { return mHandle; }
38
Mathias Agopian16475702009-05-19 19:08:10 -070039 virtual status_t transact( uint32_t code,
40 const Parcel& data,
41 Parcel* reply,
Martijn Coenen79c2f4d2016-05-20 10:55:59 +020042 uint32_t flags = 0,
43 TransactCallback callback = nullptr);
Mathias Agopian16475702009-05-19 19:08:10 -070044
45 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient,
Yi Kong55d41072018-07-23 14:55:39 -070046 void* cookie = nullptr,
Mathias Agopian16475702009-05-19 19:08:10 -070047 uint32_t flags = 0);
48 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient,
Yi Kong55d41072018-07-23 14:55:39 -070049 void* cookie = nullptr,
Mathias Agopian16475702009-05-19 19:08:10 -070050 uint32_t flags = 0,
Yi Kong55d41072018-07-23 14:55:39 -070051 wp<DeathRecipient>* outRecipient = nullptr);
Mathias Agopian16475702009-05-19 19:08:10 -070052
53 virtual void attachObject( const void* objectID,
54 void* object,
55 void* cleanupCookie,
56 object_cleanup_func func);
57 virtual void* findObject(const void* objectID) const;
58 virtual void detachObject(const void* objectID);
59
Yifan Hong1e118d22017-01-12 14:42:28 -080060 virtual BpHwBinder* remoteBinder();
Mathias Agopian16475702009-05-19 19:08:10 -070061
Mathias Agopian16475702009-05-19 19:08:10 -070062 void sendObituary();
Martijn Coenen320e1d32018-09-07 10:41:33 +020063 // This refcount includes:
64 // 1. Strong references to the node by this and other processes
65 // 2. Temporary strong references held by the kernel during a
66 // transaction on the node.
67 // It does NOT include local strong references to the node
68 ssize_t getNodeStrongRefCount();
Mathias Agopian16475702009-05-19 19:08:10 -070069 class ObjectManager
70 {
71 public:
72 ObjectManager();
73 ~ObjectManager();
74
75 void attach( const void* objectID,
76 void* object,
77 void* cleanupCookie,
78 IBinder::object_cleanup_func func);
79 void* find(const void* objectID) const;
80 void detach(const void* objectID);
81
82 void kill();
83
84 private:
85 ObjectManager(const ObjectManager&);
86 ObjectManager& operator=(const ObjectManager&);
87
88 struct entry_t
89 {
90 void* object;
91 void* cleanupCookie;
92 IBinder::object_cleanup_func func;
93 };
94
95 KeyedVector<const void*, entry_t> mObjects;
96 };
97
98protected:
Yifan Hong1e118d22017-01-12 14:42:28 -080099 virtual ~BpHwBinder();
Mathias Agopian16475702009-05-19 19:08:10 -0700100 virtual void onFirstRef();
101 virtual void onLastStrongRef(const void* id);
102 virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
103
104private:
105 const int32_t mHandle;
106
107 struct Obituary {
108 wp<DeathRecipient> recipient;
109 void* cookie;
110 uint32_t flags;
111 };
112
113 void reportOneDeath(const Obituary& obit);
Mathias Agopiana6286c32009-05-22 19:00:22 -0700114 bool isDescriptorCached() const;
Mathias Agopian16475702009-05-19 19:08:10 -0700115
116 mutable Mutex mLock;
117 volatile int32_t mAlive;
118 volatile int32_t mObitsSent;
119 Vector<Obituary>* mObituaries;
120 ObjectManager mObjects;
Mathias Agopiana6286c32009-05-22 19:00:22 -0700121 mutable String16 mDescriptorCache;
Mathias Agopian16475702009-05-19 19:08:10 -0700122};
123
Steven Moreland7173a4c2019-09-26 15:55:02 -0700124} // namespace hardware
125} // namespace android
Mathias Agopian16475702009-05-19 19:08:10 -0700126
127// ---------------------------------------------------------------------------
128
Yifan Hong1e118d22017-01-12 14:42:28 -0800129#endif // ANDROID_HARDWARE_BPHWBINDER_H