2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
27 #include <sigrok-internal.h>
30 extern GIOChannel channels[2];
36 sr_receive_data_callback cb;
40 /* There can only be one session at a time. */
41 struct sr_session *session;
44 struct source *sources = NULL;
45 int source_timeout = -1;
48 struct sr_session *sr_session_new(void)
50 session = calloc(1, sizeof(struct sr_session));
55 void sr_session_destroy(void)
58 g_slist_free(session->devices);
60 /* TODO: Loop over protocol decoders and free them. */
65 void sr_session_device_clear(void)
67 g_slist_free(session->devices);
68 session->devices = NULL;
71 int sr_session_device_add(struct sr_device *device)
75 if (device->plugin && device->plugin->opendev) {
76 ret = device->plugin->opendev(device->plugin_index);
81 session->devices = g_slist_append(session->devices, device);
87 void sr_session_pa_clear(void)
90 * The protocols are pointers to the global set of PA plugins,
93 g_slist_free(session->analyzers);
94 session->analyzers = NULL;
97 void sr_session_pa_add(struct sr_analyzer *an)
99 session->analyzers = g_slist_append(session->analyzers, an);
103 void sr_session_datafeed_callback_clear(void)
105 g_slist_free(session->datafeed_callbacks);
106 session->datafeed_callbacks = NULL;
109 void sr_session_datafeed_callback_add(sr_datafeed_callback callback)
111 session->datafeed_callbacks =
112 g_slist_append(session->datafeed_callbacks, callback);
115 static void sr_session_run_poll()
117 GPollFD *fds, my_gpollfd;
121 while (session->running) {
125 /* Construct g_poll()'s array. */
126 fds = malloc(sizeof(GPollFD) * num_sources);
127 for (i = 0; i < num_sources; i++) {
129 g_io_channel_win32_make_pollfd(&channels[0],
130 sources[i].events, &my_gpollfd);
132 my_gpollfd.fd = sources[i].fd;
133 my_gpollfd.events = sources[i].events;
138 ret = g_poll(fds, num_sources, source_timeout);
140 for (i = 0; i < num_sources; i++) {
141 if (fds[i].revents > 0 || (ret == 0
142 && source_timeout == sources[i].timeout)) {
144 * Invoke the source's callback on an event,
145 * or if the poll timeout out and this source
146 * asked for that timeout.
148 if (!sources[i].cb(fds[i].fd, fds[i].revents,
149 sources[i].user_data))
150 sr_session_source_remove(sources[i].fd);
158 int sr_session_start(void)
160 struct sr_device *device;
164 sr_info("session: starting");
165 for (l = session->devices; l; l = l->next) {
167 if ((ret = device->plugin->start_acquisition(
168 device->plugin_index, device)) != SR_OK)
175 void sr_session_run(void)
178 sr_info("session: running");
179 session->running = TRUE;
181 /* do we have real sources? */
182 if (num_sources == 1 && sources[0].fd == -1)
183 /* dummy source, freewheel over it */
184 while (session->running)
185 sources[0].cb(-1, 0, sources[0].user_data);
187 /* real sources, use g_poll() main loop */
188 sr_session_run_poll();
192 void sr_session_halt(void)
195 sr_info("session: halting");
196 session->running = FALSE;
200 void sr_session_stop(void)
202 struct sr_device *device;
205 sr_info("session: stopping");
206 session->running = FALSE;
207 for (l = session->devices; l; l = l->next) {
209 if (device->plugin && device->plugin->stop_acquisition)
210 device->plugin->stop_acquisition(device->plugin_index, device);
215 static void datafeed_dump(struct sr_datafeed_packet *packet)
217 struct sr_datafeed_logic *logic;
219 switch (packet->type) {
221 sr_dbg("bus: received SR_DF_HEADER");
224 sr_dbg("bus: received SR_DF_TRIGGER at %lu ms",
225 packet->timeoffset / 1000000);
228 logic = packet->payload;
229 sr_dbg("bus: received SR_DF_LOGIC at %f ms duration %f ms, %"PRIu64" bytes",
230 packet->timeoffset / 1000000.0, packet->duration / 1000000.0,
234 sr_dbg("bus: received SR_DF_END");
237 sr_dbg("bus: received unknown packet type %d", packet->type);
242 void sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet)
245 sr_datafeed_callback cb;
248 * TODO: Send packet through PD pipe, and send the output of that to
249 * the callbacks as well.
251 for (l = session->datafeed_callbacks; l; l = l->next) {
253 datafeed_dump(packet);
258 void sr_session_source_add(int fd, int events, int timeout,
259 sr_receive_data_callback callback, void *user_data)
261 struct source *new_sources, *s;
263 new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
266 memcpy(new_sources, sources,
267 sizeof(struct source) * num_sources);
271 s = &new_sources[num_sources++];
274 s->timeout = timeout;
276 s->user_data = user_data;
277 sources = new_sources;
279 if (timeout != source_timeout && timeout > 0
280 && (source_timeout == -1 || timeout < source_timeout))
281 source_timeout = timeout;
284 void sr_session_source_remove(int fd)
286 struct source *new_sources;
292 new_sources = calloc(1, sizeof(struct source) * num_sources);
293 for (old = 0, new = 0; old < num_sources; old++)
294 if (sources[old].fd != fd)
295 memcpy(&new_sources[new++], &sources[old],
296 sizeof(struct source));
300 sources = new_sources;
303 /* Target fd was not found. */