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 | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 35 | sr_receive_data_callback_t cb; |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 36 | void *cb_data; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 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 | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 79 | g_slist_free(session->devs); |
| 80 | session->devs = 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 | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 100 | SR_API int sr_session_dev_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 | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 107 | g_slist_free(session->devs); |
| 108 | session->devs = 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 | * |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 116 | * @param dev The device to add to the current session. Must not be NULL. |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 117 | * Also, dev->driver and dev->driver->dev_open must not be NULL. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 118 | * |
| 119 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
| 120 | */ |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 121 | SR_API int sr_session_dev_add(struct sr_dev *dev) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 122 | { |
| 123 | int ret; |
| 124 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 125 | if (!dev) { |
| 126 | sr_err("session: %s: dev was NULL", __func__); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 127 | return SR_ERR_ARG; |
| 128 | } |
| 129 | |
Uwe Hermann | d6eb0c3 | 2012-03-18 12:57:34 +0100 | [diff] [blame^] | 130 | if (!session) { |
| 131 | sr_err("session: %s: session was NULL", __func__); |
| 132 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Uwe Hermann | d6eb0c3 | 2012-03-18 12:57:34 +0100 | [diff] [blame^] | 135 | /* If dev->driver is NULL, this is a virtual device. */ |
| 136 | if (!dev->driver) { |
| 137 | sr_dbg("session: %s: dev->driver was NULL, this seems to be " |
| 138 | "a virtual device; continuing", __func__); |
| 139 | /* Just add the device, don't run dev_open(). */ |
| 140 | session->devs = g_slist_append(session->devs, dev); |
| 141 | return SR_OK; |
| 142 | } |
| 143 | |
| 144 | /* dev->driver is non-NULL (i.e. we have a real device). */ |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 145 | if (!dev->driver->dev_open) { |
| 146 | sr_err("session: %s: dev->driver->dev_open was NULL", |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 147 | __func__); |
| 148 | return SR_ERR_ARG; |
| 149 | } |
| 150 | |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 151 | if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) { |
Uwe Hermann | e7eb703 | 2012-02-18 11:57:43 +0100 | [diff] [blame] | 152 | sr_err("session: %s: dev_open failed (%d)", __func__, ret); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 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 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 156 | session->devs = g_slist_append(session->devs, dev); |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 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 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 165 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 166 | SR_API int sr_session_datafeed_callback_clear(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 167 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 168 | if (!session) { |
| 169 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 170 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 173 | g_slist_free(session->datafeed_callbacks); |
| 174 | session->datafeed_callbacks = NULL; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 175 | |
| 176 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 179 | /** |
| 180 | * Add a datafeed callback to the current session. |
| 181 | * |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 182 | * @param cb Function to call when a chunk of data is received. |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 183 | * Must not be NULL. |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 184 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 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 | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 187 | SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb) |
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 | |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 194 | if (!cb) { |
| 195 | sr_err("session: %s: cb was NULL", __func__); |
| 196 | return SR_ERR_ARG; |
| 197 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 198 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 199 | session->datafeed_callbacks = |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 200 | g_slist_append(session->datafeed_callbacks, cb); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 201 | |
| 202 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 205 | /** |
| 206 | * TODO. |
| 207 | */ |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 208 | static int sr_session_run_poll(void) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 209 | { |
| 210 | GPollFD *fds, my_gpollfd; |
| 211 | int ret, i; |
| 212 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 213 | fds = NULL; |
| 214 | while (session->running) { |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 215 | /* TODO: Add comment. */ |
| 216 | g_free(fds); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 217 | |
| 218 | /* Construct g_poll()'s array. */ |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 219 | if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) { |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 220 | sr_err("session: %s: fds malloc failed", __func__); |
| 221 | return SR_ERR_MALLOC; |
| 222 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 223 | for (i = 0; i < num_sources; i++) { |
| 224 | #ifdef _WIN32 |
| 225 | g_io_channel_win32_make_pollfd(&channels[0], |
| 226 | sources[i].events, &my_gpollfd); |
| 227 | #else |
| 228 | my_gpollfd.fd = sources[i].fd; |
| 229 | my_gpollfd.events = sources[i].events; |
| 230 | fds[i] = my_gpollfd; |
| 231 | #endif |
| 232 | } |
| 233 | |
| 234 | ret = g_poll(fds, num_sources, source_timeout); |
| 235 | |
| 236 | for (i = 0; i < num_sources; i++) { |
| 237 | if (fds[i].revents > 0 || (ret == 0 |
| 238 | && source_timeout == sources[i].timeout)) { |
| 239 | /* |
| 240 | * Invoke the source's callback on an event, |
| 241 | * or if the poll timeout out and this source |
| 242 | * asked for that timeout. |
| 243 | */ |
Gareth McMullin | 5c582d9 | 2011-11-06 11:44:25 +1300 | [diff] [blame] | 244 | if (!sources[i].cb(fds[i].fd, fds[i].revents, |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 245 | sources[i].cb_data)) |
Gareth McMullin | 5c582d9 | 2011-11-06 11:44:25 +1300 | [diff] [blame] | 246 | sr_session_source_remove(sources[i].fd); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 250 | g_free(fds); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 251 | |
| 252 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 255 | /** |
| 256 | * Start a session. |
| 257 | * |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 258 | * There can only be one session at a time. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 259 | * |
| 260 | * @return SR_OK upon success, SR_ERR upon errors. |
| 261 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 262 | SR_API int sr_session_start(void) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 263 | { |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 264 | struct sr_dev *dev; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 265 | GSList *l; |
| 266 | int ret; |
| 267 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 268 | if (!session) { |
| 269 | sr_err("session: %s: session was NULL; a session must be " |
| 270 | "created first, before starting it.", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 271 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 274 | if (!session->devs) { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 275 | /* TODO: Actually the case? */ |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 276 | sr_err("session: %s: session->devs was NULL; a session " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 277 | "cannot be started without devices.", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 278 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 281 | /* TODO: Check driver_index validity? */ |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 282 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 283 | sr_info("session: starting"); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 284 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 285 | for (l = session->devs; l; l = l->next) { |
| 286 | dev = l->data; |
| 287 | /* TODO: Check for dev != NULL. */ |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 288 | if ((ret = dev->driver->dev_acquisition_start( |
| 289 | dev->driver_index, dev)) != SR_OK) { |
Renato Caldas | 446a037 | 2012-01-06 00:04:29 +0000 | [diff] [blame] | 290 | sr_err("session: %s: could not start an acquisition " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 291 | "(%d)", __func__, ret); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 292 | break; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 293 | } |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 296 | /* TODO: What if there are multiple devices? Which return code? */ |
| 297 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 298 | return ret; |
| 299 | } |
| 300 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 301 | /** |
| 302 | * Run the session. |
| 303 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 304 | * TODO: Various error checks etc. |
| 305 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 306 | * @return SR_OK upon success, SR_ERR_BUG upon errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 307 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 308 | SR_API int sr_session_run(void) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 309 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 310 | if (!session) { |
| 311 | sr_err("session: %s: session was NULL; a session must be " |
| 312 | "created first, before running it.", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 313 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 316 | if (!session->devs) { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 317 | /* TODO: Actually the case? */ |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 318 | sr_err("session: %s: session->devs was NULL; a session " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 319 | "cannot be run without devices.", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 320 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 323 | sr_info("session: running"); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 324 | session->running = TRUE; |
| 325 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 326 | /* Do we have real sources? */ |
| 327 | if (num_sources == 1 && sources[0].fd == -1) { |
| 328 | /* Dummy source, freewheel over it. */ |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 329 | while (session->running) |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 330 | sources[0].cb(-1, 0, sources[0].cb_data); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 331 | } else { |
| 332 | /* Real sources, use g_poll() main loop. */ |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 333 | sr_session_run_poll(); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 336 | return SR_OK; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 339 | /** |
| 340 | * Halt the current session. |
| 341 | * |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 342 | * This requests the current session be stopped as soon as possible, for |
| 343 | * example on receiving an SR_DF_END packet. |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 344 | * |
| 345 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 346 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 347 | SR_API int sr_session_halt(void) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 348 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 349 | if (!session) { |
| 350 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 351 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 354 | sr_info("session: halting"); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 355 | session->running = FALSE; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 356 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 357 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 360 | /** |
| 361 | * Stop the current session. |
| 362 | * |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 363 | * The current session is stopped immediately, with all acquisition sessions |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 364 | * being stopped and hardware drivers cleaned up. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 365 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 366 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 367 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 368 | SR_API int sr_session_stop(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 369 | { |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 370 | struct sr_dev *dev; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 371 | GSList *l; |
| 372 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 373 | if (!session) { |
| 374 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 375 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 378 | sr_info("session: stopping"); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 379 | session->running = FALSE; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 380 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 381 | for (l = session->devs; l; l = l->next) { |
| 382 | dev = l->data; |
| 383 | /* Check for dev != NULL. */ |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 384 | if (dev->driver) { |
| 385 | if (dev->driver->dev_acquisition_stop) |
| 386 | dev->driver->dev_acquisition_stop(dev->driver_index, dev); |
| 387 | if (dev->driver->cleanup) |
| 388 | dev->driver->cleanup(); |
Bert Vermeulen | 8c76be5 | 2012-01-08 22:05:00 +0100 | [diff] [blame] | 389 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 390 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 391 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 392 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 395 | /** |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 396 | * Debug helper. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 397 | * |
Bert Vermeulen | 996b0c7 | 2012-02-13 02:13:51 +0100 | [diff] [blame] | 398 | * @param packet The packet to show debugging information for. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 399 | */ |
Bert Vermeulen | 18beaef | 2012-02-13 00:08:23 +0100 | [diff] [blame] | 400 | static void 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 | |
| 404 | switch (packet->type) { |
| 405 | case SR_DF_HEADER: |
| 406 | sr_dbg("bus: received SR_DF_HEADER"); |
| 407 | break; |
| 408 | case SR_DF_TRIGGER: |
Bert Vermeulen | 0146970 | 2012-02-01 02:59:41 +0100 | [diff] [blame] | 409 | sr_dbg("bus: received SR_DF_TRIGGER"); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 410 | break; |
| 411 | case SR_DF_LOGIC: |
| 412 | logic = packet->payload; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 413 | /* TODO: Check for logic != NULL. */ |
Bert Vermeulen | 0146970 | 2012-02-01 02:59:41 +0100 | [diff] [blame] | 414 | sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 415 | break; |
| 416 | case SR_DF_END: |
| 417 | sr_dbg("bus: received SR_DF_END"); |
| 418 | break; |
| 419 | default: |
Bert Vermeulen | 18beaef | 2012-02-13 00:08:23 +0100 | [diff] [blame] | 420 | sr_dbg("bus: received unknown packet type %d", packet->type); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 421 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 422 | } |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 425 | /** |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 426 | * Send a packet to whatever is listening on the datafeed bus. |
| 427 | * |
| 428 | * Hardware drivers use this to send a data packet to the frontend. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 429 | * |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 430 | * @param dev TODO. |
Uwe Hermann | 31ccebc | 2012-02-29 22:08:45 +0100 | [diff] [blame] | 431 | * @param packet The datafeed packet to send to the session bus. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 432 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 433 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 434 | */ |
Uwe Hermann | 31ccebc | 2012-02-29 22:08:45 +0100 | [diff] [blame] | 435 | SR_PRIV int sr_session_send(struct sr_dev *dev, |
| 436 | struct sr_datafeed_packet *packet) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 437 | { |
| 438 | GSList *l; |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 439 | sr_datafeed_callback_t cb; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 440 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 441 | if (!dev) { |
| 442 | sr_err("session: %s: dev was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 443 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 446 | if (!packet) { |
| 447 | sr_err("session: %s: packet was NULL", __func__); |
| 448 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 451 | for (l = session->datafeed_callbacks; l; l = l->next) { |
Bert Vermeulen | 18beaef | 2012-02-13 00:08:23 +0100 | [diff] [blame] | 452 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
| 453 | datafeed_dump(packet); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 454 | cb = l->data; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 455 | /* TODO: Check for cb != NULL. */ |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 456 | cb(dev, packet); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 457 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 458 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 459 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 462 | /** |
| 463 | * TODO. |
| 464 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 465 | * TODO: More error checks etc. |
| 466 | * |
| 467 | * @param fd TODO. |
| 468 | * @param events TODO. |
| 469 | * @param timeout TODO. |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 470 | * @param cb Callback function to add. Must not be NULL. |
| 471 | * @param cb_data Data for the callback function. Can be NULL. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 472 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 473 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 474 | * SR_ERR_MALLOC upon memory allocation errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 475 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 476 | SR_API int sr_session_source_add(int fd, int events, int timeout, |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 477 | sr_receive_data_callback_t cb, void *cb_data) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 478 | { |
| 479 | struct source *new_sources, *s; |
| 480 | |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 481 | if (!cb) { |
| 482 | sr_err("session: %s: cb was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 483 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 486 | /* Note: cb_data can be NULL, that's not a bug. */ |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 487 | |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 488 | new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1)); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 489 | if (!new_sources) { |
| 490 | sr_err("session: %s: new_sources malloc failed", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 491 | return SR_ERR_MALLOC; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 492 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 493 | |
| 494 | if (sources) { |
| 495 | memcpy(new_sources, sources, |
| 496 | sizeof(struct source) * num_sources); |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 497 | g_free(sources); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | s = &new_sources[num_sources++]; |
| 501 | s->fd = fd; |
| 502 | s->events = events; |
| 503 | s->timeout = timeout; |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 504 | s->cb = cb; |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 505 | s->cb_data = cb_data; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 506 | sources = new_sources; |
| 507 | |
| 508 | if (timeout != source_timeout && timeout > 0 |
| 509 | && (source_timeout == -1 || timeout < source_timeout)) |
| 510 | source_timeout = timeout; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 511 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 512 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 515 | /** |
| 516 | * Remove the source belonging to the specified file descriptor. |
| 517 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 518 | * TODO: More error checks. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 519 | * |
| 520 | * @param fd TODO. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 521 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 522 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 523 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon |
| 524 | * internal errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 525 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 526 | SR_API int sr_session_source_remove(int fd) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 527 | { |
| 528 | struct source *new_sources; |
| 529 | int old, new; |
| 530 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 531 | if (!sources) { |
| 532 | sr_err("session: %s: sources was NULL", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 533 | return SR_ERR_BUG; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /* TODO: Check if 'fd' valid. */ |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 537 | |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 538 | new_sources = g_try_malloc0(sizeof(struct source) * num_sources); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 539 | if (!new_sources) { |
| 540 | sr_err("session: %s: new_sources malloc failed", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 541 | return SR_ERR_MALLOC; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | for (old = 0, new = 0; old < num_sources; old++) { |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 545 | if (sources[old].fd != fd) |
| 546 | memcpy(&new_sources[new++], &sources[old], |
| 547 | sizeof(struct source)); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 548 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 549 | |
| 550 | if (old != new) { |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 551 | g_free(sources); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 552 | sources = new_sources; |
| 553 | num_sources--; |
| 554 | } else { |
| 555 | /* Target fd was not found. */ |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 556 | g_free(new_sources); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 557 | } |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 558 | |
| 559 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 560 | } |