]>
Commit | Line | Data |
---|---|---|
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> | |
26 | #include <sigrok.h> | |
27 | #include <sigrok-internal.h> | |
28 | ||
29 | /* size of payloads sent across the session bus */ | |
30 | #define CHUNKSIZE 4096 | |
31 | ||
32 | struct session_vdevice { | |
33 | char *capturefile; | |
34 | struct zip *archive; | |
35 | struct zip_file *capfile; | |
36 | int bytes_read; | |
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 | ||
50 | ||
51 | static struct session_vdevice *get_vdevice_by_index(int device_index) | |
52 | { | |
53 | struct sr_device_instance *sdi; | |
54 | struct session_vdevice *vdevice; | |
55 | ||
56 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) | |
57 | return NULL; | |
58 | ||
59 | vdevice = sdi->priv; | |
60 | ||
61 | return vdevice; | |
62 | } | |
63 | ||
64 | static int feed_chunk(int fd, int revents, void *session_data) | |
65 | { | |
66 | struct sr_device_instance *sdi; | |
67 | struct session_vdevice *vdevice; | |
68 | struct sr_datafeed_packet packet; | |
69 | struct sr_datafeed_logic logic; | |
70 | uint64_t sample_period_ps; | |
71 | GSList *l; | |
72 | void *buf; | |
73 | int ret, got_data; | |
74 | ||
75 | /* Avoid compiler warnings. */ | |
76 | (void)fd; | |
77 | (void)revents; | |
78 | ||
79 | sr_dbg("session_driver: feed chunk"); | |
80 | ||
81 | got_data = FALSE; | |
82 | for (l = device_instances; l; l = l->next) { | |
83 | sdi = l->data; | |
84 | vdevice = sdi->priv; | |
85 | if (!vdevice) | |
86 | /* already done with this instance */ | |
87 | continue; | |
88 | ||
89 | if (!(buf = g_try_malloc(CHUNKSIZE))) { | |
90 | sr_err("session: %s: buf malloc failed", __func__); | |
91 | // return SR_ERR_MALLOC; | |
92 | return FALSE; | |
93 | } | |
94 | ||
95 | ret = zip_fread(vdevice->capfile, buf, CHUNKSIZE); | |
96 | if (ret > 0) { | |
97 | got_data = TRUE; | |
98 | packet.type = SR_DF_LOGIC; | |
99 | sample_period_ps = 1000000000000 / vdevice->samplerate; | |
100 | packet.timeoffset = sample_period_ps * (vdevice->bytes_read / vdevice->unitsize); | |
101 | packet.duration = sample_period_ps * (ret / vdevice->unitsize); | |
102 | packet.payload = &logic; | |
103 | logic.length = ret; | |
104 | logic.unitsize = vdevice->unitsize; | |
105 | logic.data = buf; | |
106 | vdevice->bytes_read += ret; | |
107 | sr_session_bus(session_data, &packet); | |
108 | } else { | |
109 | /* done with this capture file */ | |
110 | zip_fclose(vdevice->capfile); | |
111 | g_free(vdevice->capturefile); | |
112 | g_free(vdevice); | |
113 | sdi->priv = NULL; | |
114 | } | |
115 | } | |
116 | ||
117 | if (!got_data) { | |
118 | packet.type = SR_DF_END; | |
119 | sr_session_bus(session_data, &packet); | |
120 | } | |
121 | ||
122 | return TRUE; | |
123 | } | |
124 | ||
125 | /* driver callbacks */ | |
126 | static void hw_cleanup(void); | |
127 | ||
128 | static int hw_init(const char *deviceinfo) | |
129 | { | |
130 | hw_cleanup(); | |
131 | ||
132 | sessionfile = g_strdup(deviceinfo); | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static void hw_cleanup(void) | |
138 | { | |
139 | GSList *l; | |
140 | ||
141 | for (l = device_instances; l; l = l->next) | |
142 | sr_device_instance_free(l->data); | |
143 | ||
144 | g_slist_free(device_instances); | |
145 | device_instances = NULL; | |
146 | ||
147 | sr_session_source_remove(-1); | |
148 | ||
149 | g_free(sessionfile); | |
150 | ||
151 | } | |
152 | ||
153 | static int hw_opendev(int device_index) | |
154 | { | |
155 | struct sr_device_instance *sdi; | |
156 | ||
157 | sdi = sr_device_instance_new(device_index, SR_ST_INITIALIZING, | |
158 | NULL, NULL, NULL); | |
159 | if (!sdi) | |
160 | return SR_ERR; | |
161 | ||
162 | if (!(sdi->priv = g_try_malloc0(sizeof(struct session_vdevice)))) { | |
163 | sr_err("session: %s: sdi->priv malloc failed", __func__); | |
164 | return SR_ERR_MALLOC; | |
165 | } | |
166 | ||
167 | device_instances = g_slist_append(device_instances, sdi); | |
168 | ||
169 | return SR_OK; | |
170 | } | |
171 | ||
172 | static void *hw_get_device_info(int device_index, int device_info_id) | |
173 | { | |
174 | struct session_vdevice *vdevice; | |
175 | void *info; | |
176 | ||
177 | if (device_info_id != SR_DI_CUR_SAMPLERATE) | |
178 | return NULL; | |
179 | ||
180 | if (!(vdevice = get_vdevice_by_index(device_index))) | |
181 | return NULL; | |
182 | ||
183 | info = &vdevice->samplerate; | |
184 | ||
185 | return info; | |
186 | } | |
187 | ||
188 | static int hw_get_status(int device_index) | |
189 | { | |
190 | /* Avoid compiler warnings. */ | |
191 | (void)device_index; | |
192 | ||
193 | if (devices) | |
194 | return SR_OK; | |
195 | else | |
196 | return SR_ERR; | |
197 | } | |
198 | ||
199 | static int *hw_get_capabilities(void) | |
200 | { | |
201 | ||
202 | return capabilities; | |
203 | } | |
204 | ||
205 | static int hw_set_configuration(int device_index, int capability, void *value) | |
206 | { | |
207 | struct session_vdevice *vdevice; | |
208 | uint64_t *tmp_u64; | |
209 | ||
210 | if (!(vdevice = get_vdevice_by_index(device_index))) | |
211 | return SR_ERR; | |
212 | ||
213 | switch (capability) { | |
214 | case SR_HWCAP_SAMPLERATE: | |
215 | tmp_u64 = value; | |
216 | vdevice->samplerate = *tmp_u64; | |
217 | break; | |
218 | case SR_HWCAP_CAPTUREFILE: | |
219 | vdevice->capturefile = g_strdup(value); | |
220 | break; | |
221 | case SR_HWCAP_CAPTURE_UNITSIZE: | |
222 | tmp_u64 = value; | |
223 | vdevice->unitsize = *tmp_u64; | |
224 | break; | |
225 | case SR_HWCAP_CAPTURE_NUM_PROBES: | |
226 | tmp_u64 = value; | |
227 | vdevice->num_probes = *tmp_u64; | |
228 | break; | |
229 | default: | |
230 | return SR_ERR; | |
231 | } | |
232 | ||
233 | return SR_OK; | |
234 | } | |
235 | ||
236 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
237 | { | |
238 | struct zip_stat zs; | |
239 | struct session_vdevice *vdevice; | |
240 | struct sr_datafeed_header *header; | |
241 | struct sr_datafeed_packet *packet; | |
242 | int err; | |
243 | ||
244 | /* Avoid compiler warnings. */ | |
245 | (void)session_device_id; | |
246 | ||
247 | if (!(vdevice = get_vdevice_by_index(device_index))) | |
248 | return SR_ERR; | |
249 | ||
250 | sr_info("session_driver: opening archive %s file %s", sessionfile, | |
251 | vdevice->capturefile); | |
252 | ||
253 | if (!(vdevice->archive = zip_open(sessionfile, 0, &err))) { | |
254 | sr_warn("Failed to open session file '%s': zip error %d\n", | |
255 | sessionfile, err); | |
256 | return SR_ERR; | |
257 | } | |
258 | ||
259 | if (zip_stat(vdevice->archive, vdevice->capturefile, 0, &zs) == -1) { | |
260 | sr_warn("Failed to check capture file '%s' in session file '%s'.", | |
261 | vdevice->capturefile, sessionfile); | |
262 | return SR_ERR; | |
263 | } | |
264 | ||
265 | if (!(vdevice->capfile = zip_fopen(vdevice->archive, vdevice->capturefile, 0))) { | |
266 | sr_warn("Failed to open capture file '%s' in session file '%s'.", | |
267 | vdevice->capturefile, sessionfile); | |
268 | return SR_ERR; | |
269 | } | |
270 | ||
271 | /* freewheeling source */ | |
272 | sr_session_source_add(-1, 0, 0, feed_chunk, session_device_id); | |
273 | ||
274 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { | |
275 | sr_err("session: %s: packet malloc failed", __func__); | |
276 | return SR_ERR_MALLOC; | |
277 | } | |
278 | ||
279 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
280 | sr_err("session: %s: header malloc failed", __func__); | |
281 | return SR_ERR_MALLOC; | |
282 | } | |
283 | ||
284 | /* Send header packet to the session bus. */ | |
285 | packet->type = SR_DF_HEADER; | |
286 | packet->payload = (unsigned char *)header; | |
287 | header->feed_version = 1; | |
288 | gettimeofday(&header->starttime, NULL); | |
289 | header->samplerate = vdevice->samplerate; | |
290 | header->num_logic_probes = vdevice->num_probes; | |
291 | header->num_analog_probes = 0; | |
292 | sr_session_bus(session_device_id, packet); | |
293 | g_free(header); | |
294 | g_free(packet); | |
295 | ||
296 | return SR_OK; | |
297 | } | |
298 | ||
299 | struct sr_device_plugin session_driver = { | |
300 | "session", | |
301 | "Session-emulating driver", | |
302 | 1, | |
303 | hw_init, | |
304 | hw_cleanup, | |
305 | hw_opendev, | |
306 | NULL, | |
307 | hw_get_device_info, | |
308 | hw_get_status, | |
309 | hw_get_capabilities, | |
310 | hw_set_configuration, | |
311 | hw_start_acquisition, | |
312 | NULL, | |
313 | }; |