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