]> sigrok.org Git - pulseview.git/blob - pv/sigsession.cpp
5b9630631a1fe9c1e104e9f8437745c49d2c61ba
[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 <QDebug>
31
32 #include <assert.h>
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         _capture_state(Stopped)
44 {
45         // TODO: This should not be necessary
46         _session = this;
47 }
48
49 SigSession::~SigSession()
50 {
51         stop_capture();
52
53         if (_sampling_thread.get())
54                 _sampling_thread->join();
55         _sampling_thread.reset();
56
57         // TODO: This should not be necessary
58         _session = NULL;
59 }
60
61 void SigSession::load_file(const string &name)
62 {
63         stop_capture();
64         _sampling_thread.reset(new boost::thread(
65                 &SigSession::load_thread_proc, this, name));
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, uint64_t sample_rate)
76 {
77         stop_capture();
78
79         lock_guard<mutex> lock(_sampling_mutex);
80         _sample_rate = sample_rate;
81
82         _sampling_thread.reset(new boost::thread(
83                 &SigSession::sample_thread_proc, this, sdi,
84                 record_length));
85 }
86
87 void SigSession::stop_capture()
88 {
89         if (get_capture_state() == Stopped)
90                 return;
91
92         sr_session_stop();
93
94         // Check that sampling stopped
95         if (_sampling_thread.get())
96                 _sampling_thread->join();
97         _sampling_thread.reset();
98 }
99
100 vector< shared_ptr<view::Signal> > SigSession::get_signals()
101 {
102         lock_guard<mutex> lock(_signals_mutex);
103         return _signals;
104 }
105
106 boost::shared_ptr<data::Logic> SigSession::get_data()
107 {
108         return _logic_data;
109 }
110
111 void SigSession::set_capture_state(capture_state state)
112 {
113         lock_guard<mutex> lock(_sampling_mutex);
114         _capture_state = state;
115         capture_state_changed(state);
116 }
117
118 void SigSession::load_thread_proc(const string name)
119 {
120         if (sr_session_load(name.c_str()) != SR_OK) {
121                 qDebug() << "Failed to load file.";
122                 return;
123         }
124
125         sr_session_datafeed_callback_add(data_feed_in_proc);
126
127         if (sr_session_start() != SR_OK) {
128                 qDebug() << "Failed to start session.";
129                 return;
130         }
131
132         set_capture_state(Running);
133
134         sr_session_run();
135         sr_session_stop();
136
137         set_capture_state(Stopped);
138 }
139
140 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
141         uint64_t record_length)
142 {
143         assert(sdi);
144
145         sr_session_new();
146         sr_session_datafeed_callback_add(data_feed_in_proc);
147
148         if (sr_session_dev_add(sdi) != SR_OK) {
149                 qDebug() << "Failed to use device.";
150                 sr_session_destroy();
151                 return;
152         }
153
154         // Set the sample limit
155         if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES,
156                 &record_length) != SR_OK) {
157                 qDebug() << "Failed to configure time-based sample limit.";
158                 sr_session_destroy();
159                 return;
160         }
161
162         // Set the samplerate
163         {
164                 lock_guard<mutex> lock(_sampling_mutex);
165                 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
166                         &_sample_rate) != SR_OK) {
167                         qDebug() << "Failed to configure samplerate.";
168                         sr_session_destroy();
169                         return;
170                 }
171         }
172
173         if (sr_session_start() != SR_OK) {
174                 qDebug() << "Failed to start session.";
175                 return;
176         }
177
178         set_capture_state(Running);
179
180         sr_session_run();
181         sr_session_destroy();
182
183         set_capture_state(Stopped);
184 }
185
186 void SigSession::feed_in_header(const sr_dev_inst *sdi)
187 {
188         shared_ptr<view::Signal> signal;
189         unsigned int logic_probe_count = 0;
190         unsigned int analog_probe_count = 0;
191
192         // Detect what data types we will receive
193         for (const GSList *l = sdi->probes; l; l = l->next) {
194                 const sr_probe *const probe = (const sr_probe *)l->data;
195                 if (!probe->enabled)
196                         continue;
197
198                 switch(probe->type) {
199                 case SR_PROBE_LOGIC:
200                         logic_probe_count++;
201                         break;
202
203                 case SR_PROBE_ANALOG:
204                         analog_probe_count++;
205                         break;
206                 }
207         }
208
209         // Create data containers for the coming data snapshots
210         {
211                 lock_guard<mutex> data_lock(_data_mutex);
212                 lock_guard<mutex> sampling_lock(_sampling_mutex);
213
214                 if (logic_probe_count != 0) {
215                         _logic_data.reset(new data::Logic(
216                                 logic_probe_count, _sample_rate));
217                         assert(_logic_data);
218                 }
219
220                 if (analog_probe_count != 0) {
221                         _analog_data.reset(new data::Analog(_sample_rate));
222                         assert(_analog_data);
223                 }
224         }
225
226         // Make the logic probe list
227         {
228                 lock_guard<mutex> lock(_signals_mutex);
229
230                 _signals.clear();
231
232                 for (const GSList *l = sdi->probes; l; l = l->next) {
233                         const sr_probe *const probe =
234                                 (const sr_probe *)l->data;
235                         assert(probe);
236                         if (!probe->enabled)
237                                 continue;
238
239                         switch(probe->type) {
240                         case SR_PROBE_LOGIC:
241                                 signal = shared_ptr<view::Signal>(
242                                         new view::LogicSignal(probe->name,
243                                                 _logic_data, probe->index));
244                                 break;
245
246                         case SR_PROBE_ANALOG:
247                                 signal = shared_ptr<view::Signal>(
248                                         new view::AnalogSignal(probe->name,
249                                                 _analog_data));
250                                 break;
251                         }
252
253                         _signals.push_back(signal);
254                 }
255
256                 signals_changed();
257         }
258 }
259
260 void SigSession::feed_in_meta(const sr_dev_inst *sdi,
261         const sr_datafeed_meta &meta)
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