]>
Commit | Line | Data |
---|---|---|
7d658874 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 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 <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; |
cb1e389c | 90 | uint64_t sample_period_ps; |
7d658874 BV |
91 | GSList *l; |
92 | void *buf; | |
93 | int ret, got_data; | |
94 | ||
cb93f8a9 UH |
95 | /* Avoid compiler warnings. */ |
96 | (void)fd; | |
97 | (void)revents; | |
7d658874 | 98 | |
b08024a8 | 99 | sr_dbg("session_driver: feed chunk"); |
7d658874 BV |
100 | |
101 | got_data = FALSE; | |
102 | for (l = device_instances; l; l = l->next) { | |
103 | sdi = l->data; | |
104 | vdevice = sdi->priv; | |
105 | if (!vdevice) | |
106 | /* already done with this instance */ | |
107 | continue; | |
108 | ||
b53738ba UH |
109 | if (!(buf = g_try_malloc(CHUNKSIZE))) { |
110 | sr_err("session: %s: buf malloc failed", __func__); | |
111 | // return SR_ERR_MALLOC; | |
112 | return FALSE; | |
113 | } | |
114 | ||
7d658874 BV |
115 | ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE); |
116 | if (ret > 0) { | |
117 | got_data = TRUE; | |
118 | packet.type = SR_DF_LOGIC; | |
cb1e389c BV |
119 | sample_period_ps = 1000000000000 / vdevice->samplerate; |
120 | packet.timeoffset = sample_period_ps * (vdevice->bytes_read / vdevice->unitsize); | |
121 | packet.duration = sample_period_ps * (ret / vdevice->unitsize); | |
9c939c51 BV |
122 | packet.payload = &logic; |
123 | logic.length = ret; | |
124 | logic.unitsize = vdevice->unitsize; | |
125 | logic.data = buf; | |
cb1e389c | 126 | vdevice->bytes_read += ret; |
9c939c51 | 127 | sr_session_bus(session_data, &packet); |
7d658874 BV |
128 | } else { |
129 | /* done with this capture file */ | |
130 | zip_fclose(vdevice->capfile); | |
131 | g_free(vdevice->capturefile); | |
132 | g_free(vdevice); | |
133 | sdi->priv = NULL; | |
134 | } | |
135 | } | |
136 | ||
137 | if (!got_data) { | |
138 | packet.type = SR_DF_END; | |
9c939c51 | 139 | sr_session_bus(session_data, &packet); |
7d658874 BV |
140 | } |
141 | ||
142 | return TRUE; | |
143 | } | |
144 | ||
145 | /* driver callbacks */ | |
805e9640 | 146 | static void hw_cleanup(void); |
7d658874 | 147 | |
9f45fb3a UH |
148 | /** |
149 | * TODO. | |
150 | * | |
151 | * @param deviceinfo TODO. | |
152 | * | |
153 | * @return TODO. | |
154 | */ | |
54ac5277 | 155 | static int hw_init(const char *deviceinfo) |
7d658874 BV |
156 | { |
157 | ||
158 | sessionfile = g_strdup(deviceinfo); | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
9f45fb3a UH |
163 | /** |
164 | * TODO. | |
165 | * | |
166 | */ | |
7d658874 BV |
167 | static void hw_cleanup(void) |
168 | { | |
169 | GSList *l; | |
170 | ||
171 | for (l = device_instances; l; l = l->next) | |
172 | sr_device_instance_free(l->data); | |
173 | ||
805e9640 GM |
174 | g_slist_free(device_instances); |
175 | device_instances = NULL; | |
176 | ||
177 | sr_session_source_remove(-1); | |
178 | ||
7d658874 | 179 | g_free(sessionfile); |
7d658874 BV |
180 | } |
181 | ||
182 | static int hw_opendev(int device_index) | |
183 | { | |
184 | struct sr_device_instance *sdi; | |
185 | ||
186 | sdi = sr_device_instance_new(device_index, SR_ST_INITIALIZING, | |
187 | NULL, NULL, NULL); | |
188 | if (!sdi) | |
189 | return SR_ERR; | |
b53738ba UH |
190 | |
191 | if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) { | |
192 | sr_err("session: %s: sdi->priv malloc failed", __func__); | |
193 | return SR_ERR_MALLOC; | |
194 | } | |
195 | ||
7d658874 BV |
196 | device_instances = g_slist_append(device_instances, sdi); |
197 | ||
198 | return SR_OK; | |
199 | } | |
200 | ||
7d658874 BV |
201 | static void *hw_get_device_info(int device_index, int device_info_id) |
202 | { | |
203 | struct session_vdevice *vdevice; | |
204 | void *info; | |
205 | ||
b8c2f85f BV |
206 | if (device_info_id != SR_DI_CUR_SAMPLERATE) |
207 | return NULL; | |
208 | ||
7d658874 BV |
209 | if (!(vdevice = get_vdevice_by_index(device_index))) |
210 | return NULL; | |
211 | ||
212 | info = &vdevice->samplerate; | |
213 | ||
214 | return info; | |
215 | } | |
216 | ||
217 | static int hw_get_status(int device_index) | |
218 | { | |
cb93f8a9 UH |
219 | /* Avoid compiler warnings. */ |
220 | (void)device_index; | |
7d658874 | 221 | |
a0ecd83b | 222 | if (sr_device_list() != NULL) |
7d658874 BV |
223 | return SR_OK; |
224 | else | |
225 | return SR_ERR; | |
226 | } | |
227 | ||
9f45fb3a UH |
228 | /** |
229 | * Get the list of hardware capabilities. | |
230 | * | |
231 | * @return A pointer to the (hardware) capabilities of this virtual session | |
232 | * driver. This could be NULL, if no such capabilities exist. | |
233 | */ | |
7d658874 BV |
234 | static int *hw_get_capabilities(void) |
235 | { | |
7d658874 BV |
236 | return capabilities; |
237 | } | |
238 | ||
239 | static int hw_set_configuration(int device_index, int capability, void *value) | |
240 | { | |
241 | struct session_vdevice *vdevice; | |
242 | uint64_t *tmp_u64; | |
243 | ||
244 | if (!(vdevice = get_vdevice_by_index(device_index))) | |
245 | return SR_ERR; | |
246 | ||
247 | switch (capability) { | |
248 | case SR_HWCAP_SAMPLERATE: | |
249 | tmp_u64 = value; | |
250 | vdevice->samplerate = *tmp_u64; | |
9f45fb3a UH |
251 | sr_info("session driver: setting samplerate to %" PRIu64, |
252 | vdevice->samplerate); | |
7d658874 BV |
253 | break; |
254 | case SR_HWCAP_CAPTUREFILE: | |
255 | vdevice->capturefile = g_strdup(value); | |
9f45fb3a UH |
256 | sr_info("session driver: setting capturefile to %s", |
257 | vdevice->capturefile); | |
7d658874 BV |
258 | break; |
259 | case SR_HWCAP_CAPTURE_UNITSIZE: | |
260 | tmp_u64 = value; | |
261 | vdevice->unitsize = *tmp_u64; | |
262 | break; | |
263 | case SR_HWCAP_CAPTURE_NUM_PROBES: | |
264 | tmp_u64 = value; | |
265 | vdevice->num_probes = *tmp_u64; | |
266 | break; | |
267 | default: | |
9f45fb3a UH |
268 | sr_err("session driver: %s: unknown capability %d requested", |
269 | __func__, capability); | |
7d658874 BV |
270 | return SR_ERR; |
271 | } | |
272 | ||
273 | return SR_OK; | |
274 | } | |
275 | ||
276 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
277 | { | |
278 | struct zip_stat zs; | |
279 | struct session_vdevice *vdevice; | |
280 | struct sr_datafeed_header *header; | |
281 | struct sr_datafeed_packet *packet; | |
282 | int err; | |
283 | ||
cb93f8a9 UH |
284 | /* Avoid compiler warnings. */ |
285 | (void)session_device_id; | |
7d658874 BV |
286 | |
287 | if (!(vdevice = get_vdevice_by_index(device_index))) | |
288 | return SR_ERR; | |
289 | ||
b08024a8 UH |
290 | sr_info("session_driver: opening archive %s file %s", sessionfile, |
291 | vdevice->capturefile); | |
7d658874 BV |
292 | |
293 | if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) { | |
b08024a8 UH |
294 | sr_warn("Failed to open session file '%s': zip error %d\n", |
295 | sessionfile, err); | |
7d658874 BV |
296 | return SR_ERR; |
297 | } | |
298 | ||
299 | if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) { | |
b08024a8 UH |
300 | sr_warn("Failed to check capture file '%s' in session file '%s'.", |
301 | vdevice->capturefile, sessionfile); | |
7d658874 BV |
302 | return SR_ERR; |
303 | } | |
304 | ||
305 | if (!(vdevice->capfile = zip_fopen(vdevice->archive, vdevice->capturefile, 0))) { | |
b08024a8 UH |
306 | sr_warn("Failed to open capture file '%s' in session file '%s'.", |
307 | vdevice->capturefile, sessionfile); | |
7d658874 BV |
308 | return SR_ERR; |
309 | } | |
310 | ||
311 | /* freewheeling source */ | |
8a2efef2 | 312 | sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id); |
7d658874 | 313 | |
b53738ba UH |
314 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { |
315 | sr_err("session: %s: packet malloc failed", __func__); | |
316 | return SR_ERR_MALLOC; | |
317 | } | |
318 | ||
319 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
320 | sr_err("session: %s: header malloc failed", __func__); | |
321 | return SR_ERR_MALLOC; | |
322 | } | |
323 | ||
7d658874 | 324 | /* Send header packet to the session bus. */ |
7d658874 | 325 | packet->type = SR_DF_HEADER; |
7d658874 BV |
326 | packet->payload = (unsigned char *)header; |
327 | header->feed_version = 1; | |
328 | gettimeofday(&header->starttime, NULL); | |
276585ff | 329 | header->samplerate = vdevice->samplerate; |
7d658874 BV |
330 | header->num_logic_probes = vdevice->num_probes; |
331 | header->num_analog_probes = 0; | |
8a2efef2 | 332 | sr_session_bus(session_device_id, packet); |
7d658874 BV |
333 | g_free(header); |
334 | g_free(packet); | |
335 | ||
336 | return SR_OK; | |
337 | } | |
338 | ||
a0ecd83b | 339 | /* Not static, it's used elsewhere (via 'extern'). */ |
7d658874 BV |
340 | struct sr_device_plugin session_driver = { |
341 | "session", | |
342 | "Session-emulating driver", | |
343 | 1, | |
344 | hw_init, | |
345 | hw_cleanup, | |
346 | hw_opendev, | |
b8c2f85f | 347 | NULL, |
7d658874 BV |
348 | hw_get_device_info, |
349 | hw_get_status, | |
350 | hw_get_capabilities, | |
351 | hw_set_configuration, | |
352 | hw_start_acquisition, | |
8a2efef2 | 353 | NULL, |
7d658874 | 354 | }; |