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/>.
29 extern GIOChannel channels[2];
35 sr_receive_data_callback cb;
39 /* There can only be one session at a time. */
40 struct sr_session *session;
43 struct source *sources = NULL;
44 int source_timeout = -1;
47 struct sr_session *sr_session_new(void)
49 session = calloc(1, sizeof(struct sr_session));
54 void sr_session_destroy(void)
56 g_slist_free(session->devices);
58 /* TODO: Loop over protocol decoders and free them. */
63 void sr_session_device_clear(void)
65 g_slist_free(session->devices);
66 session->devices = NULL;
69 int sr_session_device_add(struct sr_device *device)
73 if (device->plugin && device->plugin->open) {
74 ret = device->plugin->open(device->plugin_index);
79 session->devices = g_slist_append(session->devices, device);
85 void sr_session_pa_clear(void)
88 * The protocols are pointers to the global set of PA plugins,
91 g_slist_free(session->analyzers);
92 session->analyzers = NULL;
95 void sr_session_pa_add(struct sr_analyzer *an)
97 session->analyzers = g_slist_append(session->analyzers, an);
101 void sr_session_datafeed_callback_clear(void)
103 g_slist_free(session->datafeed_callbacks);
104 session->datafeed_callbacks = NULL;
107 void sr_session_datafeed_callback_add(sr_datafeed_callback callback)
109 session->datafeed_callbacks =
110 g_slist_append(session->datafeed_callbacks, callback);
113 static void sr_session_run_poll()
115 GPollFD *fds, my_gpollfd;
119 while (session->running) {
123 /* Construct g_poll()'s array. */
124 fds = malloc(sizeof(GPollFD) * num_sources);
125 for (i = 0; i < num_sources; i++) {
127 g_io_channel_win32_make_pollfd(&channels[0],
128 sources[i].events, &my_gpollfd);
130 my_gpollfd.fd = sources[i].fd;
131 my_gpollfd.events = sources[i].events;
136 ret = g_poll(fds, num_sources, source_timeout);
138 for (i = 0; i < num_sources; i++) {
139 if (fds[i].revents > 0 || (ret == 0
140 && source_timeout == sources[i].timeout)) {
142 * Invoke the source's callback on an event,
143 * or if the poll timeout out and this source
144 * asked for that timeout.
146 sources[i].cb(fds[i].fd, fds[i].revents,
147 sources[i].user_data);
155 int sr_session_start(void)
157 struct sr_device *device;
161 g_message("session: starting");
162 for (l = session->devices; l; l = l->next) {
164 if ((ret = device->plugin->start_acquisition(
165 device->plugin_index, device)) != SR_OK)
172 void sr_session_run(void)
175 g_message("session: running");
176 session->running = TRUE;
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);
184 /* real sources, use g_poll() main loop */
185 sr_session_run_poll();
189 void sr_session_halt(void)
192 g_message("session: halting");
193 session->running = FALSE;
197 void sr_session_stop(void)
199 struct sr_device *device;
202 g_message("session: stopping");
203 session->running = FALSE;
204 for (l = session->devices; l; l = l->next) {
206 if (device->plugin && device->plugin->stop_acquisition)
207 device->plugin->stop_acquisition(device->plugin_index, device);
212 void sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet)
215 sr_datafeed_callback cb;
218 * TODO: Send packet through PD pipe, and send the output of that to
219 * the callbacks as well.
221 for (l = session->datafeed_callbacks; l; l = l->next) {
227 void sr_session_source_add(int fd, int events, int timeout,
228 sr_receive_data_callback callback, void *user_data)
230 struct source *new_sources, *s;
232 new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
235 memcpy(new_sources, sources,
236 sizeof(struct source) * num_sources);
240 s = &new_sources[num_sources++];
243 s->timeout = timeout;
245 s->user_data = user_data;
246 sources = new_sources;
248 if (timeout != source_timeout && timeout > 0
249 && (source_timeout == -1 || timeout < source_timeout))
250 source_timeout = timeout;
253 void sr_session_source_remove(int fd)
255 struct source *new_sources;
261 new_sources = calloc(1, sizeof(struct source) * num_sources);
262 for (old = 0; old < num_sources; old++)
263 if (sources[old].fd != fd)
264 memcpy(&new_sources[new++], &sources[old],
265 sizeof(struct source));
269 sources = new_sources;
272 /* Target fd was not found. */