blob: de68a375a5a156eac4a563007a32282654abd78d [file] [log] [blame]
Robert Shih28c2ed32019-10-27 22:55:12 -07001/*
2 * Copyright (C) 2019 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#ifndef ANDROID_DRMUTILS_H
18#define ANDROID_DRMUTILS_H
19
Robert Shih10fe9432019-11-09 08:26:49 -080020#include <android/hardware/drm/1.0/ICryptoFactory.h>
Robert Shih5ff3ad62019-11-09 08:26:49 -080021#include <android/hardware/drm/1.0/IDrmFactory.h>
Robert Shih5944a0b2021-02-10 04:26:33 -080022#include <android/hardware/drm/1.4/types.h>
Robert Shih28c2ed32019-10-27 22:55:12 -070023#include <utils/Errors.h> // for status_t
24#include <utils/StrongPointer.h>
Robert Shih10fe9432019-11-09 08:26:49 -080025#include <vector>
26
27using namespace ::android::hardware::drm;
Robert Shih28c2ed32019-10-27 22:55:12 -070028
29namespace android {
30
31struct ICrypto;
32struct IDrm;
33
34namespace DrmUtils {
35
36bool UseDrmService();
37
38sp<IDrm> MakeDrm(status_t *pstatus = nullptr);
39
40sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
41
Robert Shihd3f9ba72019-11-20 17:25:26 -080042template<typename BA, typename PARCEL>
43void WriteByteArray(PARCEL &obj, const BA &vec) {
Robert Shih61e1c762019-10-31 21:26:58 -070044 obj.writeInt32(vec.size());
45 if (vec.size()) {
46 obj.write(vec.data(), vec.size());
47 }
48}
49
Robert Shihd3f9ba72019-11-20 17:25:26 -080050template<typename ET, typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -070051void WriteEventToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -080052 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -070053 ET eventType,
54 const BA &sessionId,
55 const BA &data) {
56 WriteByteArray(obj, sessionId);
57 WriteByteArray(obj, data);
58 obj.writeInt32(eventType);
59}
60
Robert Shihd3f9ba72019-11-20 17:25:26 -080061template<typename BA, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -070062void WriteExpirationUpdateToParcel(
Robert Shihd3f9ba72019-11-20 17:25:26 -080063 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -070064 const BA &sessionId,
65 int64_t expiryTimeInMS) {
66 WriteByteArray(obj, sessionId);
67 obj.writeInt64(expiryTimeInMS);
68}
69
Robert Shihd3f9ba72019-11-20 17:25:26 -080070template<typename BA, typename KSL, typename PARCEL>
Robert Shih61e1c762019-10-31 21:26:58 -070071void WriteKeysChange(
Robert Shihd3f9ba72019-11-20 17:25:26 -080072 PARCEL &obj,
Robert Shih61e1c762019-10-31 21:26:58 -070073 const BA &sessionId,
74 const KSL &keyStatusList,
75 bool hasNewUsableKey) {
76 WriteByteArray(obj, sessionId);
77 obj.writeInt32(keyStatusList.size());
78 for (const auto &keyStatus : keyStatusList) {
79 WriteByteArray(obj, keyStatus.keyId);
80 obj.writeInt32(keyStatus.type);
81 }
82 obj.writeInt32(hasNewUsableKey);
83}
84
Robert Shihc0d1d0e2019-11-24 13:21:04 -080085std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
Robert Shih5ff3ad62019-11-09 08:26:49 -080086
87std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
88 const char *appPackageName);
89
Robert Shih10fe9432019-11-09 08:26:49 -080090std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
91
92std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
93 const void *initData, size_t initDataSize);
94
Robert Shih5944a0b2021-02-10 04:26:33 -080095status_t toStatusT_1_4(::V1_4::Status status);
96
97template<typename S>
98inline status_t toStatusT(S status) {
99 auto err = static_cast<::V1_4::Status>(status);
100 return toStatusT_1_4(err);
101}
102
103template<typename T>
104inline status_t toStatusT(const android::hardware::Return<T> &status) {
105 auto t = static_cast<T>(status);
106 auto err = static_cast<::V1_4::Status>(t);
107 return toStatusT_1_4(err);
108}
109
Robert Shih28c2ed32019-10-27 22:55:12 -0700110} // namespace DrmUtils
Robert Shih28c2ed32019-10-27 22:55:12 -0700111} // namespace android
Robert Shih28c2ed32019-10-27 22:55:12 -0700112#endif // ANDROID_DRMUTILS_H