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