1 /*
2  * Copyright (C) 2008 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_BINDER_H
18 #define ANDROID_HARDWARE_BINDER_H
19 
20 #include <atomic>
21 #include <stdint.h>
22 #include <hwbinder/IBinder.h>
23 
24 // ---------------------------------------------------------------------------
25 namespace android {
26 namespace hardware {
27 
28 class BHwBinder : public IBinder
29 {
30 public:
31                         BHwBinder();
32 
33     virtual status_t    transact(   uint32_t code,
34                                     const Parcel& data,
35                                     Parcel* reply,
36                                     uint32_t flags = 0,
37                                     TransactCallback callback = nullptr);
38 
39     virtual status_t    linkToDeath(const sp<DeathRecipient>& recipient,
40                                     void* cookie = nullptr,
41                                     uint32_t flags = 0);
42 
43     virtual status_t    unlinkToDeath(  const wp<DeathRecipient>& recipient,
44                                         void* cookie = nullptr,
45                                         uint32_t flags = 0,
46                                         wp<DeathRecipient>* outRecipient = nullptr);
47 
48     virtual void        attachObject(   const void* objectID,
49                                         void* object,
50                                         void* cleanupCookie,
51                                         object_cleanup_func func);
52     virtual void*       findObject(const void* objectID) const;
53     virtual void        detachObject(const void* objectID);
54 
55     virtual BHwBinder*    localBinder();
56 
57     int                 getMinSchedulingPolicy();
58     int                 getMinSchedulingPriority();
59 
60     bool                isRequestingSid();
61 
62 protected:
63     virtual             ~BHwBinder();
64 
65     virtual status_t    onTransact( uint32_t code,
66                                     const Parcel& data,
67                                     Parcel* reply,
68                                     uint32_t flags = 0,
69                                     TransactCallback callback = nullptr);
70 
71     // This must be called before the object is sent to another process. Not thread safe.
72     //
73     // If this is called with true, and the kernel supports it,
74     // IPCThreadState::getCallingSid will return values for remote processes.
75     void                setRequestingSid(bool requestSid);
76 
77     int                 mSchedPolicy; // policy to run transaction from this node at
78     // priority [-20..19] for SCHED_NORMAL, [1..99] for SCHED_FIFO/RT
79     int                 mSchedPriority;
80 private:
81                         BHwBinder(const BHwBinder& o);
82             BHwBinder&    operator=(const BHwBinder& o);
83 
84     class Extras;
85 
86     Extras*             getOrCreateExtras();
87 
88     std::atomic<Extras*> mExtras;
89             void*       mReserved0;
90 };
91 
92 // ---------------------------------------------------------------------------
93 
94 class BpHwRefBase : public virtual RefBase
95 {
96 protected:
97     explicit                BpHwRefBase(const sp<IBinder>& o);
98     virtual                 ~BpHwRefBase();
99     virtual void            onFirstRef();
100     virtual void            onLastStrongRef(const void* id);
101     virtual bool            onIncStrongAttempted(uint32_t flags, const void* id);
102 
103 public:
remote()104     inline  IBinder*        remote() const          { return mRemote; }
105 
106 private:
107                             BpHwRefBase(const BpHwRefBase& o);
108     BpHwRefBase&              operator=(const BpHwRefBase& o);
109 
110     IBinder* const          mRemote;
111     RefBase::weakref_type*  mRefs;
112     std::atomic<int32_t>    mState;
113 };
114 
115 } // namespace hardware
116 } // namespace android
117 
118 // ---------------------------------------------------------------------------
119 
120 #endif // ANDROID_HARDWARE_BINDER_H
121