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