]>
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 | |
e2cfc0ef ML |
31 | Sink::Sink(GstBaseSink *gobj) : |
32 | Gst::BaseSink(gobj) | |
4b7d782a ML |
33 | { |
34 | } | |
35 | ||
36 | Device::Device(GstElement *gobj) : | |
e2cfc0ef | 37 | Gst::Element(gobj) |
4b7d782a ML |
38 | { |
39 | } | |
40 | ||
41 | CaptureDevice::CaptureDevice(GstElement *gobj) : | |
42 | Device(gobj) | |
43 | { | |
44 | } | |
45 | ||
1060e9b5 | 46 | #ifdef HAVE_LIBSIGROKCXX |
92d521e7 ML |
47 | void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass) |
48 | { | |
49 | klass->set_metadata("sigrok legacy input", | |
50 | "Transform", "Wrapper for inputs using legacy libsigrok APIs", | |
51 | "Martin Ling"); | |
52 | ||
53 | klass->add_pad_template(Gst::PadTemplate::create( | |
54 | "sink", | |
55 | Gst::PAD_SINK, | |
56 | Gst::PAD_ALWAYS, | |
57 | Gst::Caps::create_any())); | |
58 | ||
59 | klass->add_pad_template(Gst::PadTemplate::create( | |
60 | "src", | |
61 | Gst::PAD_SRC, | |
62 | Gst::PAD_ALWAYS, | |
63 | Gst::Caps::create_any())); | |
64 | } | |
65 | ||
66 | bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
67 | { | |
68 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input", | |
69 | 0, Gst::register_mm_type<LegacyInput>( | |
70 | "sigrok_legacy_input")); | |
31b3babd | 71 | |
92d521e7 ML |
72 | return true; |
73 | } | |
74 | ||
75 | LegacyInput::LegacyInput(GstElement *gobj) : | |
76 | Glib::ObjectBase(typeid(LegacyInput)), | |
77 | Gst::Element(gobj) | |
78 | { | |
b37c14d6 UH |
79 | add_pad(sink_pad_ = Gst::Pad::create(get_pad_template("sink"), "sink")); |
80 | add_pad(src_pad_ = Gst::Pad::create(get_pad_template("src"), "src")); | |
81 | sink_pad_->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain)); | |
92d521e7 ML |
82 | } |
83 | ||
84 | Glib::RefPtr<LegacyInput> LegacyInput::create( | |
85 | shared_ptr<sigrok::InputFormat> libsigrok_input_format, | |
86 | map<string, Glib::VariantBase> options) | |
87 | { | |
88 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_input"); | |
89 | if (!element) | |
90 | throw runtime_error("Failed to create element - plugin not registered?"); | |
91 | auto input = Glib::RefPtr<LegacyInput>::cast_static(element); | |
b37c14d6 UH |
92 | input->libsigrok_input_format_ = libsigrok_input_format; |
93 | input->options_ = options; | |
31b3babd | 94 | |
92d521e7 ML |
95 | return input; |
96 | } | |
97 | ||
98 | bool LegacyInput::start_vfunc() | |
99 | { | |
b37c14d6 UH |
100 | libsigrok_input_ = libsigrok_input_format_->create_input(options_); |
101 | auto context = libsigrok_input_format_->parent(); | |
102 | session_ = context->create_session(); | |
103 | session_->add_device(libsigrok_input_->device()); | |
104 | session_->add_datafeed_callback(bind(&LegacyInput::datafeed_callback_, this, _1, _2)); | |
105 | session_->start(); | |
31b3babd | 106 | |
92d521e7 ML |
107 | return true; |
108 | } | |
109 | ||
b37c14d6 | 110 | void LegacyInput::datafeed_callback_( |
92d521e7 ML |
111 | shared_ptr<sigrok::Device> device, |
112 | shared_ptr<sigrok::Packet> packet) | |
113 | { | |
31b3babd UH |
114 | (void)device; |
115 | ||
92d521e7 | 116 | switch (packet->type()->id()) { |
31b3babd UH |
117 | case SR_DF_LOGIC: { |
118 | auto logic = static_pointer_cast<sigrok::Logic>(packet->payload()); | |
119 | auto mem = Gst::Memory::create( | |
120 | Gst::MEMORY_FLAG_READONLY, | |
121 | logic->data_pointer(), | |
122 | logic->data_length(), | |
123 | 0, | |
124 | logic->data_length()); | |
125 | auto buf = Gst::Buffer::create(); | |
126 | buf->append_memory(move(mem)); | |
b37c14d6 | 127 | src_pad_->push(move(buf)); |
31b3babd UH |
128 | break; |
129 | } | |
130 | case SR_DF_END: | |
b37c14d6 UH |
131 | session_->stop(); |
132 | src_pad_->push_event(Gst::EventEos::create()); | |
31b3babd UH |
133 | break; |
134 | default: | |
135 | break; | |
92d521e7 ML |
136 | } |
137 | } | |
138 | ||
139 | Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &, | |
31b3babd | 140 | const Glib::RefPtr<Gst::Buffer> &buf) |
92d521e7 ML |
141 | { |
142 | Gst::MapInfo info; | |
143 | buf->map(info, Gst::MAP_READ); | |
b37c14d6 | 144 | libsigrok_input_->send(info.get_data(), info.get_size()); |
92d521e7 | 145 | buf->unmap(info); |
31b3babd | 146 | |
92d521e7 ML |
147 | return Gst::FLOW_OK; |
148 | } | |
149 | ||
150 | bool LegacyInput::stop_vfunc() | |
151 | { | |
b37c14d6 | 152 | libsigrok_input_->end(); |
31b3babd | 153 | |
92d521e7 ML |
154 | return true; |
155 | } | |
156 | ||
e2cfc0ef ML |
157 | void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass) |
158 | { | |
159 | klass->set_metadata("sigrok legacy output", | |
160 | "Sink", "Wrapper for outputs using legacy libsigrok APIs", | |
161 | "Martin Ling"); | |
162 | ||
163 | klass->add_pad_template(Gst::PadTemplate::create( | |
164 | "sink", | |
165 | Gst::PAD_SINK, | |
166 | Gst::PAD_ALWAYS, | |
167 | Gst::Caps::create_any())); | |
168 | } | |
169 | ||
170 | bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
171 | { | |
172 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output", | |
173 | 0, Gst::register_mm_type<LegacyOutput>( | |
174 | "sigrok_legacy_output")); | |
31b3babd | 175 | |
e2cfc0ef ML |
176 | return true; |
177 | } | |
178 | ||
179 | LegacyOutput::LegacyOutput(GstBaseSink *gobj) : | |
180 | Glib::ObjectBase(typeid(LegacyOutput)), | |
181 | Sink(gobj) | |
182 | { | |
e2cfc0ef ML |
183 | } |
184 | ||
185 | Glib::RefPtr<LegacyOutput>LegacyOutput::create( | |
726122c2 ML |
186 | shared_ptr<sigrok::OutputFormat> libsigrok_output_format, |
187 | shared_ptr<sigrok::Device> libsigrok_device, | |
188 | map<string, Glib::VariantBase> options) | |
e2cfc0ef ML |
189 | { |
190 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_output"); | |
191 | if (!element) | |
192 | throw runtime_error("Failed to create element - plugin not registered?"); | |
193 | auto output = Glib::RefPtr<LegacyOutput>::cast_static(element); | |
b37c14d6 UH |
194 | output->libsigrok_output_format_ = libsigrok_output_format; |
195 | output->libsigrok_device_ = libsigrok_device; | |
196 | output->options_ = options; | |
31b3babd | 197 | |
e2cfc0ef ML |
198 | return output; |
199 | } | |
200 | ||
726122c2 | 201 | bool LegacyOutput::start_vfunc() |
e2cfc0ef | 202 | { |
b37c14d6 UH |
203 | libsigrok_output_ = libsigrok_output_format_->create_output( |
204 | libsigrok_device_, options_); | |
31b3babd | 205 | |
726122c2 | 206 | return true; |
e2cfc0ef ML |
207 | } |
208 | ||
209 | Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer) | |
210 | { | |
211 | Gst::MapInfo info; | |
212 | buffer->map(info, Gst::MAP_READ); | |
b37c14d6 | 213 | auto context = libsigrok_output_format_->parent(); |
e2cfc0ef ML |
214 | auto packet = context->create_logic_packet( |
215 | info.get_data(), info.get_size(), 2); | |
b37c14d6 | 216 | auto result = libsigrok_output_->receive(packet); |
e2cfc0ef ML |
217 | cout << result; |
218 | buffer->unmap(info); | |
31b3babd | 219 | |
e2cfc0ef ML |
220 | return Gst::FLOW_OK; |
221 | } | |
222 | ||
223 | bool LegacyOutput::stop_vfunc() | |
224 | { | |
b37c14d6 | 225 | auto context = libsigrok_output_format_->parent(); |
e2cfc0ef | 226 | auto end_packet = context->create_end_packet(); |
b37c14d6 | 227 | auto result = libsigrok_output_->receive(end_packet); |
e2cfc0ef | 228 | cout << result; |
31b3babd | 229 | |
e2cfc0ef ML |
230 | return true; |
231 | } | |
1060e9b5 | 232 | #endif |
e2cfc0ef | 233 | |
45e6e688 | 234 | #ifdef HAVE_LIBSIGROKDECODE |
b1322000 UH |
235 | void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass) |
236 | { | |
237 | klass->set_metadata("sigrok legacy decoder", | |
238 | "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs", | |
239 | "Uwe Hermann"); | |
240 | ||
241 | klass->add_pad_template(Gst::PadTemplate::create( | |
242 | "sink", | |
243 | Gst::PAD_SINK, | |
244 | Gst::PAD_ALWAYS, | |
245 | Gst::Caps::create_any())); | |
246 | } | |
247 | ||
248 | bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
249 | { | |
250 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder", | |
251 | 0, Gst::register_mm_type<LegacyDecoder>( | |
252 | "sigrok_legacy_decoder")); | |
31b3babd | 253 | |
b1322000 UH |
254 | return true; |
255 | } | |
256 | ||
257 | LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) : | |
258 | Glib::ObjectBase(typeid(LegacyDecoder)), | |
259 | Sink(gobj) | |
260 | { | |
261 | } | |
262 | ||
263 | Glib::RefPtr<LegacyDecoder>LegacyDecoder::create( | |
264 | struct srd_session *libsigrokdecode_session, uint64_t unitsize) | |
265 | { | |
266 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder"); | |
267 | if (!element) | |
268 | throw runtime_error("Failed to create element - plugin not registered?"); | |
269 | auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element); | |
b37c14d6 UH |
270 | decoder->session_ = libsigrokdecode_session; |
271 | decoder->unitsite_ = unitsize; | |
31b3babd | 272 | |
b1322000 UH |
273 | return decoder; |
274 | } | |
275 | ||
276 | struct srd_session *LegacyDecoder::libsigrokdecode_session() | |
277 | { | |
b37c14d6 | 278 | return session_; |
b1322000 UH |
279 | } |
280 | ||
281 | Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer) | |
282 | { | |
283 | Gst::MapInfo info; | |
284 | buffer->map(info, Gst::MAP_READ); | |
b37c14d6 UH |
285 | uint64_t num_samples = info.get_size() / unitsite_; |
286 | srd_session_send(session_, abs_ss_, abs_ss_ + num_samples, | |
287 | info.get_data(), info.get_size(), unitsite_); | |
288 | abs_ss_ += num_samples; | |
b1322000 | 289 | buffer->unmap(info); |
31b3babd | 290 | |
b1322000 UH |
291 | return Gst::FLOW_OK; |
292 | } | |
293 | ||
294 | bool LegacyDecoder::start_vfunc() | |
295 | { | |
b37c14d6 UH |
296 | abs_ss_ = 0; |
297 | srd_session_start(session_); | |
31b3babd | 298 | |
b1322000 UH |
299 | return true; |
300 | } | |
301 | ||
302 | bool LegacyDecoder::stop_vfunc() | |
303 | { | |
b37c14d6 | 304 | srd_session_terminate_reset(session_); |
31b3babd | 305 | |
b1322000 UH |
306 | return true; |
307 | } | |
45e6e688 | 308 | #endif |
b1322000 | 309 | |
b903bb0a | 310 | } |