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> |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 25 | #include <sigrok.h> |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 26 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 27 | /* There can only be one session at a time. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 28 | struct session *session; |
| 29 | |
Uwe Hermann | afc8e4d | 2010-04-09 22:18:46 +0200 | [diff] [blame] | 30 | struct session *session_load(const char *filename) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 31 | { |
| 32 | struct session *session; |
| 33 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 34 | /* TODO: Implement. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 35 | session = NULL; |
| 36 | |
Uwe Hermann | afc8e4d | 2010-04-09 22:18:46 +0200 | [diff] [blame] | 37 | /* QUICK HACK */ |
| 38 | filename = filename; |
| 39 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 40 | return session; |
| 41 | } |
| 42 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 43 | struct session *session_new(void) |
| 44 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 45 | session = calloc(1, sizeof(struct session)); |
| 46 | |
| 47 | return session; |
| 48 | } |
| 49 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 50 | void session_destroy(void) |
| 51 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 52 | g_slist_free(session->devices); |
| 53 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 54 | /* TODO: Loop over protocols and free them. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 55 | |
| 56 | g_free(session); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 59 | void session_device_clear(void) |
| 60 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 61 | g_slist_free(session->devices); |
| 62 | session->devices = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 65 | int session_device_add(struct device *device) |
| 66 | { |
| 67 | int ret; |
| 68 | |
| 69 | ret = device->plugin->open(device->plugin_index); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 70 | if (ret == SIGROK_OK) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 71 | session->devices = g_slist_append(session->devices, device); |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 76 | void session_pa_clear(void) |
| 77 | { |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 78 | /* |
| 79 | * The protocols are pointers to the global set of PA plugins, |
| 80 | * so don't free them. |
| 81 | */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 82 | g_slist_free(session->analyzers); |
| 83 | session->analyzers = 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 | void session_pa_add(struct analyzer *an) |
| 87 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 88 | session->analyzers = g_slist_append(session->analyzers, an); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 91 | void session_datafeed_callback_clear(void) |
| 92 | { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 93 | g_slist_free(session->datafeed_callbacks); |
| 94 | session->datafeed_callbacks = NULL; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 97 | void session_datafeed_callback_add(datafeed_callback callback) |
| 98 | { |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 99 | session->datafeed_callbacks = |
| 100 | g_slist_append(session->datafeed_callbacks, callback); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 103 | int session_start(void) |
| 104 | { |
| 105 | struct device *device; |
| 106 | GSList *l; |
| 107 | int ret; |
| 108 | |
| 109 | g_message("starting acquisition"); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 110 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 111 | device = l->data; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 112 | if ((ret = device->plugin->start_acquisition( |
| 113 | device->plugin_index, device)) != SIGROK_OK) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 120 | void session_stop(void) |
| 121 | { |
| 122 | struct device *device; |
| 123 | GSList *l; |
| 124 | |
| 125 | g_message("stopping acquisition"); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 126 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 127 | device = l->data; |
| 128 | device->plugin->stop_acquisition(device->plugin_index, device); |
| 129 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 132 | void session_bus(struct device *device, struct datafeed_packet *packet) |
| 133 | { |
| 134 | GSList *l; |
| 135 | datafeed_callback cb; |
| 136 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 137 | /* |
| 138 | * TODO: Send packet through PA pipe, and send the output of that to |
| 139 | * the callbacks as well. |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 140 | */ |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 141 | for (l = session->datafeed_callbacks; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 142 | cb = l->data; |
| 143 | cb(device, packet); |
| 144 | } |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 147 | void make_metadata(char *filename) |
| 148 | { |
| 149 | GSList *l, *p; |
| 150 | struct device *device; |
| 151 | struct probe *probe; |
| 152 | FILE *f; |
| 153 | int devcnt; |
| 154 | |
| 155 | f = fopen(filename, "wb"); |
| 156 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 157 | /* General */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 158 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 159 | /* Devices */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 160 | devcnt = 1; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 161 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 162 | device = l->data; |
| 163 | fprintf(f, "[device]\n"); |
| 164 | fprintf(f, "driver = %s\n", device->plugin->name); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 165 | |
| 166 | if (device->datastore) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 167 | fprintf(f, "capturefile = raw-%d\n", devcnt); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 168 | |
| 169 | for (p = device->probes; p; p = p->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 170 | probe = p->data; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 171 | if (probe->enabled) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 172 | fprintf(f, "probe %d", probe->index); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 173 | if (probe->name) |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 174 | fprintf(f, " name \"%s\"", probe->name); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 175 | if (probe->trigger) |
| 176 | fprintf(f, " trigger \"%s\"", |
| 177 | probe->trigger); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 178 | fprintf(f, "\n"); |
| 179 | } |
| 180 | } |
| 181 | devcnt++; |
| 182 | } |
| 183 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 184 | /* TODO: Protocol analyzers */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 185 | |
| 186 | fclose(f); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 189 | int session_save(char *filename) |
| 190 | { |
| 191 | GSList *l, *d; |
| 192 | struct device *device; |
| 193 | struct datastore *ds; |
| 194 | struct zip *zipfile; |
| 195 | struct zip_source *src; |
| 196 | int bufcnt, devcnt, tmpfile, ret, error; |
| 197 | char version[1], rawname[16], metafile[32], *buf; |
| 198 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 199 | /* Quietly delete it first, libzip wants replace ops otherwise. */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 200 | unlink(filename); |
| 201 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 202 | if (!(zipfile = zip_open(filename, ZIP_CREATE, &error))) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 203 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 204 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 205 | /* Version */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 206 | version[0] = '1'; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 207 | if (!(src = zip_source_buffer(zipfile, version, 1, 0))) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 208 | return SIGROK_ERR; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 209 | if (zip_add(zipfile, "version", src) == -1) { |
| 210 | g_message("error saving version into zipfile: %s", |
| 211 | zip_strerror(zipfile)); |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 212 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 215 | /* Metadata */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 216 | strcpy(metafile, "sigrok-meta-XXXXXX"); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 217 | if ((tmpfile = g_mkstemp(metafile)) == -1) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 218 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 219 | close(tmpfile); |
| 220 | make_metadata(metafile); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 221 | if (!(src = zip_source_file(zipfile, metafile, 0, -1))) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 222 | return SIGROK_ERR; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 223 | if (zip_add(zipfile, "metadata", src) == -1) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 224 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 225 | unlink(metafile); |
| 226 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 227 | /* Raw */ |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 228 | devcnt = 1; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 229 | for (l = session->devices; l; l = l->next) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 230 | device = l->data; |
| 231 | ds = device->datastore; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 232 | if (ds) { |
| 233 | buf = malloc(ds->num_units * ds->ds_unitsize + |
| 234 | DATASTORE_CHUNKSIZE); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 235 | bufcnt = 0; |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 236 | for (d = ds->chunklist; d; d = d->next) { |
| 237 | memcpy(buf + bufcnt, d->data, |
| 238 | DATASTORE_CHUNKSIZE); |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 239 | bufcnt += DATASTORE_CHUNKSIZE; |
| 240 | } |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 241 | if (!(src = zip_source_buffer(zipfile, buf, |
| 242 | ds->num_units * ds->ds_unitsize, TRUE))) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 243 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 244 | snprintf(rawname, 15, "raw-%d", devcnt); |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 245 | if (zip_add(zipfile, rawname, src) == -1) |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 246 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 247 | } |
| 248 | devcnt++; |
| 249 | } |
| 250 | |
Uwe Hermann | 62c8202 | 2010-04-15 20:16:53 +0200 | [diff] [blame^] | 251 | if ((ret = zip_close(zipfile)) == -1) { |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 252 | g_message("error saving zipfile: %s", zip_strerror(zipfile)); |
Uwe Hermann | e31b636 | 2010-04-05 16:41:54 +0200 | [diff] [blame] | 253 | return SIGROK_ERR; |
Uwe Hermann | a1bb33a | 2010-04-02 20:18:27 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return SIGROK_OK; |
| 257 | } |