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