Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the sigrok project. |
| 3 | * |
| 4 | * Copyright (C) 2011 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 | |
Uwe Hermann | 22b0238 | 2011-02-02 10:25:52 +0100 | [diff] [blame] | 20 | #include "config.h" |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <zip.h> |
| 25 | #include <glib.h> |
Uwe Hermann | 3bbd984 | 2011-02-06 02:14:57 +0100 | [diff] [blame] | 26 | #include <glib/gstdio.h> |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 27 | #include <sigrok.h> |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 28 | |
Uwe Hermann | 2872d21 | 2011-02-08 17:50:29 +0100 | [diff] [blame] | 29 | extern struct sr_session *session; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 30 | extern struct sr_device_plugin session_driver; |
| 31 | |
| 32 | |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 33 | int sr_session_load(const char *filename) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 34 | { |
| 35 | GKeyFile *kf; |
| 36 | GPtrArray *capturefiles; |
| 37 | struct zip *archive; |
| 38 | struct zip_file *zf; |
| 39 | struct zip_stat zs; |
Uwe Hermann | 2872d21 | 2011-02-08 17:50:29 +0100 | [diff] [blame] | 40 | struct sr_session *session; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 41 | struct sr_device *device; |
Uwe Hermann | 1afe898 | 2011-02-08 17:47:38 +0100 | [diff] [blame] | 42 | struct sr_probe *probe; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 43 | int ret, err, probenum, devcnt, i, j; |
| 44 | uint64_t tmp_u64, total_probes, enabled_probes, p; |
| 45 | char **sections, **keys, *metafile, *val, c; |
| 46 | |
| 47 | if (!(archive = zip_open(filename, 0, &err))) { |
| 48 | g_debug("Failed to open session file: zip error %d", err); |
| 49 | return SR_ERR; |
| 50 | } |
| 51 | |
| 52 | /* check "version" */ |
| 53 | if (!(zf = zip_fopen(archive, "version", 0))) { |
| 54 | g_debug("Not a sigrok session file."); |
| 55 | return SR_ERR; |
| 56 | } |
| 57 | ret = zip_fread(zf, &c, 1); |
| 58 | if (ret != 1 || c != '1') { |
| 59 | g_debug("Not a valid sigrok session file."); |
| 60 | return SR_ERR; |
| 61 | } |
| 62 | zip_fclose(zf); |
| 63 | |
| 64 | /* read "metadata" */ |
| 65 | if (zip_stat(archive, "metadata", 0, &zs) == -1) { |
| 66 | g_debug("Not a valid sigrok session file."); |
| 67 | return SR_ERR; |
| 68 | } |
| 69 | metafile = g_malloc(zs.size); |
| 70 | zf = zip_fopen_index(archive, zs.index, 0); |
| 71 | zip_fread(zf, metafile, zs.size); |
| 72 | zip_fclose(zf); |
| 73 | |
| 74 | kf = g_key_file_new(); |
| 75 | if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) { |
| 76 | g_debug("Failed to parse metadata."); |
| 77 | return SR_ERR; |
| 78 | } |
| 79 | |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 80 | session = sr_session_new(); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 81 | |
| 82 | devcnt = 0; |
| 83 | capturefiles = g_ptr_array_new_with_free_func(g_free); |
| 84 | sections = g_key_file_get_groups(kf, NULL); |
| 85 | for (i = 0; sections[i]; i++) { |
| 86 | if (!strcmp(sections[i], "global")) |
| 87 | /* nothing really interesting in here yet */ |
| 88 | continue; |
| 89 | if (!strncmp(sections[i], "device ", 7)) { |
| 90 | /* device section */ |
| 91 | device = NULL; |
| 92 | enabled_probes = 0; |
| 93 | keys = g_key_file_get_keys(kf, sections[i], NULL, NULL); |
| 94 | for (j = 0; keys[j]; j++) { |
| 95 | val = g_key_file_get_string(kf, sections[i], keys[j], NULL); |
| 96 | if (!strcmp(keys[j], "capturefile")) { |
Uwe Hermann | 2bf4aca | 2011-02-08 18:19:38 +0100 | [diff] [blame^] | 97 | device = sr_device_new(&session_driver, devcnt, 0); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 98 | if (devcnt == 0) |
| 99 | /* first device, init the plugin */ |
| 100 | device->plugin->init((char *)filename); |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 101 | sr_session_device_add(device); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 102 | device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTUREFILE, val); |
| 103 | g_ptr_array_add(capturefiles, val); |
| 104 | } else if (!strcmp(keys[j], "samplerate")) { |
| 105 | tmp_u64 = sr_parse_sizestring(val); |
| 106 | device->plugin->set_configuration(devcnt, SR_HWCAP_SAMPLERATE, &tmp_u64); |
| 107 | } else if (!strcmp(keys[j], "unitsize")) { |
| 108 | tmp_u64 = strtoull(val, NULL, 10); |
| 109 | device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_UNITSIZE, &tmp_u64); |
| 110 | } else if (!strcmp(keys[j], "total probes")) { |
| 111 | total_probes = strtoull(val, NULL, 10); |
| 112 | device->plugin->set_configuration(devcnt, SR_HWCAP_CAPTURE_NUM_PROBES, &total_probes); |
| 113 | for (p = 1; p <= total_probes; p++) |
Uwe Hermann | 2bf4aca | 2011-02-08 18:19:38 +0100 | [diff] [blame^] | 114 | sr_device_probe_add(device, NULL); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 115 | } else if (!strncmp(keys[j], "probe", 5)) { |
| 116 | if (!device) |
| 117 | continue; |
| 118 | enabled_probes++; |
| 119 | tmp_u64 = strtoul(keys[j]+5, NULL, 10); |
Uwe Hermann | 2bf4aca | 2011-02-08 18:19:38 +0100 | [diff] [blame^] | 120 | sr_device_probe_name(device, tmp_u64, val); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 121 | } else if (!strncmp(keys[j], "trigger", 7)) { |
| 122 | probenum = strtoul(keys[j]+7, NULL, 10); |
Uwe Hermann | 2bf4aca | 2011-02-08 18:19:38 +0100 | [diff] [blame^] | 123 | sr_device_trigger_set(device, probenum, val); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | g_strfreev(keys); |
| 127 | for (p = enabled_probes; p < total_probes; p++) { |
| 128 | probe = g_slist_nth_data(device->probes, p); |
| 129 | probe->enabled = FALSE; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | g_strfreev(sections); |
| 134 | g_key_file_free(kf); |
| 135 | |
| 136 | return SR_OK; |
| 137 | } |
| 138 | |
Uwe Hermann | 8a2efef | 2011-02-08 18:00:49 +0100 | [diff] [blame] | 139 | int sr_session_save(char *filename) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 140 | { |
| 141 | GSList *l, *p, *d; |
| 142 | FILE *meta; |
| 143 | struct sr_device *device; |
Uwe Hermann | 1afe898 | 2011-02-08 17:47:38 +0100 | [diff] [blame] | 144 | struct sr_probe *probe; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 145 | struct datastore *ds; |
| 146 | struct zip *zipfile; |
| 147 | struct zip_source *versrc, *metasrc, *logicsrc; |
| 148 | int bufcnt, devcnt, tmpfile, ret, error, probecnt; |
| 149 | uint64_t samplerate; |
Bert Vermeulen | 4a1b18f | 2011-02-01 01:41:33 +0100 | [diff] [blame] | 150 | char version[1], rawname[16], metafile[32], *buf, *s; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 151 | |
| 152 | /* Quietly delete it first, libzip wants replace ops otherwise. */ |
Bert Vermeulen | 4a1b18f | 2011-02-01 01:41:33 +0100 | [diff] [blame] | 153 | unlink(filename); |
| 154 | if (!(zipfile = zip_open(filename, ZIP_CREATE, &error))) |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 155 | return SR_ERR; |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 156 | |
| 157 | /* "version" */ |
| 158 | version[0] = '1'; |
| 159 | if (!(versrc = zip_source_buffer(zipfile, version, 1, 0))) |
| 160 | return SR_ERR; |
| 161 | if (zip_add(zipfile, "version", versrc) == -1) { |
| 162 | g_message("error saving version into zipfile: %s", |
| 163 | zip_strerror(zipfile)); |
| 164 | return SR_ERR; |
| 165 | } |
| 166 | |
| 167 | /* init "metadata" */ |
| 168 | strcpy(metafile, "sigrok-meta-XXXXXX"); |
| 169 | if ((tmpfile = g_mkstemp(metafile)) == -1) |
| 170 | return SR_ERR; |
| 171 | close(tmpfile); |
Uwe Hermann | 868d8ce | 2011-02-05 20:03:17 +0100 | [diff] [blame] | 172 | meta = g_fopen(metafile, "wb"); |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 173 | fprintf(meta, "[global]\n"); |
| 174 | fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION); |
| 175 | /* TODO: save protocol decoders used */ |
| 176 | |
| 177 | /* all datastores in all devices */ |
| 178 | devcnt = 1; |
| 179 | for (l = session->devices; l; l = l->next) { |
| 180 | device = l->data; |
| 181 | /* metadata */ |
| 182 | fprintf(meta, "[device %d]\n", devcnt); |
| 183 | if (device->plugin) |
| 184 | fprintf(meta, "driver = %s\n", device->plugin->name); |
| 185 | |
| 186 | ds = device->datastore; |
| 187 | if (ds) { |
| 188 | /* metadata */ |
| 189 | fprintf(meta, "capturefile = logic-%d\n", devcnt); |
| 190 | fprintf(meta, "unitsize = %d\n", ds->ds_unitsize); |
| 191 | fprintf(meta, "total probes = %d\n", g_slist_length(device->probes)); |
Uwe Hermann | 2bf4aca | 2011-02-08 18:19:38 +0100 | [diff] [blame^] | 192 | if (sr_device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) { |
Bert Vermeulen | 7d65887 | 2011-01-31 22:34:14 +0100 | [diff] [blame] | 193 | samplerate = *((uint64_t *) device->plugin->get_device_info( |
| 194 | device->plugin_index, SR_DI_CUR_SAMPLERATE)); |
| 195 | s = sr_samplerate_string(samplerate); |
| 196 | fprintf(meta, "samplerate = %s\n", s); |
| 197 | free(s); |
| 198 | } |
| 199 | probecnt = 1; |
| 200 | for (p = device->probes; p; p = p->next) { |
| 201 | probe = p->data; |
| 202 | if (probe->enabled) { |
| 203 | if (probe->name) |
| 204 | fprintf(meta, "probe%d = %s\n", probecnt, probe->name); |
| 205 | if (probe->trigger) |
| 206 | fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger); |
| 207 | probecnt++; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* dump datastore into logic-n */ |
| 212 | buf = malloc(ds->num_units * ds->ds_unitsize + |
| 213 | DATASTORE_CHUNKSIZE); |
| 214 | bufcnt = 0; |
| 215 | for (d = ds->chunklist; d; d = d->next) { |
| 216 | memcpy(buf + bufcnt, d->data, |
| 217 | DATASTORE_CHUNKSIZE); |
| 218 | bufcnt += DATASTORE_CHUNKSIZE; |
| 219 | } |
| 220 | if (!(logicsrc = zip_source_buffer(zipfile, buf, |
| 221 | ds->num_units * ds->ds_unitsize, TRUE))) |
| 222 | return SR_ERR; |
| 223 | snprintf(rawname, 15, "logic-%d", devcnt); |
| 224 | if (zip_add(zipfile, rawname, logicsrc) == -1) |
| 225 | return SR_ERR; |
| 226 | } |
| 227 | devcnt++; |
| 228 | } |
| 229 | fclose(meta); |
| 230 | |
| 231 | if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) |
| 232 | return SR_ERR; |
| 233 | if (zip_add(zipfile, "metadata", metasrc) == -1) |
| 234 | return SR_ERR; |
| 235 | |
| 236 | if ((ret = zip_close(zipfile)) == -1) { |
| 237 | g_message("error saving zipfile: %s", zip_strerror(zipfile)); |
| 238 | return SR_ERR; |
| 239 | } |
| 240 | |
| 241 | unlink(metafile); |
| 242 | |
| 243 | return SR_OK; |
| 244 | } |
| 245 | |