Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2017 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "CasImpl" |
| 19 | |
| 20 | #include <android/media/ICasListener.h> |
| 21 | #include <media/cas/CasAPI.h> |
| 22 | #include <media/CasImpl.h> |
| 23 | #include <media/SharedLibrary.h> |
| 24 | #include <utils/Log.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | static Status getBinderStatus(status_t err) { |
| 29 | if (err == OK) { |
| 30 | return Status::ok(); |
| 31 | } |
| 32 | if (err == BAD_VALUE) { |
| 33 | return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT); |
| 34 | } |
| 35 | if (err == INVALID_OPERATION) { |
| 36 | return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); |
| 37 | } |
| 38 | return Status::fromServiceSpecificError(err); |
| 39 | } |
| 40 | |
| 41 | static String8 sessionIdToString(const CasSessionId &sessionId) { |
| 42 | String8 result; |
| 43 | for (size_t i = 0; i < sessionId.size(); i++) { |
| 44 | result.appendFormat("%02x ", sessionId[i]); |
| 45 | } |
| 46 | if (result.isEmpty()) { |
| 47 | result.append("(null)"); |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | CasImpl::CasImpl(const sp<ICasListener> &listener) |
| 53 | : mPlugin(NULL), mListener(listener) { |
| 54 | ALOGV("CTOR: mPlugin=%p", mPlugin); |
| 55 | } |
| 56 | |
| 57 | CasImpl::~CasImpl() { |
| 58 | ALOGV("DTOR: mPlugin=%p", mPlugin); |
| 59 | release(); |
| 60 | } |
| 61 | |
| 62 | //static |
| 63 | void CasImpl::OnEvent( |
| 64 | void *appData, |
| 65 | int32_t event, |
| 66 | int32_t arg, |
| 67 | uint8_t *data, |
| 68 | size_t size) { |
| 69 | if (appData == NULL) { |
| 70 | ALOGE("Invalid appData!"); |
| 71 | return; |
| 72 | } |
| 73 | CasImpl *casImpl = static_cast<CasImpl *>(appData); |
| 74 | casImpl->onEvent(event, arg, data, size); |
| 75 | } |
| 76 | |
| 77 | void CasImpl::init(const sp<SharedLibrary>& library, CasPlugin *plugin) { |
| 78 | mLibrary = library; |
| 79 | mPlugin = plugin; |
| 80 | } |
| 81 | |
| 82 | void CasImpl::onEvent( |
| 83 | int32_t event, int32_t arg, uint8_t *data, size_t size) { |
| 84 | if (mListener == NULL) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<CasData> eventData; |
| 89 | if (data != NULL && size > 0) { |
| 90 | eventData.reset(new CasData(data, data + size)); |
| 91 | } |
| 92 | |
| 93 | mListener->onEvent(event, arg, eventData); |
| 94 | } |
| 95 | |
| 96 | Status CasImpl::setPrivateData(const CasData& pvtData) { |
| 97 | ALOGV("setPrivateData"); |
| 98 | return getBinderStatus(mPlugin->setPrivateData(pvtData)); |
| 99 | } |
| 100 | |
| 101 | Status CasImpl::openSession(int32_t program_number, CasSessionId* sessionId) { |
| 102 | ALOGV("openSession: program_number=%d", program_number); |
| 103 | |
| 104 | status_t err = mPlugin->openSession(program_number, sessionId); |
| 105 | |
| 106 | ALOGV("openSession: session opened for program_number=%d, sessionId=%s", |
| 107 | program_number, sessionIdToString(*sessionId).string()); |
| 108 | |
| 109 | return getBinderStatus(err); |
| 110 | } |
| 111 | |
| 112 | Status CasImpl::openSessionForStream( |
| 113 | int32_t program_number, |
| 114 | int32_t elementary_PID, |
| 115 | CasSessionId* sessionId) { |
| 116 | ALOGV("openSession: program_number=%d, elementary_PID=%d", |
| 117 | program_number, elementary_PID); |
| 118 | |
| 119 | status_t err = mPlugin->openSession( |
| 120 | program_number, elementary_PID, sessionId); |
| 121 | |
| 122 | ALOGV("openSession: session opened for " |
| 123 | "program_number=%d, elementary_PID=%d, sessionId=%s", |
| 124 | program_number, elementary_PID, |
| 125 | sessionIdToString(*sessionId).string()); |
| 126 | |
| 127 | return getBinderStatus(err); |
| 128 | } |
| 129 | |
| 130 | Status CasImpl::setSessionPrivateData( |
| 131 | const CasSessionId &sessionId, const CasData& pvtData) { |
| 132 | ALOGV("setSessionPrivateData: sessionId=%s", |
| 133 | sessionIdToString(sessionId).string()); |
| 134 | |
| 135 | return getBinderStatus(mPlugin->setSessionPrivateData(sessionId, pvtData)); |
| 136 | } |
| 137 | |
| 138 | Status CasImpl::closeSession(const CasSessionId &sessionId) { |
| 139 | ALOGV("closeSession: sessionId=%s", |
| 140 | sessionIdToString(sessionId).string()); |
| 141 | |
| 142 | return getBinderStatus(mPlugin->closeSession(sessionId)); |
| 143 | } |
| 144 | |
| 145 | Status CasImpl::processEcm(const CasSessionId &sessionId, const ParcelableCasData& ecm) { |
| 146 | ALOGV("processEcm: sessionId=%s", |
| 147 | sessionIdToString(sessionId).string()); |
| 148 | |
| 149 | return getBinderStatus(mPlugin->processEcm(sessionId, ecm)); |
| 150 | } |
| 151 | |
| 152 | Status CasImpl::processEmm(const ParcelableCasData& emm) { |
| 153 | ALOGV("processEmm"); |
| 154 | |
| 155 | return getBinderStatus(mPlugin->processEmm(emm)); |
| 156 | } |
| 157 | |
| 158 | Status CasImpl::sendEvent( |
| 159 | int32_t event, int32_t arg, const ::std::unique_ptr<CasData> &eventData) { |
| 160 | ALOGV("sendEvent"); |
| 161 | |
| 162 | status_t err; |
| 163 | if (eventData == nullptr) { |
| 164 | err = mPlugin->sendEvent(event, arg, CasData()); |
| 165 | } else { |
| 166 | err = mPlugin->sendEvent(event, arg, *eventData); |
| 167 | } |
| 168 | return getBinderStatus(err); |
| 169 | } |
| 170 | |
| 171 | Status CasImpl::provision(const String16& provisionString) { |
| 172 | ALOGV("provision: provisionString=%s", String8(provisionString).string()); |
| 173 | |
| 174 | return getBinderStatus(mPlugin->provision(String8(provisionString))); |
| 175 | } |
| 176 | |
| 177 | Status CasImpl::refreshEntitlements( |
| 178 | int32_t refreshType, const ::std::unique_ptr<CasData> &refreshData) { |
| 179 | ALOGV("refreshEntitlements"); |
| 180 | |
| 181 | status_t err; |
| 182 | if (refreshData == nullptr) { |
| 183 | err = mPlugin->refreshEntitlements(refreshType, CasData()); |
| 184 | } else { |
| 185 | err = mPlugin->refreshEntitlements(refreshType, *refreshData); |
| 186 | } |
| 187 | return getBinderStatus(err); |
| 188 | } |
| 189 | |
| 190 | Status CasImpl::release() { |
| 191 | ALOGV("release: mPlugin=%p", mPlugin); |
| 192 | |
| 193 | if (mPlugin != NULL) { |
| 194 | delete mPlugin; |
| 195 | mPlugin = NULL; |
| 196 | } |
| 197 | return Status::ok(); |
| 198 | } |
| 199 | |
| 200 | } // namespace android |
| 201 | |