1 /*
2  * Copyright (C) 2021 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 #pragma once
17 
18 #include <cstdint>
19 #include <string>
20 #include <vector>
21 
22 #include <json/json.h>
23 
24 #include <android/binder_auto_utils.h>
25 #include "aidl/android/hardware/drm/Status.h"
26 #include "ClearKeyTypes.h"
27 
28 namespace aidl {
29 namespace android {
30 namespace hardware {
31 namespace drm {
32 namespace clearkey {
33 
toMockStatus(::aidl::android::hardware::drm::Status status)34 inline ::aidl::android::hardware::drm::Status toMockStatus(
35         ::aidl::android::hardware::drm::Status status) {
36     switch (status) {
37         case ::aidl::android::hardware::drm::Status::ERROR_DRM_INSUFFICIENT_SECURITY:
38         case ::aidl::android::hardware::drm::Status::ERROR_DRM_FRAME_TOO_LARGE:
39         case ::aidl::android::hardware::drm::Status::ERROR_DRM_SESSION_LOST_STATE:
40             return ::aidl::android::hardware::drm::Status::ERROR_DRM_UNKNOWN;
41         default:
42             return status;
43     }
44 }
45 
46 inline ::ndk::ScopedAStatus toNdkScopedAStatus(::aidl::android::hardware::drm::Status status,
47                                                const char* msg = nullptr,
48                                                int32_t oemError = 0,
49                                                int32_t errorContext = 0) {
50 
51 
52     if (Status::OK == status) {
53         return ::ndk::ScopedAStatus::ok();
54     }
55 
56     Json::Value errObj(Json::objectValue);
57     auto err = static_cast<int32_t>(status);
58     errObj["cdmError"] = err;
59 
60     if (oemError) {
61         errObj["oemError"] = oemError;
62     }
63     if (errorContext) {
64         errObj["context"] = errorContext;
65     }
66     if (msg) {
67         errObj["errorMessage"] = msg;
68     }
69 
70     Json::FastWriter writer;
71     return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
72         err, writer.write(errObj).c_str());
73 }
74 
toNdkScopedAStatus(clearkeydrm::CdmResponseType res)75 inline ::ndk::ScopedAStatus toNdkScopedAStatus(clearkeydrm::CdmResponseType res) {
76     return toNdkScopedAStatus(static_cast<::aidl::android::hardware::drm::Status>(res));
77 }
78 
79 #define UNUSED(x) (void)(x);
80 
81 }  // namespace clearkey
82 }  // namespace drm
83 }  // namespace hardware
84 }  // namespace android
85 }  // namespace aidl
86