]>
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 |
e2cfc0ef ML |
47 | void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass) |
48 | { | |
49 | klass->set_metadata("sigrok legacy output", | |
50 | "Sink", "Wrapper for outputs 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 | ||
60 | bool LegacyOutput::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
61 | { | |
62 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_output", | |
63 | 0, Gst::register_mm_type<LegacyOutput>( | |
64 | "sigrok_legacy_output")); | |
31b3babd | 65 | |
e2cfc0ef ML |
66 | return true; |
67 | } | |
68 | ||
69 | LegacyOutput::LegacyOutput(GstBaseSink *gobj) : | |
70 | Glib::ObjectBase(typeid(LegacyOutput)), | |
71 | Sink(gobj) | |
72 | { | |
e2cfc0ef ML |
73 | } |
74 | ||
75 | Glib::RefPtr<LegacyOutput>LegacyOutput::create( | |
726122c2 ML |
76 | shared_ptr<sigrok::OutputFormat> libsigrok_output_format, |
77 | shared_ptr<sigrok::Device> libsigrok_device, | |
78 | map<string, Glib::VariantBase> options) | |
e2cfc0ef ML |
79 | { |
80 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_output"); | |
81 | if (!element) | |
82 | throw runtime_error("Failed to create element - plugin not registered?"); | |
83 | auto output = Glib::RefPtr<LegacyOutput>::cast_static(element); | |
b37c14d6 UH |
84 | output->libsigrok_output_format_ = libsigrok_output_format; |
85 | output->libsigrok_device_ = libsigrok_device; | |
86 | output->options_ = options; | |
31b3babd | 87 | |
e2cfc0ef ML |
88 | return output; |
89 | } | |
90 | ||
726122c2 | 91 | bool LegacyOutput::start_vfunc() |
e2cfc0ef | 92 | { |
b37c14d6 UH |
93 | libsigrok_output_ = libsigrok_output_format_->create_output( |
94 | libsigrok_device_, options_); | |
31b3babd | 95 | |
726122c2 | 96 | return true; |
e2cfc0ef ML |
97 | } |
98 | ||
99 | Gst::FlowReturn LegacyOutput::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer) | |
100 | { | |
101 | Gst::MapInfo info; | |
102 | buffer->map(info, Gst::MAP_READ); | |
b37c14d6 | 103 | auto context = libsigrok_output_format_->parent(); |
e2cfc0ef ML |
104 | auto packet = context->create_logic_packet( |
105 | info.get_data(), info.get_size(), 2); | |
b37c14d6 | 106 | auto result = libsigrok_output_->receive(packet); |
e2cfc0ef ML |
107 | cout << result; |
108 | buffer->unmap(info); | |
31b3babd | 109 | |
e2cfc0ef ML |
110 | return Gst::FLOW_OK; |
111 | } | |
112 | ||
113 | bool LegacyOutput::stop_vfunc() | |
114 | { | |
b37c14d6 | 115 | auto context = libsigrok_output_format_->parent(); |
e2cfc0ef | 116 | auto end_packet = context->create_end_packet(); |
b37c14d6 | 117 | auto result = libsigrok_output_->receive(end_packet); |
e2cfc0ef | 118 | cout << result; |
31b3babd | 119 | |
e2cfc0ef ML |
120 | return true; |
121 | } | |
1060e9b5 | 122 | #endif |
e2cfc0ef | 123 | |
45e6e688 | 124 | #ifdef HAVE_LIBSIGROKDECODE |
b1322000 UH |
125 | void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass) |
126 | { | |
127 | klass->set_metadata("sigrok legacy decoder", | |
128 | "Sink", "Wrapper for protocol decoders using legacy libsigrokdecode APIs", | |
129 | "Uwe Hermann"); | |
130 | ||
131 | klass->add_pad_template(Gst::PadTemplate::create( | |
132 | "sink", | |
133 | Gst::PAD_SINK, | |
134 | Gst::PAD_ALWAYS, | |
135 | Gst::Caps::create_any())); | |
136 | } | |
137 | ||
138 | bool LegacyDecoder::register_element(Glib::RefPtr<Gst::Plugin> plugin) | |
139 | { | |
140 | Gst::ElementFactory::register_element(plugin, "sigrok_legacy_decoder", | |
141 | 0, Gst::register_mm_type<LegacyDecoder>( | |
142 | "sigrok_legacy_decoder")); | |
31b3babd | 143 | |
b1322000 UH |
144 | return true; |
145 | } | |
146 | ||
147 | LegacyDecoder::LegacyDecoder(GstBaseSink *gobj) : | |
148 | Glib::ObjectBase(typeid(LegacyDecoder)), | |
149 | Sink(gobj) | |
150 | { | |
151 | } | |
152 | ||
153 | Glib::RefPtr<LegacyDecoder>LegacyDecoder::create( | |
154 | struct srd_session *libsigrokdecode_session, uint64_t unitsize) | |
155 | { | |
156 | auto element = Gst::ElementFactory::create_element("sigrok_legacy_decoder"); | |
157 | if (!element) | |
158 | throw runtime_error("Failed to create element - plugin not registered?"); | |
159 | auto decoder = Glib::RefPtr<LegacyDecoder>::cast_static(element); | |
b37c14d6 UH |
160 | decoder->session_ = libsigrokdecode_session; |
161 | decoder->unitsite_ = unitsize; | |
31b3babd | 162 | |
b1322000 UH |
163 | return decoder; |
164 | } | |
165 | ||
166 | struct srd_session *LegacyDecoder::libsigrokdecode_session() | |
167 | { | |
b37c14d6 | 168 | return session_; |
b1322000 UH |
169 | } |
170 | ||
171 | Gst::FlowReturn LegacyDecoder::render_vfunc(const Glib::RefPtr<Gst::Buffer> &buffer) | |
172 | { | |
173 | Gst::MapInfo info; | |
174 | buffer->map(info, Gst::MAP_READ); | |
b37c14d6 UH |
175 | uint64_t num_samples = info.get_size() / unitsite_; |
176 | srd_session_send(session_, abs_ss_, abs_ss_ + num_samples, | |
177 | info.get_data(), info.get_size(), unitsite_); | |
178 | abs_ss_ += num_samples; | |
b1322000 | 179 | buffer->unmap(info); |
31b3babd | 180 | |
b1322000 UH |
181 | return Gst::FLOW_OK; |
182 | } | |
183 | ||
184 | bool LegacyDecoder::start_vfunc() | |
185 | { | |
b37c14d6 UH |
186 | abs_ss_ = 0; |
187 | srd_session_start(session_); | |
31b3babd | 188 | |
b1322000 UH |
189 | return true; |
190 | } | |
191 | ||
192 | bool LegacyDecoder::stop_vfunc() | |
193 | { | |
b37c14d6 | 194 | srd_session_terminate_reset(session_); |
31b3babd | 195 | |
b1322000 UH |
196 | return true; |
197 | } | |
45e6e688 | 198 | #endif |
b1322000 | 199 | |
b903bb0a | 200 | } |