Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the sigrok project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> |
| 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> |
| 24 | #include <zip.h> |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame^] | 25 | #include <glib.h> |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 26 | #include <sigrok.h> |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 27 | #include <config.h> |
| 28 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 29 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 30 | /* There can only be one session at a time. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 31 | struct session *session; |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame^] | 32 | int num_sources = 0; |
| 33 | /* These live in hwplugin.c, for the frontend to override. */ |
| 34 | extern source_callback_add source_cb_add; |
| 35 | extern source_callback_remove source_cb_remove; |
| 36 | |
| 37 | struct source { |
| 38 | int fd; |
| 39 | int events; |
| 40 | int timeout; |
| 41 | receive_data_callback cb; |
| 42 | void *user_data; |
| 43 | }; |
| 44 | |
| 45 | struct source *sources = NULL; |
| 46 | int source_timeout = -1; |
| 47 | |
| 48 | |
| 49 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 50 | |
Uwe Hermann | afc8e4d | 2010-04-09 22:18:46 +0200 | [diff] [blame] | 51 | struct session *session_load(const char *filename) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 52 | { |
| 53 | struct session *session; |
| 54 | |
Uwe Hermann | 17e1afc | 2011-01-13 23:50:34 +0100 | [diff] [blame] | 55 | /* Avoid compiler warnings. */ |
| 56 | filename = filename; |
| 57 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 58 | /* TODO: Implement. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 59 | session = NULL; |
| 60 | |
| 61 | return session; |
| 62 | } |
| 63 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 64 | struct session *session_new(void) |
| 65 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 66 | session = calloc(1, sizeof(struct session)); |
| 67 | |
| 68 | return session; |
| 69 | } |
| 70 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 71 | void session_destroy(void) |
| 72 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 73 | g_slist_free(session->devices); |
| 74 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 75 | /* TODO: Loop over protocol decoders and free them. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 76 | |
| 77 | g_free(session); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 80 | void session_device_clear(void) |
| 81 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 82 | g_slist_free(session->devices); |
| 83 | session->devices = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 86 | int session_device_add(struct device *device) |
| 87 | { |
| 88 | int ret; |
| 89 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 90 | if (device->plugin && device->plugin->open) { |
| 91 | ret = device->plugin->open(device->plugin_index); |
| 92 | if (ret != SIGROK_OK) |
| 93 | return ret; |
| 94 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 95 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 96 | session->devices = g_slist_append(session->devices, device); |
| 97 | |
| 98 | return SIGROK_OK; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 101 | void session_pa_clear(void) |
| 102 | { |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 103 | /* |
| 104 | * The protocols are pointers to the global set of PA plugins, |
| 105 | * so don't free them. |
| 106 | */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 107 | g_slist_free(session->analyzers); |
| 108 | session->analyzers = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 111 | void session_pa_add(struct analyzer *an) |
| 112 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 113 | session->analyzers = g_slist_append(session->analyzers, an); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 116 | void session_datafeed_callback_clear(void) |
| 117 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 118 | g_slist_free(session->datafeed_callbacks); |
| 119 | session->datafeed_callbacks = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 122 | void session_datafeed_callback_add(datafeed_callback callback) |
| 123 | { |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 124 | session->datafeed_callbacks = |
| 125 | g_slist_append(session->datafeed_callbacks, callback); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 128 | int session_start(void) |
| 129 | { |
| 130 | struct device *device; |
| 131 | GSList *l; |
| 132 | int ret; |
| 133 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame^] | 134 | g_message("starting session"); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 135 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 136 | device = l->data; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 137 | if ((ret = device->plugin->start_acquisition( |
| 138 | device->plugin_index, device)) != SIGROK_OK) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame^] | 145 | void session_run(void) |
| 146 | { |
| 147 | GPollFD *fds, my_gpollfd; |
| 148 | int ret, i; |
| 149 | |
| 150 | g_message("running session"); |
| 151 | session->running = TRUE; |
| 152 | fds = NULL; |
| 153 | while (session->running) { |
| 154 | if (fds) |
| 155 | free(fds); |
| 156 | |
| 157 | /* Construct g_poll()'s array. */ |
| 158 | fds = malloc(sizeof(GPollFD) * num_sources); |
| 159 | for (i = 0; i < num_sources; i++) { |
| 160 | #ifdef _WIN32 |
| 161 | g_io_channel_win32_make_pollfd(&channels[0], |
| 162 | sources[i].events, &my_gpollfd); |
| 163 | #else |
| 164 | my_gpollfd.fd = sources[i].fd; |
| 165 | my_gpollfd.events = sources[i].events; |
| 166 | fds[i] = my_gpollfd; |
| 167 | #endif |
| 168 | } |
| 169 | |
| 170 | ret = g_poll(fds, num_sources, source_timeout); |
| 171 | |
| 172 | for (i = 0; i < num_sources; i++) { |
| 173 | if (fds[i].revents > 0 || (ret == 0 |
| 174 | && source_timeout == sources[i].timeout)) { |
| 175 | /* |
| 176 | * Invoke the source's callback on an event, |
| 177 | * or if the poll timeout out and this source |
| 178 | * asked for that timeout. |
| 179 | */ |
| 180 | sources[i].cb(fds[i].fd, fds[i].revents, |
| 181 | sources[i].user_data); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | free(fds); |
| 186 | |
| 187 | } |
| 188 | |
| 189 | void session_halt(void) |
| 190 | { |
| 191 | |
| 192 | g_message("halting session"); |
| 193 | session->running = FALSE; |
| 194 | |
| 195 | } |
| 196 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 197 | void session_stop(void) |
| 198 | { |
| 199 | struct device *device; |
| 200 | GSList *l; |
| 201 | |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame^] | 202 | g_message("stopping session"); |
| 203 | session->running = FALSE; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 204 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 205 | device = l->data; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 206 | if (device->plugin) |
| 207 | device->plugin->stop_acquisition(device->plugin_index, device); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 208 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame^] | 209 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 212 | void session_bus(struct device *device, struct datafeed_packet *packet) |
| 213 | { |
| 214 | GSList *l; |
| 215 | datafeed_callback cb; |
| 216 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 217 | /* |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 218 | * 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] | 219 | * the callbacks as well. |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 220 | */ |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 221 | for (l = session->datafeed_callbacks; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 222 | cb = l->data; |
| 223 | cb(device, packet); |
| 224 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 227 | int session_save(char *filename) |
| 228 | { |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 229 | GSList *l, *p, *d; |
| 230 | FILE *meta; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 231 | struct device *device; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 232 | struct probe *probe; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 233 | struct datastore *ds; |
| 234 | struct zip *zipfile; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 235 | struct zip_source *versrc, *metasrc, *logicsrc; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 236 | int bufcnt, devcnt, tmpfile, ret, error; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 237 | char version[1], rawname[16], metafile[32], *newfn, *buf; |
| 238 | |
| 239 | newfn = g_malloc(strlen(filename) + 10); |
| 240 | strcpy(newfn, filename); |
| 241 | if (strstr(filename, ".sigrok") != filename+strlen(filename)-7) |
| 242 | strcat(newfn, ".sigrok"); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 243 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 244 | /* Quietly delete it first, libzip wants replace ops otherwise. */ |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 245 | unlink(newfn); |
| 246 | if (!(zipfile = zip_open(newfn, ZIP_CREATE, &error))) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 247 | return SIGROK_ERR; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 248 | g_free(newfn); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 249 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 250 | /* "version" */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 251 | version[0] = '1'; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 252 | if (!(versrc = zip_source_buffer(zipfile, version, 1, 0))) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 253 | return SIGROK_ERR; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 254 | if (zip_add(zipfile, "version", versrc) == -1) { |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 255 | g_message("error saving version into zipfile: %s", |
| 256 | zip_strerror(zipfile)); |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 257 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 260 | /* init "metadata" */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 261 | strcpy(metafile, "sigrok-meta-XXXXXX"); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 262 | if ((tmpfile = g_mkstemp(metafile)) == -1) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 263 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 264 | close(tmpfile); |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 265 | meta = fopen(metafile, "wb"); |
| 266 | fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION); |
| 267 | /* TODO: save protocol decoders used */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 268 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 269 | /* all datastores in all devices */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 270 | devcnt = 1; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 271 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 272 | device = l->data; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 273 | /* metadata */ |
| 274 | fprintf(meta, "[device]\n"); |
| 275 | if (device->plugin) |
| 276 | fprintf(meta, "driver = %s\n", device->plugin->name); |
| 277 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 278 | ds = device->datastore; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 279 | if (ds) { |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 280 | /* metadata */ |
| 281 | fprintf(meta, "capturefile = logic-%d\n", devcnt); |
| 282 | for (p = device->probes; p; p = p->next) { |
| 283 | probe = p->data; |
| 284 | if (probe->enabled) { |
| 285 | fprintf(meta, "probe %d", probe->index); |
| 286 | if (probe->name) |
| 287 | fprintf(meta, " name \"%s\"", probe->name); |
| 288 | if (probe->trigger) |
| 289 | fprintf(meta, " trigger \"%s\"", |
| 290 | probe->trigger); |
| 291 | fprintf(meta, "\n"); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* dump datastore into logic-n */ |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 296 | buf = malloc(ds->num_units * ds->ds_unitsize + |
| 297 | DATASTORE_CHUNKSIZE); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 298 | bufcnt = 0; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 299 | for (d = ds->chunklist; d; d = d->next) { |
| 300 | memcpy(buf + bufcnt, d->data, |
| 301 | DATASTORE_CHUNKSIZE); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 302 | bufcnt += DATASTORE_CHUNKSIZE; |
| 303 | } |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 304 | if (!(logicsrc = zip_source_buffer(zipfile, buf, |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 305 | ds->num_units * ds->ds_unitsize, TRUE))) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 306 | return SIGROK_ERR; |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 307 | snprintf(rawname, 15, "logic-%d", devcnt); |
| 308 | if (zip_add(zipfile, rawname, logicsrc) == -1) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 309 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 310 | } |
| 311 | devcnt++; |
| 312 | } |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 313 | fclose(meta); |
| 314 | |
| 315 | if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) |
| 316 | return SIGROK_ERR; |
| 317 | if (zip_add(zipfile, "metadata", metasrc) == -1) |
| 318 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 319 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame] | 320 | if ((ret = zip_close(zipfile)) == -1) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 321 | g_message("error saving zipfile: %s", zip_strerror(zipfile)); |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 322 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Bert Vermeulen | aa4b110 | 2011-01-24 07:46:16 +0100 | [diff] [blame] | 325 | unlink(metafile); |
| 326 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 327 | return SIGROK_OK; |
| 328 | } |
Bert Vermeulen | 544a458 | 2011-01-30 02:40:55 +0100 | [diff] [blame^] | 329 | |
| 330 | void session_source_add(int fd, int events, int timeout, |
| 331 | receive_data_callback callback, void *user_data) |
| 332 | { |
| 333 | struct source *new_sources, *s; |
| 334 | |
| 335 | new_sources = calloc(1, sizeof(struct source) * (num_sources + 1)); |
| 336 | |
| 337 | if (sources) { |
| 338 | memcpy(new_sources, sources, |
| 339 | sizeof(struct source) * num_sources); |
| 340 | free(sources); |
| 341 | } |
| 342 | |
| 343 | s = &new_sources[num_sources++]; |
| 344 | s->fd = fd; |
| 345 | s->events = events; |
| 346 | s->timeout = timeout; |
| 347 | s->cb = callback; |
| 348 | s->user_data = user_data; |
| 349 | sources = new_sources; |
| 350 | |
| 351 | if (timeout != source_timeout && timeout > 0 |
| 352 | && (source_timeout == -1 || timeout < source_timeout)) |
| 353 | source_timeout = timeout; |
| 354 | } |
| 355 | |
| 356 | void session_source_remove(int fd) |
| 357 | { |
| 358 | struct source *new_sources; |
| 359 | int old, new; |
| 360 | |
| 361 | if (!sources) |
| 362 | return; |
| 363 | |
| 364 | new_sources = calloc(1, sizeof(struct source) * num_sources); |
| 365 | for (old = 0; old < num_sources; old++) |
| 366 | if (sources[old].fd != fd) |
| 367 | memcpy(&new_sources[new++], &sources[old], |
| 368 | sizeof(struct source)); |
| 369 | |
| 370 | if (old != new) { |
| 371 | free(sources); |
| 372 | sources = new_sources; |
| 373 | num_sources--; |
| 374 | } else { |
| 375 | /* Target fd was not found. */ |
| 376 | free(new_sources); |
| 377 | } |
| 378 | } |
| 379 | |