1 /*
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
20 #include <android/hardware/drm/1.0/ICryptoFactory.h>
21 #include <android/hardware/drm/1.0/IDrmFactory.h>
22 #include <android/hardware/drm/1.4/IDrmPlugin.h>
23 #include <android/hardware/drm/1.4/types.h>
24 #include <media/stagefright/MediaErrors.h>
25 #include <mediadrm/DrmMetricsLogger.h>
26 #include <mediadrm/DrmStatus.h>
27 #include <utils/Errors.h> // for status_t
28 #include <utils/Log.h>
29 #include <utils/String8.h>
30 #include <utils/StrongPointer.h>
31 #include <utils/Vector.h>
32 #include <algorithm>
33 #include <chrono>
34 #include <cstddef>
35 #include <cstdint>
36 #include <cstring>
37 #include <ctime>
38 #include <deque>
39 #include <endian.h>
40 #include <inttypes.h>
41 #include <iterator>
42 #include <mutex>
43 #include <string>
44 #include <vector>
45 #include <aidl/android/hardware/drm/LogMessage.h>
46 #include <aidl/android/hardware/drm/Status.h>
47 #include <aidl/android/hardware/drm/IDrmFactory.h>
48
49 using namespace ::android::hardware::drm;
50 using ::android::hardware::hidl_vec;
51 using ::android::hardware::Return;
52
53 using ::aidl::android::hardware::drm::LogPriority;
54 using ::aidl::android::hardware::drm::LogMessage;
55 using ::aidl::android::hardware::drm::Uuid;
56 using StatusAidl = ::aidl::android::hardware::drm::Status;
57 using IDrmFactoryAidl = ::aidl::android::hardware::drm::IDrmFactory;
58
59 namespace android {
60
61 struct ICrypto;
62 struct IDrm;
63
64 namespace DrmUtils {
65
66 // Log APIs
67 class LogBuffer {
68 public:
69 static const int MAX_CAPACITY = 100;
70 void addLog(const ::V1_4::LogMessage &log);
71 Vector<::V1_4::LogMessage> getLogs();
72
73 private:
74 std::deque<::V1_4::LogMessage> mBuffer;
75 std::mutex mMutex;
76 };
77
78 extern LogBuffer gLogBuf;
79
formatBuffer(char * buf,size_t size,const char * msg)80 static inline int formatBuffer(char *buf, size_t size, const char *msg) {
81 return snprintf(buf, size, "%s", msg);
82 }
83
84 template <typename First, typename... Args>
formatBuffer(char * buf,size_t size,const char * fmt,First first,Args...args)85 static inline int formatBuffer(char *buf, size_t size, const char *fmt, First first, Args... args) {
86 return snprintf(buf, size, fmt, first, args...);
87 }
88
89 template <typename... Args>
LogToBuffer(android_LogPriority level,const char * fmt,Args...args)90 void LogToBuffer(android_LogPriority level, const char *fmt, Args... args) {
91 const int LOG_BUF_SIZE = 256;
92 char buf[LOG_BUF_SIZE];
93 int len = formatBuffer(buf, LOG_BUF_SIZE, fmt, args...);
94 if (len <= 0) {
95 return;
96 }
97 __android_log_write(level, LOG_TAG, buf);
98 if (level >= ANDROID_LOG_INFO) {
99 int64_t epochTimeMs =
100 std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
101 gLogBuf.addLog({epochTimeMs, static_cast<::V1_4::LogPriority>(level), buf});
102 }
103 }
104
105 template <typename... Args>
LogToBuffer(android_LogPriority level,const uint8_t uuid[16],const char * fmt,Args...args)106 void LogToBuffer(android_LogPriority level, const uint8_t uuid[16], const char *fmt, Args... args) {
107 uint64_t uuid2[2] = {};
108 std::memcpy(uuid2, uuid, sizeof(uuid2));
109 std::string uuidFmt("uuid=[%" PRIx64 " %" PRIx64 "] ");
110 uuidFmt += fmt;
111 LogToBuffer(level, uuidFmt.c_str(), betoh64(uuid2[0]), betoh64(uuid2[1]), args...);
112 }
113
114 #ifndef LOG2BE
115 #define LOG2BE(...) LogToBuffer(ANDROID_LOG_ERROR, __VA_ARGS__)
116 #define LOG2BW(...) LogToBuffer(ANDROID_LOG_WARN, __VA_ARGS__)
117 #define LOG2BI(...) LogToBuffer(ANDROID_LOG_INFO, __VA_ARGS__)
118 #define LOG2BD(...) LogToBuffer(ANDROID_LOG_DEBUG, __VA_ARGS__)
119 #define LOG2BV(...) LogToBuffer(ANDROID_LOG_VERBOSE, __VA_ARGS__)
120 #endif
121
122 bool UseDrmService();
123
124 sp<IDrm> MakeDrm(IDrmFrontend frontend = IDRM_JNI, status_t* pstatus = nullptr);
125
126 sp<ICrypto> MakeCrypto(status_t *pstatus = nullptr);
127
128 template<typename BA, typename PARCEL>
WriteByteArray(PARCEL & obj,const BA & vec)129 void WriteByteArray(PARCEL &obj, const BA &vec) {
130 obj.writeInt32(vec.size());
131 if (vec.size()) {
132 obj.write(vec.data(), vec.size());
133 }
134 }
135
136 template<typename ET, typename BA, typename PARCEL>
WriteEventToParcel(PARCEL & obj,ET eventType,const BA & sessionId,const BA & data)137 void WriteEventToParcel(
138 PARCEL &obj,
139 ET eventType,
140 const BA &sessionId,
141 const BA &data) {
142 WriteByteArray(obj, sessionId);
143 WriteByteArray(obj, data);
144 obj.writeInt32(eventType);
145 }
146
147 template<typename BA, typename PARCEL>
WriteExpirationUpdateToParcel(PARCEL & obj,const BA & sessionId,int64_t expiryTimeInMS)148 void WriteExpirationUpdateToParcel(
149 PARCEL &obj,
150 const BA &sessionId,
151 int64_t expiryTimeInMS) {
152 WriteByteArray(obj, sessionId);
153 obj.writeInt64(expiryTimeInMS);
154 }
155
156 template<typename BA, typename KSL, typename PARCEL>
WriteKeysChange(PARCEL & obj,const BA & sessionId,const KSL & keyStatusList,bool hasNewUsableKey)157 void WriteKeysChange(
158 PARCEL &obj,
159 const BA &sessionId,
160 const KSL &keyStatusList,
161 bool hasNewUsableKey) {
162 WriteByteArray(obj, sessionId);
163 obj.writeInt32(keyStatusList.size());
164 for (const auto &keyStatus : keyStatusList) {
165 WriteByteArray(obj, keyStatus.keyId);
166 obj.writeInt32(keyStatus.type);
167 }
168 obj.writeInt32(hasNewUsableKey);
169 }
170
toAidlUuid(const uint8_t uuid[16])171 inline Uuid toAidlUuid(const uint8_t uuid[16]) {
172 Uuid uuidAidl;
173 for (int i = 0; i < 16; ++i) uuidAidl.uuid[i] = uuid[i];
174 return uuidAidl;
175 }
176
177 std::vector<std::shared_ptr<IDrmFactoryAidl>> makeDrmFactoriesAidl();
178
179 std::vector<sp<::V1_0::IDrmFactory>> MakeDrmFactories(const uint8_t uuid[16] = nullptr);
180
181 std::vector<sp<::V1_0::IDrmPlugin>> MakeDrmPlugins(const uint8_t uuid[16],
182 const char *appPackageName);
183
184 std::vector<sp<::V1_0::ICryptoFactory>> MakeCryptoFactories(const uint8_t uuid[16]);
185
186 std::vector<sp<::V1_0::ICryptoPlugin>> MakeCryptoPlugins(const uint8_t uuid[16],
187 const void *initData, size_t initDataSize);
188
189 status_t toStatusT_1_4(::V1_4::Status status);
190
191 template<typename S>
toStatusT(S status)192 inline status_t toStatusT(S status) {
193 auto err = static_cast<::V1_4::Status>(status);
194 return toStatusT_1_4(err);
195 }
196
197 template<typename T>
toStatusT(const android::hardware::Return<T> & status)198 inline status_t toStatusT(const android::hardware::Return<T> &status) {
199 auto t = static_cast<T>(status);
200 auto err = static_cast<::V1_4::Status>(t);
201 return toStatusT_1_4(err);
202 }
203
204 DrmStatus statusAidlToDrmStatus(::ndk::ScopedAStatus& statusAidl);
205
206 template<typename T, typename U>
GetLogMessagesAidl(const std::shared_ptr<U> & obj,Vector<::V1_4::LogMessage> & logs)207 status_t GetLogMessagesAidl(const std::shared_ptr<U> &obj, Vector<::V1_4::LogMessage> &logs) {
208 std::shared_ptr<T> plugin = obj;
209 if (obj == NULL) {
210 LOG2BW("%s obj is null", U::descriptor);
211 } else if (plugin == NULL) {
212 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
213 }
214
215 std::vector<LogMessage> pluginLogsAidl;
216 if (plugin != NULL) {
217 if(!plugin->getLogMessages(&pluginLogsAidl).isOk()) {
218 LOG2BW("%s::getLogMessages remote call failed", T::descriptor);
219 }
220 }
221
222 std::vector<::V1_4::LogMessage> pluginLogs;
223 for (LogMessage log : pluginLogsAidl) {
224 ::V1_4::LogMessage logHidl;
225 logHidl.timeMs = log.timeMs;
226 // skip negative convert check as count of enum elements is 7
227 logHidl.priority = static_cast<::V1_4::LogPriority>((int32_t)log.priority);
228 logHidl.message = log.message;
229 pluginLogs.push_back(logHidl);
230 }
231
232 auto allLogs(gLogBuf.getLogs());
233 LOG2BD("framework logs size %zu; plugin logs size %zu",
234 allLogs.size(), pluginLogs.size());
235 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
236 std::sort(allLogs.begin(), allLogs.end(),
237 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
238 return a.timeMs < b.timeMs;
239 });
240
241 logs.appendVector(allLogs);
242 return OK;
243 }
244
245 template<typename T, typename U>
GetLogMessages(const sp<U> & obj,Vector<::V1_4::LogMessage> & logs)246 status_t GetLogMessages(const sp<U> &obj, Vector<::V1_4::LogMessage> &logs) {
247 sp<T> plugin = T::castFrom(obj);
248 if (obj == NULL) {
249 LOG2BW("%s obj is null", U::descriptor);
250 } else if (plugin == NULL) {
251 LOG2BW("Cannot cast %s obj to %s plugin", U::descriptor, T::descriptor);
252 }
253
254 ::V1_4::Status err{};
255 std::vector<::V1_4::LogMessage> pluginLogs;
256 ::V1_4::IDrmPlugin::getLogMessages_cb cb = [&](
257 ::V1_4::Status status,
258 hidl_vec<::V1_4::LogMessage> hLogs) {
259 if (::V1_4::Status::OK != status) {
260 err = status;
261 return;
262 }
263 pluginLogs.assign(hLogs.data(), hLogs.data() + hLogs.size());
264 };
265
266 Return<void> hResult;
267 if (plugin != NULL) {
268 hResult = plugin->getLogMessages(cb);
269 }
270 if (!hResult.isOk()) {
271 LOG2BW("%s::getLogMessages remote call failed %s",
272 T::descriptor, hResult.description().c_str());
273 }
274
275 auto allLogs(gLogBuf.getLogs());
276 LOG2BD("framework logs size %zu; plugin logs size %zu",
277 allLogs.size(), pluginLogs.size());
278 std::copy(pluginLogs.begin(), pluginLogs.end(), std::back_inserter(allLogs));
279 std::sort(allLogs.begin(), allLogs.end(),
280 [](const ::V1_4::LogMessage &a, const ::V1_4::LogMessage &b) {
281 return a.timeMs < b.timeMs;
282 });
283
284 logs.appendVector(allLogs);
285 return toStatusT(err);
286 }
287
288 std::string GetExceptionMessage(const DrmStatus & err, const char *defaultMsg,
289 const Vector<::V1_4::LogMessage> &logs);
290
291 template<typename T>
GetExceptionMessage(const DrmStatus & err,const char * defaultMsg,const sp<T> & iface)292 std::string GetExceptionMessage(const DrmStatus &err, const char *defaultMsg, const sp<T> &iface) {
293 Vector<::V1_4::LogMessage> logs;
294 if (iface != NULL) {
295 iface->getLogMessages(logs);
296 }
297 return GetExceptionMessage(err, defaultMsg, logs);
298 }
299
300 } // namespace DrmUtils
301 } // namespace android
302 #endif // ANDROID_DRMUTILS_H
303