1164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian/*
2164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * Copyright (C) 2005 The Android Open Source Project
3164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian *
4164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * you may not use this file except in compliance with the License.
6164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * You may obtain a copy of the License at
7164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian *
8164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian *
10164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * See the License for the specific language governing permissions and
14164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian * limitations under the License.
15164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian */
16164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
171e118d2b86540f7a7d840ec1510337da49f1446cYifan Hong#ifndef ANDROID_HARDWARE_BPHWBINDER_H
181e118d2b86540f7a7d840ec1510337da49f1446cYifan Hong#define ANDROID_HARDWARE_BPHWBINDER_H
19164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
204080edcfb5810a4a1817c03740f5f1bb19815ae1Martijn Coenen#include <hwbinder/IBinder.h>
21164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian#include <utils/KeyedVector.h>
22164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian#include <utils/threads.h>
23164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
24164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian// ---------------------------------------------------------------------------
25164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopiannamespace android {
26f75a23d54cc9e5f7b6b976e65b6ec346178f306dMartijn Coenennamespace hardware {
27164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
281e118d2b86540f7a7d840ec1510337da49f1446cYifan Hongclass BpHwBinder : public IBinder
29164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian{
30164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopianpublic:
311e118d2b86540f7a7d840ec1510337da49f1446cYifan Hong                        BpHwBinder(int32_t handle);
32164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
33164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    inline  int32_t     handle() const { return mHandle; }
34164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
35164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual status_t    transact(   uint32_t code,
36164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                    const Parcel& data,
37164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                    Parcel* reply,
3879c2f4d38c83ac8fe4c9c77e98e04f310016e7a6Martijn Coenen                                    uint32_t flags = 0,
3979c2f4d38c83ac8fe4c9c77e98e04f310016e7a6Martijn Coenen                                    TransactCallback callback = nullptr);
40164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
41164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual status_t    linkToDeath(const sp<DeathRecipient>& recipient,
42164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                    void* cookie = NULL,
43164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                    uint32_t flags = 0);
44164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual status_t    unlinkToDeath(  const wp<DeathRecipient>& recipient,
45164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                        void* cookie = NULL,
46164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                        uint32_t flags = 0,
47164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                        wp<DeathRecipient>* outRecipient = NULL);
48164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
49164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual void        attachObject(   const void* objectID,
50164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                        void* object,
51164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                        void* cleanupCookie,
52164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                                        object_cleanup_func func);
53164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual void*       findObject(const void* objectID) const;
54164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual void        detachObject(const void* objectID);
55164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
561e118d2b86540f7a7d840ec1510337da49f1446cYifan Hong    virtual BpHwBinder*   remoteBinder();
57164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
58164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            status_t    setConstantData(const void* data, size_t size);
59164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            void        sendObituary();
60164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
61164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    class ObjectManager
62164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    {
63164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    public:
64164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                    ObjectManager();
65164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                    ~ObjectManager();
66164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
67164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        void        attach( const void* objectID,
68164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                            void* object,
69164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                            void* cleanupCookie,
70164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                            IBinder::object_cleanup_func func);
71164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        void*       find(const void* objectID) const;
72164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        void        detach(const void* objectID);
73164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
74164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        void        kill();
75164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
76164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    private:
77164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian                    ObjectManager(const ObjectManager&);
78164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        ObjectManager& operator=(const ObjectManager&);
79164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
80164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        struct entry_t
81164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        {
82164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            void* object;
83164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            void* cleanupCookie;
84164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            IBinder::object_cleanup_func func;
85164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        };
86164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
87164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        KeyedVector<const void*, entry_t> mObjects;
88164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    };
89164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
90164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopianprotected:
911e118d2b86540f7a7d840ec1510337da49f1446cYifan Hong    virtual             ~BpHwBinder();
92164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual void        onFirstRef();
93164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual void        onLastStrongRef(const void* id);
94164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    virtual bool        onIncStrongAttempted(uint32_t flags, const void* id);
95164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
96164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopianprivate:
97164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    const   int32_t             mHandle;
98164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
99164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    struct Obituary {
100164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        wp<DeathRecipient> recipient;
101164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        void* cookie;
102164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian        uint32_t flags;
103164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    };
104164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
105164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            void                reportOneDeath(const Obituary& obit);
106a6286c36406f6bb21b53d92dc165c603365b358bMathias Agopian            bool                isDescriptorCached() const;
107164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
108164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian    mutable Mutex               mLock;
109164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            volatile int32_t    mAlive;
110164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            volatile int32_t    mObitsSent;
111164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            Vector<Obituary>*   mObituaries;
112164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            ObjectManager       mObjects;
113164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian            Parcel*             mConstantData;
114a6286c36406f6bb21b53d92dc165c603365b358bMathias Agopian    mutable String16            mDescriptorCache;
115164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian};
116164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
117f75a23d54cc9e5f7b6b976e65b6ec346178f306dMartijn Coenen}; // namespace hardware
118164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian}; // namespace android
119164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
120164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian// ---------------------------------------------------------------------------
121164757037f944dae1eb5f76176570b2e43a37b55Mathias Agopian
1221e118d2b86540f7a7d840ec1510337da49f1446cYifan Hong#endif // ANDROID_HARDWARE_BPHWBINDER_H
123