]> sigrok.org Git - pulseview.git/blob - pv/sigsession.cpp
Added UI error handling for file loading and capturing
[pulseview.git] / pv / sigsession.cpp
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
21 #include "sigsession.h"
22
23 #include "data/analog.h"
24 #include "data/analogsnapshot.h"
25 #include "data/logic.h"
26 #include "data/logicsnapshot.h"
27 #include "view/analogsignal.h"
28 #include "view/logicsignal.h"
29
30 #include <assert.h>
31
32 using namespace boost;
33 using namespace std;
34
35 namespace pv {
36
37 // TODO: This should not be necessary
38 SigSession* SigSession::_session = NULL;
39
40 SigSession::SigSession() :
41         _capture_state(Stopped)
42 {
43         // TODO: This should not be necessary
44         _session = this;
45 }
46
47 SigSession::~SigSession()
48 {
49         stop_capture();
50
51         if (_sampling_thread.get())
52                 _sampling_thread->join();
53         _sampling_thread.reset();
54
55         // TODO: This should not be necessary
56         _session = NULL;
57 }
58
59 void SigSession::load_file(const string &name,
60         function<void (const QString)> error_handler)
61 {
62         stop_capture();
63         _sampling_thread.reset(new boost::thread(
64                 &SigSession::load_thread_proc, this, name,
65                 error_handler));
66 }
67
68 SigSession::capture_state SigSession::get_capture_state() const
69 {
70         lock_guard<mutex> lock(_sampling_mutex);
71         return _capture_state;
72 }
73
74 void SigSession::start_capture(struct sr_dev_inst *sdi,
75         uint64_t record_length,
76         function<void (const QString)> error_handler)
77 {
78         stop_capture();
79
80         _sampling_thread.reset(new boost::thread(
81                 &SigSession::sample_thread_proc, this, sdi,
82                 record_length, error_handler));
83 }
84
85 void SigSession::stop_capture()
86 {
87         if (get_capture_state() == Stopped)
88                 return;
89
90         sr_session_stop();
91
92         // Check that sampling stopped
93         if (_sampling_thread.get())
94                 _sampling_thread->join();
95         _sampling_thread.reset();
96 }
97
98 vector< shared_ptr<view::Signal> > SigSession::get_signals()
99 {
100         lock_guard<mutex> lock(_signals_mutex);
101         return _signals;
102 }
103
104 boost::shared_ptr<data::Logic> SigSession::get_data()
105 {
106         return _logic_data;
107 }
108
109 void SigSession::set_capture_state(capture_state state)
110 {
111         lock_guard<mutex> lock(_sampling_mutex);
112         _capture_state = state;
113         capture_state_changed(state);
114 }
115
116 void SigSession::load_thread_proc(const string name,
117         function<void (const QString)> error_handler)
118 {
119         if (sr_session_load(name.c_str()) != SR_OK) {
120                 error_handler(tr("Failed to load file."));
121                 return;
122         }
123
124         sr_session_datafeed_callback_add(data_feed_in_proc);
125
126         if (sr_session_start() != SR_OK) {
127                 error_handler(tr("Failed to start session."));
128                 return;
129         }
130
131         set_capture_state(Running);
132
133         sr_session_run();
134         sr_session_stop();
135
136         set_capture_state(Stopped);
137 }
138
139 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
140         uint64_t record_length,
141         function<void (const QString)> error_handler)
142 {
143         assert(sdi);
144         assert(error_handler);
145
146         sr_session_new();
147         sr_session_datafeed_callback_add(data_feed_in_proc);
148
149         if (sr_session_dev_add(sdi) != SR_OK) {
150                 error_handler(tr("Failed to use device."));
151                 sr_session_destroy();
152                 return;
153         }
154
155         // Set the sample limit
156         if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
157                 &record_length) != SR_OK) {
158                 error_handler(tr("Failed to configure "
159                         "time-based sample limit."));
160                 sr_session_destroy();
161                 return;
162         }
163
164         if (sr_session_start() != SR_OK) {
165                 error_handler(tr("Failed to start session."));
166                 return;
167         }
168
169         set_capture_state(Running);
170
171         sr_session_run();
172         sr_session_destroy();
173
174         set_capture_state(Stopped);
175 }
176
177 void SigSession::feed_in_header(const sr_dev_inst *sdi)
178 {
179         shared_ptr<view::Signal> signal;
180         uint64_t *sample_rate = NULL;
181         unsigned int logic_probe_count = 0;
182         unsigned int analog_probe_count = 0;
183
184         // Detect what data types we will receive
185         for (const GSList *l = sdi->probes; l; l = l->next) {
186                 const sr_probe *const probe = (const sr_probe *)l->data;
187                 if (!probe->enabled)
188                         continue;
189
190                 switch(probe->type) {
191                 case SR_PROBE_LOGIC:
192                         logic_probe_count++;
193                         break;
194
195                 case SR_PROBE_ANALOG:
196                         analog_probe_count++;
197                         break;
198                 }
199         }
200
201         // Read out the sample rate
202         assert(sdi->driver);
203
204         const int ret = sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
205                 (const void**)&sample_rate, sdi);
206         assert(ret == SR_OK);
207
208         // Create data containers for the coming data snapshots
209         {
210                 lock_guard<mutex> data_lock(_data_mutex);
211
212                 if (logic_probe_count != 0) {
213                         _logic_data.reset(new data::Logic(
214                                 logic_probe_count, *sample_rate));
215                         assert(_logic_data);
216                 }
217
218                 if (analog_probe_count != 0) {
219                         _analog_data.reset(new data::Analog(*sample_rate));
220                         assert(_analog_data);
221                 }
222         }
223
224         // Make the logic probe list
225         {
226                 lock_guard<mutex> lock(_signals_mutex);
227
228                 _signals.clear();
229
230                 for (const GSList *l = sdi->probes; l; l = l->next) {
231                         const sr_probe *const probe =
232                                 (const sr_probe *)l->data;
233                         assert(probe);
234                         if (!probe->enabled)
235                                 continue;
236
237                         switch(probe->type) {
238                         case SR_PROBE_LOGIC:
239                                 signal = shared_ptr<view::Signal>(
240                                         new view::LogicSignal(probe->name,
241                                                 _logic_data, probe->index));
242                                 break;
243
244                         case SR_PROBE_ANALOG:
245                                 signal = shared_ptr<view::Signal>(
246                                         new view::AnalogSignal(probe->name,
247                                                 _analog_data));
248                                 break;
249                         }
250
251                         _signals.push_back(signal);
252                 }
253
254                 signals_changed();
255         }
256 }
257
258 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
259         const sr_datafeed_meta &meta)
260 {
261         (void)sdi;
262
263         for (const GSList *l = meta.config; l; l = l->next) {
264                 const sr_config *const src = (const sr_config*)l->data;
265                 switch (src->key) {
266                 case SR_CONF_SAMPLERATE:
267                         /// @todo handle samplerate changes
268                         /// samplerate = (uint64_t *)src->value;
269                         break;
270                 default:
271                         // Unknown metadata is not an error.
272                         break;
273                 }
274         }
275 }
276
277 void SigSession::feed_in_logic(const sr_datafeed_logic &logic)
278 {
279         lock_guard<mutex> lock(_data_mutex);
280         if (!_cur_logic_snapshot)
281         {
282                 assert(_logic_data);
283
284                 // Create a new data snapshot
285                 _cur_logic_snapshot = shared_ptr<data::LogicSnapshot>(
286                         new data::LogicSnapshot(logic));
287                 _logic_data->push_snapshot(_cur_logic_snapshot);
288         }
289         else
290         {
291                 // Append to the existing data snapshot
292                 _cur_logic_snapshot->append_payload(logic);
293         }
294
295         data_updated();
296 }
297
298 void SigSession::feed_in_analog(const sr_datafeed_analog &analog)
299 {
300         lock_guard<mutex> lock(_data_mutex);
301         if (!_cur_analog_snapshot)
302         {
303                 assert(_analog_data);
304
305                 // Create a new data snapshot
306                 _cur_analog_snapshot = shared_ptr<data::AnalogSnapshot>(
307                         new data::AnalogSnapshot(analog));
308                 _analog_data->push_snapshot(_cur_analog_snapshot);
309         }
310         else
311         {
312                 // Append to the existing data snapshot
313                 _cur_analog_snapshot->append_payload(analog);
314         }
315
316         data_updated();
317 }
318
319 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
320         const struct sr_datafeed_packet *packet)
321 {
322         assert(sdi);
323         assert(packet);
324
325         switch (packet->type) {
326         case SR_DF_HEADER:
327                 feed_in_header(sdi);
328                 break;
329
330         case SR_DF_META:
331                 assert(packet->payload);
332                 feed_in_meta(sdi,
333                         *(const sr_datafeed_meta*)packet->payload);
334                 break;
335
336         case SR_DF_LOGIC:
337                 assert(packet->payload);
338                 feed_in_logic(*(const sr_datafeed_logic*)packet->payload);
339                 break;
340
341         case SR_DF_ANALOG:
342                 assert(packet->payload);
343                 feed_in_analog(*(const sr_datafeed_analog*)packet->payload);
344                 break;
345
346         case SR_DF_END:
347         {
348                 {
349                         lock_guard<mutex> lock(_data_mutex);
350                         _cur_logic_snapshot.reset();
351                         _cur_analog_snapshot.reset();
352                 }
353                 data_updated();
354                 break;
355         }
356         }
357 }
358
359 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
360         const struct sr_datafeed_packet *packet)
361 {
362         assert(_session);
363         _session->data_feed_in(sdi, packet);
364 }
365
366 } // namespace pv