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> |
Bert Vermeulen | 45c59c8 | 2012-07-05 00:55:07 +0200 | [diff] [blame] | 25 | #include "libsigrok.h" |
| 26 | #include "libsigrok-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 | 01c3e9d | 2012-03-28 21:55:48 +0200 | [diff] [blame] | 100 | SR_API int sr_session_dev_remove_all(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__); |
Uwe Hermann | 8ec95d2 | 2012-03-21 19:28:43 +0100 | [diff] [blame] | 148 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 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 | /** |
Uwe Hermann | 01c3e9d | 2012-03-28 21:55:48 +0200 | [diff] [blame] | 162 | * Remove all datafeed callbacks in the current session. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 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 | 01c3e9d | 2012-03-28 21:55:48 +0200 | [diff] [blame] | 166 | SR_API int sr_session_datafeed_callback_remove_all(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 | c13536f | 2012-06-19 23:33:50 +0200 | [diff] [blame] | 220 | /* Not enough memory, or num_sources was 0. */ |
| 221 | sr_err("session: %s: fds malloc failed " |
| 222 | "(num_sources was %d).", __func__, num_sources); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 223 | return SR_ERR_MALLOC; |
| 224 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 225 | for (i = 0; i < num_sources; i++) { |
| 226 | #ifdef _WIN32 |
| 227 | g_io_channel_win32_make_pollfd(&channels[0], |
| 228 | sources[i].events, &my_gpollfd); |
| 229 | #else |
| 230 | my_gpollfd.fd = sources[i].fd; |
| 231 | my_gpollfd.events = sources[i].events; |
| 232 | fds[i] = my_gpollfd; |
| 233 | #endif |
| 234 | } |
| 235 | |
| 236 | ret = g_poll(fds, num_sources, source_timeout); |
| 237 | |
| 238 | for (i = 0; i < num_sources; i++) { |
| 239 | if (fds[i].revents > 0 || (ret == 0 |
| 240 | && source_timeout == sources[i].timeout)) { |
| 241 | /* |
| 242 | * Invoke the source's callback on an event, |
| 243 | * or if the poll timeout out and this source |
| 244 | * asked for that timeout. |
| 245 | */ |
Gareth McMullin | 5c582d9 | 2011-11-06 11:44:25 +1300 | [diff] [blame] | 246 | if (!sources[i].cb(fds[i].fd, fds[i].revents, |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 247 | sources[i].cb_data)) |
Gareth McMullin | 5c582d9 | 2011-11-06 11:44:25 +1300 | [diff] [blame] | 248 | sr_session_source_remove(sources[i].fd); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 252 | g_free(fds); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 253 | |
| 254 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 257 | /** |
| 258 | * Start a session. |
| 259 | * |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 260 | * There can only be one session at a time. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 261 | * |
| 262 | * @return SR_OK upon success, SR_ERR upon errors. |
| 263 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 264 | SR_API int sr_session_start(void) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 265 | { |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 266 | struct sr_dev *dev; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 267 | GSList *l; |
| 268 | int ret; |
| 269 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 270 | if (!session) { |
| 271 | sr_err("session: %s: session was NULL; a session must be " |
| 272 | "created first, before starting it.", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 273 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 276 | if (!session->devs) { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 277 | /* TODO: Actually the case? */ |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 278 | sr_err("session: %s: session->devs was NULL; a session " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 279 | "cannot be started without devices.", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 280 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 283 | /* TODO: Check driver_index validity? */ |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 284 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 285 | sr_info("session: starting"); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 286 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 287 | for (l = session->devs; l; l = l->next) { |
| 288 | dev = l->data; |
| 289 | /* TODO: Check for dev != NULL. */ |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 290 | if ((ret = dev->driver->dev_acquisition_start( |
| 291 | dev->driver_index, dev)) != SR_OK) { |
Renato Caldas | 446a037 | 2012-01-06 00:04:29 +0000 | [diff] [blame] | 292 | sr_err("session: %s: could not start an acquisition " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 293 | "(%d)", __func__, ret); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 294 | break; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 295 | } |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 298 | /* TODO: What if there are multiple devices? Which return code? */ |
| 299 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 300 | return ret; |
| 301 | } |
| 302 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 303 | /** |
| 304 | * Run the session. |
| 305 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 306 | * TODO: Various error checks etc. |
| 307 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 308 | * @return SR_OK upon success, SR_ERR_BUG upon errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 309 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 310 | SR_API int sr_session_run(void) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 311 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 312 | if (!session) { |
| 313 | sr_err("session: %s: session was NULL; a session must be " |
| 314 | "created first, before running it.", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 315 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 318 | if (!session->devs) { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 319 | /* TODO: Actually the case? */ |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 320 | sr_err("session: %s: session->devs was NULL; a session " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 321 | "cannot be run without devices.", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 322 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 325 | sr_info("session: running"); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 326 | session->running = TRUE; |
| 327 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 328 | /* Do we have real sources? */ |
| 329 | if (num_sources == 1 && sources[0].fd == -1) { |
| 330 | /* Dummy source, freewheel over it. */ |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 331 | while (session->running) |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 332 | sources[0].cb(-1, 0, sources[0].cb_data); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 333 | } else { |
| 334 | /* Real sources, use g_poll() main loop. */ |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 335 | sr_session_run_poll(); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 338 | return SR_OK; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 341 | /** |
| 342 | * Halt the current session. |
| 343 | * |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 344 | * This requests the current session be stopped as soon as possible, for |
| 345 | * example on receiving an SR_DF_END packet. |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 346 | * |
| 347 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 348 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 349 | SR_API int sr_session_halt(void) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 350 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 351 | if (!session) { |
| 352 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 353 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 356 | sr_info("session: halting"); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 357 | session->running = FALSE; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 358 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 359 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 362 | /** |
| 363 | * Stop the current session. |
| 364 | * |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 365 | * The current session is stopped immediately, with all acquisition sessions |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 366 | * being stopped and hardware drivers cleaned up. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 367 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 368 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 369 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 370 | SR_API int sr_session_stop(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 371 | { |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 372 | struct sr_dev *dev; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 373 | GSList *l; |
| 374 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 375 | if (!session) { |
| 376 | sr_err("session: %s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 377 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Uwe Hermann | b08024a | 2011-04-14 09:46:53 +0200 | [diff] [blame] | 380 | sr_info("session: stopping"); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 381 | session->running = FALSE; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 382 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 383 | for (l = session->devs; l; l = l->next) { |
| 384 | dev = l->data; |
| 385 | /* Check for dev != NULL. */ |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 386 | if (dev->driver) { |
| 387 | if (dev->driver->dev_acquisition_stop) |
| 388 | dev->driver->dev_acquisition_stop(dev->driver_index, dev); |
| 389 | if (dev->driver->cleanup) |
| 390 | dev->driver->cleanup(); |
Bert Vermeulen | 8c76be5 | 2012-01-08 22:05:00 +0100 | [diff] [blame] | 391 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 392 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 393 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 394 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 397 | /** |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 398 | * Debug helper. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 399 | * |
Bert Vermeulen | 996b0c7 | 2012-02-13 02:13:51 +0100 | [diff] [blame] | 400 | * @param packet The packet to show debugging information for. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 401 | */ |
Bert Vermeulen | 18beaef | 2012-02-13 00:08:23 +0100 | [diff] [blame] | 402 | static void datafeed_dump(struct sr_datafeed_packet *packet) |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 403 | { |
| 404 | struct sr_datafeed_logic *logic; |
Bert Vermeulen | ee7489d | 2012-04-22 18:11:31 +0200 | [diff] [blame] | 405 | struct sr_datafeed_analog *analog; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 406 | |
| 407 | switch (packet->type) { |
| 408 | case SR_DF_HEADER: |
| 409 | sr_dbg("bus: received SR_DF_HEADER"); |
| 410 | break; |
| 411 | case SR_DF_TRIGGER: |
Bert Vermeulen | 0146970 | 2012-02-01 02:59:41 +0100 | [diff] [blame] | 412 | sr_dbg("bus: received SR_DF_TRIGGER"); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 413 | break; |
Bert Vermeulen | ee7489d | 2012-04-22 18:11:31 +0200 | [diff] [blame] | 414 | case SR_DF_META_LOGIC: |
| 415 | sr_dbg("bus: received SR_DF_META_LOGIC"); |
| 416 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 417 | case SR_DF_LOGIC: |
| 418 | logic = packet->payload; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 419 | /* TODO: Check for logic != NULL. */ |
Bert Vermeulen | 0146970 | 2012-02-01 02:59:41 +0100 | [diff] [blame] | 420 | sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 421 | break; |
Bert Vermeulen | ee7489d | 2012-04-22 18:11:31 +0200 | [diff] [blame] | 422 | case SR_DF_META_ANALOG: |
| 423 | sr_dbg("bus: received SR_DF_META_LOGIC"); |
| 424 | break; |
| 425 | case SR_DF_ANALOG: |
| 426 | analog = packet->payload; |
| 427 | /* TODO: Check for analog != NULL. */ |
| 428 | sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples); |
| 429 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 430 | case SR_DF_END: |
| 431 | sr_dbg("bus: received SR_DF_END"); |
| 432 | break; |
Bert Vermeulen | 6ea7669 | 2012-04-30 19:55:06 +0200 | [diff] [blame] | 433 | case SR_DF_FRAME_BEGIN: |
| 434 | sr_dbg("bus: received SR_DF_FRAME_BEGIN"); |
| 435 | break; |
| 436 | case SR_DF_FRAME_END: |
| 437 | sr_dbg("bus: received SR_DF_FRAME_END"); |
| 438 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 439 | default: |
Bert Vermeulen | 18beaef | 2012-02-13 00:08:23 +0100 | [diff] [blame] | 440 | sr_dbg("bus: received unknown packet type %d", packet->type); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 441 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 442 | } |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 445 | /** |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 446 | * Send a packet to whatever is listening on the datafeed bus. |
| 447 | * |
| 448 | * Hardware drivers use this to send a data packet to the frontend. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 449 | * |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 450 | * @param dev TODO. |
Uwe Hermann | 31ccebc | 2012-02-29 22:08:45 +0100 | [diff] [blame] | 451 | * @param packet The datafeed packet to send to the session bus. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 452 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 453 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 454 | */ |
Uwe Hermann | 31ccebc | 2012-02-29 22:08:45 +0100 | [diff] [blame] | 455 | SR_PRIV int sr_session_send(struct sr_dev *dev, |
| 456 | struct sr_datafeed_packet *packet) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 457 | { |
| 458 | GSList *l; |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 459 | sr_datafeed_callback_t cb; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 460 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 461 | if (!dev) { |
| 462 | sr_err("session: %s: dev was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 463 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 466 | if (!packet) { |
| 467 | sr_err("session: %s: packet was NULL", __func__); |
| 468 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 471 | for (l = session->datafeed_callbacks; l; l = l->next) { |
Bert Vermeulen | 18beaef | 2012-02-13 00:08:23 +0100 | [diff] [blame] | 472 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
| 473 | datafeed_dump(packet); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 474 | cb = l->data; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 475 | /* TODO: Check for cb != NULL. */ |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 476 | cb(dev, packet); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 477 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 478 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 479 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 482 | /** |
| 483 | * TODO. |
| 484 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 485 | * TODO: More error checks etc. |
| 486 | * |
| 487 | * @param fd TODO. |
| 488 | * @param events TODO. |
| 489 | * @param timeout TODO. |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 490 | * @param cb Callback function to add. Must not be NULL. |
| 491 | * @param cb_data Data for the callback function. Can be NULL. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 492 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 493 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 494 | * SR_ERR_MALLOC upon memory allocation errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 495 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 496 | 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] | 497 | sr_receive_data_callback_t cb, void *cb_data) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 498 | { |
| 499 | struct source *new_sources, *s; |
| 500 | |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 501 | if (!cb) { |
| 502 | sr_err("session: %s: cb was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 503 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 506 | /* Note: cb_data can be NULL, that's not a bug. */ |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 507 | |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame^] | 508 | new_sources = g_try_realloc(sources, sizeof(struct source) * (num_sources + 1)); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 509 | if (!new_sources) { |
| 510 | sr_err("session: %s: new_sources malloc failed", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 511 | return SR_ERR_MALLOC; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 512 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 513 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 514 | s = &new_sources[num_sources++]; |
| 515 | s->fd = fd; |
| 516 | s->events = events; |
| 517 | s->timeout = timeout; |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 518 | s->cb = cb; |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 519 | s->cb_data = cb_data; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 520 | sources = new_sources; |
| 521 | |
| 522 | if (timeout != source_timeout && timeout > 0 |
| 523 | && (source_timeout == -1 || timeout < source_timeout)) |
| 524 | source_timeout = timeout; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 525 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 526 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 529 | /** |
| 530 | * Remove the source belonging to the specified file descriptor. |
| 531 | * |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 532 | * TODO: More error checks. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 533 | * |
| 534 | * @param fd TODO. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 535 | * |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 536 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 537 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon |
| 538 | * internal errors. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 539 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 540 | SR_API int sr_session_source_remove(int fd) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 541 | { |
| 542 | struct source *new_sources; |
| 543 | int old, new; |
| 544 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 545 | if (!sources) { |
| 546 | sr_err("session: %s: sources was NULL", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 547 | return SR_ERR_BUG; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame^] | 550 | for (old = 0; old < num_sources; old++) { |
| 551 | if (sources[old].fd == fd) |
| 552 | break; |
| 553 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 554 | |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame^] | 555 | /* fd not found, nothing to do */ |
| 556 | if (old == num_sources) |
| 557 | return SR_OK; |
| 558 | |
| 559 | num_sources -= 1; |
| 560 | |
| 561 | if (old != num_sources) { |
| 562 | memmove(&sources[old], &sources[old+1], |
| 563 | (num_sources - old) * sizeof(struct source)); |
| 564 | } |
| 565 | |
| 566 | new_sources = g_try_realloc(sources, sizeof(struct source) * (num_sources - 1)); |
| 567 | if (!new_sources && num_sources > 0) { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 568 | sr_err("session: %s: new_sources malloc failed", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 569 | return SR_ERR_MALLOC; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame^] | 572 | sources = new_sources; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 573 | |
| 574 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 575 | } |