static void probe_trigger_set(GtkCellRendererText *cel, gchar *path, gchar *text, GtkTreeModel *probes) { struct sr_dev *dev = g_object_get_data(G_OBJECT(probes), "dev"); GtkTreeIter iter; gint i; (void)cel; gtk_tree_model_get_iter_from_string(probes, &iter, path); gtk_tree_model_get(probes, &iter, PROBE_NUMBER, &i, -1); sr_dev_trigger_set(dev, i, text); gtk_list_store_set(GTK_LIST_STORE(probes), &iter, PROBE_TRIGGER, text, -1); }
/** * Load the session from the specified filename. * * @param filename The name of the session file to load. Must not be NULL. * * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, * SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon * other errors. */ SR_API int sr_session_load(const char *filename) { GKeyFile *kf; GPtrArray *capturefiles; struct zip *archive; struct zip_file *zf; struct zip_stat zs; struct sr_dev_inst *sdi; struct sr_channel *probe; int ret, devcnt, i, j, k; uint16_t probenum; uint64_t tmp_u64, total_probes, enabled_probes; uint16_t p; char **sections, **keys, *metafile, *val, s[11]; char probename[SR_MAX_PROBENAME_LEN + 1]; int mode = LOGIC; int channel_type = SR_CHANNEL_LOGIC; double tmp_double; if (!filename) { sr_err("%s: filename was NULL", __func__); return SR_ERR_ARG; } if (!(archive = zip_open(filename, 0, &ret))) { sr_dbg("Failed to open session file: zip error %d", ret); return SR_ERR; } /* read "metadata" */ if (zip_stat(archive, "header", 0, &zs) == -1) { sr_dbg("Not a valid DSView data file."); 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; } sr_session_new(); devcnt = 0; capturefiles = g_ptr_array_new_with_free_func(g_free); sections = g_key_file_get_groups(kf, NULL); for (i = 0; sections[i]; i++) { if (!strcmp(sections[i], "version")) /* nothing really interesting in here yet */ continue; if (!strncmp(sections[i], "header", 6)) { /* device section */ sdi = NULL; enabled_probes = total_probes = 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], "device mode")) { mode = strtoull(val, NULL, 10); } else if (!strcmp(keys[j], "capturefile")) { sdi = sr_dev_inst_new(mode, devcnt, SR_ST_ACTIVE, NULL, NULL, NULL); sdi->driver = &session_driver; if (devcnt == 0) /* first device, init the driver */ sdi->driver->init(NULL); sr_dev_open(sdi); sr_session_dev_add(sdi); sdi->driver->config_set(SR_CONF_SESSIONFILE, g_variant_new_bytestring(filename), sdi, NULL, NULL); sdi->driver->config_set(SR_CONF_CAPTUREFILE, g_variant_new_bytestring(val), sdi, NULL, NULL); g_ptr_array_add(capturefiles, val); } else if (!strcmp(keys[j], "samplerate")) { sr_parse_sizestring(val, &tmp_u64); sdi->driver->config_set(SR_CONF_SAMPLERATE, g_variant_new_uint64(tmp_u64), sdi, NULL, NULL); } else if (!strcmp(keys[j], "unitsize")) { tmp_u64 = strtoull(val, NULL, 10); sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE, g_variant_new_uint64(tmp_u64), sdi, NULL, NULL); } else if (!strcmp(keys[j], "total samples")) { tmp_u64 = strtoull(val, NULL, 10); sdi->driver->config_set(SR_CONF_LIMIT_SAMPLES, g_variant_new_uint64(tmp_u64), sdi, NULL, NULL); } else if (!strcmp(keys[j], "hDiv")) { tmp_u64 = strtoull(val, NULL, 10); sdi->driver->config_set(SR_CONF_TIMEBASE, g_variant_new_uint64(tmp_u64), sdi, NULL, NULL); } else if (!strcmp(keys[j], "total probes")) { total_probes = strtoull(val, NULL, 10); sdi->driver->config_set(SR_CONF_CAPTURE_NUM_PROBES, g_variant_new_uint64(total_probes), sdi, NULL, NULL); channel_type = (mode == DSO) ? SR_CHANNEL_DSO : (mode == ANALOG) ? SR_CHANNEL_ANALOG : SR_CHANNEL_LOGIC; for (p = 0; p < total_probes; p++) { snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p); if (!(probe = sr_channel_new(p, channel_type, FALSE, probename))) return SR_ERR; sdi->channels = g_slist_append(sdi->channels, probe); } } else if (!strncmp(keys[j], "probe", 5)) { if (!sdi) continue; enabled_probes++; tmp_u64 = strtoul(keys[j]+5, NULL, 10); /* sr_session_save() */ sr_dev_probe_name_set(sdi, tmp_u64, val); sr_dev_probe_enable(sdi, tmp_u64, TRUE); } else if (!strncmp(keys[j], "trigger", 7)) { probenum = strtoul(keys[j]+7, NULL, 10); sr_dev_trigger_set(sdi, probenum, val); } else if (!strncmp(keys[j], "enable", 6)) { probenum = strtoul(keys[j]+6, NULL, 10); tmp_u64 = strtoull(val, NULL, 10); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_EN_CH, g_variant_new_boolean(tmp_u64), sdi, probe, NULL); } } else if (!strncmp(keys[j], "coupling", 8)) { probenum = strtoul(keys[j]+8, NULL, 10); tmp_u64 = strtoull(val, NULL, 10); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_COUPLING, g_variant_new_byte(tmp_u64), sdi, probe, NULL); } } else if (!strncmp(keys[j], "vDiv", 4)) { probenum = strtoul(keys[j]+4, NULL, 10); tmp_u64 = strtoull(val, NULL, 10); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_VDIV, g_variant_new_uint64(tmp_u64), sdi, probe, NULL); } } else if (!strncmp(keys[j], "vFactor", 7)) { probenum = strtoul(keys[j]+7, NULL, 10); tmp_u64 = strtoull(val, NULL, 10); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_FACTOR, g_variant_new_uint64(tmp_u64), sdi, probe, NULL); } } else if (!strncmp(keys[j], "vPos", 4)) { probenum = strtoul(keys[j]+4, NULL, 10); tmp_double = strtod(val, NULL); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_VPOS, g_variant_new_double(tmp_double), sdi, probe, NULL); } } else if (!strncmp(keys[j], "period", 6)) { probenum = strtoul(keys[j]+6, NULL, 10); tmp_u64 = strtoull(val, NULL, 10); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_STATUS_PERIOD, g_variant_new_uint64(tmp_u64), sdi, probe, NULL); } } else if (!strncmp(keys[j], "pcnt", 4)) { probenum = strtoul(keys[j]+4, NULL, 10); tmp_u64 = strtoull(val, NULL, 10); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_STATUS_PCNT, g_variant_new_uint64(tmp_u64), sdi, probe, NULL); } } else if (!strncmp(keys[j], "max", 3)) { probenum = strtoul(keys[j]+3, NULL, 10); tmp_u64 = strtoull(val, NULL, 10); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_STATUS_MAX, g_variant_new_uint64(tmp_u64), sdi, probe, NULL); } } else if (!strncmp(keys[j], "min", 3)) { probenum = strtoul(keys[j]+3, NULL, 10); tmp_u64 = strtoull(val, NULL, 10); if (probenum < g_slist_length(sdi->channels)) { probe = g_slist_nth(sdi->channels, probenum)->data; sdi->driver->config_set(SR_CONF_STATUS_MIN, g_variant_new_uint64(tmp_u64), sdi, probe, NULL); } } } g_strfreev(keys); } devcnt++; } g_strfreev(sections); g_key_file_free(kf); return SR_OK; }
static void run_session(void) { struct sr_dev *dev; GHashTable *devargs; int num_devs, max_probes, i; uint64_t time_msec; char **probelist, *devspec; devargs = NULL; if (opt_dev) { devargs = parse_generic_arg(opt_dev); devspec = g_hash_table_lookup(devargs, "sigrok_key"); dev = parse_devstring(devspec); if (!dev) { g_critical("Device not found."); return; } g_hash_table_remove(devargs, "sigrok_key"); } else { num_devs = num_real_devs(); if (num_devs == 1) { /* No device specified, but there is only one. */ devargs = NULL; dev = parse_devstring("0"); } else if (num_devs == 0) { g_critical("No devices found."); return; } else { g_critical("%d devices found, please select one.", num_devs); return; } } sr_session_new(); sr_session_datafeed_callback_add(datafeed_in); if (sr_session_dev_add(dev) != SR_OK) { g_critical("Failed to use device."); sr_session_destroy(); return; } if (devargs) { if (set_dev_options(dev, devargs) != SR_OK) { sr_session_destroy(); return; } g_hash_table_destroy(devargs); } if (select_probes(dev) != SR_OK) return; if (opt_continuous) { if (!sr_driver_hwcap_exists(dev->driver, SR_HWCAP_CONTINUOUS)) { g_critical("This device does not support continuous sampling."); sr_session_destroy(); return; } } if (opt_triggers) { probelist = sr_parse_triggerstring(dev, opt_triggers); if (!probelist) { sr_session_destroy(); return; } max_probes = g_slist_length(dev->probes); for (i = 0; i < max_probes; i++) { if (probelist[i]) { sr_dev_trigger_set(dev, i + 1, probelist[i]); g_free(probelist[i]); } } g_free(probelist); } if (opt_time) { time_msec = sr_parse_timestring(opt_time); if (time_msec == 0) { g_critical("Invalid time '%s'", opt_time); sr_session_destroy(); return; } if (sr_driver_hwcap_exists(dev->driver, SR_HWCAP_LIMIT_MSEC)) { if (dev->driver->dev_config_set(dev->driver_index, SR_HWCAP_LIMIT_MSEC, &time_msec) != SR_OK) { g_critical("Failed to configure time limit."); sr_session_destroy(); return; } } else { /* time limit set, but device doesn't support this... * convert to samples based on the samplerate. */ limit_samples = 0; if (sr_dev_has_hwcap(dev, SR_HWCAP_SAMPLERATE)) { const uint64_t *samplerate; sr_dev_info_get(dev, SR_DI_CUR_SAMPLERATE, (const void **)&samplerate); limit_samples = (*samplerate) * time_msec / (uint64_t)1000; } if (limit_samples == 0) { g_critical("Not enough time at this samplerate."); sr_session_destroy(); return; } if (dev->driver->dev_config_set(dev->driver_index, SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK) { g_critical("Failed to configure time-based sample limit."); sr_session_destroy(); return; } } } if (opt_samples) { if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK) || (dev->driver->dev_config_set(dev->driver_index, SR_HWCAP_LIMIT_SAMPLES, &limit_samples) != SR_OK)) { g_critical("Failed to configure sample limit."); sr_session_destroy(); return; } } if (opt_frames) { if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK) || (dev->driver->dev_config_set(dev->driver_index, SR_HWCAP_LIMIT_FRAMES, &limit_frames) != SR_OK)) { printf("Failed to configure frame limit.\n"); sr_session_destroy(); return; } } if (dev->driver->dev_config_set(dev->driver_index, SR_HWCAP_PROBECONFIG, (char *)dev->probes) != SR_OK) { g_critical("Failed to configure probes."); sr_session_destroy(); return; } if (sr_session_start() != SR_OK) { g_critical("Failed to start session."); sr_session_destroy(); return; } if (opt_continuous) add_anykey(); sr_session_run(); if (opt_continuous) clear_anykey(); if (opt_output_file && default_output_format) { if (sr_session_save(opt_output_file) != SR_OK) g_critical("Failed to save session."); } sr_session_destroy(); }