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;
42 static char *sessionfile = NULL;
43 static GSList *device_instances = NULL;
44 static int capabilities[] = {
46 SR_HWCAP_CAPTURE_UNITSIZE,
51 static struct session_vdevice *get_vdevice_by_index(int device_index)
53 struct sr_device_instance *sdi;
54 struct session_vdevice *vdevice;
56 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
64 static int feed_chunk(int fd, int revents, void *session_data)
66 struct sr_device_instance *sdi;
67 struct session_vdevice *vdevice;
68 struct sr_datafeed_packet packet;
69 struct sr_datafeed_logic logic;
70 uint64_t sample_period_ps;
75 /* Avoid compiler warnings. */
79 sr_dbg("session_driver: feed chunk");
82 for (l = device_instances; l; l = l->next) {
86 /* already done with this instance */
89 if (!(buf = g_try_malloc(CHUNKSIZE))) {
90 sr_err("session: %s: buf malloc failed", __func__);
91 // return SR_ERR_MALLOC;
95 ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE);
98 packet.type = SR_DF_LOGIC;
99 sample_period_ps = 1000000000000 / vdevice->samplerate;
100 packet.timeoffset = sample_period_ps * (vdevice->bytes_read / vdevice->unitsize);
101 packet.duration = sample_period_ps * (ret / vdevice->unitsize);
102 packet.payload = &logic;
104 logic.unitsize = vdevice->unitsize;
106 vdevice->bytes_read += ret;
107 sr_session_bus(session_data, &packet);
109 /* done with this capture file */
110 zip_fclose(vdevice->capfile);
111 g_free(vdevice->capturefile);
118 packet.type = SR_DF_END;
119 sr_session_bus(session_data, &packet);
125 /* driver callbacks */
126 static void hw_cleanup(void);
128 static int hw_init(const char *deviceinfo)
132 sessionfile = g_strdup(deviceinfo);
137 static void hw_cleanup(void)
141 for (l = device_instances; l; l = l->next)
142 sr_device_instance_free(l->data);
144 g_slist_free(device_instances);
145 device_instances = NULL;
147 sr_session_source_remove(-1);
153 static int hw_opendev(int device_index)
155 struct sr_device_instance *sdi;
157 sdi = sr_device_instance_new(device_index, SR_ST_INITIALIZING,
162 if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) {
163 sr_err("session: %s: sdi->priv malloc failed", __func__);
164 return SR_ERR_MALLOC;
167 device_instances = g_slist_append(device_instances, sdi);
172 static void *hw_get_device_info(int device_index, int device_info_id)
174 struct session_vdevice *vdevice;
177 if (device_info_id != SR_DI_CUR_SAMPLERATE)
180 if (!(vdevice = get_vdevice_by_index(device_index)))
183 info = &vdevice->samplerate;
188 static int hw_get_status(int device_index)
190 /* Avoid compiler warnings. */
199 static int *hw_get_capabilities(void)
205 static int hw_set_configuration(int device_index, int capability, void *value)
207 struct session_vdevice *vdevice;
210 if (!(vdevice = get_vdevice_by_index(device_index)))
213 switch (capability) {
214 case SR_HWCAP_SAMPLERATE:
216 vdevice->samplerate = *tmp_u64;
218 case SR_HWCAP_CAPTUREFILE:
219 vdevice->capturefile = g_strdup(value);
221 case SR_HWCAP_CAPTURE_UNITSIZE:
223 vdevice->unitsize = *tmp_u64;
225 case SR_HWCAP_CAPTURE_NUM_PROBES:
227 vdevice->num_probes = *tmp_u64;
236 static int hw_start_acquisition(int device_index, gpointer session_device_id)
239 struct session_vdevice *vdevice;
240 struct sr_datafeed_header *header;
241 struct sr_datafeed_packet *packet;
244 /* Avoid compiler warnings. */
245 (void)session_device_id;
247 if (!(vdevice = get_vdevice_by_index(device_index)))
250 sr_info("session_driver: opening archive %s file %s", sessionfile,
251 vdevice->capturefile);
253 if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) {
254 sr_warn("Failed to open session file '%s': zip error %d\n",
259 if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) {
260 sr_warn("Failed to check capture file '%s' in session file '%s'.",
261 vdevice->capturefile, sessionfile);
265 if (!(vdevice->capfile = zip_fopen(vdevice->archive, vdevice->capturefile, 0))) {
266 sr_warn("Failed to open capture file '%s' in session file '%s'.",
267 vdevice->capturefile, sessionfile);
271 /* freewheeling source */
272 sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id);
274 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
275 sr_err("session: %s: packet malloc failed", __func__);
276 return SR_ERR_MALLOC;
279 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
280 sr_err("session: %s: header malloc failed", __func__);
281 return SR_ERR_MALLOC;
284 /* Send header packet to the session bus. */
285 packet->type = SR_DF_HEADER;
286 packet->payload = (unsigned char *)header;
287 header->feed_version = 1;
288 gettimeofday(&header->starttime, NULL);
289 header->samplerate = vdevice->samplerate;
290 header->num_logic_probes = vdevice->num_probes;
291 header->num_analog_probes = 0;
292 sr_session_bus(session_device_id, packet);
299 struct sr_device_plugin session_driver = {
301 "Session-emulating driver",
310 hw_set_configuration,
311 hw_start_acquisition,