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