1 /*
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
25 namespace aidl {
26 namespace android {
27 namespace hardware {
28 namespace cas {
29
CasImpl(const shared_ptr<ICasListener> & listener)30 CasImpl::CasImpl(const shared_ptr<ICasListener>& listener) : mListener(listener) {
31 ALOGV("CTOR");
32 }
33
~CasImpl()34 CasImpl::~CasImpl() {
35 ALOGV("DTOR");
36 release();
37 }
38
39 // static
OnEvent(void * appData,int32_t event,int32_t arg,uint8_t * data,size_t size)40 void 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
CallBackExt(void * appData,int32_t event,int32_t arg,uint8_t * data,size_t size,const CasSessionId * sessionId)50 void 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
StatusUpdate(void * appData,int32_t event,int32_t arg)61 void 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
init(CasPlugin * plugin)70 void CasImpl::init(CasPlugin* plugin) {
71 shared_ptr<CasPlugin> holder(plugin);
72 atomic_store(&mPluginHolder, holder);
73 }
74
onEvent(int32_t event,int32_t arg,uint8_t * data,size_t size)75 void 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
onEvent(const CasSessionId * sessionId,int32_t event,int32_t arg,uint8_t * data,size_t size)88 void 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
onStatusUpdate(int32_t event,int32_t arg)106 void 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
setPluginStatusUpdateCallback()113 ScopedAStatus 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
setPrivateData(const vector<uint8_t> & pvtData)122 ScopedAStatus 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
openSessionDefault(vector<uint8_t> * sessionId)131 ScopedAStatus CasImpl::openSessionDefault(vector<uint8_t>* sessionId) {
132 ALOGV("%s", __FUNCTION__);
133
134 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
135 status_t err = INVALID_OPERATION;
136 if (holder.get() != nullptr) {
137 err = holder->openSession(sessionId);
138 holder.reset();
139 }
140
141 return toStatus(err);
142 }
143
openSession(SessionIntent intent,ScramblingMode mode,vector<uint8_t> * sessionId)144 ScopedAStatus CasImpl::openSession(SessionIntent intent, ScramblingMode mode,
145 vector<uint8_t>* sessionId) {
146 ALOGV("%s", __FUNCTION__);
147
148 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
149 status_t err = INVALID_OPERATION;
150 if (holder.get() != nullptr) {
151 err = holder->openSession(static_cast<uint32_t>(intent), static_cast<uint32_t>(mode),
152 sessionId);
153 holder.reset();
154 }
155
156 return toStatus(err);
157 }
158
setSessionPrivateData(const vector<uint8_t> & sessionId,const vector<uint8_t> & pvtData)159 ScopedAStatus CasImpl::setSessionPrivateData(const vector<uint8_t>& sessionId,
160 const vector<uint8_t>& pvtData) {
161 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).c_str());
162 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
163 if (holder.get() == nullptr) {
164 return toStatus(INVALID_OPERATION);
165 }
166 return toStatus(holder->setSessionPrivateData(sessionId, pvtData));
167 }
168
closeSession(const vector<uint8_t> & sessionId)169 ScopedAStatus CasImpl::closeSession(const vector<uint8_t>& sessionId) {
170 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).c_str());
171 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
172 if (holder.get() == nullptr) {
173 return toStatus(INVALID_OPERATION);
174 }
175 return toStatus(holder->closeSession(sessionId));
176 }
177
processEcm(const vector<uint8_t> & sessionId,const vector<uint8_t> & ecm)178 ScopedAStatus CasImpl::processEcm(const vector<uint8_t>& sessionId, const vector<uint8_t>& ecm) {
179 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).c_str());
180 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
181 if (holder.get() == nullptr) {
182 return toStatus(INVALID_OPERATION);
183 }
184
185 return toStatus(holder->processEcm(sessionId, ecm));
186 }
187
processEmm(const vector<uint8_t> & emm)188 ScopedAStatus CasImpl::processEmm(const vector<uint8_t>& emm) {
189 ALOGV("%s", __FUNCTION__);
190 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
191 if (holder.get() == nullptr) {
192 return toStatus(INVALID_OPERATION);
193 }
194
195 return toStatus(holder->processEmm(emm));
196 }
197
sendEvent(int32_t event,int32_t arg,const vector<uint8_t> & eventData)198 ScopedAStatus CasImpl::sendEvent(int32_t event, int32_t arg, const vector<uint8_t>& eventData) {
199 ALOGV("%s", __FUNCTION__);
200 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
201 if (holder.get() == nullptr) {
202 return toStatus(INVALID_OPERATION);
203 }
204
205 status_t err = holder->sendEvent(event, arg, eventData);
206 return toStatus(err);
207 }
208
sendSessionEvent(const vector<uint8_t> & sessionId,int32_t event,int32_t arg,const vector<uint8_t> & eventData)209 ScopedAStatus CasImpl::sendSessionEvent(const vector<uint8_t>& sessionId, int32_t event,
210 int32_t arg, const vector<uint8_t>& eventData) {
211 ALOGV("%s", __FUNCTION__);
212 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
213 if (holder.get() == nullptr) {
214 return toStatus(INVALID_OPERATION);
215 }
216
217 status_t err = holder->sendSessionEvent(sessionId, event, arg, eventData);
218 return toStatus(err);
219 }
220
provision(const string & provisionString)221 ScopedAStatus CasImpl::provision(const string& provisionString) {
222 ALOGV("%s: provisionString=%s", __FUNCTION__, provisionString.c_str());
223 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
224 if (holder.get() == nullptr) {
225 return toStatus(INVALID_OPERATION);
226 }
227
228 return toStatus(holder->provision(String8(provisionString.c_str())));
229 }
230
refreshEntitlements(int32_t refreshType,const vector<uint8_t> & refreshData)231 ScopedAStatus CasImpl::refreshEntitlements(int32_t refreshType,
232 const vector<uint8_t>& refreshData) {
233 ALOGV("%s", __FUNCTION__);
234 shared_ptr<CasPlugin> holder = atomic_load(&mPluginHolder);
235 if (holder.get() == nullptr) {
236 return toStatus(INVALID_OPERATION);
237 }
238
239 status_t err = holder->refreshEntitlements(refreshType, refreshData);
240 return toStatus(err);
241 }
242
release()243 ScopedAStatus CasImpl::release() {
244 ALOGV("%s: plugin=%p", __FUNCTION__, mPluginHolder.get());
245
246 shared_ptr<CasPlugin> holder(nullptr);
247 atomic_store(&mPluginHolder, holder);
248
249 return ScopedAStatus::ok();
250 }
251
252 } // namespace cas
253 } // namespace hardware
254 } // namespace android
255 } // namespace aidl
256