]> sigrok.org Git - libsigrokflow.git/blob - src/main.cpp
Add missing copyright lines.
[libsigrokflow.git] / src / main.cpp
1 /*
2  * This file is part of the libsigrokflow project.
3  *
4  * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li>
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
24 #include <libsigrokdecode/libsigrokdecode.h>
25
26 #include <iostream>
27
28 namespace Srf
29 {
30
31 using namespace std;
32 using namespace std::placeholders;
33
34 void init()
35 {
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),
40                         "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
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");
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");
51 }
52
53 Sink::Sink(GstBaseSink *gobj) :
54         Gst::BaseSink(gobj)
55 {
56 }
57
58 Device::Device(GstElement *gobj) :
59         Gst::Element(gobj)
60 {
61 }
62
63 CaptureDevice::CaptureDevice(GstElement *gobj) :
64         Device(gobj)
65 {
66 }
67
68 void 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
81 bool 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
89 LegacyCaptureDevice::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"));
94 }
95
96 Glib::RefPtr<LegacyCaptureDevice>LegacyCaptureDevice::create(
97         shared_ptr<sigrok::HardwareDevice> libsigrok_device)
98 {
99         auto element = Gst::ElementFactory::create_element("sigrok_legacy_capture_device");
100         if (!element)
101                 throw runtime_error("Failed to create element - plugin not registered?");
102         auto device = Glib::RefPtr<LegacyCaptureDevice>::cast_static(element);
103         device->_libsigrok_device = libsigrok_device;
104         return device;
105 }
106
107 shared_ptr<sigrok::HardwareDevice> LegacyCaptureDevice::libsigrok_device()
108 {
109         return _libsigrok_device;
110 }
111
112 Gst::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:
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
129 void 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
158 void LegacyCaptureDevice::_run()
159 {
160         _session = _libsigrok_device->driver()->parent()->create_session();
161         _session->add_device(_libsigrok_device);
162         _session->add_datafeed_callback(bind(&LegacyCaptureDevice::_datafeed_callback, this, _1, _2));
163         _session->start();
164         _session->run();
165         _task->stop();
166 }
167
168 void 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
181 bool 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
189 LegacyOutput::LegacyOutput(GstBaseSink *gobj) :
190         Glib::ObjectBase(typeid(LegacyOutput)),
191         Sink(gobj)
192 {
193 }
194
195 Glib::RefPtr<LegacyOutput>LegacyOutput::create(
196         shared_ptr<sigrok::OutputFormat> libsigrok_output_format,
197         shared_ptr<sigrok::Device> libsigrok_device,
198         map<string, Glib::VariantBase> options)
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);
204         output->_libsigrok_output_format = libsigrok_output_format;
205         output->_libsigrok_device = libsigrok_device;
206         output->_options = options;
207         return output;
208 }
209
210 bool LegacyOutput::start_vfunc()
211 {
212         _libsigrok_output = _libsigrok_output_format->create_output(
213                         _libsigrok_device, _options);
214         return true;
215 }
216
217 Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer)
218 {
219         Gst::MapInfo info;
220         buffer->map(info, Gst::MAP_READ);
221         auto context = _libsigrok_output_format->parent();
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
230 bool LegacyOutput::stop_vfunc()
231 {
232         auto context = _libsigrok_output_format->parent();
233         auto end_packet = context->create_end_packet();
234         auto result = _libsigrok_output->receive(end_packet);
235         cout << result;
236         return true;
237 }
238
239 void 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
252 bool 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
260 LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) :
261         Glib::ObjectBase(typeid(LegacyDecoder)),
262         Sink(gobj)
263 {
264 }
265
266 Glib::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
278 struct srd_session *LegacyDecoder::libsigrokdecode_session()
279 {
280         return _session;
281 }
282
283 Gst::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
295 bool LegacyDecoder::start_vfunc()
296 {
297         _abs_ss = 0;
298         srd_session_start(_session);
299         return true;
300 }
301
302 bool LegacyDecoder::stop_vfunc()
303 {
304         srd_session_terminate_reset(_session);
305         return true;
306 }
307
308 }