1 /*
2  * Copyright (C) 2017 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 "CryptoHal"
19 #include <mediadrm/CryptoHal.h>
20 #include <mediadrm/CryptoHalHidl.h>
21 #include <mediadrm/CryptoHalAidl.h>
22 #include <mediadrm/DrmUtils.h>
23 
24 namespace android {
25 
CryptoHal()26 CryptoHal::CryptoHal() {
27     mCryptoHalAidl = sp<CryptoHalAidl>::make();
28     mCryptoHalHidl = sp<CryptoHalHidl>::make();
29 }
30 
~CryptoHal()31 CryptoHal::~CryptoHal() {}
32 
initCheck() const33 status_t CryptoHal::initCheck() const {
34     if (mCryptoHalAidl->initCheck() == OK || mCryptoHalHidl->initCheck() == OK) return OK;
35     if (mCryptoHalAidl->initCheck() == NO_INIT || mCryptoHalHidl->initCheck() == NO_INIT)
36         return NO_INIT;
37     return mCryptoHalHidl->initCheck();
38 }
39 
isCryptoSchemeSupported(const uint8_t uuid[16])40 bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) {
41     return mCryptoHalAidl->isCryptoSchemeSupported(uuid) ||
42            mCryptoHalHidl->isCryptoSchemeSupported(uuid);
43 }
44 
createPlugin(const uint8_t uuid[16],const void * data,size_t size)45 status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void* data, size_t size) {
46     if (mCryptoHalAidl->createPlugin(uuid, data, size) != OK)
47         return mCryptoHalHidl->createPlugin(uuid, data, size);
48     return OK;
49 }
50 
destroyPlugin()51 status_t CryptoHal::destroyPlugin() {
52     // This requires plugin to be created.
53     if (mCryptoHalAidl->initCheck() == OK) return mCryptoHalAidl->destroyPlugin();
54     return mCryptoHalHidl->destroyPlugin();
55 }
56 
requiresSecureDecoderComponent(const char * mime) const57 bool CryptoHal::requiresSecureDecoderComponent(const char* mime) const {
58     // This requires plugin to be created.
59     if (mCryptoHalAidl->initCheck() == OK)
60         return mCryptoHalAidl->requiresSecureDecoderComponent(mime);
61     return mCryptoHalHidl->requiresSecureDecoderComponent(mime);
62 }
63 
notifyResolution(uint32_t width,uint32_t height)64 void CryptoHal::notifyResolution(uint32_t width, uint32_t height) {
65     // This requires plugin to be created.
66     if (mCryptoHalAidl->initCheck() == OK) {
67         mCryptoHalAidl->notifyResolution(width, height);
68         return;
69     }
70 
71     mCryptoHalHidl->notifyResolution(width, height);
72 }
73 
setMediaDrmSession(const Vector<uint8_t> & sessionId)74 DrmStatus CryptoHal::setMediaDrmSession(const Vector<uint8_t>& sessionId) {
75     // This requires plugin to be created.
76     if (mCryptoHalAidl->initCheck() == OK) return mCryptoHalAidl->setMediaDrmSession(sessionId);
77     return mCryptoHalHidl->setMediaDrmSession(sessionId);
78 }
79 
decrypt(const uint8_t key[16],const uint8_t iv[16],CryptoPlugin::Mode mode,const CryptoPlugin::Pattern & pattern,const::SharedBuffer & source,size_t offset,const CryptoPlugin::SubSample * subSamples,size_t numSubSamples,const::DestinationBuffer & destination,AString * errorDetailMsg)80 ssize_t CryptoHal::decrypt(const uint8_t key[16], const uint8_t iv[16], CryptoPlugin::Mode mode,
81                            const CryptoPlugin::Pattern& pattern, const ::SharedBuffer& source,
82                            size_t offset, const CryptoPlugin::SubSample* subSamples,
83                            size_t numSubSamples, const ::DestinationBuffer& destination,
84                            AString* errorDetailMsg) {
85     // This requires plugin to be created.
86     if (mCryptoHalAidl->initCheck() == OK)
87         return mCryptoHalAidl->decrypt(key, iv, mode, pattern, source, offset, subSamples,
88                                        numSubSamples, destination, errorDetailMsg);
89     return mCryptoHalHidl->decrypt(key, iv, mode, pattern, source, offset, subSamples,
90                                    numSubSamples, destination, errorDetailMsg);
91 }
92 
setHeap(const sp<HidlMemory> & heap)93 int32_t CryptoHal::setHeap(const sp<HidlMemory>& heap) {
94     // This requires plugin to be created.
95     if (mCryptoHalAidl->initCheck() == OK) return mCryptoHalAidl->setHeap(heap);
96     return mCryptoHalHidl->setHeap(heap);
97 }
98 
unsetHeap(int32_t seqNum)99 void CryptoHal::unsetHeap(int32_t seqNum) {
100     // This requires plugin to be created.
101     if (mCryptoHalAidl->initCheck() == OK) {
102         mCryptoHalAidl->unsetHeap(seqNum);
103         return;
104     }
105 
106     mCryptoHalHidl->unsetHeap(seqNum);
107 }
108 
getLogMessages(Vector<drm::V1_4::LogMessage> & logs) const109 status_t CryptoHal::getLogMessages(Vector<drm::V1_4::LogMessage>& logs) const {
110     // This requires plugin to be created.
111     if (mCryptoHalAidl->initCheck() == OK) return mCryptoHalAidl->getLogMessages(logs);
112     return mCryptoHalHidl->getLogMessages(logs);
113 }
114 
115 }  // namespace android