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