]> sigrok.org Git - libsigrok.git/blob - session_driver.c
sr: moved sigrok.h so libsigrok/libsigrok.h
[libsigrok.git] / session_driver.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 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 "libsigrok.h"
27 #include "libsigrok-internal.h"
28
29 /* size of payloads sent across the session bus */
30 #define CHUNKSIZE (512 * 1024)
31
32 struct session_vdev {
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 *dev_insts = NULL;
44 static const int hwcaps[] = {
45         SR_HWCAP_CAPTUREFILE,
46         SR_HWCAP_CAPTURE_UNITSIZE,
47         0,
48 };
49
50 /**
51  * TODO.
52  *
53  * @param dev_index TODO.
54  */
55 static struct session_vdev *get_vdev_by_index(int dev_index)
56 {
57         struct sr_dev_inst *sdi;
58         struct session_vdev *vdev;
59
60         /* TODO: Sanity checks on dev_index. */
61
62         if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
63                 sr_err("session driver: %s: device instance with device "
64                        "index %d was not found", __func__, dev_index);
65                 return NULL;
66         }
67
68         /* TODO: Is sdi->priv == NULL valid? */
69
70         vdev = sdi->priv;
71
72         return vdev;
73 }
74
75 /**
76  * TODO.
77  *
78  * @param fd TODO.
79  * @param revents TODO.
80  * @param cb_data TODO.
81  *
82  * @return TODO.
83  */
84 static int receive_data(int fd, int revents, void *cb_data)
85 {
86         struct sr_dev_inst *sdi;
87         struct session_vdev *vdev;
88         struct sr_datafeed_packet packet;
89         struct sr_datafeed_logic logic;
90         GSList *l;
91         void *buf;
92         int ret, got_data;
93
94         /* Avoid compiler warnings. */
95         (void)fd;
96         (void)revents;
97
98         sr_dbg("session_driver: feed chunk");
99
100         got_data = FALSE;
101         for (l = dev_insts; l; l = l->next) {
102                 sdi = l->data;
103                 vdev = sdi->priv;
104                 if (!vdev)
105                         /* already done with this instance */
106                         continue;
107
108                 if (!(buf = g_try_malloc(CHUNKSIZE))) {
109                         sr_err("session driver: %s: buf malloc failed",
110                                __func__);
111                         return FALSE; /* TODO: SR_ERR_MALLOC */
112                 }
113
114                 ret = zip_fread(vdev->capfile, buf, CHUNKSIZE);
115                 if (ret > 0) {
116                         got_data = TRUE;
117                         packet.type = SR_DF_LOGIC;
118                         packet.payload = &logic;
119                         logic.length = ret;
120                         logic.unitsize = vdev->unitsize;
121                         logic.data = buf;
122                         vdev->bytes_read += ret;
123                         sr_session_send(cb_data, &packet);
124                 } else {
125                         /* done with this capture file */
126                         zip_fclose(vdev->capfile);
127                         g_free(vdev->capturefile);
128                         g_free(vdev);
129                         sdi->priv = NULL;
130                 }
131         }
132
133         if (!got_data) {
134                 packet.type = SR_DF_END;
135                 sr_session_send(cb_data, &packet);
136         }
137
138         return TRUE;
139 }
140
141 /* driver callbacks */
142 static int hw_cleanup(void);
143
144 /**
145  * TODO.
146  *
147  * @param devinfo TODO.
148  *
149  * @return TODO.
150  */
151 static int hw_init(const char *devinfo)
152 {
153         sessionfile = g_strdup(devinfo);
154
155         return 0;
156 }
157
158 /**
159  * TODO.
160  */
161 static int hw_cleanup(void)
162 {
163         GSList *l;
164
165         for (l = dev_insts; l; l = l->next)
166                 sr_dev_inst_free(l->data);
167         g_slist_free(dev_insts);
168         dev_insts = NULL;
169
170         sr_session_source_remove(-1);
171
172         g_free(sessionfile);
173
174         return SR_OK;
175 }
176
177 static int hw_dev_open(int dev_index)
178 {
179         struct sr_dev_inst *sdi;
180
181         sdi = sr_dev_inst_new(dev_index, SR_ST_INITIALIZING,
182                               NULL, NULL, NULL);
183         if (!sdi)
184                 return SR_ERR;
185
186         if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdev)))) {
187                 sr_err("session driver: %s: sdi->priv malloc failed", __func__);
188                 return SR_ERR_MALLOC;
189         }
190
191         dev_insts = g_slist_append(dev_insts, sdi);
192
193         return SR_OK;
194 }
195
196 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
197 {
198         struct session_vdev *vdev;
199         void *info;
200
201         if (dev_info_id != SR_DI_CUR_SAMPLERATE)
202                 return NULL;
203
204         if (!(vdev = get_vdev_by_index(dev_index)))
205                 return NULL;
206
207         info = &vdev->samplerate;
208
209         return info;
210 }
211
212 static int hw_dev_status_get(int dev_index)
213 {
214         /* Avoid compiler warnings. */
215         (void)dev_index;
216
217         if (sr_dev_list() != NULL)
218                 return SR_OK;
219         else
220                 return SR_ERR;
221 }
222
223 /**
224  * Get the list of hardware capabilities.
225  *
226  * @return A pointer to the (hardware) capabilities of this virtual session
227  *         driver. This could be NULL, if no such capabilities exist.
228  */
229 static const int *hw_hwcap_get_all(void)
230 {
231         return hwcaps;
232 }
233
234 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
235 {
236         struct session_vdev *vdev;
237         const uint64_t *tmp_u64;
238
239         if (!(vdev = get_vdev_by_index(dev_index)))
240                 return SR_ERR;
241
242         switch (hwcap) {
243         case SR_HWCAP_SAMPLERATE:
244                 tmp_u64 = value;
245                 vdev->samplerate = *tmp_u64;
246                 sr_info("session driver: setting samplerate to %" PRIu64,
247                         vdev->samplerate);
248                 break;
249         case SR_HWCAP_CAPTUREFILE:
250                 vdev->capturefile = g_strdup(value);
251                 sr_info("session driver: setting capturefile to %s",
252                         vdev->capturefile);
253                 break;
254         case SR_HWCAP_CAPTURE_UNITSIZE:
255                 tmp_u64 = value;
256                 vdev->unitsize = *tmp_u64;
257                 break;
258         case SR_HWCAP_CAPTURE_NUM_PROBES:
259                 tmp_u64 = value;
260                 vdev->num_probes = *tmp_u64;
261                 break;
262         default:
263                 sr_err("session driver: %s: unknown capability %d requested",
264                        __func__, hwcap);
265                 return SR_ERR;
266         }
267
268         return SR_OK;
269 }
270
271 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
272 {
273         struct zip_stat zs;
274         struct session_vdev *vdev;
275         struct sr_datafeed_header *header;
276         struct sr_datafeed_packet *packet;
277         struct sr_datafeed_meta_logic meta;
278         int ret;
279
280         if (!(vdev = get_vdev_by_index(dev_index)))
281                 return SR_ERR;
282
283         sr_info("session_driver: opening archive %s file %s", sessionfile,
284                 vdev->capturefile);
285
286         if (!(vdev->archive = zip_open(sessionfile, 0, &ret))) {
287                 sr_err("session driver: Failed to open session file '%s': "
288                        "zip error %d\n", sessionfile, ret);
289                 return SR_ERR;
290         }
291
292         if (zip_stat(vdev->archive, vdev->capturefile, 0, &zs) == -1) {
293                 sr_err("session driver: Failed to check capture file '%s' in "
294                        "session file '%s'.", vdev->capturefile, sessionfile);
295                 return SR_ERR;
296         }
297
298         if (!(vdev->capfile = zip_fopen(vdev->archive, vdev->capturefile, 0))) {
299                 sr_err("session driver: Failed to open capture file '%s' in "
300                        "session file '%s'.", vdev->capturefile, sessionfile);
301                 return SR_ERR;
302         }
303
304         /* freewheeling source */
305         sr_session_source_add(-1, 0, 0, receive_data, cb_data);
306
307         if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
308                 sr_err("session driver: %s: packet malloc failed", __func__);
309                 return SR_ERR_MALLOC;
310         }
311
312         if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
313                 sr_err("session driver: %s: header malloc failed", __func__);
314                 return SR_ERR_MALLOC;
315         }
316
317         /* Send header packet to the session bus. */
318         packet->type = SR_DF_HEADER;
319         packet->payload = (unsigned char *)header;
320         header->feed_version = 1;
321         gettimeofday(&header->starttime, NULL);
322         sr_session_send(cb_data, packet);
323
324         /* Send metadata about the SR_DF_LOGIC packets to come. */
325         packet->type = SR_DF_META_LOGIC;
326         packet->payload = &meta;
327         meta.samplerate = vdev->samplerate;
328         meta.num_probes = vdev->num_probes;
329         sr_session_send(cb_data, packet);
330
331         g_free(header);
332         g_free(packet);
333
334         return SR_OK;
335 }
336
337 SR_PRIV struct sr_dev_driver session_driver = {
338         .name = "session",
339         .longname = "Session-emulating driver",
340         .api_version = 1,
341         .init = hw_init,
342         .cleanup = hw_cleanup,
343         .dev_open = hw_dev_open,
344         .dev_close = NULL,
345         .dev_info_get = hw_dev_info_get,
346         .dev_status_get = hw_dev_status_get,
347         .hwcap_get_all = hw_hwcap_get_all,
348         .dev_config_set = hw_dev_config_set,
349         .dev_acquisition_start = hw_dev_acquisition_start,
350         .dev_acquisition_stop = NULL,
351 };