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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "android.hardware.cas@1.1-CasImpl"
19
20 #include <android/hardware/cas/1.1/ICasListener.h>
21 #include <media/cas/CasAPI.h>
22 #include <utils/Log.h>
23
24 #include "CasImpl.h"
25 #include "SharedLibrary.h"
26 #include "TypeConvert.h"
27
28 namespace android {
29 namespace hardware {
30 namespace cas {
31 namespace V1_1 {
32 namespace implementation {
33
CasImpl(const sp<ICasListener> & listener)34 CasImpl::CasImpl(const sp<ICasListener>& listener) : mListener(listener) {
35 ALOGV("CTOR");
36 }
37
~CasImpl()38 CasImpl::~CasImpl() {
39 ALOGV("DTOR");
40 release();
41 }
42
43 // static
OnEvent(void * appData,int32_t event,int32_t arg,uint8_t * data,size_t size)44 void CasImpl::OnEvent(void* appData, int32_t event, int32_t arg, uint8_t* data, size_t size) {
45 if (appData == NULL) {
46 ALOGE("Invalid appData!");
47 return;
48 }
49 CasImpl* casImpl = static_cast<CasImpl*>(appData);
50 casImpl->onEvent(event, arg, data, size);
51 }
52
53 // static
CallBackExt(void * appData,int32_t event,int32_t arg,uint8_t * data,size_t size,const CasSessionId * sessionId)54 void CasImpl::CallBackExt(void* appData, int32_t event, int32_t arg, uint8_t* data, size_t size,
55 const CasSessionId* sessionId) {
56 if (appData == NULL) {
57 ALOGE("Invalid appData!");
58 return;
59 }
60 CasImpl* casImpl = static_cast<CasImpl*>(appData);
61 casImpl->onEvent(sessionId, event, arg, data, size);
62 }
63
init(const sp<SharedLibrary> & library,CasPlugin * plugin)64 void CasImpl::init(const sp<SharedLibrary>& library, CasPlugin* plugin) {
65 mLibrary = library;
66 std::shared_ptr<CasPlugin> holder(plugin);
67 std::atomic_store(&mPluginHolder, holder);
68 }
69
onEvent(int32_t event,int32_t arg,uint8_t * data,size_t size)70 void CasImpl::onEvent(int32_t event, int32_t arg, uint8_t* data, size_t size) {
71 if (mListener == NULL) {
72 return;
73 }
74
75 HidlCasData eventData;
76 if (data != NULL) {
77 eventData.setToExternal(data, size);
78 }
79
80 mListener->onEvent(event, arg, eventData);
81 }
82
onEvent(const CasSessionId * sessionId,int32_t event,int32_t arg,uint8_t * data,size_t size)83 void CasImpl::onEvent(const CasSessionId* sessionId, int32_t event, int32_t arg, uint8_t* data,
84 size_t size) {
85 if (mListener == NULL) {
86 return;
87 }
88
89 HidlCasData eventData;
90 if (data != NULL) {
91 eventData.setToExternal(data, size);
92 }
93
94 if (sessionId != NULL) {
95 mListener->onSessionEvent(*sessionId, event, arg, eventData);
96 } else {
97 mListener->onEvent(event, arg, eventData);
98 }
99 }
100
setPrivateData(const HidlCasData & pvtData)101 Return<Status> CasImpl::setPrivateData(const HidlCasData& pvtData) {
102 ALOGV("%s", __FUNCTION__);
103 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
104 if (holder.get() == nullptr) {
105 return toStatus(INVALID_OPERATION);
106 }
107 return toStatus(holder->setPrivateData(pvtData));
108 }
109
openSession(openSession_cb _hidl_cb)110 Return<void> CasImpl::openSession(openSession_cb _hidl_cb) {
111 ALOGV("%s", __FUNCTION__);
112 CasSessionId sessionId;
113
114 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
115 status_t err = INVALID_OPERATION;
116 if (holder.get() != nullptr) {
117 err = holder->openSession(&sessionId);
118 holder.reset();
119 }
120
121 _hidl_cb(toStatus(err), sessionId);
122
123 return Void();
124 }
125
setSessionPrivateData(const HidlCasSessionId & sessionId,const HidlCasData & pvtData)126 Return<Status> CasImpl::setSessionPrivateData(const HidlCasSessionId& sessionId,
127 const HidlCasData& pvtData) {
128 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).string());
129 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
130 if (holder.get() == nullptr) {
131 return toStatus(INVALID_OPERATION);
132 }
133 return toStatus(holder->setSessionPrivateData(sessionId, pvtData));
134 }
135
closeSession(const HidlCasSessionId & sessionId)136 Return<Status> CasImpl::closeSession(const HidlCasSessionId& sessionId) {
137 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).string());
138 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
139 if (holder.get() == nullptr) {
140 return toStatus(INVALID_OPERATION);
141 }
142 return toStatus(holder->closeSession(sessionId));
143 }
144
processEcm(const HidlCasSessionId & sessionId,const HidlCasData & ecm)145 Return<Status> CasImpl::processEcm(const HidlCasSessionId& sessionId, const HidlCasData& ecm) {
146 ALOGV("%s: sessionId=%s", __FUNCTION__, sessionIdToString(sessionId).string());
147 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
148 if (holder.get() == nullptr) {
149 return toStatus(INVALID_OPERATION);
150 }
151
152 return toStatus(holder->processEcm(sessionId, ecm));
153 }
154
processEmm(const HidlCasData & emm)155 Return<Status> CasImpl::processEmm(const HidlCasData& emm) {
156 ALOGV("%s", __FUNCTION__);
157 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
158 if (holder.get() == nullptr) {
159 return toStatus(INVALID_OPERATION);
160 }
161
162 return toStatus(holder->processEmm(emm));
163 }
164
sendEvent(int32_t event,int32_t arg,const HidlCasData & eventData)165 Return<Status> CasImpl::sendEvent(int32_t event, int32_t arg, const HidlCasData& eventData) {
166 ALOGV("%s", __FUNCTION__);
167 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
168 if (holder.get() == nullptr) {
169 return toStatus(INVALID_OPERATION);
170 }
171
172 status_t err = holder->sendEvent(event, arg, eventData);
173 return toStatus(err);
174 }
175
sendSessionEvent(const HidlCasSessionId & sessionId,int32_t event,int32_t arg,const HidlCasData & eventData)176 Return<Status> CasImpl::sendSessionEvent(const HidlCasSessionId& sessionId, int32_t event,
177 int32_t arg, const HidlCasData& eventData) {
178 ALOGV("%s", __FUNCTION__);
179 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
180 if (holder.get() == nullptr) {
181 return toStatus(INVALID_OPERATION);
182 }
183
184 status_t err = holder->sendSessionEvent(sessionId, event, arg, eventData);
185 return toStatus(err);
186 }
187
provision(const hidl_string & provisionString)188 Return<Status> CasImpl::provision(const hidl_string& provisionString) {
189 ALOGV("%s: provisionString=%s", __FUNCTION__, provisionString.c_str());
190 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
191 if (holder.get() == nullptr) {
192 return toStatus(INVALID_OPERATION);
193 }
194
195 return toStatus(holder->provision(String8(provisionString.c_str())));
196 }
197
refreshEntitlements(int32_t refreshType,const HidlCasData & refreshData)198 Return<Status> CasImpl::refreshEntitlements(int32_t refreshType, const HidlCasData& refreshData) {
199 ALOGV("%s", __FUNCTION__);
200 std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
201 if (holder.get() == nullptr) {
202 return toStatus(INVALID_OPERATION);
203 }
204
205 status_t err = holder->refreshEntitlements(refreshType, refreshData);
206 return toStatus(err);
207 }
208
release()209 Return<Status> CasImpl::release() {
210 ALOGV("%s: plugin=%p", __FUNCTION__, mPluginHolder.get());
211
212 std::shared_ptr<CasPlugin> holder(nullptr);
213 std::atomic_store(&mPluginHolder, holder);
214
215 return Status::OK;
216 }
217
218 } // namespace implementation
219 } // namespace V1_1
220 } // namespace cas
221 } // namespace hardware
222 } // namespace android
223