]>
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 <glib.h> | |
26 | #include <sigrok.h> | |
27 | #include <config.h> | |
28 | ||
29 | ||
30 | /* There can only be one session at a time. */ | |
31 | struct session *session; | |
32 | int num_sources = 0; | |
33 | /* These live in hwplugin.c, for the frontend to override. */ | |
34 | extern source_callback_add source_cb_add; | |
35 | extern source_callback_remove source_cb_remove; | |
36 | ||
37 | struct source { | |
38 | int fd; | |
39 | int events; | |
40 | int timeout; | |
41 | receive_data_callback cb; | |
42 | void *user_data; | |
43 | }; | |
44 | ||
45 | struct source *sources = NULL; | |
46 | int source_timeout = -1; | |
47 | ||
48 | ||
49 | ||
50 | ||
51 | struct session *session_load(const char *filename) | |
52 | { | |
53 | struct session *session; | |
54 | ||
55 | /* Avoid compiler warnings. */ | |
56 | filename = filename; | |
57 | ||
58 | /* TODO: Implement. */ | |
59 | session = NULL; | |
60 | ||
61 | return session; | |
62 | } | |
63 | ||
64 | struct session *session_new(void) | |
65 | { | |
66 | session = calloc(1, sizeof(struct session)); | |
67 | ||
68 | return session; | |
69 | } | |
70 | ||
71 | void session_destroy(void) | |
72 | { | |
73 | g_slist_free(session->devices); | |
74 | ||
75 | /* TODO: Loop over protocol decoders and free them. */ | |
76 | ||
77 | g_free(session); | |
78 | } | |
79 | ||
80 | void session_device_clear(void) | |
81 | { | |
82 | g_slist_free(session->devices); | |
83 | session->devices = NULL; | |
84 | } | |
85 | ||
86 | int session_device_add(struct sr_device *device) | |
87 | { | |
88 | int ret; | |
89 | ||
90 | if (device->plugin && device->plugin->open) { | |
91 | ret = device->plugin->open(device->plugin_index); | |
92 | if (ret != SR_OK) | |
93 | return ret; | |
94 | } | |
95 | ||
96 | session->devices = g_slist_append(session->devices, device); | |
97 | ||
98 | return SR_OK; | |
99 | } | |
100 | ||
101 | void session_pa_clear(void) | |
102 | { | |
103 | /* | |
104 | * The protocols are pointers to the global set of PA plugins, | |
105 | * so don't free them. | |
106 | */ | |
107 | g_slist_free(session->analyzers); | |
108 | session->analyzers = NULL; | |
109 | } | |
110 | ||
111 | void session_pa_add(struct analyzer *an) | |
112 | { | |
113 | session->analyzers = g_slist_append(session->analyzers, an); | |
114 | } | |
115 | ||
116 | void session_datafeed_callback_clear(void) | |
117 | { | |
118 | g_slist_free(session->datafeed_callbacks); | |
119 | session->datafeed_callbacks = NULL; | |
120 | } | |
121 | ||
122 | void session_datafeed_callback_add(datafeed_callback callback) | |
123 | { | |
124 | session->datafeed_callbacks = | |
125 | g_slist_append(session->datafeed_callbacks, callback); | |
126 | } | |
127 | ||
128 | int session_start(void) | |
129 | { | |
130 | struct sr_device *device; | |
131 | GSList *l; | |
132 | int ret; | |
133 | ||
134 | g_message("starting session"); | |
135 | for (l = session->devices; l; l = l->next) { | |
136 | device = l->data; | |
137 | if ((ret = device->plugin->start_acquisition( | |
138 | device->plugin_index, device)) != SR_OK) | |
139 | break; | |
140 | } | |
141 | ||
142 | return ret; | |
143 | } | |
144 | ||
145 | void session_run(void) | |
146 | { | |
147 | GPollFD *fds, my_gpollfd; | |
148 | int ret, i; | |
149 | ||
150 | g_message("running session"); | |
151 | session->running = TRUE; | |
152 | fds = NULL; | |
153 | while (session->running) { | |
154 | if (fds) | |
155 | free(fds); | |
156 | ||
157 | /* Construct g_poll()'s array. */ | |
158 | fds = malloc(sizeof(GPollFD) * num_sources); | |
159 | for (i = 0; i < num_sources; i++) { | |
160 | #ifdef _WIN32 | |
161 | g_io_channel_win32_make_pollfd(&channels[0], | |
162 | sources[i].events, &my_gpollfd); | |
163 | #else | |
164 | my_gpollfd.fd = sources[i].fd; | |
165 | my_gpollfd.events = sources[i].events; | |
166 | fds[i] = my_gpollfd; | |
167 | #endif | |
168 | } | |
169 | ||
170 | ret = g_poll(fds, num_sources, source_timeout); | |
171 | ||
172 | for (i = 0; i < num_sources; i++) { | |
173 | if (fds[i].revents > 0 || (ret == 0 | |
174 | && source_timeout == sources[i].timeout)) { | |
175 | /* | |
176 | * Invoke the source's callback on an event, | |
177 | * or if the poll timeout out and this source | |
178 | * asked for that timeout. | |
179 | */ | |
180 | sources[i].cb(fds[i].fd, fds[i].revents, | |
181 | sources[i].user_data); | |
182 | } | |
183 | } | |
184 | } | |
185 | free(fds); | |
186 | ||
187 | } | |
188 | ||
189 | void session_halt(void) | |
190 | { | |
191 | ||
192 | g_message("halting session"); | |
193 | session->running = FALSE; | |
194 | ||
195 | } | |
196 | ||
197 | void session_stop(void) | |
198 | { | |
199 | struct sr_device *device; | |
200 | GSList *l; | |
201 | ||
202 | g_message("stopping session"); | |
203 | session->running = FALSE; | |
204 | for (l = session->devices; l; l = l->next) { | |
205 | device = l->data; | |
206 | if (device->plugin) | |
207 | device->plugin->stop_acquisition(device->plugin_index, device); | |
208 | } | |
209 | ||
210 | } | |
211 | ||
212 | void session_bus(struct sr_device *device, struct sr_datafeed_packet *packet) | |
213 | { | |
214 | GSList *l; | |
215 | datafeed_callback cb; | |
216 | ||
217 | /* | |
218 | * TODO: Send packet through PD pipe, and send the output of that to | |
219 | * the callbacks as well. | |
220 | */ | |
221 | for (l = session->datafeed_callbacks; l; l = l->next) { | |
222 | cb = l->data; | |
223 | cb(device, packet); | |
224 | } | |
225 | } | |
226 | ||
227 | int session_save(char *filename) | |
228 | { | |
229 | GSList *l, *p, *d; | |
230 | FILE *meta; | |
231 | struct sr_device *device; | |
232 | struct probe *probe; | |
233 | struct datastore *ds; | |
234 | struct zip *zipfile; | |
235 | struct zip_source *versrc, *metasrc, *logicsrc; | |
236 | int bufcnt, devcnt, tmpfile, ret, error; | |
237 | char version[1], rawname[16], metafile[32], *newfn, *buf; | |
238 | ||
239 | newfn = g_malloc(strlen(filename) + 10); | |
240 | strcpy(newfn, filename); | |
241 | if (strstr(filename, ".sigrok") != filename+strlen(filename)-7) | |
242 | strcat(newfn, ".sigrok"); | |
243 | ||
244 | /* Quietly delete it first, libzip wants replace ops otherwise. */ | |
245 | unlink(newfn); | |
246 | if (!(zipfile = zip_open(newfn, ZIP_CREATE, &error))) | |
247 | return SR_ERR; | |
248 | g_free(newfn); | |
249 | ||
250 | /* "version" */ | |
251 | version[0] = '1'; | |
252 | if (!(versrc = zip_source_buffer(zipfile, version, 1, 0))) | |
253 | return SR_ERR; | |
254 | if (zip_add(zipfile, "version", versrc) == -1) { | |
255 | g_message("error saving version into zipfile: %s", | |
256 | zip_strerror(zipfile)); | |
257 | return SR_ERR; | |
258 | } | |
259 | ||
260 | /* init "metadata" */ | |
261 | strcpy(metafile, "sigrok-meta-XXXXXX"); | |
262 | if ((tmpfile = g_mkstemp(metafile)) == -1) | |
263 | return SR_ERR; | |
264 | close(tmpfile); | |
265 | meta = fopen(metafile, "wb"); | |
266 | fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION); | |
267 | /* TODO: save protocol decoders used */ | |
268 | ||
269 | /* all datastores in all devices */ | |
270 | devcnt = 1; | |
271 | for (l = session->devices; l; l = l->next) { | |
272 | device = l->data; | |
273 | /* metadata */ | |
274 | fprintf(meta, "[device]\n"); | |
275 | if (device->plugin) | |
276 | fprintf(meta, "driver = %s\n", device->plugin->name); | |
277 | ||
278 | ds = device->datastore; | |
279 | if (ds) { | |
280 | /* metadata */ | |
281 | fprintf(meta, "capturefile = logic-%d\n", devcnt); | |
282 | for (p = device->probes; p; p = p->next) { | |
283 | probe = p->data; | |
284 | if (probe->enabled) { | |
285 | fprintf(meta, "probe %d", probe->index); | |
286 | if (probe->name) | |
287 | fprintf(meta, " name \"%s\"", probe->name); | |
288 | if (probe->trigger) | |
289 | fprintf(meta, " trigger \"%s\"", | |
290 | probe->trigger); | |
291 | fprintf(meta, "\n"); | |
292 | } | |
293 | } | |
294 | ||
295 | /* dump datastore into logic-n */ | |
296 | buf = malloc(ds->num_units * ds->ds_unitsize + | |
297 | DATASTORE_CHUNKSIZE); | |
298 | bufcnt = 0; | |
299 | for (d = ds->chunklist; d; d = d->next) { | |
300 | memcpy(buf + bufcnt, d->data, | |
301 | DATASTORE_CHUNKSIZE); | |
302 | bufcnt += DATASTORE_CHUNKSIZE; | |
303 | } | |
304 | if (!(logicsrc = zip_source_buffer(zipfile, buf, | |
305 | ds->num_units * ds->ds_unitsize, TRUE))) | |
306 | return SR_ERR; | |
307 | snprintf(rawname, 15, "logic-%d", devcnt); | |
308 | if (zip_add(zipfile, rawname, logicsrc) == -1) | |
309 | return SR_ERR; | |
310 | } | |
311 | devcnt++; | |
312 | } | |
313 | fclose(meta); | |
314 | ||
315 | if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) | |
316 | return SR_ERR; | |
317 | if (zip_add(zipfile, "metadata", metasrc) == -1) | |
318 | return SR_ERR; | |
319 | ||
320 | if ((ret = zip_close(zipfile)) == -1) { | |
321 | g_message("error saving zipfile: %s", zip_strerror(zipfile)); | |
322 | return SR_ERR; | |
323 | } | |
324 | ||
325 | unlink(metafile); | |
326 | ||
327 | return SR_OK; | |
328 | } | |
329 | ||
330 | void session_source_add(int fd, int events, int timeout, | |
331 | receive_data_callback callback, void *user_data) | |
332 | { | |
333 | struct source *new_sources, *s; | |
334 | ||
335 | new_sources = calloc(1, sizeof(struct source) * (num_sources + 1)); | |
336 | ||
337 | if (sources) { | |
338 | memcpy(new_sources, sources, | |
339 | sizeof(struct source) * num_sources); | |
340 | free(sources); | |
341 | } | |
342 | ||
343 | s = &new_sources[num_sources++]; | |
344 | s->fd = fd; | |
345 | s->events = events; | |
346 | s->timeout = timeout; | |
347 | s->cb = callback; | |
348 | s->user_data = user_data; | |
349 | sources = new_sources; | |
350 | ||
351 | if (timeout != source_timeout && timeout > 0 | |
352 | && (source_timeout == -1 || timeout < source_timeout)) | |
353 | source_timeout = timeout; | |
354 | } | |
355 | ||
356 | void session_source_remove(int fd) | |
357 | { | |
358 | struct source *new_sources; | |
359 | int old, new; | |
360 | ||
361 | if (!sources) | |
362 | return; | |
363 | ||
364 | new_sources = calloc(1, sizeof(struct source) * num_sources); | |
365 | for (old = 0; old < num_sources; old++) | |
366 | if (sources[old].fd != fd) | |
367 | memcpy(&new_sources[new++], &sources[old], | |
368 | sizeof(struct source)); | |
369 | ||
370 | if (old != new) { | |
371 | free(sources); | |
372 | sources = new_sources; | |
373 | num_sources--; | |
374 | } else { | |
375 | /* Target fd was not found. */ | |
376 | free(new_sources); | |
377 | } | |
378 | } | |
379 |