X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fsession_file.c;h=9def98d732efdcb1aef743345627bc6dfd9d4481;hb=2a8f2d41adcd0aa9e498c4eea2a5f82263039e5c;hp=fa9f9edba3f5a9e8d744a3db14d9512878fa6864;hpb=7efe889e7aa5849ae3e4d09b8f50779992502b79;p=libsigrok.git diff --git a/src/session_file.c b/src/session_file.c index fa9f9edb..9def98d7 100644 --- a/src/session_file.c +++ b/src/session_file.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include #include #include #include @@ -26,8 +27,7 @@ #include #include #include -#include "config.h" /* Needed for PACKAGE_VERSION and others. */ -#include "libsigrok.h" +#include #include "libsigrok-internal.h" /** @cond PRIVATE */ @@ -46,8 +46,8 @@ * @{ */ -extern struct sr_session *session; extern SR_PRIV struct sr_dev_driver session_driver; +static int session_driver_initialized = 0; /** @private */ SR_PRIV int sr_sessionfile_check(const char *filename) @@ -63,7 +63,7 @@ SR_PRIV int sr_sessionfile_check(const char *filename) return SR_ERR_ARG; if (stat(filename, &st) == -1) { - sr_err("Couldn't stat %s: %s", filename, strerror(errno)); + sr_err("Couldn't stat %s: %s", filename, g_strerror(errno)); return SR_ERR; } @@ -73,7 +73,6 @@ SR_PRIV int sr_sessionfile_check(const char *filename) return SR_ERR; /* check "version" */ - version = 0; if (!(zf = zip_fopen(archive, "version", 0))) { sr_dbg("Not a sigrok session file: no version found."); return SR_ERR; @@ -95,20 +94,28 @@ SR_PRIV int sr_sessionfile_check(const char *filename) return SR_ERR; } + if ((ret = zip_close(archive)) == -1) { + sr_dbg("error closing zipfile: %s", zip_strerror(archive)); + return SR_ERR; + } + return SR_OK; } /** * Load the session from the specified filename. * - * @param filename The name of the session file to load. Must not be NULL. - * @param session The session to load the file into. Must not be NULL. + * @param ctx The context in which to load the session. + * @param filename The name of the session file to load. + * @param session The session to load the file into. * - * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, - * SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon - * other errors. + * @retval SR_OK Success + * @retval SR_ERR_MALLOC Memory allocation error + * @retval SR_ERR_DATA Malformed session file + * @retval SR_ERR This is not a session file */ -SR_API int sr_session_load(const char *filename, struct sr_session **session) +SR_API int sr_session_load(struct sr_context *ctx, const char *filename, + struct sr_session **session) { GKeyFile *kf; GPtrArray *capturefiles; @@ -117,8 +124,8 @@ SR_API int sr_session_load(const char *filename, struct sr_session **session) struct zip_stat zs; struct sr_dev_inst *sdi; struct sr_channel *ch; - int devcnt, ret, i, j; - uint64_t tmp_u64, total_channels, enabled_channels, p; + int ret, i, j; + uint64_t tmp_u64, total_channels, p; char **sections, **keys, *metafile, *val; char channelname[SR_MAX_CHANNELNAME_LEN + 1]; @@ -146,76 +153,88 @@ SR_API int sr_session_load(const char *filename, struct sr_session **session) return SR_ERR; } - if ((ret = sr_session_new(session)) != SR_OK) + if ((ret = sr_session_new(ctx, session)) != SR_OK) return ret; - devcnt = 0; + ret = SR_OK; capturefiles = g_ptr_array_new_with_free_func(g_free); sections = g_key_file_get_groups(kf, NULL); - for (i = 0; sections[i]; i++) { + for (i = 0; sections[i] && ret == SR_OK; i++) { if (!strcmp(sections[i], "global")) /* nothing really interesting in here yet */ continue; if (!strncmp(sections[i], "device ", 7)) { /* device section */ sdi = NULL; - enabled_channels = total_channels = 0; keys = g_key_file_get_keys(kf, sections[i], NULL, NULL); for (j = 0; keys[j]; j++) { val = g_key_file_get_string(kf, sections[i], keys[j], NULL); if (!strcmp(keys[j], "capturefile")) { - sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, NULL, NULL, NULL); + sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->driver = &session_driver; - if (devcnt == 0) + sdi->status = SR_ST_ACTIVE; + if (!session_driver_initialized) { /* first device, init the driver */ - sdi->driver->init(NULL); + session_driver_initialized = 1; + sdi->driver->init(sdi->driver, NULL); + } sr_dev_open(sdi); sr_session_dev_add(*session, sdi); + (*session)->owned_devs = g_slist_append( + (*session)->owned_devs, sdi); sdi->driver->config_set(SR_CONF_SESSIONFILE, g_variant_new_string(filename), sdi, NULL); sdi->driver->config_set(SR_CONF_CAPTUREFILE, g_variant_new_string(val), sdi, NULL); g_ptr_array_add(capturefiles, val); } else if (!strcmp(keys[j], "samplerate")) { + if (!sdi) { + ret = SR_ERR_DATA; + break; + } sr_parse_sizestring(val, &tmp_u64); sdi->driver->config_set(SR_CONF_SAMPLERATE, g_variant_new_uint64(tmp_u64), sdi, NULL); } else if (!strcmp(keys[j], "unitsize")) { + if (!sdi) { + ret = SR_ERR_DATA; + break; + } tmp_u64 = strtoull(val, NULL, 10); sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE, g_variant_new_uint64(tmp_u64), sdi, NULL); } else if (!strcmp(keys[j], "total probes")) { + if (!sdi) { + ret = SR_ERR_DATA; + break; + } total_channels = strtoull(val, NULL, 10); sdi->driver->config_set(SR_CONF_NUM_LOGIC_CHANNELS, g_variant_new_uint64(total_channels), sdi, NULL); for (p = 0; p < total_channels; p++) { snprintf(channelname, SR_MAX_CHANNELNAME_LEN, "%" PRIu64, p); - if (!(ch = sr_channel_new(p, SR_CHANNEL_LOGIC, TRUE, - channelname))) - return SR_ERR; - sdi->channels = g_slist_append(sdi->channels, ch); + sr_channel_new(sdi, p, SR_CHANNEL_LOGIC, FALSE, + channelname); } } else if (!strncmp(keys[j], "probe", 5)) { - if (!sdi) - continue; - enabled_channels++; - tmp_u64 = strtoul(keys[j]+5, NULL, 10); + if (!sdi) { + ret = SR_ERR_DATA; + break; + } + tmp_u64 = strtoul(keys[j]+5, NULL, 10) - 1; + ch = g_slist_nth_data(sdi->channels, tmp_u64); /* sr_session_save() */ - sr_dev_channel_name_set(sdi, tmp_u64 - 1, val); + sr_dev_channel_name_set(ch, val); + sr_dev_channel_enable(ch, TRUE); } } g_strfreev(keys); - /* Disable channels not specifically listed. */ - if (total_channels) - for (p = enabled_channels; p < total_channels; p++) - sr_dev_channel_enable(sdi, p, FALSE); } - devcnt++; } g_strfreev(sections); g_key_file_free(kf); - return SR_OK; + return ret; } /** @@ -332,7 +351,7 @@ SR_API int sr_session_save_init(struct sr_session *session, close(tmpfile); meta = g_fopen(metafile, "wb"); fprintf(meta, "[global]\n"); - fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION); + fprintf(meta, "sigrok version = %s\n", SR_PACKAGE_VERSION_STRING); /* metadata */ fprintf(meta, "[device 1]\n"); @@ -446,7 +465,7 @@ SR_API int sr_session_append(struct sr_session *session, const char *filename, if ((tmpfile = g_mkstemp(tmpname)) == -1) return SR_ERR; if (write(tmpfile, metafile, len) < 0) { - sr_dbg("Failed to create new metadata: %s", strerror(errno)); + sr_dbg("Failed to create new metadata: %s", g_strerror(errno)); g_free(metafile); unlink(tmpname); return SR_ERR;