]> sigrok.org Git - libsigrok.git/blame_incremental - session.c
Make the demo driver work.
[libsigrok.git] / session.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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 <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <string.h>
24#include <zip.h>
25#include <sigrok.h>
26
27/* There can only be one session at a time. */
28struct session *session;
29
30struct session *session_load(const char *filename)
31{
32 struct session *session;
33
34 /* TODO: Implement. */
35 session = NULL;
36
37 /* QUICK HACK */
38 filename = filename;
39
40 return session;
41}
42
43struct session *session_new(void)
44{
45 session = calloc(1, sizeof(struct session));
46
47 return session;
48}
49
50void session_destroy(void)
51{
52 g_slist_free(session->devices);
53
54 /* TODO: Loop over protocols and free them. */
55
56 g_free(session);
57}
58
59void session_device_clear(void)
60{
61 g_slist_free(session->devices);
62 session->devices = NULL;
63}
64
65int session_device_add(struct device *device)
66{
67 int ret;
68
69 ret = device->plugin->open(device->plugin_index);
70 if (ret == SIGROK_OK)
71 session->devices = g_slist_append(session->devices, device);
72
73 return ret;
74}
75
76void session_pa_clear(void)
77{
78 /*
79 * The protocols are pointers to the global set of PA plugins,
80 * so don't free them.
81 */
82 g_slist_free(session->analyzers);
83 session->analyzers = NULL;
84}
85
86void session_pa_add(struct analyzer *an)
87{
88 session->analyzers = g_slist_append(session->analyzers, an);
89}
90
91void session_datafeed_callback_clear(void)
92{
93 g_slist_free(session->datafeed_callbacks);
94 session->datafeed_callbacks = NULL;
95}
96
97void session_datafeed_callback_add(datafeed_callback callback)
98{
99 session->datafeed_callbacks =
100 g_slist_append(session->datafeed_callbacks, callback);
101}
102
103int session_start(void)
104{
105 struct device *device;
106 GSList *l;
107 int ret;
108
109 g_message("starting acquisition");
110 for (l = session->devices; l; l = l->next) {
111 device = l->data;
112 if ((ret = device->plugin->start_acquisition(
113 device->plugin_index, device)) != SIGROK_OK)
114 break;
115 }
116
117 return ret;
118}
119
120void session_stop(void)
121{
122 struct device *device;
123 GSList *l;
124
125 g_message("stopping acquisition");
126 for (l = session->devices; l; l = l->next) {
127 device = l->data;
128 device->plugin->stop_acquisition(device->plugin_index, device);
129 }
130}
131
132void session_bus(struct device *device, struct datafeed_packet *packet)
133{
134 GSList *l;
135 datafeed_callback cb;
136
137 /*
138 * TODO: Send packet through PA pipe, and send the output of that to
139 * the callbacks as well.
140 */
141 for (l = session->datafeed_callbacks; l; l = l->next) {
142 cb = l->data;
143 cb(device, packet);
144 }
145}
146
147void make_metadata(char *filename)
148{
149 GSList *l, *p;
150 struct device *device;
151 struct probe *probe;
152 FILE *f;
153 int devcnt;
154
155 f = fopen(filename, "wb");
156
157 /* General */
158
159 /* Devices */
160 devcnt = 1;
161 for (l = session->devices; l; l = l->next) {
162 device = l->data;
163 fprintf(f, "[device]\n");
164 fprintf(f, "driver = %s\n", device->plugin->name);
165
166 if (device->datastore)
167 fprintf(f, "capturefile = raw-%d\n", devcnt);
168
169 for (p = device->probes; p; p = p->next) {
170 probe = p->data;
171 if (probe->enabled) {
172 fprintf(f, "probe %d", probe->index);
173 if (probe->name)
174 fprintf(f, " name \"%s\"", probe->name);
175 if (probe->trigger)
176 fprintf(f, " trigger \"%s\"",
177 probe->trigger);
178 fprintf(f, "\n");
179 }
180 }
181 devcnt++;
182 }
183
184 /* TODO: Protocol analyzers */
185
186 fclose(f);
187}
188
189int session_save(char *filename)
190{
191 GSList *l, *d;
192 struct device *device;
193 struct datastore *ds;
194 struct zip *zipfile;
195 struct zip_source *src;
196 int bufcnt, devcnt, tmpfile, ret, error;
197 char version[1], rawname[16], metafile[32], *buf;
198
199 /* Quietly delete it first, libzip wants replace ops otherwise. */
200 unlink(filename);
201
202 if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
203 return SIGROK_ERR;
204
205 /* Version */
206 version[0] = '1';
207 if (!(src = zip_source_buffer(zipfile, version, 1, 0)))
208 return SIGROK_ERR;
209 if (zip_add(zipfile, "version", src) == -1) {
210 g_message("error saving version into zipfile: %s",
211 zip_strerror(zipfile));
212 return SIGROK_ERR;
213 }
214
215 /* Metadata */
216 strcpy(metafile, "sigrok-meta-XXXXXX");
217 if ((tmpfile = g_mkstemp(metafile)) == -1)
218 return SIGROK_ERR;
219 close(tmpfile);
220 make_metadata(metafile);
221 if (!(src = zip_source_file(zipfile, metafile, 0, -1)))
222 return SIGROK_ERR;
223 if (zip_add(zipfile, "metadata", src) == -1)
224 return SIGROK_ERR;
225 unlink(metafile);
226
227 /* Raw */
228 devcnt = 1;
229 for (l = session->devices; l; l = l->next) {
230 device = l->data;
231 ds = device->datastore;
232 if (ds) {
233 buf = malloc(ds->num_units * ds->ds_unitsize +
234 DATASTORE_CHUNKSIZE);
235 bufcnt = 0;
236 for (d = ds->chunklist; d; d = d->next) {
237 memcpy(buf + bufcnt, d->data,
238 DATASTORE_CHUNKSIZE);
239 bufcnt += DATASTORE_CHUNKSIZE;
240 }
241 if (!(src = zip_source_buffer(zipfile, buf,
242 ds->num_units * ds->ds_unitsize, TRUE)))
243 return SIGROK_ERR;
244 snprintf(rawname, 15, "raw-%d", devcnt);
245 if (zip_add(zipfile, rawname, src) == -1)
246 return SIGROK_ERR;
247 }
248 devcnt++;
249 }
250
251 if ((ret = zip_close(zipfile)) == -1) {
252 g_message("error saving zipfile: %s", zip_strerror(zipfile));
253 return SIGROK_ERR;
254 }
255
256 return SIGROK_OK;
257}