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 = NULL,
43                                     uint32_t flags = 0);
44     virtual status_t    unlinkToDeath(  const wp<DeathRecipient>& recipient,
45                                         void* cookie = NULL,
46                                         uint32_t flags = 0,
47                                         wp<DeathRecipient>* outRecipient = NULL);
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             status_t    setConstantData(const void* data, size_t size);
59             void        sendObituary();
60 
61     class ObjectManager
62     {
63     public:
64                     ObjectManager();
65                     ~ObjectManager();
66 
67         void        attach( const void* objectID,
68                             void* object,
69                             void* cleanupCookie,
70                             IBinder::object_cleanup_func func);
71         void*       find(const void* objectID) const;
72         void        detach(const void* objectID);
73 
74         void        kill();
75 
76     private:
77                     ObjectManager(const ObjectManager&);
78         ObjectManager& operator=(const ObjectManager&);
79 
80         struct entry_t
81         {
82             void* object;
83             void* cleanupCookie;
84             IBinder::object_cleanup_func func;
85         };
86 
87         KeyedVector<const void*, entry_t> mObjects;
88     };
89 
90 protected:
91     virtual             ~BpHwBinder();
92     virtual void        onFirstRef();
93     virtual void        onLastStrongRef(const void* id);
94     virtual bool        onIncStrongAttempted(uint32_t flags, const void* id);
95 
96 private:
97     const   int32_t             mHandle;
98 
99     struct Obituary {
100         wp<DeathRecipient> recipient;
101         void* cookie;
102         uint32_t flags;
103     };
104 
105             void                reportOneDeath(const Obituary& obit);
106             bool                isDescriptorCached() const;
107 
108     mutable Mutex               mLock;
109             volatile int32_t    mAlive;
110             volatile int32_t    mObitsSent;
111             Vector<Obituary>*   mObituaries;
112             ObjectManager       mObjects;
113             Parcel*             mConstantData;
114     mutable String16            mDescriptorCache;
115 };
116 
117 }; // namespace hardware
118 }; // namespace android
119 
120 // ---------------------------------------------------------------------------
121 
122 #endif // ANDROID_HARDWARE_BPHWBINDER_H
123