]> sigrok.org Git - pulseview.git/blob - pv/sigsession.cpp
Initial threaded capture
[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 "logicdata.h"
24 #include "logicdatasnapshot.h"
25 #include "view/logicsignal.h"
26
27 #include <QDebug>
28
29 #include <assert.h>
30
31 using namespace boost;
32 using namespace std;
33
34 namespace pv {
35
36 // TODO: This should not be necessary
37 SigSession* SigSession::_session = NULL;
38
39 SigSession::SigSession()
40 {
41         // TODO: This should not be necessary
42         _session = this;
43 }
44
45 SigSession::~SigSession()
46 {
47         if(_sampling_thread.get())
48                 _sampling_thread->join();
49         _sampling_thread.reset();
50
51         // TODO: This should not be necessary
52         _session = NULL;
53 }
54
55 void SigSession::load_file(const std::string &name)
56 {
57         if (sr_session_load(name.c_str()) == SR_OK) {
58                 /* sigrok session file */
59                 sr_session_datafeed_callback_add(data_feed_in_proc);
60                 sr_session_start();
61                 sr_session_run();
62                 sr_session_stop();
63         }
64 }
65
66 void SigSession::start_capture(struct sr_dev_inst *sdi,
67         uint64_t record_length, uint64_t sample_rate)
68 {
69         // Check sampling isn't already active
70         if(_sampling_thread.get())
71                 _sampling_thread->join();
72
73         _sampling_thread.reset(new boost::thread(
74                 &SigSession::sample_thread_proc, this, sdi,
75                 record_length, sample_rate));
76 }
77
78 vector< shared_ptr<view::Signal> >& SigSession::get_signals()
79 {
80         return _signals;
81 }
82
83 boost::shared_ptr<LogicData> SigSession::get_data()
84 {
85         return _logic_data;
86 }
87
88 void SigSession::sample_thread_proc(struct sr_dev_inst *sdi,
89         uint64_t record_length, uint64_t sample_rate)
90 {
91         sr_session_new();
92         sr_session_datafeed_callback_add(data_feed_in_proc);
93
94         if (sr_session_dev_add(sdi) != SR_OK) {
95                 qDebug() << "Failed to use device.";
96                 sr_session_destroy();
97                 return;
98         }
99
100         if (sr_dev_config_set(sdi, SR_HWCAP_LIMIT_SAMPLES,
101                 &record_length) != SR_OK) {
102                 qDebug() << "Failed to configure time-based sample limit.";
103                 sr_session_destroy();
104                 return;
105         }
106
107         if (sr_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
108                 &sample_rate) != SR_OK) {
109                 qDebug() << "Failed to configure samplerate.";
110                 sr_session_destroy();
111                 return;
112         }
113
114         if (sr_session_start() != SR_OK) {
115                 qDebug() << "Failed to start session.";
116                 return;
117         }
118
119         sr_session_run();
120         sr_session_destroy();
121 }
122
123 void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
124         struct sr_datafeed_packet *packet)
125 {
126         using view::LogicSignal;
127
128         assert(sdi);
129         assert(packet);
130
131         switch (packet->type) {
132         case SR_DF_HEADER:
133         {
134                 lock_guard<mutex> lock(_data_mutex);
135                 _signals.clear();
136                 break;
137         }
138
139         case SR_DF_META_LOGIC:
140         {
141                 assert(packet->payload);
142
143                 lock_guard<mutex> lock(_data_mutex);
144
145                 const sr_datafeed_meta_logic &meta_logic =
146                         *(sr_datafeed_meta_logic*)packet->payload;
147
148                 // Create an empty LogiData for coming data snapshots
149                 _logic_data.reset(new LogicData(meta_logic));
150                 assert(_logic_data);
151                 if(!_logic_data)
152                         break;
153
154                 // Add the signals
155                 for (int i = 0; i < meta_logic.num_probes; i++)
156                 {
157                         const sr_probe *const probe =
158                                 (const sr_probe*)g_slist_nth_data(
159                                         sdi->probes, i);
160                         if(probe->enabled)
161                         {
162                                 shared_ptr<LogicSignal> signal(
163                                         new LogicSignal(probe->name,
164                                                 _logic_data,
165                                                 probe->index));
166                                 _signals.push_back(signal);
167                         }
168                 }
169
170                 break;
171         }
172
173         case SR_DF_LOGIC:
174         {
175                 lock_guard<mutex> lock(_data_mutex);
176                 assert(packet->payload);
177                 if(!_cur_logic_snapshot)
178                 {
179                         // Create a new data snapshot
180                         _cur_logic_snapshot = shared_ptr<LogicDataSnapshot>(
181                                 new LogicDataSnapshot(
182                                 *(sr_datafeed_logic*)packet->payload));
183                         _logic_data->push_snapshot(_cur_logic_snapshot);
184                 }
185                 else
186                 {
187                         // Append to the existing data snapshot
188                         _cur_logic_snapshot->append_payload(
189                                 *(sr_datafeed_logic*)packet->payload);
190                 }
191
192                 break;
193         }
194
195         case SR_DF_END:
196         {
197                 {
198                         lock_guard<mutex> lock(_data_mutex);
199                         _cur_logic_snapshot.reset();
200                 }
201                 data_updated();
202                 break;
203         }
204         }
205 }
206
207 void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
208         struct sr_datafeed_packet *packet)
209 {
210         assert(_session);
211         _session->data_feed_in(sdi, packet);
212 }
213
214 } // namespace pv