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 status_t setConstantData(const void* data, size_t size); 59 void sendObituary(); 60 // This refcount includes: 61 // 1. Strong references to the node by this and other processes 62 // 2. Temporary strong references held by the kernel during a 63 // transaction on the node. 64 // It does NOT include local strong references to the node 65 ssize_t getNodeStrongRefCount(); 66 class ObjectManager 67 { 68 public: 69 ObjectManager(); 70 ~ObjectManager(); 71 72 void attach( const void* objectID, 73 void* object, 74 void* cleanupCookie, 75 IBinder::object_cleanup_func func); 76 void* find(const void* objectID) const; 77 void detach(const void* objectID); 78 79 void kill(); 80 81 private: 82 ObjectManager(const ObjectManager&); 83 ObjectManager& operator=(const ObjectManager&); 84 85 struct entry_t 86 { 87 void* object; 88 void* cleanupCookie; 89 IBinder::object_cleanup_func func; 90 }; 91 92 KeyedVector<const void*, entry_t> mObjects; 93 }; 94 95 protected: 96 virtual ~BpHwBinder(); 97 virtual void onFirstRef(); 98 virtual void onLastStrongRef(const void* id); 99 virtual bool onIncStrongAttempted(uint32_t flags, const void* id); 100 101 private: 102 const int32_t mHandle; 103 104 struct Obituary { 105 wp<DeathRecipient> recipient; 106 void* cookie; 107 uint32_t flags; 108 }; 109 110 void reportOneDeath(const Obituary& obit); 111 bool isDescriptorCached() const; 112 113 mutable Mutex mLock; 114 volatile int32_t mAlive; 115 volatile int32_t mObitsSent; 116 Vector<Obituary>* mObituaries; 117 ObjectManager mObjects; 118 Parcel* mConstantData; 119 mutable String16 mDescriptorCache; 120 }; 121 122 }; // namespace hardware 123 }; // namespace android 124 125 // --------------------------------------------------------------------------- 126 127 #endif // ANDROID_HARDWARE_BPHWBINDER_H 128