]>
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 | ||
119aff65 JH |
29 | #include "decoder.h" |
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 | ||
e0fc5810 JH |
42 | const double Decoder::DecodeMargin = 1.0; |
43 | const double Decoder::DecodeThreshold = 0.2; | |
44 | const int64_t Decoder::DecodeChunkLength = 4096; | |
45 | ||
708c5523 JH |
46 | Decoder::Decoder(const srd_decoder *const dec, |
47 | std::map<const srd_probe*, | |
3045c869 | 48 | boost::shared_ptr<pv::view::LogicSignal> > probes, |
67fe5e9c | 49 | GHashTable *options) : |
708c5523 | 50 | _decoder(dec), |
e332f6d3 | 51 | _probes(probes), |
b6b267bb | 52 | _options(options) |
119aff65 | 53 | { |
b6b267bb | 54 | init_decoder(); |
e92cd4e4 | 55 | |
e0fc5810 | 56 | begin_decode(); |
119aff65 JH |
57 | } |
58 | ||
640d69ce JH |
59 | Decoder::~Decoder() |
60 | { | |
61 | _decode_thread.interrupt(); | |
62 | _decode_thread.join(); | |
67fe5e9c JH |
63 | |
64 | g_hash_table_destroy(_options); | |
640d69ce JH |
65 | } |
66 | ||
119aff65 JH |
67 | const srd_decoder* Decoder::get_decoder() const |
68 | { | |
69 | return _decoder; | |
70 | } | |
71 | ||
e0fc5810 JH |
72 | const vector< shared_ptr<view::decode::Annotation> > |
73 | Decoder::annotations() const | |
74 | { | |
b6b267bb | 75 | lock_guard<mutex> lock(_mutex); |
e0fc5810 JH |
76 | return _annotations; |
77 | } | |
78 | ||
b6b267bb JH |
79 | QString Decoder::error_message() |
80 | { | |
81 | lock_guard<mutex> lock(_mutex); | |
82 | return _error_message; | |
83 | } | |
84 | ||
640d69ce JH |
85 | void Decoder::begin_decode() |
86 | { | |
87 | _decode_thread.interrupt(); | |
88 | _decode_thread.join(); | |
89 | ||
e0fc5810 JH |
90 | if (_probes.empty()) |
91 | return; | |
92 | ||
93 | // We get the logic data of the first probe in the list. | |
94 | // This works because we are currently assuming all | |
95 | // LogicSignals have the same data/snapshot | |
3045c869 | 96 | shared_ptr<pv::view::LogicSignal> sig = (*_probes.begin()).second; |
e0fc5810 | 97 | assert(sig); |
3045c869 | 98 | shared_ptr<data::Logic> data = sig->data(); |
e0fc5810 | 99 | |
640d69ce | 100 | _decode_thread = boost::thread(&Decoder::decode_proc, this, |
e0fc5810 | 101 | data); |
640d69ce JH |
102 | } |
103 | ||
104 | void Decoder::clear_snapshots() | |
105 | { | |
106 | } | |
107 | ||
b6b267bb | 108 | void Decoder::init_decoder() |
e332f6d3 | 109 | { |
e0fc5810 JH |
110 | if (!_probes.empty()) |
111 | { | |
112 | shared_ptr<pv::view::LogicSignal> logic_signal = | |
113 | dynamic_pointer_cast<pv::view::LogicSignal>( | |
114 | (*_probes.begin()).second); | |
115 | if (logic_signal) { | |
116 | shared_ptr<pv::data::Logic> data( | |
117 | logic_signal->data()); | |
118 | if (data) { | |
e0fc5810 | 119 | _start_time = data->get_start_time(); |
b6b267bb JH |
120 | _samplerate = data->get_samplerate(); |
121 | if (_samplerate == 0.0) | |
122 | _samplerate = 1.0; | |
e0fc5810 JH |
123 | } |
124 | } | |
125 | } | |
e332f6d3 JH |
126 | } |
127 | ||
640d69ce | 128 | void Decoder::decode_proc(shared_ptr<data::Logic> data) |
119aff65 | 129 | { |
b6b267bb | 130 | srd_session *session; |
e0fc5810 JH |
131 | uint8_t chunk[DecodeChunkLength]; |
132 | ||
133 | assert(data); | |
134 | ||
135 | _annotations.clear(); | |
136 | ||
137 | const deque< shared_ptr<pv::data::LogicSnapshot> > &snapshots = | |
138 | data->get_snapshots(); | |
139 | if (snapshots.empty()) | |
140 | return; | |
141 | ||
142 | const shared_ptr<pv::data::LogicSnapshot> &snapshot = | |
143 | snapshots.front(); | |
144 | const int64_t sample_count = snapshot->get_sample_count() - 1; | |
e0fc5810 | 145 | |
b6b267bb JH |
146 | // Create the session |
147 | srd_session_new(&session); | |
148 | assert(session); | |
e0fc5810 | 149 | |
b6b267bb | 150 | srd_session_config_set(session, SRD_CONF_NUM_PROBES, |
9d20a741 | 151 | g_variant_new_uint64(_probes.size())); |
b6b267bb | 152 | srd_session_config_set(session, SRD_CONF_UNITSIZE, |
9d20a741 | 153 | g_variant_new_uint64(snapshot->unit_size())); |
b6b267bb JH |
154 | srd_session_config_set(session, SRD_CONF_SAMPLERATE, |
155 | g_variant_new_uint64((uint64_t)_samplerate)); | |
e0fc5810 | 156 | |
b6b267bb | 157 | srd_pd_output_callback_add(session, SRD_OUTPUT_ANN, |
e0fc5810 JH |
158 | Decoder::annotation_callback, this); |
159 | ||
b6b267bb JH |
160 | // Create the decoder instance |
161 | srd_decoder_inst *const decoder_inst = srd_inst_new( | |
162 | session, _decoder->id, _options); | |
163 | if(!decoder_inst) { | |
164 | _error_message = tr("Failed to initialise decoder"); | |
165 | return; | |
166 | } | |
167 | ||
168 | decoder_inst->data_samplerate = _samplerate; | |
169 | ||
170 | // Setup the probes | |
171 | GHashTable *const probes = g_hash_table_new_full(g_str_hash, | |
172 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
173 | ||
174 | for(map<const srd_probe*, shared_ptr<view::LogicSignal> >:: | |
175 | const_iterator i = _probes.begin(); | |
176 | i != _probes.end(); i++) | |
177 | { | |
178 | shared_ptr<view::Signal> signal((*i).second); | |
179 | GVariant *const gvar = g_variant_new_int32( | |
180 | signal->probe()->index); | |
181 | g_variant_ref_sink(gvar); | |
182 | g_hash_table_insert(probes, (*i).first->id, gvar); | |
183 | } | |
184 | ||
185 | srd_inst_probe_set_all(decoder_inst, probes); | |
186 | ||
187 | // Start the session | |
188 | srd_session_start(session); | |
189 | ||
e0fc5810 JH |
190 | for (int64_t i = 0; |
191 | !this_thread::interruption_requested() && i < sample_count; | |
192 | i += DecodeChunkLength) | |
193 | { | |
194 | const int64_t chunk_end = min( | |
195 | i + DecodeChunkLength, sample_count); | |
196 | snapshot->get_samples(chunk, i, chunk_end); | |
197 | ||
b6b267bb JH |
198 | if (srd_session_send(session, i, chunk, chunk_end - i) != |
199 | SRD_OK) { | |
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 | ||
209 | void Decoder::annotation_callback(srd_proto_data *pdata, void *decoder) | |
210 | { | |
211 | using namespace pv::view::decode; | |
212 | ||
213 | assert(pdata); | |
214 | assert(decoder); | |
215 | ||
216 | Decoder *const d = (Decoder*)decoder; | |
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 |