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_IBINDER_H
18#define ANDROID_IBINDER_H
19
20#include <utils/Errors.h>
21#include <utils/RefBase.h>
22#include <utils/String16.h>
23#include <utils/Vector.h>
24
25
26#define B_PACK_CHARS(c1, c2, c3, c4) \
27    ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
28
29// ---------------------------------------------------------------------------
30namespace android {
31
32class BBinder;
33class BpBinder;
34class IInterface;
35class Parcel;
36
37/**
38 * Base class and low-level protocol for a remotable object.
39 * You can derive from this class to create an object for which other
40 * processes can hold references to it.  Communication between processes
41 * (method calls, property get and set) is down through a low-level
42 * protocol implemented on top of the transact() API.
43 */
44class IBinder : public virtual RefBase
45{
46public:
47    enum {
48        FIRST_CALL_TRANSACTION  = 0x00000001,
49        LAST_CALL_TRANSACTION   = 0x00ffffff,
50
51        PING_TRANSACTION        = B_PACK_CHARS('_','P','N','G'),
52        DUMP_TRANSACTION        = B_PACK_CHARS('_','D','M','P'),
53        INTERFACE_TRANSACTION   = B_PACK_CHARS('_', 'N', 'T', 'F'),
54        SYSPROPS_TRANSACTION    = B_PACK_CHARS('_', 'S', 'P', 'R'),
55
56        // Corresponds to TF_ONE_WAY -- an asynchronous call.
57        FLAG_ONEWAY             = 0x00000001
58    };
59
60                          IBinder();
61
62    /**
63     * Check if this IBinder implements the interface named by
64     * @a descriptor.  If it does, the base pointer to it is returned,
65     * which you can safely static_cast<> to the concrete C++ interface.
66     */
67    virtual sp<IInterface>  queryLocalInterface(const String16& descriptor);
68
69    /**
70     * Return the canonical name of the interface provided by this IBinder
71     * object.
72     */
73    virtual const String16& getInterfaceDescriptor() const = 0;
74
75    virtual bool            isBinderAlive() const = 0;
76    virtual status_t        pingBinder() = 0;
77    virtual status_t        dump(int fd, const Vector<String16>& args) = 0;
78
79    virtual status_t        transact(   uint32_t code,
80                                        const Parcel& data,
81                                        Parcel* reply,
82                                        uint32_t flags = 0) = 0;
83
84    class DeathRecipient : public virtual RefBase
85    {
86    public:
87        virtual void binderDied(const wp<IBinder>& who) = 0;
88    };
89
90    /**
91     * Register the @a recipient for a notification if this binder
92     * goes away.  If this binder object unexpectedly goes away
93     * (typically because its hosting process has been killed),
94     * then DeathRecipient::binderDied() will be called with a reference
95     * to this.
96     *
97     * The @a cookie is optional -- if non-NULL, it should be a
98     * memory address that you own (that is, you know it is unique).
99     *
100     * @note You will only receive death notifications for remote binders,
101     * as local binders by definition can't die without you dying as well.
102     * Trying to use this function on a local binder will result in an
103     * INVALID_OPERATION code being returned and nothing happening.
104     *
105     * @note This link always holds a weak reference to its recipient.
106     *
107     * @note You will only receive a weak reference to the dead
108     * binder.  You should not try to promote this to a strong reference.
109     * (Nor should you need to, as there is nothing useful you can
110     * directly do with it now that it has passed on.)
111     */
112    virtual status_t        linkToDeath(const sp<DeathRecipient>& recipient,
113                                        void* cookie = NULL,
114                                        uint32_t flags = 0) = 0;
115
116    /**
117     * Remove a previously registered death notification.
118     * The @a recipient will no longer be called if this object
119     * dies.  The @a cookie is optional.  If non-NULL, you can
120     * supply a NULL @a recipient, and the recipient previously
121     * added with that cookie will be unlinked.
122     */
123    virtual status_t        unlinkToDeath(  const wp<DeathRecipient>& recipient,
124                                            void* cookie = NULL,
125                                            uint32_t flags = 0,
126                                            wp<DeathRecipient>* outRecipient = NULL) = 0;
127
128    virtual bool            checkSubclass(const void* subclassID) const;
129
130    typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);
131
132    virtual void            attachObject(   const void* objectID,
133                                            void* object,
134                                            void* cleanupCookie,
135                                            object_cleanup_func func) = 0;
136    virtual void*           findObject(const void* objectID) const = 0;
137    virtual void            detachObject(const void* objectID) = 0;
138
139    virtual BBinder*        localBinder();
140    virtual BpBinder*       remoteBinder();
141
142protected:
143    virtual          ~IBinder();
144
145private:
146};
147
148}; // namespace android
149
150// ---------------------------------------------------------------------------
151
152#endif // ANDROID_IBINDER_H
153