1 /*
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 
17 #ifndef ANDROID_HARDWARE_BPHWBINDER_H
18 #define ANDROID_HARDWARE_BPHWBINDER_H
19 
20 #include <hwbinder/IBinder.h>
21 #include <utils/KeyedVector.h>
22 #include <utils/threads.h>
23 
24 // 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 
28 // ---------------------------------------------------------------------------
29 namespace android {
30 namespace hardware {
31 
32 class BpHwBinder : public IBinder
33 {
34 public:
35                         BpHwBinder(int32_t handle);
36 
handle()37     inline  int32_t     handle() const { return mHandle; }
38 
39     virtual status_t    transact(   uint32_t code,
40                                     const Parcel& data,
41                                     Parcel* reply,
42                                     uint32_t flags = 0,
43                                     TransactCallback callback = nullptr);
44 
45     virtual status_t    linkToDeath(const sp<DeathRecipient>& recipient,
46                                     void* cookie = nullptr,
47                                     uint32_t flags = 0);
48     virtual status_t    unlinkToDeath(  const wp<DeathRecipient>& recipient,
49                                         void* cookie = nullptr,
50                                         uint32_t flags = 0,
51                                         wp<DeathRecipient>* outRecipient = nullptr);
52 
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 
60     virtual BpHwBinder*   remoteBinder();
61 
62             void        sendObituary();
63                         // 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();
69     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 
98 protected:
99     virtual             ~BpHwBinder();
100     virtual void        onFirstRef();
101     virtual void        onLastStrongRef(const void* id);
102     virtual bool        onIncStrongAttempted(uint32_t flags, const void* id);
103 
104 private:
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);
114             bool                isDescriptorCached() const;
115 
116     mutable Mutex               mLock;
117             volatile int32_t    mAlive;
118             volatile int32_t    mObitsSent;
119             Vector<Obituary>*   mObituaries;
120             ObjectManager       mObjects;
121     mutable String16            mDescriptorCache;
122 };
123 
124 } // namespace hardware
125 } // namespace android
126 
127 // ---------------------------------------------------------------------------
128 
129 #endif // ANDROID_HARDWARE_BPHWBINDER_H
130