1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker *
4*ec779b8eSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker *
8*ec779b8eSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker *
10*ec779b8eSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker */
16*ec779b8eSAndroid Build Coastguard Worker
17*ec779b8eSAndroid Build Coastguard Worker #define LOG_NDEBUG 0
18*ec779b8eSAndroid Build Coastguard Worker #define LOG_TAG "C2ComponentWrapper"
19*ec779b8eSAndroid Build Coastguard Worker
20*ec779b8eSAndroid Build Coastguard Worker #include <chrono>
21*ec779b8eSAndroid Build Coastguard Worker #include <functional>
22*ec779b8eSAndroid Build Coastguard Worker #include <thread>
23*ec779b8eSAndroid Build Coastguard Worker
24*ec779b8eSAndroid Build Coastguard Worker #include <C2ComponentWrapper.h>
25*ec779b8eSAndroid Build Coastguard Worker #include <C2Config.h>
26*ec779b8eSAndroid Build Coastguard Worker #include <C2PlatformSupport.h>
27*ec779b8eSAndroid Build Coastguard Worker
28*ec779b8eSAndroid Build Coastguard Worker namespace android {
29*ec779b8eSAndroid Build Coastguard Worker
30*ec779b8eSAndroid Build Coastguard Worker namespace {
31*ec779b8eSAndroid Build Coastguard Worker
32*ec779b8eSAndroid Build Coastguard Worker using namespace std::chrono_literals;
33*ec779b8eSAndroid Build Coastguard Worker
WrapSimpleMethod(std::function<c2_status_t (void)> op,const SimpleMethodState & state)34*ec779b8eSAndroid Build Coastguard Worker c2_status_t WrapSimpleMethod(
35*ec779b8eSAndroid Build Coastguard Worker std::function<c2_status_t(void)> op, const SimpleMethodState &state) {
36*ec779b8eSAndroid Build Coastguard Worker c2_status_t result = C2_OK;
37*ec779b8eSAndroid Build Coastguard Worker switch (state.getMode()) {
38*ec779b8eSAndroid Build Coastguard Worker case SimpleMethodState::EXECUTE:
39*ec779b8eSAndroid Build Coastguard Worker result = op();
40*ec779b8eSAndroid Build Coastguard Worker break;
41*ec779b8eSAndroid Build Coastguard Worker case SimpleMethodState::NO_OP:
42*ec779b8eSAndroid Build Coastguard Worker break;
43*ec779b8eSAndroid Build Coastguard Worker case SimpleMethodState::HANG:
44*ec779b8eSAndroid Build Coastguard Worker while (true) {
45*ec779b8eSAndroid Build Coastguard Worker std::this_thread::sleep_for(1s);
46*ec779b8eSAndroid Build Coastguard Worker }
47*ec779b8eSAndroid Build Coastguard Worker break;
48*ec779b8eSAndroid Build Coastguard Worker }
49*ec779b8eSAndroid Build Coastguard Worker (void)state.overrideResult(&result);
50*ec779b8eSAndroid Build Coastguard Worker return result;
51*ec779b8eSAndroid Build Coastguard Worker }
52*ec779b8eSAndroid Build Coastguard Worker
53*ec779b8eSAndroid Build Coastguard Worker } // namespace
54*ec779b8eSAndroid Build Coastguard Worker
Injecter(C2ComponentWrapper * thiz)55*ec779b8eSAndroid Build Coastguard Worker C2ComponentWrapper::Injecter::Injecter(C2ComponentWrapper *thiz) : mThiz(thiz) {}
56*ec779b8eSAndroid Build Coastguard Worker
start()57*ec779b8eSAndroid Build Coastguard Worker SimpleMethodState::Injecter C2ComponentWrapper::Injecter::start() {
58*ec779b8eSAndroid Build Coastguard Worker return SimpleMethodState::Injecter(&mThiz->mStartState);
59*ec779b8eSAndroid Build Coastguard Worker }
60*ec779b8eSAndroid Build Coastguard Worker
Listener(const std::shared_ptr<C2Component::Listener> & listener)61*ec779b8eSAndroid Build Coastguard Worker C2ComponentWrapper::Listener::Listener(
62*ec779b8eSAndroid Build Coastguard Worker const std::shared_ptr<C2Component::Listener> &listener) : mListener(listener) {}
63*ec779b8eSAndroid Build Coastguard Worker
onWorkDone_nb(std::weak_ptr<C2Component> component,std::list<std::unique_ptr<C2Work>> workItems)64*ec779b8eSAndroid Build Coastguard Worker void C2ComponentWrapper::Listener::onWorkDone_nb(std::weak_ptr<C2Component> component,
65*ec779b8eSAndroid Build Coastguard Worker std::list<std::unique_ptr<C2Work>> workItems) {
66*ec779b8eSAndroid Build Coastguard Worker mListener->onWorkDone_nb(component, std::move(workItems));
67*ec779b8eSAndroid Build Coastguard Worker }
68*ec779b8eSAndroid Build Coastguard Worker
onTripped_nb(std::weak_ptr<C2Component> component,std::vector<std::shared_ptr<C2SettingResult>> settingResult)69*ec779b8eSAndroid Build Coastguard Worker void C2ComponentWrapper::Listener::onTripped_nb(std::weak_ptr<C2Component> component,
70*ec779b8eSAndroid Build Coastguard Worker std::vector<std::shared_ptr<C2SettingResult>> settingResult) {
71*ec779b8eSAndroid Build Coastguard Worker mListener->onTripped_nb(component,settingResult);
72*ec779b8eSAndroid Build Coastguard Worker }
73*ec779b8eSAndroid Build Coastguard Worker
onError_nb(std::weak_ptr<C2Component> component,uint32_t errorCode)74*ec779b8eSAndroid Build Coastguard Worker void C2ComponentWrapper::Listener::onError_nb(
75*ec779b8eSAndroid Build Coastguard Worker std::weak_ptr<C2Component> component, uint32_t errorCode) {
76*ec779b8eSAndroid Build Coastguard Worker mListener->onError_nb(component, errorCode);
77*ec779b8eSAndroid Build Coastguard Worker }
78*ec779b8eSAndroid Build Coastguard Worker
C2ComponentWrapper(const std::shared_ptr<C2Component> & comp)79*ec779b8eSAndroid Build Coastguard Worker C2ComponentWrapper::C2ComponentWrapper(
80*ec779b8eSAndroid Build Coastguard Worker const std::shared_ptr<C2Component> &comp) : mComp(comp) {}
81*ec779b8eSAndroid Build Coastguard Worker
setListener_vb(const std::shared_ptr<C2Component::Listener> & listener,c2_blocking_t mayBlock)82*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::setListener_vb(
83*ec779b8eSAndroid Build Coastguard Worker const std::shared_ptr<C2Component::Listener> &listener, c2_blocking_t mayBlock) {
84*ec779b8eSAndroid Build Coastguard Worker mListener = std::make_shared<Listener>(listener);
85*ec779b8eSAndroid Build Coastguard Worker return mComp->setListener_vb(mListener, mayBlock);
86*ec779b8eSAndroid Build Coastguard Worker }
87*ec779b8eSAndroid Build Coastguard Worker
queue_nb(std::list<std::unique_ptr<C2Work>> * const items)88*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::queue_nb(std::list<std::unique_ptr<C2Work>>* const items) {
89*ec779b8eSAndroid Build Coastguard Worker return mComp->queue_nb(items);
90*ec779b8eSAndroid Build Coastguard Worker }
91*ec779b8eSAndroid Build Coastguard Worker
announce_nb(const std::vector<C2WorkOutline> & items)92*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::announce_nb(const std::vector<C2WorkOutline> &items) {
93*ec779b8eSAndroid Build Coastguard Worker return mComp->announce_nb(items);
94*ec779b8eSAndroid Build Coastguard Worker }
95*ec779b8eSAndroid Build Coastguard Worker
flush_sm(C2Component::flush_mode_t mode,std::list<std::unique_ptr<C2Work>> * const flushedWork)96*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::flush_sm(
97*ec779b8eSAndroid Build Coastguard Worker C2Component::flush_mode_t mode, std::list<std::unique_ptr<C2Work>>* const flushedWork) {
98*ec779b8eSAndroid Build Coastguard Worker return mComp->flush_sm(mode, flushedWork);
99*ec779b8eSAndroid Build Coastguard Worker }
100*ec779b8eSAndroid Build Coastguard Worker
drain_nb(C2Component::drain_mode_t mode)101*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::drain_nb(C2Component::drain_mode_t mode) {
102*ec779b8eSAndroid Build Coastguard Worker return mComp->drain_nb(mode);
103*ec779b8eSAndroid Build Coastguard Worker }
104*ec779b8eSAndroid Build Coastguard Worker
start()105*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::start() {
106*ec779b8eSAndroid Build Coastguard Worker return WrapSimpleMethod([this] { return mComp->start(); }, mStartState);
107*ec779b8eSAndroid Build Coastguard Worker }
108*ec779b8eSAndroid Build Coastguard Worker
stop()109*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::stop() {
110*ec779b8eSAndroid Build Coastguard Worker return mComp->stop();
111*ec779b8eSAndroid Build Coastguard Worker }
112*ec779b8eSAndroid Build Coastguard Worker
reset()113*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::reset() {
114*ec779b8eSAndroid Build Coastguard Worker return mComp->reset();
115*ec779b8eSAndroid Build Coastguard Worker }
116*ec779b8eSAndroid Build Coastguard Worker
release()117*ec779b8eSAndroid Build Coastguard Worker c2_status_t C2ComponentWrapper::release() {
118*ec779b8eSAndroid Build Coastguard Worker return mComp->release();
119*ec779b8eSAndroid Build Coastguard Worker }
120*ec779b8eSAndroid Build Coastguard Worker
intf()121*ec779b8eSAndroid Build Coastguard Worker std::shared_ptr<C2ComponentInterface> C2ComponentWrapper::intf(){
122*ec779b8eSAndroid Build Coastguard Worker return mComp->intf();
123*ec779b8eSAndroid Build Coastguard Worker }
124*ec779b8eSAndroid Build Coastguard Worker
inject()125*ec779b8eSAndroid Build Coastguard Worker C2ComponentWrapper::Injecter C2ComponentWrapper::inject() {
126*ec779b8eSAndroid Build Coastguard Worker return Injecter(this);
127*ec779b8eSAndroid Build Coastguard Worker }
128*ec779b8eSAndroid Build Coastguard Worker
129*ec779b8eSAndroid Build Coastguard Worker } // namespace android
130