]> sigrok.org Git - libsigrokflow.git/blame - src/main.cpp
Add initial tests/main.cpp (main Catch2 file).
[libsigrokflow.git] / src / main.cpp
CommitLineData
572e76fe
UH
1/*
2 * This file is part of the libsigrokflow project.
3 *
f1eaad84 4 * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li>
572e76fe
UH
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>
f7363af1 22#include <iostream>
45e6e688 23#include <libsigrokflow/libsigrokflow.hpp>
f7363af1 24
b903bb0a
UH
25namespace Srf
26{
27
f7363af1 28using namespace std;
d03b3a98 29using namespace std::placeholders;
f7363af1 30
b903bb0a
UH
31void init()
32{
1060e9b5 33#ifdef HAVE_LIBSIGROKCXX
6d71d36a
ML
34 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
35 "sigrok_legacy_capture_device",
36 "Wrapper for capture devices using legacy libsigrok APIs",
37 sigc::ptr_fun(&LegacyCaptureDevice::register_element),
97513ff0 38 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
92d521e7
ML
39 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
40 "sigrok_legacy_input",
41 "Wrapper for inputs using legacy libsigrok APIs",
42 sigc::ptr_fun(&LegacyInput::register_element),
43 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
e2cfc0ef
ML
44 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
45 "sigrok_legacy_output",
46 "Wrapper for outputs using legacy libsigrok APIs",
47 sigc::ptr_fun(&LegacyOutput::register_element),
48 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
1060e9b5 49#endif
45e6e688 50#ifdef HAVE_LIBSIGROKDECODE
b1322000
UH
51 Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
52 "sigrok_legacy_decoder",
53 "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
54 sigc::ptr_fun(&LegacyDecoder::register_element),
55 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
45e6e688 56#endif
6d71d36a
ML
57}
58
e2cfc0ef
ML
59Sink::Sink(GstBaseSink *gobj) :
60 Gst::BaseSink(gobj)
4b7d782a
ML
61{
62}
63
64Device::Device(GstElement *gobj) :
e2cfc0ef 65 Gst::Element(gobj)
4b7d782a
ML
66{
67}
68
69CaptureDevice::CaptureDevice(GstElement *gobj) :
70 Device(gobj)
71{
72}
73
1060e9b5 74#ifdef HAVE_LIBSIGROKCXX
6d71d36a
ML
75void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
76{
77 klass->set_metadata("sigrok legacy capture device",
78 "Source", "Wrapper for capture devices using legacy libsigrok APIs",
79 "Martin Ling");
80
81 klass->add_pad_template(Gst::PadTemplate::create(
82 "src",
83 Gst::PAD_SRC,
84 Gst::PAD_ALWAYS,
85 Gst::Caps::create_any()));
86}
87
88bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
89{
90 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
91 0, Gst::register_mm_type<LegacyCaptureDevice>(
92 "sigrok_legacy_capture_device"));
93 return true;
94}
95
96LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
97 Glib::ObjectBase(typeid(LegacyCaptureDevice)),
98 CaptureDevice(gobj)
99{
100 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
b903bb0a
UH
101}
102
64b0db03
ML
103Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
104 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
f7363af1 105{
64b0db03 106 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
d75c9a6a
ML
107 if (!element)
108 throw runtime_error("Failed to create element - plugin not registered?");
64b0db03 109 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
64b0db03
ML
110 device->_libsigrok_device = libsigrok_device;
111 return device;
f7363af1
ML
112}
113
d03b3a98 114shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
f7363af1 115{
64b0db03 116 return _libsigrok_device;
f7363af1
ML
117}
118
d03b3a98
ML
119Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
120{
121 switch (transition)
122 {
123 case Gst::STATE_CHANGE_READY_TO_PAUSED:
124 return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
125 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
d03b3a98
ML
126 _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
127 _task->set_lock(_mutex);
128 _src_pad->set_active(true);
129 _task->start();
130 return Gst::STATE_CHANGE_SUCCESS;
131 default:
132 return Gst::STATE_CHANGE_SUCCESS;
133 }
134}
135
136void LegacyCaptureDevice::_datafeed_callback(
137 shared_ptr<sigrok::Device> device,
138 shared_ptr<sigrok::Packet> packet)
139{
140 (void) device;
141 switch (packet->type()->id()) {
142 case SR_DF_LOGIC:
143 {
144 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
145 auto mem = Gst::Memory::create(
146 Gst::MEMORY_FLAG_READONLY,
147 logic->data_pointer(),
148 logic->data_length(),
149 0,
150 logic->data_length());
151 auto buf = Gst::Buffer::create();
152 buf->append_memory(move(mem));
153 _src_pad->push(move(buf));
154 break;
155 }
156 case SR_DF_END:
157 _session->stop();
158 _src_pad->push_event(Gst::EventEos::create());
159 break;
160 default:
161 break;
162 }
163}
164
165void LegacyCaptureDevice::_run()
166{
64b0db03
ML
167 _session = _libsigrok_device->driver()->parent()->create_session();
168 _session->add_device(_libsigrok_device);
d03b3a98
ML
169 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
170 _session->start();
171 _session->run();
172 _task->stop();
173}
174
92d521e7
ML
175void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
176{
177 klass->set_metadata("sigrok legacy input",
178 "Transform", "Wrapper for inputs using legacy libsigrok APIs",
179 "Martin Ling");
180
181 klass->add_pad_template(Gst::PadTemplate::create(
182 "sink",
183 Gst::PAD_SINK,
184 Gst::PAD_ALWAYS,
185 Gst::Caps::create_any()));
186
187 klass->add_pad_template(Gst::PadTemplate::create(
188 "src",
189 Gst::PAD_SRC,
190 Gst::PAD_ALWAYS,
191 Gst::Caps::create_any()));
192}
193
194bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
195{
196 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
197 0, Gst::register_mm_type<LegacyInput>(
198 "sigrok_legacy_input"));
199 return true;
200}
201
202LegacyInput::LegacyInput(GstElement *gobj) :
203 Glib::ObjectBase(typeid(LegacyInput)),
204 Gst::Element(gobj)
205{
206 add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
207 add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
208 _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
209}
210
211Glib::RefPtr<LegacyInput> LegacyInput::create(
212 shared_ptr<sigrok::InputFormat> libsigrok_input_format,
213 map<string, Glib::VariantBase> options)
214{
215 auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
216 if (!element)
217 throw runtime_error("Failed to create element - plugin not registered?");
218 auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
219 input->_libsigrok_input_format = libsigrok_input_format;
220 input->_options = options;
221 return input;
222}
223
224bool LegacyInput::start_vfunc()
225{
226 _libsigrok_input = _libsigrok_input_format->create_input(_options);
227 auto context = _libsigrok_input_format->parent();
228 _session = context->create_session();
229 _session->add_device(_libsigrok_input->device());
230 _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2));
231 _session->start();
232 return true;
233}
234
235void LegacyInput::_datafeed_callback(
236 shared_ptr<sigrok::Device> device,
237 shared_ptr<sigrok::Packet> packet)
238{
239 (void) device;
240 switch (packet->type()->id()) {
241 case SR_DF_LOGIC:
242 {
243 auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
244 auto mem = Gst::Memory::create(
245 Gst::MEMORY_FLAG_READONLY,
246 logic->data_pointer(),
247 logic->data_length(),
248 0,
249 logic->data_length());
250 auto buf = Gst::Buffer::create();
251 buf->append_memory(move(mem));
252 _src_pad->push(move(buf));
253 break;
254 }
255 case SR_DF_END:
256 _session->stop();
257 _src_pad->push_event(Gst::EventEos::create());
258 break;
259 default:
260 break;
261 }
262}
263
264Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
265 const Glib::RefPtr<Gst::Buffer> &buf)
266{
267 Gst::MapInfo info;
268 buf->map(info, Gst::MAP_READ);
269 _libsigrok_input->send(info.get_data(), info.get_size());
270 buf->unmap(info);
271 return Gst::FLOW_OK;
272}
273
274bool LegacyInput::stop_vfunc()
275{
276 _libsigrok_input->end();
277 return true;
278}
279
e2cfc0ef
ML
280void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
281{
282 klass->set_metadata("sigrok legacy output",
283 "Sink", "Wrapper for outputs using legacy libsigrok APIs",
284 "Martin Ling");
285
286 klass->add_pad_template(Gst::PadTemplate::create(
287 "sink",
288 Gst::PAD_SINK,
289 Gst::PAD_ALWAYS,
290 Gst::Caps::create_any()));
291}
292
293bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
294{
295 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
296 0, Gst::register_mm_type<LegacyOutput>(
297 "sigrok_legacy_output"));
298 return true;
299}
300
301LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
302 Glib::ObjectBase(typeid(LegacyOutput)),
303 Sink(gobj)
304{
e2cfc0ef
ML
305}
306
307Glib::RefPtr<LegacyOutput>LegacyOutput::create(
726122c2
ML
308 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
309 shared_ptr<sigrok::Device> libsigrok_device,
310 map<string, Glib::VariantBase> options)
e2cfc0ef
ML
311{
312 auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
313 if (!element)
314 throw runtime_error("Failed to create element - plugin not registered?");
315 auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
726122c2
ML
316 output->_libsigrok_output_format = libsigrok_output_format;
317 output->_libsigrok_device = libsigrok_device;
318 output->_options = options;
e2cfc0ef
ML
319 return output;
320}
321
726122c2 322bool LegacyOutput::start_vfunc()
e2cfc0ef 323{
726122c2
ML
324 _libsigrok_output = _libsigrok_output_format->create_output(
325 _libsigrok_device, _options);
326 return true;
e2cfc0ef
ML
327}
328
329Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
330{
331 Gst::MapInfo info;
332 buffer->map(info, Gst::MAP_READ);
726122c2 333 auto context = _libsigrok_output_format->parent();
e2cfc0ef
ML
334 auto packet = context->create_logic_packet(
335 info.get_data(), info.get_size(), 2);
336 auto result = _libsigrok_output->receive(packet);
337 cout << result;
338 buffer->unmap(info);
339 return Gst::FLOW_OK;
340}
341
342bool LegacyOutput::stop_vfunc()
343{
726122c2 344 auto context = _libsigrok_output_format->parent();
e2cfc0ef
ML
345 auto end_packet = context->create_end_packet();
346 auto result = _libsigrok_output->receive(end_packet);
347 cout << result;
348 return true;
349}
1060e9b5 350#endif
e2cfc0ef 351
45e6e688 352#ifdef HAVE_LIBSIGROKDECODE
b1322000
UH
353void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
354{
355 klass->set_metadata("sigrok legacy decoder",
356 "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
357 "Uwe Hermann");
358
359 klass->add_pad_template(Gst::PadTemplate::create(
360 "sink",
361 Gst::PAD_SINK,
362 Gst::PAD_ALWAYS,
363 Gst::Caps::create_any()));
364}
365
366bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
367{
368 Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
369 0, Gst::register_mm_type<LegacyDecoder>(
370 "sigrok_legacy_decoder"));
371 return true;
372}
373
374LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
375 Glib::ObjectBase(typeid(LegacyDecoder)),
376 Sink(gobj)
377{
378}
379
380Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
381 struct srd_session *libsigrokdecode_session, uint64_t unitsize)
382{
383 auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
384 if (!element)
385 throw runtime_error("Failed to create element - plugin not registered?");
386 auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
387 decoder->_session = libsigrokdecode_session;
388 decoder->_unitsize = unitsize;
389 return decoder;
390}
391
392struct srd_session *LegacyDecoder::libsigrokdecode_session()
393{
394 return _session;
395}
396
397Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
398{
399 Gst::MapInfo info;
400 buffer->map(info, Gst::MAP_READ);
401 uint64_t num_samples = info.get_size() / _unitsize;
402 srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
403 info.get_data(), info.get_size(), _unitsize);
404 _abs_ss += num_samples;
405 buffer->unmap(info);
406 return Gst::FLOW_OK;
407}
408
409bool LegacyDecoder::start_vfunc()
410{
411 _abs_ss = 0;
412 srd_session_start(_session);
413 return true;
414}
415
416bool LegacyDecoder::stop_vfunc()
417{
418 srd_session_terminate_reset(_session);
419 return true;
420}
45e6e688 421#endif
b1322000 422
b903bb0a 423}