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