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