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