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 // ---------------------------------------------------------------------------
25 namespace android {
26 namespace hardware {
27 
28 class BpHwBinder : public IBinder
29 {
30 public:
31                         BpHwBinder(int32_t handle);
32 
handle()33     inline  int32_t     handle() const { return mHandle; }
34 
35     virtual status_t    transact(   uint32_t code,
36                                     const Parcel& data,
37                                     Parcel* reply,
38                                     uint32_t flags = 0,
39                                     TransactCallback callback = nullptr);
40 
41     virtual status_t    linkToDeath(const sp<DeathRecipient>& recipient,
42                                     void* cookie = nullptr,
43                                     uint32_t flags = 0);
44     virtual status_t    unlinkToDeath(  const wp<DeathRecipient>& recipient,
45                                         void* cookie = nullptr,
46                                         uint32_t flags = 0,
47                                         wp<DeathRecipient>* outRecipient = nullptr);
48 
49     virtual void        attachObject(   const void* objectID,
50                                         void* object,
51                                         void* cleanupCookie,
52                                         object_cleanup_func func);
53     virtual void*       findObject(const void* objectID) const;
54     virtual void        detachObject(const void* objectID);
55 
56     virtual BpHwBinder*   remoteBinder();
57 
58             void        sendObituary();
59                         // This refcount includes:
60                         // 1. Strong references to the node by this and other processes
61                         // 2. Temporary strong references held by the kernel during a
62                         //    transaction on the node.
63                         // It does NOT include local strong references to the node
64             ssize_t     getNodeStrongRefCount();
65     class ObjectManager
66     {
67     public:
68                     ObjectManager();
69                     ~ObjectManager();
70 
71         void        attach( const void* objectID,
72                             void* object,
73                             void* cleanupCookie,
74                             IBinder::object_cleanup_func func);
75         void*       find(const void* objectID) const;
76         void        detach(const void* objectID);
77 
78         void        kill();
79 
80     private:
81                     ObjectManager(const ObjectManager&);
82         ObjectManager& operator=(const ObjectManager&);
83 
84         struct entry_t
85         {
86             void* object;
87             void* cleanupCookie;
88             IBinder::object_cleanup_func func;
89         };
90 
91         KeyedVector<const void*, entry_t> mObjects;
92     };
93 
94 protected:
95     virtual             ~BpHwBinder();
96     virtual void        onFirstRef();
97     virtual void        onLastStrongRef(const void* id);
98     virtual bool        onIncStrongAttempted(uint32_t flags, const void* id);
99 
100 private:
101     const   int32_t             mHandle;
102 
103     struct Obituary {
104         wp<DeathRecipient> recipient;
105         void* cookie;
106         uint32_t flags;
107     };
108 
109             void                reportOneDeath(const Obituary& obit);
110             bool                isDescriptorCached() const;
111 
112     mutable Mutex               mLock;
113             volatile int32_t    mAlive;
114             volatile int32_t    mObitsSent;
115             Vector<Obituary>*   mObituaries;
116             ObjectManager       mObjects;
117     mutable String16            mDescriptorCache;
118 };
119 
120 } // namespace hardware
121 } // namespace android
122 
123 // ---------------------------------------------------------------------------
124 
125 #endif // ANDROID_HARDWARE_BPHWBINDER_H
126