blob: 37471ba7fbf9067b10f4efe772d59d862ee74142 [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 */
16
Jeff Tinkerfbf36502017-01-24 06:58:26 -080017#include <utils/Log.h>
18
Jeff Tinkerb075caa2016-12-06 23:15:20 -080019#include "CryptoFactory.h"
20#include "CryptoPlugin.h"
21#include "TypeConvert.h"
Jeff Tinkerb075caa2016-12-06 23:15:20 -080022
23namespace android {
24namespace hardware {
25namespace drm {
Jeff Tinkerb075caa2016-12-06 23:15:20 -080026namespace V1_0 {
27namespace implementation {
28
Jeff Tinkerfbf36502017-01-24 06:58:26 -080029CryptoFactory::CryptoFactory() :
30 trebleLoader("/vendor/lib/hw", "createCryptoFactory"),
31 legacyLoader("/vendor/lib/mediadrm", "createCryptoFactory") {
32}
Jeff Tinkerb075caa2016-12-06 23:15:20 -080033
Jeff Tinkerfbf36502017-01-24 06:58:26 -080034// Methods from ::android::hardware::drm::V1_0::ICryptoFactory follow.
35Return<bool> CryptoFactory::isCryptoSchemeSupported(
36 const hidl_array<uint8_t, 16>& uuid) {
37 return isCryptoSchemeSupported(trebleLoader, uuid) ||
38 isCryptoSchemeSupported(legacyLoader, uuid);
39}
Jeff Tinkerb075caa2016-12-06 23:15:20 -080040
Jeff Tinkerfbf36502017-01-24 06:58:26 -080041Return<void> CryptoFactory::createPlugin(const hidl_array<uint8_t, 16>& uuid,
42 const hidl_vec<uint8_t>& initData, createPlugin_cb _hidl_cb) {
43 sp<ICryptoPlugin> plugin = createTreblePlugin(uuid, initData);
44 if (plugin == nullptr) {
45 plugin = createLegacyPlugin(uuid, initData);
46 }
47 _hidl_cb(plugin != nullptr ? Status::OK : Status::ERROR_DRM_CANNOT_HANDLE, plugin);
48 return Void();
49}
50
51sp<ICryptoPlugin> CryptoFactory::createTreblePlugin(const hidl_array<uint8_t, 16>& uuid,
52 const hidl_vec<uint8_t>& initData) {
53 sp<ICryptoPlugin> plugin;
54 for (size_t i = 0; i < trebleLoader.factoryCount(); i++) {
55 Return<void> hResult = trebleLoader.getFactory(i)->createPlugin(uuid, initData,
56 [&](Status status, const sp<ICryptoPlugin>& hPlugin) {
57 if (status == Status::OK) {
58 plugin = hPlugin;
59 }
Jeff Tinkerb075caa2016-12-06 23:15:20 -080060 }
Jeff Tinkerfbf36502017-01-24 06:58:26 -080061 );
62 if (plugin != nullptr) {
63 return plugin;
Jeff Tinkerb075caa2016-12-06 23:15:20 -080064 }
Jeff Tinkerb075caa2016-12-06 23:15:20 -080065 }
Jeff Tinkerfbf36502017-01-24 06:58:26 -080066 return nullptr;
67}
Jeff Tinkerb075caa2016-12-06 23:15:20 -080068
Jeff Tinkerfbf36502017-01-24 06:58:26 -080069sp<ICryptoPlugin> CryptoFactory::createLegacyPlugin(const hidl_array<uint8_t, 16>& uuid,
70 const hidl_vec<uint8_t>& initData) {
71 android::CryptoPlugin *legacyPlugin = nullptr;
72 for (size_t i = 0; i < legacyLoader.factoryCount(); i++) {
73 legacyLoader.getFactory(i)->createPlugin(uuid.data(),
74 initData.data(), initData.size(), &legacyPlugin);
75 if (legacyPlugin) {
76 return new CryptoPlugin(legacyPlugin);
77 }
Jeff Tinkerb075caa2016-12-06 23:15:20 -080078 }
Jeff Tinkerfbf36502017-01-24 06:58:26 -080079 return nullptr;
80}
81
82
83ICryptoFactory* HIDL_FETCH_ICryptoFactory(const char* /* name */) {
84 return new CryptoFactory();
85}
Jeff Tinkerb075caa2016-12-06 23:15:20 -080086
87} // namespace implementation
88} // namespace V1_0
Jeff Tinkerb075caa2016-12-06 23:15:20 -080089} // namespace drm
90} // namespace hardware
91} // namespace android