]> sigrok.org Git - libsigrok.git/blame - session_driver.c
Added "autostuff" to the .gitignore.
[libsigrok.git] / session_driver.c
CommitLineData
7d658874
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 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 <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <unistd.h>
24#include <sys/time.h>
25#include <zip.h>
b7f09cf8
UH
26#include "sigrok.h"
27#include "sigrok-internal.h"
7d658874
BV
28
29/* size of payloads sent across the session bus */
30#define CHUNKSIZE 4096
31
32struct session_vdevice {
33 char *capturefile;
34 struct zip *archive;
35 struct zip_file *capfile;
cb1e389c 36 int bytes_read;
7d658874
BV
37 uint64_t samplerate;
38 int unitsize;
39 int num_probes;
40};
41
42static char *sessionfile = NULL;
43static GSList *device_instances = NULL;
44static int capabilities[] = {
45 SR_HWCAP_CAPTUREFILE,
46 SR_HWCAP_CAPTURE_UNITSIZE,
47 0,
48};
49
9f45fb3a
UH
50/**
51 * TODO.
52 *
53 * @param device_index TODO.
54 */
7d658874
BV
55static struct session_vdevice *get_vdevice_by_index(int device_index)
56{
57 struct sr_device_instance *sdi;
58 struct session_vdevice *vdevice;
59
9f45fb3a
UH
60 /* TODO: Sanity checks on device_index. */
61
62 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
63 sr_err("session driver: %s: device instance with device "
64 "index %d was not found", __func__, device_index);
7d658874 65 return NULL;
9f45fb3a
UH
66 }
67
68 /* TODO: Is sdi->priv == NULL valid? */
7d658874
BV
69
70 vdevice = sdi->priv;
71
72 return vdevice;
73}
74
9f45fb3a
UH
75/**
76 * TODO.
77 *
78 * @param fd TODO.
79 * @param revents TODO.
80 * @param session_data TODO.
81 *
82 * @return TODO.
83 */
9c939c51 84static int feed_chunk(int fd, int revents, void *session_data)
7d658874
BV
85{
86 struct sr_device_instance *sdi;
87 struct session_vdevice *vdevice;
88 struct sr_datafeed_packet packet;
9c939c51 89 struct sr_datafeed_logic logic;
cb1e389c 90 uint64_t sample_period_ps;
7d658874
BV
91 GSList *l;
92 void *buf;
93 int ret, got_data;
94
cb93f8a9
UH
95 /* Avoid compiler warnings. */
96 (void)fd;
97 (void)revents;
7d658874 98
b08024a8 99 sr_dbg("session_driver: feed chunk");
7d658874
BV
100
101 got_data = FALSE;
102 for (l = device_instances; l; l = l->next) {
103 sdi = l->data;
104 vdevice = sdi->priv;
105 if (!vdevice)
106 /* already done with this instance */
107 continue;
108
b53738ba
UH
109 if (!(buf = g_try_malloc(CHUNKSIZE))) {
110 sr_err("session: %s: buf malloc failed", __func__);
111 // return SR_ERR_MALLOC;
112 return FALSE;
113 }
114
7d658874
BV
115 ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE);
116 if (ret > 0) {
117 got_data = TRUE;
118 packet.type = SR_DF_LOGIC;
cb1e389c
BV
119 sample_period_ps = 1000000000000 / vdevice->samplerate;
120 packet.timeoffset = sample_period_ps * (vdevice->bytes_read / vdevice->unitsize);
121 packet.duration = sample_period_ps * (ret / vdevice->unitsize);
9c939c51
BV
122 packet.payload = &logic;
123 logic.length = ret;
124 logic.unitsize = vdevice->unitsize;
125 logic.data = buf;
cb1e389c 126 vdevice->bytes_read += ret;
9c939c51 127 sr_session_bus(session_data, &packet);
7d658874
BV
128 } else {
129 /* done with this capture file */
130 zip_fclose(vdevice->capfile);
131 g_free(vdevice->capturefile);
132 g_free(vdevice);
133 sdi->priv = NULL;
134 }
135 }
136
137 if (!got_data) {
138 packet.type = SR_DF_END;
9c939c51 139 sr_session_bus(session_data, &packet);
7d658874
BV
140 }
141
142 return TRUE;
143}
144
145/* driver callbacks */
805e9640 146static void hw_cleanup(void);
7d658874 147
9f45fb3a
UH
148/**
149 * TODO.
150 *
151 * @param deviceinfo TODO.
152 *
153 * @return TODO.
154 */
54ac5277 155static int hw_init(const char *deviceinfo)
7d658874 156{
805e9640 157 hw_cleanup();
7d658874
BV
158
159 sessionfile = g_strdup(deviceinfo);
160
161 return 0;
162}
163
9f45fb3a
UH
164/**
165 * TODO.
166 *
167 */
7d658874
BV
168static void hw_cleanup(void)
169{
170 GSList *l;
171
172 for (l = device_instances; l; l = l->next)
173 sr_device_instance_free(l->data);
174
805e9640
GM
175 g_slist_free(device_instances);
176 device_instances = NULL;
177
178 sr_session_source_remove(-1);
179
7d658874 180 g_free(sessionfile);
7d658874
BV
181}
182
183static int hw_opendev(int device_index)
184{
185 struct sr_device_instance *sdi;
186
187 sdi = sr_device_instance_new(device_index, SR_ST_INITIALIZING,
188 NULL, NULL, NULL);
189 if (!sdi)
190 return SR_ERR;
b53738ba
UH
191
192 if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) {
193 sr_err("session: %s: sdi->priv malloc failed", __func__);
194 return SR_ERR_MALLOC;
195 }
196
7d658874
BV
197 device_instances = g_slist_append(device_instances, sdi);
198
199 return SR_OK;
200}
201
7d658874
BV
202static void *hw_get_device_info(int device_index, int device_info_id)
203{
204 struct session_vdevice *vdevice;
205 void *info;
206
b8c2f85f
BV
207 if (device_info_id != SR_DI_CUR_SAMPLERATE)
208 return NULL;
209
7d658874
BV
210 if (!(vdevice = get_vdevice_by_index(device_index)))
211 return NULL;
212
213 info = &vdevice->samplerate;
214
215 return info;
216}
217
218static int hw_get_status(int device_index)
219{
cb93f8a9
UH
220 /* Avoid compiler warnings. */
221 (void)device_index;
7d658874 222
a0ecd83b 223 if (sr_device_list() != NULL)
7d658874
BV
224 return SR_OK;
225 else
226 return SR_ERR;
227}
228
9f45fb3a
UH
229/**
230 * Get the list of hardware capabilities.
231 *
232 * @return A pointer to the (hardware) capabilities of this virtual session
233 * driver. This could be NULL, if no such capabilities exist.
234 */
7d658874
BV
235static int *hw_get_capabilities(void)
236{
7d658874
BV
237 return capabilities;
238}
239
240static int hw_set_configuration(int device_index, int capability, void *value)
241{
242 struct session_vdevice *vdevice;
243 uint64_t *tmp_u64;
244
245 if (!(vdevice = get_vdevice_by_index(device_index)))
246 return SR_ERR;
247
248 switch (capability) {
249 case SR_HWCAP_SAMPLERATE:
250 tmp_u64 = value;
251 vdevice->samplerate = *tmp_u64;
9f45fb3a
UH
252 sr_info("session driver: setting samplerate to %" PRIu64,
253 vdevice->samplerate);
7d658874
BV
254 break;
255 case SR_HWCAP_CAPTUREFILE:
256 vdevice->capturefile = g_strdup(value);
9f45fb3a
UH
257 sr_info("session driver: setting capturefile to %s",
258 vdevice->capturefile);
7d658874
BV
259 break;
260 case SR_HWCAP_CAPTURE_UNITSIZE:
261 tmp_u64 = value;
262 vdevice->unitsize = *tmp_u64;
263 break;
264 case SR_HWCAP_CAPTURE_NUM_PROBES:
265 tmp_u64 = value;
266 vdevice->num_probes = *tmp_u64;
267 break;
268 default:
9f45fb3a
UH
269 sr_err("session driver: %s: unknown capability %d requested",
270 __func__, capability);
7d658874
BV
271 return SR_ERR;
272 }
273
274 return SR_OK;
275}
276
277static int hw_start_acquisition(int device_index, gpointer session_device_id)
278{
279 struct zip_stat zs;
280 struct session_vdevice *vdevice;
281 struct sr_datafeed_header *header;
282 struct sr_datafeed_packet *packet;
283 int err;
284
cb93f8a9
UH
285 /* Avoid compiler warnings. */
286 (void)session_device_id;
7d658874
BV
287
288 if (!(vdevice = get_vdevice_by_index(device_index)))
289 return SR_ERR;
290
b08024a8
UH
291 sr_info("session_driver: opening archive %s file %s", sessionfile,
292 vdevice->capturefile);
7d658874
BV
293
294 if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) {
b08024a8
UH
295 sr_warn("Failed to open session file '%s': zip error %d\n",
296 sessionfile, err);
7d658874
BV
297 return SR_ERR;
298 }
299
300 if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) {
b08024a8
UH
301 sr_warn("Failed to check capture file '%s' in session file '%s'.",
302 vdevice->capturefile, sessionfile);
7d658874
BV
303 return SR_ERR;
304 }
305
306 if (!(vdevice->capfile = zip_fopen(vdevice->archive, vdevice->capturefile, 0))) {
b08024a8
UH
307 sr_warn("Failed to open capture file '%s' in session file '%s'.",
308 vdevice->capturefile, sessionfile);
7d658874
BV
309 return SR_ERR;
310 }
311
312 /* freewheeling source */
8a2efef2 313 sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id);
7d658874 314
b53738ba
UH
315 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
316 sr_err("session: %s: packet malloc failed", __func__);
317 return SR_ERR_MALLOC;
318 }
319
320 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
321 sr_err("session: %s: header malloc failed", __func__);
322 return SR_ERR_MALLOC;
323 }
324
7d658874 325 /* Send header packet to the session bus. */
7d658874 326 packet->type = SR_DF_HEADER;
7d658874
BV
327 packet->payload = (unsigned char *)header;
328 header->feed_version = 1;
329 gettimeofday(&header->starttime, NULL);
276585ff 330 header->samplerate = vdevice->samplerate;
7d658874
BV
331 header->num_logic_probes = vdevice->num_probes;
332 header->num_analog_probes = 0;
8a2efef2 333 sr_session_bus(session_device_id, packet);
7d658874
BV
334 g_free(header);
335 g_free(packet);
336
337 return SR_OK;
338}
339
a0ecd83b 340/* Not static, it's used elsewhere (via 'extern'). */
7d658874
BV
341struct sr_device_plugin session_driver = {
342 "session",
343 "Session-emulating driver",
344 1,
345 hw_init,
346 hw_cleanup,
347 hw_opendev,
b8c2f85f 348 NULL,
7d658874
BV
349 hw_get_device_info,
350 hw_get_status,
351 hw_get_capabilities,
352 hw_set_configuration,
353 hw_start_acquisition,
8a2efef2 354 NULL,
7d658874 355};