]> sigrok.org Git - libsigrokflow.git/blob - src/main.cpp
Srf::init(): Throw an exception upon multiple init() calls.
[libsigrokflow.git] / src / main.cpp
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
25 namespace Srf
26 {
27
28 using namespace std;
29 using namespace std::placeholders;
30
31 void init()
32 {
33         static bool srf_initialized = false;
34
35         if (srf_initialized)
36                 throw runtime_error("libsigrokflow is already initialized");
37
38 #ifdef HAVE_LIBSIGROKCXX
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),
43                         "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
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");
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");
54 #endif
55 #ifdef HAVE_LIBSIGROKDECODE
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");
61 #endif
62
63         srf_initialized = true;
64 }
65
66 Sink::Sink(GstBaseSink *gobj) :
67         Gst::BaseSink(gobj)
68 {
69 }
70
71 Device::Device(GstElement *gobj) :
72         Gst::Element(gobj)
73 {
74 }
75
76 CaptureDevice::CaptureDevice(GstElement *gobj) :
77         Device(gobj)
78 {
79 }
80
81 #ifdef HAVE_LIBSIGROKCXX
82 void 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
95 bool 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
103 LegacyCaptureDevice::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"));
108 }
109
110 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
111         shared_ptr<sigrok::HardwareDevice> libsigrok_device)
112 {
113         auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
114         if (!element)
115                 throw runtime_error("Failed to create element - plugin not registered?");
116         auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
117         device->_libsigrok_device = libsigrok_device;
118         return device;
119 }
120
121 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
122 {
123         return _libsigrok_device;
124 }
125
126 Gst::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:
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
143 void 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
172 void LegacyCaptureDevice::_run()
173 {
174         _session = _libsigrok_device->driver()->parent()->create_session();
175         _session->add_device(_libsigrok_device);
176         _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
177         _session->start();
178         _session->run();
179         _task->stop();
180 }
181
182 void 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
201 bool 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
209 LegacyInput::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
218 Glib::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
231 bool 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
242 void 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
271 Gst::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
281 bool LegacyInput::stop_vfunc()
282 {
283         _libsigrok_input->end();
284         return true;
285 }
286
287 void 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
300 bool 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
308 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
309         Glib::ObjectBase(typeid(LegacyOutput)),
310         Sink(gobj)
311 {
312 }
313
314 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
315         shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
316         shared_ptr<sigrok::Device> libsigrok_device,
317         map<string, Glib::VariantBase> options)
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);
323         output->_libsigrok_output_format = libsigrok_output_format;
324         output->_libsigrok_device = libsigrok_device;
325         output->_options = options;
326         return output;
327 }
328
329 bool LegacyOutput::start_vfunc()
330 {
331         _libsigrok_output = _libsigrok_output_format->create_output(
332                         _libsigrok_device, _options);
333         return true;
334 }
335
336 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
337 {
338         Gst::MapInfo info;
339         buffer->map(info, Gst::MAP_READ);
340         auto context = _libsigrok_output_format->parent();
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
349 bool LegacyOutput::stop_vfunc()
350 {
351         auto context = _libsigrok_output_format->parent();
352         auto end_packet = context->create_end_packet();
353         auto result = _libsigrok_output->receive(end_packet);
354         cout << result;
355         return true;
356 }
357 #endif
358
359 #ifdef HAVE_LIBSIGROKDECODE
360 void 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
373 bool 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
381 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
382         Glib::ObjectBase(typeid(LegacyDecoder)),
383         Sink(gobj)
384 {
385 }
386
387 Glib::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
399 struct srd_session *LegacyDecoder::libsigrokdecode_session()
400 {
401         return _session;
402 }
403
404 Gst::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
416 bool LegacyDecoder::start_vfunc()
417 {
418         _abs_ss = 0;
419         srd_session_start(_session);
420         return true;
421 }
422
423 bool LegacyDecoder::stop_vfunc()
424 {
425         srd_session_terminate_reset(_session);
426         return true;
427 }
428 #endif
429
430 }