]> sigrok.org Git - libsigrokflow.git/blame_incremental - src/main.cpp
Various whitespace/consistency/cosmetic fixes.
[libsigrokflow.git] / src / main.cpp
... / ...
CommitLineData
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
25namespace Srf
26{
27
28using namespace std;
29using namespace std::placeholders;
30
31static bool srf_initialized_ = false;
32
33void init()
34{
35 if (srf_initialized_)
36 throw runtime_error("libsigrokflow is already initialized");
37
38 if (!Gst::is_initialized())
39 throw runtime_error("Gst::init() has not run yet");
40
41#ifdef HAVE_LIBSIGROKCXX
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),
46 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
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");
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");
57#endif
58#ifdef HAVE_LIBSIGROKDECODE
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");
64#endif
65
66 srf_initialized_ = true;
67}
68
69void deinit()
70{
71 if (!srf_initialized_)
72 throw runtime_error("libsigrokflow is not initialized");
73
74 srf_initialized_ = false;
75}
76
77Sink::Sink(GstBaseSink *gobj) :
78 Gst::BaseSink(gobj)
79{
80}
81
82Device::Device(GstElement *gobj) :
83 Gst::Element(gobj)
84{
85}
86
87CaptureDevice::CaptureDevice(GstElement *gobj) :
88 Device(gobj)
89{
90}
91
92#ifdef HAVE_LIBSIGROKCXX
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"));
111
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"));
120}
121
122Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
123 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
124{
125 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
126 if (!element)
127 throw runtime_error("Failed to create element - plugin not registered?");
128 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
129 device->_libsigrok_device = libsigrok_device;
130
131 return device;
132}
133
134shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
135{
136 return _libsigrok_device;
137}
138
139Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
140{
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;
152 }
153}
154
155void LegacyCaptureDevice::_datafeed_callback(
156 shared_ptr<sigrok::Device> device,
157 shared_ptr<sigrok::Packet> packet)
158{
159 (void)device;
160
161 switch (packet->type()->id()) {
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;
181 }
182}
183
184void LegacyCaptureDevice::_run()
185{
186 _session = _libsigrok_device->driver()->parent()->create_session();
187 _session->add_device(_libsigrok_device);
188 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
189 _session->start();
190 _session->run();
191 _task->stop();
192}
193
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"));
218
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;
241
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();
253
254 return true;
255}
256
257void LegacyInput::_datafeed_callback(
258 shared_ptr<sigrok::Device> device,
259 shared_ptr<sigrok::Packet> packet)
260{
261 (void)device;
262
263 switch (packet->type()->id()) {
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;
283 }
284}
285
286Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
287 const Glib::RefPtr<Gst::Buffer> &buf)
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);
293
294 return Gst::FLOW_OK;
295}
296
297bool LegacyInput::stop_vfunc()
298{
299 _libsigrok_input->end();
300
301 return true;
302}
303
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"));
322
323 return true;
324}
325
326LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
327 Glib::ObjectBase(typeid(LegacyOutput)),
328 Sink(gobj)
329{
330}
331
332Glib::RefPtr<LegacyOutput>LegacyOutput::create(
333 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
334 shared_ptr<sigrok::Device> libsigrok_device,
335 map<string, Glib::VariantBase> options)
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);
341 output->_libsigrok_output_format = libsigrok_output_format;
342 output->_libsigrok_device = libsigrok_device;
343 output->_options = options;
344
345 return output;
346}
347
348bool LegacyOutput::start_vfunc()
349{
350 _libsigrok_output = _libsigrok_output_format->create_output(
351 _libsigrok_device, _options);
352
353 return true;
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);
360 auto context = _libsigrok_output_format->parent();
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);
366
367 return Gst::FLOW_OK;
368}
369
370bool LegacyOutput::stop_vfunc()
371{
372 auto context = _libsigrok_output_format->parent();
373 auto end_packet = context->create_end_packet();
374 auto result = _libsigrok_output->receive(end_packet);
375 cout << result;
376
377 return true;
378}
379#endif
380
381#ifdef HAVE_LIBSIGROKDECODE
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"));
400
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;
419
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);
437
438 return Gst::FLOW_OK;
439}
440
441bool LegacyDecoder::start_vfunc()
442{
443 _abs_ss = 0;
444 srd_session_start(_session);
445
446 return true;
447}
448
449bool LegacyDecoder::stop_vfunc()
450{
451 srd_session_terminate_reset(_session);
452
453 return true;
454}
455#endif
456
457}