IEffect.cpp 6.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
**
** Copyright 2010, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

//#define LOG_NDEBUG 0
#define LOG_TAG "IEffect"
#include <utils/Log.h>
#include <stdint.h>
#include <sys/types.h>
#include <binder/Parcel.h>
#include <media/IEffect.h>

namespace android {

enum {
    ENABLE = IBinder::FIRST_CALL_TRANSACTION,
    DISABLE,
    COMMAND,
    DISCONNECT,
    GET_CBLK
};

class BpEffect: public BpInterface<IEffect>
{
public:
    BpEffect(const sp<IBinder>& impl)
        : BpInterface<IEffect>(impl)
    {
    }

    status_t enable()
    {
46
        ALOGV("enable");
47 48 49 50 51 52 53 54
        Parcel data, reply;
        data.writeInterfaceToken(IEffect::getInterfaceDescriptor());
        remote()->transact(ENABLE, data, &reply);
        return reply.readInt32();
    }

    status_t disable()
    {
55
        ALOGV("disable");
56 57 58 59 60 61
        Parcel data, reply;
        data.writeInterfaceToken(IEffect::getInterfaceDescriptor());
        remote()->transact(DISABLE, data, &reply);
        return reply.readInt32();
    }

62 63 64 65 66
    status_t command(uint32_t cmdCode,
                     uint32_t cmdSize,
                     void *pCmdData,
                     uint32_t *pReplySize,
                     void *pReplyData)
67
    {
68
        ALOGV("command");
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        Parcel data, reply;
        data.writeInterfaceToken(IEffect::getInterfaceDescriptor());
        data.writeInt32(cmdCode);
        int size = cmdSize;
        if (pCmdData == NULL) {
            size = 0;
        }
        data.writeInt32(size);
        if (size) {
            data.write(pCmdData, size);
        }
        if (pReplySize == NULL) {
            size = 0;
        } else {
            size = *pReplySize;
        }
        data.writeInt32(size);
86 87

        status_t status = remote()->transact(COMMAND, data, &reply);
88 89 90
        if (status == NO_ERROR) {
            status = reply.readInt32();
        }
91 92 93 94 95 96
        if (status != NO_ERROR) {
            if (pReplySize != NULL)
                *pReplySize = 0;
            return status;
        }

97 98 99 100 101 102 103 104 105 106
        size = reply.readInt32();
        if (size != 0 && pReplyData != NULL && pReplySize != NULL) {
            reply.read(pReplyData, size);
            *pReplySize = size;
        }
        return status;
    }

    void disconnect()
    {
107
        ALOGV("disconnect");
108 109 110 111 112 113 114 115 116 117 118 119 120 121
        Parcel data, reply;
        data.writeInterfaceToken(IEffect::getInterfaceDescriptor());
        remote()->transact(DISCONNECT, data, &reply);
        return;
    }

    virtual sp<IMemory> getCblk() const
    {
        Parcel data, reply;
        sp<IMemory> cblk;
        data.writeInterfaceToken(IEffect::getInterfaceDescriptor());
        status_t status = remote()->transact(GET_CBLK, data, &reply);
        if (status == NO_ERROR) {
            cblk = interface_cast<IMemory>(reply.readStrongBinder());
122 123 124
            if (cblk != 0 && cblk->pointer() == NULL) {
                cblk.clear();
            }
125 126 127 128 129 130 131 132 133 134 135 136
        }
        return cblk;
    }
 };

IMPLEMENT_META_INTERFACE(Effect, "android.media.IEffect");

// ----------------------------------------------------------------------

status_t BnEffect::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
Glenn Kasten's avatar
Glenn Kasten committed
137
    switch (code) {
138
        case ENABLE: {
139
            ALOGV("ENABLE");
140 141 142 143 144 145
            CHECK_INTERFACE(IEffect, data, reply);
            reply->writeInt32(enable());
            return NO_ERROR;
        } break;

        case DISABLE: {
146
            ALOGV("DISABLE");
147 148 149 150 151 152
            CHECK_INTERFACE(IEffect, data, reply);
            reply->writeInt32(disable());
            return NO_ERROR;
        } break;

        case COMMAND: {
153
            ALOGV("COMMAND");
154
            CHECK_INTERFACE(IEffect, data, reply);
155 156
            uint32_t cmdCode = data.readInt32();
            uint32_t cmdSize = data.readInt32();
157 158
            char *cmd = NULL;
            if (cmdSize) {
159
                cmd = (char *)calloc(cmdSize, 1);
160 161 162 163
                if (cmd == NULL) {
                    reply->writeInt32(NO_MEMORY);
                    return NO_ERROR;
                }
164 165
                data.read(cmd, cmdSize);
            }
166 167
            uint32_t replySize = data.readInt32();
            uint32_t replySz = replySize;
168 169
            char *resp = NULL;
            if (replySize) {
170
                resp = (char *)calloc(replySize, 1);
171 172 173 174 175
                if (resp == NULL) {
                    free(cmd);
                    reply->writeInt32(NO_MEMORY);
                    return NO_ERROR;
                }
176 177 178
            }
            status_t status = command(cmdCode, cmdSize, cmd, &replySz, resp);
            reply->writeInt32(status);
179 180 181 182 183 184 185 186
            if (status == NO_ERROR) {
                if (replySz < replySize) {
                    replySize = replySz;
                }
                reply->writeInt32(replySize);
                if (replySize) {
                    reply->write(resp, replySize);
                }
187 188 189 190 191 192 193 194 195 196 197
            }
            if (cmd) {
                free(cmd);
            }
            if (resp) {
                free(resp);
            }
            return NO_ERROR;
        } break;

        case DISCONNECT: {
198
            ALOGV("DISCONNECT");
199 200 201 202 203 204
            CHECK_INTERFACE(IEffect, data, reply);
            disconnect();
            return NO_ERROR;
        } break;

        case GET_CBLK: {
Glenn Kasten's avatar
Glenn Kasten committed
205
            CHECK_INTERFACE(IEffect, data, reply);
206
            reply->writeStrongBinder(IInterface::asBinder(getCblk()));
Glenn Kasten's avatar
Glenn Kasten committed
207 208
            return NO_ERROR;
        } break;
209 210 211 212 213 214 215 216

        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}

// ----------------------------------------------------------------------------

217
} // namespace android