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