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