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