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