]>
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 | ||
22b02383 | 20 | #include "config.h" |
a1bb33af UH |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
24 | #include <string.h> | |
544a4582 | 25 | #include <glib.h> |
b7f09cf8 UH |
26 | #include "sigrok.h" |
27 | #include "sigrok-internal.h" | |
aa4b1107 | 28 | |
9f4bc44e UH |
29 | /* demo.c */ |
30 | extern GIOChannel channels[2]; | |
a1bb33af | 31 | |
544a4582 BV |
32 | struct source { |
33 | int fd; | |
34 | int events; | |
35 | int timeout; | |
a887e3da | 36 | sr_receive_data_callback cb; |
544a4582 BV |
37 | void *user_data; |
38 | }; | |
39 | ||
7d658874 | 40 | /* There can only be one session at a time. */ |
a0ecd83b | 41 | /* 'session' is not static, it's used elsewhere (via 'extern'). */ |
2872d21e | 42 | struct sr_session *session; |
a0ecd83b | 43 | static int num_sources = 0; |
544a4582 | 44 | |
a0ecd83b UH |
45 | static struct source *sources = NULL; |
46 | static int source_timeout = -1; | |
544a4582 | 47 | |
8a2efef2 | 48 | struct sr_session *sr_session_new(void) |
a1bb33af | 49 | { |
2872d21e | 50 | session = calloc(1, sizeof(struct sr_session)); |
a1bb33af UH |
51 | |
52 | return session; | |
53 | } | |
54 | ||
8a2efef2 | 55 | void sr_session_destroy(void) |
a1bb33af | 56 | { |
a1bb33af UH |
57 | g_slist_free(session->devices); |
58 | ||
aa4b1107 | 59 | /* TODO: Loop over protocol decoders and free them. */ |
a1bb33af UH |
60 | |
61 | g_free(session); | |
a1bb33af UH |
62 | } |
63 | ||
8a2efef2 | 64 | void sr_session_device_clear(void) |
a1bb33af | 65 | { |
a1bb33af UH |
66 | g_slist_free(session->devices); |
67 | session->devices = NULL; | |
a1bb33af UH |
68 | } |
69 | ||
8a2efef2 | 70 | int sr_session_device_add(struct sr_device *device) |
a1bb33af UH |
71 | { |
72 | int ret; | |
73 | ||
86f5e3d8 UH |
74 | if (device->plugin && device->plugin->opendev) { |
75 | ret = device->plugin->opendev(device->plugin_index); | |
e46b8fb1 | 76 | if (ret != SR_OK) |
aa4b1107 BV |
77 | return ret; |
78 | } | |
a1bb33af | 79 | |
aa4b1107 BV |
80 | session->devices = g_slist_append(session->devices, device); |
81 | ||
e46b8fb1 | 82 | return SR_OK; |
a1bb33af UH |
83 | } |
84 | ||
8bb416be | 85 | #if 0 |
8a2efef2 | 86 | void sr_session_pa_clear(void) |
a1bb33af | 87 | { |
62c82025 UH |
88 | /* |
89 | * The protocols are pointers to the global set of PA plugins, | |
90 | * so don't free them. | |
91 | */ | |
a1bb33af UH |
92 | g_slist_free(session->analyzers); |
93 | session->analyzers = NULL; | |
a1bb33af UH |
94 | } |
95 | ||
809c5f20 | 96 | void sr_session_pa_add(struct sr_analyzer *an) |
a1bb33af | 97 | { |
a1bb33af | 98 | session->analyzers = g_slist_append(session->analyzers, an); |
a1bb33af | 99 | } |
8bb416be | 100 | #endif |
a1bb33af | 101 | |
8a2efef2 | 102 | void sr_session_datafeed_callback_clear(void) |
a1bb33af | 103 | { |
a1bb33af UH |
104 | g_slist_free(session->datafeed_callbacks); |
105 | session->datafeed_callbacks = NULL; | |
a1bb33af UH |
106 | } |
107 | ||
13b05733 | 108 | void sr_session_datafeed_callback_add(sr_datafeed_callback callback) |
a1bb33af | 109 | { |
62c82025 UH |
110 | session->datafeed_callbacks = |
111 | g_slist_append(session->datafeed_callbacks, callback); | |
a1bb33af UH |
112 | } |
113 | ||
8a2efef2 | 114 | static void sr_session_run_poll() |
544a4582 BV |
115 | { |
116 | GPollFD *fds, my_gpollfd; | |
117 | int ret, i; | |
118 | ||
544a4582 BV |
119 | fds = NULL; |
120 | while (session->running) { | |
121 | if (fds) | |
122 | free(fds); | |
123 | ||
124 | /* Construct g_poll()'s array. */ | |
125 | fds = malloc(sizeof(GPollFD) * num_sources); | |
126 | for (i = 0; i < num_sources; i++) { | |
127 | #ifdef _WIN32 | |
128 | g_io_channel_win32_make_pollfd(&channels[0], | |
129 | sources[i].events, &my_gpollfd); | |
130 | #else | |
131 | my_gpollfd.fd = sources[i].fd; | |
132 | my_gpollfd.events = sources[i].events; | |
133 | fds[i] = my_gpollfd; | |
134 | #endif | |
135 | } | |
136 | ||
137 | ret = g_poll(fds, num_sources, source_timeout); | |
138 | ||
139 | for (i = 0; i < num_sources; i++) { | |
140 | if (fds[i].revents > 0 || (ret == 0 | |
141 | && source_timeout == sources[i].timeout)) { | |
142 | /* | |
143 | * Invoke the source's callback on an event, | |
144 | * or if the poll timeout out and this source | |
145 | * asked for that timeout. | |
146 | */ | |
5c582d9f GM |
147 | if (!sources[i].cb(fds[i].fd, fds[i].revents, |
148 | sources[i].user_data)) | |
149 | sr_session_source_remove(sources[i].fd); | |
544a4582 BV |
150 | } |
151 | } | |
152 | } | |
153 | free(fds); | |
544a4582 BV |
154 | } |
155 | ||
8a2efef2 | 156 | int sr_session_start(void) |
7d658874 BV |
157 | { |
158 | struct sr_device *device; | |
159 | GSList *l; | |
160 | int ret; | |
161 | ||
b08024a8 | 162 | sr_info("session: starting"); |
7d658874 BV |
163 | for (l = session->devices; l; l = l->next) { |
164 | device = l->data; | |
165 | if ((ret = device->plugin->start_acquisition( | |
166 | device->plugin_index, device)) != SR_OK) | |
167 | break; | |
168 | } | |
169 | ||
170 | return ret; | |
171 | } | |
172 | ||
8a2efef2 | 173 | void sr_session_run(void) |
7d658874 | 174 | { |
b08024a8 | 175 | sr_info("session: running"); |
7d658874 BV |
176 | session->running = TRUE; |
177 | ||
178 | /* do we have real sources? */ | |
179 | if (num_sources == 1 && sources[0].fd == -1) | |
180 | /* dummy source, freewheel over it */ | |
181 | while (session->running) | |
182 | sources[0].cb(-1, 0, sources[0].user_data); | |
183 | else | |
184 | /* real sources, use g_poll() main loop */ | |
8a2efef2 | 185 | sr_session_run_poll(); |
7d658874 BV |
186 | } |
187 | ||
8a2efef2 | 188 | void sr_session_halt(void) |
544a4582 | 189 | { |
b08024a8 | 190 | sr_info("session: halting"); |
544a4582 | 191 | session->running = FALSE; |
544a4582 BV |
192 | } |
193 | ||
8a2efef2 | 194 | void sr_session_stop(void) |
a1bb33af | 195 | { |
5c2d46d1 | 196 | struct sr_device *device; |
a1bb33af UH |
197 | GSList *l; |
198 | ||
b08024a8 | 199 | sr_info("session: stopping"); |
544a4582 | 200 | session->running = FALSE; |
62c82025 | 201 | for (l = session->devices; l; l = l->next) { |
a1bb33af | 202 | device = l->data; |
b8c2f85f | 203 | if (device->plugin && device->plugin->stop_acquisition) |
aa4b1107 | 204 | device->plugin->stop_acquisition(device->plugin_index, device); |
a1bb33af | 205 | } |
a1bb33af UH |
206 | } |
207 | ||
7d2afd6c BV |
208 | static void datafeed_dump(struct sr_datafeed_packet *packet) |
209 | { | |
210 | struct sr_datafeed_logic *logic; | |
211 | ||
212 | switch (packet->type) { | |
213 | case SR_DF_HEADER: | |
214 | sr_dbg("bus: received SR_DF_HEADER"); | |
215 | break; | |
216 | case SR_DF_TRIGGER: | |
217 | sr_dbg("bus: received SR_DF_TRIGGER at %lu ms", | |
218 | packet->timeoffset / 1000000); | |
219 | break; | |
220 | case SR_DF_LOGIC: | |
221 | logic = packet->payload; | |
222 | sr_dbg("bus: received SR_DF_LOGIC at %f ms duration %f ms, %"PRIu64" bytes", | |
223 | packet->timeoffset / 1000000.0, packet->duration / 1000000.0, | |
224 | logic->length); | |
225 | break; | |
226 | case SR_DF_END: | |
227 | sr_dbg("bus: received SR_DF_END"); | |
228 | break; | |
229 | default: | |
230 | sr_dbg("bus: received unknown packet type %d", packet->type); | |
231 | } | |
7d2afd6c BV |
232 | } |
233 | ||
8a2efef2 | 234 | void sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet) |
a1bb33af UH |
235 | { |
236 | GSList *l; | |
13b05733 | 237 | sr_datafeed_callback cb; |
a1bb33af | 238 | |
62c82025 | 239 | /* |
aa4b1107 | 240 | * TODO: Send packet through PD pipe, and send the output of that to |
62c82025 | 241 | * the callbacks as well. |
a1bb33af | 242 | */ |
62c82025 | 243 | for (l = session->datafeed_callbacks; l; l = l->next) { |
a1bb33af | 244 | cb = l->data; |
7d2afd6c | 245 | datafeed_dump(packet); |
a1bb33af UH |
246 | cb(device, packet); |
247 | } | |
a1bb33af UH |
248 | } |
249 | ||
8a2efef2 | 250 | void sr_session_source_add(int fd, int events, int timeout, |
a887e3da | 251 | sr_receive_data_callback callback, void *user_data) |
544a4582 BV |
252 | { |
253 | struct source *new_sources, *s; | |
254 | ||
255 | new_sources = calloc(1, sizeof(struct source) * (num_sources + 1)); | |
256 | ||
257 | if (sources) { | |
258 | memcpy(new_sources, sources, | |
259 | sizeof(struct source) * num_sources); | |
260 | free(sources); | |
261 | } | |
262 | ||
263 | s = &new_sources[num_sources++]; | |
264 | s->fd = fd; | |
265 | s->events = events; | |
266 | s->timeout = timeout; | |
267 | s->cb = callback; | |
268 | s->user_data = user_data; | |
269 | sources = new_sources; | |
270 | ||
271 | if (timeout != source_timeout && timeout > 0 | |
272 | && (source_timeout == -1 || timeout < source_timeout)) | |
273 | source_timeout = timeout; | |
274 | } | |
275 | ||
8a2efef2 | 276 | void sr_session_source_remove(int fd) |
544a4582 BV |
277 | { |
278 | struct source *new_sources; | |
279 | int old, new; | |
280 | ||
281 | if (!sources) | |
282 | return; | |
283 | ||
284 | new_sources = calloc(1, sizeof(struct source) * num_sources); | |
5c582d9f | 285 | for (old = 0, new = 0; old < num_sources; old++) |
544a4582 BV |
286 | if (sources[old].fd != fd) |
287 | memcpy(&new_sources[new++], &sources[old], | |
288 | sizeof(struct source)); | |
289 | ||
290 | if (old != new) { | |
291 | free(sources); | |
292 | sources = new_sources; | |
293 | num_sources--; | |
294 | } else { | |
295 | /* Target fd was not found. */ | |
296 | free(new_sources); | |
297 | } | |
298 | } |