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