]>
Commit | Line | Data |
---|---|---|
119aff65 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
e332f6d3 JH |
21 | #include <libsigrokdecode/libsigrokdecode.h> |
22 | ||
7491a29f | 23 | #include <boost/foreach.hpp> |
e0fc5810 JH |
24 | #include <boost/thread/thread.hpp> |
25 | ||
e92cd4e4 JH |
26 | #include <stdexcept> |
27 | ||
b213ef09 JH |
28 | #include <QDebug> |
29 | ||
6e89374a | 30 | #include "decoderstack.h" |
119aff65 | 31 | |
e0fc5810 JH |
32 | #include <pv/data/logic.h> |
33 | #include <pv/data/logicsnapshot.h> | |
7491a29f | 34 | #include <pv/data/decode/decoder.h> |
e0fc5810 JH |
35 | #include <pv/view/logicsignal.h> |
36 | #include <pv/view/decode/annotation.h> | |
708c5523 | 37 | |
e332f6d3 JH |
38 | using namespace boost; |
39 | using namespace std; | |
40 | ||
119aff65 JH |
41 | namespace pv { |
42 | namespace data { | |
43 | ||
6e89374a JH |
44 | const double DecoderStack::DecodeMargin = 1.0; |
45 | const double DecoderStack::DecodeThreshold = 0.2; | |
46 | const int64_t DecoderStack::DecodeChunkLength = 4096; | |
e0fc5810 | 47 | |
6e89374a | 48 | mutex DecoderStack::_global_decode_mutex; |
fe89c961 | 49 | |
5dfeb70f JH |
50 | DecoderStack::DecoderStack(const srd_decoder *const dec) : |
51 | _samples_decoded(0) | |
119aff65 | 52 | { |
7491a29f JH |
53 | _stack.push_back(shared_ptr<decode::Decoder>( |
54 | new decode::Decoder(dec))); | |
119aff65 JH |
55 | } |
56 | ||
6e89374a | 57 | DecoderStack::~DecoderStack() |
640d69ce JH |
58 | { |
59 | _decode_thread.interrupt(); | |
60 | _decode_thread.join(); | |
4e5a4405 JH |
61 | } |
62 | ||
7491a29f JH |
63 | const std::list< boost::shared_ptr<decode::Decoder> >& |
64 | DecoderStack::stack() const | |
4e5a4405 | 65 | { |
7491a29f | 66 | return _stack; |
4e5a4405 JH |
67 | } |
68 | ||
7491a29f | 69 | void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder) |
4e5a4405 | 70 | { |
7491a29f JH |
71 | assert(decoder); |
72 | _stack.push_back(decoder); | |
4e5a4405 JH |
73 | } |
74 | ||
613d097c JH |
75 | void DecoderStack::remove(int index) |
76 | { | |
77 | using pv::data::decode::Decoder; | |
78 | ||
79 | assert(index >= 0); | |
80 | assert(index < (int)_stack.size()); | |
81 | ||
82 | // Find the decoder in the stack | |
83 | list< shared_ptr<Decoder> >::iterator iter = _stack.begin(); | |
84 | for(int i = 0; i < index; i++, iter++) | |
85 | assert(iter != _stack.end()); | |
86 | ||
87 | // Delete the element | |
88 | _stack.erase(iter); | |
89 | } | |
90 | ||
5dfeb70f JH |
91 | int64_t DecoderStack::samples_decoded() const |
92 | { | |
93 | lock_guard<mutex> decode_lock(_mutex); | |
94 | return _samples_decoded; | |
95 | } | |
96 | ||
e0fc5810 | 97 | const vector< shared_ptr<view::decode::Annotation> > |
6e89374a | 98 | DecoderStack::annotations() const |
e0fc5810 | 99 | { |
b6b267bb | 100 | lock_guard<mutex> lock(_mutex); |
e0fc5810 JH |
101 | return _annotations; |
102 | } | |
103 | ||
6e89374a | 104 | QString DecoderStack::error_message() |
b6b267bb JH |
105 | { |
106 | lock_guard<mutex> lock(_mutex); | |
107 | return _error_message; | |
108 | } | |
109 | ||
6e89374a | 110 | void DecoderStack::begin_decode() |
640d69ce | 111 | { |
7491a29f JH |
112 | shared_ptr<pv::view::LogicSignal> logic_signal; |
113 | shared_ptr<pv::data::Logic> data; | |
114 | ||
640d69ce JH |
115 | _decode_thread.interrupt(); |
116 | _decode_thread.join(); | |
117 | ||
5dfeb70f JH |
118 | _samples_decoded = 0; |
119 | ||
4e5a4405 JH |
120 | _annotations.clear(); |
121 | ||
e0fc5810 JH |
122 | // We get the logic data of the first probe in the list. |
123 | // This works because we are currently assuming all | |
124 | // LogicSignals have the same data/snapshot | |
7491a29f JH |
125 | BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack) |
126 | if (dec && !dec->probes().empty() && | |
127 | ((logic_signal = (*dec->probes().begin()).second)) && | |
7aa09b00 | 128 | ((data = logic_signal->logic_data()))) |
7491a29f JH |
129 | break; |
130 | ||
131 | if (!data) | |
132 | return; | |
133 | ||
134 | // Get the samplerate and start time | |
135 | _start_time = data->get_start_time(); | |
136 | _samplerate = data->get_samplerate(); | |
137 | if (_samplerate == 0.0) | |
138 | _samplerate = 1.0; | |
e0fc5810 | 139 | |
6e89374a | 140 | _decode_thread = boost::thread(&DecoderStack::decode_proc, this, |
e0fc5810 | 141 | data); |
640d69ce JH |
142 | } |
143 | ||
6e89374a | 144 | void DecoderStack::clear_snapshots() |
640d69ce JH |
145 | { |
146 | } | |
147 | ||
6e89374a | 148 | void DecoderStack::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 149 | { |
b6b267bb | 150 | srd_session *session; |
e0fc5810 | 151 | uint8_t chunk[DecodeChunkLength]; |
7491a29f | 152 | srd_decoder_inst *prev_di = NULL; |
e0fc5810 JH |
153 | |
154 | assert(data); | |
155 | ||
e0fc5810 JH |
156 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = |
157 | data->get_snapshots(); | |
158 | if (snapshots.empty()) | |
159 | return; | |
160 | ||
161 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = | |
162 | snapshots.front(); | |
163 | const int64_t sample_count = snapshot->get_sample_count() - 1; | |
e0fc5810 | 164 | |
b6b267bb JH |
165 | // Create the session |
166 | srd_session_new(&session); | |
167 | assert(session); | |
e0fc5810 | 168 | |
e0fc5810 | 169 | |
7491a29f JH |
170 | // Create the decoders |
171 | BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack) | |
172 | { | |
173 | srd_decoder_inst *const di = dec->create_decoder_inst(session); | |
e0fc5810 | 174 | |
7491a29f JH |
175 | if (!di) |
176 | { | |
177 | _error_message = tr("Failed to initialise decoder"); | |
178 | srd_session_destroy(session); | |
179 | return; | |
180 | } | |
b6b267bb | 181 | |
7491a29f JH |
182 | if (prev_di) |
183 | srd_inst_stack (session, prev_di, di); | |
b6b267bb | 184 | |
7491a29f | 185 | prev_di = di; |
b6b267bb JH |
186 | } |
187 | ||
b6b267bb | 188 | // Start the session |
7491a29f JH |
189 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, |
190 | g_variant_new_uint64((uint64_t)_samplerate)); | |
191 | ||
192 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, | |
193 | DecoderStack::annotation_callback, this); | |
194 | ||
b6b267bb JH |
195 | srd_session_start(session); |
196 | ||
e0fc5810 JH |
197 | for (int64_t i = 0; |
198 | !this_thread::interruption_requested() && i < sample_count; | |
199 | i += DecodeChunkLength) | |
200 | { | |
fe89c961 JH |
201 | lock_guard<mutex> decode_lock(_global_decode_mutex); |
202 | ||
e0fc5810 JH |
203 | const int64_t chunk_end = min( |
204 | i + DecodeChunkLength, sample_count); | |
205 | snapshot->get_samples(chunk, i, chunk_end); | |
206 | ||
f00fc2a9 BV |
207 | if (srd_session_send(session, i, i + sample_count, |
208 | chunk, chunk_end - i) != SRD_OK) { | |
b6b267bb | 209 | _error_message = tr("Failed to initialise decoder"); |
e0fc5810 | 210 | break; |
b6b267bb | 211 | } |
5dfeb70f JH |
212 | |
213 | { | |
214 | lock_guard<mutex> lock(_mutex); | |
215 | _samples_decoded = chunk_end; | |
216 | } | |
e0fc5810 | 217 | } |
b6b267bb JH |
218 | |
219 | // Destroy the session | |
220 | srd_session_destroy(session); | |
e0fc5810 JH |
221 | } |
222 | ||
6e89374a | 223 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 JH |
224 | { |
225 | using namespace pv::view::decode; | |
226 | ||
227 | assert(pdata); | |
228 | assert(decoder); | |
229 | ||
6e89374a | 230 | DecoderStack *const d = (DecoderStack*)decoder; |
e0fc5810 JH |
231 | |
232 | shared_ptr<Annotation> a(new Annotation(pdata)); | |
b6b267bb | 233 | lock_guard<mutex> lock(d->_mutex); |
e0fc5810 | 234 | d->_annotations.push_back(a); |
9cef9567 JH |
235 | |
236 | d->new_decode_data(); | |
119aff65 JH |
237 | } |
238 | ||
239 | } // namespace data | |
240 | } // namespace pv |