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