2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
29 /* There can only be one session at a time. */
30 struct session *session;
32 struct session *session_load(const char *filename)
34 struct session *session;
36 /* Avoid compiler warnings. */
39 /* TODO: Implement. */
45 struct session *session_new(void)
47 session = calloc(1, sizeof(struct session));
52 void session_destroy(void)
54 g_slist_free(session->devices);
56 /* TODO: Loop over protocol decoders and free them. */
61 void session_device_clear(void)
63 g_slist_free(session->devices);
64 session->devices = NULL;
67 int session_device_add(struct device *device)
71 if (device->plugin && device->plugin->open) {
72 ret = device->plugin->open(device->plugin_index);
77 session->devices = g_slist_append(session->devices, device);
82 void session_pa_clear(void)
85 * The protocols are pointers to the global set of PA plugins,
88 g_slist_free(session->analyzers);
89 session->analyzers = NULL;
92 void session_pa_add(struct analyzer *an)
94 session->analyzers = g_slist_append(session->analyzers, an);
97 void session_datafeed_callback_clear(void)
99 g_slist_free(session->datafeed_callbacks);
100 session->datafeed_callbacks = NULL;
103 void session_datafeed_callback_add(datafeed_callback callback)
105 session->datafeed_callbacks =
106 g_slist_append(session->datafeed_callbacks, callback);
109 int session_start(void)
111 struct device *device;
115 g_message("starting acquisition");
116 for (l = session->devices; l; l = l->next) {
118 if ((ret = device->plugin->start_acquisition(
119 device->plugin_index, device)) != SIGROK_OK)
126 void session_stop(void)
128 struct device *device;
131 g_message("stopping acquisition");
132 for (l = session->devices; l; l = l->next) {
135 device->plugin->stop_acquisition(device->plugin_index, device);
139 void session_bus(struct device *device, struct datafeed_packet *packet)
142 datafeed_callback cb;
145 * TODO: Send packet through PD pipe, and send the output of that to
146 * the callbacks as well.
148 for (l = session->datafeed_callbacks; l; l = l->next) {
154 int session_save(char *filename)
158 struct device *device;
160 struct datastore *ds;
162 struct zip_source *versrc, *metasrc, *logicsrc;
163 int bufcnt, devcnt, tmpfile, ret, error;
164 char version[1], rawname[16], metafile[32], *newfn, *buf;
166 newfn = g_malloc(strlen(filename) + 10);
167 strcpy(newfn, filename);
168 if (strstr(filename, ".sigrok") != filename+strlen(filename)-7)
169 strcat(newfn, ".sigrok");
171 /* Quietly delete it first, libzip wants replace ops otherwise. */
173 if (!(zipfile = zip_open(newfn, ZIP_CREATE, &error)))
179 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
181 if (zip_add(zipfile, "version", versrc) == -1) {
182 g_message("error saving version into zipfile: %s",
183 zip_strerror(zipfile));
187 /* init "metadata" */
188 strcpy(metafile, "sigrok-meta-XXXXXX");
189 if ((tmpfile = g_mkstemp(metafile)) == -1)
192 meta = fopen(metafile, "wb");
193 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
194 /* TODO: save protocol decoders used */
196 /* all datastores in all devices */
198 for (l = session->devices; l; l = l->next) {
201 fprintf(meta, "[device]\n");
203 fprintf(meta, "driver = %s\n", device->plugin->name);
205 ds = device->datastore;
208 fprintf(meta, "capturefile = logic-%d\n", devcnt);
209 for (p = device->probes; p; p = p->next) {
211 if (probe->enabled) {
212 fprintf(meta, "probe %d", probe->index);
214 fprintf(meta, " name \"%s\"", probe->name);
216 fprintf(meta, " trigger \"%s\"",
222 /* dump datastore into logic-n */
223 buf = malloc(ds->num_units * ds->ds_unitsize +
224 DATASTORE_CHUNKSIZE);
226 for (d = ds->chunklist; d; d = d->next) {
227 memcpy(buf + bufcnt, d->data,
228 DATASTORE_CHUNKSIZE);
229 bufcnt += DATASTORE_CHUNKSIZE;
231 if (!(logicsrc = zip_source_buffer(zipfile, buf,
232 ds->num_units * ds->ds_unitsize, TRUE)))
234 snprintf(rawname, 15, "logic-%d", devcnt);
235 if (zip_add(zipfile, rawname, logicsrc) == -1)
242 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
244 if (zip_add(zipfile, "metadata", metasrc) == -1)
247 if ((ret = zip_close(zipfile)) == -1) {
248 g_message("error saving zipfile: %s", zip_strerror(zipfile));