blob: 2d31b352bb9e293bb27c775f3f523f420efbed1c [file] [log] [blame]
Shraddha Basantwani6545b4e2022-09-21 16:26:19 +05301/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 icensed 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#define LOG_TAG "android.hardware.cas-CasImpl"
18
19#include <media/cas/CasAPI.h>
20#include <utils/Log.h>
21
22#include "CasImpl.h"
23#include "TypeConvert.h"
24
25namespace aidl {
26namespace android {
27namespace hardware {
28namespace cas {
29
30CasImpl::CasImpl(const shared_ptr<ICasListener>& listener) : mListener(listener) {
31 ALOGV("CTOR");
32}
33
34CasImpl::~CasImpl() {
35 ALOGV("DTOR");
36 release();
37}
38
39// static
40void CasImpl::OnEvent(void* appData, int32_t event, int32_t arg, uint8_t* data, size_t size) {
41 if (appData == NULL) {
42 ALOGE("Invalid appData!");
43 return;
44 }
45 CasImpl* casImpl = static_cast<CasImpl*>(appData);
46 casImpl->onEvent(event, arg, data, size);
47}
48
49// static
50void CasImpl::CallBackExt(void* appData, int32_t event, int32_t arg, uint8_t* data, size_t size,
51 const CasSessionId* sessionId) {
52 if (appData == NULL) {
53 ALOGE("Invalid appData!");
54 return;
55 }
56 CasImpl* casImpl = static_cast<CasImpl*>(appData);
57 casImpl->onEvent(sessionId, event, arg, data, size);
58}
59
60// static
61void CasImpl::StatusUpdate(void* appData, int32_t event, int32_t arg) {
62 if (appData == NULL) {
63 ALOGE("Invalid appData!");
64 return;
65 }
66 CasImpl* casImpl = static_cast<CasImpl*>(appData);
67 casImpl->onStatusUpdate(event, arg);
68}
69
70void CasImpl::init(CasPlugin* plugin) {
71 shared_ptr<CasPlugin> holder(plugin);
72 atomic_store(&mPluginHolder, holder);
73}
74
75void CasImpl::onEvent(int32_t event, int32_t arg, uint8_t* data, size_t size) {
76 if (mListener == NULL) {
77 return;
78 }
79
80 vector<uint8_t> eventData;
81 if (data != NULL) {
82 eventData.assign(data, data + size);
83 }
84
85 mListener->onEvent(event, arg, eventData);
86}
87
88void CasImpl::onEvent(const CasSessionId* sessionId, int32_t event, int32_t arg, uint8_t* data,
89 size_t size) {
90 if (mListener == NULL) {
91 return;
92 }
93
94 vector<uint8_t> eventData;
95 if (data != NULL) {
96 eventData.assign(data, data + size);
97 }
98
99 if (sessionId != NULL) {
100 mListener->onSessionEvent(*sessionId, event, arg, eventData);
101 } else {
102 mListener->onEvent(event, arg, eventData);
103 }
104}
105
106void CasImpl::onStatusUpdate(int32_t event, int32_t arg) {
107 if (mListener == NULL) {
108 return;
109 }
110 mListener->onStatusUpdate(static_cast<StatusEvent>(event), arg);
111}
112
113ScopedAStatus CasImpl::setPluginStatusUpdateCallback() {
114 ALOGV("%s", __FUNCTION__);
115 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
116 if (holder.get() == nullptr) {
117 return toStatus(INVALID_OPERATION);
118 }
119 return toStatus(holder->setStatusCallback(&CasImpl::StatusUpdate));
120}
121
122ScopedAStatus CasImpl::setPrivateData(const vector<uint8_t>& pvtData) {
123 ALOGV("%s", __FUNCTION__);
124 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
125 if (holder.get() == nullptr) {
126 return toStatus(INVALID_OPERATION);
127 }
128 return toStatus(holder->setPrivateData(pvtData));
129}
130
131ScopedAStatus CasImpl::openSession(SessionIntent intent, ScramblingMode mode,
132 vector<uint8_t>* sessionId) {
133 ALOGV("%s", __FUNCTION__);
134
135 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
136 status_t err = INVALID_OPERATION;
137 if (holder.get() != nullptr) {
138 err = holder->openSession(static_cast<uint32_t>(intent), static_cast<uint32_t>(mode),
139 sessionId);
140 holder.reset();
141 }
142
143 return toStatus(err);
144}
145
146ScopedAStatus CasImpl::setSessionPrivateData(const vector<uint8_t>& sessionId,
147 const vector<uint8_t>& pvtData) {
148 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).string());
149 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
150 if (holder.get() == nullptr) {
151 return toStatus(INVALID_OPERATION);
152 }
153 return toStatus(holder->setSessionPrivateData(sessionId, pvtData));
154}
155
156ScopedAStatus CasImpl::closeSession(const vector<uint8_t>& sessionId) {
157 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).string());
158 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
159 if (holder.get() == nullptr) {
160 return toStatus(INVALID_OPERATION);
161 }
162 return toStatus(holder->closeSession(sessionId));
163}
164
165ScopedAStatus CasImpl::processEcm(const vector<uint8_t>& sessionId, const vector<uint8_t>& ecm) {
166 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).string());
167 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
168 if (holder.get() == nullptr) {
169 return toStatus(INVALID_OPERATION);
170 }
171
172 return toStatus(holder->processEcm(sessionId, ecm));
173}
174
175ScopedAStatus CasImpl::processEmm(const vector<uint8_t>& emm) {
176 ALOGV("%s", __FUNCTION__);
177 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
178 if (holder.get() == nullptr) {
179 return toStatus(INVALID_OPERATION);
180 }
181
182 return toStatus(holder->processEmm(emm));
183}
184
185ScopedAStatus CasImpl::sendEvent(int32_t event, int32_t arg, const vector<uint8_t>& eventData) {
186 ALOGV("%s", __FUNCTION__);
187 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
188 if (holder.get() == nullptr) {
189 return toStatus(INVALID_OPERATION);
190 }
191
192 status_t err = holder->sendEvent(event, arg, eventData);
193 return toStatus(err);
194}
195
196ScopedAStatus CasImpl::sendSessionEvent(const vector<uint8_t>& sessionId, int32_t event,
197 int32_t arg, const vector<uint8_t>& eventData) {
198 ALOGV("%s", __FUNCTION__);
199 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
200 if (holder.get() == nullptr) {
201 return toStatus(INVALID_OPERATION);
202 }
203
204 status_t err = holder->sendSessionEvent(sessionId, event, arg, eventData);
205 return toStatus(err);
206}
207
208ScopedAStatus CasImpl::provision(const string& provisionString) {
209 ALOGV("%s: provisionString=%s", __FUNCTION__, provisionString.c_str());
210 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
211 if (holder.get() == nullptr) {
212 return toStatus(INVALID_OPERATION);
213 }
214
215 return toStatus(holder->provision(String8(provisionString.c_str())));
216}
217
218ScopedAStatus CasImpl::refreshEntitlements(int32_t refreshType,
219 const vector<uint8_t>& refreshData) {
220 ALOGV("%s", __FUNCTION__);
221 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
222 if (holder.get() == nullptr) {
223 return toStatus(INVALID_OPERATION);
224 }
225
226 status_t err = holder->refreshEntitlements(refreshType, refreshData);
227 return toStatus(err);
228}
229
230ScopedAStatus CasImpl::release() {
231 ALOGV("%s: plugin=%p", __FUNCTION__, mPluginHolder.get());
232
233 shared_ptr<CasPlugin> holder(nullptr);
234 atomic_store(&mPluginHolder, holder);
235
236 return ScopedAStatus::ok();
237}
238
239} // namespace cas
240} // namespace hardware
241} // namespace android
242} // namespace aidl