]>
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 | ||
e0fc5810 JH |
23 | #include <boost/thread/thread.hpp> |
24 | ||
e92cd4e4 JH |
25 | #include <stdexcept> |
26 | ||
b213ef09 JH |
27 | #include <QDebug> |
28 | ||
6e89374a | 29 | #include "decoderstack.h" |
119aff65 | 30 | |
e0fc5810 JH |
31 | #include <pv/data/logic.h> |
32 | #include <pv/data/logicsnapshot.h> | |
33 | #include <pv/view/logicsignal.h> | |
34 | #include <pv/view/decode/annotation.h> | |
708c5523 | 35 | |
e332f6d3 JH |
36 | using namespace boost; |
37 | using namespace std; | |
38 | ||
119aff65 JH |
39 | namespace pv { |
40 | namespace data { | |
41 | ||
6e89374a JH |
42 | const double DecoderStack::DecodeMargin = 1.0; |
43 | const double DecoderStack::DecodeThreshold = 0.2; | |
44 | const int64_t DecoderStack::DecodeChunkLength = 4096; | |
e0fc5810 | 45 | |
6e89374a | 46 | mutex DecoderStack::_global_decode_mutex; |
fe89c961 | 47 | |
6e89374a | 48 | DecoderStack::DecoderStack(const srd_decoder *const dec) : |
708c5523 | 49 | _decoder(dec), |
4e5a4405 JH |
50 | _options(g_hash_table_new_full(g_str_hash, |
51 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref)) | |
119aff65 JH |
52 | { |
53 | } | |
54 | ||
6e89374a | 55 | DecoderStack::~DecoderStack() |
640d69ce JH |
56 | { |
57 | _decode_thread.interrupt(); | |
58 | _decode_thread.join(); | |
67fe5e9c JH |
59 | |
60 | g_hash_table_destroy(_options); | |
640d69ce JH |
61 | } |
62 | ||
6e89374a | 63 | const srd_decoder* DecoderStack::decoder() const |
119aff65 JH |
64 | { |
65 | return _decoder; | |
66 | } | |
67 | ||
4e5a4405 | 68 | const map<const srd_probe*, shared_ptr<view::LogicSignal> >& |
6e89374a | 69 | DecoderStack::probes() const |
4e5a4405 JH |
70 | { |
71 | return _probes; | |
72 | } | |
73 | ||
6e89374a | 74 | void DecoderStack::set_probes(std::map<const srd_probe*, |
4e5a4405 JH |
75 | boost::shared_ptr<view::LogicSignal> > probes) |
76 | { | |
77 | _probes = probes; | |
78 | begin_decode(); | |
79 | } | |
80 | ||
6e89374a | 81 | const GHashTable* DecoderStack::options() const |
4e5a4405 JH |
82 | { |
83 | return _options; | |
84 | } | |
85 | ||
6e89374a | 86 | void DecoderStack::set_option(const char *id, GVariant *value) |
4e5a4405 JH |
87 | { |
88 | g_variant_ref(value); | |
89 | g_hash_table_replace(_options, (void*)g_strdup(id), value); | |
90 | begin_decode(); | |
91 | } | |
92 | ||
e0fc5810 | 93 | const vector< shared_ptr<view::decode::Annotation> > |
6e89374a | 94 | DecoderStack::annotations() const |
e0fc5810 | 95 | { |
b6b267bb | 96 | lock_guard<mutex> lock(_mutex); |
e0fc5810 JH |
97 | return _annotations; |
98 | } | |
99 | ||
6e89374a | 100 | QString DecoderStack::error_message() |
b6b267bb JH |
101 | { |
102 | lock_guard<mutex> lock(_mutex); | |
103 | return _error_message; | |
104 | } | |
105 | ||
6e89374a | 106 | void DecoderStack::begin_decode() |
640d69ce JH |
107 | { |
108 | _decode_thread.interrupt(); | |
109 | _decode_thread.join(); | |
110 | ||
4e5a4405 JH |
111 | _annotations.clear(); |
112 | ||
e0fc5810 JH |
113 | if (_probes.empty()) |
114 | return; | |
115 | ||
4e5a4405 JH |
116 | // Get the samplerate and start time |
117 | shared_ptr<pv::view::LogicSignal> logic_signal = | |
118 | dynamic_pointer_cast<pv::view::LogicSignal>( | |
119 | (*_probes.begin()).second); | |
120 | if (logic_signal) { | |
121 | shared_ptr<pv::data::Logic> data( | |
122 | logic_signal->data()); | |
123 | if (data) { | |
124 | _start_time = data->get_start_time(); | |
125 | _samplerate = data->get_samplerate(); | |
126 | if (_samplerate == 0.0) | |
127 | _samplerate = 1.0; | |
128 | } | |
129 | } | |
130 | ||
e0fc5810 JH |
131 | // We get the logic data of the first probe in the list. |
132 | // This works because we are currently assuming all | |
133 | // LogicSignals have the same data/snapshot | |
3045c869 | 134 | shared_ptr<pv::view::LogicSignal> sig = (*_probes.begin()).second; |
e0fc5810 | 135 | assert(sig); |
3045c869 | 136 | shared_ptr<data::Logic> data = sig->data(); |
e0fc5810 | 137 | |
6e89374a | 138 | _decode_thread = boost::thread(&DecoderStack::decode_proc, this, |
e0fc5810 | 139 | data); |
640d69ce JH |
140 | } |
141 | ||
6e89374a | 142 | void DecoderStack::clear_snapshots() |
640d69ce JH |
143 | { |
144 | } | |
145 | ||
6e89374a | 146 | void DecoderStack::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 147 | { |
b6b267bb | 148 | srd_session *session; |
e0fc5810 JH |
149 | uint8_t chunk[DecodeChunkLength]; |
150 | ||
151 | assert(data); | |
152 | ||
e0fc5810 JH |
153 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = |
154 | data->get_snapshots(); | |
155 | if (snapshots.empty()) | |
156 | return; | |
157 | ||
158 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = | |
159 | snapshots.front(); | |
160 | const int64_t sample_count = snapshot->get_sample_count() - 1; | |
e0fc5810 | 161 | |
b6b267bb JH |
162 | // Create the session |
163 | srd_session_new(&session); | |
164 | assert(session); | |
e0fc5810 | 165 | |
f00fc2a9 | 166 | srd_session_metadata_set(session, SRD_CONF_SAMPLERATE, |
b6b267bb | 167 | g_variant_new_uint64((uint64_t)_samplerate)); |
e0fc5810 | 168 | |
b6b267bb | 169 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, |
6e89374a | 170 | DecoderStack::annotation_callback, this); |
e0fc5810 | 171 | |
b6b267bb JH |
172 | // Create the decoder instance |
173 | srd_decoder_inst *const decoder_inst = srd_inst_new( | |
174 | session, _decoder->id, _options); | |
175 | if(!decoder_inst) { | |
176 | _error_message = tr("Failed to initialise decoder"); | |
177 | return; | |
178 | } | |
179 | ||
b6b267bb JH |
180 | // Setup the probes |
181 | GHashTable *const probes = g_hash_table_new_full(g_str_hash, | |
182 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
183 | ||
184 | for(map<const srd_probe*, shared_ptr<view::LogicSignal> >:: | |
185 | const_iterator i = _probes.begin(); | |
186 | i != _probes.end(); i++) | |
187 | { | |
188 | shared_ptr<view::Signal> signal((*i).second); | |
189 | GVariant *const gvar = g_variant_new_int32( | |
190 | signal->probe()->index); | |
191 | g_variant_ref_sink(gvar); | |
192 | g_hash_table_insert(probes, (*i).first->id, gvar); | |
193 | } | |
194 | ||
195 | srd_inst_probe_set_all(decoder_inst, probes); | |
196 | ||
197 | // Start the session | |
198 | srd_session_start(session); | |
199 | ||
e0fc5810 JH |
200 | for (int64_t i = 0; |
201 | !this_thread::interruption_requested() && i < sample_count; | |
202 | i += DecodeChunkLength) | |
203 | { | |
fe89c961 JH |
204 | lock_guard<mutex> decode_lock(_global_decode_mutex); |
205 | ||
e0fc5810 JH |
206 | const int64_t chunk_end = min( |
207 | i + DecodeChunkLength, sample_count); | |
208 | snapshot->get_samples(chunk, i, chunk_end); | |
209 | ||
f00fc2a9 BV |
210 | if (srd_session_send(session, i, i + sample_count, |
211 | chunk, chunk_end - i) != SRD_OK) { | |
b6b267bb | 212 | _error_message = tr("Failed to initialise decoder"); |
e0fc5810 | 213 | break; |
b6b267bb | 214 | } |
e0fc5810 | 215 | } |
b6b267bb JH |
216 | |
217 | // Destroy the session | |
218 | srd_session_destroy(session); | |
e0fc5810 JH |
219 | } |
220 | ||
6e89374a | 221 | void DecoderStack::annotation_callback(srd_proto_data *pdata, void *decoder) |
e0fc5810 JH |
222 | { |
223 | using namespace pv::view::decode; | |
224 | ||
225 | assert(pdata); | |
226 | assert(decoder); | |
227 | ||
6e89374a | 228 | DecoderStack *const d = (DecoderStack*)decoder; |
e0fc5810 JH |
229 | |
230 | shared_ptr<Annotation> a(new Annotation(pdata)); | |
b6b267bb | 231 | lock_guard<mutex> lock(d->_mutex); |
e0fc5810 | 232 | d->_annotations.push_back(a); |
9cef9567 JH |
233 | |
234 | d->new_decode_data(); | |
119aff65 JH |
235 | } |
236 | ||
237 | } // namespace data | |
238 | } // namespace pv |