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