]>
Commit | Line | Data |
---|---|---|
7d658874 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
7d658874 BV |
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 <sys/types.h> | |
21 | #include <sys/stat.h> | |
22 | #include <fcntl.h> | |
23 | #include <unistd.h> | |
24 | #include <sys/time.h> | |
25 | #include <zip.h> | |
b7f09cf8 UH |
26 | #include "sigrok.h" |
27 | #include "sigrok-internal.h" | |
7d658874 BV |
28 | |
29 | /* size of payloads sent across the session bus */ | |
8ff6afc9 | 30 | #define CHUNKSIZE (512 * 1024) |
7d658874 BV |
31 | |
32 | struct session_vdevice { | |
33 | char *capturefile; | |
34 | struct zip *archive; | |
35 | struct zip_file *capfile; | |
cb1e389c | 36 | int bytes_read; |
7d658874 BV |
37 | uint64_t samplerate; |
38 | int unitsize; | |
39 | int num_probes; | |
40 | }; | |
41 | ||
42 | static char *sessionfile = NULL; | |
43 | static GSList *device_instances = NULL; | |
44 | static int capabilities[] = { | |
45 | SR_HWCAP_CAPTUREFILE, | |
46 | SR_HWCAP_CAPTURE_UNITSIZE, | |
47 | 0, | |
48 | }; | |
49 | ||
9f45fb3a UH |
50 | /** |
51 | * TODO. | |
52 | * | |
53 | * @param device_index TODO. | |
54 | */ | |
7d658874 BV |
55 | static struct session_vdevice *get_vdevice_by_index(int device_index) |
56 | { | |
57 | struct sr_device_instance *sdi; | |
58 | struct session_vdevice *vdevice; | |
59 | ||
9f45fb3a UH |
60 | /* TODO: Sanity checks on device_index. */ |
61 | ||
62 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { | |
63 | sr_err("session driver: %s: device instance with device " | |
64 | "index %d was not found", __func__, device_index); | |
7d658874 | 65 | return NULL; |
9f45fb3a UH |
66 | } |
67 | ||
68 | /* TODO: Is sdi->priv == NULL valid? */ | |
7d658874 BV |
69 | |
70 | vdevice = sdi->priv; | |
71 | ||
72 | return vdevice; | |
73 | } | |
74 | ||
9f45fb3a UH |
75 | /** |
76 | * TODO. | |
77 | * | |
78 | * @param fd TODO. | |
79 | * @param revents TODO. | |
80 | * @param session_data TODO. | |
81 | * | |
82 | * @return TODO. | |
83 | */ | |
9c939c51 | 84 | static int feed_chunk(int fd, int revents, void *session_data) |
7d658874 BV |
85 | { |
86 | struct sr_device_instance *sdi; | |
87 | struct session_vdevice *vdevice; | |
88 | struct sr_datafeed_packet packet; | |
9c939c51 | 89 | struct sr_datafeed_logic logic; |
7d658874 BV |
90 | GSList *l; |
91 | void *buf; | |
92 | int ret, got_data; | |
93 | ||
cb93f8a9 UH |
94 | /* Avoid compiler warnings. */ |
95 | (void)fd; | |
96 | (void)revents; | |
7d658874 | 97 | |
b08024a8 | 98 | sr_dbg("session_driver: feed chunk"); |
7d658874 BV |
99 | |
100 | got_data = FALSE; | |
101 | for (l = device_instances; l; l = l->next) { | |
102 | sdi = l->data; | |
103 | vdevice = sdi->priv; | |
104 | if (!vdevice) | |
105 | /* already done with this instance */ | |
106 | continue; | |
107 | ||
b53738ba UH |
108 | if (!(buf = g_try_malloc(CHUNKSIZE))) { |
109 | sr_err("session: %s: buf malloc failed", __func__); | |
133a37bf | 110 | return FALSE; /* TODO: SR_ERR_MALLOC */ |
b53738ba UH |
111 | } |
112 | ||
7d658874 BV |
113 | ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE); |
114 | if (ret > 0) { | |
115 | got_data = TRUE; | |
116 | packet.type = SR_DF_LOGIC; | |
9c939c51 BV |
117 | packet.payload = &logic; |
118 | logic.length = ret; | |
119 | logic.unitsize = vdevice->unitsize; | |
120 | logic.data = buf; | |
cb1e389c | 121 | vdevice->bytes_read += ret; |
9c939c51 | 122 | sr_session_bus(session_data, &packet); |
7d658874 BV |
123 | } else { |
124 | /* done with this capture file */ | |
125 | zip_fclose(vdevice->capfile); | |
126 | g_free(vdevice->capturefile); | |
127 | g_free(vdevice); | |
128 | sdi->priv = NULL; | |
129 | } | |
130 | } | |
131 | ||
132 | if (!got_data) { | |
133 | packet.type = SR_DF_END; | |
9c939c51 | 134 | sr_session_bus(session_data, &packet); |
7d658874 BV |
135 | } |
136 | ||
137 | return TRUE; | |
138 | } | |
139 | ||
140 | /* driver callbacks */ | |
805e9640 | 141 | static void hw_cleanup(void); |
7d658874 | 142 | |
9f45fb3a UH |
143 | /** |
144 | * TODO. | |
145 | * | |
146 | * @param deviceinfo TODO. | |
147 | * | |
148 | * @return TODO. | |
149 | */ | |
54ac5277 | 150 | static int hw_init(const char *deviceinfo) |
7d658874 BV |
151 | { |
152 | ||
153 | sessionfile = g_strdup(deviceinfo); | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
9f45fb3a UH |
158 | /** |
159 | * TODO. | |
160 | * | |
161 | */ | |
7d658874 BV |
162 | static void hw_cleanup(void) |
163 | { | |
164 | GSList *l; | |
165 | ||
166 | for (l = device_instances; l; l = l->next) | |
167 | sr_device_instance_free(l->data); | |
168 | ||
805e9640 GM |
169 | g_slist_free(device_instances); |
170 | device_instances = NULL; | |
171 | ||
172 | sr_session_source_remove(-1); | |
173 | ||
7d658874 | 174 | g_free(sessionfile); |
7d658874 BV |
175 | } |
176 | ||
177 | static int hw_opendev(int device_index) | |
178 | { | |
179 | struct sr_device_instance *sdi; | |
180 | ||
181 | sdi = sr_device_instance_new(device_index, SR_ST_INITIALIZING, | |
182 | NULL, NULL, NULL); | |
183 | if (!sdi) | |
184 | return SR_ERR; | |
b53738ba UH |
185 | |
186 | if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) { | |
187 | sr_err("session: %s: sdi->priv malloc failed", __func__); | |
188 | return SR_ERR_MALLOC; | |
189 | } | |
190 | ||
7d658874 BV |
191 | device_instances = g_slist_append(device_instances, sdi); |
192 | ||
193 | return SR_OK; | |
194 | } | |
195 | ||
7d658874 BV |
196 | static void *hw_get_device_info(int device_index, int device_info_id) |
197 | { | |
198 | struct session_vdevice *vdevice; | |
199 | void *info; | |
200 | ||
b8c2f85f BV |
201 | if (device_info_id != SR_DI_CUR_SAMPLERATE) |
202 | return NULL; | |
203 | ||
7d658874 BV |
204 | if (!(vdevice = get_vdevice_by_index(device_index))) |
205 | return NULL; | |
206 | ||
207 | info = &vdevice->samplerate; | |
208 | ||
209 | return info; | |
210 | } | |
211 | ||
212 | static int hw_get_status(int device_index) | |
213 | { | |
cb93f8a9 UH |
214 | /* Avoid compiler warnings. */ |
215 | (void)device_index; | |
7d658874 | 216 | |
03168500 | 217 | if (sr_dev_list() != NULL) |
7d658874 BV |
218 | return SR_OK; |
219 | else | |
220 | return SR_ERR; | |
221 | } | |
222 | ||
9f45fb3a UH |
223 | /** |
224 | * Get the list of hardware capabilities. | |
225 | * | |
226 | * @return A pointer to the (hardware) capabilities of this virtual session | |
227 | * driver. This could be NULL, if no such capabilities exist. | |
228 | */ | |
7d658874 BV |
229 | static int *hw_get_capabilities(void) |
230 | { | |
7d658874 BV |
231 | return capabilities; |
232 | } | |
233 | ||
234 | static int hw_set_configuration(int device_index, int capability, void *value) | |
235 | { | |
236 | struct session_vdevice *vdevice; | |
237 | uint64_t *tmp_u64; | |
238 | ||
239 | if (!(vdevice = get_vdevice_by_index(device_index))) | |
240 | return SR_ERR; | |
241 | ||
242 | switch (capability) { | |
243 | case SR_HWCAP_SAMPLERATE: | |
244 | tmp_u64 = value; | |
245 | vdevice->samplerate = *tmp_u64; | |
9f45fb3a UH |
246 | sr_info("session driver: setting samplerate to %" PRIu64, |
247 | vdevice->samplerate); | |
7d658874 BV |
248 | break; |
249 | case SR_HWCAP_CAPTUREFILE: | |
250 | vdevice->capturefile = g_strdup(value); | |
9f45fb3a UH |
251 | sr_info("session driver: setting capturefile to %s", |
252 | vdevice->capturefile); | |
7d658874 BV |
253 | break; |
254 | case SR_HWCAP_CAPTURE_UNITSIZE: | |
255 | tmp_u64 = value; | |
256 | vdevice->unitsize = *tmp_u64; | |
257 | break; | |
258 | case SR_HWCAP_CAPTURE_NUM_PROBES: | |
259 | tmp_u64 = value; | |
260 | vdevice->num_probes = *tmp_u64; | |
261 | break; | |
262 | default: | |
9f45fb3a UH |
263 | sr_err("session driver: %s: unknown capability %d requested", |
264 | __func__, capability); | |
7d658874 BV |
265 | return SR_ERR; |
266 | } | |
267 | ||
268 | return SR_OK; | |
269 | } | |
270 | ||
271 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
272 | { | |
273 | struct zip_stat zs; | |
274 | struct session_vdevice *vdevice; | |
275 | struct sr_datafeed_header *header; | |
276 | struct sr_datafeed_packet *packet; | |
277 | int err; | |
278 | ||
cb93f8a9 UH |
279 | /* Avoid compiler warnings. */ |
280 | (void)session_device_id; | |
7d658874 BV |
281 | |
282 | if (!(vdevice = get_vdevice_by_index(device_index))) | |
283 | return SR_ERR; | |
284 | ||
b08024a8 UH |
285 | sr_info("session_driver: opening archive %s file %s", sessionfile, |
286 | vdevice->capturefile); | |
7d658874 BV |
287 | |
288 | if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) { | |
133a37bf UH |
289 | sr_err("Failed to open session file '%s': zip error %d\n", |
290 | sessionfile, err); | |
7d658874 BV |
291 | return SR_ERR; |
292 | } | |
293 | ||
294 | if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) { | |
133a37bf UH |
295 | sr_err("Failed to check capture file '%s' in session file " |
296 | "'%s'.", vdevice->capturefile, sessionfile); | |
7d658874 BV |
297 | return SR_ERR; |
298 | } | |
299 | ||
133a37bf UH |
300 | if (!(vdevice->capfile = zip_fopen(vdevice->archive, |
301 | vdevice->capturefile, 0))) { | |
302 | sr_err("Failed to open capture file '%s' in session file '%s'.", | |
303 | vdevice->capturefile, sessionfile); | |
7d658874 BV |
304 | return SR_ERR; |
305 | } | |
306 | ||
307 | /* freewheeling source */ | |
8a2efef2 | 308 | sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id); |
7d658874 | 309 | |
b53738ba UH |
310 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { |
311 | sr_err("session: %s: packet malloc failed", __func__); | |
312 | return SR_ERR_MALLOC; | |
313 | } | |
314 | ||
315 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
316 | sr_err("session: %s: header malloc failed", __func__); | |
317 | return SR_ERR_MALLOC; | |
318 | } | |
319 | ||
7d658874 | 320 | /* Send header packet to the session bus. */ |
7d658874 | 321 | packet->type = SR_DF_HEADER; |
7d658874 BV |
322 | packet->payload = (unsigned char *)header; |
323 | header->feed_version = 1; | |
324 | gettimeofday(&header->starttime, NULL); | |
276585ff | 325 | header->samplerate = vdevice->samplerate; |
7d658874 | 326 | header->num_logic_probes = vdevice->num_probes; |
8a2efef2 | 327 | sr_session_bus(session_device_id, packet); |
7d658874 BV |
328 | g_free(header); |
329 | g_free(packet); | |
330 | ||
331 | return SR_OK; | |
332 | } | |
333 | ||
a0ecd83b | 334 | /* Not static, it's used elsewhere (via 'extern'). */ |
7d658874 BV |
335 | struct sr_device_plugin session_driver = { |
336 | "session", | |
337 | "Session-emulating driver", | |
338 | 1, | |
339 | hw_init, | |
340 | hw_cleanup, | |
341 | hw_opendev, | |
b8c2f85f | 342 | NULL, |
7d658874 BV |
343 | hw_get_device_info, |
344 | hw_get_status, | |
345 | hw_get_capabilities, | |
346 | hw_set_configuration, | |
347 | hw_start_acquisition, | |
8a2efef2 | 348 | NULL, |
7d658874 | 349 | }; |