]> sigrok.org Git - libsigrokflow.git/blame - src/main.cpp
Initial LegacyDecoder implementation.
[libsigrokflow.git] / src / main.cpp
CommitLineData
572e76fe
UH
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
b1322000
UH
23#include <libsigrokdecode/libsigrokdecode.h>
24
f7363af1
ML
25#include <iostream>
26
b903bb0a
UH
27namespace Srf
28{
29
f7363af1 30using namespace std;
d03b3a98 31using namespace std::placeholders;
f7363af1 32
b903bb0a
UH
33void init()
34{
6d71d36a
ML
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),
97513ff0 39 "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
e2cfc0ef
ML
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");
b1322000
UH
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");
6d71d36a
ML
50}
51
e2cfc0ef
ML
52Sink::Sink(GstBaseSink *gobj) :
53 Gst::BaseSink(gobj)
4b7d782a
ML
54{
55}
56
57Device::Device(GstElement *gobj) :
e2cfc0ef 58 Gst::Element(gobj)
4b7d782a
ML
59{
60}
61
62CaptureDevice::CaptureDevice(GstElement *gobj) :
63 Device(gobj)
64{
65}
66
6d71d36a
ML
67void 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
80bool 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
88LegacyCaptureDevice::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"));
b903bb0a
UH
93}
94
64b0db03
ML
95Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
96 shared_ptr<sigrok::HardwareDevice> libsigrok_device)
f7363af1 97{
64b0db03 98 auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
d75c9a6a
ML
99 if (!element)
100 throw runtime_error("Failed to create element - plugin not registered?");
64b0db03 101 auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
64b0db03
ML
102 device->_libsigrok_device = libsigrok_device;
103 return device;
f7363af1
ML
104}
105
d03b3a98 106shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
f7363af1 107{
64b0db03 108 return _libsigrok_device;
f7363af1
ML
109}
110
d03b3a98
ML
111Gst::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:
d03b3a98
ML
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
128void 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
157void LegacyCaptureDevice::_run()
158{
64b0db03
ML
159 _session = _libsigrok_device->driver()->parent()->create_session();
160 _session->add_device(_libsigrok_device);
d03b3a98
ML
161 _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
162 _session->start();
163 _session->run();
164 _task->stop();
165}
166
e2cfc0ef
ML
167void 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
180bool 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
188LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
189 Glib::ObjectBase(typeid(LegacyOutput)),
190 Sink(gobj)
191{
e2cfc0ef
ML
192}
193
194Glib::RefPtr<LegacyOutput>LegacyOutput::create(
726122c2
ML
195 shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
196 shared_ptr<sigrok::Device> libsigrok_device,
197 map<string, Glib::VariantBase> options)
e2cfc0ef
ML
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);
726122c2
ML
203 output->_libsigrok_output_format = libsigrok_output_format;
204 output->_libsigrok_device = libsigrok_device;
205 output->_options = options;
e2cfc0ef
ML
206 return output;
207}
208
726122c2 209bool LegacyOutput::start_vfunc()
e2cfc0ef 210{
726122c2
ML
211 _libsigrok_output = _libsigrok_output_format->create_output(
212 _libsigrok_device, _options);
213 return true;
e2cfc0ef
ML
214}
215
216Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
217{
218 Gst::MapInfo info;
219 buffer->map(info, Gst::MAP_READ);
726122c2 220 auto context = _libsigrok_output_format->parent();
e2cfc0ef
ML
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
229bool LegacyOutput::stop_vfunc()
230{
726122c2 231 auto context = _libsigrok_output_format->parent();
e2cfc0ef
ML
232 auto end_packet = context->create_end_packet();
233 auto result = _libsigrok_output->receive(end_packet);
234 cout << result;
235 return true;
236}
237
b1322000
UH
238void 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
251bool 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
259LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
260 Glib::ObjectBase(typeid(LegacyDecoder)),
261 Sink(gobj)
262{
263}
264
265Glib::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
277struct srd_session *LegacyDecoder::libsigrokdecode_session()
278{
279 return _session;
280}
281
282Gst::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
294bool LegacyDecoder::start_vfunc()
295{
296 _abs_ss = 0;
297 srd_session_start(_session);
298 return true;
299}
300
301bool LegacyDecoder::stop_vfunc()
302{
303 srd_session_terminate_reset(_session);
304 return true;
305}
306
b903bb0a 307}