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