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 | |
Uwe Hermann | 22b0238 | 2011-02-02 10:25:52 +0100 | [diff] [blame] | 20 | #include "config.h" |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <string.h> |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 25 | #include <glib.h> |
Uwe Hermann | b7f09cf | 2011-12-28 23:07:08 +0100 | [diff] [blame] | 26 | #include "sigrok.h" |
| 27 | #include "sigrok-internal.h" |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 28 | |
Uwe Hermann | 9f4bc44 | 2011-01-31 14:14:28 +0100 | [diff] [blame] | 29 | /* demo.c */ |
| 30 | extern GIOChannel channels[2]; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 31 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 32 | struct source { |
| 33 | int fd; |
| 34 | int events; |
| 35 | int timeout; |
Uwe Hermann | a887e3d | 2011-02-20 14:14:13 +0100 | [diff] [blame] | 36 | sr_receive_data_callback cb; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 37 | void *user_data; |
| 38 | }; |
| 39 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 40 | /* There can only be one session at a time. */ |
Uwe Hermann | a0ecd83 | 2011-12-28 22:55:21 +0100 | [diff] [blame] | 41 | /* 'session' is not static, it's used elsewhere (via 'extern'). */ |
Uwe Hermann | 2872d21 | 2011-02-08 17:50:29 +0100 | [diff] [blame] | 42 | struct sr_session *session; |
Uwe Hermann | a0ecd83 | 2011-12-28 22:55:21 +0100 | [diff] [blame] | 43 | static int num_sources = 0; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 44 | |
Uwe Hermann | a0ecd83 | 2011-12-28 22:55:21 +0100 | [diff] [blame] | 45 | static struct source *sources = NULL; |
| 46 | static int source_timeout = -1; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 47 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 48 | /** |
| 49 | * Create a new session. |
| 50 | * |
| 51 | * TODO. |
| 52 | * |
| 53 | * TODO: Should return int? |
| 54 | * TODO: Should it use the file-global "session" variable or take an argument? |
| 55 | * The same question applies to all the other session functions. |
| 56 | * |
| 57 | * @return A pointer to the newly allocated session, or NULL upon errors. |
| 58 | */ |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 59 | struct sr_session *sr_session_new(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 60 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 61 | if (!(session = calloc(1, sizeof(struct sr_session)))) { |
| 62 | sr_err("session: %s: session malloc failed", __func__); |
| 63 | return NULL; /* TODO: SR_ERR_MALLOC? */ |
| 64 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 65 | |
| 66 | return session; |
| 67 | } |
| 68 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 69 | /** |
| 70 | * Destroy the current session. |
| 71 | * |
| 72 | * This frees up all memory used by the session. |
| 73 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 74 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 75 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 76 | int sr_session_destroy(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 77 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 78 | if (!session) { |
| 79 | sr_warn("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 80 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 83 | g_slist_free(session->devices); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 84 | session->devices = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 85 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 86 | /* TODO: Error checks needed? */ |
| 87 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 88 | /* TODO: Loop over protocol decoders and free them. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 89 | |
| 90 | g_free(session); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 91 | session = NULL; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 92 | |
| 93 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 96 | /** |
| 97 | * Remove all the devices from the current session. TODO? |
| 98 | * |
| 99 | * The session itself (i.e., the struct sr_session) is not free'd and still |
| 100 | * exists after this function returns. |
| 101 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 102 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 103 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 104 | int sr_session_device_clear(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 105 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 106 | if (!session) { |
| 107 | sr_warn("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 108 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 111 | g_slist_free(session->devices); |
| 112 | session->devices = NULL; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 113 | |
| 114 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 117 | /** |
| 118 | * Add a device to the current session. |
| 119 | * |
| 120 | * @param device The device to add to the current session. Must not be NULL. |
| 121 | * Also, device->plugin and device->plugin->opendev must not |
| 122 | * be NULL. |
| 123 | * |
| 124 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
| 125 | */ |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 126 | int sr_session_device_add(struct sr_device *device) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 127 | { |
| 128 | int ret; |
| 129 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 130 | if (!device) { |
| 131 | sr_err("session: %s: device was NULL", __func__); |
| 132 | return SR_ERR_ARG; |
| 133 | } |
| 134 | |
| 135 | if (!device->plugin) { |
| 136 | sr_err("session: %s: device->plugin was NULL", __func__); |
| 137 | return SR_ERR_ARG; |
| 138 | } |
| 139 | |
| 140 | if (!device->plugin->opendev) { |
| 141 | sr_err("session: %s: device->plugin->opendev was NULL", |
| 142 | __func__); |
| 143 | return SR_ERR_ARG; |
| 144 | } |
| 145 | |
| 146 | if (!session) { |
| 147 | sr_err("session: %s: session was NULL", __func__); |
| 148 | return SR_ERR; /* TODO: SR_ERR_BUG? */ |
| 149 | } |
| 150 | |
| 151 | if ((ret = device->plugin->opendev(device->plugin_index)) != SR_OK) { |
| 152 | sr_err("session: %s: opendev failed (%d)", __func__, ret); |
| 153 | return ret; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 154 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 155 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 156 | session->devices = g_slist_append(session->devices, device); |
| 157 | |
Uwe Hermann | e46b8fb | 2011-01-29 16:23:12 +0100 | [diff] [blame] | 158 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 161 | /** |
| 162 | * Clear all datafeed callbacks in the current session. |
| 163 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 164 | * TODO. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 165 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 166 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 167 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 168 | int sr_session_datafeed_callback_clear(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 169 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 170 | if (!session) { |
| 171 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 172 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 175 | g_slist_free(session->datafeed_callbacks); |
| 176 | session->datafeed_callbacks = NULL; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 177 | |
| 178 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 181 | /** |
| 182 | * Add a datafeed callback to the current session. |
| 183 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 184 | * @param callback TODO. |
| 185 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 186 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 187 | int sr_session_datafeed_callback_add(sr_datafeed_callback callback) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 188 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 189 | if (!session) { |
| 190 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 191 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* TODO: Is 'callback' allowed to be NULL? */ |
| 195 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 196 | session->datafeed_callbacks = |
| 197 | g_slist_append(session->datafeed_callbacks, callback); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 198 | |
| 199 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 202 | /** |
| 203 | * TODO. |
| 204 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 205 | static int sr_session_run_poll(void) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 206 | { |
| 207 | GPollFD *fds, my_gpollfd; |
| 208 | int ret, i; |
| 209 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 210 | fds = NULL; |
| 211 | while (session->running) { |
| 212 | if (fds) |
| 213 | free(fds); |
| 214 | |
| 215 | /* Construct g_poll()'s array. */ |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 216 | /* TODO: Check malloc return value. */ |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 217 | fds = malloc(sizeof(GPollFD) * num_sources); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 218 | if (!fds) { |
| 219 | sr_err("session: %s: fds malloc failed", __func__); |
| 220 | return SR_ERR_MALLOC; |
| 221 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 222 | for (i = 0; i < num_sources; i++) { |
| 223 | #ifdef _WIN32 |
| 224 | g_io_channel_win32_make_pollfd(&channels[0], |
| 225 | sources[i].events, &my_gpollfd); |
| 226 | #else |
| 227 | my_gpollfd.fd = sources[i].fd; |
| 228 | my_gpollfd.events = sources[i].events; |
| 229 | fds[i] = my_gpollfd; |
| 230 | #endif |
| 231 | } |
| 232 | |
| 233 | ret = g_poll(fds, num_sources, source_timeout); |
| 234 | |
| 235 | for (i = 0; i < num_sources; i++) { |
| 236 | if (fds[i].revents > 0 || (ret == 0 |
| 237 | && source_timeout == sources[i].timeout)) { |
| 238 | /* |
| 239 | * Invoke the source's callback on an event, |
| 240 | * or if the poll timeout out and this source |
| 241 | * asked for that timeout. |
| 242 | */ |
Gareth McMullin | 5c582d9 | 2011-11-06 11:44:25 +1300 | [diff] [blame] | 243 | if (!sources[i].cb(fds[i].fd, fds[i].revents, |
| 244 | sources[i].user_data)) |
| 245 | sr_session_source_remove(sources[i].fd); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | free(fds); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 250 | |
| 251 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 254 | /** |
| 255 | * Start a session. |
| 256 | * |
| 257 | * There can only be one session at a time. TODO |
| 258 | * |
| 259 | * @return SR_OK upon success, SR_ERR upon errors. |
| 260 | */ |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 261 | int sr_session_start(void) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 262 | { |
| 263 | struct sr_device *device; |
| 264 | GSList *l; |
| 265 | int ret; |
| 266 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 267 | if (!session) { |
| 268 | sr_err("session: %s: session was NULL; a session must be " |
| 269 | "created first, before starting it.", __func__); |
| 270 | return SR_ERR; /* TODO: SR_ERR_BUG? */ |
| 271 | } |
| 272 | |
| 273 | if (!session->devices) { |
| 274 | /* TODO: Actually the case? */ |
| 275 | sr_err("session: %s: session->devices was NULL; a session " |
| 276 | "cannot be started without devices.", __func__); |
| 277 | return SR_ERR; /* TODO: SR_ERR_BUG? */ |
| 278 | } |
| 279 | |
| 280 | /* TODO: Check plugin_index validity? */ |
| 281 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 282 | sr_info("session: starting"); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 283 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 284 | for (l = session->devices; l; l = l->next) { |
| 285 | device = l->data; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 286 | /* TODO: Check for device != NULL. */ |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 287 | if ((ret = device->plugin->start_acquisition( |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 288 | device->plugin_index, device)) != SR_OK) { |
Renato Caldas | 446a037 | 2012-01-06 00:04:29 +0000 | [diff] [blame] | 289 | sr_err("session: %s: could not start an acquisition " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 290 | "(%d)", __func__, ret); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 291 | break; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 292 | } |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 295 | /* TODO: What if there are multiple devices? Which return code? */ |
| 296 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 297 | return ret; |
| 298 | } |
| 299 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 300 | /** |
| 301 | * Run the session. |
| 302 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 303 | * TODO: Various error checks etc. |
| 304 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 305 | * @return SR_OK upon success, SR_ERR_BUG upon errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 306 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 307 | int sr_session_run(void) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 308 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 309 | if (!session) { |
| 310 | sr_err("session: %s: session was NULL; a session must be " |
| 311 | "created first, before running it.", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 312 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if (!session->devices) { |
| 316 | /* TODO: Actually the case? */ |
| 317 | sr_err("session: %s: session->devices was NULL; a session " |
| 318 | "cannot be run without devices.", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 319 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 322 | sr_info("session: running"); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 323 | session->running = TRUE; |
| 324 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 325 | /* Do we have real sources? */ |
| 326 | if (num_sources == 1 && sources[0].fd == -1) { |
| 327 | /* Dummy source, freewheel over it. */ |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 328 | while (session->running) |
| 329 | sources[0].cb(-1, 0, sources[0].user_data); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 330 | } else { |
| 331 | /* Real sources, use g_poll() main loop. */ |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 332 | sr_session_run_poll(); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 335 | return SR_OK; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 338 | /** |
| 339 | * Halt the current session. |
| 340 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 341 | * TODO. |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 342 | * |
| 343 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 344 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 345 | int sr_session_halt(void) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 346 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 347 | if (!session) { |
| 348 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 349 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 352 | sr_info("session: halting"); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 353 | session->running = FALSE; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 354 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 355 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 358 | /** |
| 359 | * Stop the current session. |
| 360 | * |
| 361 | * TODO: Difference to halt? |
| 362 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 363 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 364 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 365 | int sr_session_stop(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 366 | { |
Uwe Hermann | 5c2d46d | 2011-01-30 16:19:42 +0100 | [diff] [blame] | 367 | struct sr_device *device; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 368 | GSList *l; |
| 369 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 370 | if (!session) { |
| 371 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 372 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 375 | sr_info("session: stopping"); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 376 | session->running = FALSE; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 377 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 378 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 379 | device = l->data; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 380 | /* Check for device != NULL. */ |
Bert Vermeulen | 8c76be5 | 2012-01-08 22:05:00 +0100 | [diff] [blame^] | 381 | if (device->plugin) { |
| 382 | if (device->plugin->stop_acquisition) |
| 383 | device->plugin->stop_acquisition(device->plugin_index, device); |
| 384 | if (device->plugin->cleanup) |
| 385 | device->plugin->cleanup(); |
| 386 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 387 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 388 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 389 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 392 | /** |
| 393 | * TODO. |
| 394 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 395 | * TODO: Various error checks. |
| 396 | * |
| 397 | * @param packet TODO. |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 398 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 399 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 400 | static int datafeed_dump(struct sr_datafeed_packet *packet) |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 401 | { |
| 402 | struct sr_datafeed_logic *logic; |
| 403 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 404 | if (!packet) { |
| 405 | sr_err("session: %s: packet was NULL", __func__); |
| 406 | return SR_ERR_ARG; |
| 407 | } |
| 408 | |
| 409 | /* TODO: Validity checks for packet contents. */ |
| 410 | |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 411 | switch (packet->type) { |
| 412 | case SR_DF_HEADER: |
| 413 | sr_dbg("bus: received SR_DF_HEADER"); |
| 414 | break; |
| 415 | case SR_DF_TRIGGER: |
| 416 | sr_dbg("bus: received SR_DF_TRIGGER at %lu ms", |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 417 | packet->timeoffset / 1000000); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 418 | break; |
| 419 | case SR_DF_LOGIC: |
| 420 | logic = packet->payload; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 421 | /* TODO: Check for logic != NULL. */ |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 422 | sr_dbg("bus: received SR_DF_LOGIC at %f ms duration %f ms, " |
| 423 | "%" PRIu64 " bytes", packet->timeoffset / 1000000.0, |
| 424 | packet->duration / 1000000.0, logic->length); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 425 | break; |
| 426 | case SR_DF_END: |
| 427 | sr_dbg("bus: received SR_DF_END"); |
| 428 | break; |
| 429 | default: |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 430 | /* TODO: Abort? */ |
| 431 | sr_err("bus: received unknown packet type %d", packet->type); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 432 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 433 | } |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 434 | |
| 435 | return SR_OK; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 438 | /** |
| 439 | * TODO. |
| 440 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 441 | * @param device TODO. |
| 442 | * @param packet TODO. |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 443 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 444 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 445 | int sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 446 | { |
| 447 | GSList *l; |
Uwe Hermann | 13b0573 | 2011-02-20 14:09:15 +0100 | [diff] [blame] | 448 | sr_datafeed_callback cb; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 449 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 450 | if (!device) { |
| 451 | sr_err("session: %s: device was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 452 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | if (!device->plugin) { |
| 456 | sr_err("session: %s: device->plugin was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 457 | return SR_ERR_ARG; |
| 458 | } |
| 459 | |
| 460 | if (!packet) { |
| 461 | sr_err("session: %s: packet was NULL", __func__); |
| 462 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 465 | /* |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 466 | * 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] | 467 | * the callbacks as well. |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 468 | */ |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 469 | for (l = session->datafeed_callbacks; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 470 | cb = l->data; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 471 | /* TODO: Check for cb != NULL. */ |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 472 | datafeed_dump(packet); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 473 | cb(device, packet); |
| 474 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 475 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 476 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 479 | /** |
| 480 | * TODO. |
| 481 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 482 | * TODO: Switch to g_try_malloc0() / g_free(). |
| 483 | * TODO: More error checks etc. |
| 484 | * |
| 485 | * @param fd TODO. |
| 486 | * @param events TODO. |
| 487 | * @param timeout TODO. |
| 488 | * @param callback TODO. |
| 489 | * @param user_data TODO. |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 490 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 491 | * SR_ERR_MALLOC upon memory allocation errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 492 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 493 | int sr_session_source_add(int fd, int events, int timeout, |
Uwe Hermann | a887e3d | 2011-02-20 14:14:13 +0100 | [diff] [blame] | 494 | sr_receive_data_callback callback, void *user_data) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 495 | { |
| 496 | struct source *new_sources, *s; |
| 497 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 498 | if (!callback) { |
| 499 | sr_err("session: %s: callback was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 500 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* Note: user_data can be NULL, that's not a bug. */ |
| 504 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 505 | new_sources = calloc(1, sizeof(struct source) * (num_sources + 1)); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 506 | if (!new_sources) { |
| 507 | sr_err("session: %s: new_sources malloc failed", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 508 | return SR_ERR_MALLOC; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 509 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 510 | |
| 511 | if (sources) { |
| 512 | memcpy(new_sources, sources, |
| 513 | sizeof(struct source) * num_sources); |
| 514 | free(sources); |
| 515 | } |
| 516 | |
| 517 | s = &new_sources[num_sources++]; |
| 518 | s->fd = fd; |
| 519 | s->events = events; |
| 520 | s->timeout = timeout; |
| 521 | s->cb = callback; |
| 522 | s->user_data = user_data; |
| 523 | sources = new_sources; |
| 524 | |
| 525 | if (timeout != source_timeout && timeout > 0 |
| 526 | && (source_timeout == -1 || timeout < source_timeout)) |
| 527 | source_timeout = timeout; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 528 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 529 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 532 | /** |
| 533 | * Remove the source belonging to the specified file descriptor. |
| 534 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 535 | * TODO: More error checks. |
| 536 | * TODO: Switch to g_try_malloc0() / g_free(). |
| 537 | * |
| 538 | * @param fd TODO. |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 539 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 540 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon |
| 541 | * internal errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 542 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 543 | int sr_session_source_remove(int fd) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 544 | { |
| 545 | struct source *new_sources; |
| 546 | int old, new; |
| 547 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 548 | if (!sources) { |
| 549 | sr_err("session: %s: sources was NULL", __func__); |
| 550 | return SR_ERR_BUG; /* TODO: Other? */ |
| 551 | } |
| 552 | |
| 553 | /* TODO: Check if 'fd' valid. */ |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 554 | |
| 555 | new_sources = calloc(1, sizeof(struct source) * num_sources); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 556 | if (!new_sources) { |
| 557 | sr_err("session: %s: new_sources malloc failed", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 558 | return SR_ERR_MALLOC; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | for (old = 0, new = 0; old < num_sources; old++) { |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 562 | if (sources[old].fd != fd) |
| 563 | memcpy(&new_sources[new++], &sources[old], |
| 564 | sizeof(struct source)); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 565 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 566 | |
| 567 | if (old != new) { |
| 568 | free(sources); |
| 569 | sources = new_sources; |
| 570 | num_sources--; |
| 571 | } else { |
| 572 | /* Target fd was not found. */ |
| 573 | free(new_sources); |
| 574 | } |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 575 | |
| 576 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 577 | } |