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