1 /*
2 * worker.cpp - worker implementation
3 *
4 * Copyright (c) 2017 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21 #include "worker.h"
22
23 namespace XCam {
24
Worker(const char * name,const SmartPtr<Callback> & cb)25 Worker::Worker (const char *name, const SmartPtr<Callback> &cb)
26 : _name (NULL)
27 , _callback (cb)
28 {
29 if (name)
30 _name = strndup (name, XCAM_MAX_STR_SIZE);
31 }
32
~Worker()33 Worker::~Worker ()
34 {
35 xcam_mem_clear (_name);
36 }
37
38 bool
set_name(const char * name)39 Worker::set_name (const char *name)
40 {
41 XCAM_FAIL_RETURN (
42 ERROR, name,
43 false, "worker set name failed with parameter NULL");
44
45 XCAM_FAIL_RETURN (
46 ERROR, !_name, false,
47 "worker(%s) set name(%s) failed, already got a name", XCAM_STR (get_name ()), XCAM_STR (name));
48
49 _name = strndup (name, XCAM_MAX_STR_SIZE);
50 return true;
51 }
52
53 bool
set_callback(const SmartPtr<Worker::Callback> & callback)54 Worker::set_callback (const SmartPtr<Worker::Callback> &callback)
55 {
56 XCAM_ASSERT (!_callback.ptr ());
57 XCAM_FAIL_RETURN (
58 ERROR, !_callback.ptr (),
59 false, "worker(%s) callback was already set", XCAM_STR(get_name ()));
60
61 _callback = callback;
62 return true;
63 }
64
65 void
status_check(const SmartPtr<Worker::Arguments> & args,const XCamReturn error)66 Worker::status_check (const SmartPtr<Worker::Arguments> &args, const XCamReturn error)
67 {
68 if (_callback.ptr ())
69 _callback->work_status (this, args, error);
70 }
71
72 #if ENABLE_FUNC_OBJ
73 bool
set_func_obj(const SmartPtr<FuncObj> & obj)74 Worker::set_func_obj (const SmartPtr<FuncObj> &obj)
75 {
76 XCAM_FAIL_RETURN (
77 ERROR, !_func_obj.ptr (),
78 false, "worker(%s) func_obj was already set", XCAM_STR(get_name ()));
79 _func_obj = obj;
80 return true;
81 }
82
83 XCamReturn
work(const SmartPtr<Worker::Arguments> & args)84 Worker::work (const SmartPtr<Worker::Arguments> &args)
85 {
86 XCamReturn ret = _func_obj->impl(args);
87 status_check (args, ret);
88 return ret;
89 }
90 #endif
91 };
92
93 namespace UnitTestWorker {
94 using namespace XCam;
95
96 struct UTArguments : Worker::Arguments {
97 uint32_t data;
UTArgumentsUnitTestWorker::UTArguments98 UTArguments () : data (5) {}
99 };
100
101 class UnitTestWorker: public Worker {
102 public:
UnitTestWorker()103 UnitTestWorker () : Worker("UnitTestWorker") {}
work(const SmartPtr<Worker::Arguments> & args)104 XCamReturn work (const SmartPtr<Worker::Arguments> &args) {
105 SmartPtr<UTArguments> ut_args = args.dynamic_cast_ptr<UTArguments> ();
106 XCAM_ASSERT (ut_args.ptr ());
107 printf ("unit test worker runing on data:%d\n", ut_args->data);
108 status_check (args, XCAM_RETURN_NO_ERROR);
109 return XCAM_RETURN_NO_ERROR;
110 }
stop()111 XCamReturn stop () {
112 return XCAM_RETURN_NO_ERROR;
113 }
114 };
115
116 class UintTestHandler {
117 public:
work_done(const SmartPtr<Worker> & w,const SmartPtr<Worker::Arguments> &,const XCamReturn error)118 XCamReturn work_done (
119 const SmartPtr<Worker> &w, const SmartPtr<Worker::Arguments> &,
120 const XCamReturn error) {
121 printf ("worker(%s) done, error:%d",
122 XCAM_STR(w->get_name ()), error);
123 return error;
124 }
125 };
126
127 DECLARE_WORK_CALLBACK (UTCbBridge, UintTestHandler, work_done);
128
test_base_worker()129 void test_base_worker()
130 {
131 SmartPtr<UintTestHandler> handler = new UintTestHandler;
132 SmartPtr<Worker> worker = new UnitTestWorker;
133 worker->set_callback (new UTCbBridge (handler));
134 worker->work (new UTArguments);
135 }
136
137 };
138