1/*
2 * Copyright (C) 2010 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//#define LOG_NDEBUG 0
18#define LOG_TAG "IDrmManagerService(Native)"
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <sys/types.h>
23#include <binder/IPCThreadState.h>
24
25#include <drm/DrmInfo.h>
26#include <drm/DrmConstraints.h>
27#include <drm/DrmMetadata.h>
28#include <drm/DrmRights.h>
29#include <drm/DrmInfoStatus.h>
30#include <drm/DrmConvertedStatus.h>
31#include <drm/DrmInfoRequest.h>
32#include <drm/DrmSupportInfo.h>
33
34#include "IDrmManagerService.h"
35
36#define INVALID_BUFFER_LENGTH -1
37
38using namespace android;
39
40static void writeDecryptHandleToParcelData(
41        const DecryptHandle* handle, Parcel* data) {
42    data->writeInt32(handle->decryptId);
43    data->writeString8(handle->mimeType);
44    data->writeInt32(handle->decryptApiType);
45    data->writeInt32(handle->status);
46
47    int size = handle->copyControlVector.size();
48    data->writeInt32(size);
49    for (int i = 0; i < size; i++) {
50        data->writeInt32(handle->copyControlVector.keyAt(i));
51        data->writeInt32(handle->copyControlVector.valueAt(i));
52    }
53
54    size = handle->extendedData.size();
55    data->writeInt32(size);
56    for (int i = 0; i < size; i++) {
57        data->writeString8(handle->extendedData.keyAt(i));
58        data->writeString8(handle->extendedData.valueAt(i));
59    }
60
61    if (NULL != handle->decryptInfo) {
62        data->writeInt32(handle->decryptInfo->decryptBufferLength);
63    } else {
64        data->writeInt32(INVALID_BUFFER_LENGTH);
65    }
66}
67
68static void readDecryptHandleFromParcelData(
69        DecryptHandle* handle, const Parcel& data) {
70    if (0 == data.dataAvail()) {
71        return;
72    }
73
74    handle->decryptId = data.readInt32();
75    handle->mimeType = data.readString8();
76    handle->decryptApiType = data.readInt32();
77    handle->status = data.readInt32();
78
79    int size = data.readInt32();
80    for (int i = 0; i < size; i++) {
81        DrmCopyControl key = (DrmCopyControl)data.readInt32();
82        int value = data.readInt32();
83        handle->copyControlVector.add(key, value);
84    }
85
86    size = data.readInt32();
87    for (int i = 0; i < size; i++) {
88        String8 key = data.readString8();
89        String8 value = data.readString8();
90        handle->extendedData.add(key, value);
91    }
92
93    handle->decryptInfo = NULL;
94    const int bufferLen = data.readInt32();
95    if (INVALID_BUFFER_LENGTH != bufferLen) {
96        handle->decryptInfo = new DecryptInfo();
97        handle->decryptInfo->decryptBufferLength = bufferLen;
98    }
99}
100
101static void clearDecryptHandle(DecryptHandle* handle) {
102    if (handle == NULL) {
103        return;
104    }
105    if (handle->decryptInfo) {
106        delete handle->decryptInfo;
107        handle->decryptInfo = NULL;
108    }
109    handle->copyControlVector.clear();
110    handle->extendedData.clear();
111}
112
113int BpDrmManagerService::addUniqueId(bool isNative) {
114    ALOGV("add uniqueid");
115    Parcel data, reply;
116    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
117    data.writeInt32(isNative);
118    remote()->transact(ADD_UNIQUEID, data, &reply);
119    return reply.readInt32();
120}
121
122void BpDrmManagerService::removeUniqueId(int uniqueId) {
123    ALOGV("remove uniqueid");
124    Parcel data, reply;
125    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
126    data.writeInt32(uniqueId);
127    remote()->transact(REMOVE_UNIQUEID, data, &reply);
128}
129
130void BpDrmManagerService::addClient(int uniqueId) {
131    Parcel data, reply;
132    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
133    data.writeInt32(uniqueId);
134    remote()->transact(ADD_CLIENT, data, &reply);
135}
136
137void BpDrmManagerService::removeClient(int uniqueId) {
138    Parcel data, reply;
139    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
140    data.writeInt32(uniqueId);
141    remote()->transact(REMOVE_CLIENT, data, &reply);
142}
143
144status_t BpDrmManagerService::setDrmServiceListener(
145            int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
146    ALOGV("setDrmServiceListener");
147    Parcel data, reply;
148
149    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
150    data.writeInt32(uniqueId);
151    data.writeStrongBinder(drmServiceListener->asBinder());
152    remote()->transact(SET_DRM_SERVICE_LISTENER, data, &reply);
153    return reply.readInt32();
154}
155
156DrmConstraints* BpDrmManagerService::getConstraints(
157            int uniqueId, const String8* path, const int action) {
158    ALOGV("Get Constraints");
159    Parcel data, reply;
160
161    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
162    data.writeInt32(uniqueId);
163    data.writeString8(*path);
164    data.writeInt32(action);
165
166    remote()->transact(GET_CONSTRAINTS_FROM_CONTENT, data, &reply);
167
168    DrmConstraints* drmConstraints = NULL;
169    if (0 != reply.dataAvail()) {
170        //Filling Drm Constraints
171        drmConstraints = new DrmConstraints();
172
173        const int size = reply.readInt32();
174        for (int index = 0; index < size; ++index) {
175            const String8 key(reply.readString8());
176            const int bufferSize = reply.readInt32();
177            char* data = NULL;
178            if (0 < bufferSize) {
179                data = new char[bufferSize];
180                reply.read(data, bufferSize);
181                drmConstraints->put(&key, data);
182                delete[] data;
183            }
184        }
185    }
186    return drmConstraints;
187}
188
189DrmMetadata* BpDrmManagerService::getMetadata(int uniqueId, const String8* path) {
190    ALOGV("Get Metadata");
191    Parcel data, reply;
192    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
193    data.writeInt32(uniqueId);
194
195    DrmMetadata* drmMetadata = NULL;
196    data.writeString8(*path);
197    remote()->transact(GET_METADATA_FROM_CONTENT, data, &reply);
198
199    if (0 != reply.dataAvail()) {
200        //Filling Drm Metadata
201        drmMetadata = new DrmMetadata();
202
203        const int size = reply.readInt32();
204        for (int index = 0; index < size; ++index) {
205            const String8 key(reply.readString8());
206            const int bufferSize = reply.readInt32();
207            char* data = NULL;
208            if (0 < bufferSize) {
209                data = new char[bufferSize];
210                reply.read(data, bufferSize);
211                drmMetadata->put(&key, data);
212                delete[] data;
213            }
214        }
215    }
216    return drmMetadata;
217}
218
219bool BpDrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
220    ALOGV("Can Handle");
221    Parcel data, reply;
222
223    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
224    data.writeInt32(uniqueId);
225
226    data.writeString8(path);
227    data.writeString8(mimeType);
228
229    remote()->transact(CAN_HANDLE, data, &reply);
230
231    return static_cast<bool>(reply.readInt32());
232}
233
234DrmInfoStatus* BpDrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
235    ALOGV("Process DRM Info");
236    Parcel data, reply;
237
238    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
239    data.writeInt32(uniqueId);
240
241    //Filling DRM info
242    data.writeInt32(drmInfo->getInfoType());
243    const DrmBuffer dataBuffer = drmInfo->getData();
244    const int dataBufferSize = dataBuffer.length;
245    data.writeInt32(dataBufferSize);
246    if (0 < dataBufferSize) {
247        data.write(dataBuffer.data, dataBufferSize);
248    }
249    data.writeString8(drmInfo->getMimeType());
250
251    data.writeInt32(drmInfo->getCount());
252    DrmInfo::KeyIterator keyIt = drmInfo->keyIterator();
253
254    while (keyIt.hasNext()) {
255        const String8 key = keyIt.next();
256        data.writeString8(key);
257        const String8 value = drmInfo->get(key);
258        data.writeString8((value == String8("")) ? String8("NULL") : value);
259    }
260
261    remote()->transact(PROCESS_DRM_INFO, data, &reply);
262
263    DrmInfoStatus* drmInfoStatus = NULL;
264    if (0 != reply.dataAvail()) {
265        //Filling DRM Info Status
266        const int statusCode = reply.readInt32();
267        const int infoType = reply.readInt32();
268        const String8 mimeType = reply.readString8();
269
270        DrmBuffer* drmBuffer = NULL;
271        if (0 != reply.dataAvail()) {
272            const int bufferSize = reply.readInt32();
273            char* data = NULL;
274            if (0 < bufferSize) {
275                data = new char[bufferSize];
276                reply.read(data, bufferSize);
277            }
278            drmBuffer = new DrmBuffer(data, bufferSize);
279        }
280        drmInfoStatus = new DrmInfoStatus(statusCode, infoType, drmBuffer, mimeType);
281    }
282    return drmInfoStatus;
283}
284
285DrmInfo* BpDrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInforequest) {
286    ALOGV("Acquire DRM Info");
287    Parcel data, reply;
288
289    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
290    data.writeInt32(uniqueId);
291
292    //Filling DRM Info Request
293    data.writeInt32(drmInforequest->getInfoType());
294    data.writeString8(drmInforequest->getMimeType());
295
296    data.writeInt32(drmInforequest->getCount());
297    DrmInfoRequest::KeyIterator keyIt = drmInforequest->keyIterator();
298
299    while (keyIt.hasNext()) {
300        const String8 key = keyIt.next();
301        data.writeString8(key);
302        const String8 value = drmInforequest->get(key);
303        if (key == String8("FileDescriptorKey")) {
304            int fd = -1;
305            sscanf(value.string(), "FileDescriptor[%d]", &fd);
306            data.writeFileDescriptor(fd);
307        } else {
308            data.writeString8((value == String8("")) ? String8("NULL") : value);
309        }
310    }
311
312    remote()->transact(ACQUIRE_DRM_INFO, data, &reply);
313
314    DrmInfo* drmInfo = NULL;
315    if (0 != reply.dataAvail()) {
316        //Filling DRM Info
317        const int infoType = reply.readInt32();
318        const int bufferSize = reply.readInt32();
319        char* data = NULL;
320
321        if (0 < bufferSize) {
322            data = new char[bufferSize];
323            reply.read(data, bufferSize);
324        }
325        drmInfo = new DrmInfo(infoType, DrmBuffer(data, bufferSize), reply.readString8());
326
327        const int size = reply.readInt32();
328        for (int index = 0; index < size; ++index) {
329            const String8 key(reply.readString8());
330            const String8 value(reply.readString8());
331            drmInfo->put(key, (value == String8("NULL")) ? String8("") : value);
332        }
333    }
334    return drmInfo;
335}
336
337status_t BpDrmManagerService::saveRights(
338            int uniqueId, const DrmRights& drmRights,
339            const String8& rightsPath, const String8& contentPath) {
340    ALOGV("Save Rights");
341    Parcel data, reply;
342
343    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
344    data.writeInt32(uniqueId);
345
346    //Filling Drm Rights
347    const DrmBuffer dataBuffer = drmRights.getData();
348    data.writeInt32(dataBuffer.length);
349    data.write(dataBuffer.data, dataBuffer.length);
350
351    const String8 mimeType = drmRights.getMimeType();
352    data.writeString8((mimeType == String8("")) ? String8("NULL") : mimeType);
353
354    const String8 accountId = drmRights.getAccountId();
355    data.writeString8((accountId == String8("")) ? String8("NULL") : accountId);
356
357    const String8 subscriptionId = drmRights.getSubscriptionId();
358    data.writeString8((subscriptionId == String8("")) ? String8("NULL") : subscriptionId);
359
360    data.writeString8((rightsPath == String8("")) ? String8("NULL") : rightsPath);
361    data.writeString8((contentPath == String8("")) ? String8("NULL") : contentPath);
362
363    remote()->transact(SAVE_RIGHTS, data, &reply);
364    return reply.readInt32();
365}
366
367String8 BpDrmManagerService::getOriginalMimeType(int uniqueId, const String8& path, int fd) {
368    ALOGV("Get Original MimeType");
369    Parcel data, reply;
370
371    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
372    data.writeInt32(uniqueId);
373    data.writeString8(path);
374    int32_t isFdValid = (fd >= 0);
375    data.writeInt32(isFdValid);
376    if (isFdValid) {
377        data.writeFileDescriptor(fd);
378    }
379
380    remote()->transact(GET_ORIGINAL_MIMETYPE, data, &reply);
381    return reply.readString8();
382}
383
384int BpDrmManagerService::getDrmObjectType(
385            int uniqueId, const String8& path, const String8& mimeType) {
386    ALOGV("Get Drm object type");
387    Parcel data, reply;
388
389    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
390    data.writeInt32(uniqueId);
391    data.writeString8(path);
392    data.writeString8(mimeType);
393
394    remote()->transact(GET_DRM_OBJECT_TYPE, data, &reply);
395
396    return reply.readInt32();
397}
398
399int BpDrmManagerService::checkRightsStatus(int uniqueId, const String8& path, int action) {
400    ALOGV("checkRightsStatus");
401    Parcel data, reply;
402
403    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
404    data.writeInt32(uniqueId);
405    data.writeString8(path);
406    data.writeInt32(action);
407
408    remote()->transact(CHECK_RIGHTS_STATUS, data, &reply);
409
410    return reply.readInt32();
411}
412
413status_t BpDrmManagerService::consumeRights(
414            int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
415    ALOGV("consumeRights");
416    Parcel data, reply;
417
418    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
419    data.writeInt32(uniqueId);
420
421    writeDecryptHandleToParcelData(decryptHandle, &data);
422
423    data.writeInt32(action);
424    data.writeInt32(static_cast< int>(reserve));
425
426    remote()->transact(CONSUME_RIGHTS, data, &reply);
427    return reply.readInt32();
428}
429
430status_t BpDrmManagerService::setPlaybackStatus(
431            int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
432    ALOGV("setPlaybackStatus");
433    Parcel data, reply;
434
435    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
436    data.writeInt32(uniqueId);
437
438    writeDecryptHandleToParcelData(decryptHandle, &data);
439
440    data.writeInt32(playbackStatus);
441    data.writeInt64(position);
442
443    remote()->transact(SET_PLAYBACK_STATUS, data, &reply);
444    return reply.readInt32();
445}
446
447bool BpDrmManagerService::validateAction(
448            int uniqueId, const String8& path,
449            int action, const ActionDescription& description) {
450    ALOGV("validateAction");
451    Parcel data, reply;
452
453    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
454    data.writeInt32(uniqueId);
455    data.writeString8(path);
456    data.writeInt32(action);
457    data.writeInt32(description.outputType);
458    data.writeInt32(description.configuration);
459
460    remote()->transact(VALIDATE_ACTION, data, &reply);
461
462    return static_cast<bool>(reply.readInt32());
463}
464
465status_t BpDrmManagerService::removeRights(int uniqueId, const String8& path) {
466    ALOGV("removeRights");
467    Parcel data, reply;
468
469    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
470    data.writeInt32(uniqueId);
471    data.writeString8(path);
472
473    remote()->transact(REMOVE_RIGHTS, data, &reply);
474    return reply.readInt32();
475}
476
477status_t BpDrmManagerService::removeAllRights(int uniqueId) {
478    ALOGV("removeAllRights");
479    Parcel data, reply;
480
481    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
482    data.writeInt32(uniqueId);
483
484    remote()->transact(REMOVE_ALL_RIGHTS, data, &reply);
485    return reply.readInt32();
486}
487
488int BpDrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
489    ALOGV("openConvertSession");
490    Parcel data, reply;
491
492    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
493    data.writeInt32(uniqueId);
494    data.writeString8(mimeType);
495
496    remote()->transact(OPEN_CONVERT_SESSION, data, &reply);
497    return reply.readInt32();
498}
499
500DrmConvertedStatus* BpDrmManagerService::convertData(
501            int uniqueId, int convertId, const DrmBuffer* inputData) {
502    ALOGV("convertData");
503    Parcel data, reply;
504
505    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
506    data.writeInt32(uniqueId);
507    data.writeInt32(convertId);
508    data.writeInt32(inputData->length);
509    data.write(inputData->data, inputData->length);
510
511    remote()->transact(CONVERT_DATA, data, &reply);
512
513    DrmConvertedStatus* drmConvertedStatus = NULL;
514
515    if (0 != reply.dataAvail()) {
516        //Filling DRM Converted Status
517        const int statusCode = reply.readInt32();
518        const off64_t offset = reply.readInt64();
519
520        DrmBuffer* convertedData = NULL;
521        if (0 != reply.dataAvail()) {
522            const int bufferSize = reply.readInt32();
523            char* data = NULL;
524            if (0 < bufferSize) {
525                data = new char[bufferSize];
526                reply.read(data, bufferSize);
527            }
528            convertedData = new DrmBuffer(data, bufferSize);
529        }
530        drmConvertedStatus = new DrmConvertedStatus(statusCode, convertedData, offset);
531    }
532    return drmConvertedStatus;
533}
534
535DrmConvertedStatus* BpDrmManagerService::closeConvertSession(int uniqueId, int convertId) {
536    ALOGV("closeConvertSession");
537    Parcel data, reply;
538
539    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
540    data.writeInt32(uniqueId);
541    data.writeInt32(convertId);
542
543    remote()->transact(CLOSE_CONVERT_SESSION, data, &reply);
544
545    DrmConvertedStatus* drmConvertedStatus = NULL;
546
547    if (0 != reply.dataAvail()) {
548        //Filling DRM Converted Status
549        const int statusCode = reply.readInt32();
550        const off64_t offset = reply.readInt64();
551
552        DrmBuffer* convertedData = NULL;
553        if (0 != reply.dataAvail()) {
554            const int bufferSize = reply.readInt32();
555            char* data = NULL;
556            if (0 < bufferSize) {
557                data = new char[bufferSize];
558                reply.read(data, bufferSize);
559            }
560            convertedData = new DrmBuffer(data, bufferSize);
561        }
562        drmConvertedStatus = new DrmConvertedStatus(statusCode, convertedData, offset);
563    }
564    return drmConvertedStatus;
565}
566
567status_t BpDrmManagerService::getAllSupportInfo(
568            int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
569    ALOGV("Get All Support Info");
570    Parcel data, reply;
571
572    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
573    data.writeInt32(uniqueId);
574
575    remote()->transact(GET_ALL_SUPPORT_INFO, data, &reply);
576
577    //Filling DRM Support Info
578    const int arraySize = reply.readInt32();
579    if (0 < arraySize) {
580        *drmSupportInfoArray = new DrmSupportInfo[arraySize];
581
582        for (int index = 0; index < arraySize; ++index) {
583            DrmSupportInfo drmSupportInfo;
584
585            const int fileSuffixVectorSize = reply.readInt32();
586            for (int i = 0; i < fileSuffixVectorSize; ++i) {
587                drmSupportInfo.addFileSuffix(reply.readString8());
588            }
589
590            const int mimeTypeVectorSize = reply.readInt32();
591            for (int i = 0; i < mimeTypeVectorSize; ++i) {
592                drmSupportInfo.addMimeType(reply.readString8());
593            }
594
595            drmSupportInfo.setDescription(reply.readString8());
596            (*drmSupportInfoArray)[index] = drmSupportInfo;
597        }
598    }
599    *length = arraySize;
600    return reply.readInt32();
601}
602
603DecryptHandle* BpDrmManagerService::openDecryptSession(
604            int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
605    ALOGV("Entering BpDrmManagerService::openDecryptSession");
606    Parcel data, reply;
607
608    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
609    data.writeInt32(uniqueId);
610    data.writeFileDescriptor(fd);
611    data.writeInt64(offset);
612    data.writeInt64(length);
613    String8 mimeType;
614    if (mime) {
615        mimeType = mime;
616    }
617    data.writeString8(mimeType);
618
619    remote()->transact(OPEN_DECRYPT_SESSION, data, &reply);
620
621    DecryptHandle* handle = NULL;
622    if (0 != reply.dataAvail()) {
623        handle = new DecryptHandle();
624        readDecryptHandleFromParcelData(handle, reply);
625    }
626    return handle;
627}
628
629DecryptHandle* BpDrmManagerService::openDecryptSession(
630        int uniqueId, const char* uri, const char* mime) {
631
632    ALOGV("Entering BpDrmManagerService::openDecryptSession: mime=%s", mime? mime: "NULL");
633    Parcel data, reply;
634
635    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
636    data.writeInt32(uniqueId);
637    data.writeString8(String8(uri));
638    String8 mimeType;
639    if (mime) {
640        mimeType = mime;
641    }
642    data.writeString8(mimeType);
643
644    remote()->transact(OPEN_DECRYPT_SESSION_FROM_URI, data, &reply);
645
646    DecryptHandle* handle = NULL;
647    if (0 != reply.dataAvail()) {
648        handle = new DecryptHandle();
649        readDecryptHandleFromParcelData(handle, reply);
650    } else {
651        ALOGV("no decryptHandle is generated in service side");
652    }
653    return handle;
654}
655
656DecryptHandle* BpDrmManagerService::openDecryptSession(
657            int uniqueId, const DrmBuffer& buf, const String8& mimeType) {
658    ALOGV("Entering BpDrmManagerService::openDecryptSession");
659    Parcel data, reply;
660
661    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
662    data.writeInt32(uniqueId);
663    if (buf.data != NULL && buf.length > 0) {
664        data.writeInt32(buf.length);
665        data.write(buf.data, buf.length);
666    } else {
667        data.writeInt32(0);
668    }
669    data.writeString8(mimeType);
670
671    remote()->transact(OPEN_DECRYPT_SESSION_FOR_STREAMING, data, &reply);
672
673    DecryptHandle* handle = NULL;
674    if (0 != reply.dataAvail()) {
675        handle = new DecryptHandle();
676        readDecryptHandleFromParcelData(handle, reply);
677    } else {
678        ALOGV("no decryptHandle is generated in service side");
679    }
680    return handle;
681}
682
683status_t BpDrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
684    ALOGV("closeDecryptSession");
685    Parcel data, reply;
686
687    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
688    data.writeInt32(uniqueId);
689
690    writeDecryptHandleToParcelData(decryptHandle, &data);
691
692    remote()->transact(CLOSE_DECRYPT_SESSION, data, &reply);
693
694    return reply.readInt32();
695}
696
697status_t BpDrmManagerService::initializeDecryptUnit(
698            int uniqueId, DecryptHandle* decryptHandle,
699            int decryptUnitId, const DrmBuffer* headerInfo) {
700    ALOGV("initializeDecryptUnit");
701    Parcel data, reply;
702
703    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
704    data.writeInt32(uniqueId);
705
706    writeDecryptHandleToParcelData(decryptHandle, &data);
707
708    data.writeInt32(decryptUnitId);
709
710    data.writeInt32(headerInfo->length);
711    data.write(headerInfo->data, headerInfo->length);
712
713    remote()->transact(INITIALIZE_DECRYPT_UNIT, data, &reply);
714    return reply.readInt32();
715}
716
717status_t BpDrmManagerService::decrypt(
718            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
719            const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
720    ALOGV("decrypt");
721    Parcel data, reply;
722
723    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
724    data.writeInt32(uniqueId);
725
726    writeDecryptHandleToParcelData(decryptHandle, &data);
727
728    data.writeInt32(decryptUnitId);
729    data.writeInt32((*decBuffer)->length);
730
731    data.writeInt32(encBuffer->length);
732    data.write(encBuffer->data, encBuffer->length);
733
734    if (NULL != IV) {
735        data.writeInt32(IV->length);
736        data.write(IV->data, IV->length);
737    }
738
739    remote()->transact(DECRYPT, data, &reply);
740
741    const status_t status = reply.readInt32();
742    ALOGV("Return value of decrypt() is %d", status);
743
744    const int size = reply.readInt32();
745    (*decBuffer)->length = size;
746    reply.read((void *)(*decBuffer)->data, size);
747
748    return status;
749}
750
751status_t BpDrmManagerService::finalizeDecryptUnit(
752            int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
753    ALOGV("finalizeDecryptUnit");
754    Parcel data, reply;
755
756    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
757    data.writeInt32(uniqueId);
758
759    writeDecryptHandleToParcelData(decryptHandle, &data);
760
761    data.writeInt32(decryptUnitId);
762
763    remote()->transact(FINALIZE_DECRYPT_UNIT, data, &reply);
764    return reply.readInt32();
765}
766
767ssize_t BpDrmManagerService::pread(
768            int uniqueId, DecryptHandle* decryptHandle, void* buffer,
769            ssize_t numBytes, off64_t offset) {
770    ALOGV("read");
771    Parcel data, reply;
772    int result;
773
774    data.writeInterfaceToken(IDrmManagerService::getInterfaceDescriptor());
775    data.writeInt32(uniqueId);
776
777    writeDecryptHandleToParcelData(decryptHandle, &data);
778
779    data.writeInt32(numBytes);
780    data.writeInt64(offset);
781
782    remote()->transact(PREAD, data, &reply);
783    result = reply.readInt32();
784    if (0 < result) {
785        reply.read(buffer, result);
786    }
787    return result;
788}
789
790IMPLEMENT_META_INTERFACE(DrmManagerService, "drm.IDrmManagerService");
791
792status_t BnDrmManagerService::onTransact(
793            uint32_t code, const Parcel& data,
794            Parcel* reply, uint32_t flags) {
795    ALOGV("Entering BnDrmManagerService::onTransact with code %d", code);
796
797    switch (code) {
798    case ADD_UNIQUEID:
799    {
800        ALOGV("BnDrmManagerService::onTransact :ADD_UNIQUEID");
801        CHECK_INTERFACE(IDrmManagerService, data, reply);
802        int uniqueId = addUniqueId(data.readInt32());
803        reply->writeInt32(uniqueId);
804        return DRM_NO_ERROR;
805    }
806
807    case REMOVE_UNIQUEID:
808    {
809        ALOGV("BnDrmManagerService::onTransact :REMOVE_UNIQUEID");
810        CHECK_INTERFACE(IDrmManagerService, data, reply);
811        removeUniqueId(data.readInt32());
812        return DRM_NO_ERROR;
813    }
814
815    case ADD_CLIENT:
816    {
817        ALOGV("BnDrmManagerService::onTransact :ADD_CLIENT");
818        CHECK_INTERFACE(IDrmManagerService, data, reply);
819        addClient(data.readInt32());
820        return DRM_NO_ERROR;
821    }
822
823    case REMOVE_CLIENT:
824    {
825        ALOGV("BnDrmManagerService::onTransact :REMOVE_CLIENT");
826        CHECK_INTERFACE(IDrmManagerService, data, reply);
827        removeClient(data.readInt32());
828        return DRM_NO_ERROR;
829    }
830
831    case SET_DRM_SERVICE_LISTENER:
832    {
833        ALOGV("BnDrmManagerService::onTransact :SET_DRM_SERVICE_LISTENER");
834        CHECK_INTERFACE(IDrmManagerService, data, reply);
835
836        const int uniqueId = data.readInt32();
837        const sp<IDrmServiceListener> drmServiceListener
838            = interface_cast<IDrmServiceListener> (data.readStrongBinder());
839
840        status_t status = setDrmServiceListener(uniqueId, drmServiceListener);
841
842        reply->writeInt32(status);
843        return DRM_NO_ERROR;
844    }
845
846    case GET_CONSTRAINTS_FROM_CONTENT:
847    {
848        ALOGV("BnDrmManagerService::onTransact :GET_CONSTRAINTS_FROM_CONTENT");
849        CHECK_INTERFACE(IDrmManagerService, data, reply);
850
851        const int uniqueId = data.readInt32();
852        const String8 path = data.readString8();
853
854        DrmConstraints* drmConstraints
855            = getConstraints(uniqueId, &path, data.readInt32());
856
857        if (NULL != drmConstraints) {
858            //Filling DRM Constraints contents
859            reply->writeInt32(drmConstraints->getCount());
860
861            DrmConstraints::KeyIterator keyIt = drmConstraints->keyIterator();
862            while (keyIt.hasNext()) {
863                const String8 key = keyIt.next();
864                reply->writeString8(key);
865                const char* value = drmConstraints->getAsByteArray(&key);
866                int bufferSize = 0;
867                if (NULL != value) {
868                    bufferSize = strlen(value);
869                    reply->writeInt32(bufferSize + 1);
870                    reply->write(value, bufferSize + 1);
871                } else {
872                    reply->writeInt32(0);
873                }
874            }
875        }
876        delete drmConstraints; drmConstraints = NULL;
877        return DRM_NO_ERROR;
878    }
879
880    case GET_METADATA_FROM_CONTENT:
881    {
882        ALOGV("BnDrmManagerService::onTransact :GET_METADATA_FROM_CONTENT");
883        CHECK_INTERFACE(IDrmManagerService, data, reply);
884
885        const int uniqueId = data.readInt32();
886        const String8 path = data.readString8();
887
888        DrmMetadata* drmMetadata = getMetadata(uniqueId, &path);
889        if (NULL != drmMetadata) {
890            //Filling DRM Metadata contents
891            reply->writeInt32(drmMetadata->getCount());
892
893            DrmMetadata::KeyIterator keyIt = drmMetadata->keyIterator();
894            while (keyIt.hasNext()) {
895                const String8 key = keyIt.next();
896                reply->writeString8(key);
897                const char* value = drmMetadata->getAsByteArray(&key);
898                int bufferSize = 0;
899                if (NULL != value) {
900                    bufferSize = strlen(value);
901                    reply->writeInt32(bufferSize + 1);
902                    reply->write(value, bufferSize + 1);
903                } else {
904                    reply->writeInt32(0);
905                }
906            }
907        }
908        delete drmMetadata; drmMetadata = NULL;
909        return NO_ERROR;
910    }
911
912    case CAN_HANDLE:
913    {
914        ALOGV("BnDrmManagerService::onTransact :CAN_HANDLE");
915        CHECK_INTERFACE(IDrmManagerService, data, reply);
916
917        const int uniqueId = data.readInt32();
918        const String8 path = data.readString8();
919        const String8 mimeType = data.readString8();
920
921        bool result = canHandle(uniqueId, path, mimeType);
922
923        reply->writeInt32(result);
924        return DRM_NO_ERROR;
925    }
926
927    case PROCESS_DRM_INFO:
928    {
929        ALOGV("BnDrmManagerService::onTransact :PROCESS_DRM_INFO");
930        CHECK_INTERFACE(IDrmManagerService, data, reply);
931
932        const int uniqueId = data.readInt32();
933
934        //Filling DRM info
935        const int infoType = data.readInt32();
936        const int bufferSize = data.readInt32();
937        char* buffer = NULL;
938        if (0 < bufferSize) {
939            buffer = (char *)data.readInplace(bufferSize);
940        }
941        const DrmBuffer drmBuffer(buffer, bufferSize);
942        DrmInfo* drmInfo = new DrmInfo(infoType, drmBuffer, data.readString8());
943
944        const int size = data.readInt32();
945        for (int index = 0; index < size; ++index) {
946            const String8 key(data.readString8());
947            const String8 value(data.readString8());
948            drmInfo->put(key, (value == String8("NULL")) ? String8("") : value);
949        }
950
951        DrmInfoStatus* drmInfoStatus = processDrmInfo(uniqueId, drmInfo);
952
953        if (NULL != drmInfoStatus) {
954            //Filling DRM Info Status contents
955            reply->writeInt32(drmInfoStatus->statusCode);
956            reply->writeInt32(drmInfoStatus->infoType);
957            reply->writeString8(drmInfoStatus->mimeType);
958
959            if (NULL != drmInfoStatus->drmBuffer) {
960                const DrmBuffer* drmBuffer = drmInfoStatus->drmBuffer;
961                const int bufferSize = drmBuffer->length;
962                reply->writeInt32(bufferSize);
963                if (0 < bufferSize) {
964                    reply->write(drmBuffer->data, bufferSize);
965                }
966                delete [] drmBuffer->data;
967                delete drmBuffer; drmBuffer = NULL;
968            }
969        }
970        delete drmInfo; drmInfo = NULL;
971        delete drmInfoStatus; drmInfoStatus = NULL;
972        return DRM_NO_ERROR;
973    }
974
975    case ACQUIRE_DRM_INFO:
976    {
977        ALOGV("BnDrmManagerService::onTransact :ACQUIRE_DRM_INFO");
978        CHECK_INTERFACE(IDrmManagerService, data, reply);
979
980        const int uniqueId = data.readInt32();
981
982        //Filling DRM info Request
983        const int infoType = data.readInt32();
984        const String8 mimeType = data.readString8();
985        DrmInfoRequest* drmInfoRequest = new DrmInfoRequest(infoType, mimeType);
986
987        const int size = data.readInt32();
988        for (int index = 0; index < size; ++index) {
989            const String8 key(data.readString8());
990            if (key == String8("FileDescriptorKey")) {
991                char buffer[16];
992                int fd = data.readFileDescriptor();
993                sprintf(buffer, "%lu", (unsigned long)fd);
994                drmInfoRequest->put(key, String8(buffer));
995            } else {
996                const String8 value(data.readString8());
997                drmInfoRequest->put(key, (value == String8("NULL")) ? String8("") : value);
998            }
999        }
1000
1001        DrmInfo* drmInfo = acquireDrmInfo(uniqueId, drmInfoRequest);
1002
1003        if (NULL != drmInfo) {
1004            //Filling DRM Info
1005            const DrmBuffer drmBuffer = drmInfo->getData();
1006            reply->writeInt32(drmInfo->getInfoType());
1007
1008            const int bufferSize = drmBuffer.length;
1009            reply->writeInt32(bufferSize);
1010            if (0 < bufferSize) {
1011                reply->write(drmBuffer.data, bufferSize);
1012            }
1013            reply->writeString8(drmInfo->getMimeType());
1014            reply->writeInt32(drmInfo->getCount());
1015
1016            DrmInfo::KeyIterator keyIt = drmInfo->keyIterator();
1017            while (keyIt.hasNext()) {
1018                const String8 key = keyIt.next();
1019                reply->writeString8(key);
1020                const String8 value = drmInfo->get(key);
1021                reply->writeString8((value == String8("")) ? String8("NULL") : value);
1022            }
1023            delete [] drmBuffer.data;
1024        }
1025        delete drmInfoRequest; drmInfoRequest = NULL;
1026        delete drmInfo; drmInfo = NULL;
1027        return DRM_NO_ERROR;
1028    }
1029
1030    case SAVE_RIGHTS:
1031    {
1032        ALOGV("BnDrmManagerService::onTransact :SAVE_RIGHTS");
1033        CHECK_INTERFACE(IDrmManagerService, data, reply);
1034
1035        const int uniqueId = data.readInt32();
1036
1037        //Filling DRM Rights
1038        const int bufferSize = data.readInt32();
1039        const DrmBuffer drmBuffer((char *)data.readInplace(bufferSize), bufferSize);
1040
1041        const String8 mimeType(data.readString8());
1042        const String8 accountId(data.readString8());
1043        const String8 subscriptionId(data.readString8());
1044        const String8 rightsPath(data.readString8());
1045        const String8 contentPath(data.readString8());
1046
1047        DrmRights drmRights(drmBuffer,
1048                            ((mimeType == String8("NULL")) ? String8("") : mimeType),
1049                            ((accountId == String8("NULL")) ? String8("") : accountId),
1050                            ((subscriptionId == String8("NULL")) ? String8("") : subscriptionId));
1051
1052        const status_t status = saveRights(uniqueId, drmRights,
1053                            ((rightsPath == String8("NULL")) ? String8("") : rightsPath),
1054                            ((contentPath == String8("NULL")) ? String8("") : contentPath));
1055
1056        reply->writeInt32(status);
1057        return DRM_NO_ERROR;
1058    }
1059
1060    case GET_ORIGINAL_MIMETYPE:
1061    {
1062        ALOGV("BnDrmManagerService::onTransact :GET_ORIGINAL_MIMETYPE");
1063        CHECK_INTERFACE(IDrmManagerService, data, reply);
1064
1065        const int uniqueId = data.readInt32();
1066        const String8 path = data.readString8();
1067        const int32_t isFdValid = data.readInt32();
1068        int fd = -1;
1069        if (isFdValid) {
1070            fd = data.readFileDescriptor();
1071        }
1072        const String8 originalMimeType = getOriginalMimeType(uniqueId, path, fd);
1073
1074        reply->writeString8(originalMimeType);
1075        return DRM_NO_ERROR;
1076    }
1077
1078    case GET_DRM_OBJECT_TYPE:
1079    {
1080        ALOGV("BnDrmManagerService::onTransact :GET_DRM_OBJECT_TYPE");
1081        CHECK_INTERFACE(IDrmManagerService, data, reply);
1082
1083        const int uniqueId = data.readInt32();
1084        const String8 path = data.readString8();
1085        const String8 mimeType = data.readString8();
1086        const int drmObjectType = getDrmObjectType(uniqueId, path, mimeType);
1087
1088        reply->writeInt32(drmObjectType);
1089        return DRM_NO_ERROR;
1090    }
1091
1092    case CHECK_RIGHTS_STATUS:
1093    {
1094        ALOGV("BnDrmManagerService::onTransact :CHECK_RIGHTS_STATUS");
1095        CHECK_INTERFACE(IDrmManagerService, data, reply);
1096
1097        const int uniqueId = data.readInt32();
1098        const String8 path = data.readString8();
1099        const int action = data.readInt32();
1100        const int result = checkRightsStatus(uniqueId, path, action);
1101
1102        reply->writeInt32(result);
1103        return DRM_NO_ERROR;
1104    }
1105
1106    case CONSUME_RIGHTS:
1107    {
1108        ALOGV("BnDrmManagerService::onTransact :CONSUME_RIGHTS");
1109        CHECK_INTERFACE(IDrmManagerService, data, reply);
1110
1111        const int uniqueId = data.readInt32();
1112
1113        DecryptHandle handle;
1114        readDecryptHandleFromParcelData(&handle, data);
1115
1116        const int action = data.readInt32();
1117        const bool reserve = static_cast<bool>(data.readInt32());
1118        const status_t status
1119            = consumeRights(uniqueId, &handle, action, reserve);
1120        reply->writeInt32(status);
1121
1122        clearDecryptHandle(&handle);
1123        return DRM_NO_ERROR;
1124    }
1125
1126    case SET_PLAYBACK_STATUS:
1127    {
1128        ALOGV("BnDrmManagerService::onTransact :SET_PLAYBACK_STATUS");
1129        CHECK_INTERFACE(IDrmManagerService, data, reply);
1130
1131        const int uniqueId = data.readInt32();
1132
1133        DecryptHandle handle;
1134        readDecryptHandleFromParcelData(&handle, data);
1135
1136        const int playbackStatus = data.readInt32();
1137        const int64_t position = data.readInt64();
1138        const status_t status
1139            = setPlaybackStatus(uniqueId, &handle, playbackStatus, position);
1140        reply->writeInt32(status);
1141
1142        clearDecryptHandle(&handle);
1143        return DRM_NO_ERROR;
1144    }
1145
1146    case VALIDATE_ACTION:
1147    {
1148        ALOGV("BnDrmManagerService::onTransact :VALIDATE_ACTION");
1149        CHECK_INTERFACE(IDrmManagerService, data, reply);
1150
1151        const int uniqueId = data.readInt32();
1152        const String8 path = data.readString8();
1153        const int action = data.readInt32();
1154        const int outputType = data.readInt32();
1155        const int configuration = data.readInt32();
1156        bool result = validateAction(uniqueId, path, action,
1157                ActionDescription(outputType, configuration));
1158
1159        reply->writeInt32(result);
1160        return DRM_NO_ERROR;
1161    }
1162
1163    case REMOVE_RIGHTS:
1164    {
1165        ALOGV("BnDrmManagerService::onTransact :REMOVE_RIGHTS");
1166        CHECK_INTERFACE(IDrmManagerService, data, reply);
1167
1168        int uniqueId = data.readInt32();
1169        String8 path = data.readString8();
1170        const status_t status = removeRights(uniqueId, path);
1171        reply->writeInt32(status);
1172
1173        return DRM_NO_ERROR;
1174    }
1175
1176    case REMOVE_ALL_RIGHTS:
1177    {
1178        ALOGV("BnDrmManagerService::onTransact :REMOVE_ALL_RIGHTS");
1179        CHECK_INTERFACE(IDrmManagerService, data, reply);
1180
1181        const status_t status = removeAllRights(data.readInt32());
1182        reply->writeInt32(status);
1183
1184        return DRM_NO_ERROR;
1185    }
1186
1187    case OPEN_CONVERT_SESSION:
1188    {
1189        ALOGV("BnDrmManagerService::onTransact :OPEN_CONVERT_SESSION");
1190        CHECK_INTERFACE(IDrmManagerService, data, reply);
1191
1192        const int uniqueId = data.readInt32();
1193        const String8 mimeType = data.readString8();
1194        const int convertId = openConvertSession(uniqueId, mimeType);
1195
1196        reply->writeInt32(convertId);
1197        return DRM_NO_ERROR;
1198    }
1199
1200    case CONVERT_DATA:
1201    {
1202        ALOGV("BnDrmManagerService::onTransact :CONVERT_DATA");
1203        CHECK_INTERFACE(IDrmManagerService, data, reply);
1204
1205        const int uniqueId = data.readInt32();
1206        const int convertId = data.readInt32();
1207
1208        //Filling input data
1209        const int bufferSize = data.readInt32();
1210        DrmBuffer* inputData = new DrmBuffer((char *)data.readInplace(bufferSize), bufferSize);
1211
1212        DrmConvertedStatus*    drmConvertedStatus = convertData(uniqueId, convertId, inputData);
1213
1214        if (NULL != drmConvertedStatus) {
1215            //Filling Drm Converted Ststus
1216            reply->writeInt32(drmConvertedStatus->statusCode);
1217            reply->writeInt64(drmConvertedStatus->offset);
1218
1219            if (NULL != drmConvertedStatus->convertedData) {
1220                const DrmBuffer* convertedData = drmConvertedStatus->convertedData;
1221                const int bufferSize = convertedData->length;
1222                reply->writeInt32(bufferSize);
1223                if (0 < bufferSize) {
1224                    reply->write(convertedData->data, bufferSize);
1225                }
1226                delete [] convertedData->data;
1227                delete convertedData; convertedData = NULL;
1228            }
1229        }
1230        delete inputData; inputData = NULL;
1231        delete drmConvertedStatus; drmConvertedStatus = NULL;
1232        return DRM_NO_ERROR;
1233    }
1234
1235    case CLOSE_CONVERT_SESSION:
1236    {
1237        ALOGV("BnDrmManagerService::onTransact :CLOSE_CONVERT_SESSION");
1238        CHECK_INTERFACE(IDrmManagerService, data, reply);
1239
1240        const int uniqueId = data.readInt32();
1241        const int convertId = data.readInt32();
1242        DrmConvertedStatus* drmConvertedStatus
1243            = closeConvertSession(uniqueId, convertId);
1244
1245        if (NULL != drmConvertedStatus) {
1246            //Filling Drm Converted Ststus
1247            reply->writeInt32(drmConvertedStatus->statusCode);
1248            reply->writeInt64(drmConvertedStatus->offset);
1249
1250            if (NULL != drmConvertedStatus->convertedData) {
1251                const DrmBuffer* convertedData = drmConvertedStatus->convertedData;
1252                const int bufferSize = convertedData->length;
1253                reply->writeInt32(bufferSize);
1254                if (0 < bufferSize) {
1255                    reply->write(convertedData->data, bufferSize);
1256                }
1257                delete [] convertedData->data;
1258                delete convertedData; convertedData = NULL;
1259            }
1260        }
1261        delete drmConvertedStatus; drmConvertedStatus = NULL;
1262        return DRM_NO_ERROR;
1263    }
1264
1265    case GET_ALL_SUPPORT_INFO:
1266    {
1267        ALOGV("BnDrmManagerService::onTransact :GET_ALL_SUPPORT_INFO");
1268        CHECK_INTERFACE(IDrmManagerService, data, reply);
1269
1270        const int uniqueId = data.readInt32();
1271        int length = 0;
1272        DrmSupportInfo* drmSupportInfoArray = NULL;
1273
1274        status_t status = getAllSupportInfo(uniqueId, &length, &drmSupportInfoArray);
1275
1276        reply->writeInt32(length);
1277        for (int i = 0; i < length; ++i) {
1278            DrmSupportInfo drmSupportInfo = drmSupportInfoArray[i];
1279
1280            reply->writeInt32(drmSupportInfo.getFileSuffixCount());
1281            DrmSupportInfo::FileSuffixIterator fileSuffixIt
1282                = drmSupportInfo.getFileSuffixIterator();
1283            while (fileSuffixIt.hasNext()) {
1284                reply->writeString8(fileSuffixIt.next());
1285            }
1286
1287            reply->writeInt32(drmSupportInfo.getMimeTypeCount());
1288            DrmSupportInfo::MimeTypeIterator mimeTypeIt = drmSupportInfo.getMimeTypeIterator();
1289            while (mimeTypeIt.hasNext()) {
1290                reply->writeString8(mimeTypeIt.next());
1291            }
1292            reply->writeString8(drmSupportInfo.getDescription());
1293        }
1294        delete [] drmSupportInfoArray; drmSupportInfoArray = NULL;
1295        reply->writeInt32(status);
1296        return DRM_NO_ERROR;
1297    }
1298
1299    case OPEN_DECRYPT_SESSION:
1300    {
1301        ALOGV("BnDrmManagerService::onTransact :OPEN_DECRYPT_SESSION");
1302        CHECK_INTERFACE(IDrmManagerService, data, reply);
1303
1304        const int uniqueId = data.readInt32();
1305        const int fd = data.readFileDescriptor();
1306
1307        const off64_t offset = data.readInt64();
1308        const off64_t length = data.readInt64();
1309        const String8 mime = data.readString8();
1310
1311        DecryptHandle* handle
1312            = openDecryptSession(uniqueId, fd, offset, length, mime.string());
1313
1314        if (NULL != handle) {
1315            writeDecryptHandleToParcelData(handle, reply);
1316            clearDecryptHandle(handle);
1317            delete handle; handle = NULL;
1318        }
1319        return DRM_NO_ERROR;
1320    }
1321
1322    case OPEN_DECRYPT_SESSION_FROM_URI:
1323    {
1324        ALOGV("BnDrmManagerService::onTransact :OPEN_DECRYPT_SESSION_FROM_URI");
1325        CHECK_INTERFACE(IDrmManagerService, data, reply);
1326
1327        const int uniqueId = data.readInt32();
1328        const String8 uri = data.readString8();
1329        const String8 mime = data.readString8();
1330
1331        DecryptHandle* handle = openDecryptSession(uniqueId, uri.string(), mime.string());
1332
1333        if (NULL != handle) {
1334            writeDecryptHandleToParcelData(handle, reply);
1335
1336            clearDecryptHandle(handle);
1337            delete handle; handle = NULL;
1338        } else {
1339            ALOGV("NULL decryptHandle is returned");
1340        }
1341        return DRM_NO_ERROR;
1342    }
1343
1344    case OPEN_DECRYPT_SESSION_FOR_STREAMING:
1345    {
1346        ALOGV("BnDrmManagerService::onTransact :OPEN_DECRYPT_SESSION_FOR_STREAMING");
1347        CHECK_INTERFACE(IDrmManagerService, data, reply);
1348
1349        const int uniqueId = data.readInt32();
1350        const int bufferSize = data.readInt32();
1351        DrmBuffer buf((bufferSize > 0) ? (char *)data.readInplace(bufferSize) : NULL,
1352                bufferSize);
1353        const String8 mimeType(data.readString8());
1354
1355        DecryptHandle* handle = openDecryptSession(uniqueId, buf, mimeType);
1356
1357        if (handle != NULL) {
1358            writeDecryptHandleToParcelData(handle, reply);
1359            clearDecryptHandle(handle);
1360            delete handle;
1361            handle = NULL;
1362        } else {
1363            ALOGV("NULL decryptHandle is returned");
1364        }
1365        return DRM_NO_ERROR;
1366    }
1367
1368    case CLOSE_DECRYPT_SESSION:
1369    {
1370        ALOGV("BnDrmManagerService::onTransact :CLOSE_DECRYPT_SESSION");
1371        CHECK_INTERFACE(IDrmManagerService, data, reply);
1372
1373        const int uniqueId = data.readInt32();
1374
1375        DecryptHandle* handle = new DecryptHandle();
1376        readDecryptHandleFromParcelData(handle, data);
1377
1378        const status_t status = closeDecryptSession(uniqueId, handle);
1379        reply->writeInt32(status);
1380        return DRM_NO_ERROR;
1381    }
1382
1383    case INITIALIZE_DECRYPT_UNIT:
1384    {
1385        ALOGV("BnDrmManagerService::onTransact :INITIALIZE_DECRYPT_UNIT");
1386        CHECK_INTERFACE(IDrmManagerService, data, reply);
1387
1388        const int uniqueId = data.readInt32();
1389
1390        DecryptHandle handle;
1391        readDecryptHandleFromParcelData(&handle, data);
1392
1393        const int decryptUnitId = data.readInt32();
1394
1395        //Filling Header info
1396        const int bufferSize = data.readInt32();
1397        DrmBuffer* headerInfo = NULL;
1398        headerInfo = new DrmBuffer((char *)data.readInplace(bufferSize), bufferSize);
1399
1400        const status_t status
1401            = initializeDecryptUnit(uniqueId, &handle, decryptUnitId, headerInfo);
1402        reply->writeInt32(status);
1403
1404        clearDecryptHandle(&handle);
1405        delete headerInfo; headerInfo = NULL;
1406        return DRM_NO_ERROR;
1407    }
1408
1409    case DECRYPT:
1410    {
1411        ALOGV("BnDrmManagerService::onTransact :DECRYPT");
1412        CHECK_INTERFACE(IDrmManagerService, data, reply);
1413
1414        const int uniqueId = data.readInt32();
1415
1416        DecryptHandle handle;
1417        readDecryptHandleFromParcelData(&handle, data);
1418
1419        const int decryptUnitId = data.readInt32();
1420        const int decBufferSize = data.readInt32();
1421
1422        const int encBufferSize = data.readInt32();
1423        DrmBuffer* encBuffer
1424            = new DrmBuffer((char *)data.readInplace(encBufferSize), encBufferSize);
1425
1426        char* buffer = NULL;
1427        buffer = new char[decBufferSize];
1428        DrmBuffer* decBuffer = new DrmBuffer(buffer, decBufferSize);
1429
1430        DrmBuffer* IV = NULL;
1431        if (0 != data.dataAvail()) {
1432            const int ivBufferlength = data.readInt32();
1433            IV = new DrmBuffer((char *)data.readInplace(ivBufferlength), ivBufferlength);
1434        }
1435
1436        const status_t status
1437            = decrypt(uniqueId, &handle, decryptUnitId, encBuffer, &decBuffer, IV);
1438
1439        reply->writeInt32(status);
1440
1441        const int size = decBuffer->length;
1442        reply->writeInt32(size);
1443        reply->write(decBuffer->data, size);
1444
1445        clearDecryptHandle(&handle);
1446        delete encBuffer; encBuffer = NULL;
1447        delete decBuffer; decBuffer = NULL;
1448        delete [] buffer; buffer = NULL;
1449        delete IV; IV = NULL;
1450        return DRM_NO_ERROR;
1451    }
1452
1453    case FINALIZE_DECRYPT_UNIT:
1454    {
1455        ALOGV("BnDrmManagerService::onTransact :FINALIZE_DECRYPT_UNIT");
1456        CHECK_INTERFACE(IDrmManagerService, data, reply);
1457
1458        const int uniqueId = data.readInt32();
1459
1460        DecryptHandle handle;
1461        readDecryptHandleFromParcelData(&handle, data);
1462
1463        const status_t status = finalizeDecryptUnit(uniqueId, &handle, data.readInt32());
1464        reply->writeInt32(status);
1465
1466        clearDecryptHandle(&handle);
1467        return DRM_NO_ERROR;
1468    }
1469
1470    case PREAD:
1471    {
1472        ALOGV("BnDrmManagerService::onTransact :READ");
1473        CHECK_INTERFACE(IDrmManagerService, data, reply);
1474
1475        const int uniqueId = data.readInt32();
1476
1477        DecryptHandle handle;
1478        readDecryptHandleFromParcelData(&handle, data);
1479
1480        const int numBytes = data.readInt32();
1481        char* buffer = new char[numBytes];
1482
1483        const off64_t offset = data.readInt64();
1484
1485        ssize_t result = pread(uniqueId, &handle, buffer, numBytes, offset);
1486        reply->writeInt32(result);
1487        if (0 < result) {
1488            reply->write(buffer, result);
1489        }
1490
1491        clearDecryptHandle(&handle);
1492        delete [] buffer, buffer = NULL;
1493        return DRM_NO_ERROR;
1494    }
1495
1496    default:
1497        return BBinder::onTransact(code, data, reply, flags);
1498    }
1499}
1500