blob: 13cad672072a1fa4ea99887c5520b0d5261a9717 [file] [log] [blame]
Jeff Tinkerb075caa2016-12-06 23:15:20 -08001/*
2 * Copyright (C) 2016 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 */
Jeff Tinker972a3e32017-01-23 14:02:50 -080016#define LOG_TAG "android.hardware.drm@1.0-impl"
Jeff Tinkerb075caa2016-12-06 23:15:20 -080017
Jeff Tinkerfbf36502017-01-24 06:58:26 -080018#include <utils/Log.h>
19
Jeff Tinkerb075caa2016-12-06 23:15:20 -080020#include "CryptoFactory.h"
21#include "CryptoPlugin.h"
22#include "TypeConvert.h"
Jeff Tinkerb075caa2016-12-06 23:15:20 -080023
24namespace android {
25namespace hardware {
26namespace drm {
Jeff Tinkerb075caa2016-12-06 23:15:20 -080027namespace V1_0 {
28namespace implementation {
29
Jeff Tinkerfbf36502017-01-24 06:58:26 -080030CryptoFactory::CryptoFactory() :
31 trebleLoader("/vendor/lib/hw", "createCryptoFactory"),
32 legacyLoader("/vendor/lib/mediadrm", "createCryptoFactory") {
33}
Jeff Tinkerb075caa2016-12-06 23:15:20 -080034
Jeff Tinkerfbf36502017-01-24 06:58:26 -080035// Methods from ::android::hardware::drm::V1_0::ICryptoFactory follow.
36Return<bool> CryptoFactory::isCryptoSchemeSupported(
37 const hidl_array<uint8_t, 16>& uuid) {
38 return isCryptoSchemeSupported(trebleLoader, uuid) ||
39 isCryptoSchemeSupported(legacyLoader, uuid);
40}
Jeff Tinkerb075caa2016-12-06 23:15:20 -080041
Jeff Tinkerfbf36502017-01-24 06:58:26 -080042Return<void> CryptoFactory::createPlugin(const hidl_array<uint8_t, 16>& uuid,
43 const hidl_vec<uint8_t>& initData, createPlugin_cb _hidl_cb) {
44 sp<ICryptoPlugin> plugin = createTreblePlugin(uuid, initData);
45 if (plugin == nullptr) {
46 plugin = createLegacyPlugin(uuid, initData);
47 }
48 _hidl_cb(plugin != nullptr ? Status::OK : Status::ERROR_DRM_CANNOT_HANDLE, plugin);
49 return Void();
50}
51
52sp<ICryptoPlugin> CryptoFactory::createTreblePlugin(const hidl_array<uint8_t, 16>& uuid,
53 const hidl_vec<uint8_t>& initData) {
54 sp<ICryptoPlugin> plugin;
55 for (size_t i = 0; i < trebleLoader.factoryCount(); i++) {
56 Return<void> hResult = trebleLoader.getFactory(i)->createPlugin(uuid, initData,
57 [&](Status status, const sp<ICryptoPlugin>& hPlugin) {
58 if (status == Status::OK) {
59 plugin = hPlugin;
60 }
Jeff Tinkerb075caa2016-12-06 23:15:20 -080061 }
Jeff Tinkerfbf36502017-01-24 06:58:26 -080062 );
63 if (plugin != nullptr) {
64 return plugin;
Jeff Tinkerb075caa2016-12-06 23:15:20 -080065 }
Jeff Tinkerb075caa2016-12-06 23:15:20 -080066 }
Jeff Tinkerfbf36502017-01-24 06:58:26 -080067 return nullptr;
68}
Jeff Tinkerb075caa2016-12-06 23:15:20 -080069
Jeff Tinkerfbf36502017-01-24 06:58:26 -080070sp<ICryptoPlugin> CryptoFactory::createLegacyPlugin(const hidl_array<uint8_t, 16>& uuid,
71 const hidl_vec<uint8_t>& initData) {
72 android::CryptoPlugin *legacyPlugin = nullptr;
73 for (size_t i = 0; i < legacyLoader.factoryCount(); i++) {
74 legacyLoader.getFactory(i)->createPlugin(uuid.data(),
75 initData.data(), initData.size(), &legacyPlugin);
76 if (legacyPlugin) {
77 return new CryptoPlugin(legacyPlugin);
78 }
Jeff Tinkerb075caa2016-12-06 23:15:20 -080079 }
Jeff Tinkerfbf36502017-01-24 06:58:26 -080080 return nullptr;
81}
82
83
84ICryptoFactory* HIDL_FETCH_ICryptoFactory(const char* /* name */) {
85 return new CryptoFactory();
86}
Jeff Tinkerb075caa2016-12-06 23:15:20 -080087
88} // namespace implementation
89} // namespace V1_0
Jeff Tinkerb075caa2016-12-06 23:15:20 -080090} // namespace drm
91} // namespace hardware
92} // namespace android