2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 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 4096
32 struct session_vdevice {
35 struct zip_file *capfile;
41 static char *sessionfile = NULL;
42 static GSList *device_instances = NULL;
43 static int capabilities[] = {
45 SR_HWCAP_CAPTURE_UNITSIZE,
50 static struct session_vdevice *get_vdevice_by_index(int device_index)
52 struct sr_device_instance *sdi;
53 struct session_vdevice *vdevice;
55 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
63 static int feed_chunk(int fd, int revents, void *user_data)
65 struct sr_device_instance *sdi;
66 struct session_vdevice *vdevice;
67 struct sr_datafeed_packet packet;
72 /* avoid compiler warning */
76 sr_dbg("session_driver: feed chunk");
79 for (l = device_instances; l; l = l->next) {
83 /* already done with this instance */
86 buf = g_malloc(CHUNKSIZE);
87 ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE);
90 packet.type = SR_DF_LOGIC;
92 packet.unitsize = vdevice->unitsize;
94 sr_session_bus(user_data, &packet);
96 /* done with this capture file */
97 zip_fclose(vdevice->capfile);
98 g_free(vdevice->capturefile);
105 packet.type = SR_DF_END;
107 sr_session_bus(user_data, &packet);
113 /* driver callbacks */
115 static int hw_init(const char *deviceinfo)
118 sessionfile = g_strdup(deviceinfo);
123 static void hw_cleanup(void)
127 for (l = device_instances; l; l = l->next)
128 sr_device_instance_free(l->data);
134 static int hw_opendev(int device_index)
136 struct sr_device_instance *sdi;
138 sdi = sr_device_instance_new(device_index, SR_ST_INITIALIZING,
142 sdi->priv = g_malloc0(sizeof(struct session_vdevice));
145 device_instances = g_slist_append(device_instances, sdi);
150 static void *hw_get_device_info(int device_index, int device_info_id)
152 struct session_vdevice *vdevice;
155 if (device_info_id != SR_DI_CUR_SAMPLERATE)
158 if (!(vdevice = get_vdevice_by_index(device_index)))
161 info = &vdevice->samplerate;
166 static int hw_get_status(int device_index)
169 /* avoid compiler warning */
170 device_index = device_index;
178 static int *hw_get_capabilities(void)
184 static int hw_set_configuration(int device_index, int capability, void *value)
186 struct session_vdevice *vdevice;
189 if (!(vdevice = get_vdevice_by_index(device_index)))
192 switch (capability) {
193 case SR_HWCAP_SAMPLERATE:
195 vdevice->samplerate = *tmp_u64;
197 case SR_HWCAP_CAPTUREFILE:
198 vdevice->capturefile = g_strdup(value);
200 case SR_HWCAP_CAPTURE_UNITSIZE:
202 vdevice->unitsize = *tmp_u64;
204 case SR_HWCAP_CAPTURE_NUM_PROBES:
206 vdevice->num_probes = *tmp_u64;
215 static int hw_start_acquisition(int device_index, gpointer session_device_id)
218 struct session_vdevice *vdevice;
219 struct sr_datafeed_header *header;
220 struct sr_datafeed_packet *packet;
223 /* avoid compiler warning */
224 session_device_id = session_device_id;
226 if (!(vdevice = get_vdevice_by_index(device_index)))
229 sr_info("session_driver: opening archive %s file %s", sessionfile,
230 vdevice->capturefile);
232 if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) {
233 sr_warn("Failed to open session file '%s': zip error %d\n",
238 if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) {
239 sr_warn("Failed to check capture file '%s' in session file '%s'.",
240 vdevice->capturefile, sessionfile);
244 if (!(vdevice->capfile = zip_fopen(vdevice->archive, vdevice->capturefile, 0))) {
245 sr_warn("Failed to open capture file '%s' in session file '%s'.",
246 vdevice->capturefile, sessionfile);
250 /* freewheeling source */
251 sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id);
253 /* Send header packet to the session bus. */
254 packet = g_malloc(sizeof(struct sr_datafeed_packet));
255 header = g_malloc(sizeof(struct sr_datafeed_header));
256 if (!packet || !header)
258 packet->type = SR_DF_HEADER;
259 packet->length = sizeof(struct sr_datafeed_header);
260 packet->payload = (unsigned char *)header;
261 header->feed_version = 1;
262 gettimeofday(&header->starttime, NULL);
263 header->samplerate = 0;
264 header->protocol_id = SR_PROTO_RAW;
265 header->num_logic_probes = vdevice->num_probes;
266 header->num_analog_probes = 0;
267 sr_session_bus(session_device_id, packet);
275 struct sr_device_plugin session_driver = {
277 "Session-emulating driver",
286 hw_set_configuration,
287 hw_start_acquisition,