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>
28 /* size of payloads sent across the session bus */
29 #define CHUNKSIZE 4096
31 struct session_vdevice {
34 struct zip_file *capfile;
40 static char *sessionfile = NULL;
41 static GSList *device_instances = NULL;
42 static int capabilities[] = {
44 SR_HWCAP_CAPTURE_UNITSIZE,
49 static struct session_vdevice *get_vdevice_by_index(int device_index)
51 struct sr_device_instance *sdi;
52 struct session_vdevice *vdevice;
54 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
62 static int feed_chunk(int fd, int revents, void *user_data)
64 struct sr_device_instance *sdi;
65 struct session_vdevice *vdevice;
66 struct sr_datafeed_packet packet;
71 /* avoid compiler warning */
75 g_debug("session_driver: feed chunk");
78 for (l = device_instances; l; l = l->next) {
82 /* already done with this instance */
85 buf = g_malloc(CHUNKSIZE);
86 ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE);
89 packet.type = SR_DF_LOGIC;
91 packet.unitsize = vdevice->unitsize;
93 sr_session_bus(user_data, &packet);
95 /* done with this capture file */
96 zip_fclose(vdevice->capfile);
97 g_free(vdevice->capturefile);
104 packet.type = SR_DF_END;
106 sr_session_bus(user_data, &packet);
112 /* driver callbacks */
114 static int hw_init(const char *deviceinfo)
117 sessionfile = g_strdup(deviceinfo);
122 static void hw_cleanup(void)
126 for (l = device_instances; l; l = l->next)
127 sr_device_instance_free(l->data);
133 static int hw_opendev(int device_index)
135 struct sr_device_instance *sdi;
137 sdi = sr_device_instance_new(device_index, SR_ST_INITIALIZING,
141 sdi->priv = g_malloc0(sizeof(struct session_vdevice));
144 device_instances = g_slist_append(device_instances, sdi);
149 static void *hw_get_device_info(int device_index, int device_info_id)
151 struct session_vdevice *vdevice;
154 if (device_info_id != SR_DI_CUR_SAMPLERATE)
157 if (!(vdevice = get_vdevice_by_index(device_index)))
160 info = &vdevice->samplerate;
165 static int hw_get_status(int device_index)
168 /* avoid compiler warning */
169 device_index = device_index;
177 static int *hw_get_capabilities(void)
183 static int hw_set_configuration(int device_index, int capability, void *value)
185 struct session_vdevice *vdevice;
188 if (!(vdevice = get_vdevice_by_index(device_index)))
191 switch (capability) {
192 case SR_HWCAP_SAMPLERATE:
194 vdevice->samplerate = *tmp_u64;
196 case SR_HWCAP_CAPTUREFILE:
197 vdevice->capturefile = g_strdup(value);
199 case SR_HWCAP_CAPTURE_UNITSIZE:
201 vdevice->unitsize = *tmp_u64;
203 case SR_HWCAP_CAPTURE_NUM_PROBES:
205 vdevice->num_probes = *tmp_u64;
214 static int hw_start_acquisition(int device_index, gpointer session_device_id)
217 struct session_vdevice *vdevice;
218 struct sr_datafeed_header *header;
219 struct sr_datafeed_packet *packet;
222 /* avoid compiler warning */
223 session_device_id = session_device_id;
225 if (!(vdevice = get_vdevice_by_index(device_index)))
228 g_message("session_driver: opening archive %s file %s",
229 sessionfile, vdevice->capturefile);
231 if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) {
232 g_warning("Failed to open session file '%s': zip error %d\n",
237 if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) {
238 g_warning("Failed to check capture file '%s' in session file '%s'.",
239 vdevice->capturefile, sessionfile);
243 if (!(vdevice->capfile = zip_fopen(vdevice->archive, vdevice->capturefile, 0))) {
244 g_warning("Failed to open capture file '%s' in session file '%s'.",
245 vdevice->capturefile, sessionfile);
249 /* freewheeling source */
250 sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id);
252 /* Send header packet to the session bus. */
253 packet = g_malloc(sizeof(struct sr_datafeed_packet));
254 header = g_malloc(sizeof(struct sr_datafeed_header));
255 if (!packet || !header)
257 packet->type = SR_DF_HEADER;
258 packet->length = sizeof(struct sr_datafeed_header);
259 packet->payload = (unsigned char *)header;
260 header->feed_version = 1;
261 gettimeofday(&header->starttime, NULL);
262 header->samplerate = 0;
263 header->protocol_id = SR_PROTO_RAW;
264 header->num_logic_probes = vdevice->num_probes;
265 header->num_analog_probes = 0;
266 sr_session_bus(session_device_id, packet);
274 struct sr_device_plugin session_driver = {
276 "Session-emulating driver",
285 hw_set_configuration,
286 hw_start_acquisition,