Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_HARDWARE_BPHWBINDER_H |
| 18 | #define ANDROID_HARDWARE_BPHWBINDER_H |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 20 | #include <hwbinder/IBinder.h> |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <utils/KeyedVector.h> |
| 22 | #include <utils/threads.h> |
| 23 | |
Steven Moreland | b5f6e00 | 2021-02-26 01:51:20 +0000 | [diff] [blame] | 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 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 28 | // --------------------------------------------------------------------------- |
| 29 | namespace android { |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 30 | namespace hardware { |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 31 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 32 | class BpHwBinder : public IBinder |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | { |
| 34 | public: |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 35 | BpHwBinder(int32_t handle); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 36 | |
| 37 | inline int32_t handle() const { return mHandle; } |
| 38 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 39 | virtual status_t transact( uint32_t code, |
| 40 | const Parcel& data, |
| 41 | Parcel* reply, |
Martijn Coenen | 79c2f4d | 2016-05-20 10:55:59 +0200 | [diff] [blame] | 42 | uint32_t flags = 0, |
| 43 | TransactCallback callback = nullptr); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 44 | |
| 45 | virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 46 | void* cookie = nullptr, |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 47 | uint32_t flags = 0); |
| 48 | virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 49 | void* cookie = nullptr, |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 50 | uint32_t flags = 0, |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 51 | wp<DeathRecipient>* outRecipient = nullptr); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 60 | virtual BpHwBinder* remoteBinder(); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 61 | |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 62 | void sendObituary(); |
Martijn Coenen | 320e1d3 | 2018-09-07 10:41:33 +0200 | [diff] [blame] | 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(); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 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: |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 99 | virtual ~BpHwBinder(); |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 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); |
Mathias Agopian | a6286c3 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 114 | bool isDescriptorCached() const; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 115 | |
| 116 | mutable Mutex mLock; |
| 117 | volatile int32_t mAlive; |
| 118 | volatile int32_t mObitsSent; |
| 119 | Vector<Obituary>* mObituaries; |
| 120 | ObjectManager mObjects; |
Mathias Agopian | a6286c3 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 121 | mutable String16 mDescriptorCache; |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Steven Moreland | 7173a4c | 2019-09-26 15:55:02 -0700 | [diff] [blame] | 124 | } // namespace hardware |
| 125 | } // namespace android |
Mathias Agopian | 1647570 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 126 | |
| 127 | // --------------------------------------------------------------------------- |
| 128 | |
Yifan Hong | 1e118d2 | 2017-01-12 14:42:28 -0800 | [diff] [blame] | 129 | #endif // ANDROID_HARDWARE_BPHWBINDER_H |