1 /*
2  * Copyright (C) 2018 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 "C2ComponentWrapper"
19 
20 #include <chrono>
21 #include <functional>
22 #include <thread>
23 
24 #include <C2ComponentWrapper.h>
25 #include <C2Config.h>
26 #include <C2PlatformSupport.h>
27 
28 namespace android {
29 
30 namespace {
31 
32 using namespace std::chrono_literals;
33 
WrapSimpleMethod(std::function<c2_status_t (void)> op,const SimpleMethodState & state)34 c2_status_t WrapSimpleMethod(
35         std::function<c2_status_t(void)> op, const SimpleMethodState &state) {
36     c2_status_t result = C2_OK;
37     switch (state.getMode()) {
38         case SimpleMethodState::EXECUTE:
39             result = op();
40             break;
41         case SimpleMethodState::NO_OP:
42             break;
43         case SimpleMethodState::HANG:
44             while (true) {
45                 std::this_thread::sleep_for(1s);
46             }
47             break;
48     }
49     (void)state.overrideResult(&result);
50     return result;
51 }
52 
53 }  // namespace
54 
Injecter(C2ComponentWrapper * thiz)55 C2ComponentWrapper::Injecter::Injecter(C2ComponentWrapper *thiz) : mThiz(thiz) {}
56 
start()57 SimpleMethodState::Injecter C2ComponentWrapper::Injecter::start() {
58     return SimpleMethodState::Injecter(&mThiz->mStartState);
59 }
60 
Listener(const std::shared_ptr<C2Component::Listener> & listener)61 C2ComponentWrapper::Listener::Listener(
62         const std::shared_ptr<C2Component::Listener> &listener) : mListener(listener) {}
63 
onWorkDone_nb(std::weak_ptr<C2Component> component,std::list<std::unique_ptr<C2Work>> workItems)64 void C2ComponentWrapper::Listener::onWorkDone_nb(std::weak_ptr<C2Component> component,
65         std::list<std::unique_ptr<C2Work>> workItems) {
66     mListener->onWorkDone_nb(component, std::move(workItems));
67 }
68 
onTripped_nb(std::weak_ptr<C2Component> component,std::vector<std::shared_ptr<C2SettingResult>> settingResult)69 void C2ComponentWrapper::Listener::onTripped_nb(std::weak_ptr<C2Component> component,
70         std::vector<std::shared_ptr<C2SettingResult>> settingResult) {
71     mListener->onTripped_nb(component,settingResult);
72 }
73 
onError_nb(std::weak_ptr<C2Component> component,uint32_t errorCode)74 void C2ComponentWrapper::Listener::onError_nb(
75         std::weak_ptr<C2Component> component, uint32_t errorCode) {
76     mListener->onError_nb(component, errorCode);
77 }
78 
C2ComponentWrapper(const std::shared_ptr<C2Component> & comp)79 C2ComponentWrapper::C2ComponentWrapper(
80         const std::shared_ptr<C2Component> &comp) : mComp(comp) {}
81 
setListener_vb(const std::shared_ptr<C2Component::Listener> & listener,c2_blocking_t mayBlock)82 c2_status_t C2ComponentWrapper::setListener_vb(
83         const std::shared_ptr<C2Component::Listener> &listener, c2_blocking_t mayBlock) {
84     mListener = std::make_shared<Listener>(listener);
85     return mComp->setListener_vb(mListener, mayBlock);
86 }
87 
queue_nb(std::list<std::unique_ptr<C2Work>> * const items)88 c2_status_t C2ComponentWrapper::queue_nb(std::list<std::unique_ptr<C2Work>>* const items) {
89     return mComp->queue_nb(items);
90 }
91 
announce_nb(const std::vector<C2WorkOutline> & items)92 c2_status_t C2ComponentWrapper::announce_nb(const std::vector<C2WorkOutline> &items) {
93     return mComp->announce_nb(items);
94 }
95 
flush_sm(C2Component::flush_mode_t mode,std::list<std::unique_ptr<C2Work>> * const flushedWork)96 c2_status_t C2ComponentWrapper::flush_sm(
97         C2Component::flush_mode_t mode, std::list<std::unique_ptr<C2Work>>* const flushedWork) {
98     return mComp->flush_sm(mode, flushedWork);
99 }
100 
drain_nb(C2Component::drain_mode_t mode)101 c2_status_t C2ComponentWrapper::drain_nb(C2Component::drain_mode_t mode) {
102     return mComp->drain_nb(mode);
103 }
104 
start()105 c2_status_t C2ComponentWrapper::start() {
106     return WrapSimpleMethod([this] { return mComp->start(); }, mStartState);
107 }
108 
stop()109 c2_status_t C2ComponentWrapper::stop() {
110     return mComp->stop();
111 }
112 
reset()113 c2_status_t C2ComponentWrapper::reset() {
114     return mComp->reset();
115 }
116 
release()117 c2_status_t C2ComponentWrapper::release() {
118     return mComp->release();
119 }
120 
intf()121 std::shared_ptr<C2ComponentInterface> C2ComponentWrapper::intf(){
122     return mComp->intf();
123 }
124 
inject()125 C2ComponentWrapper::Injecter C2ComponentWrapper::inject() {
126     return Injecter(this);
127 }
128 
129 }  // namespace android
130