]> sigrok.org Git - libsigrok.git/blame - src/session_driver.c
scpi-dmm: model and MQ dependent delay when switching functions
[libsigrok.git] / src / session_driver.c
CommitLineData
7d658874 1/*
50985c20 2 * This file is part of the libsigrok project.
7d658874 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7d658874
BV
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
7d658874
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <sys/time.h>
26#include <zip.h>
c1aae900 27#include <libsigrok/libsigrok.h>
45c59c8b 28#include "libsigrok-internal.h"
7d658874 29
3544f848 30#define LOG_PREFIX "virtual-session"
a885ce3e 31
7d658874 32/* size of payloads sent across the session bus */
b4bd7088 33/** @cond PRIVATE */
df3e608a 34#define CHUNKSIZE (4 * 1024 * 1024)
b4bd7088 35/** @endcond */
7d658874 36
102f1239 37SR_PRIV struct sr_dev_driver session_driver_info;
102f1239 38
bb7ef793 39struct session_vdev {
40dda2c3 40 char *sessionfile;
7d658874
BV
41 char *capturefile;
42 struct zip *archive;
43 struct zip_file *capfile;
cb1e389c 44 int bytes_read;
7d658874
BV
45 uint64_t samplerate;
46 int unitsize;
6fb5570b 47 int num_logic_channels;
b317d1bb 48 int num_analog_channels;
906921ba 49 int cur_analog_channel;
2dc40815 50 GArray *analog_channels;
f438e0c9 51 int cur_chunk;
aa48adf9 52 gboolean finished;
7d658874
BV
53};
54
f254bc4b 55static const uint32_t devopts[] = {
68ac991d
BV
56 SR_CONF_CAPTUREFILE | SR_CONF_SET,
57 SR_CONF_CAPTURE_UNITSIZE | SR_CONF_GET | SR_CONF_SET,
02e49ae5 58 SR_CONF_NUM_LOGIC_CHANNELS | SR_CONF_SET,
b317d1bb 59 SR_CONF_NUM_ANALOG_CHANNELS | SR_CONF_SET,
68ac991d 60 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET,
02e49ae5 61 SR_CONF_SESSIONFILE | SR_CONF_SET,
7d658874
BV
62};
63
8d4097ff 64static gboolean stream_session_data(struct sr_dev_inst *sdi)
7d658874 65{
bb7ef793 66 struct session_vdev *vdev;
7d658874 67 struct sr_datafeed_packet packet;
9c939c51 68 struct sr_datafeed_logic logic;
6cdb1da6
UH
69 struct sr_datafeed_analog analog;
70 struct sr_analog_encoding encoding;
71 struct sr_analog_meaning meaning;
72 struct sr_analog_spec spec;
f438e0c9 73 struct zip_stat zs;
7d658874 74 int ret, got_data;
567fe538 75 char capturefile[128];
f438e0c9 76 void *buf;
7d658874 77
7d658874 78 got_data = FALSE;
102f1239 79 vdev = sdi->priv;
7c69b528 80
8d4097ff
DE
81 if (!vdev->capfile) {
82 /* No capture file opened yet, or finished with the last
83 * chunked one. */
7c69b528 84 if (vdev->capturefile && (vdev->cur_chunk == 0)) {
8d4097ff
DE
85 /* capturefile is always the unchunked base name. */
86 if (zip_stat(vdev->archive, vdev->capturefile, 0, &zs) != -1) {
87 /* No chunks, just a single capture file. */
88 vdev->cur_chunk = 0;
89 if (!(vdev->capfile = zip_fopen(vdev->archive,
90 vdev->capturefile, 0)))
91 return FALSE;
92 sr_dbg("Opened %s.", vdev->capturefile);
f438e0c9 93 } else {
8d4097ff 94 /* Try as first chunk filename. */
00ed77f2 95 snprintf(capturefile, sizeof(capturefile) - 1, "%s-1", vdev->capturefile);
f438e0c9 96 if (zip_stat(vdev->archive, capturefile, 0, &zs) != -1) {
8d4097ff 97 vdev->cur_chunk = 1;
f438e0c9
BV
98 if (!(vdev->capfile = zip_fopen(vdev->archive,
99 capturefile, 0)))
100 return FALSE;
101 sr_dbg("Opened %s.", capturefile);
102 } else {
8d4097ff
DE
103 sr_err("No capture file '%s' in " "session file '%s'.",
104 vdev->capturefile, vdev->sessionfile);
105 return FALSE;
f438e0c9
BV
106 }
107 }
7d658874 108 } else {
8d4097ff
DE
109 /* Capture data is chunked, advance to the next chunk. */
110 vdev->cur_chunk++;
00ed77f2 111 snprintf(capturefile, sizeof(capturefile) - 1, "%s-%d", vdev->capturefile,
8d4097ff
DE
112 vdev->cur_chunk);
113 if (zip_stat(vdev->archive, capturefile, 0, &zs) != -1) {
114 if (!(vdev->capfile = zip_fopen(vdev->archive,
115 capturefile, 0)))
116 return FALSE;
117 sr_dbg("Opened %s.", capturefile);
906921ba
ML
118 } else if (vdev->cur_analog_channel < vdev->num_analog_channels) {
119 vdev->capturefile = g_strdup_printf("analog-1-%d",
6fb5570b 120 vdev->num_logic_channels + vdev->cur_analog_channel + 1);
906921ba
ML
121 vdev->cur_analog_channel++;
122 vdev->cur_chunk = 0;
123 return TRUE;
f438e0c9 124 } else {
8d4097ff 125 /* We got all the chunks, finish up. */
b7939d7c 126 g_free(vdev->capturefile);
16a1d50a
SA
127
128 /* If the file has logic channels, the initial value for
129 * capturefile is set by stream_session_data() - however only
130 * once. In order to not mess this mechanism up, we simulate
131 * this here if needed. For purely analog files, capturefile
132 * is not set.
133 */
134 if (vdev->num_logic_channels)
135 vdev->capturefile = g_strdup("logic-1");
136 else
137 vdev->capturefile = NULL;
8d4097ff 138 return FALSE;
f438e0c9 139 }
7d658874
BV
140 }
141 }
142
8d4097ff
DE
143 buf = g_malloc(CHUNKSIZE);
144
9cfc695f
SA
145 /* unitsize is not defined for purely analog session files. */
146 if (vdev->unitsize)
147 ret = zip_fread(vdev->capfile, buf,
148 CHUNKSIZE / vdev->unitsize * vdev->unitsize);
149 else
150 ret = zip_fread(vdev->capfile, buf, CHUNKSIZE);
151
8d4097ff 152 if (ret > 0) {
2dc40815 153 if (vdev->cur_analog_channel != 0) {
7ee8511b 154 got_data = TRUE;
6cdb1da6 155 packet.type = SR_DF_ANALOG;
2dc40815 156 packet.payload = &analog;
7dcaddd3
UH
157 /* TODO: Use proper 'digits' value for this device (and its modes). */
158 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
6cdb1da6 159 analog.meaning->channels = g_slist_prepend(NULL,
2dc40815
ML
160 g_array_index(vdev->analog_channels,
161 struct sr_channel *, vdev->cur_analog_channel - 1));
162 analog.num_samples = ret / sizeof(float);
6cdb1da6
UH
163 analog.meaning->mq = SR_MQ_VOLTAGE;
164 analog.meaning->unit = SR_UNIT_VOLT;
165 analog.meaning->mqflags = SR_MQFLAG_DC;
2dc40815 166 analog.data = (float *) buf;
7ee8511b
GS
167 } else if (vdev->unitsize) {
168 got_data = TRUE;
2dc40815
ML
169 if (ret % vdev->unitsize != 0)
170 sr_warn("Read size %d not a multiple of the"
171 " unit size %d.", ret, vdev->unitsize);
172 packet.type = SR_DF_LOGIC;
173 packet.payload = &logic;
174 logic.length = ret;
175 logic.unitsize = vdev->unitsize;
176 logic.data = buf;
7ee8511b
GS
177 } else {
178 /*
179 * Neither analog data, nor logic which has
180 * unitsize, must be an unexpected API use.
181 */
182 sr_warn("Neither analog nor logic data. Ignoring.");
183 }
184 if (got_data) {
185 vdev->bytes_read += ret;
186 sr_session_send(sdi, &packet);
2dc40815 187 }
8d4097ff
DE
188 } else {
189 /* done with this capture file */
190 zip_fclose(vdev->capfile);
191 vdev->capfile = NULL;
192 if (vdev->cur_chunk != 0) {
193 /* There might be more chunks, so don't fall through
194 * to the SR_DF_END here. */
195 got_data = TRUE;
196 }
7d658874 197 }
8d4097ff 198 g_free(buf);
7d658874 199
8d4097ff
DE
200 return got_data;
201}
202
203static int receive_data(int fd, int revents, void *cb_data)
204{
205 struct sr_dev_inst *sdi;
206 struct session_vdev *vdev;
8d4097ff
DE
207
208 (void)fd;
209 (void)revents;
210
211 sdi = cb_data;
212 vdev = sdi->priv;
213
214 if (!vdev->finished && !stream_session_data(sdi))
215 vdev->finished = TRUE;
216 if (!vdev->finished)
217 return G_SOURCE_CONTINUE;
218
219 if (vdev->capfile) {
220 zip_fclose(vdev->capfile);
221 vdev->capfile = NULL;
222 }
223 if (vdev->archive) {
224 zip_discard(vdev->archive);
225 vdev->archive = NULL;
226 }
3be42bc2 227
bee2b016 228 std_session_send_df_end(sdi);
8d4097ff
DE
229
230 return G_SOURCE_REMOVE;
7d658874
BV
231}
232
233/* driver callbacks */
234
6078d2c9 235static int dev_open(struct sr_dev_inst *sdi)
7d658874 236{
4f840ce9 237 struct sr_dev_driver *di;
102f1239 238 struct drv_context *drvc;
aa48adf9 239 struct session_vdev *vdev;
b53738ba 240
4f840ce9 241 di = sdi->driver;
41812aca 242 drvc = di->context;
102f1239 243 vdev = g_malloc0(sizeof(struct session_vdev));
aa48adf9 244 sdi->priv = vdev;
102f1239 245 drvc->instances = g_slist_append(drvc->instances, sdi);
7d658874
BV
246
247 return SR_OK;
248}
249
aa48adf9
BV
250static int dev_close(struct sr_dev_inst *sdi)
251{
8c9d4d67
JH
252 const struct session_vdev *const vdev = sdi->priv;
253 g_free(vdev->sessionfile);
254 g_free(vdev->capturefile);
255
aa48adf9
BV
256 g_free(sdi->priv);
257 sdi->priv = NULL;
258
259 return SR_OK;
260}
261
dd7a72ea
UH
262static int config_get(uint32_t key, GVariant **data,
263 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
7d658874 264{
bb7ef793 265 struct session_vdev *vdev;
7d658874 266
53b4680f 267 (void)cg;
8f996b89 268
3f2cd87f
UH
269 if (!sdi)
270 return SR_ERR;
271
272 vdev = sdi->priv;
273
584560f1 274 switch (key) {
123e1313 275 case SR_CONF_SAMPLERATE:
3f2cd87f
UH
276 *data = g_variant_new_uint64(vdev->samplerate);
277 break;
278 case SR_CONF_CAPTURE_UNITSIZE:
279 *data = g_variant_new_uint64(vdev->unitsize);
387014de
BV
280 break;
281 default:
96ea73db 282 return SR_ERR_NA;
387014de 283 }
7d658874 284
387014de 285 return SR_OK;
7d658874
BV
286}
287
dd7a72ea
UH
288static int config_set(uint32_t key, GVariant *data,
289 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
7d658874 290{
bb7ef793 291 struct session_vdev *vdev;
7d658874 292
53b4680f 293 (void)cg;
8f996b89 294
6f4b1868 295 vdev = sdi->priv;
7d658874 296
584560f1 297 switch (key) {
1953564a 298 case SR_CONF_SAMPLERATE:
003595ac 299 vdev->samplerate = g_variant_get_uint64(data);
a885ce3e 300 sr_info("Setting samplerate to %" PRIu64 ".", vdev->samplerate);
7d658874 301 break;
1953564a 302 case SR_CONF_SESSIONFILE:
8c9d4d67 303 g_free(vdev->sessionfile);
003595ac 304 vdev->sessionfile = g_strdup(g_variant_get_string(data, NULL));
a885ce3e 305 sr_info("Setting sessionfile to '%s'.", vdev->sessionfile);
40dda2c3 306 break;
1953564a 307 case SR_CONF_CAPTUREFILE:
8c9d4d67 308 g_free(vdev->capturefile);
003595ac 309 vdev->capturefile = g_strdup(g_variant_get_string(data, NULL));
a885ce3e 310 sr_info("Setting capturefile to '%s'.", vdev->capturefile);
7d658874 311 break;
1953564a 312 case SR_CONF_CAPTURE_UNITSIZE:
003595ac 313 vdev->unitsize = g_variant_get_uint64(data);
7d658874 314 break;
3f239f08 315 case SR_CONF_NUM_LOGIC_CHANNELS:
6fb5570b 316 vdev->num_logic_channels = g_variant_get_int32(data);
7d658874 317 break;
b317d1bb
ML
318 case SR_CONF_NUM_ANALOG_CHANNELS:
319 vdev->num_analog_channels = g_variant_get_int32(data);
320 break;
7d658874 321 default:
96ea73db 322 return SR_ERR_NA;
7d658874
BV
323 }
324
325 return SR_OK;
326}
327
dd7a72ea
UH
328static int config_list(uint32_t key, GVariant **data,
329 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
9a6517d1 330{
23772462 331 return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, NO_OPTS, devopts);
9a6517d1
BV
332}
333
695dc859 334static int dev_acquisition_start(const struct sr_dev_inst *sdi)
7d658874 335{
bb7ef793 336 struct session_vdev *vdev;
ebc34738 337 int ret;
2dc40815
ML
338 GSList *l;
339 struct sr_channel *ch;
7d658874 340
102f1239 341 vdev = sdi->priv;
8c9d4d67 342 vdev->bytes_read = 0;
906921ba 343 vdev->cur_analog_channel = 0;
2dc40815
ML
344 vdev->analog_channels = g_array_sized_new(FALSE, FALSE,
345 sizeof(struct sr_channel *), vdev->num_analog_channels);
346 for (l = sdi->channels; l; l = l->next) {
347 ch = l->data;
348 if (ch->type == SR_CHANNEL_ANALOG)
349 g_array_append_val(vdev->analog_channels, ch);
350 }
8c9d4d67
JH
351 vdev->cur_chunk = 0;
352 vdev->finished = FALSE;
353
a885ce3e 354 sr_info("Opening archive %s file %s", vdev->sessionfile,
bb7ef793 355 vdev->capturefile);
7d658874 356
40dda2c3 357 if (!(vdev->archive = zip_open(vdev->sessionfile, 0, &ret))) {
a885ce3e 358 sr_err("Failed to open session file '%s': "
9d122af8 359 "zip error %d.", vdev->sessionfile, ret);
7d658874
BV
360 return SR_ERR;
361 }
362
bee2b016 363 std_session_send_df_header(sdi);
4afdfd46 364
7d658874 365 /* freewheeling source */
102f1239 366 sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
7d658874 367
7d658874
BV
368 return SR_OK;
369}
370
695dc859 371static int dev_acquisition_stop(struct sr_dev_inst *sdi)
8d4097ff
DE
372{
373 struct session_vdev *vdev;
374
8d4097ff
DE
375 vdev = sdi->priv;
376
377 vdev->finished = TRUE;
378
379 return SR_OK;
380}
381
b4bd7088 382/** @private */
c09f0b57 383SR_PRIV struct sr_dev_driver session_driver = {
a885ce3e 384 .name = "virtual-session",
5097b0d0
UH
385 .longname = "Session-emulating driver",
386 .api_version = 1,
c2fdcc25 387 .init = std_init,
b4fde527 388 .cleanup = std_cleanup,
96ea73db
UH
389 .scan = NULL,
390 .dev_list = NULL,
029b2042 391 .dev_clear = std_dev_clear,
035a1078
BV
392 .config_get = config_get,
393 .config_set = config_set,
9a6517d1 394 .config_list = config_list,
6078d2c9 395 .dev_open = dev_open,
aa48adf9 396 .dev_close = dev_close,
6078d2c9 397 .dev_acquisition_start = dev_acquisition_start,
8d4097ff 398 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 399 .context = NULL,
7d658874 400};