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