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