Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 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 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <string.h> |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 24 | #include <glib.h> |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 25 | #include <sigrok.h> |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 26 | #include <config.h> |
| 27 | |
Uwe Hermann | 9f4bc44 | 2011-01-31 14:14:28 +0100 | [diff] [blame] | 28 | /* demo.c */ |
| 29 | extern GIOChannel channels[2]; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 30 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 31 | struct source { |
| 32 | int fd; |
| 33 | int events; |
| 34 | int timeout; |
| 35 | receive_data_callback cb; |
| 36 | void *user_data; |
| 37 | }; |
| 38 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame^] | 39 | /* There can only be one session at a time. */ |
| 40 | struct session *session; |
| 41 | int num_sources = 0; |
| 42 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 43 | struct source *sources = NULL; |
| 44 | int source_timeout = -1; |
| 45 | |
| 46 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 47 | struct session *session_new(void) |
| 48 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 49 | session = calloc(1, sizeof(struct session)); |
| 50 | |
| 51 | return session; |
| 52 | } |
| 53 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 54 | void session_destroy(void) |
| 55 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 56 | g_slist_free(session->devices); |
| 57 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 58 | /* TODO: Loop over protocol decoders and free them. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 59 | |
| 60 | g_free(session); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 63 | void session_device_clear(void) |
| 64 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 65 | g_slist_free(session->devices); |
| 66 | session->devices = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Uwe Hermann | 5c2d46d | 2011-01-30 16:19:42 +0100 | [diff] [blame] | 69 | int session_device_add(struct sr_device *device) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 70 | { |
| 71 | int ret; |
| 72 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 73 | if (device->plugin && device->plugin->open) { |
| 74 | ret = device->plugin->open(device->plugin_index); |
Uwe Hermann | e46b8fb | 2011-01-29 16:23:12 +0100 | [diff] [blame] | 75 | if (ret != SR_OK) |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 76 | return ret; |
| 77 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 78 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 79 | session->devices = g_slist_append(session->devices, device); |
| 80 | |
Uwe Hermann | e46b8fb | 2011-01-29 16:23:12 +0100 | [diff] [blame] | 81 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 84 | void session_pa_clear(void) |
| 85 | { |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 86 | /* |
| 87 | * The protocols are pointers to the global set of PA plugins, |
| 88 | * so don't free them. |
| 89 | */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 90 | g_slist_free(session->analyzers); |
| 91 | session->analyzers = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 94 | void session_pa_add(struct analyzer *an) |
| 95 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 96 | session->analyzers = g_slist_append(session->analyzers, an); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 99 | void session_datafeed_callback_clear(void) |
| 100 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 101 | g_slist_free(session->datafeed_callbacks); |
| 102 | session->datafeed_callbacks = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 105 | void session_datafeed_callback_add(datafeed_callback callback) |
| 106 | { |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 107 | session->datafeed_callbacks = |
| 108 | g_slist_append(session->datafeed_callbacks, callback); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame^] | 111 | static void session_run_poll() |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 112 | { |
| 113 | GPollFD *fds, my_gpollfd; |
| 114 | int ret, i; |
| 115 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 116 | fds = NULL; |
| 117 | while (session->running) { |
| 118 | if (fds) |
| 119 | free(fds); |
| 120 | |
| 121 | /* Construct g_poll()'s array. */ |
| 122 | fds = malloc(sizeof(GPollFD) * num_sources); |
| 123 | for (i = 0; i < num_sources; i++) { |
| 124 | #ifdef _WIN32 |
| 125 | g_io_channel_win32_make_pollfd(&channels[0], |
| 126 | sources[i].events, &my_gpollfd); |
| 127 | #else |
| 128 | my_gpollfd.fd = sources[i].fd; |
| 129 | my_gpollfd.events = sources[i].events; |
| 130 | fds[i] = my_gpollfd; |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | ret = g_poll(fds, num_sources, source_timeout); |
| 135 | |
| 136 | for (i = 0; i < num_sources; i++) { |
| 137 | if (fds[i].revents > 0 || (ret == 0 |
| 138 | && source_timeout == sources[i].timeout)) { |
| 139 | /* |
| 140 | * Invoke the source's callback on an event, |
| 141 | * or if the poll timeout out and this source |
| 142 | * asked for that timeout. |
| 143 | */ |
| 144 | sources[i].cb(fds[i].fd, fds[i].revents, |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame^] | 145 | sources[i].user_data); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | free(fds); |
| 150 | |
| 151 | } |
| 152 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame^] | 153 | int session_start(void) |
| 154 | { |
| 155 | struct sr_device *device; |
| 156 | GSList *l; |
| 157 | int ret; |
| 158 | |
| 159 | g_message("session: starting"); |
| 160 | for (l = session->devices; l; l = l->next) { |
| 161 | device = l->data; |
| 162 | if ((ret = device->plugin->start_acquisition( |
| 163 | device->plugin_index, device)) != SR_OK) |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | void session_run(void) |
| 171 | { |
| 172 | |
| 173 | g_message("session: running"); |
| 174 | session->running = TRUE; |
| 175 | |
| 176 | /* do we have real sources? */ |
| 177 | if (num_sources == 1 && sources[0].fd == -1) |
| 178 | /* dummy source, freewheel over it */ |
| 179 | while (session->running) |
| 180 | sources[0].cb(-1, 0, sources[0].user_data); |
| 181 | else |
| 182 | /* real sources, use g_poll() main loop */ |
| 183 | session_run_poll(); |
| 184 | |
| 185 | } |
| 186 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 187 | void session_halt(void) |
| 188 | { |
| 189 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame^] | 190 | g_message("session: halting"); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 191 | session->running = FALSE; |
| 192 | |
| 193 | } |
| 194 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 195 | void session_stop(void) |
| 196 | { |
Uwe Hermann | 5c2d46d | 2011-01-30 16:19:42 +0100 | [diff] [blame] | 197 | struct sr_device *device; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 198 | GSList *l; |
| 199 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame^] | 200 | g_message("session: stopping"); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 201 | session->running = FALSE; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 202 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 203 | device = l->data; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 204 | if (device->plugin) |
| 205 | device->plugin->stop_acquisition(device->plugin_index, device); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 206 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 207 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Uwe Hermann | 5c2d46d | 2011-01-30 16:19:42 +0100 | [diff] [blame] | 210 | void session_bus(struct sr_device *device, struct sr_datafeed_packet *packet) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 211 | { |
| 212 | GSList *l; |
| 213 | datafeed_callback cb; |
| 214 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 215 | /* |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 216 | * TODO: Send packet through PD pipe, and send the output of that to |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 217 | * the callbacks as well. |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 218 | */ |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 219 | for (l = session->datafeed_callbacks; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 220 | cb = l->data; |
| 221 | cb(device, packet); |
| 222 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 225 | void session_source_add(int fd, int events, int timeout, |
| 226 | receive_data_callback callback, void *user_data) |
| 227 | { |
| 228 | struct source *new_sources, *s; |
| 229 | |
| 230 | new_sources = calloc(1, sizeof(struct source) * (num_sources + 1)); |
| 231 | |
| 232 | if (sources) { |
| 233 | memcpy(new_sources, sources, |
| 234 | sizeof(struct source) * num_sources); |
| 235 | free(sources); |
| 236 | } |
| 237 | |
| 238 | s = &new_sources[num_sources++]; |
| 239 | s->fd = fd; |
| 240 | s->events = events; |
| 241 | s->timeout = timeout; |
| 242 | s->cb = callback; |
| 243 | s->user_data = user_data; |
| 244 | sources = new_sources; |
| 245 | |
| 246 | if (timeout != source_timeout && timeout > 0 |
| 247 | && (source_timeout == -1 || timeout < source_timeout)) |
| 248 | source_timeout = timeout; |
| 249 | } |
| 250 | |
| 251 | void session_source_remove(int fd) |
| 252 | { |
| 253 | struct source *new_sources; |
| 254 | int old, new; |
| 255 | |
| 256 | if (!sources) |
| 257 | return; |
| 258 | |
| 259 | new_sources = calloc(1, sizeof(struct source) * num_sources); |
| 260 | for (old = 0; old < num_sources; old++) |
| 261 | if (sources[old].fd != fd) |
| 262 | memcpy(&new_sources[new++], &sources[old], |
| 263 | sizeof(struct source)); |
| 264 | |
| 265 | if (old != new) { |
| 266 | free(sources); |
| 267 | sources = new_sources; |
| 268 | num_sources--; |
| 269 | } else { |
| 270 | /* Target fd was not found. */ |
| 271 | free(new_sources); |
| 272 | } |
| 273 | } |
| 274 | |