]> sigrok.org Git - libsigrokflow.git/blob - src/main.cpp
fadd19c50df9a660c100b483fb6a8bb1583cd4d4
[libsigrokflow.git] / src / main.cpp
1 /*
2  * This file is part of the libsigrokflow project.
3  *
4  * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li>
5  * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <iostream>
23 #include <libsigrokflow/libsigrokflow.hpp>
24
25 namespace Srf
26 {
27
28 using namespace std;
29 using namespace std::placeholders;
30
31 Sink::Sink(GstBaseSink *gobj) :
32         Gst::BaseSink(gobj)
33 {
34 }
35
36 Device::Device(GstElement *gobj) :
37         Gst::Element(gobj)
38 {
39 }
40
41 CaptureDevice::CaptureDevice(GstElement *gobj) :
42         Device(gobj)
43 {
44 }
45
46 #ifdef HAVE_LIBSIGROKCXX
47 void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
48 {
49         klass->set_metadata("sigrok legacy input",
50                         "Transform", "Wrapper for inputs using legacy libsigrok APIs",
51                         "Martin Ling");
52
53         klass->add_pad_template(Gst::PadTemplate::create(
54                         "sink",
55                         Gst::PAD_SINK,
56                         Gst::PAD_ALWAYS,
57                         Gst::Caps::create_any()));
58
59         klass->add_pad_template(Gst::PadTemplate::create(
60                         "src",
61                         Gst::PAD_SRC,
62                         Gst::PAD_ALWAYS,
63                         Gst::Caps::create_any()));
64 }
65
66 bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
67 {
68         Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
69                         0, Gst::register_mm_type<LegacyInput>(
70                                 "sigrok_legacy_input"));
71
72         return true;
73 }
74
75 LegacyInput::LegacyInput(GstElement *gobj) :
76         Glib::ObjectBase(typeid(LegacyInput)),
77         Gst::Element(gobj)
78 {
79         add_pad(sink_pad_ = Gst::Pad::create(get_pad_template("sink"), "sink"));
80         add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src"));
81         sink_pad_->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
82 }
83
84 Glib::RefPtr<LegacyInput> LegacyInput::create(
85         shared_ptr<sigrok::InputFormat> libsigrok_input_format,
86         map<string, Glib::VariantBase> options)
87 {
88         auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
89         if (!element)
90                 throw runtime_error("Failed to create element - plugin not registered?");
91         auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
92         input->libsigrok_input_format_ = libsigrok_input_format;
93         input->options_ = options;
94
95         return input;
96 }
97
98 bool LegacyInput::start_vfunc()
99 {
100         libsigrok_input_ = libsigrok_input_format_->create_input(options_);
101         auto context = libsigrok_input_format_->parent();
102         session_ = context->create_session();
103         session_->add_device(libsigrok_input_->device());
104         session_->add_datafeed_callback(bind(&LegacyInput::datafeed_callback_, this, _1, _2));
105         session_->start();
106
107         return true;
108 }
109
110 void LegacyInput::datafeed_callback_(
111         shared_ptr<sigrok::Device> device,
112         shared_ptr<sigrok::Packet> packet)
113 {
114         (void)device;
115
116         switch (packet->type()->id()) {
117         case SR_DF_LOGIC: {
118                 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
119                 auto mem = Gst::Memory::create(
120                                 Gst::MEMORY_FLAG_READONLY,
121                                 logic->data_pointer(),
122                                 logic->data_length(),
123                                 0,
124                                 logic->data_length());
125                 auto buf = Gst::Buffer::create();
126                 buf->append_memory(move(mem));
127                 src_pad_->push(move(buf));
128                 break;
129         }
130         case SR_DF_END:
131                 session_->stop();
132                 src_pad_->push_event(Gst::EventEos::create());
133                 break;
134         default:
135                 break;
136         }
137 }
138
139 Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
140         const Glib::RefPtr<Gst::Buffer> &buf)
141 {
142         Gst::MapInfo info;
143         buf->map(info, Gst::MAP_READ);
144         libsigrok_input_->send(info.get_data(), info.get_size());
145         buf->unmap(info);
146
147         return Gst::FLOW_OK;
148 }
149
150 bool LegacyInput::stop_vfunc()
151 {
152         libsigrok_input_->end();
153
154         return true;
155 }
156
157 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
158 {
159         klass->set_metadata("sigrok legacy output",
160                         "Sink", "Wrapper for outputs using legacy libsigrok APIs",
161                         "Martin Ling");
162
163         klass->add_pad_template(Gst::PadTemplate::create(
164                         "sink",
165                         Gst::PAD_SINK,
166                         Gst::PAD_ALWAYS,
167                         Gst::Caps::create_any()));
168 }
169
170 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
171 {
172         Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
173                         0, Gst::register_mm_type<LegacyOutput>(
174                                 "sigrok_legacy_output"));
175
176         return true;
177 }
178
179 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
180         Glib::ObjectBase(typeid(LegacyOutput)),
181         Sink(gobj)
182 {
183 }
184
185 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
186         shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
187         shared_ptr<sigrok::Device> libsigrok_device,
188         map<string, Glib::VariantBase> options)
189 {
190         auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
191         if (!element)
192                 throw runtime_error("Failed to create element - plugin not registered?");
193         auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
194         output->libsigrok_output_format_ = libsigrok_output_format;
195         output->libsigrok_device_ = libsigrok_device;
196         output->options_ = options;
197
198         return output;
199 }
200
201 bool LegacyOutput::start_vfunc()
202 {
203         libsigrok_output_ = libsigrok_output_format_->create_output(
204                         libsigrok_device_, options_);
205
206         return true;
207 }
208
209 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
210 {
211         Gst::MapInfo info;
212         buffer->map(info, Gst::MAP_READ);
213         auto context = libsigrok_output_format_->parent();
214         auto packet = context->create_logic_packet(
215                         info.get_data(), info.get_size(), 2);
216         auto result = libsigrok_output_->receive(packet);
217         cout << result;
218         buffer->unmap(info);
219
220         return Gst::FLOW_OK;
221 }
222
223 bool LegacyOutput::stop_vfunc()
224 {
225         auto context = libsigrok_output_format_->parent();
226         auto end_packet = context->create_end_packet();
227         auto result = libsigrok_output_->receive(end_packet);
228         cout << result;
229
230         return true;
231 }
232 #endif
233
234 #ifdef HAVE_LIBSIGROKDECODE
235 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
236 {
237         klass->set_metadata("sigrok legacy decoder",
238                         "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
239                         "Uwe Hermann");
240
241         klass->add_pad_template(Gst::PadTemplate::create(
242                         "sink",
243                         Gst::PAD_SINK,
244                         Gst::PAD_ALWAYS,
245                         Gst::Caps::create_any()));
246 }
247
248 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
249 {
250         Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
251                         0, Gst::register_mm_type<LegacyDecoder>(
252                                 "sigrok_legacy_decoder"));
253
254         return true;
255 }
256
257 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
258         Glib::ObjectBase(typeid(LegacyDecoder)),
259         Sink(gobj)
260 {
261 }
262
263 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
264         struct srd_session *libsigrokdecode_session, uint64_t unitsize)
265 {
266         auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
267         if (!element)
268                 throw runtime_error("Failed to create element - plugin not registered?");
269         auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
270         decoder->session_ = libsigrokdecode_session;
271         decoder->unitsite_ = unitsize;
272
273         return decoder;
274 }
275
276 struct srd_session *LegacyDecoder::libsigrokdecode_session()
277 {
278         return session_;
279 }
280
281 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
282 {
283         Gst::MapInfo info;
284         buffer->map(info, Gst::MAP_READ);
285         uint64_t num_samples = info.get_size() / unitsite_;
286         srd_session_send(session_, abs_ss_, abs_ss_ + num_samples,
287                 info.get_data(), info.get_size(), unitsite_);
288         abs_ss_ += num_samples;
289         buffer->unmap(info);
290
291         return Gst::FLOW_OK;
292 }
293
294 bool LegacyDecoder::start_vfunc()
295 {
296         abs_ss_ = 0;
297         srd_session_start(session_);
298
299         return true;
300 }
301
302 bool LegacyDecoder::stop_vfunc()
303 {
304         srd_session_terminate_reset(session_);
305
306         return true;
307 }
308 #endif
309
310 }