]> sigrok.org Git - libsigrok.git/blob - src/session_driver.c
De-init vdev->capturefile when done
[libsigrok.git] / src / session_driver.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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
20 #include <config.h>
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>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
29
30 #define LOG_PREFIX "virtual-session"
31
32 /* size of payloads sent across the session bus */
33 /** @cond PRIVATE */
34 #define CHUNKSIZE (4 * 1024 * 1024)
35 /** @endcond */
36
37 SR_PRIV struct sr_dev_driver session_driver_info;
38
39 struct session_vdev {
40         char *sessionfile;
41         char *capturefile;
42         struct zip *archive;
43         struct zip_file *capfile;
44         int bytes_read;
45         uint64_t samplerate;
46         int unitsize;
47         int num_channels;
48         int num_analog_channels;
49         int cur_analog_channel;
50         GArray *analog_channels;
51         int cur_chunk;
52         gboolean finished;
53 };
54
55 static const uint32_t devopts[] = {
56         SR_CONF_CAPTUREFILE | SR_CONF_SET,
57         SR_CONF_CAPTURE_UNITSIZE | SR_CONF_GET | SR_CONF_SET,
58         SR_CONF_NUM_LOGIC_CHANNELS | SR_CONF_SET,
59         SR_CONF_NUM_ANALOG_CHANNELS | SR_CONF_SET,
60         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET,
61         SR_CONF_SESSIONFILE | SR_CONF_SET,
62 };
63
64 static gboolean stream_session_data(struct sr_dev_inst *sdi)
65 {
66         struct session_vdev *vdev;
67         struct sr_datafeed_packet packet;
68         struct sr_datafeed_logic logic;
69         struct sr_datafeed_analog analog;
70         struct sr_analog_encoding encoding;
71         struct sr_analog_meaning meaning;
72         struct sr_analog_spec spec;
73         struct zip_stat zs;
74         int ret, got_data;
75         char capturefile[16];
76         void *buf;
77
78         got_data = FALSE;
79         vdev = sdi->priv;
80
81         if (!vdev->capfile) {
82                 /* No capture file opened yet, or finished with the last
83                  * chunked one. */
84                 if (vdev->capturefile && (vdev->cur_chunk == 0)) {
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);
93                         } else {
94                                 /* Try as first chunk filename. */
95                                 snprintf(capturefile, 15, "%s-1", vdev->capturefile);
96                                 if (zip_stat(vdev->archive, capturefile, 0, &zs) != -1) {
97                                         vdev->cur_chunk = 1;
98                                         if (!(vdev->capfile = zip_fopen(vdev->archive,
99                                                         capturefile, 0)))
100                                                 return FALSE;
101                                         sr_dbg("Opened %s.", capturefile);
102                                 } else {
103                                         sr_err("No capture file '%s' in " "session file '%s'.",
104                                                         vdev->capturefile, vdev->sessionfile);
105                                         return FALSE;
106                                 }
107                         }
108                 } else {
109                         /* Capture data is chunked, advance to the next chunk. */
110                         vdev->cur_chunk++;
111                         snprintf(capturefile, 15, "%s-%d", vdev->capturefile,
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);
118                         } else if (vdev->cur_analog_channel < vdev->num_analog_channels) {
119                                 vdev->capturefile = g_strdup_printf("analog-1-%d",
120                                                 vdev->num_channels + vdev->cur_analog_channel + 1);
121                                 vdev->cur_analog_channel++;
122                                 vdev->cur_chunk = 0;
123                                 return TRUE;
124                         } else {
125                                 /* We got all the chunks, finish up. */
126                                 g_free(vdev->capturefile);
127                                 vdev->capturefile = NULL;
128                                 return FALSE;
129                         }
130                 }
131         }
132
133         buf = g_malloc(CHUNKSIZE);
134
135         /* unitsize is not defined for purely analog session files. */
136         if (vdev->unitsize)
137                 ret = zip_fread(vdev->capfile, buf,
138                                 CHUNKSIZE / vdev->unitsize * vdev->unitsize);
139         else
140                 ret = zip_fread(vdev->capfile, buf, CHUNKSIZE);
141
142         if (ret > 0) {
143                 got_data = TRUE;
144                 if (vdev->cur_analog_channel != 0) {
145                         packet.type = SR_DF_ANALOG;
146                         packet.payload = &analog;
147                         /* TODO: Use proper 'digits' value for this device (and its modes). */
148                         sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
149                         analog.meaning->channels = g_slist_prepend(NULL,
150                                         g_array_index(vdev->analog_channels,
151                                                 struct sr_channel *, vdev->cur_analog_channel - 1));
152                         analog.num_samples = ret / sizeof(float);
153                         analog.meaning->mq = SR_MQ_VOLTAGE;
154                         analog.meaning->unit = SR_UNIT_VOLT;
155                         analog.meaning->mqflags = SR_MQFLAG_DC;
156                         analog.data = (float *) buf;
157                 } else {
158                         if (ret % vdev->unitsize != 0)
159                                 sr_warn("Read size %d not a multiple of the"
160                                         " unit size %d.", ret, vdev->unitsize);
161                         packet.type = SR_DF_LOGIC;
162                         packet.payload = &logic;
163                         logic.length = ret;
164                         logic.unitsize = vdev->unitsize;
165                         logic.data = buf;
166                 }
167                 vdev->bytes_read += ret;
168                 sr_session_send(sdi, &packet);
169         } else {
170                 /* done with this capture file */
171                 zip_fclose(vdev->capfile);
172                 vdev->capfile = NULL;
173                 if (vdev->cur_chunk != 0) {
174                         /* There might be more chunks, so don't fall through
175                          * to the SR_DF_END here. */
176                         got_data = TRUE;
177                 }
178         }
179         g_free(buf);
180
181         return got_data;
182 }
183
184 static int receive_data(int fd, int revents, void *cb_data)
185 {
186         struct sr_dev_inst *sdi;
187         struct session_vdev *vdev;
188
189         (void)fd;
190         (void)revents;
191
192         sdi = cb_data;
193         vdev = sdi->priv;
194
195         if (!vdev->finished && !stream_session_data(sdi))
196                 vdev->finished = TRUE;
197         if (!vdev->finished)
198                 return G_SOURCE_CONTINUE;
199
200         if (vdev->capfile) {
201                 zip_fclose(vdev->capfile);
202                 vdev->capfile = NULL;
203         }
204         if (vdev->archive) {
205                 zip_discard(vdev->archive);
206                 vdev->archive = NULL;
207         }
208
209         std_session_send_df_end(sdi);
210
211         return G_SOURCE_REMOVE;
212 }
213
214 /* driver callbacks */
215
216 static int dev_clear(const struct sr_dev_driver *di)
217 {
218         struct drv_context *drvc;
219         GSList *l;
220
221         drvc = di->context;
222         for (l = drvc->instances; l; l = l->next)
223                 sr_dev_inst_free(l->data);
224         g_slist_free(drvc->instances);
225         drvc->instances = NULL;
226
227         return SR_OK;
228 }
229
230 static int dev_open(struct sr_dev_inst *sdi)
231 {
232         struct sr_dev_driver *di;
233         struct drv_context *drvc;
234         struct session_vdev *vdev;
235
236         di = sdi->driver;
237         drvc = di->context;
238         vdev = g_malloc0(sizeof(struct session_vdev));
239         sdi->priv = vdev;
240         drvc->instances = g_slist_append(drvc->instances, sdi);
241
242         return SR_OK;
243 }
244
245 static int dev_close(struct sr_dev_inst *sdi)
246 {
247         const struct session_vdev *const vdev = sdi->priv;
248         g_free(vdev->sessionfile);
249         g_free(vdev->capturefile);
250
251         g_free(sdi->priv);
252         sdi->priv = NULL;
253
254         return SR_OK;
255 }
256
257 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
258                 const struct sr_channel_group *cg)
259 {
260         struct session_vdev *vdev;
261
262         (void)cg;
263
264         if (!sdi)
265                 return SR_ERR;
266
267         vdev = sdi->priv;
268
269         switch (key) {
270         case SR_CONF_SAMPLERATE:
271                 *data = g_variant_new_uint64(vdev->samplerate);
272                 break;
273         case SR_CONF_CAPTURE_UNITSIZE:
274                 *data = g_variant_new_uint64(vdev->unitsize);
275                 break;
276         default:
277                 return SR_ERR_NA;
278         }
279
280         return SR_OK;
281 }
282
283 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
284                 const struct sr_channel_group *cg)
285 {
286         struct session_vdev *vdev;
287
288         (void)cg;
289
290         vdev = sdi->priv;
291
292         switch (key) {
293         case SR_CONF_SAMPLERATE:
294                 vdev->samplerate = g_variant_get_uint64(data);
295                 sr_info("Setting samplerate to %" PRIu64 ".", vdev->samplerate);
296                 break;
297         case SR_CONF_SESSIONFILE:
298                 g_free(vdev->sessionfile);
299                 vdev->sessionfile = g_strdup(g_variant_get_string(data, NULL));
300                 sr_info("Setting sessionfile to '%s'.", vdev->sessionfile);
301                 break;
302         case SR_CONF_CAPTUREFILE:
303                 g_free(vdev->capturefile);
304                 vdev->capturefile = g_strdup(g_variant_get_string(data, NULL));
305                 sr_info("Setting capturefile to '%s'.", vdev->capturefile);
306                 break;
307         case SR_CONF_CAPTURE_UNITSIZE:
308                 vdev->unitsize = g_variant_get_uint64(data);
309                 break;
310         case SR_CONF_NUM_LOGIC_CHANNELS:
311                 vdev->num_channels = g_variant_get_int32(data);
312                 break;
313         case SR_CONF_NUM_ANALOG_CHANNELS:
314                 vdev->num_analog_channels = g_variant_get_int32(data);
315                 break;
316         default:
317                 return SR_ERR_NA;
318         }
319
320         return SR_OK;
321 }
322
323 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
324                 const struct sr_channel_group *cg)
325 {
326         (void)sdi;
327         (void)cg;
328
329         switch (key) {
330         case SR_CONF_DEVICE_OPTIONS:
331                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
332                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
333                 break;
334         default:
335                 return SR_ERR_NA;
336         }
337
338         return SR_OK;
339 }
340
341 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
342 {
343         struct session_vdev *vdev;
344         int ret;
345         GSList *l;
346         struct sr_channel *ch;
347
348         vdev = sdi->priv;
349         vdev->bytes_read = 0;
350         vdev->cur_analog_channel = 0;
351         vdev->analog_channels = g_array_sized_new(FALSE, FALSE,
352                         sizeof(struct sr_channel *), vdev->num_analog_channels);
353         for (l = sdi->channels; l; l = l->next) {
354                 ch = l->data;
355                 if (ch->type == SR_CHANNEL_ANALOG)
356                         g_array_append_val(vdev->analog_channels, ch);
357         }
358         vdev->cur_chunk = 0;
359         vdev->finished = FALSE;
360
361         sr_info("Opening archive %s file %s", vdev->sessionfile,
362                 vdev->capturefile);
363
364         if (!(vdev->archive = zip_open(vdev->sessionfile, 0, &ret))) {
365                 sr_err("Failed to open session file '%s': "
366                        "zip error %d.", vdev->sessionfile, ret);
367                 return SR_ERR;
368         }
369
370         std_session_send_df_header(sdi);
371
372         /* freewheeling source */
373         sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
374
375         return SR_OK;
376 }
377
378 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
379 {
380         struct session_vdev *vdev;
381
382         vdev = sdi->priv;
383
384         vdev->finished = TRUE;
385
386         return SR_OK;
387 }
388
389 /** @private */
390 SR_PRIV struct sr_dev_driver session_driver = {
391         .name = "virtual-session",
392         .longname = "Session-emulating driver",
393         .api_version = 1,
394         .init = std_init,
395         .cleanup = dev_clear,
396         .scan = NULL,
397         .dev_list = NULL,
398         .dev_clear = dev_clear,
399         .config_get = config_get,
400         .config_set = config_set,
401         .config_list = config_list,
402         .dev_open = dev_open,
403         .dev_close = dev_close,
404         .dev_acquisition_start = dev_acquisition_start,
405         .dev_acquisition_stop = dev_acquisition_stop,
406         .context = NULL,
407 };