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