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
28namespace android {
29namespace hardware {
30namespace cas {
31namespace V1_0 {
32namespace implementation {
33
34CasImpl::CasImpl(const sp<ICasListener> &listener)
35    : mListener(listener) {
36    ALOGV("CTOR");
37}
38
39CasImpl::~CasImpl() {
40    ALOGV("DTOR");
41    release();
42}
43
44//static
45void 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
59void 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
65void 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
79Return<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
88Return<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
104Return<Status> CasImpl::setSessionPrivateData(
105        const HidlCasSessionId &sessionId, const HidlCasData& pvtData) {
106    ALOGV("%s: sessionId=%s", __FUNCTION__,
107            sessionIdToString(sessionId).string());
108    std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
109    if (holder.get() == nullptr) {
110        return toStatus(INVALID_OPERATION);
111    }
112    return toStatus(holder->setSessionPrivateData(sessionId, pvtData));
113}
114
115Return<Status> CasImpl::closeSession(const HidlCasSessionId &sessionId) {
116    ALOGV("%s: sessionId=%s", __FUNCTION__,
117            sessionIdToString(sessionId).string());
118    std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
119    if (holder.get() == nullptr) {
120        return toStatus(INVALID_OPERATION);
121    }
122    return toStatus(holder->closeSession(sessionId));
123}
124
125Return<Status> CasImpl::processEcm(
126        const HidlCasSessionId &sessionId, const HidlCasData& ecm) {
127    ALOGV("%s: sessionId=%s", __FUNCTION__,
128            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
134    return toStatus(holder->processEcm(sessionId, ecm));
135}
136
137Return<Status> CasImpl::processEmm(const HidlCasData& emm) {
138    ALOGV("%s", __FUNCTION__);
139    std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
140    if (holder.get() == nullptr) {
141        return toStatus(INVALID_OPERATION);
142    }
143
144    return toStatus(holder->processEmm(emm));
145}
146
147Return<Status> CasImpl::sendEvent(
148        int32_t event, int32_t arg,
149        const HidlCasData& eventData) {
150    ALOGV("%s", __FUNCTION__);
151    std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
152    if (holder.get() == nullptr) {
153        return toStatus(INVALID_OPERATION);
154    }
155
156    status_t err = holder->sendEvent(event, arg, eventData);
157    return toStatus(err);
158}
159
160Return<Status> CasImpl::provision(const hidl_string& provisionString) {
161    ALOGV("%s: provisionString=%s", __FUNCTION__, provisionString.c_str());
162    std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
163    if (holder.get() == nullptr) {
164        return toStatus(INVALID_OPERATION);
165    }
166
167    return toStatus(holder->provision(String8(provisionString.c_str())));
168}
169
170Return<Status> CasImpl::refreshEntitlements(
171        int32_t refreshType,
172        const HidlCasData& refreshData) {
173    ALOGV("%s", __FUNCTION__);
174    std::shared_ptr<CasPlugin> holder = std::atomic_load(&mPluginHolder);
175    if (holder.get() == nullptr) {
176        return toStatus(INVALID_OPERATION);
177    }
178
179    status_t err = holder->refreshEntitlements(refreshType, refreshData);
180    return toStatus(err);
181}
182
183Return<Status> CasImpl::release() {
184    ALOGV("%s: plugin=%p", __FUNCTION__, mPluginHolder.get());
185
186    std::shared_ptr<CasPlugin> holder(nullptr);
187    std::atomic_store(&mPluginHolder, holder);
188
189    return Status::OK;
190}
191
192} // namespace implementation
193} // namespace V1_0
194} // namespace cas
195} // namespace hardware
196} // namespace android
197