]> sigrok.org Git - libsigrok.git/blame - session_driver.c
libsigrok: Fix #includes.
[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
7d658874
BV
50static struct session_vdevice *get_vdevice_by_index(int device_index)
51{
52 struct sr_device_instance *sdi;
53 struct session_vdevice *vdevice;
54
55 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
56 return NULL;
57
58 vdevice = sdi->priv;
59
60 return vdevice;
61}
62
9c939c51 63static int feed_chunk(int fd, int revents, void *session_data)
7d658874
BV
64{
65 struct sr_device_instance *sdi;
66 struct session_vdevice *vdevice;
67 struct sr_datafeed_packet packet;
9c939c51 68 struct sr_datafeed_logic logic;
cb1e389c 69 uint64_t sample_period_ps;
7d658874
BV
70 GSList *l;
71 void *buf;
72 int ret, got_data;
73
cb93f8a9
UH
74 /* Avoid compiler warnings. */
75 (void)fd;
76 (void)revents;
7d658874 77
b08024a8 78 sr_dbg("session_driver: feed chunk");
7d658874
BV
79
80 got_data = FALSE;
81 for (l = device_instances; l; l = l->next) {
82 sdi = l->data;
83 vdevice = sdi->priv;
84 if (!vdevice)
85 /* already done with this instance */
86 continue;
87
b53738ba
UH
88 if (!(buf = g_try_malloc(CHUNKSIZE))) {
89 sr_err("session: %s: buf malloc failed", __func__);
90 // return SR_ERR_MALLOC;
91 return FALSE;
92 }
93
7d658874
BV
94 ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE);
95 if (ret > 0) {
96 got_data = TRUE;
97 packet.type = SR_DF_LOGIC;
cb1e389c
BV
98 sample_period_ps = 1000000000000 / vdevice->samplerate;
99 packet.timeoffset = sample_period_ps * (vdevice->bytes_read / vdevice->unitsize);
100 packet.duration = sample_period_ps * (ret / vdevice->unitsize);
9c939c51
BV
101 packet.payload = &logic;
102 logic.length = ret;
103 logic.unitsize = vdevice->unitsize;
104 logic.data = buf;
cb1e389c 105 vdevice->bytes_read += ret;
9c939c51 106 sr_session_bus(session_data, &packet);
7d658874
BV
107 } else {
108 /* done with this capture file */
109 zip_fclose(vdevice->capfile);
110 g_free(vdevice->capturefile);
111 g_free(vdevice);
112 sdi->priv = NULL;
113 }
114 }
115
116 if (!got_data) {
117 packet.type = SR_DF_END;
9c939c51 118 sr_session_bus(session_data, &packet);
7d658874
BV
119 }
120
121 return TRUE;
122}
123
124/* driver callbacks */
805e9640 125static void hw_cleanup(void);
7d658874 126
54ac5277 127static int hw_init(const char *deviceinfo)
7d658874 128{
805e9640 129 hw_cleanup();
7d658874
BV
130
131 sessionfile = g_strdup(deviceinfo);
132
133 return 0;
134}
135
136static void hw_cleanup(void)
137{
138 GSList *l;
139
140 for (l = device_instances; l; l = l->next)
141 sr_device_instance_free(l->data);
142
805e9640
GM
143 g_slist_free(device_instances);
144 device_instances = NULL;
145
146 sr_session_source_remove(-1);
147
7d658874 148 g_free(sessionfile);
7d658874
BV
149}
150
151static int hw_opendev(int device_index)
152{
153 struct sr_device_instance *sdi;
154
155 sdi = sr_device_instance_new(device_index, SR_ST_INITIALIZING,
156 NULL, NULL, NULL);
157 if (!sdi)
158 return SR_ERR;
b53738ba
UH
159
160 if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) {
161 sr_err("session: %s: sdi->priv malloc failed", __func__);
162 return SR_ERR_MALLOC;
163 }
164
7d658874
BV
165 device_instances = g_slist_append(device_instances, sdi);
166
167 return SR_OK;
168}
169
7d658874
BV
170static void *hw_get_device_info(int device_index, int device_info_id)
171{
172 struct session_vdevice *vdevice;
173 void *info;
174
b8c2f85f
BV
175 if (device_info_id != SR_DI_CUR_SAMPLERATE)
176 return NULL;
177
7d658874
BV
178 if (!(vdevice = get_vdevice_by_index(device_index)))
179 return NULL;
180
181 info = &vdevice->samplerate;
182
183 return info;
184}
185
186static int hw_get_status(int device_index)
187{
cb93f8a9
UH
188 /* Avoid compiler warnings. */
189 (void)device_index;
7d658874 190
a0ecd83b 191 if (sr_device_list() != NULL)
7d658874
BV
192 return SR_OK;
193 else
194 return SR_ERR;
195}
196
197static int *hw_get_capabilities(void)
198{
7d658874
BV
199 return capabilities;
200}
201
202static int hw_set_configuration(int device_index, int capability, void *value)
203{
204 struct session_vdevice *vdevice;
205 uint64_t *tmp_u64;
206
207 if (!(vdevice = get_vdevice_by_index(device_index)))
208 return SR_ERR;
209
210 switch (capability) {
211 case SR_HWCAP_SAMPLERATE:
212 tmp_u64 = value;
213 vdevice->samplerate = *tmp_u64;
214 break;
215 case SR_HWCAP_CAPTUREFILE:
216 vdevice->capturefile = g_strdup(value);
217 break;
218 case SR_HWCAP_CAPTURE_UNITSIZE:
219 tmp_u64 = value;
220 vdevice->unitsize = *tmp_u64;
221 break;
222 case SR_HWCAP_CAPTURE_NUM_PROBES:
223 tmp_u64 = value;
224 vdevice->num_probes = *tmp_u64;
225 break;
226 default:
227 return SR_ERR;
228 }
229
230 return SR_OK;
231}
232
233static int hw_start_acquisition(int device_index, gpointer session_device_id)
234{
235 struct zip_stat zs;
236 struct session_vdevice *vdevice;
237 struct sr_datafeed_header *header;
238 struct sr_datafeed_packet *packet;
239 int err;
240
cb93f8a9
UH
241 /* Avoid compiler warnings. */
242 (void)session_device_id;
7d658874
BV
243
244 if (!(vdevice = get_vdevice_by_index(device_index)))
245 return SR_ERR;
246
b08024a8
UH
247 sr_info("session_driver: opening archive %s file %s", sessionfile,
248 vdevice->capturefile);
7d658874
BV
249
250 if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) {
b08024a8
UH
251 sr_warn("Failed to open session file '%s': zip error %d\n",
252 sessionfile, err);
7d658874
BV
253 return SR_ERR;
254 }
255
256 if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) {
b08024a8
UH
257 sr_warn("Failed to check capture file '%s' in session file '%s'.",
258 vdevice->capturefile, sessionfile);
7d658874
BV
259 return SR_ERR;
260 }
261
262 if (!(vdevice->capfile = zip_fopen(vdevice->archive, vdevice->capturefile, 0))) {
b08024a8
UH
263 sr_warn("Failed to open capture file '%s' in session file '%s'.",
264 vdevice->capturefile, sessionfile);
7d658874
BV
265 return SR_ERR;
266 }
267
268 /* freewheeling source */
8a2efef2 269 sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id);
7d658874 270
b53738ba
UH
271 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
272 sr_err("session: %s: packet malloc failed", __func__);
273 return SR_ERR_MALLOC;
274 }
275
276 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
277 sr_err("session: %s: header malloc failed", __func__);
278 return SR_ERR_MALLOC;
279 }
280
7d658874 281 /* Send header packet to the session bus. */
7d658874 282 packet->type = SR_DF_HEADER;
7d658874
BV
283 packet->payload = (unsigned char *)header;
284 header->feed_version = 1;
285 gettimeofday(&header->starttime, NULL);
276585ff 286 header->samplerate = vdevice->samplerate;
7d658874
BV
287 header->num_logic_probes = vdevice->num_probes;
288 header->num_analog_probes = 0;
8a2efef2 289 sr_session_bus(session_device_id, packet);
7d658874
BV
290 g_free(header);
291 g_free(packet);
292
293 return SR_OK;
294}
295
a0ecd83b 296/* Not static, it's used elsewhere (via 'extern'). */
7d658874
BV
297struct sr_device_plugin session_driver = {
298 "session",
299 "Session-emulating driver",
300 1,
301 hw_init,
302 hw_cleanup,
303 hw_opendev,
b8c2f85f 304 NULL,
7d658874
BV
305 hw_get_device_info,
306 hw_get_status,
307 hw_get_capabilities,
308 hw_set_configuration,
309 hw_start_acquisition,
8a2efef2 310 NULL,
7d658874 311};