]> sigrok.org Git - libsigrokflow.git/blob - src/main.cpp
Initial LegacyDecoder implementation.
[libsigrokflow.git] / src / main.cpp
1 /*
2  * This file is part of the libsigrokflow project.
3  *
4  * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <libsigrokflow/libsigrokflow.hpp>
22
23 #include <libsigrokdecode/libsigrokdecode.h>
24
25 #include <iostream>
26
27 namespace Srf
28 {
29
30 using namespace std;
31 using namespace std::placeholders;
32
33 void init()
34 {
35         Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
36                         "sigrok_legacy_capture_device",
37                         "Wrapper for capture devices using legacy libsigrok APIs",
38                         sigc::ptr_fun(&LegacyCaptureDevice::register_element),
39                         "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
40         Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
41                         "sigrok_legacy_output",
42                         "Wrapper for outputs using legacy libsigrok APIs",
43                         sigc::ptr_fun(&LegacyOutput::register_element),
44                         "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
45         Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
46                         "sigrok_legacy_decoder",
47                         "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
48                         sigc::ptr_fun(&LegacyDecoder::register_element),
49                         "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
50 }
51
52 Sink::Sink(GstBaseSink *gobj) :
53         Gst::BaseSink(gobj)
54 {
55 }
56
57 Device::Device(GstElement *gobj) :
58         Gst::Element(gobj)
59 {
60 }
61
62 CaptureDevice::CaptureDevice(GstElement *gobj) :
63         Device(gobj)
64 {
65 }
66
67 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
68 {
69         klass->set_metadata("sigrok legacy capture device",
70                         "Source", "Wrapper for capture devices using legacy libsigrok APIs",
71                         "Martin Ling");
72
73         klass->add_pad_template(Gst::PadTemplate::create(
74                         "src",
75                         Gst::PAD_SRC,
76                         Gst::PAD_ALWAYS,
77                         Gst::Caps::create_any()));
78 }
79
80 bool LegacyCaptureDevice::register_element(Glib::RefPtr<Gst::Plugin> plugin)
81 {
82         Gst::ElementFactory::register_element(plugin, "sigrok_legacy_capture_device",
83                         0, Gst::register_mm_type<LegacyCaptureDevice>(
84                                 "sigrok_legacy_capture_device"));
85         return true;
86 }
87
88 LegacyCaptureDevice::LegacyCaptureDevice(GstElement *gobj) :
89         Glib::ObjectBase(typeid(LegacyCaptureDevice)),
90         CaptureDevice(gobj)
91 {
92         add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
93 }
94
95 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
96         shared_ptr<sigrok::HardwareDevice> libsigrok_device)
97 {
98         auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
99         if (!element)
100                 throw runtime_error("Failed to create element - plugin not registered?");
101         auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
102         device->_libsigrok_device = libsigrok_device;
103         return device;
104 }
105
106 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
107 {
108         return _libsigrok_device;
109 }
110
111 Gst::StateChangeReturn LegacyCaptureDevice::change_state_vfunc(Gst::StateChange transition)
112 {
113         switch (transition)
114         {
115                 case Gst::STATE_CHANGE_READY_TO_PAUSED:
116                         return Gst::StateChangeReturn::STATE_CHANGE_NO_PREROLL;
117                 case Gst::STATE_CHANGE_PAUSED_TO_PLAYING:
118                         _task = Gst::Task::create(std::bind(&LegacyCaptureDevice::_run, this));
119                         _task->set_lock(_mutex);
120                         _src_pad->set_active(true);
121                         _task->start();
122                         return Gst::STATE_CHANGE_SUCCESS;
123                 default:
124                         return Gst::STATE_CHANGE_SUCCESS;
125         }
126 }
127
128 void LegacyCaptureDevice::_datafeed_callback(
129         shared_ptr<sigrok::Device> device,
130         shared_ptr<sigrok::Packet> packet)
131 {
132         (void) device;
133         switch (packet->type()->id()) {
134                 case SR_DF_LOGIC:
135                 {
136                         auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
137                         auto mem = Gst::Memory::create(
138                                         Gst::MEMORY_FLAG_READONLY,
139                                         logic->data_pointer(),
140                                         logic->data_length(),
141                                         0,
142                                         logic->data_length());
143                         auto buf = Gst::Buffer::create();
144                         buf->append_memory(move(mem));
145                         _src_pad->push(move(buf));
146                         break;
147                 }
148                 case SR_DF_END:
149                         _session->stop();
150                         _src_pad->push_event(Gst::EventEos::create());
151                         break;
152                 default:
153                         break;
154         }
155 }
156
157 void LegacyCaptureDevice::_run()
158 {
159         _session = _libsigrok_device->driver()->parent()->create_session();
160         _session->add_device(_libsigrok_device);
161         _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
162         _session->start();
163         _session->run();
164         _task->stop();
165 }
166
167 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
168 {
169         klass->set_metadata("sigrok legacy output",
170                         "Sink", "Wrapper for outputs using legacy libsigrok APIs",
171                         "Martin Ling");
172
173         klass->add_pad_template(Gst::PadTemplate::create(
174                         "sink",
175                         Gst::PAD_SINK,
176                         Gst::PAD_ALWAYS,
177                         Gst::Caps::create_any()));
178 }
179
180 bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
181 {
182         Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output",
183                         0, Gst::register_mm_type<LegacyOutput>(
184                                 "sigrok_legacy_output"));
185         return true;
186 }
187
188 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
189         Glib::ObjectBase(typeid(LegacyOutput)),
190         Sink(gobj)
191 {
192 }
193
194 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
195         shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
196         shared_ptr<sigrok::Device> libsigrok_device,
197         map<string, Glib::VariantBase> options)
198 {
199         auto element = Gst::ElementFactory::create_element("sigrok_legacy_output");
200         if (!element)
201                 throw runtime_error("Failed to create element - plugin not registered?");
202         auto output = Glib::RefPtr<LegacyOutput>::cast_static(element);
203         output->_libsigrok_output_format = libsigrok_output_format;
204         output->_libsigrok_device = libsigrok_device;
205         output->_options = options;
206         return output;
207 }
208
209 bool LegacyOutput::start_vfunc()
210 {
211         _libsigrok_output = _libsigrok_output_format->create_output(
212                         _libsigrok_device, _options);
213         return true;
214 }
215
216 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
217 {
218         Gst::MapInfo info;
219         buffer->map(info, Gst::MAP_READ);
220         auto context = _libsigrok_output_format->parent();
221         auto packet = context->create_logic_packet(
222                         info.get_data(), info.get_size(), 2);
223         auto result = _libsigrok_output->receive(packet);
224         cout << result;
225         buffer->unmap(info);
226         return Gst::FLOW_OK;
227 }
228
229 bool LegacyOutput::stop_vfunc()
230 {
231         auto context = _libsigrok_output_format->parent();
232         auto end_packet = context->create_end_packet();
233         auto result = _libsigrok_output->receive(end_packet);
234         cout << result;
235         return true;
236 }
237
238 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
239 {
240         klass->set_metadata("sigrok legacy decoder",
241                         "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
242                         "Uwe Hermann");
243
244         klass->add_pad_template(Gst::PadTemplate::create(
245                         "sink",
246                         Gst::PAD_SINK,
247                         Gst::PAD_ALWAYS,
248                         Gst::Caps::create_any()));
249 }
250
251 bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin)
252 {
253         Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder",
254                         0, Gst::register_mm_type<LegacyDecoder>(
255                                 "sigrok_legacy_decoder"));
256         return true;
257 }
258
259 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
260         Glib::ObjectBase(typeid(LegacyDecoder)),
261         Sink(gobj)
262 {
263 }
264
265 Glib::RefPtr<LegacyDecoder>LegacyDecoder::create(
266         struct srd_session *libsigrokdecode_session, uint64_t unitsize)
267 {
268         auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder");
269         if (!element)
270                 throw runtime_error("Failed to create element - plugin not registered?");
271         auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element);
272         decoder->_session = libsigrokdecode_session;
273         decoder->_unitsize = unitsize;
274         return decoder;
275 }
276
277 struct srd_session *LegacyDecoder::libsigrokdecode_session()
278 {
279         return _session;
280 }
281
282 Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
283 {
284         Gst::MapInfo info;
285         buffer->map(info, Gst::MAP_READ);
286         uint64_t num_samples = info.get_size() / _unitsize;
287         srd_session_send(_session, _abs_ss, _abs_ss + num_samples,
288                 info.get_data(), info.get_size(), _unitsize);
289         _abs_ss += num_samples;
290         buffer->unmap(info);
291         return Gst::FLOW_OK;
292 }
293
294 bool LegacyDecoder::start_vfunc()
295 {
296         _abs_ss = 0;
297         srd_session_start(_session);
298         return true;
299 }
300
301 bool LegacyDecoder::stop_vfunc()
302 {
303         srd_session_terminate_reset(_session);
304         return true;
305 }
306
307 }