]> sigrok.org Git - libsigrokflow.git/blob - src/main.cpp
tests/init: Multiple Srf::deinit() calls should throw.
[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 static bool srf_initialized_ = false;
32
33 void init()
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 void deinit()
67 {
68         srf_initialized_ = false;
69 }
70
71 Sink::Sink(GstBaseSink *gobj) :
72         Gst::BaseSink(gobj)
73 {
74 }
75
76 Device::Device(GstElement *gobj) :
77         Gst::Element(gobj)
78 {
79 }
80
81 CaptureDevice::CaptureDevice(GstElement *gobj) :
82         Device(gobj)
83 {
84 }
85
86 #ifdef HAVE_LIBSIGROKCXX
87 void 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
100 bool 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
108 LegacyCaptureDevice::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"));
113 }
114
115 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
116         shared_ptr<sigrok::HardwareDevice> libsigrok_device)
117 {
118         auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
119         if (!element)
120                 throw runtime_error("Failed to create element - plugin not registered?");
121         auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
122         device->_libsigrok_device = libsigrok_device;
123         return device;
124 }
125
126 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
127 {
128         return _libsigrok_device;
129 }
130
131 Gst::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:
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
148 void 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
177 void LegacyCaptureDevice::_run()
178 {
179         _session = _libsigrok_device->driver()->parent()->create_session();
180         _session->add_device(_libsigrok_device);
181         _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
182         _session->start();
183         _session->run();
184         _task->stop();
185 }
186
187 void 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
206 bool 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
214 LegacyInput::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
223 Glib::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
236 bool 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
247 void 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
276 Gst::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
286 bool LegacyInput::stop_vfunc()
287 {
288         _libsigrok_input->end();
289         return true;
290 }
291
292 void 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
305 bool 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
313 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
314         Glib::ObjectBase(typeid(LegacyOutput)),
315         Sink(gobj)
316 {
317 }
318
319 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
320         shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
321         shared_ptr<sigrok::Device> libsigrok_device,
322         map<string, Glib::VariantBase> options)
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);
328         output->_libsigrok_output_format = libsigrok_output_format;
329         output->_libsigrok_device = libsigrok_device;
330         output->_options = options;
331         return output;
332 }
333
334 bool LegacyOutput::start_vfunc()
335 {
336         _libsigrok_output = _libsigrok_output_format->create_output(
337                         _libsigrok_device, _options);
338         return true;
339 }
340
341 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
342 {
343         Gst::MapInfo info;
344         buffer->map(info, Gst::MAP_READ);
345         auto context = _libsigrok_output_format->parent();
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
354 bool LegacyOutput::stop_vfunc()
355 {
356         auto context = _libsigrok_output_format->parent();
357         auto end_packet = context->create_end_packet();
358         auto result = _libsigrok_output->receive(end_packet);
359         cout << result;
360         return true;
361 }
362 #endif
363
364 #ifdef HAVE_LIBSIGROKDECODE
365 void 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
378 bool 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
386 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
387         Glib::ObjectBase(typeid(LegacyDecoder)),
388         Sink(gobj)
389 {
390 }
391
392 Glib::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
404 struct srd_session *LegacyDecoder::libsigrokdecode_session()
405 {
406         return _session;
407 }
408
409 Gst::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
421 bool LegacyDecoder::start_vfunc()
422 {
423         _abs_ss = 0;
424         srd_session_start(_session);
425         return true;
426 }
427
428 bool LegacyDecoder::stop_vfunc()
429 {
430         srd_session_terminate_reset(_session);
431         return true;
432 }
433 #endif
434
435 }