/** * Load the session from the specified filename. * * @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. * * @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(struct sr_context *ctx, const char *filename, struct sr_session **session) { GKeyFile *kf; GError *error; struct zip *archive; struct zip_stat zs; struct sr_dev_inst *sdi; struct sr_channel *ch; int ret, i, j; uint64_t tmp_u64; int total_channels, total_analog, k; GSList *l; int unitsize; char **sections, **keys, *val; char channelname[SR_MAX_CHANNELNAME_LEN + 1]; gboolean file_has_logic; if ((ret = sr_sessionfile_check(filename)) != SR_OK) return ret; if (!(archive = zip_open(filename, 0, NULL))) return SR_ERR; if (zip_stat(archive, "metadata", 0, &zs) < 0) { zip_discard(archive); return SR_ERR; } kf = sr_sessionfile_read_metadata(archive, &zs); zip_discard(archive); if (!kf) return SR_ERR_DATA; if ((ret = sr_session_new(ctx, session)) != SR_OK) { g_key_file_free(kf); return ret; } total_channels = 0; error = NULL; ret = SR_OK; file_has_logic = FALSE; sections = g_key_file_get_groups(kf, NULL); 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; keys = g_key_file_get_keys(kf, sections[i], NULL, NULL); /* File contains analog data if there are analog channels. */ total_analog = g_key_file_get_integer(kf, sections[i], "total analog", &error); if (total_analog > 0 && !error) sdi = sr_session_prepare_sdi(filename, session); g_clear_error(&error); /* File contains logic data if a capturefile is set. */ val = g_key_file_get_string(kf, sections[i], "capturefile", &error); if (val && !error) { if (!sdi) sdi = sr_session_prepare_sdi(filename, session); sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE, g_variant_new_string(val)); g_free(val); file_has_logic = TRUE; } g_clear_error(&error); for (j = 0; keys[j]; j++) { if (!strcmp(keys[j], "samplerate")) { val = g_key_file_get_string(kf, sections[i], keys[j], &error); if (!sdi || !val || sr_parse_sizestring(val, &tmp_u64) != SR_OK) { g_free(val); ret = SR_ERR_DATA; break; } g_free(val); sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE, g_variant_new_uint64(tmp_u64)); } else if (!strcmp(keys[j], "unitsize") && file_has_logic) { unitsize = g_key_file_get_integer(kf, sections[i], keys[j], &error); if (!sdi || unitsize <= 0 || error) { ret = SR_ERR_DATA; break; } sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE, g_variant_new_uint64(unitsize)); } else if (!strcmp(keys[j], "total probes")) { total_channels = g_key_file_get_integer(kf, sections[i], keys[j], &error); if (!sdi || total_channels < 0 || error) { ret = SR_ERR_DATA; break; } sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS, g_variant_new_int32(total_channels)); for (k = 0; k < total_channels; k++) { g_snprintf(channelname, sizeof(channelname), "%d", k); sr_channel_new(sdi, k, SR_CHANNEL_LOGIC, FALSE, channelname); } } else if (!strcmp(keys[j], "total analog")) { total_analog = g_key_file_get_integer(kf, sections[i], keys[j], &error); if (!sdi || total_analog < 0 || error) { ret = SR_ERR_DATA; break; } sr_config_set(sdi, NULL, SR_CONF_NUM_ANALOG_CHANNELS, g_variant_new_int32(total_analog)); for (k = total_channels; k < (total_channels + total_analog); k++) { g_snprintf(channelname, sizeof(channelname), "%d", k); sr_channel_new(sdi, k, SR_CHANNEL_ANALOG, FALSE, channelname); } } else if (!strncmp(keys[j], "probe", 5)) { tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10); if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) { ret = SR_ERR_DATA; break; } ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1); if (!ch) { ret = SR_ERR_DATA; break; } val = g_key_file_get_string(kf, sections[i], keys[j], &error); if (!val) { ret = SR_ERR_DATA; break; } /* sr_session_save() */ sr_dev_channel_name_set(ch, val); g_free(val); sr_dev_channel_enable(ch, TRUE); } else if (!strncmp(keys[j], "analog", 6)) { tmp_u64 = g_ascii_strtoull(keys[j]+6, NULL, 10); if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) { ret = SR_ERR_DATA; break; } ch = NULL; for (l = sdi->channels; l; l = l->next) { ch = l->data; if ((guint64)ch->index == tmp_u64 - 1) break; else ch = NULL; } if (!ch) { ret = SR_ERR_DATA; break; } val = g_key_file_get_string(kf, sections[i], keys[j], &error); if (!val) { ret = SR_ERR_DATA; break; } /* sr_session_save() */ sr_dev_channel_name_set(ch, val); g_free(val); sr_dev_channel_enable(ch, TRUE); } } g_strfreev(keys); } } g_strfreev(sections); g_key_file_free(kf); if (error) { sr_err("Failed to parse metadata: %s", error->message); g_error_free(error); } return ret; }
/** * Load the session from the specified filename. * * @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. * * @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(struct sr_context *ctx, const char *filename, struct sr_session **session) { GKeyFile *kf; GPtrArray *capturefiles; struct zip *archive; struct zip_file *zf; struct zip_stat zs; struct sr_dev_inst *sdi; struct sr_channel *ch; int ret, i, j; uint64_t tmp_u64, total_channels, p; char **sections, **keys, *metafile, *val; char channelname[SR_MAX_CHANNELNAME_LEN + 1]; if ((ret = sr_sessionfile_check(filename)) != SR_OK) return ret; if (!(archive = zip_open(filename, 0, &ret))) return SR_ERR; if (zip_stat(archive, "metadata", 0, &zs) == -1) return SR_ERR; if (!(metafile = g_try_malloc(zs.size))) { sr_err("%s: metafile malloc failed", __func__); return SR_ERR_MALLOC; } zf = zip_fopen_index(archive, zs.index, 0); zip_fread(zf, metafile, zs.size); zip_fclose(zf); kf = g_key_file_new(); if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) { sr_dbg("Failed to parse metadata."); return SR_ERR; } if ((ret = sr_session_new(ctx, session)) != SR_OK) return ret; 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] && 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; 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 = g_malloc0(sizeof(struct sr_dev_inst)); sdi->driver = &session_driver; sdi->status = SR_ST_ACTIVE; if (!session_driver_initialized) { /* first device, init the driver */ 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); sr_channel_new(sdi, p, SR_CHANNEL_LOGIC, FALSE, channelname); } } else if (!strncmp(keys[j], "probe", 5)) { 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(ch, val); sr_dev_channel_enable(ch, TRUE); } } g_strfreev(keys); } } g_strfreev(sections); g_key_file_free(kf); return ret; }
/** * Append data to an existing session file. * * The session file must have been created with sr_session_save_init() * or sr_session_save() beforehand. * * @param filename The name of the filename to append to. Must not be NULL. * @param buf The data to be appended. * @param unitsize The number of bytes per sample. * @param units The number of samples. * * @retval SR_OK Success * @retval SR_ERR_ARG Invalid arguments * @retval SR_ERR Other errors * * @since 0.3.0 */ SR_API int sr_session_append(const char *filename, unsigned char *buf, int unitsize, int units) { struct zip *archive; struct zip_source *logicsrc; zip_int64_t num_files; struct zip_file *zf; struct zip_stat zs; struct zip_source *metasrc; GKeyFile *kf; GError *error; gsize len; int chunk_num, next_chunk_num, tmpfile, ret, i; const char *entry_name; char *metafile, tmpname[32], chunkname[16]; if ((ret = sr_sessionfile_check(filename)) != SR_OK) return ret; if (!(archive = zip_open(filename, 0, &ret))) return SR_ERR; if (zip_stat(archive, "metadata", 0, &zs) == -1) return SR_ERR; metafile = g_malloc(zs.size); zf = zip_fopen_index(archive, zs.index, 0); zip_fread(zf, metafile, zs.size); zip_fclose(zf); /* * If the file was only initialized but doesn't yet have any * data it in, it won't have a unitsize field in metadata yet. */ error = NULL; kf = g_key_file_new(); if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, &error)) { sr_err("Failed to parse metadata: %s.", error->message); return SR_ERR; } g_free(metafile); tmpname[0] = '\0'; if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) { if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) { sr_err("Failed to check unitsize key: %s", error ? error->message : "?"); return SR_ERR; } /* Add unitsize field. */ g_key_file_set_integer(kf, "device 1", "unitsize", unitsize); metafile = g_key_file_to_data(kf, &len, &error); strcpy(tmpname, "sigrok-meta-XXXXXX"); 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)); g_free(metafile); unlink(tmpname); return SR_ERR; } close(tmpfile); if (!(metasrc = zip_source_file(archive, tmpname, 0, -1))) { sr_err("Failed to create zip source for metadata."); g_free(metafile); unlink(tmpname); return SR_ERR; } if (zip_replace(archive, zs.index, metasrc) == -1) { sr_err("Failed to replace metadata file."); g_free(metafile); unlink(tmpname); return SR_ERR; } g_free(metafile); } g_key_file_free(kf); next_chunk_num = 1; num_files = zip_get_num_entries(archive, 0); for (i = 0; i < num_files; i++) { entry_name = zip_get_name(archive, i, 0); if (strncmp(entry_name, "logic-1", 7)) continue; if (strlen(entry_name) == 7) { /* This file has no extra chunks, just a single "logic-1". * Rename it to "logic-1-1" * and continue with chunk 2. */ if (zip_rename(archive, i, "logic-1-1") == -1) { sr_err("Failed to rename 'logic-1' to 'logic-1-1'."); unlink(tmpname); return SR_ERR; } next_chunk_num = 2; break; } else if (strlen(entry_name) > 8 && entry_name[7] == '-') { chunk_num = strtoull(entry_name + 8, NULL, 10); if (chunk_num >= next_chunk_num) next_chunk_num = chunk_num + 1; } } snprintf(chunkname, 15, "logic-1-%d", next_chunk_num); if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE))) { unlink(tmpname); return SR_ERR; } if (zip_add(archive, chunkname, logicsrc) == -1) { unlink(tmpname); return SR_ERR; } if ((ret = zip_close(archive)) == -1) { sr_info("error saving session file: %s", zip_strerror(archive)); unlink(tmpname); return SR_ERR; } unlink(tmpname); return SR_OK; }
/** * Load the session from the specified filename. * * @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. * * @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(struct sr_context *ctx, const char *filename, struct sr_session **session) { GKeyFile *kf; GError *error; struct zip *archive; struct zip_stat zs; struct sr_dev_inst *sdi; struct sr_channel *ch; int ret, i, j; uint64_t tmp_u64; int total_channels, k; int unitsize; char **sections, **keys, *val; char channelname[SR_MAX_CHANNELNAME_LEN + 1]; if ((ret = sr_sessionfile_check(filename)) != SR_OK) return ret; if (!(archive = zip_open(filename, 0, NULL))) return SR_ERR; if (zip_stat(archive, "metadata", 0, &zs) < 0) { zip_discard(archive); return SR_ERR; } kf = sr_sessionfile_read_metadata(archive, &zs); zip_discard(archive); if (!kf) return SR_ERR_DATA; if ((ret = sr_session_new(ctx, session)) != SR_OK) { g_key_file_free(kf); return ret; } error = NULL; ret = SR_OK; sections = g_key_file_get_groups(kf, NULL); 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; keys = g_key_file_get_keys(kf, sections[i], NULL, NULL); for (j = 0; keys[j]; j++) { if (!strcmp(keys[j], "capturefile")) { val = g_key_file_get_string(kf, sections[i], keys[j], &error); if (!val) { ret = SR_ERR_DATA; break; } sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->driver = &session_driver; sdi->status = SR_ST_ACTIVE; if (!session_driver_initialized) { /* first device, init the driver */ 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); sr_config_set(sdi, NULL, SR_CONF_SESSIONFILE, g_variant_new_string(filename)); sr_config_set(sdi, NULL, SR_CONF_CAPTUREFILE, g_variant_new_string(val)); g_free(val); } else if (!strcmp(keys[j], "samplerate")) { val = g_key_file_get_string(kf, sections[i], keys[j], &error); if (!sdi || !val || sr_parse_sizestring(val, &tmp_u64) != SR_OK) { g_free(val); ret = SR_ERR_DATA; break; } g_free(val); sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE, g_variant_new_uint64(tmp_u64)); } else if (!strcmp(keys[j], "unitsize")) { unitsize = g_key_file_get_integer(kf, sections[i], keys[j], &error); if (!sdi || unitsize <= 0 || error) { ret = SR_ERR_DATA; break; } sr_config_set(sdi, NULL, SR_CONF_CAPTURE_UNITSIZE, g_variant_new_uint64(unitsize)); } else if (!strcmp(keys[j], "total probes")) { total_channels = g_key_file_get_integer(kf, sections[i], keys[j], &error); if (!sdi || total_channels < 0 || error) { ret = SR_ERR_DATA; break; } sr_config_set(sdi, NULL, SR_CONF_NUM_LOGIC_CHANNELS, g_variant_new_int32(total_channels)); for (k = 0; k < total_channels; k++) { g_snprintf(channelname, sizeof channelname, "%d", k); sr_channel_new(sdi, k, SR_CHANNEL_LOGIC, FALSE, channelname); } } else if (!strncmp(keys[j], "probe", 5)) { tmp_u64 = g_ascii_strtoull(keys[j]+5, NULL, 10); if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) { ret = SR_ERR_DATA; break; } ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1); if (!ch) { ret = SR_ERR_DATA; break; } val = g_key_file_get_string(kf, sections[i], keys[j], &error); if (!val) { ret = SR_ERR_DATA; break; } /* sr_session_save() */ sr_dev_channel_name_set(ch, val); g_free(val); sr_dev_channel_enable(ch, TRUE); } } g_strfreev(keys); } } g_strfreev(sections); g_key_file_free(kf); if (error) { sr_err("Failed to parse metadata: %s", error->message); g_error_free(error); } return ret; }