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