Home | History | Annotate | Download | only in default
      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 (at) 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 
     34 struct CasImpl::PluginHolder : public RefBase {
     35 public:
     36     explicit PluginHolder(CasPlugin *plugin) : mPlugin(plugin) {}
     37     ~PluginHolder() { if (mPlugin != NULL) delete mPlugin; }
     38     CasPlugin* get() { return mPlugin; }
     39 
     40 private:
     41     CasPlugin *mPlugin;
     42     DISALLOW_EVIL_CONSTRUCTORS(PluginHolder);
     43 };
     44 
     45 CasImpl::CasImpl(const sp<ICasListener> &listener)
     46     : mPluginHolder(NULL), mListener(listener) {
     47     ALOGV("CTOR");
     48 }
     49 
     50 CasImpl::~CasImpl() {
     51     ALOGV("DTOR");
     52     release();
     53 }
     54 
     55 //static
     56 void 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 
     70 void CasImpl::init(const sp<SharedLibrary>& library, CasPlugin *plugin) {
     71     mLibrary = library;
     72     mPluginHolder = new PluginHolder(plugin);
     73 }
     74 
     75 void 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 
     89 Return<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 
     98 Return<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);
    106     }
    107 
    108     _hidl_cb(toStatus(err), sessionId);
    109 
    110     return Void();
    111 }
    112 
    113 Return<Status> CasImpl::setSessionPrivateData(
    114         const HidlCasSessionId &sessionId, const HidlCasData& pvtData) {
    115     ALOGV("%s: sessionId=%s", __FUNCTION__,
    116             sessionIdToString(sessionId).string());
    117     sp<PluginHolder> holder = mPluginHolder;
    118     if (holder == NULL) {
    119         return toStatus(INVALID_OPERATION);
    120     }
    121     return toStatus(
    122             holder->get()->setSessionPrivateData(
    123                     sessionId, pvtData));
    124 }
    125 
    126 Return<Status> CasImpl::closeSession(const HidlCasSessionId &sessionId) {
    127     ALOGV("%s: sessionId=%s", __FUNCTION__,
    128             sessionIdToString(sessionId).string());
    129     sp<PluginHolder> holder = mPluginHolder;
    130     if (holder == NULL) {
    131         return toStatus(INVALID_OPERATION);
    132     }
    133     return toStatus(holder->get()->closeSession(sessionId));
    134 }
    135 
    136 Return<Status> CasImpl::processEcm(
    137         const HidlCasSessionId &sessionId, const HidlCasData& ecm) {
    138     ALOGV("%s: sessionId=%s", __FUNCTION__,
    139             sessionIdToString(sessionId).string());
    140     sp<PluginHolder> holder = mPluginHolder;
    141     if (holder == NULL) {
    142         return toStatus(INVALID_OPERATION);
    143     }
    144 
    145     return toStatus(holder->get()->processEcm(sessionId, ecm));
    146 }
    147 
    148 Return<Status> CasImpl::processEmm(const HidlCasData& emm) {
    149     ALOGV("%s", __FUNCTION__);
    150     sp<PluginHolder> holder = mPluginHolder;
    151     if (holder == NULL) {
    152         return toStatus(INVALID_OPERATION);
    153     }
    154 
    155     return toStatus(holder->get()->processEmm(emm));
    156 }
    157 
    158 Return<Status> CasImpl::sendEvent(
    159         int32_t event, int32_t arg,
    160         const HidlCasData& eventData) {
    161     ALOGV("%s", __FUNCTION__);
    162     sp<PluginHolder> holder = mPluginHolder;
    163     if (holder == NULL) {
    164         return toStatus(INVALID_OPERATION);
    165     }
    166 
    167     status_t err = holder->get()->sendEvent(event, arg, eventData);
    168     return toStatus(err);
    169 }
    170 
    171 Return<Status> CasImpl::provision(const hidl_string& provisionString) {
    172     ALOGV("%s: provisionString=%s", __FUNCTION__, provisionString.c_str());
    173     sp<PluginHolder> holder = mPluginHolder;
    174     if (holder == NULL) {
    175         return toStatus(INVALID_OPERATION);
    176     }
    177 
    178     return toStatus(holder->get()->provision(String8(provisionString.c_str())));
    179 }
    180 
    181 Return<Status> CasImpl::refreshEntitlements(
    182         int32_t refreshType,
    183         const HidlCasData& refreshData) {
    184     ALOGV("%s", __FUNCTION__);
    185     sp<PluginHolder> holder = mPluginHolder;
    186     if (holder == NULL) {
    187         return toStatus(INVALID_OPERATION);
    188     }
    189 
    190     status_t err = holder->get()->refreshEntitlements(refreshType, refreshData);
    191     return toStatus(err);
    192 }
    193 
    194 Return<Status> CasImpl::release() {
    195     ALOGV("%s: plugin=%p", __FUNCTION__,
    196             mPluginHolder != NULL ? mPluginHolder->get() : NULL);
    197     mPluginHolder.clear();
    198     return Status::OK;
    199 }
    200 
    201 } // namespace implementation
    202 } // namespace V1_0
    203 } // namespace cas
    204 } // namespace hardware
    205 } // namespace android
    206