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