Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 1 | /* |
Uwe Hermann | 50985c2 | 2013-04-23 22:24:30 +0200 | [diff] [blame] | 2 | * This file is part of the libsigrok project. |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 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 | |
Martin Ling | 3544f84 | 2013-12-23 03:38:35 +0000 | [diff] [blame^] | 28 | #define LOG_PREFIX "session" |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 29 | |
Uwe Hermann | 7b870c3 | 2012-10-21 16:13:36 +0200 | [diff] [blame] | 30 | /** |
Uwe Hermann | 393fb9c | 2012-10-22 00:30:12 +0200 | [diff] [blame] | 31 | * @file |
| 32 | * |
| 33 | * Creating, using, or destroying libsigrok sessions. |
| 34 | */ |
| 35 | |
| 36 | /** |
Uwe Hermann | 7b870c3 | 2012-10-21 16:13:36 +0200 | [diff] [blame] | 37 | * @defgroup grp_session Session handling |
| 38 | * |
| 39 | * Creating, using, or destroying libsigrok sessions. |
| 40 | * |
| 41 | * @{ |
| 42 | */ |
| 43 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 44 | struct source { |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 45 | int timeout; |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 46 | sr_receive_data_callback_t cb; |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 47 | void *cb_data; |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 48 | |
| 49 | /* This is used to keep track of the object (fd, pollfd or channel) which is |
| 50 | * being polled and will be used to match the source when removing it again. |
| 51 | */ |
| 52 | gintptr poll_object; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Martin Ling | 2726474 | 2013-04-15 21:08:55 +0100 | [diff] [blame] | 55 | struct datafeed_callback { |
| 56 | sr_datafeed_callback_t cb; |
| 57 | void *cb_data; |
| 58 | }; |
| 59 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 60 | /* There can only be one session at a time. */ |
Uwe Hermann | a0ecd83 | 2011-12-28 22:55:21 +0100 | [diff] [blame] | 61 | /* 'session' is not static, it's used elsewhere (via 'extern'). */ |
Uwe Hermann | 2872d21 | 2011-02-08 17:50:29 +0100 | [diff] [blame] | 62 | struct sr_session *session; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 63 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 64 | /** |
| 65 | * Create a new session. |
| 66 | * |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 67 | * @todo Should it use the file-global "session" variable or take an argument? |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 68 | * The same question applies to all the other session functions. |
| 69 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 70 | * @retval NULL Error. |
Bert Vermeulen | 98582bf | 2013-12-09 22:49:12 +0100 | [diff] [blame] | 71 | * @retval other A pointer to the newly allocated session. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 72 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 73 | SR_API struct sr_session *sr_session_new(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 74 | { |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 75 | if (!(session = g_try_malloc0(sizeof(struct sr_session)))) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 76 | sr_err("Session malloc failed."); |
| 77 | return NULL; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 78 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 79 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 80 | session->source_timeout = -1; |
Bert Vermeulen | 5451816 | 2013-09-21 17:44:49 +0200 | [diff] [blame] | 81 | session->running = FALSE; |
Alexandru Gagniuc | 33c6e4c | 2013-02-02 00:50:00 -0600 | [diff] [blame] | 82 | session->abort_session = FALSE; |
| 83 | g_mutex_init(&session->stop_mutex); |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 84 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 85 | return session; |
| 86 | } |
| 87 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 88 | /** |
| 89 | * Destroy the current session. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 90 | * This frees up all memory used by the session. |
| 91 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 92 | * @retval SR_OK Success. |
| 93 | * @retval SR_ERR_BUG No session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 94 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 95 | SR_API int sr_session_destroy(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 96 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 97 | if (!session) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 98 | sr_err("%s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 99 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Lars-Peter Clausen | ed229aa | 2012-07-05 21:15:07 +0200 | [diff] [blame] | 102 | sr_session_dev_remove_all(); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 103 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 104 | /* TODO: Error checks needed? */ |
| 105 | |
Alexandru Gagniuc | 33c6e4c | 2013-02-02 00:50:00 -0600 | [diff] [blame] | 106 | g_mutex_clear(&session->stop_mutex); |
| 107 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 108 | g_free(session); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 109 | session = NULL; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 110 | |
| 111 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Uwe Hermann | 961009b | 2013-02-01 22:58:54 +0100 | [diff] [blame] | 114 | /** |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 115 | * Remove all the devices from the current session. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 116 | * |
| 117 | * The session itself (i.e., the struct sr_session) is not free'd and still |
| 118 | * exists after this function returns. |
| 119 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 120 | * @retval SR_OK Success. |
| 121 | * @retval SR_ERR_BUG No session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 122 | */ |
Uwe Hermann | 01c3e9d | 2012-03-28 21:55:48 +0200 | [diff] [blame] | 123 | SR_API int sr_session_dev_remove_all(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 124 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 125 | if (!session) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 126 | sr_err("%s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 127 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Bert Vermeulen | 681803d | 2013-04-28 22:35:42 +0200 | [diff] [blame] | 130 | g_slist_free(session->devs); |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 131 | session->devs = NULL; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 132 | |
| 133 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 136 | /** |
Uwe Hermann | 9c5332d | 2012-10-21 16:52:56 +0200 | [diff] [blame] | 137 | * Add a device instance to the current session. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 138 | * |
Uwe Hermann | 9c5332d | 2012-10-21 16:52:56 +0200 | [diff] [blame] | 139 | * @param sdi The device instance to add to the current session. Must not |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 140 | * be NULL. Also, sdi->driver and sdi->driver->dev_open must |
| 141 | * not be NULL. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 142 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 143 | * @retval SR_OK Success. |
| 144 | * @retval SR_ERR_ARG Invalid argument. |
| 145 | * @retval SR_ERR_BUG No session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 146 | */ |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 147 | SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 148 | { |
Bert Vermeulen | 5451816 | 2013-09-21 17:44:49 +0200 | [diff] [blame] | 149 | int ret; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 150 | |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 151 | if (!sdi) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 152 | sr_err("%s: sdi was NULL", __func__); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 153 | return SR_ERR_ARG; |
| 154 | } |
| 155 | |
Uwe Hermann | d6eb0c3 | 2012-03-18 12:57:34 +0100 | [diff] [blame] | 156 | if (!session) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 157 | sr_err("%s: session was NULL", __func__); |
Uwe Hermann | d6eb0c3 | 2012-03-18 12:57:34 +0100 | [diff] [blame] | 158 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 161 | /* If sdi->driver is NULL, this is a virtual device. */ |
| 162 | if (!sdi->driver) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 163 | sr_dbg("%s: sdi->driver was NULL, this seems to be " |
Uwe Hermann | d6eb0c3 | 2012-03-18 12:57:34 +0100 | [diff] [blame] | 164 | "a virtual device; continuing", __func__); |
| 165 | /* Just add the device, don't run dev_open(). */ |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 166 | session->devs = g_slist_append(session->devs, (gpointer)sdi); |
Uwe Hermann | d6eb0c3 | 2012-03-18 12:57:34 +0100 | [diff] [blame] | 167 | return SR_OK; |
| 168 | } |
| 169 | |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 170 | /* sdi->driver is non-NULL (i.e. we have a real device). */ |
| 171 | if (!sdi->driver->dev_open) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 172 | sr_err("%s: sdi->driver->dev_open was NULL", __func__); |
Uwe Hermann | 8ec95d2 | 2012-03-21 19:28:43 +0100 | [diff] [blame] | 173 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 176 | session->devs = g_slist_append(session->devs, (gpointer)sdi); |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 177 | |
Bert Vermeulen | 5451816 | 2013-09-21 17:44:49 +0200 | [diff] [blame] | 178 | if (session->running) { |
| 179 | /* Adding a device to a running session. Start acquisition |
| 180 | * on that device now. */ |
| 181 | if ((ret = sdi->driver->dev_acquisition_start(sdi, |
| 182 | (void *)sdi)) != SR_OK) |
| 183 | sr_err("Failed to start acquisition of device in " |
| 184 | "running session: %d", ret); |
| 185 | } |
| 186 | |
Uwe Hermann | e46b8fb | 2011-01-29 16:23:12 +0100 | [diff] [blame] | 187 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 190 | /** |
Bert Vermeulen | 2bb311b | 2013-09-02 14:24:32 +0200 | [diff] [blame] | 191 | * List all device instances attached to the current session. |
| 192 | * |
| 193 | * @param devlist A pointer where the device instance list will be |
| 194 | * stored on return. If no devices are in the session, |
| 195 | * this will be NULL. Each element in the list points |
| 196 | * to a struct sr_dev_inst *. |
| 197 | * The list must be freed by the caller, but not the |
| 198 | * elements pointed to. |
| 199 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 200 | * @retval SR_OK Success. |
| 201 | * @retval SR_ERR Invalid argument. |
Bert Vermeulen | 2bb311b | 2013-09-02 14:24:32 +0200 | [diff] [blame] | 202 | */ |
| 203 | SR_API int sr_session_dev_list(GSList **devlist) |
| 204 | { |
| 205 | |
| 206 | *devlist = NULL; |
| 207 | |
| 208 | if (!session) |
| 209 | return SR_ERR; |
| 210 | |
| 211 | *devlist = g_slist_copy(session->devs); |
| 212 | |
| 213 | return SR_OK; |
| 214 | } |
| 215 | |
| 216 | /** |
Uwe Hermann | 01c3e9d | 2012-03-28 21:55:48 +0200 | [diff] [blame] | 217 | * Remove all datafeed callbacks in the current session. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 218 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 219 | * @retval SR_OK Success. |
| 220 | * @retval SR_ERR_BUG No session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 221 | */ |
Uwe Hermann | 01c3e9d | 2012-03-28 21:55:48 +0200 | [diff] [blame] | 222 | SR_API int sr_session_datafeed_callback_remove_all(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 223 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 224 | if (!session) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 225 | sr_err("%s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 226 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Martin Ling | 2726474 | 2013-04-15 21:08:55 +0100 | [diff] [blame] | 229 | g_slist_free_full(session->datafeed_callbacks, g_free); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 230 | session->datafeed_callbacks = NULL; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 231 | |
| 232 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 235 | /** |
| 236 | * Add a datafeed callback to the current session. |
| 237 | * |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 238 | * @param cb Function to call when a chunk of data is received. |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 239 | * Must not be NULL. |
Uwe Hermann | 8522279 | 2013-04-16 12:48:58 +0200 | [diff] [blame] | 240 | * @param cb_data Opaque pointer passed in by the caller. |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 241 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 242 | * @retval SR_OK Success. |
| 243 | * @retval SR_ERR_BUG No session exists. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 244 | */ |
Martin Ling | 2726474 | 2013-04-15 21:08:55 +0100 | [diff] [blame] | 245 | SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb, void *cb_data) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 246 | { |
Martin Ling | 2726474 | 2013-04-15 21:08:55 +0100 | [diff] [blame] | 247 | struct datafeed_callback *cb_struct; |
| 248 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 249 | if (!session) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 250 | sr_err("%s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 251 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 254 | if (!cb) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 255 | sr_err("%s: cb was NULL", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 256 | return SR_ERR_ARG; |
| 257 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 258 | |
Martin Ling | 2726474 | 2013-04-15 21:08:55 +0100 | [diff] [blame] | 259 | if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback)))) |
| 260 | return SR_ERR_MALLOC; |
| 261 | |
| 262 | cb_struct->cb = cb; |
| 263 | cb_struct->cb_data = cb_data; |
| 264 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 265 | session->datafeed_callbacks = |
Martin Ling | 2726474 | 2013-04-15 21:08:55 +0100 | [diff] [blame] | 266 | g_slist_append(session->datafeed_callbacks, cb_struct); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 267 | |
| 268 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Bert Vermeulen | b483be7 | 2013-07-15 14:14:28 +0200 | [diff] [blame] | 271 | /** |
| 272 | * Call every device in the session's callback. |
| 273 | * |
| 274 | * For sessions not driven by select loops such as sr_session_run(), |
| 275 | * but driven by another scheduler, this can be used to poll the devices |
| 276 | * from within that scheduler. |
| 277 | * |
Bert Vermeulen | f6eb2cb | 2013-07-18 15:06:06 +0200 | [diff] [blame] | 278 | * @param block If TRUE, this call will wait for any of the session's |
| 279 | * sources to fire an event on the file descriptors, or |
| 280 | * any of their timeouts to activate. In other words, this |
| 281 | * can be used as a select loop. |
| 282 | * If FALSE, all sources have their callback run, regardless |
| 283 | * of file descriptor or timeout status. |
| 284 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 285 | * @retval SR_OK Success. |
| 286 | * @retval SR_ERR Error occured. |
Bert Vermeulen | b483be7 | 2013-07-15 14:14:28 +0200 | [diff] [blame] | 287 | */ |
Uwe Hermann | 1861be0 | 2013-08-07 00:34:06 +0200 | [diff] [blame] | 288 | static int sr_session_iteration(gboolean block) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 289 | { |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 290 | unsigned int i; |
| 291 | int ret; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 292 | |
Bert Vermeulen | b483be7 | 2013-07-15 14:14:28 +0200 | [diff] [blame] | 293 | ret = g_poll(session->pollfds, session->num_sources, |
| 294 | block ? session->source_timeout : 0); |
| 295 | for (i = 0; i < session->num_sources; i++) { |
| 296 | if (session->pollfds[i].revents > 0 || (ret == 0 |
| 297 | && session->source_timeout == session->sources[i].timeout)) { |
Alexandru Gagniuc | 33c6e4c | 2013-02-02 00:50:00 -0600 | [diff] [blame] | 298 | /* |
Bert Vermeulen | b483be7 | 2013-07-15 14:14:28 +0200 | [diff] [blame] | 299 | * Invoke the source's callback on an event, |
| 300 | * or if the poll timed out and this source |
| 301 | * asked for that timeout. |
Alexandru Gagniuc | 33c6e4c | 2013-02-02 00:50:00 -0600 | [diff] [blame] | 302 | */ |
Bert Vermeulen | b483be7 | 2013-07-15 14:14:28 +0200 | [diff] [blame] | 303 | if (!session->sources[i].cb(session->pollfds[i].fd, |
| 304 | session->pollfds[i].revents, |
| 305 | session->sources[i].cb_data)) |
| 306 | sr_session_source_remove(session->sources[i].poll_object); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 307 | } |
Bert Vermeulen | b483be7 | 2013-07-15 14:14:28 +0200 | [diff] [blame] | 308 | /* |
| 309 | * We want to take as little time as possible to stop |
| 310 | * the session if we have been told to do so. Therefore, |
| 311 | * we check the flag after processing every source, not |
| 312 | * just once per main event loop. |
| 313 | */ |
| 314 | g_mutex_lock(&session->stop_mutex); |
| 315 | if (session->abort_session) { |
| 316 | sr_session_stop_sync(); |
| 317 | /* But once is enough. */ |
| 318 | session->abort_session = FALSE; |
| 319 | } |
| 320 | g_mutex_unlock(&session->stop_mutex); |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 321 | } |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 322 | |
| 323 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 326 | /** |
| 327 | * Start a session. |
| 328 | * |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 329 | * There can only be one session at a time. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 330 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 331 | * @retval SR_OK Success. |
| 332 | * @retval SR_ERR Error occured. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 333 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 334 | SR_API int sr_session_start(void) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 335 | { |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 336 | struct sr_dev_inst *sdi; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 337 | GSList *l; |
| 338 | int ret; |
| 339 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 340 | if (!session) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 341 | sr_err("%s: session was NULL; a session must be " |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 342 | "created before starting it.", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 343 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 346 | if (!session->devs) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 347 | sr_err("%s: session->devs was NULL; a session " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 348 | "cannot be started without devices.", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 349 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Uwe Hermann | c714260 | 2013-02-07 09:16:28 +0100 | [diff] [blame] | 352 | sr_info("Starting."); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 353 | |
Uwe Hermann | b7c3e84 | 2012-12-31 21:05:11 +0100 | [diff] [blame] | 354 | ret = SR_OK; |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 355 | for (l = session->devs; l; l = l->next) { |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 356 | sdi = l->data; |
| 357 | if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 358 | sr_err("%s: could not start an acquisition " |
Bert Vermeulen | 568dcac | 2013-10-16 10:40:38 +0200 | [diff] [blame] | 359 | "(%s)", __func__, sr_strerror(ret)); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 360 | break; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 361 | } |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 364 | /* TODO: What if there are multiple devices? Which return code? */ |
| 365 | |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 366 | return ret; |
| 367 | } |
| 368 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 369 | /** |
| 370 | * Run the session. |
| 371 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 372 | * @retval SR_OK Success. |
| 373 | * @retval SR_ERR_BUG Error occured. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 374 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 375 | SR_API int sr_session_run(void) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 376 | { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 377 | if (!session) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 378 | sr_err("%s: session was NULL; a session must be " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 379 | "created first, before running it.", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 380 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 383 | if (!session->devs) { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 384 | /* TODO: Actually the case? */ |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 385 | sr_err("%s: session->devs was NULL; a session " |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 386 | "cannot be run without devices.", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 387 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 388 | } |
Bert Vermeulen | 5451816 | 2013-09-21 17:44:49 +0200 | [diff] [blame] | 389 | session->running = TRUE; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 390 | |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 391 | sr_info("Running."); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 392 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 393 | /* Do we have real sources? */ |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 394 | if (session->num_sources == 1 && session->pollfds[0].fd == -1) { |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 395 | /* Dummy source, freewheel over it. */ |
Bert Vermeulen | 2cbeb2b | 2012-08-03 01:04:05 +0200 | [diff] [blame] | 396 | while (session->num_sources) |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 397 | session->sources[0].cb(-1, 0, session->sources[0].cb_data); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 398 | } else { |
| 399 | /* Real sources, use g_poll() main loop. */ |
Bert Vermeulen | b483be7 | 2013-07-15 14:14:28 +0200 | [diff] [blame] | 400 | while (session->num_sources) |
| 401 | sr_session_iteration(TRUE); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 404 | return SR_OK; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 407 | /** |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 408 | * Stop the current session. |
| 409 | * |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 410 | * The current session is stopped immediately, with all acquisition sessions |
Uwe Hermann | c09f0b5 | 2012-02-28 23:52:30 +0100 | [diff] [blame] | 411 | * being stopped and hardware drivers cleaned up. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 412 | * |
Alexandru Gagniuc | 33c6e4c | 2013-02-02 00:50:00 -0600 | [diff] [blame] | 413 | * This must be called from within the session thread, to prevent freeing |
| 414 | * resources that the session thread will try to use. |
| 415 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 416 | * @retval SR_OK Success. |
| 417 | * @retval SR_ERR_BUG No session exists. |
Bert Vermeulen | 72a08bc | 2013-09-25 11:51:38 +0200 | [diff] [blame] | 418 | * |
| 419 | * @private |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 420 | */ |
Alexandru Gagniuc | 33c6e4c | 2013-02-02 00:50:00 -0600 | [diff] [blame] | 421 | SR_PRIV int sr_session_stop_sync(void) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 422 | { |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 423 | struct sr_dev_inst *sdi; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 424 | GSList *l; |
| 425 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 426 | if (!session) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 427 | sr_err("%s: session was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 428 | return SR_ERR_BUG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 431 | sr_info("Stopping."); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 432 | |
Uwe Hermann | bb7ef79 | 2012-02-17 22:25:01 +0100 | [diff] [blame] | 433 | for (l = session->devs; l; l = l->next) { |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 434 | sdi = l->data; |
| 435 | if (sdi->driver) { |
| 436 | if (sdi->driver->dev_acquisition_stop) |
| 437 | sdi->driver->dev_acquisition_stop(sdi, sdi); |
Bert Vermeulen | 8c76be5 | 2012-01-08 22:05:00 +0100 | [diff] [blame] | 438 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 439 | } |
Bert Vermeulen | 5451816 | 2013-09-21 17:44:49 +0200 | [diff] [blame] | 440 | session->running = FALSE; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 441 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 442 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 445 | /** |
Alexandru Gagniuc | 33c6e4c | 2013-02-02 00:50:00 -0600 | [diff] [blame] | 446 | * Stop the current session. |
| 447 | * |
| 448 | * The current session is stopped immediately, with all acquisition sessions |
| 449 | * being stopped and hardware drivers cleaned up. |
| 450 | * |
| 451 | * If the session is run in a separate thread, this function will not block |
| 452 | * until the session is finished executing. It is the caller's responsibility |
| 453 | * to wait for the session thread to return before assuming that the session is |
| 454 | * completely decommissioned. |
| 455 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 456 | * @retval SR_OK Success. |
| 457 | * @retval SR_ERR_BUG No session exists. |
Alexandru Gagniuc | 33c6e4c | 2013-02-02 00:50:00 -0600 | [diff] [blame] | 458 | */ |
| 459 | SR_API int sr_session_stop(void) |
| 460 | { |
| 461 | if (!session) { |
| 462 | sr_err("%s: session was NULL", __func__); |
| 463 | return SR_ERR_BUG; |
| 464 | } |
| 465 | |
| 466 | g_mutex_lock(&session->stop_mutex); |
| 467 | session->abort_session = TRUE; |
| 468 | g_mutex_unlock(&session->stop_mutex); |
| 469 | |
| 470 | return SR_OK; |
| 471 | } |
| 472 | |
| 473 | /** |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 474 | * Debug helper. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 475 | * |
Bert Vermeulen | 996b0c7 | 2012-02-13 02:13:51 +0100 | [diff] [blame] | 476 | * @param packet The packet to show debugging information for. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 477 | */ |
Joel Holdsworth | bf53457 | 2012-12-13 21:07:53 +0000 | [diff] [blame] | 478 | static void datafeed_dump(const struct sr_datafeed_packet *packet) |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 479 | { |
Joel Holdsworth | bf53457 | 2012-12-13 21:07:53 +0000 | [diff] [blame] | 480 | const struct sr_datafeed_logic *logic; |
| 481 | const struct sr_datafeed_analog *analog; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 482 | |
| 483 | switch (packet->type) { |
| 484 | case SR_DF_HEADER: |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 485 | sr_dbg("bus: Received SR_DF_HEADER packet."); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 486 | break; |
| 487 | case SR_DF_TRIGGER: |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 488 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 489 | break; |
Bert Vermeulen | c71bac3 | 2013-01-20 16:37:23 +0100 | [diff] [blame] | 490 | case SR_DF_META: |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 491 | sr_dbg("bus: Received SR_DF_META packet."); |
Bert Vermeulen | ee7489d | 2012-04-22 18:11:31 +0200 | [diff] [blame] | 492 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 493 | case SR_DF_LOGIC: |
| 494 | logic = packet->payload; |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 495 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes).", |
| 496 | logic->length); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 497 | break; |
Bert Vermeulen | ee7489d | 2012-04-22 18:11:31 +0200 | [diff] [blame] | 498 | case SR_DF_ANALOG: |
| 499 | analog = packet->payload; |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 500 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", |
| 501 | analog->num_samples); |
Bert Vermeulen | ee7489d | 2012-04-22 18:11:31 +0200 | [diff] [blame] | 502 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 503 | case SR_DF_END: |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 504 | sr_dbg("bus: Received SR_DF_END packet."); |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 505 | break; |
Bert Vermeulen | 6ea7669 | 2012-04-30 19:55:06 +0200 | [diff] [blame] | 506 | case SR_DF_FRAME_BEGIN: |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 507 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); |
Bert Vermeulen | 6ea7669 | 2012-04-30 19:55:06 +0200 | [diff] [blame] | 508 | break; |
| 509 | case SR_DF_FRAME_END: |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 510 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); |
Bert Vermeulen | 6ea7669 | 2012-04-30 19:55:06 +0200 | [diff] [blame] | 511 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 512 | default: |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 513 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 514 | break; |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 515 | } |
Bert Vermeulen | 7d2afd6 | 2011-06-20 11:42:43 +0200 | [diff] [blame] | 516 | } |
| 517 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 518 | /** |
Bert Vermeulen | a1645fc | 2012-02-13 03:36:32 +0100 | [diff] [blame] | 519 | * Send a packet to whatever is listening on the datafeed bus. |
| 520 | * |
| 521 | * Hardware drivers use this to send a data packet to the frontend. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 522 | * |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 523 | * @param sdi TODO. |
Uwe Hermann | 31ccebc | 2012-02-29 22:08:45 +0100 | [diff] [blame] | 524 | * @param packet The datafeed packet to send to the session bus. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 525 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 526 | * @retval SR_OK Success. |
| 527 | * @retval SR_ERR_ARG Invalid argument. |
Uwe Hermann | b4bd708 | 2012-10-19 10:07:22 +0200 | [diff] [blame] | 528 | * |
| 529 | * @private |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 530 | */ |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 531 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
Joel Holdsworth | bf53457 | 2012-12-13 21:07:53 +0000 | [diff] [blame] | 532 | const struct sr_datafeed_packet *packet) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 533 | { |
| 534 | GSList *l; |
Martin Ling | 2726474 | 2013-04-15 21:08:55 +0100 | [diff] [blame] | 535 | struct datafeed_callback *cb_struct; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 536 | |
Bert Vermeulen | de4d3f9 | 2012-07-22 15:31:56 +0200 | [diff] [blame] | 537 | if (!sdi) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 538 | sr_err("%s: sdi was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 539 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 542 | if (!packet) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 543 | sr_err("%s: packet was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 544 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 547 | for (l = session->datafeed_callbacks; l; l = l->next) { |
Bert Vermeulen | 18beaef | 2012-02-13 00:08:23 +0100 | [diff] [blame] | 548 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
| 549 | datafeed_dump(packet); |
Martin Ling | 2726474 | 2013-04-15 21:08:55 +0100 | [diff] [blame] | 550 | cb_struct = l->data; |
| 551 | cb_struct->cb(sdi, packet, cb_struct->cb_data); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 552 | } |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 553 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 554 | return SR_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 557 | /** |
| 558 | * Add an event source for a file descriptor. |
| 559 | * |
| 560 | * @param pollfd The GPollFD. |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 561 | * @param[in] timeout Max time to wait before the callback is called, |
| 562 | * ignored if 0. |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 563 | * @param cb Callback function to add. Must not be NULL. |
| 564 | * @param cb_data Data for the callback function. Can be NULL. |
| 565 | * @param poll_object TODO. |
| 566 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 567 | * @retval SR_OK Success. |
| 568 | * @retval SR_ERR_ARG Invalid argument. |
| 569 | * @retval SR_ERR_MALLOC Memory allocation error. |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 570 | */ |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 571 | static int _sr_session_source_add(GPollFD *pollfd, int timeout, |
Uwe Hermann | 1a895c6 | 2012-07-05 01:47:44 +0200 | [diff] [blame] | 572 | sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 573 | { |
| 574 | struct source *new_sources, *s; |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 575 | GPollFD *new_pollfds; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 576 | |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 577 | if (!cb) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 578 | sr_err("%s: cb was NULL", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 579 | return SR_ERR_ARG; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 582 | /* Note: cb_data can be NULL, that's not a bug. */ |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 583 | |
Bert Vermeulen | 2ac2e62 | 2012-07-22 15:32:35 +0200 | [diff] [blame] | 584 | new_pollfds = g_try_realloc(session->pollfds, |
| 585 | sizeof(GPollFD) * (session->num_sources + 1)); |
Lars-Peter Clausen | 0687dfc | 2012-06-30 20:54:43 +0200 | [diff] [blame] | 586 | if (!new_pollfds) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 587 | sr_err("%s: new_pollfds malloc failed", __func__); |
Lars-Peter Clausen | 0687dfc | 2012-06-30 20:54:43 +0200 | [diff] [blame] | 588 | return SR_ERR_MALLOC; |
| 589 | } |
| 590 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 591 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * |
Bert Vermeulen | 2ac2e62 | 2012-07-22 15:32:35 +0200 | [diff] [blame] | 592 | (session->num_sources + 1)); |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 593 | if (!new_sources) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 594 | sr_err("%s: new_sources malloc failed", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 595 | return SR_ERR_MALLOC; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 596 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 597 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 598 | new_pollfds[session->num_sources] = *pollfd; |
| 599 | s = &new_sources[session->num_sources++]; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 600 | s->timeout = timeout; |
Uwe Hermann | d08490a | 2012-02-29 21:56:24 +0100 | [diff] [blame] | 601 | s->cb = cb; |
Uwe Hermann | 1f9813e | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 602 | s->cb_data = cb_data; |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 603 | s->poll_object = poll_object; |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 604 | session->pollfds = new_pollfds; |
| 605 | session->sources = new_sources; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 606 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 607 | if (timeout != session->source_timeout && timeout > 0 |
| 608 | && (session->source_timeout == -1 || timeout < session->source_timeout)) |
| 609 | session->source_timeout = timeout; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 610 | |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 611 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 614 | /** |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 615 | * Add an event source for a file descriptor. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 616 | * |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 617 | * @param fd The file descriptor. |
| 618 | * @param events Events to check for. |
| 619 | * @param timeout Max time to wait before the callback is called, ignored if 0. |
| 620 | * @param cb Callback function to add. Must not be NULL. |
| 621 | * @param cb_data Data for the callback function. Can be NULL. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 622 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 623 | * @retval SR_OK Success. |
| 624 | * @retval SR_ERR_ARG Invalid argument. |
| 625 | * @retval SR_ERR_MALLOC Memory allocation error. |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 626 | */ |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 627 | SR_API int sr_session_source_add(int fd, int events, int timeout, |
| 628 | sr_receive_data_callback_t cb, void *cb_data) |
| 629 | { |
| 630 | GPollFD p; |
| 631 | |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 632 | p.fd = fd; |
| 633 | p.events = events; |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 634 | |
| 635 | return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd); |
| 636 | } |
| 637 | |
| 638 | /** |
Uwe Hermann | 1a895c6 | 2012-07-05 01:47:44 +0200 | [diff] [blame] | 639 | * Add an event source for a GPollFD. |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 640 | * |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 641 | * @param pollfd The GPollFD. |
| 642 | * @param timeout Max time to wait before the callback is called, ignored if 0. |
| 643 | * @param cb Callback function to add. Must not be NULL. |
| 644 | * @param cb_data Data for the callback function. Can be NULL. |
| 645 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 646 | * @retval SR_OK Success. |
| 647 | * @retval SR_ERR_ARG Invalid argument. |
| 648 | * @retval SR_ERR_MALLOC Memory allocation error. |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 649 | */ |
| 650 | SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout, |
| 651 | sr_receive_data_callback_t cb, void *cb_data) |
| 652 | { |
Uwe Hermann | 1a895c6 | 2012-07-05 01:47:44 +0200 | [diff] [blame] | 653 | return _sr_session_source_add(pollfd, timeout, cb, |
| 654 | cb_data, (gintptr)pollfd); |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /** |
Uwe Hermann | 1a895c6 | 2012-07-05 01:47:44 +0200 | [diff] [blame] | 658 | * Add an event source for a GIOChannel. |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 659 | * |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 660 | * @param channel The GIOChannel. |
| 661 | * @param events Events to poll on. |
| 662 | * @param timeout Max time to wait before the callback is called, ignored if 0. |
| 663 | * @param cb Callback function to add. Must not be NULL. |
| 664 | * @param cb_data Data for the callback function. Can be NULL. |
| 665 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 666 | * @retval SR_OK Success. |
| 667 | * @retval SR_ERR_ARG Invalid argument. |
| 668 | * @retval SR_ERR_MALLOC Memory allocation error. |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 669 | */ |
Uwe Hermann | 1a895c6 | 2012-07-05 01:47:44 +0200 | [diff] [blame] | 670 | SR_API int sr_session_source_add_channel(GIOChannel *channel, int events, |
| 671 | int timeout, sr_receive_data_callback_t cb, void *cb_data) |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 672 | { |
| 673 | GPollFD p; |
| 674 | |
| 675 | #ifdef _WIN32 |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 676 | g_io_channel_win32_make_pollfd(channel, events, &p); |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 677 | #else |
| 678 | p.fd = g_io_channel_unix_get_fd(channel); |
| 679 | p.events = events; |
| 680 | #endif |
| 681 | |
| 682 | return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel); |
| 683 | } |
| 684 | |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 685 | /** |
| 686 | * Remove the source belonging to the specified channel. |
| 687 | * |
| 688 | * @todo Add more error checks and logging. |
| 689 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 690 | * @param poll_object The channel for which the source should be removed. |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 691 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 692 | * @retval SR_OK Success |
| 693 | * @retval SR_ERR_ARG Invalid arguments |
| 694 | * @retval SR_ERR_MALLOC Memory allocation error |
| 695 | * @retval SR_ERR_BUG Internal error |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 696 | */ |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 697 | static int _sr_session_source_remove(gintptr poll_object) |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 698 | { |
| 699 | struct source *new_sources; |
Lars-Peter Clausen | 0687dfc | 2012-06-30 20:54:43 +0200 | [diff] [blame] | 700 | GPollFD *new_pollfds; |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 701 | unsigned int old; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 702 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 703 | if (!session->sources || !session->num_sources) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 704 | sr_err("%s: sources was NULL", __func__); |
Uwe Hermann | 0abee50 | 2012-03-04 15:08:11 +0100 | [diff] [blame] | 705 | return SR_ERR_BUG; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 706 | } |
| 707 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 708 | for (old = 0; old < session->num_sources; old++) { |
| 709 | if (session->sources[old].poll_object == poll_object) |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame] | 710 | break; |
| 711 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 712 | |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame] | 713 | /* fd not found, nothing to do */ |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 714 | if (old == session->num_sources) |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame] | 715 | return SR_OK; |
| 716 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 717 | session->num_sources -= 1; |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame] | 718 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 719 | if (old != session->num_sources) { |
| 720 | memmove(&session->pollfds[old], &session->pollfds[old+1], |
| 721 | (session->num_sources - old) * sizeof(GPollFD)); |
| 722 | memmove(&session->sources[old], &session->sources[old+1], |
| 723 | (session->num_sources - old) * sizeof(struct source)); |
Lars-Peter Clausen | 2bccd32 | 2012-06-30 20:54:42 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 726 | new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); |
| 727 | if (!new_pollfds && session->num_sources > 0) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 728 | sr_err("%s: new_pollfds malloc failed", __func__); |
Lars-Peter Clausen | 0687dfc | 2012-06-30 20:54:43 +0200 | [diff] [blame] | 729 | return SR_ERR_MALLOC; |
| 730 | } |
| 731 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 732 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources); |
| 733 | if (!new_sources && session->num_sources > 0) { |
Uwe Hermann | a421dc1 | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 734 | sr_err("%s: new_sources malloc failed", __func__); |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 735 | return SR_ERR_MALLOC; |
Uwe Hermann | 9f45fb3 | 2012-01-02 14:15:25 +0100 | [diff] [blame] | 736 | } |
| 737 | |
Lars-Peter Clausen | b7e9411 | 2012-07-06 23:23:31 +0200 | [diff] [blame] | 738 | session->pollfds = new_pollfds; |
| 739 | session->sources = new_sources; |
Uwe Hermann | e0508e6 | 2012-01-07 17:08:54 +0100 | [diff] [blame] | 740 | |
| 741 | return SR_OK; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame] | 742 | } |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 743 | |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 744 | /** |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 745 | * Remove the source belonging to the specified file descriptor. |
| 746 | * |
Uwe Hermann | 1a895c6 | 2012-07-05 01:47:44 +0200 | [diff] [blame] | 747 | * @param fd The file descriptor for which the source should be removed. |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 748 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 749 | * @retval SR_OK Success |
| 750 | * @retval SR_ERR_ARG Invalid argument |
| 751 | * @retval SR_ERR_MALLOC Memory allocation error. |
| 752 | * @retval SR_ERR_BUG Internal error. |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 753 | */ |
| 754 | SR_API int sr_session_source_remove(int fd) |
| 755 | { |
| 756 | return _sr_session_source_remove((gintptr)fd); |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * Remove the source belonging to the specified poll descriptor. |
| 761 | * |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 762 | * @param pollfd The poll descriptor for which the source should be removed. |
| 763 | * |
| 764 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 765 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon |
| 766 | * internal errors. |
| 767 | */ |
| 768 | SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd) |
| 769 | { |
| 770 | return _sr_session_source_remove((gintptr)pollfd); |
| 771 | } |
| 772 | |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 773 | /** |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 774 | * Remove the source belonging to the specified channel. |
| 775 | * |
Uwe Hermann | 1a895c6 | 2012-07-05 01:47:44 +0200 | [diff] [blame] | 776 | * @param channel The channel for which the source should be removed. |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 777 | * |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 778 | * @retval SR_OK Success. |
| 779 | * @retval SR_ERR_ARG Invalid argument. |
| 780 | * @retval SR_ERR_MALLOC Memory allocation error. |
| 781 | * @return SR_ERR_BUG Internal error. |
Lars-Peter Clausen | aac0ea2 | 2012-06-30 20:54:44 +0200 | [diff] [blame] | 782 | */ |
| 783 | SR_API int sr_session_source_remove_channel(GIOChannel *channel) |
| 784 | { |
| 785 | return _sr_session_source_remove((gintptr)channel); |
| 786 | } |
Uwe Hermann | 7b870c3 | 2012-10-21 16:13:36 +0200 | [diff] [blame] | 787 | |
| 788 | /** @} */ |