]>
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 | |
7491a29f | 50 | DecoderStack::DecoderStack(const srd_decoder *const dec) |
119aff65 | 51 | { |
7491a29f JH |
52 | _stack.push_back(shared_ptr<decode::Decoder>( |
53 | new decode::Decoder(dec))); | |
119aff65 JH |
54 | } |
55 | ||
6e89374a | 56 | DecoderStack::~DecoderStack() |
640d69ce JH |
57 | { |
58 | _decode_thread.interrupt(); | |
59 | _decode_thread.join(); | |
4e5a4405 JH |
60 | } |
61 | ||
7491a29f JH |
62 | const std::list< boost::shared_ptr<decode::Decoder> >& |
63 | DecoderStack::stack() const | |
4e5a4405 | 64 | { |
7491a29f | 65 | return _stack; |
4e5a4405 JH |
66 | } |
67 | ||
7491a29f | 68 | void DecoderStack::push(boost::shared_ptr<decode::Decoder> decoder) |
4e5a4405 | 69 | { |
7491a29f JH |
70 | assert(decoder); |
71 | _stack.push_back(decoder); | |
4e5a4405 JH |
72 | } |
73 | ||
613d097c JH |
74 | void DecoderStack::remove(int index) |
75 | { | |
76 | using pv::data::decode::Decoder; | |
77 | ||
78 | assert(index >= 0); | |
79 | assert(index < (int)_stack.size()); | |
80 | ||
81 | // Find the decoder in the stack | |
82 | list< shared_ptr<Decoder> >::iterator iter = _stack.begin(); | |
83 | for(int i = 0; i < index; i++, iter++) | |
84 | assert(iter != _stack.end()); | |
85 | ||
86 | // Delete the element | |
87 | _stack.erase(iter); | |
88 | } | |
89 | ||
e0fc5810 | 90 | const vector< shared_ptr<view::decode::Annotation> > |
6e89374a | 91 | DecoderStack::annotations() const |
e0fc5810 | 92 | { |
b6b267bb | 93 | lock_guard<mutex> lock(_mutex); |
e0fc5810 JH |
94 | return _annotations; |
95 | } | |
96 | ||
6e89374a | 97 | QString DecoderStack::error_message() |
b6b267bb JH |
98 | { |
99 | lock_guard<mutex> lock(_mutex); | |
100 | return _error_message; | |
101 | } | |
102 | ||
6e89374a | 103 | void DecoderStack::begin_decode() |
640d69ce | 104 | { |
7491a29f JH |
105 | shared_ptr<pv::view::LogicSignal> logic_signal; |
106 | shared_ptr<pv::data::Logic> data; | |
107 | ||
640d69ce JH |
108 | _decode_thread.interrupt(); |
109 | _decode_thread.join(); | |
110 | ||
4e5a4405 JH |
111 | _annotations.clear(); |
112 | ||
e0fc5810 JH |
113 | // We get the logic data of the first probe in the list. |
114 | // This works because we are currently assuming all | |
115 | // LogicSignals have the same data/snapshot | |
7491a29f JH |
116 | BOOST_FOREACH (const shared_ptr<decode::Decoder> &dec, _stack) |
117 | if (dec && !dec->probes().empty() && | |
118 | ((logic_signal = (*dec->probes().begin()).second)) && | |
119 | ((data = logic_signal->data()))) | |
120 | break; | |
121 | ||
122 | if (!data) | |
123 | return; | |
124 | ||
125 | // Get the samplerate and start time | |
126 | _start_time = data->get_start_time(); | |
127 | _samplerate = data->get_samplerate(); | |
128 | if (_samplerate == 0.0) | |
129 | _samplerate = 1.0; | |
e0fc5810 | 130 | |
6e89374a | 131 | _decode_thread = boost::thread(&DecoderStack::decode_proc, this, |
e0fc5810 | 132 | data); |
640d69ce JH |
133 | } |
134 | ||
6e89374a | 135 | void DecoderStack::clear_snapshots() |
640d69ce JH |
136 | { |
137 | } | |
138 | ||
6e89374a | 139 | void DecoderStack::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 140 | { |
b6b267bb | 141 | srd_session *session; |
e0fc5810 | 142 | uint8_t chunk[DecodeChunkLength]; |
7491a29f | 143 | srd_decoder_inst *prev_di = NULL; |
e0fc5810 JH |
144 | |
145 | assert(data); | |
146 | ||
e0fc5810 JH |
147 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = |
148 | data->get_snapshots(); | |
149 | if (snapshots.empty()) | |
150 | return; | |
151 | ||
152 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = | |
153 | snapshots.front(); | |
154 | const int64_t sample_count = snapshot->get_sample_count() - 1; | |
e0fc5810 | 155 | |
b6b267bb JH |
156 | // Create the session |
157 | srd_session_new(&session); | |
158 | assert(session); | |
e0fc5810 | 159 | |
e0fc5810 | 160 | |
7491a29f JH |
161 | // Create the decoders |
162 | BOOST_FOREACH(const shared_ptr<decode::Decoder> &dec, _stack) | |
163 | { | |
164 | srd_decoder_inst *const di = dec->create_decoder_inst(session); | |
e0fc5810 | 165 | |
7491a29f JH |
166 | if (!di) |
167 | { | |
168 | _error_message = tr("Failed to initialise decoder"); | |
169 | srd_session_destroy(session); | |
170 | return; | |
171 | } | |
b6b267bb | 172 | |
7491a29f JH |
173 | if (prev_di) |
174 | srd_inst_stack (session, prev_di, di); | |
b6b267bb | 175 | |
7491a29f | 176 | prev_di = di; |
b6b267bb JH |
177 | } |
178 | ||
b6b267bb | 179 | // Start the session |
7491a29f JH |
180 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, |
181 | g_variant_new_uint64((uint64_t)_samplerate)); | |
182 | ||
183 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, | |
184 | DecoderStack::annotation_callback, this); | |
185 | ||
b6b267bb JH |
186 | srd_session_start(session); |
187 | ||
e0fc5810 JH |
188 | for (int64_t i = 0; |
189 | !this_thread::interruption_requested() && i < sample_count; | |
190 | i += DecodeChunkLength) | |
191 | { | |
fe89c961 JH |
192 | lock_guard<mutex> decode_lock(_global_decode_mutex); |
193 | ||
e0fc5810 JH |
194 | const int64_t chunk_end = min( |
195 | i + DecodeChunkLength, sample_count); | |
196 | snapshot->get_samples(chunk, i, chunk_end); | |
197 | ||
f00fc2a9 BV |
198 | if (srd_session_send(session, i, i + sample_count, |
199 | chunk, chunk_end - i) != SRD_OK) { | |
b6b267bb | 200 | _error_message = tr("Failed to initialise decoder"); |
e0fc5810 | 201 | break; |
b6b267bb | 202 | } |
e0fc5810 | 203 | } |
b6b267bb JH |
204 | |
205 | // Destroy the session | |
206 | srd_session_destroy(session); | |
e0fc5810 JH |
207 | } |
208 | ||
6e89374a | 209 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 JH |
210 | { |
211 | using namespace pv::view::decode; | |
212 | ||
213 | assert(pdata); | |
214 | assert(decoder); | |
215 | ||
6e89374a | 216 | DecoderStack *const d = (DecoderStack*)decoder; |
e0fc5810 JH |
217 | |
218 | shared_ptr<Annotation> a(new Annotation(pdata)); | |
b6b267bb | 219 | lock_guard<mutex> lock(d->_mutex); |
e0fc5810 | 220 | d->_annotations.push_back(a); |
9cef9567 JH |
221 | |
222 | d->new_decode_data(); | |
119aff65 JH |
223 | } |
224 | ||
225 | } // namespace data | |
226 | } // namespace pv |