]> sigrok.org Git - libsigrok.git/blame - session_driver.c
Code drop from DreamSourceLabs first source release.
[libsigrok.git] / session_driver.c
CommitLineData
7d658874 1/*
50985c20 2 * This file is part of the libsigrok project.
7d658874 3 *
13d8e03c 4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7d658874
BV
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>
45c59c8b
BV
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
7d658874 28
29a27196
UH
29/* Message logging helpers with subsystem-specific prefix string. */
30#define LOG_PREFIX "virtual-session: "
31#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
32#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
33#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
34#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
35#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
36#define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
a885ce3e 37
7d658874 38/* size of payloads sent across the session bus */
b4bd7088 39/** @cond PRIVATE */
8ff6afc9 40#define CHUNKSIZE (512 * 1024)
b4bd7088 41/** @endcond */
7d658874 42
bb7ef793 43struct session_vdev {
40dda2c3 44 char *sessionfile;
7d658874
BV
45 char *capturefile;
46 struct zip *archive;
47 struct zip_file *capfile;
cb1e389c 48 int bytes_read;
7d658874 49 uint64_t samplerate;
f1b296fc 50 uint64_t total_samples;
7d658874
BV
51 int unitsize;
52 int num_probes;
53};
54
d68e2d1a 55static GSList *dev_insts = NULL;
915f7cc8 56static const int hwcaps[] = {
1953564a
BV
57 SR_CONF_CAPTUREFILE,
58 SR_CONF_CAPTURE_UNITSIZE,
7d658874
BV
59 0,
60};
61
f1b296fc 62static int receive_data(int fd, int revents, const struct sr_dev_inst *cb_sdi)
7d658874 63{
d68e2d1a 64 struct sr_dev_inst *sdi;
bb7ef793 65 struct session_vdev *vdev;
7d658874 66 struct sr_datafeed_packet packet;
9c939c51 67 struct sr_datafeed_logic logic;
7d658874
BV
68 GSList *l;
69 void *buf;
70 int ret, got_data;
71
cb93f8a9
UH
72 (void)fd;
73 (void)revents;
7d658874 74
a885ce3e 75 sr_dbg("Feed chunk.");
7d658874
BV
76
77 got_data = FALSE;
d68e2d1a 78 for (l = dev_insts; l; l = l->next) {
7d658874 79 sdi = l->data;
bb7ef793
UH
80 vdev = sdi->priv;
81 if (!vdev)
7d658874
BV
82 /* already done with this instance */
83 continue;
84
b53738ba 85 if (!(buf = g_try_malloc(CHUNKSIZE))) {
a885ce3e 86 sr_err("%s: buf malloc failed", __func__);
a2e46460 87 return FALSE;
b53738ba
UH
88 }
89
bb7ef793 90 ret = zip_fread(vdev->capfile, buf, CHUNKSIZE);
7d658874
BV
91 if (ret > 0) {
92 got_data = TRUE;
93 packet.type = SR_DF_LOGIC;
9c939c51
BV
94 packet.payload = &logic;
95 logic.length = ret;
bb7ef793 96 logic.unitsize = vdev->unitsize;
9c939c51 97 logic.data = buf;
bb7ef793 98 vdev->bytes_read += ret;
f1b296fc 99 sr_session_send(cb_sdi, &packet);
7d658874
BV
100 } else {
101 /* done with this capture file */
bb7ef793
UH
102 zip_fclose(vdev->capfile);
103 g_free(vdev->capturefile);
104 g_free(vdev);
7d658874
BV
105 sdi->priv = NULL;
106 }
107 }
108
109 if (!got_data) {
110 packet.type = SR_DF_END;
f1b296fc 111 sr_session_send(cb_sdi, &packet);
b863fb1b 112 sr_session_source_remove(-1);
7d658874
BV
113 }
114
115 return TRUE;
116}
117
118/* driver callbacks */
57ab7d9f 119static int hw_cleanup(void);
7d658874 120
34f06b90 121static int hw_init(struct sr_context *sr_ctx)
7d658874 122{
34f06b90
PS
123 (void)sr_ctx;
124
61136ea6 125 return SR_OK;
7d658874
BV
126}
127
57ab7d9f 128static int hw_cleanup(void)
7d658874
BV
129{
130 GSList *l;
131
d68e2d1a 132 for (l = dev_insts; l; l = l->next)
d3683c42 133 sr_dev_inst_free(l->data);
d68e2d1a
UH
134 g_slist_free(dev_insts);
135 dev_insts = NULL;
805e9640 136
57ab7d9f 137 return SR_OK;
7d658874
BV
138}
139
25a0f108 140static int hw_dev_open(struct sr_dev_inst *sdi)
7d658874 141{
bb7ef793 142 if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdev)))) {
a885ce3e 143 sr_err("%s: sdi->priv malloc failed", __func__);
b53738ba
UH
144 return SR_ERR_MALLOC;
145 }
146
d68e2d1a 147 dev_insts = g_slist_append(dev_insts, sdi);
7d658874
BV
148
149 return SR_OK;
150}
151
003595ac 152static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
7d658874 153{
bb7ef793 154 struct session_vdev *vdev;
7d658874 155
035a1078 156 switch (id) {
123e1313 157 case SR_CONF_SAMPLERATE:
387014de
BV
158 if (sdi) {
159 vdev = sdi->priv;
003595ac 160 *data = g_variant_new_uint64(vdev->samplerate);
387014de
BV
161 } else
162 return SR_ERR;
163 break;
f1b296fc
BV
164 case SR_CONF_LIMIT_SAMPLES:
165 if (sdi) {
166 vdev = sdi->priv;
167 *data = g_variant_new_uint64(vdev->total_samples);
168 } else
169 return SR_ERR;
170 break;
387014de
BV
171 default:
172 return SR_ERR_ARG;
173 }
7d658874 174
387014de 175 return SR_OK;
7d658874
BV
176}
177
003595ac 178static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
7d658874 179{
bb7ef793 180 struct session_vdev *vdev;
7d658874 181
6f4b1868 182 vdev = sdi->priv;
7d658874 183
035a1078 184 switch (id) {
1953564a 185 case SR_CONF_SAMPLERATE:
003595ac 186 vdev->samplerate = g_variant_get_uint64(data);
a885ce3e 187 sr_info("Setting samplerate to %" PRIu64 ".", vdev->samplerate);
7d658874 188 break;
1953564a 189 case SR_CONF_SESSIONFILE:
003595ac 190 vdev->sessionfile = g_strdup(g_variant_get_string(data, NULL));
a885ce3e 191 sr_info("Setting sessionfile to '%s'.", vdev->sessionfile);
40dda2c3 192 break;
1953564a 193 case SR_CONF_CAPTUREFILE:
003595ac 194 vdev->capturefile = g_strdup(g_variant_get_string(data, NULL));
a885ce3e 195 sr_info("Setting capturefile to '%s'.", vdev->capturefile);
7d658874 196 break;
1953564a 197 case SR_CONF_CAPTURE_UNITSIZE:
003595ac 198 vdev->unitsize = g_variant_get_uint64(data);
7d658874 199 break;
f1b296fc
BV
200 case SR_CONF_LIMIT_SAMPLES:
201 vdev->total_samples = g_variant_get_uint64(data);
202 break;
1953564a 203 case SR_CONF_CAPTURE_NUM_PROBES:
003595ac 204 vdev->num_probes = g_variant_get_uint64(data);
7d658874
BV
205 break;
206 default:
035a1078 207 sr_err("Unknown capability: %d.", id);
7d658874
BV
208 return SR_ERR;
209 }
210
211 return SR_OK;
212}
213
003595ac 214static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
9a6517d1
BV
215{
216
217 (void)sdi;
218
219 switch (key) {
220 case SR_CONF_DEVICE_OPTIONS:
f1b296fc
BV
221// *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
222// hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
223 *data = g_variant_new_from_data(G_VARIANT_TYPE("ai"),
224 hwcaps, ARRAY_SIZE(hwcaps)*sizeof(int32_t), TRUE, NULL, NULL);
9a6517d1
BV
225 break;
226 default:
227 return SR_ERR_ARG;
228 }
229
230 return SR_OK;
231}
232
4d684427
BV
233static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
234 void *cb_data)
7d658874
BV
235{
236 struct zip_stat zs;
bb7ef793 237 struct session_vdev *vdev;
ebc34738 238 int ret;
7d658874 239
4d684427 240 vdev = sdi->priv;
7d658874 241
a885ce3e 242 sr_info("Opening archive %s file %s", vdev->sessionfile,
bb7ef793 243 vdev->capturefile);
7d658874 244
40dda2c3 245 if (!(vdev->archive = zip_open(vdev->sessionfile, 0, &ret))) {
a885ce3e 246 sr_err("Failed to open session file '%s': "
40dda2c3 247 "zip error %d\n", vdev->sessionfile, ret);
7d658874
BV
248 return SR_ERR;
249 }
250
bb7ef793 251 if (zip_stat(vdev->archive, vdev->capturefile, 0, &zs) == -1) {
a885ce3e 252 sr_err("Failed to check capture file '%s' in "
40dda2c3 253 "session file '%s'.", vdev->capturefile, vdev->sessionfile);
7d658874
BV
254 return SR_ERR;
255 }
256
bb7ef793 257 if (!(vdev->capfile = zip_fopen(vdev->archive, vdev->capturefile, 0))) {
a885ce3e 258 sr_err("Failed to open capture file '%s' in "
40dda2c3 259 "session file '%s'.", vdev->capturefile, vdev->sessionfile);
7d658874
BV
260 return SR_ERR;
261 }
262
4afdfd46 263 /* Send header packet to the session bus. */
f1b296fc 264 std_session_send_df_header(sdi, LOG_PREFIX);
4afdfd46 265
7d658874 266 /* freewheeling source */
f1b296fc 267 sr_session_source_add(-1, 0, 0, receive_data, sdi);
7d658874 268
7d658874
BV
269 return SR_OK;
270}
271
b4bd7088 272/** @private */
c09f0b57 273SR_PRIV struct sr_dev_driver session_driver = {
a885ce3e 274 .name = "virtual-session",
5097b0d0
UH
275 .longname = "Session-emulating driver",
276 .api_version = 1,
277 .init = hw_init,
278 .cleanup = hw_cleanup,
035a1078
BV
279 .config_get = config_get,
280 .config_set = config_set,
9a6517d1 281 .config_list = config_list,
e7eb703f
UH
282 .dev_open = hw_dev_open,
283 .dev_close = NULL,
69040b7c
UH
284 .dev_acquisition_start = hw_dev_acquisition_start,
285 .dev_acquisition_stop = NULL,
7d658874 286};