2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include <sys/types.h>
27 #include "sigrok-internal.h"
29 /* size of payloads sent across the session bus */
30 #define CHUNKSIZE (512 * 1024)
32 struct session_vdevice {
35 struct zip_file *capfile;
42 static char *sessionfile = NULL;
43 static GSList *device_instances = NULL;
44 static int capabilities[] = {
46 SR_HWCAP_CAPTURE_UNITSIZE,
53 * @param device_index TODO.
55 static struct session_vdevice *get_vdevice_by_index(int device_index)
57 struct sr_device_instance *sdi;
58 struct session_vdevice *vdevice;
60 /* TODO: Sanity checks on device_index. */
62 if (!(sdi = sr_dev_inst_get(device_instances, device_index))) {
63 sr_err("session driver: %s: device instance with device "
64 "index %d was not found", __func__, device_index);
68 /* TODO: Is sdi->priv == NULL valid? */
79 * @param revents TODO.
80 * @param session_data TODO.
84 static int feed_chunk(int fd, int revents, void *session_data)
86 struct sr_device_instance *sdi;
87 struct session_vdevice *vdevice;
88 struct sr_datafeed_packet packet;
89 struct sr_datafeed_logic logic;
94 /* Avoid compiler warnings. */
98 sr_dbg("session_driver: feed chunk");
101 for (l = device_instances; l; l = l->next) {
105 /* already done with this instance */
108 if (!(buf = g_try_malloc(CHUNKSIZE))) {
109 sr_err("session: %s: buf malloc failed", __func__);
110 return FALSE; /* TODO: SR_ERR_MALLOC */
113 ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE);
116 packet.type = SR_DF_LOGIC;
117 packet.payload = &logic;
119 logic.unitsize = vdevice->unitsize;
121 vdevice->bytes_read += ret;
122 sr_session_bus(session_data, &packet);
124 /* done with this capture file */
125 zip_fclose(vdevice->capfile);
126 g_free(vdevice->capturefile);
133 packet.type = SR_DF_END;
134 sr_session_bus(session_data, &packet);
140 /* driver callbacks */
141 static int hw_cleanup(void);
146 * @param deviceinfo TODO.
150 static int hw_init(const char *deviceinfo)
152 sessionfile = g_strdup(deviceinfo);
161 static int hw_cleanup(void)
165 /* TODO: Error handling. */
167 for (l = device_instances; l; l = l->next)
168 sr_dev_inst_free(l->data);
170 g_slist_free(device_instances);
171 device_instances = NULL;
173 sr_session_source_remove(-1);
180 static int hw_opendev(int device_index)
182 struct sr_device_instance *sdi;
184 sdi = sr_dev_inst_new(device_index, SR_ST_INITIALIZING,
189 if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) {
190 sr_err("session: %s: sdi->priv malloc failed", __func__);
191 return SR_ERR_MALLOC;
194 device_instances = g_slist_append(device_instances, sdi);
199 static void *hw_get_device_info(int device_index, int device_info_id)
201 struct session_vdevice *vdevice;
204 if (device_info_id != SR_DI_CUR_SAMPLERATE)
207 if (!(vdevice = get_vdevice_by_index(device_index)))
210 info = &vdevice->samplerate;
215 static int hw_get_status(int device_index)
217 /* Avoid compiler warnings. */
220 if (sr_dev_list() != NULL)
227 * Get the list of hardware capabilities.
229 * @return A pointer to the (hardware) capabilities of this virtual session
230 * driver. This could be NULL, if no such capabilities exist.
232 static int *hw_get_capabilities(void)
237 static int hw_set_configuration(int device_index, int capability, void *value)
239 struct session_vdevice *vdevice;
242 if (!(vdevice = get_vdevice_by_index(device_index)))
245 switch (capability) {
246 case SR_HWCAP_SAMPLERATE:
248 vdevice->samplerate = *tmp_u64;
249 sr_info("session driver: setting samplerate to %" PRIu64,
250 vdevice->samplerate);
252 case SR_HWCAP_CAPTUREFILE:
253 vdevice->capturefile = g_strdup(value);
254 sr_info("session driver: setting capturefile to %s",
255 vdevice->capturefile);
257 case SR_HWCAP_CAPTURE_UNITSIZE:
259 vdevice->unitsize = *tmp_u64;
261 case SR_HWCAP_CAPTURE_NUM_PROBES:
263 vdevice->num_probes = *tmp_u64;
266 sr_err("session driver: %s: unknown capability %d requested",
267 __func__, capability);
274 static int hw_start_acquisition(int device_index, gpointer session_device_id)
277 struct session_vdevice *vdevice;
278 struct sr_datafeed_header *header;
279 struct sr_datafeed_packet *packet;
282 /* Avoid compiler warnings. */
283 (void)session_device_id;
285 if (!(vdevice = get_vdevice_by_index(device_index)))
288 sr_info("session_driver: opening archive %s file %s", sessionfile,
289 vdevice->capturefile);
291 if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) {
292 sr_err("Failed to open session file '%s': zip error %d\n",
297 if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) {
298 sr_err("Failed to check capture file '%s' in session file "
299 "'%s'.", vdevice->capturefile, sessionfile);
303 if (!(vdevice->capfile = zip_fopen(vdevice->archive,
304 vdevice->capturefile, 0))) {
305 sr_err("Failed to open capture file '%s' in session file '%s'.",
306 vdevice->capturefile, sessionfile);
310 /* freewheeling source */
311 sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id);
313 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
314 sr_err("session: %s: packet malloc failed", __func__);
315 return SR_ERR_MALLOC;
318 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
319 sr_err("session: %s: header malloc failed", __func__);
320 return SR_ERR_MALLOC;
323 /* Send header packet to the session bus. */
324 packet->type = SR_DF_HEADER;
325 packet->payload = (unsigned char *)header;
326 header->feed_version = 1;
327 gettimeofday(&header->starttime, NULL);
328 header->samplerate = vdevice->samplerate;
329 header->num_logic_probes = vdevice->num_probes;
330 sr_session_bus(session_device_id, packet);
337 /* Not static, it's used elsewhere (via 'extern'). */
338 struct sr_device_plugin session_driver = {
340 "Session-emulating driver",
349 hw_set_configuration,
350 hw_start_acquisition,