blob: 2ac1c4f6ddfd733d368e13a7d26a203fcfa0b0e9 [file] [log] [blame]
Chong Zhanga4f67512017-04-24 17:18:25 -07001/*
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
28namespace android {
29namespace hardware {
30namespace cas {
31namespace V1_0 {
32namespace implementation {
33
34struct CasImpl::PluginHolder : public RefBase {
35public:
36 explicit PluginHolder(CasPlugin *plugin) : mPlugin(plugin) {}
37 ~PluginHolder() { if (mPlugin != NULL) delete mPlugin; }
38 CasPlugin* get() { return mPlugin; }
39
40private:
41 CasPlugin *mPlugin;
42 DISALLOW_EVIL_CONSTRUCTORS(PluginHolder);
43};
44
45CasImpl::CasImpl(const sp<ICasListener> &listener)
46 : mPluginHolder(NULL), mListener(listener) {
47 ALOGV("CTOR");
48}
49
50CasImpl::~CasImpl() {
51 ALOGV("DTOR");
52 release();
53}
54
55//static
56void CasImpl::OnEvent(
57 void *appData,
58 int32_t event,
59 int32_t arg,
60 uint8_t *data,
61 size_t size) {
62 if (appData == NULL) {
63 ALOGE("Invalid appData!");
64 return;
65 }
66 CasImpl *casImpl = static_cast<CasImpl *>(appData);
67 casImpl->onEvent(event, arg, data, size);
68}
69
70void CasImpl::init(const sp<SharedLibrary>& library, CasPlugin *plugin) {
71 mLibrary = library;
72 mPluginHolder = new PluginHolder(plugin);
73}
74
75void CasImpl::onEvent(
76 int32_t event, int32_t arg, uint8_t *data, size_t size) {
77 if (mListener == NULL) {
78 return;
79 }
80
81 HidlCasData eventData;
82 if (data != NULL) {
83 eventData.setToExternal(data, size);
84 }
85
86 mListener->onEvent(event, arg, eventData);
87}
88
89Return<Status> CasImpl::setPrivateData(const HidlCasData& pvtData) {
90 ALOGV("%s", __FUNCTION__);
91 sp<PluginHolder> holder = mPluginHolder;
92 if (holder == NULL) {
93 return toStatus(INVALID_OPERATION);
94 }
95 return toStatus(holder->get()->setPrivateData(pvtData));
96}
97
98Return<void> CasImpl::openSession(openSession_cb _hidl_cb) {
99 ALOGV("%s", __FUNCTION__);
100 CasSessionId sessionId;
101
102 sp<PluginHolder> holder = mPluginHolder;
103 status_t err = INVALID_OPERATION;
104 if (holder != NULL) {
105 err = holder->get()->openSession(&sessionId);
Chong Zhangea3f07b2017-12-12 21:51:40 -0800106 holder.clear();
Chong Zhanga4f67512017-04-24 17:18:25 -0700107 }
108
109 _hidl_cb(toStatus(err), sessionId);
110
111 return Void();
112}
113
114Return<Status> CasImpl::setSessionPrivateData(
115 const HidlCasSessionId &sessionId, const HidlCasData& pvtData) {
116 ALOGV("%s: sessionId=%s", __FUNCTION__,
117 sessionIdToString(sessionId).string());
118 sp<PluginHolder> holder = mPluginHolder;
119 if (holder == NULL) {
120 return toStatus(INVALID_OPERATION);
121 }
122 return toStatus(
123 holder->get()->setSessionPrivateData(
124 sessionId, pvtData));
125}
126
127Return<Status> CasImpl::closeSession(const HidlCasSessionId &sessionId) {
128 ALOGV("%s: sessionId=%s", __FUNCTION__,
129 sessionIdToString(sessionId).string());
130 sp<PluginHolder> holder = mPluginHolder;
131 if (holder == NULL) {
132 return toStatus(INVALID_OPERATION);
133 }
134 return toStatus(holder->get()->closeSession(sessionId));
135}
136
137Return<Status> CasImpl::processEcm(
138 const HidlCasSessionId &sessionId, const HidlCasData& ecm) {
139 ALOGV("%s: sessionId=%s", __FUNCTION__,
140 sessionIdToString(sessionId).string());
141 sp<PluginHolder> holder = mPluginHolder;
142 if (holder == NULL) {
143 return toStatus(INVALID_OPERATION);
144 }
145
146 return toStatus(holder->get()->processEcm(sessionId, ecm));
147}
148
149Return<Status> CasImpl::processEmm(const HidlCasData& emm) {
150 ALOGV("%s", __FUNCTION__);
151 sp<PluginHolder> holder = mPluginHolder;
152 if (holder == NULL) {
153 return toStatus(INVALID_OPERATION);
154 }
155
156 return toStatus(holder->get()->processEmm(emm));
157}
158
159Return<Status> CasImpl::sendEvent(
160 int32_t event, int32_t arg,
161 const HidlCasData& eventData) {
162 ALOGV("%s", __FUNCTION__);
163 sp<PluginHolder> holder = mPluginHolder;
164 if (holder == NULL) {
165 return toStatus(INVALID_OPERATION);
166 }
167
168 status_t err = holder->get()->sendEvent(event, arg, eventData);
169 return toStatus(err);
170}
171
172Return<Status> CasImpl::provision(const hidl_string& provisionString) {
173 ALOGV("%s: provisionString=%s", __FUNCTION__, provisionString.c_str());
174 sp<PluginHolder> holder = mPluginHolder;
175 if (holder == NULL) {
176 return toStatus(INVALID_OPERATION);
177 }
178
179 return toStatus(holder->get()->provision(String8(provisionString.c_str())));
180}
181
182Return<Status> CasImpl::refreshEntitlements(
183 int32_t refreshType,
184 const HidlCasData& refreshData) {
185 ALOGV("%s", __FUNCTION__);
186 sp<PluginHolder> holder = mPluginHolder;
187 if (holder == NULL) {
188 return toStatus(INVALID_OPERATION);
189 }
190
191 status_t err = holder->get()->refreshEntitlements(refreshType, refreshData);
192 return toStatus(err);
193}
194
195Return<Status> CasImpl::release() {
196 ALOGV("%s: plugin=%p", __FUNCTION__,
197 mPluginHolder != NULL ? mPluginHolder->get() : NULL);
198 mPluginHolder.clear();
199 return Status::OK;
200}
201
202} // namespace implementation
203} // namespace V1_0
204} // namespace cas
205} // namespace hardware
206} // namespace android